示例#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);
        }
示例#2
0
        public void TestExtensionSerializeAsChild()
        {
            var e      = MPTNode.NewExtension("010a".HexToBytes(), new MPTNode());
            var expect = "03" + Crypto.Hash256(new byte[] { 0x01, 0x02, 0x01, 0x0a, 0x04 }).ToHexString();

            Assert.AreEqual(expect, NodeToArrayAsChild(e).ToHexString());
        }
示例#3
0
        public void TestCloneExtension()
        {
            var l  = MPTNode.NewLeaf(Encoding.ASCII.GetBytes("leaf"));
            var n  = MPTNode.NewExtension(new byte[] { 0x01 }, new MPTNode());
            var n1 = n.Clone();

            n1.Next = l;
            Assert.IsTrue(n.Next.IsEmpty);
        }
示例#4
0
        public void TestExtensionSerialize()
        {
            var e      = MPTNode.NewExtension("010a".HexToBytes(), new MPTNode());
            var expect = "01" + "02" + "010a" + "04";

            Assert.AreEqual(expect, e.ToArrayWithoutReference().ToHexString());
            expect += "01";
            Assert.AreEqual(expect, e.ToArray().ToHexString());
            Assert.AreEqual(6, e.Size);
        }
示例#5
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);
        }
示例#6
0
        public void TestResolveExtension()
        {
            var e     = MPTNode.NewExtension(new byte[] { 0x01 }, new MPTNode());
            var store = new MemoryStore();

            store.Put(Prefix, e.Hash.ToArray(), e.ToArray());
            var snapshot = store.GetSnapshot();
            var cache    = new MPTCache(snapshot, Prefix);
            var re       = cache.Resolve(e.Hash);

            Assert.AreEqual(e.Hash, re.Hash);
            Assert.AreEqual(e.Key.ToHexString(), re.Key.ToHexString());
            Assert.IsTrue(re.Next.IsEmpty);
        }
示例#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()));
        }
示例#8
0
        public void TestPutAndChangedExtension()
        {
            var e        = MPTNode.NewExtension(new byte[] { 0x01 }, new MPTNode());
            var h        = e.Hash;
            var store    = new MemoryStore();
            var snapshot = store.GetSnapshot();
            var cache    = new MPTCache(snapshot, Prefix);

            cache.PutNode(e);
            var re = cache.Resolve(e.Hash);

            Assert.AreEqual(e.Hash, re.Hash);
            Assert.AreEqual(e.Key.ToHexString(), re.Key.ToHexString());
            Assert.IsTrue(re.Next.IsEmpty);
            e.Key  = new byte[] { 0x02 };
            e.Next = e;
            e.SetDirty();
            var re1 = cache.Resolve(h);

            Assert.AreEqual(h, re1.Hash);
            Assert.AreEqual("01", re1.Key.ToHexString());
            Assert.IsTrue(re1.Next.IsEmpty);
        }
示例#9
0
        public void TestFromNibblesException()
        {
            var b  = MPTNode.NewBranch();
            var r  = MPTNode.NewExtension("0c".HexToBytes(), b);
            var v1 = MPTNode.NewLeaf("abcd".HexToBytes()); //key=ac01
            var v2 = MPTNode.NewLeaf("2222".HexToBytes()); //key=ac
            var e1 = MPTNode.NewExtension(new byte[] { 0x01 }, v1);

            b.Children[0]  = e1;
            b.Children[16] = v2;
            var store = new MemoryStore();

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

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

            Assert.ThrowsException <FormatException>(() => mpt.Find(Array.Empty <byte>()).Count());
        }
示例#10
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());
        }
示例#11
0
 public void TestNewExtensionException()
 {
     Assert.ThrowsException <ArgumentNullException>(() => MPTNode.NewExtension(null, new MPTNode()));
     Assert.ThrowsException <ArgumentNullException>(() => MPTNode.NewExtension(new byte[] { 0x01 }, null));
     Assert.ThrowsException <InvalidOperationException>(() => MPTNode.NewExtension(Array.Empty <byte>(), new MPTNode()));
 }