diff --git a/Libplanet.Tests/Action/WorldTest.cs b/Libplanet.Tests/Action/WorldTest.cs index 1e69c721d3a..a32979c6747 100644 --- a/Libplanet.Tests/Action/WorldTest.cs +++ b/Libplanet.Tests/Action/WorldTest.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Linq; using System.Numerics; using Libplanet.Action; @@ -97,8 +96,11 @@ protected WorldTest(ITestOutputHelper output) Currencies.CurrencyF, }; - // FIXME: Should be tested on both legacy and modern. - _initWorld = new World(MockWorldState.CreateLegacy() + MockWorldState initMockWorldState = + ProtocolVersion >= BlockMetadata.WorldStateProtocolVersion + ? MockWorldState.CreateModern(version: ProtocolVersion) + : MockWorldState.CreateLegacy(); + _initWorld = new World(initMockWorldState .SetBalance(_addr[0], _currencies[0], 5) .SetBalance(_addr[0], _currencies[2], 10) .SetBalance(_addr[0], _currencies[4], 5) @@ -131,7 +133,18 @@ protected WorldTest(ITestOutputHelper output) public abstract int ProtocolVersion { get; } - public abstract IActionContext CreateContext(IWorld world, Address signer); + public IActionContext CreateContext(IWorld world, Address signer) + { + return new ActionContext( + signer, + null, + signer, + 0, + ProtocolVersion, + world, + 0, + 0); + } [Fact] public void InitialSetup() @@ -187,7 +200,7 @@ public virtual void FungibleAssets() } [Fact] - public virtual void TransferAsset() + public void TransferAsset() { Assert.Throws(() => _initWorld.TransferAsset(_initContext, _addr[0], _addr[1], Value(0, 0))); @@ -199,10 +212,20 @@ public virtual void TransferAsset() IWorld world = _initWorld.TransferAsset(_initContext, _addr[0], _addr[1], Value(0, 4)); Assert.Equal(Value(0, 1), world.GetBalance(_addr[0], _currencies[0])); Assert.Equal(Value(0, 4), world.GetBalance(_addr[1], _currencies[0])); + + world = _initWorld.TransferAsset(_initContext, _addr[0], _addr[0], Value(0, 2)); + if (ProtocolVersion >= BlockMetadata.TransferFixProtocolVersion) + { + Assert.Equal(Value(0, 5), world.GetBalance(_addr[0], _currencies[0])); + } + else + { + Assert.Equal(Value(0, 7), world.GetBalance(_addr[0], _currencies[0])); + } } [Fact] - public virtual BlockChain TransferAssetInBlock() + public void TransferAssetInBlock() { var store = new MemoryStore(); var stateStore = new TrieStateStore(new MemoryKeyValueStore()); @@ -216,14 +239,13 @@ public virtual BlockChain TransferAssetInBlock() privateKey: privateKey ); + // Mint DumbAction action = DumbAction.Create(null, (null, _addr[1], 20)); Transaction tx = Transaction.Create( 0, _keys[0], chain.Genesis.Hash, - new[] { action }.ToPlainValues() - ); - + new[] { action }.ToPlainValues()); var block1PreEval = TestUtils.ProposeNext( chain.Tip, new[] { tx }, @@ -231,12 +253,10 @@ public virtual BlockChain TransferAssetInBlock() protocolVersion: ProtocolVersion); var stateRootHash = chain.DetermineBlockStateRootHash(block1PreEval, out _); var hash = block1PreEval.Header.DeriveBlockHash(stateRootHash, null); - Block block1 = ProtocolVersion < 2 - ? new Block(block1PreEval, (stateRootHash, null, hash)) - : chain.EvaluateAndSign(block1PreEval, privateKey); - chain.Append( - block1, - TestUtils.CreateBlockCommit(block1)); + Block block1 = ProtocolVersion >= BlockMetadata.SignatureProtocolVersion + ? chain.EvaluateAndSign(block1PreEval, privateKey) + : new Block(block1PreEval, (stateRootHash, null, hash)); + chain.Append(block1, TestUtils.CreateBlockCommit(block1)); Assert.Equal( DumbAction.DumbCurrency * 0, chain @@ -248,13 +268,13 @@ public virtual BlockChain TransferAssetInBlock() .GetWorldState() .GetBalance(_addr[1], DumbAction.DumbCurrency)); + // Transfer action = DumbAction.Create(null, (_addr[1], _addr[0], 5)); tx = Transaction.Create( 1, _keys[0], chain.Genesis.Hash, - new[] { action }.ToPlainValues() - ); + new[] { action }.ToPlainValues()); var block2PreEval = TestUtils.ProposeNext( chain.Tip, new[] { tx }, @@ -263,12 +283,10 @@ public virtual BlockChain TransferAssetInBlock() lastCommit: chain.GetBlockCommit(chain.Tip.Index)); stateRootHash = chain.DetermineBlockStateRootHash(block2PreEval, out _); hash = block2PreEval.Header.DeriveBlockHash(stateRootHash, null); - Block block2 = ProtocolVersion < 2 - ? new Block(block2PreEval, (stateRootHash, null, hash)) - : chain.EvaluateAndSign(block2PreEval, privateKey); - chain.Append( - block2, - TestUtils.CreateBlockCommit(block2)); + Block block2 = ProtocolVersion >= BlockMetadata.SignatureProtocolVersion + ? chain.EvaluateAndSign(block2PreEval, privateKey) + : new Block(block2PreEval, (stateRootHash, null, hash)); + chain.Append(block2, TestUtils.CreateBlockCommit(block2)); Assert.Equal( DumbAction.DumbCurrency * 5, chain @@ -280,7 +298,37 @@ public virtual BlockChain TransferAssetInBlock() .GetWorldState() .GetBalance(_addr[1], DumbAction.DumbCurrency)); - return chain; + // Transfer bugged + action = DumbAction.Create((_addr[0], "a"), (_addr[0], _addr[0], 1)); + tx = Transaction.Create( + chain.GetNextTxNonce(_addr[0]), + _keys[0], + chain.Genesis.Hash, + new[] { action }.ToPlainValues()); + var block3PreEval = TestUtils.ProposeNext( + chain.Tip, + new[] { tx }, + miner: _keys[1].PublicKey, + protocolVersion: ProtocolVersion, + lastCommit: chain.GetBlockCommit(chain.Tip.Index)); + stateRootHash = chain.DetermineBlockStateRootHash(block3PreEval, out _); + hash = block3PreEval.Header.DeriveBlockHash(stateRootHash, null); + Block block3 = ProtocolVersion >= BlockMetadata.SignatureProtocolVersion + ? chain.EvaluateAndSign(block3PreEval, _keys[1]) + : new Block(block3PreEval, (stateRootHash, null, hash)); + chain.Append(block3, TestUtils.CreateBlockCommit(block3)); + if (ProtocolVersion >= BlockMetadata.TransferFixProtocolVersion) + { + Assert.Equal( + DumbAction.DumbCurrency * 5, + chain.GetWorldState().GetBalance(_addr[0], DumbAction.DumbCurrency)); + } + else + { + Assert.Equal( + DumbAction.DumbCurrency * 6, + chain.GetWorldState().GetBalance(_addr[0], DumbAction.DumbCurrency)); + } } [Fact] @@ -366,21 +414,58 @@ public virtual void BurnAsset() [Fact] public void SetValidatorSet() { - const int newValidatorCount = 6; - var world = _initWorld; - var keys = Enumerable - .Range(0, newValidatorCount) - .Select(i => new PrivateKey()) - .ToList(); - var validatorSet = new ValidatorSet( - keys.Select(key => new Validator(key.PublicKey, 1)).ToList()); - world = world.SetValidatorSet(validatorSet); - - Assert.Equal(newValidatorCount, world.GetValidatorSet().TotalCount); - Assert.NotEqual(_initWorld.GetValidatorSet(), world.GetValidatorSet()); - - world = world.SetValidatorSet(new ValidatorSet()); - Assert.Equal(0, world.GetValidatorSet().TotalCount); + if (ProtocolVersion > BlockMetadata.PBFTProtocolVersion) + { + const int newValidatorCount = 6; + var world = _initWorld; + var keys = Enumerable + .Range(0, newValidatorCount) + .Select(i => new PrivateKey()) + .ToList(); + var validatorSet = new ValidatorSet( + keys.Select(key => new Validator(key.PublicKey, 1)).ToList()); + world = world.SetValidatorSet(validatorSet); + + Assert.Equal(newValidatorCount, world.GetValidatorSet().TotalCount); + Assert.NotEqual(_initWorld.GetValidatorSet(), world.GetValidatorSet()); + + world = world.SetValidatorSet(new ValidatorSet()); + Assert.Equal(0, world.GetValidatorSet().TotalCount); + } + } + + [Fact] + public void TotalSupplyTracking() + { + // While not specifically tied to a protocol version, + // Total supply tracking was introduced while the block protocol version + // was at 4. + if (ProtocolVersion > BlockMetadata.PBFTProtocolVersion) + { + IWorld world = _initWorld; + IActionContext context = _initContext; + + Assert.Throws(() => + world.GetTotalSupply(_currencies[0])); + + Assert.Equal( + Value(4, 0), + _initWorld.GetTotalSupply(_currencies[4])); + + world = world.MintAsset(context, _addr[0], Value(0, 10)); + Assert.Throws(() => + world.GetTotalSupply(_currencies[0])); + + world = world.MintAsset(context, _addr[0], Value(4, 10)); + Assert.Equal( + Value(4, 10), + world.GetTotalSupply(_currencies[4])); + + world = world.BurnAsset(context, _addr[0], Value(4, 5)); + Assert.Equal( + Value(4, 5), + world.GetTotalSupply(_currencies[4])); + } } protected FungibleAssetValue Value(int currencyIndex, BigInteger quantity) => diff --git a/Libplanet.Tests/Action/WorldV0Test.cs b/Libplanet.Tests/Action/WorldV0Test.cs index a9636fc64ff..d7eb9f4bd99 100644 --- a/Libplanet.Tests/Action/WorldV0Test.cs +++ b/Libplanet.Tests/Action/WorldV0Test.cs @@ -1,11 +1,3 @@ -using Libplanet.Action; -using Libplanet.Action.State; -using Libplanet.Action.Tests.Common; -using Libplanet.Blockchain; -using Libplanet.Crypto; -using Libplanet.Types.Blocks; -using Libplanet.Types.Tx; -using Xunit; using Xunit.Abstractions; namespace Libplanet.Tests.Action @@ -18,56 +10,5 @@ public WorldV0Test(ITestOutputHelper output) } public override int ProtocolVersion { get; } = 0; - - public override IActionContext CreateContext(IWorld world, Address signer) - { - return new ActionContext( - signer, - null, - signer, - 0, - ProtocolVersion, - world, - 0, - 0); - } - - [Fact] - public override void TransferAsset() - { - base.TransferAsset(); - - IWorld world = _initWorld.TransferAsset(_initContext, _addr[0], _addr[0], Value(0, 2)); - Assert.Equal(Value(0, 7), world.GetBalance(_addr[0], _currencies[0])); - } - - [Fact] - public override BlockChain TransferAssetInBlock() - { - BlockChain chain = base.TransferAssetInBlock(); - - DumbAction action = DumbAction.Create((_addr[0], "a"), (_addr[0], _addr[0], 1)); - Transaction tx = Transaction.Create( - chain.GetNextTxNonce(_addr[0]), - _keys[0], - chain.Genesis.Hash, - new[] { action }.ToPlainValues() - ); - PreEvaluationBlock preEval = TestUtils.ProposeNext( - chain.Tip, - new[] { tx }, - miner: _keys[1].PublicKey, - protocolVersion: ProtocolVersion - ); - var stateRootHash = chain.DetermineBlockStateRootHash(preEval, out _); - var hash = preEval.Header.DeriveBlockHash(stateRootHash, null); - Block block = new Block(preEval, (stateRootHash, null, hash)); - chain.Append(block, TestUtils.CreateBlockCommit(block)); - Assert.Equal( - DumbAction.DumbCurrency * 6, - chain.GetWorldState().GetBalance(_addr[0], DumbAction.DumbCurrency)); - - return chain; - } } } diff --git a/Libplanet.Tests/Action/WorldV1Test.cs b/Libplanet.Tests/Action/WorldV1Test.cs index 4782ff082ce..9782b26da71 100644 --- a/Libplanet.Tests/Action/WorldV1Test.cs +++ b/Libplanet.Tests/Action/WorldV1Test.cs @@ -1,11 +1,4 @@ -using Libplanet.Action; -using Libplanet.Action.State; -using Libplanet.Action.Tests.Common; -using Libplanet.Blockchain; -using Libplanet.Crypto; using Libplanet.Types.Blocks; -using Libplanet.Types.Tx; -using Xunit; using Xunit.Abstractions; namespace Libplanet.Tests.Action @@ -17,88 +10,6 @@ public WorldV1Test(ITestOutputHelper output) { } - public override int ProtocolVersion { get; } = BlockMetadata.CurrentProtocolVersion; - - public override IActionContext CreateContext(IWorld world, Address signer) - { - return new ActionContext( - signer, - null, - signer, - 0, - ProtocolVersion, - world, - 0, - 0); - } - - [Fact] - public override void TransferAsset() - { - base.TransferAsset(); - - IWorld world = _initWorld.TransferAsset(_initContext, _addr[0], _addr[0], Value(0, 2)); - Assert.Equal(Value(0, 5), world.GetBalance(_addr[0], _currencies[0])); - } - - [Fact] - public override BlockChain TransferAssetInBlock() - { - BlockChain chain = base.TransferAssetInBlock(); - - DumbAction action = DumbAction.Create((_addr[0], "a"), (_addr[0], _addr[0], 1)); - Transaction tx = Transaction.Create( - chain.GetNextTxNonce(_addr[0]), - _keys[0], - chain.Genesis.Hash, - new[] { action }.ToPlainValues() - ); - Block block = chain.EvaluateAndSign( - TestUtils.ProposeNext( - chain.Tip, - new[] { tx }, - miner: _keys[1].PublicKey, - protocolVersion: ProtocolVersion, - lastCommit: TestUtils.CreateBlockCommit(chain.Tip)), - _keys[1]); - chain.Append( - block, - TestUtils.CreateBlockCommit(block) - ); - Assert.Equal( - DumbAction.DumbCurrency * 5, - chain.GetWorldState().GetBalance(_addr[0], DumbAction.DumbCurrency) - ); - - return chain; - } - - [Fact] - public void TotalSupplyTracking() - { - IWorld world = _initWorld; - IActionContext context = _initContext; - - Assert.Throws(() => - world.GetTotalSupply(_currencies[0])); - - Assert.Equal( - Value(4, 0), - _initWorld.GetTotalSupply(_currencies[4])); - - world = world.MintAsset(context, _addr[0], Value(0, 10)); - Assert.Throws(() => - world.GetTotalSupply(_currencies[0])); - - world = world.MintAsset(context, _addr[0], Value(4, 10)); - Assert.Equal( - Value(4, 10), - world.GetTotalSupply(_currencies[4])); - - world = world.BurnAsset(context, _addr[0], Value(4, 5)); - Assert.Equal( - Value(4, 5), - world.GetTotalSupply(_currencies[4])); - } + public override int ProtocolVersion { get; } = BlockMetadata.TransferFixProtocolVersion; } } diff --git a/Libplanet.Tests/Action/WorldV5Test.cs b/Libplanet.Tests/Action/WorldV5Test.cs new file mode 100644 index 00000000000..feea883cde0 --- /dev/null +++ b/Libplanet.Tests/Action/WorldV5Test.cs @@ -0,0 +1,15 @@ +using Libplanet.Types.Blocks; +using Xunit.Abstractions; + +namespace Libplanet.Tests.Action +{ + public class WorldV5Test : WorldTest + { + public WorldV5Test(ITestOutputHelper output) + : base(output) + { + } + + public override int ProtocolVersion { get; } = BlockMetadata.WorldStateProtocolVersion; + } +}