コード例 #1
0
ファイル: TrieTest.cs プロジェクト: tor0405/libplanet
        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);
        }
コード例 #2
0
        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));
        }
コード例 #3
0
ファイル: TrieTest.cs プロジェクト: planetarium/libplanet
        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);
        }