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 TestTrieFlush()
        {
            var memDb = new MemoryDictionarySource();
            var trie  = new PatriciaTrie(memDb);

            trie.Put(dog, cat);
            trie.Put(fish, bird);
            Assert.Equal(cat, trie.Get(dog));

            Assert.Empty(memDb.Db.Keys);
            trie.Flush();
            Assert.NotEmpty(memDb.Db.Keys); // This should be more specific in future. How many nodes are we expecting?
        }
        public void TestTrieGetPut()
        {
            // We can retrieve the values we put in
            var trie = new PatriciaTrie();

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

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

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

            Assert.Equal(bird, trie.Get(dog));
        }
        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))));
            }
        }
        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());
        }
        public void TestTrieGetAfterFlush()
        {
            var memDb = new MemoryDictionarySource();
            var trie  = new PatriciaTrie(memDb);

            trie.Put(dog, cat);
            Assert.Equal(cat, trie.Get(dog));
            trie.Flush();
            Assert.Equal(cat, trie.Get(dog));
        }
        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));
        }
Exemple #8
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));
        }
Exemple #9
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        private async Task SetupTrie()
        {
            Trie = new PatriciaTrie(_unitOfWork.TrieRepository);
            var height = await _unitOfWork.HashChainRepository.CountAsync() - 1;

            var blockHeader =
                await _unitOfWork.HashChainRepository.GetAsync(x => new ValueTask <bool>(x.Height == height));

            if (blockHeader == null)
            {
                return;
            }
            Trie.Put(blockHeader.ToHash(), blockHeader.ToHash());
            Trie.Flush();
        }
Exemple #10
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));
        }