Esempio n. 1
0
        public void TestInit()
        {
            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);
            this.root      = r;
            this.mptdb     = new MemoryStore();
            PutToStore(mptdb, r);
            PutToStore(mptdb, b);
            PutToStore(mptdb, e1);
            PutToStore(mptdb, e3);
            PutToStore(mptdb, v1);
            PutToStore(mptdb, v2);
            PutToStore(mptdb, v3);
        }
Esempio n. 2
0
        public void TestHashSerialize()
        {
            var n      = MPTNode.NewHash(UInt256.Zero);
            var expect = "030000000000000000000000000000000000000000000000000000000000000000";

            Assert.AreEqual(expect, n.ToArray().ToHexString());
            Assert.AreEqual(expect, NodeToArrayAsChild(n).ToHexString());
        }
Esempio n. 3
0
        public void TestSize()
        {
            var n = new MPTNode();

            Assert.AreEqual(1, n.Size);
            n = MPTNode.NewBranch();
            Assert.AreEqual(19, n.Size);
            n = MPTNode.NewExtension(new byte[] { 0x00 }, new MPTNode());
            Assert.AreEqual(5, n.Size);
            n = MPTNode.NewLeaf(new byte[] { 0x00 });
            Assert.AreEqual(4, n.Size);
            n = MPTNode.NewHash(UInt256.Zero);
            Assert.AreEqual(33, n.Size);
        }
Esempio n. 4
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()));
        }
Esempio n. 5
0
        public void TestFindCantResolve()
        {
            var b  = MPTNode.NewBranch();
            var r  = MPTNode.NewExtension("0a0c".HexToBytes(), b);
            var v1 = MPTNode.NewLeaf("abcd".HexToBytes());//key=ac01
            var v4 = MPTNode.NewLeaf(Encoding.ASCII.GetBytes("missing"));
            var e1 = MPTNode.NewExtension(new byte[] { 0x01 }, v1);
            var e4 = MPTNode.NewExtension(new byte[] { 0x01 }, v4);

            b.Children[0]  = e1;
            b.Children[15] = MPTNode.NewHash(e4.Hash);
            var store = new MemoryStore();

            PutToStore(store, r);
            PutToStore(store, b);
            PutToStore(store, e1);
            PutToStore(store, v1);

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

            Assert.ThrowsException <InvalidOperationException>(() => mpt.Find("ac".HexToBytes()).Count());
        }
Esempio n. 6
0
 public void TestNewHashException()
 {
     Assert.ThrowsException <ArgumentNullException>(() => MPTNode.NewHash(null));
 }