Beispiel #1
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 #2
0
        public void TestGetProof()
        {
            var r = new ExtensionNode();

            r.Key = "0a0c".HexToBytes();
            var b  = new BranchNode();
            var l1 = new ExtensionNode();

            l1.Key = new byte[] { 0x01 };
            var l2 = new ExtensionNode();

            l2.Key = new byte[] { 0x09 };
            var v1 = new LeafNode();

            v1.Value = "abcd".HexToBytes();
            var v2 = new LeafNode();

            v2.Value = "2222".HexToBytes();
            var v3 = new LeafNode();

            v3.Value = Encoding.ASCII.GetBytes("hello");
            var h1 = new HashNode();

            h1.Hash = v3.GetHash();
            var l3 = new ExtensionNode();

            l3.Next = h1;
            l3.Key  = "0e".HexToBytes();


            r.Next         = b;
            b.Children[0]  = l1;
            l1.Next        = v1;
            b.Children[9]  = l2;
            l2.Next        = v2;
            b.Children[10] = l3;

            var mpt = new MPTTrie(rootHash, mptdb);

            Assert.AreEqual(r.GetHash().ToString(), mpt.GetRoot().ToString());
            var result = mpt.GetProof("ac01".HexToBytes(), out HashSet <byte[]> proof);

            Assert.IsTrue(result);
            Assert.AreEqual(4, proof.Count);
            Assert.IsTrue(proof.Contains(b.Encode()));
            Assert.IsTrue(proof.Contains(r.Encode()));
            Assert.IsTrue(proof.Contains(l1.Encode()));
            Assert.IsTrue(proof.Contains(v1.Encode()));
        }
Beispiel #3
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 #4
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 #5
0
        public void TestTryDelete()
        {
            var r1 = new ExtensionNode();

            r1.Key = "0a0c0001".HexToBytes();

            var r = new ExtensionNode();

            r.Key = "0a0c".HexToBytes();

            var b = new BranchNode();

            r.Next = b;

            var l1 = new ExtensionNode();

            l1.Key = new byte[] { 0x01 };
            var v1 = new LeafNode();

            v1.Value      = "abcd".HexToBytes();
            l1.Next       = v1;
            b.Children[0] = l1;

            var l2 = new ExtensionNode();

            l2.Key = new byte[] { 0x09 };
            var v2 = new LeafNode();

            v2.Value      = "2222".HexToBytes();
            l2.Next       = v2;
            b.Children[9] = l2;

            r1.Next = v1;
            Assert.AreEqual("0xdea3ab46e9461e885ed7091c1e533e0a8030b248d39cbc638962394eaca0fbb3", r1.GetHash().ToString());
            Assert.AreEqual("0x93e8e1ffe2f83dd92fca67330e273bcc811bf64b8f8d9d1b25d5e7366b47d60d", r.GetHash().ToString());

            var mpt    = new MPTTrie(rootHash, mptdb);
            var result = true;

            result = mpt.TryGet("ac99".HexToBytes(), out byte[] value);
            Assert.IsTrue(result);
            result = mpt.TryDelete("ac99".HexToBytes());
            Assert.IsTrue(result);
            result = mpt.TryDelete("acae".HexToBytes());
            Assert.IsTrue(result);
            Assert.AreEqual("0xdea3ab46e9461e885ed7091c1e533e0a8030b248d39cbc638962394eaca0fbb3", mpt.GetRoot().ToString());
        }
Beispiel #6
0
 public override void Commit()
 {
     base.Commit();
     trieDb.PutRoot(mptTrie.GetRoot());
 }