Skip to content

Commit

Permalink
test: revive unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
limebell committed Jul 11, 2024
1 parent dc3450f commit 4a0996b
Showing 1 changed file with 138 additions and 0 deletions.
138 changes: 138 additions & 0 deletions Libplanet.Tests/Action/ActionEvaluatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,144 @@ public void OrderTxsForEvaluation(
.Select(tx => tx.Signer.ToString())));
}

[Fact]
public void EvaluateActionAndCollectFee()
{
var privateKey = new PrivateKey();
var address = privateKey.Address;
Currency foo = Currency.Uncapped(
"FOO",
18,
null
);

var freeGasAction = new UseGasAction()
{
GasUsage = 0,
Memo = "FREE",
MintValue = FungibleAssetValue.FromRawValue(foo, 10),
Receiver = address,
};

var payGasAction = new UseGasAction()
{
GasUsage = 1,
Memo = "CHARGE",
};

var store = new MemoryStore();
var stateStore = new TrieStateStore(new MemoryKeyValueStore());
var chain = TestUtils.MakeBlockChain(
policy: new BlockPolicy(),
actions: new[] { freeGasAction, },
store: store,
stateStore: stateStore,
actionLoader: new SingleActionLoader(typeof(UseGasAction)));
var tx = Transaction.Create(
nonce: 0,
privateKey: privateKey,
genesisHash: chain.Genesis.Hash,
maxGasPrice: FungibleAssetValue.FromRawValue(foo, 1),
gasLimit: 2,
actions: new[]
{
payGasAction,
}.ToPlainValues());

chain.StageTransaction(tx);
var miner = new PrivateKey();
Block block = chain.ProposeBlock(miner);

var evaluations = chain.ActionEvaluator.Evaluate(
block, chain.Store.GetNextStateRootHash((BlockHash)block.PreviousHash));

Assert.False(evaluations[0].InputContext.IsBlockAction);
Assert.Single(evaluations);
Assert.Null(evaluations.Single().Exception);
Assert.Equal(
FungibleAssetValue.FromRawValue(foo, 9),
chain
.GetWorldState(evaluations.Single().OutputState)
.GetBalance(address, foo));
Assert.Equal(
FungibleAssetValue.FromRawValue(foo, 1),
chain
.GetWorldState(evaluations.Single().OutputState)
.GetBalance(miner.Address, foo));
}

[Fact]
public void EvaluateThrowingExceedGasLimit()
{
var privateKey = new PrivateKey();
var address = privateKey.Address;
Currency foo = Currency.Uncapped(
"FOO",
18,
null
);

var freeGasAction = new UseGasAction()
{
GasUsage = 0,
Memo = "FREE",
MintValue = FungibleAssetValue.FromRawValue(foo, 10),
Receiver = address,
};

var payGasAction = new UseGasAction()
{
GasUsage = 10,
Memo = "CHARGE",
};

var store = new MemoryStore();
var stateStore = new TrieStateStore(new MemoryKeyValueStore());
var chain = TestUtils.MakeBlockChain(
policy: new BlockPolicy(),
actions: new[]
{
freeGasAction,
},
store: store,
stateStore: stateStore,
actionLoader: new SingleActionLoader(typeof(UseGasAction)));
var tx = Transaction.Create(
nonce: 0,
privateKey: privateKey,
genesisHash: chain.Genesis.Hash,
maxGasPrice: FungibleAssetValue.FromRawValue(foo, 1),
gasLimit: 5,
actions: new[]
{
payGasAction,
}.ToPlainValues());

chain.StageTransaction(tx);
var miner = new PrivateKey();
Block block = chain.ProposeBlock(miner);

var evaluations = chain.ActionEvaluator.Evaluate(
block,
chain.Store.GetNextStateRootHash((BlockHash)block.PreviousHash));

Assert.False(evaluations[0].InputContext.IsBlockAction);
Assert.Single(evaluations);
Assert.NotNull(evaluations.Single().Exception);
Assert.NotNull(evaluations.Single().Exception?.InnerException);
Assert.Equal(
typeof(GasLimitExceededException),
evaluations.Single().Exception?.InnerException?.GetType());
Assert.Equal(
FungibleAssetValue.FromRawValue(foo, 5),
chain.GetWorldState(evaluations.Single().OutputState)
.GetBalance(address, foo));
Assert.Equal(
FungibleAssetValue.FromRawValue(foo, 5),
chain.GetWorldState(evaluations.Single().OutputState)
.GetBalance(miner.Address, foo));
}

[Fact]
public void GenerateRandomSeed()
{
Expand Down

0 comments on commit 4a0996b

Please sign in to comment.