public void TestTrieDeterminism()
        {
            // No matter the order that things are put in, if the contents are the same then root hash is the same
            var trie1 = new PatriciaTrie();
            var trie2 = new PatriciaTrie();

            trie1.Put(dog, cat);
            trie1.Put(fish, bird);

            trie2.Put(fish, bird);
            trie2.Put(dog, cat);

            Assert.Equal(trie1.GetRootHash(), trie2.GetRootHash());

            trie1.Put(dog, bird);
            trie1.Put(dog, fish);
            trie1.Put(dodecahedron, dog);
            trie1.Put(dodecahedron, cat);
            trie1.Put(fish, bird);
            trie1.Put(fish, cat);

            trie2.Put(dog, fish);
            trie2.Put(fish, cat);
            trie2.Put(dodecahedron, cat);

            Assert.Equal(trie1.GetRootHash(), trie2.GetRootHash());
        }
        public void TestDelete()
        {
            var memDb = new MemoryDictionarySource();
            var trie  = new PatriciaTrie(memDb);

            trie.Put(dog, cat);

            byte[] dogCatOnlyHash = trie.GetRootHash();

            trie.Put(fish, bird);
            trie.Delete(fish);

            Assert.Equal(dogCatOnlyHash, trie.GetRootHash());

            trie.Put(fish, bird);
            trie.Put(fish, empty);

            Assert.Equal(dogCatOnlyHash, trie.GetRootHash());
        }
示例#3
0
        public string CalculateTransactionRootHash(IReadOnlyList <Sp8deTransaction> list)
        {
            var trie = new PatriciaTrie();

            foreach (var item in list)
            {
                trie.Put(Encoding.UTF8.GetBytes(item.Id), Encoding.UTF8.GetBytes(item.InternalRoot /*item.Signature*/));
            }

            var outputBytes = trie.GetRootHash();

            return(HexConverter.ToHex(outputBytes));
        }
        public void TestTrieBulkData()
        {
            var memDb = new MemoryDictionarySource();
            var trie  = new PatriciaTrie(memDb);

            Dictionary <string, string> toInput = new Dictionary <string, string>();

            for (int i = 0; i < 1000; i++)
            {
                toInput.Add(
                    new Random().Next().ToString(),
                    new Random().Next().ToString()
                    );
            }

            foreach (var kvp in toInput)
            {
                trie.Put(Encoding.UTF8.GetBytes(kvp.Key), Encoding.UTF8.GetBytes(kvp.Value));
            }

            foreach (var kvp in toInput)
            {
                Assert.Equal(kvp.Value, Encoding.UTF8.GetString(trie.Get(Encoding.UTF8.GetBytes(kvp.Key))));
            }

            trie.Put(dog, cat);
            trie.Put(fish, bird);
            trie.Put(dodecahedron, fish);
            trie.Flush();
            byte[] savedHash = trie.GetRootHash();

            var trie2 = new PatriciaTrie(memDb);

            trie2.SetRootHash(savedHash);

            Assert.Equal(cat, trie.Get(dog));
            Assert.Equal(cat, trie2.Get(dog));
            Assert.Equal(bird, trie2.Get(fish));
            Assert.Equal(fish, trie2.Get(dodecahedron));
            foreach (var kvp in toInput)
            {
                Assert.Equal(kvp.Value, Encoding.UTF8.GetString(trie2.Get(Encoding.UTF8.GetBytes(kvp.Key))));
            }
        }
示例#5
0
        public string CalculateInternalTransactionRootHash(IList <InternalTransaction> list)
        {
            if (list == null && list.Count == 0)
            {
                return(null);
            }

            var trie = new PatriciaTrie();

            for (int i = 0; i < list.Count; i++)
            {
                var item = list[i];
                trie.Put(BitConverter.GetBytes(i), Encoding.UTF8.GetBytes(item.Sign));
            }

            var outputBytes = trie.GetRootHash();

            return(HexConverter.ToHex(outputBytes));
        }
        public void TestTrieLoad()
        {
            var memDb = new MemoryDictionarySource();
            var trie  = new PatriciaTrie(memDb);

            trie.Put(dog, cat);
            trie.Put(fish, bird);
            trie.Put(dodecahedron, fish);
            trie.Flush();
            byte[] savedHash = trie.GetRootHash();

            var trie2 = new PatriciaTrie(memDb);

            trie2.SetRootHash(savedHash);

            Assert.Equal(cat, trie.Get(dog));
            Assert.Equal(cat, trie2.Get(dog));
            Assert.Equal(bird, trie2.Get(fish));
            Assert.Equal(fish, trie2.Get(dodecahedron));
        }