public static void Set(this ITrie trie, IImmutableDictionary <byte[], IValue> values) { foreach (var pair in values) { trie.Set(pair.Key, pair.Value); } }
public static ITrie Set(this ITrie trie, IEnumerable <KeyValuePair <KeyBytes, IValue?> > pairs) { foreach (var pair in pairs) { if (pair.Value is { } v) { trie = trie.Set(pair.Key, v); }
public TxWithContext Sign(PrivateKey signer, params Arithmetic[] actions) { Address signerAddress = signer.ToAddress(); string rawStateKey = KeyConverters.ToStateKey(signerAddress); long nonce = Chain.GetNextTxNonce(signerAddress); Transaction <Arithmetic> tx = Transaction <Arithmetic> .Create(nonce, signer, Genesis.Hash, actions); BigInteger prevState = Chain.GetState(signerAddress) is Bencodex.Types.Integer i ? i.Value : 0; HashDigest <SHA256> prevStateRootHash = Chain.Tip.StateRootHash; ITrie prevTrie = GetTrie(Chain.Tip.Hash); (BigInteger, HashDigest <SHA256>)prevPair = (prevState, prevStateRootHash); (BigInteger, HashDigest <SHA256>)stagedStates = Chain.ListStagedTransactions() .Where(t => t.Signer.Equals(signerAddress)) .OrderBy(t => t.Nonce) .SelectMany(t => t.Actions) .TakeWhile(a => a.Error is null) .Aggregate(prevPair, (prev, act) => { BigInteger nextState = act.Operator.ToFunc()(prev.Item1, act.Operand); var updatedRawStates = ImmutableDictionary <string, IValue> .Empty .Add(rawStateKey, (Bencodex.Types.Integer)nextState); HashDigest <SHA256> nextRootHash = prevTrie.Set(updatedRawStates).Commit().Hash; return(nextState, nextRootHash); }); Chain.StageTransaction(tx); ImmutableArray <(BigInteger, HashDigest <SHA256>)> expectedDelta = tx.Actions .Take(tx.Actions.TakeWhile(a => a.Error is null).Count() + 1) .Aggregate( ImmutableArray.Create(stagedStates), (delta, act) => { BigInteger nextState = act.Operator.ToFunc()(delta[delta.Length - 1].Item1, act.Operand); var updatedRawStates = ImmutableDictionary <string, IValue> .Empty .Add(rawStateKey, (Bencodex.Types.Integer)nextState); HashDigest <SHA256> nextRootHash = prevTrie.Set(updatedRawStates).Commit().Hash; return(delta.Add((nextState, nextRootHash))); }
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 static ITrie Commit( this IStateStore stateStore, HashDigest <SHA256>?previousStateRootHash, IImmutableDictionary <string, IValue> rawStatesDelta, bool rehearsal = false ) { ITrie trie = stateStore.GetStateRoot(previousStateRootHash); foreach (KeyValuePair <string, IValue> pair in rawStatesDelta) { byte[] keyBytes = KeyEncoding.GetBytes(pair.Key); trie = trie.Set(keyBytes, pair.Value); } ITrie stage = trie.Commit(rehearsal); return(rehearsal ? stage : stateStore.GetStateRoot(stage.Hash)); }
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(new KeyBytes(addresses[i].ByteArray), states[i]); } KeyBytes path = new KeyBytes(TestUtils.GetRandomBytes(32)); trieA = trieA.Set(path, (Text)"foo"); Assert.Equal((Text)"foo", trieA.Get(new[] { path })[0]); ITrie trieB = trieA.Commit(); Assert.Equal((Text)"foo", trieB.Get(new[] { path })[0]); trieB = trieB.Set(path, (Text)"bar"); Assert.Equal((Text)"foo", trieA.Get(new[] { path })[0]); Assert.Equal((Text)"bar", trieB.Get(new[] { path })[0]); 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 static ITrie Set(this ITrie trie, IImmutableDictionary <string, IValue> values) { return(trie.Set(values.ToImmutableDictionary( pair => Encoding.UTF8.GetBytes(pair.Key), pair => pair.Value))); }