Skip to content

Commit

Permalink
temp4
Browse files Browse the repository at this point in the history
  • Loading branch information
limebell committed Dec 5, 2023
1 parent ad27af6 commit fbbd770
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
17 changes: 15 additions & 2 deletions Libplanet.Action.Tests/State/WorldBaseStateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Libplanet.Crypto;
using Libplanet.Store;
using Libplanet.Store.Trie;
using Libplanet.Types.Blocks;
using Xunit;
using static Libplanet.Action.State.KeyConverters;

Expand All @@ -24,22 +25,34 @@ public WorldBaseStateTest()
[Fact]
public void Metadata()
{
/*var accountAddress = new Address(TestUtils.GetRandomBytes(20));*/

Check warning on line 28 in Libplanet.Action.Tests/State/WorldBaseStateTest.cs

View workflow job for this annotation

GitHub Actions / check-build

Remove this commented out code.

Check warning on line 28 in Libplanet.Action.Tests/State/WorldBaseStateTest.cs

View workflow job for this annotation

GitHub Actions / Run Benchmark.Net benchmarks (macos-latest)

Remove this commented out code.

Check warning on line 28 in Libplanet.Action.Tests/State/WorldBaseStateTest.cs

View workflow job for this annotation

GitHub Actions / Run Benchmark.Net benchmarks (ubuntu-latest)

Remove this commented out code.

Check warning on line 28 in Libplanet.Action.Tests/State/WorldBaseStateTest.cs

View workflow job for this annotation

GitHub Actions / Run Benchmark.Net benchmarks (windows-latest)

Remove this commented out code.
var accountAddress = ReservedAddresses.LegacyAccount;
var address = new Address(TestUtils.GetRandomBytes(20));
ITrie accountTrie = new MerkleTrie(_kvStore);
accountTrie = accountTrie.Set(ToStateKey(address), (Text)"foo");
accountTrie = accountTrie.SetMetadata(new TrieMetadata(
BlockMetadata.CurrentProtocolVersion,
TrieType.Account));
accountTrie = _stateStore.Commit(accountTrie);
ITrie worldTrie = new MerkleTrie(_kvStore);
worldTrie = worldTrie.Set(
ToStateKey(ReservedAddresses.LegacyAccount),
ToStateKey(accountAddress),
(Binary)accountTrie.Hash.ByteArray);
worldTrie = worldTrie.SetMetadata(new TrieMetadata(
BlockMetadata.CurrentProtocolVersion,
TrieType.World));
worldTrie = _stateStore.Commit(worldTrie);
var stateRoot = worldTrie.Hash;
var world = new World(new WorldBaseState(
_stateStore.GetStateRoot(stateRoot),
_stateStore));
Assert.Equal(worldTrie.Hash, world.Trie.Hash);
Assert.False(world.Legacy);
var account = world.GetAccount(accountAddress);
Assert.Equal(accountTrie.Hash, account.Trie.Hash);
Assert.Equal(
(Text)"foo",
world.GetAccount(ReservedAddresses.LegacyAccount).GetState(address));
world.GetAccount(accountAddress).GetState(address));
}
}
}
2 changes: 1 addition & 1 deletion Libplanet.Action/ActionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ internal IWorld MigrateLegacyStates(IWorld prevWorld, int version)
var legacyTrie = _stateStore.GetStateRoot(prevWorld.Trie.Hash);
legacyTrie = legacyTrie.SetMetadata(new TrieMetadata(
version,
TrieType.World));
TrieType.Account));
legacyTrie = _stateStore.Commit(legacyTrie);
var worldTrie = _stateStore.GetStateRoot(null).Set(
KeyConverters.ToStateKey(ReservedAddresses.LegacyAccount),
Expand Down
50 changes: 50 additions & 0 deletions Libplanet.Tests/Action/ActionEvaluatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,56 @@ private void CheckRandomSeedInAction()
}
}

[Fact]
private void MigrateStates()
{
var privateKey = new PrivateKey();
var address = privateKey.ToAddress();
long blockIndex = 2;

var action = new EvaluateTestAction()
{
BlockIndexKey = new PrivateKey().ToAddress(),
MinerKey = new PrivateKey().ToAddress(),
SignerKey = new PrivateKey().ToAddress(),
};

var store = new MemoryStore();
var stateStore = new TrieStateStore(new MemoryKeyValueStore());
var chain = TestUtils.MakeBlockChain<EvaluateTestAction>(
policy: new BlockPolicy(),
store: store,
stateStore: stateStore);
var tx = Transaction.Create(
nonce: 0,
privateKey: privateKey,
genesisHash: chain.Genesis.Hash,
actions: new[] { action }.ToPlainValues());

chain.StageTransaction(tx);
var miner = new PrivateKey();
Block block = chain.ProposeBlock(miner);
chain.Append(block, CreateBlockCommit(block));

var evaluations = chain.ActionEvaluator.Evaluate(
chain.Tip, chain.Store.GetStateRootHash(chain.Tip.PreviousHash));

Assert.False(evaluations[0].InputContext.BlockAction);
Assert.Single(evaluations);
Assert.Null(evaluations.Single().Exception);
Assert.Equal(
chain.GetWorldState()
.GetAccount(ReservedAddresses.LegacyAccount).GetState(action.SignerKey),
(Text)address.ToHex());
Assert.Equal(
chain.GetWorldState()
.GetAccount(ReservedAddresses.LegacyAccount).GetState(action.MinerKey),
(Text)miner.ToAddress().ToHex());
var state = chain.GetWorldState()
.GetAccount(ReservedAddresses.LegacyAccount).GetState(action.BlockIndexKey);
Assert.Equal((long)(Integer)state, blockIndex);
}

private sealed class EvaluateTestAction : IAction
{
public Address SignerKey { get; set; }
Expand Down

0 comments on commit fbbd770

Please sign in to comment.