Skip to content

Commit

Permalink
feat(mpt): export all states as JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
moreal committed Oct 6, 2020
1 parent 0d3e6e5 commit 81f23bc
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Libplanet.Tools/Mpt.cs
Expand Up @@ -4,6 +4,8 @@
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using Bencodex;
using Cocona;
using Cocona.Help;
using Libplanet.RocksDBStore;
Expand Down Expand Up @@ -64,6 +66,36 @@ public class Mpt
}
}

[Command(Description = "Export all states of the state root hash as JSON.")]
public void Export(
[Argument(
Name = "KEY-VALUE-STORE-PATH",
Description = "The path where IKeyValueStore implementation was used at.")]
string kvStorePath,
[Argument(
Name = "STATE-ROOT-HASH",
Description = "The state root hash to compare.")]
string stateRootHashHex,
[Option(
't',
Description = "The type of the key-value store stored" +
" at the given KEY-VALUE-STORE-PATH argument. " +
"It should be among in 'default' or 'rocksdb'.")]
string kvStoreType)
{
IKeyValueStore keyValueStore = LoadKeyValueStore(kvStorePath, kvStoreType);
var trie = new MerkleTrie(
keyValueStore,
HashDigest<SHA256>.FromString(stateRootHashHex));
var codec = new Codec();
ImmutableDictionary<string, byte[]> decoratedStates = trie.ListAllStates()
.ToImmutableDictionary(
pair => ByteUtil.Hex(pair.Key),
pair => codec.Encode(pair.Value));

Console.WriteLine(JsonSerializer.Serialize(decoratedStates));
}

[PrimaryCommand]
public void Help([FromService] ICoconaHelpMessageBuilder helpMessageBuilder)
{
Expand Down

0 comments on commit 81f23bc

Please sign in to comment.