Exemplo n.º 1
0
        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));
        }
Exemplo n.º 2
0
        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?
        }
Exemplo n.º 3
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();
        }
Exemplo n.º 4
0
        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))));
            }
        }
Exemplo n.º 5
0
        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));
        }