Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix not to use GetNextWorldState() on tx validation #2661

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions .Lib9c.Tests/Policy/BlockPolicyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ public void ValidateNextBlockTx()
0,
newActivatedPrivateKey,
genesis.Hash,
Array.Empty<IValue>()
Array.Empty<IValue>(),
gasLimit: 1,
maxGasPrice: new FungibleAssetValue(Currencies.Mead, 0, 0)
);

// Test success because the key is activated.
Expand All @@ -120,14 +122,18 @@ public void ValidateNextBlockTx()
0,
newActivatedPrivateKey,
genesis.Hash,
singleAction.ToPlainValues()
singleAction.ToPlainValues(),
gasLimit: 1,
maxGasPrice: new FungibleAssetValue(Currencies.Mead, 0, 0)
);
Transaction txWithManyActions =
Transaction.Create(
0,
newActivatedPrivateKey,
genesis.Hash,
manyActions.ToPlainValues()
manyActions.ToPlainValues(),
gasLimit: 1,
maxGasPrice: new FungibleAssetValue(Currencies.Mead, 0, 0)
);

// Transaction with more than two actions is rejected.
Expand Down Expand Up @@ -450,7 +456,9 @@ List<Transaction> GenerateTransactions(int count)
nonce++,
adminPrivateKey,
genesis.Hash,
Array.Empty<IValue>()
Array.Empty<IValue>(),
gasLimit: 1,
maxGasPrice: new FungibleAssetValue(Currencies.Mead, 0, 0)
));
}

Expand Down Expand Up @@ -552,7 +560,9 @@ List<Transaction> GenerateTransactions(int count)
nonce++,
adminPrivateKey,
genesis.Hash,
Array.Empty<IValue>()
Array.Empty<IValue>(),
gasLimit: 1,
maxGasPrice: new FungibleAssetValue(Currencies.Mead, 0, 0)
));
}

Expand Down
59 changes: 2 additions & 57 deletions Lib9c.Policy/Policy/BlockPolicySource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,69 +174,14 @@ internal IBlockPolicy GetPolicy(
try
{
if (blockChain
.GetNextWorldState()!
.GetWorldState()
.GetBalance(MeadConfig.PatronAddress, Currencies.Mead) < 1 * Currencies.Mead)
{
// Check Activation
try
{
if (transaction.Actions is { } rawActions &&
rawActions.Count == 1 &&
actionLoader.LoadAction(index, rawActions.First()) is ActionBase action &&
action is IActivateAccount activate)
{
return transaction.Nonce == 0 &&
blockChain
.GetNextWorldState()!
.GetAccountState(ReservedAddresses.LegacyAccount)
.GetState(activate.PendingAddress) is Dictionary rawPending &&
new PendingActivationState(rawPending).Verify(activate.Signature)
? null
: new TxPolicyViolationException(
$"Transaction {transaction.Id} has an invalid activate action.",
transaction.Id);
}
}
catch (Exception e)
{
return new TxPolicyViolationException(
$"Transaction {transaction.Id} has an invalid action.",
transaction.Id,
e);
}

// Check admin
if (IsAdminTransaction(blockChain, transaction))
{
return null;
}

switch (blockChain
.GetNextWorldState()!
.GetAccountState(ReservedAddresses.LegacyAccount)
.GetState(transaction.Signer.Derive(ActivationKey.DeriveKey)))
{
case null:
// Fallback for pre-migration.
if (blockChain
.GetNextWorldState()!
.GetAccountState(ReservedAddresses.LegacyAccount)
.GetState(ActivatedAccountsState.Address) is Dictionary asDict)
{
IImmutableSet<Address> activatedAccounts =
new ActivatedAccountsState(asDict).Accounts;
return !activatedAccounts.Any() ||
activatedAccounts.Contains(transaction.Signer)
? null
: new TxPolicyViolationException(
$"Transaction {transaction.Id} is by a signer " +
$"without account activation: {transaction.Signer}",
transaction.Id);
}
return null;
case Bencodex.Types.Boolean _:
return null;
}
}
}

if (!(transaction.MaxGasPrice is { } gasPrice && transaction.GasLimit is { } gasLimit))
Expand Down
Loading