diff --git a/.Lib9c.Tests/Policy/BlockPolicyTest.cs b/.Lib9c.Tests/Policy/BlockPolicyTest.cs index 61dbd90d2d..7c5fa87ea1 100644 --- a/.Lib9c.Tests/Policy/BlockPolicyTest.cs +++ b/.Lib9c.Tests/Policy/BlockPolicyTest.cs @@ -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 + { + 1 * Currencies.Mead, + }, + }; + var mint2 = new PrepareRewardAssets + { + RewardPoolAddress = MeadConfig.PatronAddress, + Assets = new List + { + 1 * Currencies.Mead, + }, + }; Block genesis = MakeGenesisBlock( adminAddress, - ImmutableHashSet.Create(adminAddress), + ImmutableHashSet
.Empty, initialValidators: new Dictionary - { { 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)); @@ -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() ); + Assert.IsType(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(), - gasLimit: 1, - maxGasPrice: new FungibleAssetValue(Currencies.Mead, 0, 0) + actions: new ActionBase[] { action }.ToPlainValues() ); + Assert.IsType(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 - // { - // 1 * Currencies.Mead, - // }, - // }; - // var mint2 = new PrepareRewardAssets - // { - // RewardPoolAddress = MeadConfig.PatronAddress, - // Assets = new List - // { - // 1 * Currencies.Mead, - // }, - // }; - // Block genesis = MakeGenesisBlock( - // adminAddress, - // ImmutableHashSet
.Empty, - // initialValidators: new Dictionary - // { { 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() - // ); - // Assert.IsType(BlockPolicySource.ValidateNextBlockTxRaw(blockChain, actionTypeLoader, txEmpty)); - // - // Transaction txByAdmin = - // Transaction.Create( - // 0, - // adminPrivateKey, - // genesis.Hash, - // new ActionBase[] { action, action }.ToPlainValues() - // ); - // Assert.IsType(BlockPolicySource.ValidateNextBlockTxRaw(blockChain, actionTypeLoader, txByAdmin)); - // - // Transaction txByStranger = - // Transaction.Create( - // 0, - // new PrivateKey(), - // genesis.Hash, - // new ActionBase[] { action }.ToPlainValues() - // ); - // Assert.IsType(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(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() { diff --git a/Lib9c.Policy/Policy/BlockPolicySource.cs b/Lib9c.Policy/Policy/BlockPolicySource.cs index 2e16611b75..bf87b2b690 100644 --- a/Lib9c.Policy/Policy/BlockPolicySource.cs +++ b/Lib9c.Policy/Policy/BlockPolicySource.cs @@ -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))