Skip to content

Commit

Permalink
Merge pull request #3743 from greymistcube/refactor/world-tests
Browse files Browse the repository at this point in the history
♻️ Refactor `IWorld` tests
  • Loading branch information
greymistcube authored Apr 15, 2024
2 parents 8c6eba7 + d3b526c commit ad6b761
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 188 deletions.
163 changes: 124 additions & 39 deletions Libplanet.Tests/Action/WorldTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using Libplanet.Action;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -187,7 +200,7 @@ public virtual void FungibleAssets()
}

[Fact]
public virtual void TransferAsset()
public void TransferAsset()
{
Assert.Throws<ArgumentOutOfRangeException>(() =>
_initWorld.TransferAsset(_initContext, _addr[0], _addr[1], Value(0, 0)));
Expand All @@ -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());
Expand All @@ -216,27 +239,24 @@ 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 },
miner: privateKey.PublicKey,
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
Expand All @@ -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 },
Expand All @@ -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
Expand All @@ -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]
Expand Down Expand Up @@ -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<TotalSupplyNotTrackableException>(() =>
world.GetTotalSupply(_currencies[0]));

Assert.Equal(
Value(4, 0),
_initWorld.GetTotalSupply(_currencies[4]));

world = world.MintAsset(context, _addr[0], Value(0, 10));
Assert.Throws<TotalSupplyNotTrackableException>(() =>
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) =>
Expand Down
59 changes: 0 additions & 59 deletions Libplanet.Tests/Action/WorldV0Test.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
}
}
}
Loading

0 comments on commit ad6b761

Please sign in to comment.