Beispiel #1
0
        public void TestBranchDeleteDirty()
        {
            var snapshot = new TestSnapshot();
            var mpt      = new MPTTrie <TestKey, TestValue>(snapshot, null);

            mpt.Put("10".HexToBytes(), "01".HexToBytes());
            mpt.Put("20".HexToBytes(), "02".HexToBytes());
            mpt.Put("30".HexToBytes(), "03".HexToBytes());
            mpt.Commit();
            Assert.AreEqual(7, snapshot.Size);
            var mpt1 = new MPTTrie <TestKey, TestValue>(snapshot, mpt.Root.Hash);

            mpt1.Delete("10".HexToBytes());
            mpt1.Commit();
            Assert.AreEqual(5, snapshot.Size);
            var mpt2 = new MPTTrie <TestKey, TestValue>(snapshot, mpt1.Root.Hash);

            mpt2.Delete("20".HexToBytes());
            mpt2.Commit();
            Assert.AreEqual(2, snapshot.Size);
            var mpt3 = new MPTTrie <TestKey, TestValue>(snapshot, mpt2.Root.Hash);

            mpt3.Delete("30".HexToBytes());
            mpt3.Commit();
            Assert.AreEqual(0, snapshot.Size);
        }
        public void TestAddLongerKey()
        {
            var store    = new MemoryStore();
            var snapshot = store.GetSnapshot();
            var mpt      = new MPTTrie <TestKey, TestValue>(snapshot, null);

            mpt.Put(new byte[] { 0xab }, new byte[] { 0x01 });
            mpt.Put(new byte[] { 0xab, 0xcd }, new byte[] { 0x02 });
            Assert.AreEqual("01", mpt[new byte[] { 0xab }].ToArray().ToHexString());
        }
Beispiel #3
0
        public void TestAddLongerKey()
        {
            var store  = new MemoryStore();
            var mpt    = new MPTTrie(null, store);
            var result = mpt.Put(new byte[] { 0xab }, new byte[] { 0x01 });

            Assert.IsTrue(result);
            result = mpt.Put(new byte[] { 0xab, 0xcd }, new byte[] { 0x02 });
            Assert.IsTrue(result);
        }
Beispiel #4
0
        public void TestAddLongerKey()
        {
            var store    = new MemoryStore();
            var snapshot = store.GetSnapshot();
            var mpt      = new MPTTrie <TestKey, TestValue>(snapshot, null);
            var result   = mpt.Put(new byte[] { 0xab }, new byte[] { 0x01 });

            Assert.IsTrue(result);
            result = mpt.Put(new byte[] { 0xab, 0xcd }, new byte[] { 0x02 });
            Assert.IsTrue(result);
        }
Beispiel #5
0
        public void TestBranchNodeRemainValue()
        {
            var store = new MemoryStore();
            var mpt   = new MPTTrie <TestKey, TestValue>(store.GetSnapshot(), null);

            Assert.IsTrue(mpt.Put("ac11".HexToBytes(), "ac11".HexToBytes()));
            Assert.IsTrue(mpt.Put("ac22".HexToBytes(), "ac22".HexToBytes()));
            Assert.IsTrue(mpt.Put("ac".HexToBytes(), "ac".HexToBytes()));
            Assert.IsTrue(mpt.Delete("ac11".HexToBytes()));
            mpt.Delete("ac22".HexToBytes());
            Assert.IsNotNull(mpt["ac".HexToBytes()]);
        }
Beispiel #6
0
        public void TestTryPut()
        {
            var store  = new MemoryStore();
            var mpt    = new MPTTrie(null, store);
            var result = mpt.Put("ac01".HexToBytes(), "abcd".HexToBytes());

            Assert.IsTrue(result);
            result = mpt.Put("ac99".HexToBytes(), "2222".HexToBytes());
            Assert.IsTrue(result);
            result = mpt.Put("acae".HexToBytes(), Encoding.ASCII.GetBytes("hello"));
            Assert.IsTrue(result);
            Assert.AreEqual(rootHash.ToString(), mpt.GetRoot().ToString());
        }
Beispiel #7
0
        public void TestTryPut()
        {
            var store  = new MemoryStore();
            var mpt    = new MPTTrie <TestKey, TestValue>(store.GetSnapshot(), null);
            var result = mpt.Put("ac01".HexToBytes(), "abcd".HexToBytes());

            Assert.IsTrue(result);
            result = mpt.Put("ac99".HexToBytes(), "2222".HexToBytes());
            Assert.IsTrue(result);
            result = mpt.Put("acae".HexToBytes(), Encoding.ASCII.GetBytes("hello"));
            Assert.IsTrue(result);
            Assert.AreEqual(root.Hash.ToString(), mpt.Root.Hash.ToString());
        }
        public void TestFindStatesIssue652()
        {
            var snapshot = new TestSnapshot();
            var mpt      = new MPTTrie <TestKey, TestValue>(snapshot, null);

            mpt.Put("abc1".HexToBytes(), "01".HexToBytes());
            mpt.Put("abc3".HexToBytes(), "02".HexToBytes());
            var r = mpt.Find("ab".HexToBytes(), "abd2".HexToBytes()).ToList();

            Assert.AreEqual(0, r.Count);
            r = mpt.Find("ab".HexToBytes(), "abb2".HexToBytes()).ToList();
            Assert.AreEqual(2, r.Count);
            r = mpt.Find("ab".HexToBytes(), "abc2".HexToBytes()).ToList();
            Assert.AreEqual(1, r.Count);
        }
Beispiel #9
0
        public void TestExtensionPutDirty()
        {
            var snapshot = new TestSnapshot();
            var mpt      = new MPTTrie <TestKey, TestValue>(snapshot, null);

            mpt.Put("a1".HexToBytes(), "01".HexToBytes());
            mpt.Put("a2".HexToBytes(), "02".HexToBytes());
            mpt.Commit();
            Assert.AreEqual(4, snapshot.Size);
            var mpt1 = new MPTTrie <TestKey, TestValue>(snapshot, mpt.Root.Hash);

            mpt1.Put("a3".HexToBytes(), "03".HexToBytes());
            mpt1.Commit();
            Assert.AreEqual(5, snapshot.Size);
        }
Beispiel #10
0
        public void TestSplitKey()
        {
            var store = new MemoryStore();
            var mpt1  = new MPTTrie(null, store);

            Assert.IsTrue(mpt1.Put(new byte[] { 0xab, 0xcd }, new byte[] { 0x01 }));
            Assert.IsTrue(mpt1.Put(new byte[] { 0xab }, new byte[] { 0x02 }));
            Assert.IsTrue(mpt1.GetProof(new byte[] { 0xab, 0xcd }, out HashSet <byte[]> set1));
            Assert.AreEqual(4, set1.Count);
            var mpt2 = new MPTTrie(null, store);

            Assert.IsTrue(mpt2.Put(new byte[] { 0xab }, new byte[] { 0x02 }));
            Assert.IsTrue(mpt2.Put(new byte[] { 0xab, 0xcd }, new byte[] { 0x01 }));
            Assert.IsTrue(mpt2.GetProof(new byte[] { 0xab, 0xcd }, out HashSet <byte[]> set2));
            Assert.AreEqual(4, set2.Count);
            Assert.AreEqual(mpt1.GetRoot(), mpt2.GetRoot());
        }
Beispiel #11
0
        public void TestBranchNodeRemainValue()
        {
            var store  = new MemoryStore();
            var mpt    = new MPTTrie(null, store);
            var result = mpt.Put("ac11".HexToBytes(), "ac11".HexToBytes());

            Assert.IsTrue(result);
            result = mpt.Put("ac22".HexToBytes(), "ac22".HexToBytes());
            Assert.IsTrue(result);
            result = mpt.Put("ac".HexToBytes(), "ac".HexToBytes());
            Assert.IsTrue(result);
            result = mpt.TryDelete("ac11".HexToBytes());
            Assert.IsTrue(result);
            result = mpt.TryDelete("ac22".HexToBytes());
            Assert.IsTrue(result);
            Assert.AreEqual("{\"key\":\"0a0c\",\"next\":{\"value\":\"ac\"}}", mpt.ToJson().ToString());
        }
Beispiel #12
0
        public void TestReference2()
        {
            var snapshot = new TestSnapshot();
            var mpt      = new MPTTrie <TestKey, TestValue>(snapshot, null);

            mpt.Put("a101".HexToBytes(), "01".HexToBytes());
            mpt.Put("a201".HexToBytes(), "01".HexToBytes());
            mpt.Put("a301".HexToBytes(), "01".HexToBytes());
            mpt.Commit();
            Assert.AreEqual(4, snapshot.Size);
            mpt.Delete("a301".HexToBytes());
            mpt.Commit();
            Assert.AreEqual(4, snapshot.Size);
            mpt.Delete("a201".HexToBytes());
            mpt.Commit();
            Assert.AreEqual(2, snapshot.Size);
            Assert.AreEqual("01", mpt["a101".HexToBytes()].ToArray().ToHexString());
        }
Beispiel #13
0
        public void TestBranchNodeRemainValue()
        {
            var snapshot = new TestSnapshot();
            var mpt      = new MPTTrie <TestKey, TestValue>(snapshot, null);

            Assert.IsTrue(mpt.Put("ac11".HexToBytes(), "ac11".HexToBytes()));
            Assert.IsTrue(mpt.Put("ac22".HexToBytes(), "ac22".HexToBytes()));
            Assert.IsTrue(mpt.Put("ac".HexToBytes(), "ac".HexToBytes()));
            mpt.Commit();
            Assert.AreEqual(7, snapshot.Size);
            Assert.IsTrue(mpt.Delete("ac11".HexToBytes()));
            mpt.Commit();
            Assert.AreEqual(5, snapshot.Size);
            Assert.IsTrue(mpt.Delete("ac22".HexToBytes()));
            Assert.IsNotNull(mpt["ac".HexToBytes()]);
            mpt.Commit();
            Assert.AreEqual(2, snapshot.Size);
        }
Beispiel #14
0
        public void TestDeleteSameValue()
        {
            var store    = new MemoryStore();
            var snapshot = store.GetSnapshot();
            var mpt      = new MPTTrie <TestKey, TestValue>(snapshot, null);

            Assert.IsTrue(mpt.Put("ac01".HexToBytes(), "abcd".HexToBytes()));
            Assert.IsTrue(mpt.Put("ac02".HexToBytes(), "abcd".HexToBytes()));
            Assert.IsNotNull(mpt["ac01".HexToBytes()]);
            Assert.IsNotNull(mpt["ac02".HexToBytes()]);
            mpt.Delete("ac01".HexToBytes());
            Assert.IsNotNull(mpt["ac02".HexToBytes()]);
            mpt.Commit();
            snapshot.Commit();
            var mpt0 = new MPTTrie <TestKey, TestValue>(store.GetSnapshot(), mpt.Root.Hash);

            Assert.IsNotNull(mpt0["ac02".HexToBytes()]);
        }
Beispiel #15
0
        public void TestDeleteRemainCanResolve()
        {
            var store    = new MemoryStore();
            var snapshot = store.GetSnapshot();
            var mpt1     = new MPTTrie <TestKey, TestValue>(snapshot, null);

            Assert.IsTrue(mpt1.Put("ac00".HexToBytes(), "abcd".HexToBytes()));
            Assert.IsTrue(mpt1.Put("ac10".HexToBytes(), "abcd".HexToBytes()));
            mpt1.Commit();
            snapshot.Commit();
            var snapshot2 = store.GetSnapshot();
            var mpt2      = new MPTTrie <TestKey, TestValue>(snapshot2, mpt1.Root.Hash);

            Assert.IsTrue(mpt2.Delete("ac00".HexToBytes()));
            mpt2.Commit();
            snapshot2.Commit();
            Assert.IsTrue(mpt2.Delete("ac10".HexToBytes()));
        }
Beispiel #16
0
        public void TestFindWithFrom()
        {
            var snapshot = new TestSnapshot();
            var mpt      = new MPTTrie <TestKey, TestValue>(snapshot, null);

            mpt.Put("aa".HexToBytes(), "02".HexToBytes());
            mpt.Put("aa10".HexToBytes(), "03".HexToBytes());
            mpt.Put("aa50".HexToBytes(), "04".HexToBytes());
            var r = mpt.Find("aa".HexToBytes()).ToList();

            Assert.AreEqual(3, r.Count);
            r = mpt.Find("aa".HexToBytes(), "aa30".HexToBytes()).ToList();
            Assert.AreEqual(1, r.Count);
            r = mpt.Find("aa".HexToBytes(), "aa60".HexToBytes()).ToList();
            Assert.AreEqual(0, r.Count);
            r = mpt.Find("aa".HexToBytes(), "aa10".HexToBytes()).ToList();
            Assert.AreEqual(1, r.Count);
        }
Beispiel #17
0
        public void TestTryPut()
        {
            var store = new MemoryStore();
            var mpt   = new MPTTrie <TestKey, TestValue>(store.GetSnapshot(), null);

            Assert.IsTrue(mpt.Put("ac01".HexToBytes(), "abcd".HexToBytes()));
            Assert.IsTrue(mpt.Put("ac".HexToBytes(), "2222".HexToBytes()));
            Assert.IsTrue(mpt.Put("acae".HexToBytes(), Encoding.ASCII.GetBytes("existing")));
            Assert.IsTrue(mpt.Put("acf1".HexToBytes(), Encoding.ASCII.GetBytes("missing")));
            Assert.AreEqual(root.Hash.ToString(), mpt.Root.Hash.ToString());
            Assert.IsFalse(mpt.Put(Array.Empty <byte>(), "01".HexToBytes()));
            Assert.IsFalse(mpt.Put("01".HexToBytes(), Array.Empty <byte>()));
            Assert.IsFalse(mpt.Put(new byte[MPTNode.MaxKeyLength / 2 + 1], Array.Empty <byte>()));
            Assert.IsFalse(mpt.Put("01".HexToBytes(), new byte[MPTNode.MaxValueLength + 1]));
            Assert.IsTrue(mpt.Put("ac01".HexToBytes(), "ab".HexToBytes()));
        }
Beispiel #18
0
        public void TestSplitKey()
        {
            var store    = new MemoryStore();
            var snapshot = store.GetSnapshot();
            var mpt1     = new MPTTrie <TestKey, TestValue>(snapshot, null);

            Assert.IsTrue(mpt1.Put(new byte[] { 0xab, 0xcd }, new byte[] { 0x01 }));
            Assert.IsTrue(mpt1.Put(new byte[] { 0xab }, new byte[] { 0x02 }));
            HashSet <byte[]> set1 = mpt1.GetProof(new byte[] { 0xab, 0xcd });

            Assert.AreEqual(4, set1.Count);
            var mpt2 = new MPTTrie <TestKey, TestValue>(snapshot, null);

            Assert.IsTrue(mpt2.Put(new byte[] { 0xab }, new byte[] { 0x02 }));
            Assert.IsTrue(mpt2.Put(new byte[] { 0xab, 0xcd }, new byte[] { 0x01 }));
            HashSet <byte[]> set2 = mpt2.GetProof(new byte[] { 0xab, 0xcd });

            Assert.AreEqual(4, set2.Count);
            Assert.AreEqual(mpt1.Root.Hash, mpt2.Root.Hash);
        }
Beispiel #19
0
        public void TestFind()
        {
            var store    = new MemoryStore();
            var snapshot = store.GetSnapshot();
            var mpt1     = new MPTTrie <TestKey, TestValue>(snapshot, null);
            var results  = mpt1.Find(ReadOnlySpan <byte> .Empty).ToArray();

            Assert.AreEqual(0, results.Count());
            var mpt2 = new MPTTrie <TestKey, TestValue>(snapshot, null);

            Assert.IsTrue(mpt2.Put(new byte[] { 0xab, 0xcd, 0xef }, new byte[] { 0x01 }));
            Assert.IsTrue(mpt2.Put(new byte[] { 0xab, 0xcd, 0xe1 }, new byte[] { 0x02 }));
            Assert.IsTrue(mpt2.Put(new byte[] { 0xab }, new byte[] { 0x03 }));
            results = mpt2.Find(ReadOnlySpan <byte> .Empty).ToArray();
            Assert.AreEqual(3, results.Count());
            results = mpt2.Find(new byte[] { 0xab }).ToArray();
            Assert.AreEqual(3, results.Count());
            results = mpt2.Find(new byte[] { 0xab, 0xcd }).ToArray();
            Assert.AreEqual(2, results.Count());
            results = mpt2.Find(new byte[] { 0xac }).ToArray();
            Assert.AreEqual(0, results.Count());
        }
Beispiel #20
0
        public void TestDeleteSameValue()
        {
            var store  = new MemoryStore();
            var mpt    = new MPTTrie(null, store);
            var result = mpt.Put("ac01".HexToBytes(), "abcd".HexToBytes());

            Assert.IsTrue(result);
            result = mpt.Put("ac02".HexToBytes(), "abcd".HexToBytes());
            Assert.IsTrue(result);
            result = mpt.TryGet("ac01".HexToBytes(), out byte[] value);
            Assert.IsTrue(result);
            result = mpt.TryGet("ac02".HexToBytes(), out value);
            Assert.IsTrue(result);
            result = mpt.TryDelete("ac01".HexToBytes());
            result = mpt.TryGet("ac02".HexToBytes(), out value);
            Assert.IsTrue(result);

            var mpt0 = new MPTTrie(mpt.GetRoot(), store);

            result = mpt0.TryGet("ac02".HexToBytes(), out value);
            Assert.IsTrue(result);
        }
Beispiel #21
0
        public void TestReference1()
        {
            var store    = new MemoryStore();
            var snapshot = store.GetSnapshot();
            var mpt      = new MPTTrie <TestKey, TestValue>(snapshot, null);

            mpt.Put("a101".HexToBytes(), "01".HexToBytes());
            mpt.Put("a201".HexToBytes(), "01".HexToBytes());
            mpt.Put("a301".HexToBytes(), "01".HexToBytes());
            mpt.Commit();
            snapshot.Commit();
            var snapshot1 = store.GetSnapshot();
            var mpt1      = new MPTTrie <TestKey, TestValue>(snapshot1, mpt.Root.Hash);

            mpt1.Delete("a301".HexToBytes());
            mpt1.Commit();
            snapshot1.Commit();
            var snapshot2 = store.GetSnapshot();
            var mpt2      = new MPTTrie <TestKey, TestValue>(snapshot2, mpt1.Root.Hash);

            mpt2.Delete("a201".HexToBytes());
            Assert.AreEqual("01", mpt2["a101".HexToBytes()]?.ToArray().ToHexString());
        }
Beispiel #22
0
        public void TestEmptyValueIssue633()
        {
            var key      = "01".HexToBytes();
            var snapshot = new TestSnapshot();
            var mpt      = new MPTTrie <TestKey, TestValue>(snapshot, null);

            mpt.Put(key, Array.Empty <byte>());
            var val = mpt["01".HexToBytes()];

            Assert.IsNotNull(val);
            Assert.AreEqual(0, val.Size);
            var r = mpt.TryGetProof(key, out var proof);

            Assert.IsTrue(r);
            val = MPTTrie <TestKey, TestValue> .VerifyProof(mpt.Root.Hash, key, proof);

            Assert.IsNotNull(val);
            Assert.AreEqual(0, val.Size);
        }
Beispiel #23
0
 protected override void AddInternal(TKey key, TValue value)
 {
     batch?.Put(prefix, key, value);
     mptTrie.Put(key.ToArray(), value.ToArray());
 }
Beispiel #24
0
        public void TestPutCantResolve()
        {
            var mpt = new MPTTrie <TestKey, TestValue>(mptdb.GetSnapshot(), root.Hash);

            Assert.ThrowsException <InvalidOperationException>(() => mpt.Put("acf111".HexToBytes(), new byte[] { 1 }));
        }