Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor RewardGold for PoS #2925

Open
wants to merge 6 commits into
base: feature/dpos-with-guild
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading