Beispiel #1
0
        public void TrieShouldUpdatable(int repeats)
        {
            const string path = "data1";

            // leveldb
            FileDB.Destroy(path);
            FileDB db = new FileDB(path, true);

            // 랜덤 키 생성 : 키의 길이는 1 - 64
            List <byte[]> keys = new List <byte[]>();

            for (int i = 0; i < repeats; i++)
            {
                keys.Add(SecureRandom.GetBytes(SecureRandom.Next(1, 65)));
            }

            ITrie trie = new Trie(db);

            // put
            foreach (var key in keys)
            {
                trie.Put(key, key.Hash256());
            }

            // update
            foreach (var key in keys)
            {
                trie.Put(key, key.Hash512());
            }

            H256 root = trie.Commit();

            trie.Dispose();

            // update된 내용이 맞는지 확인
            trie = new Trie(db, root);
            foreach (var key in keys)
            {
                Assert.Equal(key.Hash512(), trie.Get(key));
            }

            // 모든 키 삭제
            foreach (var key in keys)
            {
                trie.Del(key);
            }

            root = trie.Commit();
            trie.Dispose();

            // 키에 해당하는 내용이 있는지 확인
            trie = new Trie(db, root);
            foreach (var key in keys)
            {
                Assert.Null(trie.Get(key));
            }
        }
Beispiel #2
0
        public void TrieShouldWorksWithLevelDB(int repeats)
        {
            const string path = "data";

            // leveldb
            FileDB.Destroy(path);
            FileDB db = new FileDB(path, true);

            // 랜덤 키 생성 : 키의 길이는 1 - 64
            List <byte[]> keys = new List <byte[]>();

            for (int i = 0; i < repeats; i++)
            {
                keys.Add(SecureRandom.GetBytes(SecureRandom.Next(1, 65)));
            }

            // for BaseTrie
            {
                ITrie trie = new Trie(db);
                foreach (var key in keys)
                {
                    trie.Put(key, key.Hash256());
                }

                H256 root = trie.Commit();
                trie.Dispose();

                ITrie trie2 = new Trie(db, root);
                foreach (var key in keys)
                {
                    Assert.Equal(key.Hash256(), trie2.Get(key));
                }
            }

            // for SecureTrie
            {
                ITrie trie = new SecureTrie(db);
                foreach (var key in keys)
                {
                    trie.Put(key, key.Hash256());
                }

                H256 root = trie.Commit();
                trie.Dispose();

                ITrie trie2 = new SecureTrie(db, root);
                foreach (var key in keys)
                {
                    Assert.Equal(key.Hash256(), trie2.Get(key));
                }
            }
        }
Beispiel #3
0
        public void FileDBShouldBeEnumerable(int repeats)
        {
            const string path = "filedb/enum";

            // delete existing db
            FileDB.Destroy(path);

            // filedb
            var db = new FileDB(path, true);

            // 랜덤키 밸류 생성
            Dictionary <byte[], byte[]> expected = new Dictionary <byte[], byte[]>(new ByteArrayComparer());

            for (int i = 0; i < repeats; i++)
            {
                expected.Add(SecureRandom.GetBytes(32), SecureRandom.GetBytes(64));
            }

            // insert into db
            foreach (var kv in expected)
            {
                db.Put(kv.Key, kv.Value);
            }

            // enumerate
            int items = 0;

            foreach (var kv in db.AsEnumerable())
            {
                byte[] key   = kv.Key;
                byte[] value = kv.Value;

                Assert.True(expected.ContainsKey(key));
                Assert.Equal(expected[key], value);

                items++;
            }

            Assert.Equal(expected.Count, items);
        }