예제 #1
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()));
        }
예제 #2
0
파일: UT_MPTTrie.cs 프로젝트: xurxogp/neo
        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);
        }