public void SmartContractList_ListHasValueTest()
        {
            var listName = "testList";

            var source = new MemoryDictionarySource();
            var root   = new ContractStateRoot(source);

            IContractState       state = root.StartTracking();
            IPersistenceStrategy persistenceStrategy = new PersistenceStrategy(state);

            var persistentState = new PersistentState(
                persistenceStrategy,
                this.contractPrimitiveSerializer,
                uint160.One);

            var list = new SmartContractList <string>(persistentState, listName);

            Assert.Equal((uint)0, list.Count);

            var testItem = "Test";

            list.Add(testItem);
            var item1 = list.GetValue(0);

            Assert.Equal(testItem, item1);
        }
        public void SmartContractList_CanAccessValueUsingIndexTest()
        {
            var listName = "testList";

            var source = new MemoryDictionarySource();
            var root   = new ContractStateRoot(source);

            IContractState       state = root.StartTracking();
            IPersistenceStrategy persistenceStrategy = new PersistenceStrategy(state);
            var persistentState = new PersistentState(
                persistenceStrategy,
                this.contractPrimitiveSerializer,
                uint160.One);

            var list = new SmartContractList <string>(persistentState, listName);

            Assert.Equal((uint)0, list.Count);

            // Set a value in the list
            list.Add("Test");
            list.Add("Test2");

            var testItem  = list.GetValue(0);
            var testItem2 = list.GetValue(1);

            var firstItemHash  = $"{listName}[0]";
            var secondItemHash = $"{listName}[1]";

            var item  = persistentState.GetString(firstItemHash);
            var item2 = persistentState.GetString(secondItemHash);

            Assert.Equal(testItem, item);
            Assert.Equal(testItem2, item2);
        }
        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 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 Test4()
        {
            MemoryDictionarySource source = new MemoryDictionarySource();
            StateRepositoryRoot    root   = new StateRepositoryRoot(source);

            IStateRepository repository = root.StartTracking();

            repository.SetStorageValue(new uint160(cow), cowKey, cowValue);
            repository.SetStorageValue(new uint160(horse), horseKey, horseValue);
            repository.Commit();

            Assert.Equal(cowValue, root.GetStorageValue(new uint160(cow), cowKey));
            Assert.Equal(horseValue, root.GetStorageValue(new uint160(horse), horseKey));
        }
        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 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));
        }
        public void SmartContractList_ListHasCountTest()
        {
            var listName = "testList";

            var source = new MemoryDictionarySource();
            var root   = new ContractStateRepositoryRoot(source);

            IContractStateRepository state = root.StartTracking();
            IPersistenceStrategy     persistenceStrategy = new PersistenceStrategy(state);

            var persistentState = new PersistentState(
                persistenceStrategy,
                uint160.One,
                this.network);

            var list = new SmartContractList <string>(persistentState, listName);

            Assert.Equal((uint)0, list.Count);
            list.Add("Test");
            Assert.Equal((uint)1, list.Count);
        }
示例#10
0
        public void TestSimple()
        {
            ISource <byte[], byte[]> src        = new MemoryDictionarySource();
            WriteCache <byte[]>      writeCache = new WriteCache <byte[]>(src, WriteCache <byte[]> .CacheType.SIMPLE);

            for (int i = 0; i < 10_000; ++i)
            {
                writeCache.Put(IntToKey(i), IntToValue(i));
            }
            // Everything is cached
            Assert.Equal(ToHexString(IntToValue(0)), ToHexString(writeCache.GetCached(IntToKey(0)).Value()));
            Assert.Equal(ToHexString(IntToValue(9_999)), ToHexString(writeCache.GetCached(IntToKey(9_999)).Value()));

            // Everything is flushed
            writeCache.Flush();
            Assert.Null(writeCache.GetCached(IntToKey(0)));
            Assert.Null(writeCache.GetCached(IntToKey(9_999)));
            Assert.Equal(ToHexString(IntToValue(9_999)), ToHexString(writeCache.Get(IntToKey(9_999))));
            Assert.Equal(ToHexString(IntToValue(0)), ToHexString(writeCache.Get(IntToKey(0))));
            // Get not caches, only write cache
            Assert.Null(writeCache.GetCached(IntToKey(0)));

            // Deleting key that is currently in cache
            writeCache.Put(IntToKey(0), IntToValue(12345));
            Assert.Equal(ToHexString(IntToValue(12345)), ToHexString(writeCache.GetCached(IntToKey(0)).Value()));
            writeCache.Delete(IntToKey(0));
            Assert.True(null == writeCache.GetCached(IntToKey(0)) || null == writeCache.GetCached(IntToKey(0)).Value());
            Assert.Equal(ToHexString(IntToValue(0)), ToHexString(src.Get(IntToKey(0))));
            writeCache.Flush();
            Assert.Null(src.Get(IntToKey(0)));

            // Deleting key that is not currently in cache
            Assert.True(null == writeCache.GetCached(IntToKey(1)) || null == writeCache.GetCached(IntToKey(1)).Value());
            Assert.Equal(ToHexString(IntToValue(1)), ToHexString(src.Get(IntToKey(1))));
            writeCache.Delete(IntToKey(1));
            Assert.True(null == writeCache.GetCached(IntToKey(1)) || null == writeCache.GetCached(IntToKey(1)).Value());
            Assert.Equal(ToHexString(IntToValue(1)), ToHexString(src.Get(IntToKey(1))));
            writeCache.Flush();
            Assert.Null(src.Get(IntToKey(1)));
        }
        public void SmartContractList_ListEnumerationTest()
        {
            var listName = "testList";

            var source = new MemoryDictionarySource();
            var root   = new ContractStateRoot(source);

            IContractState       state = root.StartTracking();
            IPersistenceStrategy persistenceStrategy = new PersistenceStrategy(state);
            var persistentState = new PersistentState(
                persistenceStrategy,
                this.contractPrimitiveSerializer,
                uint160.One);

            var list = new SmartContractList <string>(persistentState, listName);

            Assert.Equal((uint)0, list.Count);

            var items = new List <string>
            {
                "this is a test",
                "we should try to find a way",
                "to paramaterize our tests"
            };

            foreach (var item in items)
            {
                // No AddRange
                list.Add(item);
            }

            Assert.Equal((uint)items.Count, list.Count);

            for (var i = 0; i < list.Count; i++)
            {
                Assert.Equal(items[i], list.GetValue((uint)i));
            }
        }
        public void SmartContractList_ListHasMultipleValuesTest()
        {
            var listName = "testList";

            var source = new MemoryDictionarySource();
            var root   = new ContractStateRepositoryRoot(source);

            IContractStateRepository state = root.StartTracking();
            IPersistenceStrategy     persistenceStrategy = new PersistenceStrategy(state);

            var persistentState = new PersistentState(
                persistenceStrategy,
                uint160.One,
                this.network);

            var list = new SmartContractList <string>(persistentState, listName);

            Assert.Equal((uint)0, list.Count);

            var testItem  = "Test";
            var testItem2 = "Test2";
            var testItem3 = "Test3";

            // Set a value in the list
            list.Add(testItem);
            list.Add(testItem2);
            list.Add(testItem3);

            Assert.Equal((uint)3, list.Count);

            var item1 = list.GetValue(0);
            var item2 = list.GetValue(1);
            var item3 = list.GetValue(2);

            Assert.Equal(testItem, item1);
            Assert.Equal(testItem2, item2);
            Assert.Equal(testItem3, item3);
        }