Skip to content

Commit

Permalink
feat(cli): query a state at the state key in kv-store
Browse files Browse the repository at this point in the history
  • Loading branch information
moreal committed Dec 23, 2020
1 parent 6dc4b72 commit a2ce7cb
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Libplanet.Tools/Mpt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using System.Text.Json;
using Bencodex;
using Bencodex.Types;
using Cocona;
using Cocona.Help;
using Libplanet.RocksDBStore;
Expand Down Expand Up @@ -188,6 +189,46 @@ public void List(
}
}

[Command(Description="Query a state of the state key at the state root hash. It will " +
"print the state as hexadecimal bytes string into stdout. " +
"If it didn't exist, it will not print anything.")]
public void Query(
[Argument(
Name = "KV-STORE",
Description = KVStoreArgumentDescription)]
string kvStoreUri,
[Argument(
Name = "STATE-ROOT-HASH",
Description = "The state root hash to compare.")]
string stateRootHashHex,
[Argument(
Name = "STATE-KEY",
Description = "The key of the state to query.")]
string stateKey,
[FromService] IConfigurationService<ToolConfiguration> configurationService)
{
ToolConfiguration toolConfiguration = configurationService.Load();
kvStoreUri = ConvertKVStoreUri(kvStoreUri, toolConfiguration);
IKeyValueStore keyValueStore = LoadKVStoreFromURI(kvStoreUri);
var trie = new MerkleTrie(
keyValueStore,
HashDigest<SHA256>.FromString(stateRootHashHex));
byte[] stateKeyBytes = Encoding.UTF8.GetBytes(stateKey);
if (trie.TryGet(
stateKeyBytes,
out IValue? value) && value is { })
{
var codec = new Codec();
Console.WriteLine(ByteUtil.Hex(codec.Encode(value)));
}
else
{
Console.Error.WriteLine(
$"The state corresponded to {stateKey} at the state root hash " +
$"\"{stateRootHashHex}\" in kv-store \"{kvStoreUri}\" seems not existed.");
}
}

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

0 comments on commit a2ce7cb

Please sign in to comment.