Skip to content

Commit

Permalink
test: Add test code
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Oct 25, 2024
1 parent ba85b11 commit da0de9a
Show file tree
Hide file tree
Showing 5 changed files with 602 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .Lib9c.Tests/Action/ActionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class ActionContext : IActionContext

public FungibleAssetValue? MaxGasPrice { get; set; }

public long? GasLimit { get; set; }

public IReadOnlyList<ITransaction> Txs
{
get => _txs ?? ImmutableList<ITransaction>.Empty;
Expand Down
149 changes: 149 additions & 0 deletions .Lib9c.Tests/Action/ValidatorDelegation/GasTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#nullable enable
namespace Lib9c.Tests.Action.ValidatorDelegation;

using System;
using System.Linq;
using Libplanet.Action;
using Libplanet.Crypto;
using Nekoyume.Action;
using Xunit;

public class GasTest : TxAcitonTestBase
{
[Theory]
[InlineData(0, 0, 4)]
[InlineData(1, 1, 4)]
[InlineData(4, 4, 4)]
[InlineData(long.MaxValue, 4, 4)]
[InlineData(long.MaxValue, 0, 4)]
[InlineData(long.MaxValue, 1, 4)]
public void Execute(long gasLimit, long gasConsumption, long gasOwned)
{
if (gasLimit < gasConsumption)
{
throw new ArgumentOutOfRangeException(
nameof(gasLimit),
$"{nameof(gasLimit)} must be greater than or equal to {nameof(gasConsumption)}.");
}

if (gasOwned < gasConsumption)
{
throw new ArgumentOutOfRangeException(
nameof(gasOwned),
$"{nameof(gasOwned)} must be greater than or equal to {nameof(gasConsumption)}.");
}

// Given
var signerKey = new PrivateKey();
var signerMead = Mead * gasOwned;
EnsureToMintAsset(signerKey, signerMead);

// When
var expectedMead = signerMead - Mead * gasConsumption;
var gasAction = new GasAction { Consumption = gasConsumption };
MakeTransaction(
signerKey,
new ActionBase[] { gasAction, },
maxGasPrice: Mead * 1,
gasLimit: gasLimit);
MoveToNextBlock(throwOnError: true);

// Then
var actualMead = GetBalance(signerKey.Address, Mead);
Assert.Equal(expectedMead, actualMead);
}

[Theory]
[InlineData(1, 1, 0)]
[InlineData(4, 4, 0)]
[InlineData(4, 4, 1)]
[InlineData(long.MaxValue, 4, 0)]
[InlineData(long.MaxValue, 4, 1)]
[InlineData(long.MaxValue, 1, 0)]
public void Execute_InsufficientMead_Throw(
long gasLimit, long gasConsumption, long gasOwned)
{
if (gasLimit < gasConsumption)
{
throw new ArgumentOutOfRangeException(
nameof(gasLimit),
$"{nameof(gasLimit)} must be greater than or equal to {nameof(gasConsumption)}.");
}

if (gasOwned >= gasConsumption)
{
throw new ArgumentOutOfRangeException(
nameof(gasOwned),
$"{nameof(gasOwned)} must be less than {nameof(gasConsumption)}.");
}

// Given
var signerKey = new PrivateKey();
var signerMead = Mead * gasOwned;
EnsureToMintAsset(signerKey, signerMead);

// When
var expectedMead = Mead * 0;
var gasAction = new GasAction { Consumption = gasConsumption };
MakeTransaction(
signerKey,
new ActionBase[] { gasAction, },
maxGasPrice: Mead * 1,
gasLimit: gasLimit);

// Then
var e = Assert.Throws<AggregateException>(() => MoveToNextBlock(throwOnError: true));
var innerExceptions = e.InnerExceptions
.Cast<UnexpectedlyTerminatedActionException>()
.ToArray();
var actualMead = GetBalance(signerKey.Address, Mead);
Assert.Single(innerExceptions);
Assert.Contains(innerExceptions, i => i.Action is GasAction);
Assert.Equal(expectedMead, actualMead);
}

[Theory]
[InlineData(4, 5, 5)]
[InlineData(1, 5, 10)]
public void Execute_ExcceedGasLimit_Throw(
long gasLimit, long gasConsumption, long gasOwned)
{
if (gasLimit >= gasConsumption)
{
throw new ArgumentOutOfRangeException(
nameof(gasLimit),
$"{nameof(gasLimit)} must be less than {nameof(gasConsumption)}.");
}

if (gasOwned < gasConsumption)
{
throw new ArgumentOutOfRangeException(
nameof(gasOwned),
$"{nameof(gasOwned)} must be greater than or equal to {nameof(gasConsumption)}.");
}

// Given
var signerKey = new PrivateKey();
var signerMead = Mead * gasOwned;
EnsureToMintAsset(signerKey, signerMead);

// When
var expectedMead = signerMead - (Mead * gasLimit);
var gasAction = new GasAction { Consumption = gasConsumption };
MakeTransaction(
signerKey,
new ActionBase[] { gasAction, },
maxGasPrice: Mead * 1,
gasLimit: gasLimit);

// Then
var e = Assert.Throws<AggregateException>(() => MoveToNextBlock(throwOnError: true));
var innerExceptions = e.InnerExceptions
.Cast<UnexpectedlyTerminatedActionException>()
.ToArray();
var actualMead = GetBalance(signerKey.Address, Mead);
Assert.Single(innerExceptions);
Assert.Contains(innerExceptions, i => i.Action is GasAction);
Assert.Equal(expectedMead, actualMead);
}
}
172 changes: 172 additions & 0 deletions .Lib9c.Tests/Action/ValidatorDelegation/GasWithTransferAssetTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
#nullable enable
namespace Lib9c.Tests.Action.ValidatorDelegation;

using System;
using System.Linq;
using Libplanet.Action;
using Libplanet.Crypto;
using Nekoyume.Action;
using Xunit;

public class GasWithTransferAssetTest : TxAcitonTestBase
{
public const long GasConsumption = 4;

[Theory]
[InlineData(4, 4)]
[InlineData(5, 4)]
[InlineData(6, 4)]
[InlineData(long.MaxValue, 4)]
[InlineData(long.MaxValue, 5)]
[InlineData(long.MaxValue, 6)]
public void Execute(long gasLimit, long gasOwned)
{
if (gasLimit < GasConsumption)
{
throw new ArgumentOutOfRangeException(
nameof(gasLimit),
$"{nameof(gasLimit)} must be greater than or equal to {GasConsumption}.");
}

if (gasOwned < GasConsumption)
{
throw new ArgumentOutOfRangeException(
nameof(gasOwned),
$"{nameof(gasOwned)} must be greater than or equal to {GasConsumption}.");
}

// Given
var signerKey = new PrivateKey();
var signerMead = Mead * gasOwned;
var recipientKey = new PrivateKey();
EnsureToMintAsset(signerKey, signerMead);
EnsureToMintAsset(signerKey, NCG * 100);

// When
var amount = NCG * 1;
var expectedNCG = NCG * 1;
var expectedMead = signerMead - Mead * GasConsumption;
var transferAsset = new TransferAsset(
signerKey.Address, recipientKey.Address, amount, memo: "test");
MakeTransaction(
signerKey,
new ActionBase[] { transferAsset, },
maxGasPrice: Mead * 1,
gasLimit: gasLimit);

// `
MoveToNextBlock(throwOnError: true);
var actualNCG = GetBalance(recipientKey.Address, NCG);
var actualMead = GetBalance(signerKey.Address, Mead);
Assert.Equal(expectedNCG, actualNCG);
Assert.Equal(expectedMead, actualMead);
}

[Theory]
[InlineData(4, 0)]
[InlineData(5, 0)]
[InlineData(6, 1)]
[InlineData(long.MaxValue, 0)]
[InlineData(long.MaxValue, 1)]
[InlineData(long.MaxValue, 2)]
public void Execute_InsufficientMead_Throw(
long gasLimit, long gasOwned)
{
if (gasLimit < GasConsumption)
{
throw new ArgumentOutOfRangeException(
nameof(gasLimit),
$"{nameof(gasLimit)} must be greater than or equal to {GasConsumption}.");
}

if (gasOwned >= GasConsumption)
{
throw new ArgumentOutOfRangeException(
nameof(gasOwned),
$"{nameof(gasOwned)} must be less than {GasConsumption}.");
}

// Given
var signerKey = new PrivateKey();
var signerMead = Mead * gasOwned;
var recipientKey = new PrivateKey();
EnsureToMintAsset(signerKey, signerMead);
EnsureToMintAsset(signerKey, NCG * 100);

// When
var amount = NCG * 1;
var expectedMead = Mead * 0;
var expectedNCG = NCG * 0;
var transferAsset = new TransferAsset(
signerKey.Address, recipientKey.Address, amount, memo: "test");
MakeTransaction(
signerKey,
new ActionBase[] { transferAsset, },
maxGasPrice: Mead * 1,
gasLimit: gasLimit);

// Then
var e = Assert.Throws<AggregateException>(() => MoveToNextBlock(throwOnError: true));
var innerExceptions = e.InnerExceptions
.Cast<UnexpectedlyTerminatedActionException>()
.ToArray();
var actualNCG = GetBalance(recipientKey.Address, NCG);
var actualMead = GetBalance(signerKey.Address, Mead);
Assert.Single(innerExceptions);
Assert.Contains(innerExceptions, i => i.Action is TransferAsset);
Assert.Equal(expectedNCG, actualNCG);
Assert.Equal(expectedMead, actualMead);
}

[Theory]
[InlineData(3, 5)]
[InlineData(1, 10)]
public void Execute_ExcceedGasLimit_Throw(
long gasLimit, long gasOwned)
{
if (gasLimit >= GasConsumption)
{
throw new ArgumentOutOfRangeException(
nameof(gasLimit),
$"{nameof(gasLimit)} must be less than {GasConsumption}.");
}

if (gasOwned < GasConsumption)
{
throw new ArgumentOutOfRangeException(
nameof(gasOwned),
$"{nameof(gasOwned)} must be greater than or equal to {GasConsumption}.");
}

// Given
var amount = NCG * 1;
var signerKey = new PrivateKey();
var signerMead = Mead * gasOwned;
var recipientKey = new PrivateKey();
EnsureToMintAsset(signerKey, signerMead);
EnsureToMintAsset(signerKey, NCG * 100);

// When
var expectedMead = signerMead - (Mead * gasLimit);
var expectedNCG = NCG * 0;
var transferAsset = new TransferAsset(
signerKey.Address, recipientKey.Address, amount, memo: "test");
MakeTransaction(
signerKey,
new ActionBase[] { transferAsset, },
maxGasPrice: Mead * 1,
gasLimit: gasLimit);

// Then
var e = Assert.Throws<AggregateException>(() => MoveToNextBlock(throwOnError: true));
var innerExceptions = e.InnerExceptions
.Cast<UnexpectedlyTerminatedActionException>()
.ToArray();
var actualNCG = GetBalance(recipientKey.Address, NCG);
var actualMead = GetBalance(signerKey.Address, Mead);
Assert.Single(innerExceptions);
Assert.Contains(innerExceptions, i => i.Action is TransferAsset);
Assert.Equal(expectedNCG, actualNCG);
Assert.Equal(expectedMead, actualMead);
}
}
Loading

0 comments on commit da0de9a

Please sign in to comment.