public void ListAllStates() { IKeyValueStore keyValueStore = new MemoryKeyValueStore(); MerkleTrie trie = new MerkleTrie(keyValueStore); trie = (MerkleTrie)trie.Set(new KeyBytes(0x01), Null.Value) .Set(new KeyBytes(0x02), Null.Value) .Set(new KeyBytes(0x03), Null.Value) .Set(new KeyBytes(0x04), Null.Value) .Set(new KeyBytes(0xbe, 0xef), Dictionary.Empty); Dictionary <KeyBytes, IValue> states = trie.ListAllStates().ToDictionary(pair => pair.Key, pair => pair.Value); Assert.Equal(5, states.Count); Assert.Equal(Null.Value, states[new KeyBytes(0x01)]); Assert.Equal(Null.Value, states[new KeyBytes(0x02)]); Assert.Equal(Null.Value, states[new KeyBytes(0x03)]); Assert.Equal(Null.Value, states[new KeyBytes(0x04)]); Assert.Equal(Dictionary.Empty, states[new KeyBytes(0xbe, 0xef)]); trie = (MerkleTrie)trie.Commit(); states = trie.ListAllStates().ToDictionary(pair => pair.Key, pair => pair.Value); Assert.Equal(5, states.Count); Assert.Equal(Null.Value, states[new KeyBytes(0x01)]); Assert.Equal(Null.Value, states[new KeyBytes(0x02)]); Assert.Equal(Null.Value, states[new KeyBytes(0x03)]); Assert.Equal(Null.Value, states[new KeyBytes(0x04)]); Assert.Equal(Dictionary.Empty, states[new KeyBytes(0xbe, 0xef)]); }
public void DifferentNodes() { IKeyValueStore keyValueStore = new MemoryKeyValueStore(); MerkleTrie trieA = new MerkleTrie(keyValueStore), trieB = new MerkleTrie(keyValueStore); trieA = (MerkleTrie)trieA.Set(new KeyBytes(0x01), Null.Value) .Set(new KeyBytes(0x02), Null.Value) .Set(new KeyBytes(0x03), Null.Value) .Commit(); trieB = (MerkleTrie)trieB.Set(new KeyBytes(0x01), Dictionary.Empty) .Set(new KeyBytes(0x02), Null.Value) .Set(new KeyBytes(0x04), Null.Value) .Commit(); Dictionary <KeyBytes, (IValue OriginValue, IValue OtherValue)> differentNodes = trieA.DifferentNodes(trieB).ToDictionary( diff => diff.Key, diff => (diff.OriginValue, diff.OtherValue)); Assert.Equal(2, differentNodes.Count); Assert.NotNull(differentNodes[new KeyBytes(1)].OtherValue); Assert.False(differentNodes.ContainsKey(new KeyBytes(2))); Assert.Null(differentNodes[new KeyBytes(3)].OtherValue); Assert.False(differentNodes.ContainsKey(new KeyBytes(4))); }
public void ThrowArgumentNullExceptionWhenSettingNull() { IKeyValueStore keyValueStore = new MemoryKeyValueStore(); ITrie trie = new MerkleTrie(keyValueStore); Assert.Throws <ArgumentNullException>( () => trie.Set(new KeyBytes(0xbe, 0xef), null) ); }
public void ThrowArgumentNullExceptionWhenSettingNull() { IKeyValueStore keyValueStore = new MemoryKeyValueStore(); ITrie trie = new MerkleTrie(keyValueStore); Assert.Throws <ArgumentNullException>(() => { _ = trie.Set(new byte[] { 0xbe, 0xef }, null); }); }
public void EmptyRootHash() { IKeyValueStore keyValueStore = new MemoryKeyValueStore(); ITrie trie = new MerkleTrie(keyValueStore); Assert.Equal(MerkleTrie.EmptyRootHash, trie.Hash); var committedTrie = trie.Commit(); Assert.Equal(MerkleTrie.EmptyRootHash, committedTrie.Hash); trie = trie.Set(default(Address).ToByteArray(), Dictionary.Empty); committedTrie = trie.Commit(); Assert.NotEqual(MerkleTrie.EmptyRootHash, committedTrie.Hash); }
public void DifferentNodes() { IKeyValueStore keyValueStore = new MemoryKeyValueStore(); MerkleTrie trieA = new MerkleTrie(keyValueStore), trieB = new MerkleTrie(keyValueStore); trieA = (MerkleTrie)trieA.Set(new byte[] { 0x01, }, default(Null)) .Set(new byte[] { 0x02, }, default(Null)) .Set(new byte[] { 0x03, }, default(Null)) .Commit(); trieB = (MerkleTrie)trieB.Set(new byte[] { 0x01, }, Dictionary.Empty) .Set(new byte[] { 0x02, }, default(Null)) .Set(new byte[] { 0x04, }, default(Null)) .Commit(); Dictionary <string, (HashDigest <SHA256> Root, IValue Value)[]> differentNodes =
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 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); }