Example #1
0
        public HashSet <byte[]> GetProof(UInt256 root, StorageKey skey)
        {
            using ISnapshot snapshot = store.GetSnapshot();
            var trie = new MPTTrie <StorageKey, StorageItem>(snapshot, root);

            return(trie.GetProof(skey));
        }
Example #2
0
        public void TestVerifyProof()
        {
            var mpt = new MPTTrie <TestKey, TestValue>(mptdb.GetSnapshot(), root.Hash);
            HashSet <byte[]> proof = mpt.GetProof("ac01".HexToBytes());
            TestValue        value = MPTTrie <TestKey, TestValue> .VerifyProof(root.Hash, "ac01".HexToBytes(), proof);

            Assert.IsNotNull(value);
            Assert.AreEqual(value.ToString(), "abcd");
        }
Example #3
0
        public void TestVerifyProof()
        {
            var mpt    = new MPTTrie(rootHash, mptdb);
            var result = mpt.GetProof("ac01".HexToBytes(), out HashSet <byte[]> proof);

            Assert.IsTrue(result);
            result = MPTTrie.VerifyProof(rootHash, "ac01".HexToBytes(), proof, out byte[] value);
            Assert.IsTrue(result);
        }
Example #4
0
        public void TestGetProof()
        {
            var b = new BranchNode();
            var r = new ExtensionNode {
                Key = "0a0c".HexToBytes(), Next = b
            };
            var v1 = new LeafNode {
                Value = "abcd".HexToBytes()
            };
            var v2 = new LeafNode {
                Value = "2222".HexToBytes()
            };
            var v3 = new LeafNode {
                Value = Encoding.ASCII.GetBytes("hello")
            };
            var h1 = new HashNode(v3.Hash);
            var l1 = new ExtensionNode {
                Key = new byte[] { 0x01 }, Next = v1
            };
            var l2 = new ExtensionNode {
                Key = new byte[] { 0x09 }, Next = v2
            };
            var l3 = new ExtensionNode {
                Key = "0e".HexToBytes(), Next = h1
            };

            b.Children[0]  = l1;
            b.Children[9]  = l2;
            b.Children[10] = l3;

            var mpt = new MPTTrie <TestKey, TestValue>(mptdb.GetSnapshot(), root.Hash);

            Assert.AreEqual(r.Hash.ToString(), mpt.Root.Hash.ToString());
            HashSet <byte[]> proof = mpt.GetProof("ac01".HexToBytes());

            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()));

            proof = mpt.GetProof(Array.Empty <byte>());
            Assert.IsNull(proof);
        }
Example #5
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()));
        }
Example #6
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());
        }
Example #7
0
        public void TestGetProof()
        {
            var b  = MPTNode.NewBranch();
            var r  = MPTNode.NewExtension("0a0c".HexToBytes(), b);
            var v1 = MPTNode.NewLeaf("abcd".HexToBytes());                 //key=ac01
            var v2 = MPTNode.NewLeaf("2222".HexToBytes());                 //key=ac
            var v3 = MPTNode.NewLeaf(Encoding.ASCII.GetBytes("existing")); //key=acae
            var v4 = MPTNode.NewLeaf(Encoding.ASCII.GetBytes("missing"));
            var h3 = MPTNode.NewHash(v3.Hash);
            var e1 = MPTNode.NewExtension(new byte[] { 0x01 }, v1);
            var e3 = MPTNode.NewExtension(new byte[] { 0x0e }, h3);
            var e4 = MPTNode.NewExtension(new byte[] { 0x01 }, v4);

            b.Children[0]  = e1;
            b.Children[10] = e3;
            b.Children[16] = v2;
            b.Children[15] = MPTNode.NewHash(e4.Hash);

            var mpt = new MPTTrie <TestKey, TestValue>(mptdb.GetSnapshot(), r.Hash);

            Assert.AreEqual(r.Hash.ToString(), mpt.Root.Hash.ToString());
            HashSet <byte[]> proof = mpt.GetProof("ac01".HexToBytes());

            Assert.AreEqual(4, proof.Count);
            Assert.IsTrue(proof.Contains(b.ToArrayWithoutReference()));
            Assert.IsTrue(proof.Contains(r.ToArrayWithoutReference()));
            Assert.IsTrue(proof.Contains(e1.ToArrayWithoutReference()));
            Assert.IsTrue(proof.Contains(v1.ToArrayWithoutReference()));

            proof = mpt.GetProof("ac".HexToBytes());
            Assert.AreEqual(3, proof.Count());

            proof = mpt.GetProof("ac10".HexToBytes());
            Assert.IsNull(proof);

            proof = mpt.GetProof("acae".HexToBytes());
            Assert.AreEqual(4, proof.Count());

            proof = mpt.GetProof(Array.Empty <byte>());
            Assert.IsNull(proof);

            proof = mpt.GetProof("ac0100".HexToBytes());
            Assert.IsNull(proof);

            Assert.ThrowsException <InvalidOperationException>(() => mpt.GetProof("acf1".HexToBytes()));
        }
Example #8
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);
        }