-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
602 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
172
.Lib9c.Tests/Action/ValidatorDelegation/GasWithTransferAssetTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.