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 { })
public void GetAndSet(int addressCount) { ITrie trie = new MerkleTrie(new MemoryKeyValueStore()); var addresses = Enumerable .Range(0, addressCount) .Select(_ => new PrivateKey().ToAddress()) .ToImmutableArray(); var states = new Dictionary <Address, IValue>(); void CheckAddressStates() { foreach (var address in addresses) { Assert.Equal( states.ContainsKey(address), trie.TryGet(address.ToByteArray(), out IValue state)); IValue expectedState = states.ContainsKey(address) ? states[address] : null; Assert.Equal(expectedState, state); } } foreach (var address in addresses) { states[address] = (Text)address.ToHex(); trie = trie.Set(address.ToByteArray(), states[address]); CheckAddressStates(); } }
public void Commit(int addressCount) { IKeyValueStore keyValueStore = new MemoryKeyValueStore(); var codec = new Codec(); ITrie trieA = new MerkleTrie(keyValueStore); var addresses = new Address[addressCount]; var states = new IValue[addressCount]; for (int i = 0; i < addressCount; ++i) { addresses[i] = new PrivateKey().ToAddress(); states[i] = (Binary)TestUtils.GetRandomBytes(128); trieA = trieA.Set(addresses[i].ToByteArray(), states[i]); } byte[] path = TestUtils.GetRandomBytes(32); trieA = trieA.Set(path, (Text)"foo"); HashDigest <SHA256> rootHash = trieA.Hash; Assert.True(trieA.TryGet(path, out IValue stateA)); Assert.Equal((Text)"foo", stateA); ITrie trieB = trieA.Commit(); Assert.True(trieB.TryGet(path, out IValue stateB)); Assert.Equal((Text)"foo", stateB); trieB = trieB.Set(path, (Text)"bar"); Assert.True(trieA.TryGet(path, out stateA)); Assert.Equal((Text)"foo", stateA); Assert.True(trieB.TryGet(path, out stateB)); Assert.Equal((Text)"bar", stateB); ITrie trieC = trieB.Commit(); ITrie trieD = trieC.Commit(); Assert.NotEqual(trieA.Hash, trieB.Hash); Assert.NotEqual(trieA.Hash, trieC.Hash); Assert.NotEqual(trieB.Hash, trieC.Hash); Assert.Equal(trieC.Hash, trieD.Hash); }
public IValue?GetState(string stateKey, BlockHash?blockHash = null, Guid?chainId = null) { if (blockHash is null) { throw new ArgumentNullException(nameof(blockHash)); } var stateHash = _stateHashKeyValueStore.Get(blockHash?.ToByteArray() !); var stateTrie = new MerkleTrie( _stateKeyValueStore, new HashNode(new HashDigest <SHA256>(stateHash)), _secure); var key = Encoding.UTF8.GetBytes(stateKey); return(stateTrie.TryGet(key, out IValue? value) ? value : null); }