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

Bugfix/fix mead #2668

Merged
merged 9 commits into from
Jul 12, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void LoadPlainValue(IValue plainValue)

public IWorld Execute(IActionContext context)
{
context.UseGas(1);
GasTracer.UseGas(1);
return context.PreviousState;
}
}
Expand Down
11 changes: 0 additions & 11 deletions .Lib9c.Tests/Action/ActionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ namespace Lib9c.Tests.Action

public class ActionContext : IActionContext
{
private long _gasUsed;

private IRandom _random = null;

private IReadOnlyList<ITransaction> _txs = null;
Expand Down Expand Up @@ -51,17 +49,8 @@ public IReadOnlyList<ITransaction> Txs
set => _txs = value;
}

public void UseGas(long gas)
{
_gasUsed += gas;
}

public IRandom GetRandom() => _random ?? new TestRandom(RandomSeed);

public long GasUsed() => _gasUsed;

public long GasLimit() => 0;

// FIXME: Temporary measure to allow inheriting already mutated IRandom.
public void SetRandom(IRandom random)
{
Expand Down
8 changes: 4 additions & 4 deletions .Lib9c.Tests/Action/DPoS/CancelUndelegationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void Execute()
// Promote the validator
states = new PromoteValidator(
validatorPrivateKey.PublicKey,
amount: GovernanceToken * 100)
amount: 100)
.Execute(
new ActionContext
{ PreviousState = states, Signer = validatorOperatorAddress });
Expand All @@ -49,7 +49,7 @@ public void Execute()
new ActionContext { PreviousState = states, Miner = validatorOperatorAddress });

// Delegate the delegator
states = new Nekoyume.Action.DPoS.Delegate(validatorAddress, GovernanceToken * 50).Execute(
states = new Nekoyume.Action.DPoS.Delegate(validatorAddress, 50).Execute(
new ActionContext { PreviousState = states, Signer = delegatorAddress });
states = new UpdateValidators().Execute(new ActionContext { PreviousState = states });

Expand Down Expand Up @@ -103,7 +103,7 @@ public void CancelAll()
// Promote the validator
states = new PromoteValidator(
validatorPrivateKey.PublicKey,
amount: GovernanceToken * 100)
amount: 100)
.Execute(
new ActionContext
{ PreviousState = states, Signer = validatorOperatorAddress });
Expand All @@ -112,7 +112,7 @@ public void CancelAll()
new ActionContext { PreviousState = states, Miner = validatorOperatorAddress });

// Delegate the delegator
states = new Nekoyume.Action.DPoS.Delegate(validatorAddress, GovernanceToken * 50).Execute(
states = new Nekoyume.Action.DPoS.Delegate(validatorAddress, 50).Execute(
new ActionContext { PreviousState = states, Signer = delegatorAddress });
states = new UpdateValidators().Execute(new ActionContext { PreviousState = states });

Expand Down
4 changes: 2 additions & 2 deletions .Lib9c.Tests/Action/DPoS/DelegateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void Execute()
// Promote the delegator
states = new PromoteValidator(
validatorPrivateKey.PublicKey,
amount: GovernanceToken * 100)
amount: 100)
.Execute(
new ActionContext
{ PreviousState = states, Signer = validatorOperatorAddress });
Expand All @@ -53,7 +53,7 @@ public void Execute()
new ActionContext { PreviousState = states, Miner = validatorOperatorAddress });

// Delegate the validator
states = new Nekoyume.Action.DPoS.Delegate(validatorAddress, GovernanceToken * 50).Execute(
states = new Nekoyume.Action.DPoS.Delegate(validatorAddress, 50).Execute(
new ActionContext { PreviousState = states, Signer = delegatorAddress });
states = new UpdateValidators().Execute(new ActionContext { PreviousState = states });

Expand Down
174 changes: 174 additions & 0 deletions .Lib9c.Tests/Action/DPoS/MeadTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
namespace Lib9c.Tests.Action.DPoS
{
using System;
using System.Collections.Immutable;
using System.Linq;
using Bencodex.Types;
using Lib9c.Renderers;
using Libplanet.Action;
using Libplanet.Action.State;
using Libplanet.Blockchain;
using Libplanet.Blockchain.Policies;
using Libplanet.Crypto;
using Libplanet.Store;
using Libplanet.Store.Trie;
using Libplanet.Types.Assets;
using Libplanet.Types.Blocks;
using Libplanet.Types.Tx;
using Nekoyume.Action;
using Nekoyume.Action.DPoS.Misc;
using Nekoyume.Action.DPoS.Sys;
using Nekoyume.Action.Loader;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Xunit;

public class MeadTest
{
private static readonly byte[] ConversionTable =
{
48, // '0'
49, // '1'
50, // '2'
51, // '3'
52, // '4'
53, // '5'
54, // '6'
55, // '7'
56, // '8'
57, // '9'
97, // 'a'
98, // 'b'
99, // 'c'
100, // 'd'
101, // 'e'
102, // 'f'
};

private PrivateKey _genesisProposer;
private IStore _store;
private ActionEvaluator _actionEvaluator;
private BlockChain _chain;

public MeadTest()
{
_genesisProposer = new PrivateKey();
_store = new MemoryStore();
var stateStore = new TrieStateStore(new MemoryKeyValueStore());
var trie = stateStore.GetStateRoot(null);
trie = trie.SetMetadata(new TrieMetadata(BlockMetadata.CurrentProtocolVersion));
IWorld world = new World(new WorldBaseState(trie, stateStore));
world = world.MintAsset(
new ActionContext { Signer = _genesisProposer.Address },
_genesisProposer.Address,
Currencies.Mead * 1000);
world = world.SetLegacyState(
GoldCurrencyState.Address,
new GoldCurrencyState(Currency.Legacy("NCG", 2, null)).Serialize());

var worldTrie = world.Trie;
foreach (var account in world.Delta.Accounts)
{
var accountTrie = stateStore.Commit(account.Value.Trie);
worldTrie = worldTrie.Set(
ToStateKey(account.Key),
new Binary(accountTrie.Hash.ByteArray));
}

worldTrie = stateStore.Commit(worldTrie);
// Create a policy without RewardGold action.
var policy = new BlockPolicy(
new IAction[] { new AllocateReward() }.ToImmutableArray(),
new IAction[] { new RecordProposer() }.ToImmutableArray(),
new IAction[] { new Mortgage() }.ToImmutableArray(),
new IAction[] { new Reward(), new Refund() }.ToImmutableArray());
var preEval = new BlockContent(
new BlockMetadata(
protocolVersion: BlockMetadata.CurrentProtocolVersion,
index: 0,
timestamp: DateTimeOffset.UtcNow,
miner: _genesisProposer.Address,
publicKey: _genesisProposer.PublicKey,
previousHash: null,
txHash: null,
lastCommit: null),
transactions: Enumerable.Empty<Transaction>()).Propose();
var genesisBlock = preEval.Sign(_genesisProposer, worldTrie.Hash);
var blockChainStates = new BlockChainStates(_store, stateStore);
_actionEvaluator = new ActionEvaluator(
new PolicyActionsRegistry(
_ => policy.BeginBlockActions,
_ => policy.EndBlockActions,
_ => policy.BeginTxActions,
_ => policy.EndTxActions),
stateStore: stateStore,
actionTypeLoader: new NCActionLoader());
_chain = BlockChain.Create(
policy,
new VolatileStagePolicy(),
_store,
stateStore,
genesisBlock,
renderers: new[] { new ActionRenderer() },
blockChainStates: blockChainStates,
actionEvaluator: _actionEvaluator
);
}

[Fact]
public void AllocateMeadReward()
{
var tx = Transaction.Create(
nonce: 0,
privateKey: _genesisProposer,
genesisHash: _chain.Genesis.Hash,
maxGasPrice: Currencies.Mead * 1,
gasLimit: 2,
actions: new[]
{
new CreateAvatar
{
name = "Foo",
index = 0,
ear = 0,
hair = 0,
lens = 0,
tail = 0,
},
}.ToPlainValues());

_chain.StageTransaction(tx);
Block block = _chain.ProposeBlock(_genesisProposer);
Assert.Single(block.Transactions);

var evaluations = _actionEvaluator.Evaluate(
block, _store.GetNextStateRootHash((BlockHash)block.PreviousHash));

// 5 policy actions + 1 CreateAvatar action
Assert.Equal(6, evaluations.Count);
Assert.Equal(
Currencies.Mead * 999,
_chain
.GetWorldState(evaluations.Last().OutputState)
.GetBalance(_genesisProposer.Address, Currencies.Mead));
Assert.Equal(
Currencies.Mead * 1,
_chain
.GetWorldState(evaluations.Last().OutputState)
.GetBalance(ReservedAddress.RewardPool, Currencies.Mead));
}

private static KeyBytes ToStateKey(Address address)
{
var addressBytes = address.ByteArray;
byte[] buffer = new byte[addressBytes.Length * 2];
for (int i = 0; i < addressBytes.Length; i++)
{
buffer[i * 2] = ConversionTable[addressBytes[i] >> 4];
buffer[i * 2 + 1] = ConversionTable[addressBytes[i] & 0xf];
}

return new KeyBytes(buffer);
}
}
}
7 changes: 3 additions & 4 deletions .Lib9c.Tests/Action/DPoS/RedelegateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Nekoyume.Action.DPoS.Model;
using Nekoyume.Action.DPoS.Sys;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Xunit;

public class RedelegateTest : PoSTest
Expand Down Expand Up @@ -53,13 +52,13 @@ public void Execute()
// Promote the delegator
states = new PromoteValidator(
validator1PrivateKey.PublicKey,
amount: GovernanceToken * 100)
amount: 100)
.Execute(
new ActionContext
{ PreviousState = states, Signer = validator1OperatorAddress });
states = new PromoteValidator(
validator2PrivateKey.PublicKey,
amount: GovernanceToken * 100)
amount: 100)
.Execute(
new ActionContext
{ PreviousState = states, Signer = validator2OperatorAddress });
Expand All @@ -68,7 +67,7 @@ public void Execute()
new ActionContext { PreviousState = states, Miner = validator1OperatorAddress });

// Delegate the validator 1
states = new Nekoyume.Action.DPoS.Delegate(validator1Address, GovernanceToken * 50).Execute(
states = new Nekoyume.Action.DPoS.Delegate(validator1Address, 50).Execute(
new ActionContext { PreviousState = states, Signer = delegatorAddress });
states = new UpdateValidators().Execute(new ActionContext { PreviousState = states });

Expand Down
4 changes: 1 addition & 3 deletions .Lib9c.Tests/Action/DPoS/Sys/UpdateValidatorsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ public void Execute()
// Stake 1 for each validator.
foreach (var key in validatorKeys)
{
initialState = new PromoteValidator(
key,
new FungibleAssetValue(GovernanceToken, 1, 0)).Execute(
initialState = new PromoteValidator(key, 1).Execute(
new ActionContext
{
PreviousState = initialState,
Expand Down
8 changes: 4 additions & 4 deletions .Lib9c.Tests/Action/DPoS/UndelegateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void Execute()
// Promote the validator
states = new PromoteValidator(
validatorPrivateKey.PublicKey,
amount: GovernanceToken * 100)
amount: 100)
.Execute(
new ActionContext
{ PreviousState = states, Signer = validatorOperatorAddress });
Expand All @@ -53,7 +53,7 @@ public void Execute()
new ActionContext { PreviousState = states, Miner = validatorOperatorAddress });

// Delegate the delegator
states = new Nekoyume.Action.DPoS.Delegate(validatorAddress, GovernanceToken * 50).Execute(
states = new Nekoyume.Action.DPoS.Delegate(validatorAddress, 50).Execute(
new ActionContext { PreviousState = states, Signer = delegatorAddress });
states = new UpdateValidators().Execute(new ActionContext { PreviousState = states });

Expand Down Expand Up @@ -175,7 +175,7 @@ public void UndelegateAll()
// Promote the validator
states = new PromoteValidator(
validatorPrivateKey.PublicKey,
amount: GovernanceToken * 100)
amount: 100)
.Execute(
new ActionContext
{ PreviousState = states, Signer = validatorOperatorAddress });
Expand All @@ -184,7 +184,7 @@ public void UndelegateAll()
new ActionContext { PreviousState = states, Miner = validatorOperatorAddress });

// Delegate the delegator
states = new Nekoyume.Action.DPoS.Delegate(validatorAddress, GovernanceToken * 50).Execute(
states = new Nekoyume.Action.DPoS.Delegate(validatorAddress, 50).Execute(
new ActionContext { PreviousState = states, Signer = delegatorAddress });
states = new UpdateValidators().Execute(new ActionContext { PreviousState = states });

Expand Down
4 changes: 2 additions & 2 deletions .Lib9c.Tests/Action/DPoS/WithdrawDelegatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public void Execute()
Nekoyume.Action.DPoS.Model.ValidatorRewards.DeriveAddress(
validatorAddress,
GovernanceToken);
var amount = GovernanceToken * 100;
var amount = 100;
states = states.TransferAsset(
new ActionContext(),
GoldCurrencyState.Address,
address,
amount);
GovernanceToken * amount);
states = new PromoteValidator(validatorPrivateKey.PublicKey, amount: amount).Execute(
new ActionContext { PreviousState = states, Signer = address });
states = new UpdateValidators().Execute(new ActionContext { PreviousState = states });
Expand Down
4 changes: 2 additions & 2 deletions .Lib9c.Tests/Action/DPoS/WithdrawValidatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public void Execute()
var address = validatorPrivateKey.Address;
var rewardAddress = AllocateRewardCtrl.RewardAddress(address);
var states = InitialState;
var amount = GovernanceToken * 100;
var amount = 100;
states = states.TransferAsset(
new ActionContext(),
GoldCurrencyState.Address,
address,
amount);
GovernanceToken * amount);
states = new PromoteValidator(validatorPrivateKey.PublicKey, amount: amount).Execute(
new ActionContext { PreviousState = states, Signer = address });
states = new UpdateValidators().Execute(new ActionContext { PreviousState = states });
Expand Down
4 changes: 2 additions & 2 deletions .Lib9c.Tests/Action/Scenario/MeadScenarioTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void Contract()
Assert.Equal(4 * mead, states6.GetBalance(agentAddress, mead));
}

[Fact]
[Fact(Skip = "No way tracing gas usage outside of ActionEvaluator for now")]
public void UseGas()
{
Type baseType = typeof(Nekoyume.Action.ActionBase);
Expand Down Expand Up @@ -119,7 +119,7 @@ bool IsTarget(Type type)
long expectedGasLimit = action is ITransferAsset || action is ITransferAssets
? expectedTransferActionGasLimit
: expectedActionGasLimit;
long gasUsed = actionContext.GasUsed();
long gasUsed = GasTracer.GasUsed;
Assert.True(expectedGasLimit == gasUsed, $"{action} invalid used gas. {gasUsed}");
}
}
Expand Down
Loading
Loading