Skip to content

Commit

Permalink
Merge pull request #2662 from OnedgeLee/fix/block-policy-source
Browse files Browse the repository at this point in the history
Fix block policy source
  • Loading branch information
OnedgeLee authored Jul 2, 2024
2 parents f5f370f + a2a60c8 commit 66c9bf1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 171 deletions.
227 changes: 61 additions & 166 deletions .Lib9c.Tests/Policy/BlockPolicyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,37 @@ public BlockPolicyTest()
}

[Fact]
public void ValidateNextBlockTx()
public void ValidateNextBlockTx_Mead()
{
var adminPrivateKey = new PrivateKey();
var adminAddress = adminPrivateKey.Address;

var blockPolicySource = new BlockPolicySource();
var actionTypeLoader = new NCActionLoader();
IBlockPolicy policy = blockPolicySource.GetPolicy(null, null, null, null);
IStagePolicy stagePolicy = new VolatileStagePolicy();
var mint = new PrepareRewardAssets
{
RewardPoolAddress = adminAddress,
Assets = new List<FungibleAssetValue>
{
1 * Currencies.Mead,
},
};
var mint2 = new PrepareRewardAssets
{
RewardPoolAddress = MeadConfig.PatronAddress,
Assets = new List<FungibleAssetValue>
{
1 * Currencies.Mead,
},
};
Block genesis = MakeGenesisBlock(
adminAddress,
ImmutableHashSet.Create(adminAddress),
ImmutableHashSet<Address>.Empty,
initialValidators: new Dictionary<PublicKey, BigInteger>
{ { adminPrivateKey.PublicKey, BigInteger.One } }
{ { adminPrivateKey.PublicKey, BigInteger.One } },
actionBases: new[] { mint, mint2 },
privateKey: adminPrivateKey
);
using var store = new DefaultStore(null);
using var stateStore = new TrieStateStore(new DefaultKeyValueStore(null));
Expand All @@ -73,189 +91,66 @@ public void ValidateNextBlockTx()
),
renderers: new[] { new BlockRenderer() }
);
Transaction txByStranger =

Block block = blockChain.ProposeBlock(adminPrivateKey);
blockChain.Append(block, GenerateBlockCommit(block, adminPrivateKey));

Assert.Equal(
1 * Currencies.Mead,
blockChain
.GetWorldState()
.GetBalance(adminAddress, Currencies.Mead));
Assert.Equal(
1 * Currencies.Mead,
blockChain
.GetWorldState()
.GetBalance(MeadConfig.PatronAddress, Currencies.Mead));
var action = new DailyReward
{
avatarAddress = adminAddress,
};

Transaction txEmpty =
Transaction.Create(
0,
new PrivateKey(),
adminPrivateKey,
genesis.Hash,
Array.Empty<IValue>()
);
Assert.IsType<TxPolicyViolationException>(BlockPolicySource.ValidateNextBlockTxRaw(blockChain, actionTypeLoader, txEmpty));

// New private key which is not in activated addresses list is blocked.
Assert.NotNull(policy.ValidateNextBlockTx(blockChain, txByStranger));

var newActivatedPrivateKey = new PrivateKey();
var newActivatedAddress = newActivatedPrivateKey.Address;

// Activate with admin account.
blockChain.MakeTransaction(
adminPrivateKey,
new ActionBase[] { new AddActivatedAccount(newActivatedAddress) }
);
Block block = blockChain.ProposeBlock(adminPrivateKey);
blockChain.Append(block, GenerateBlockCommit(block, adminPrivateKey));

Transaction txByNewActivated =
Transaction tx1 =
Transaction.Create(
0,
newActivatedPrivateKey,
adminPrivateKey,
genesis.Hash,
Array.Empty<IValue>(),
gasLimit: 1,
maxGasPrice: new FungibleAssetValue(Currencies.Mead, 0, 0)
actions: new ActionBase[] { action }.ToPlainValues()
);
Assert.IsType<TxPolicyViolationException>(BlockPolicySource.ValidateNextBlockTxRaw(blockChain, actionTypeLoader, tx1));

// Test success because the key is activated.
Assert.Null(policy.ValidateNextBlockTx(blockChain, txByNewActivated));

var singleAction = new ActionBase[]
{
new DailyReward(),
};
var manyActions = new ActionBase[]
{
new DailyReward(),
new DailyReward(),
};
Transaction txWithSingleAction =
Transaction tx2 =
Transaction.Create(
0,
newActivatedPrivateKey,
1,
adminPrivateKey,
genesis.Hash,
singleAction.ToPlainValues(),
gasLimit: 1,
maxGasPrice: new FungibleAssetValue(Currencies.Mead, 0, 0)
maxGasPrice: new FungibleAssetValue(Currencies.Mead, 10, 10),
actions: new ActionBase[] { action }.ToPlainValues()
);
Transaction txWithManyActions =
Assert.Null(BlockPolicySource.ValidateNextBlockTxRaw(blockChain, actionTypeLoader, tx2));

Transaction tx3 =
Transaction.Create(
0,
newActivatedPrivateKey,
2,
adminPrivateKey,
genesis.Hash,
manyActions.ToPlainValues(),
gasLimit: 1,
maxGasPrice: new FungibleAssetValue(Currencies.Mead, 0, 0)
maxGasPrice: new FungibleAssetValue(Currencies.Mead, 0, 0),
actions: new ActionBase[] { action }.ToPlainValues()
);

// Transaction with more than two actions is rejected.
Assert.Null(policy.ValidateNextBlockTx(blockChain, txWithSingleAction));
Assert.NotNull(policy.ValidateNextBlockTx(blockChain, txWithManyActions));
Assert.Null(BlockPolicySource.ValidateNextBlockTxRaw(blockChain, actionTypeLoader, tx3));
}

// FIXME restore this test
// [Fact]
// public void ValidateNextBlockTx_Mead()
// {
// var adminPrivateKey = new PrivateKey();
// var adminAddress = adminPrivateKey.Address;
// var blockPolicySource = new BlockPolicySource();
// var actionTypeLoader = new NCActionLoader();
// IBlockPolicy policy = blockPolicySource.GetPolicy(null, null, null, null);
// IStagePolicy stagePolicy = new VolatileStagePolicy();
// var mint = new PrepareRewardAssets
// {
// RewardPoolAddress = adminAddress,
// Assets = new List<FungibleAssetValue>
// {
// 1 * Currencies.Mead,
// },
// };
// var mint2 = new PrepareRewardAssets
// {
// RewardPoolAddress = MeadConfig.PatronAddress,
// Assets = new List<FungibleAssetValue>
// {
// 1 * Currencies.Mead,
// },
// };
// Block genesis = MakeGenesisBlock(
// adminAddress,
// ImmutableHashSet<Address>.Empty,
// initialValidators: new Dictionary<PublicKey, BigInteger>
// { { adminPrivateKey.PublicKey, BigInteger.One } },
// actionBases: new[] { mint, mint2 },
// privateKey: adminPrivateKey
// );
// using var store = new DefaultStore(null);
// using var stateStore = new TrieStateStore(new DefaultKeyValueStore(null));
// var blockChain = BlockChain.Create(
// policy,
// stagePolicy,
// store,
// stateStore,
// genesis,
// new ActionEvaluator(
// policyBlockActionGetter: _ => policy.BlockAction,
// stateStore: stateStore,
// actionTypeLoader: new NCActionLoader()
// ),
// renderers: new[] { new BlockRenderer() }
// );
// Assert.Equal(
// 1 * Currencies.Mead,
// blockChain
// .GetWorldState()
// .GetAccountState(ReservedAddresses.LegacyAccount)
// .GetBalance(adminAddress, Currencies.Mead));
// Assert.Equal(
// 1 * Currencies.Mead,
// blockChain
// .GetWorldState()
// .GetAccountState(ReservedAddresses.LegacyAccount)
// .GetBalance(MeadConfig.PatronAddress, Currencies.Mead));
// var action = new DailyReward
// {
// avatarAddress = adminAddress,
// };
//
// Transaction txEmpty =
// Transaction.Create(
// 0,
// adminPrivateKey,
// genesis.Hash,
// Array.Empty<IValue>()
// );
// Assert.IsType<TxPolicyViolationException>(BlockPolicySource.ValidateNextBlockTxRaw(blockChain, actionTypeLoader, txEmpty));
//
// Transaction txByAdmin =
// Transaction.Create(
// 0,
// adminPrivateKey,
// genesis.Hash,
// new ActionBase[] { action, action }.ToPlainValues()
// );
// Assert.IsType<TxPolicyViolationException>(BlockPolicySource.ValidateNextBlockTxRaw(blockChain, actionTypeLoader, txByAdmin));
//
// Transaction txByStranger =
// Transaction.Create(
// 0,
// new PrivateKey(),
// genesis.Hash,
// new ActionBase[] { action }.ToPlainValues()
// );
// Assert.IsType<TxPolicyViolationException>(BlockPolicySource.ValidateNextBlockTxRaw(blockChain, actionTypeLoader, txByStranger));
//
// Transaction txByAdmin2 =
// Transaction.Create(
// 1,
// adminPrivateKey,
// genesis.Hash,
// gasLimit: 1,
// maxGasPrice: new FungibleAssetValue(Currencies.Mead, 10, 10),
// actions: new ActionBase[] { action }.ToPlainValues()
// );
// Assert.IsType<TxPolicyViolationException>(BlockPolicySource.ValidateNextBlockTxRaw(blockChain, actionTypeLoader, txByAdmin2));
//
// Transaction txByAdmin3 =
// Transaction.Create(
// 2,
// adminPrivateKey,
// genesis.Hash,
// gasLimit: 1,
// maxGasPrice: new FungibleAssetValue(Currencies.Mead, 0, 0),
// actions: new ActionBase[] { action }.ToPlainValues()
// );
// Assert.Null(BlockPolicySource.ValidateNextBlockTxRaw(blockChain, actionTypeLoader, txByAdmin3));
// }
[Fact]
public void BlockCommitFromNonValidator()
{
Expand Down
8 changes: 3 additions & 5 deletions Lib9c.Policy/Policy/BlockPolicySource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,13 @@ internal IBlockPolicy GetPolicy(

try
{
// This block is used to bypass transactions before mead has been created.
// It's no longer required for future transactions, so better to be removed later.
if (blockChain
.GetWorldState()
.GetBalance(MeadConfig.PatronAddress, Currencies.Mead) < 1 * Currencies.Mead)
{
// Check admin
if (IsAdminTransaction(blockChain, transaction))
{
return null;
}
return null;
}

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

0 comments on commit 66c9bf1

Please sign in to comment.