Skip to content

Commit

Permalink
test: Add test code for collecting gas
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Oct 25, 2024
1 parent c3aeb92 commit 36fdc4d
Show file tree
Hide file tree
Showing 4 changed files with 668 additions and 5 deletions.
181 changes: 181 additions & 0 deletions .Lib9c.Tests/Action/ValidatorDelegation/GasTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
#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)]
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 gasActions = new IAction[]
{
new GasAction { Consumption = gasConsumption },
};

MakeTransaction(
signerKey,
gasActions,
maxGasPrice: Mead * 1,
gasLimit: gasLimit);
MoveToNextBlock(throwOnError: true);

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

[Theory]
[InlineData(0, 4)]
[InlineData(1, 4)]
[InlineData(4, 4)]
public void Execute_Without_GasLimit_And_MaxGasPrice(
long gasConsumption, long gasOwned)
{
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;
var gasActions = new IAction[]
{
new GasAction { Consumption = gasConsumption },
};

MakeTransaction(signerKey, gasActions);
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)]
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.IsType<GasAction>(innerExceptions[0].Action);
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);
}
}
205 changes: 205 additions & 0 deletions .Lib9c.Tests/Action/ValidatorDelegation/GasWithTransferAssetTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
#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(4, 5)]
[InlineData(4, 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(0, 4)]
[InlineData(1, 4)]
[InlineData(4, 4)]
public void Execute_Without_GasLimit_And_MaxGasPrice(
long gasConsumption, long gasOwned)
{
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;
var recipientKey = new PrivateKey();
EnsureToMintAsset(signerKey, signerMead);
EnsureToMintAsset(signerKey, NCG * 100);

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

// Then
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)]
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 36fdc4d

Please sign in to comment.