示例#1
0
        public void TestBranchSerialize()
        {
            var n = MPTNode.NewBranch();

            n.Children[1]  = MPTNode.NewLeaf(Encoding.ASCII.GetBytes("leaf1"));
            n.Children[10] = MPTNode.NewLeaf(Encoding.ASCII.GetBytes("leafa"));
            var expect = "00";

            for (int i = 0; i < MPTNode.BranchChildCount; i++)
            {
                if (i == 1)
                {
                    expect += "03" + Crypto.Hash256(new byte[] { 0x02, 0x05 }.Concat(Encoding.ASCII.GetBytes("leaf1")).ToArray()).ToHexString();
                }
                else if (i == 10)
                {
                    expect += "03" + Crypto.Hash256(new byte[] { 0x02, 0x05 }.Concat(Encoding.ASCII.GetBytes("leafa")).ToArray()).ToHexString();
                }
                else
                {
                    expect += "04";
                }
            }
            expect += "01";
            Assert.AreEqual(expect, n.ToArray().ToHexString());
            Assert.AreEqual(83, n.Size);
        }
示例#2
0
        public void TestPutAndChangedBranch()
        {
            var l        = MPTNode.NewLeaf(Encoding.ASCII.GetBytes("leaf"));
            var b        = MPTNode.NewBranch();
            var h        = b.Hash;
            var store    = new MemoryStore();
            var snapshot = store.GetSnapshot();
            var cache    = new MPTCache(snapshot, Prefix);

            cache.PutNode(b);
            var rb = cache.Resolve(h);

            Assert.AreEqual(h, rb.Hash);
            foreach (var n in rb.Children)
            {
                Assert.IsTrue(n.IsEmpty);
            }
            rb.Children[1] = l;
            rb.SetDirty();
            var rb1 = cache.Resolve(h);

            Assert.AreEqual(h, rb1.Hash);
            foreach (var n in rb1.Children)
            {
                Assert.IsTrue(n.IsEmpty);
            }
        }
示例#3
0
        public void TestGetAndChangedBranch()
        {
            var l     = MPTNode.NewLeaf(Encoding.ASCII.GetBytes("leaf"));
            var b     = MPTNode.NewBranch();
            var store = new MemoryStore();

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

            Assert.AreEqual(resolved_b.Hash, b.Hash);
            foreach (var n in resolved_b.Children)
            {
                Assert.IsTrue(n.IsEmpty);
            }
            resolved_b.Children[1] = l;
            resolved_b.SetDirty();
            var resovled_b1 = cache.Resolve(b.Hash);

            Assert.AreEqual(resovled_b1.Hash, b.Hash);
            foreach (var n in resovled_b1.Children)
            {
                Assert.IsTrue(n.IsEmpty);
            }
        }
示例#4
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);
        }
示例#5
0
        public void TestCloneBranch()
        {
            var l  = MPTNode.NewLeaf(Encoding.ASCII.GetBytes("leaf"));
            var n  = MPTNode.NewBranch();
            var n1 = n.Clone();

            n1.Children[0] = l;
            Assert.IsTrue(n.Children[0].IsEmpty);
        }
示例#6
0
        public void TestFromReplica()
        {
            var l = MPTNode.NewLeaf(new byte[] { 0x00 });
            var n = MPTNode.NewBranch();

            n.Children[1] = l;
            var r = new MPTNode();

            r.FromReplica(n);
            Assert.AreEqual(n.Hash, r.Hash);
            Assert.AreEqual(NodeType.HashNode, r.Children[1].Type);
            Assert.AreEqual(l.Hash, r.Children[1].Hash);
        }
示例#7
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);
        }
示例#8
0
        public void TestBranchSerializeAsChild()
        {
            var n    = MPTNode.NewBranch();
            var data = new List <byte>();

            data.Add(0x00);
            for (int i = 0; i < MPTNode.BranchChildCount; i++)
            {
                data.Add(0x04);
            }
            var expect = "03" + Crypto.Hash256(data.ToArray()).ToHexString();

            Assert.AreEqual(expect, NodeToArrayAsChild(n).ToHexString());
        }
示例#9
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()));
        }
示例#10
0
        public void TestResolveBranch()
        {
            var l = MPTNode.NewLeaf(Encoding.ASCII.GetBytes("leaf"));
            var b = MPTNode.NewBranch();

            b.Children[1] = l;
            var store = new MemoryStore();

            store.Put(Prefix, b.Hash.ToArray(), b.ToArray());
            store.Put(Prefix, l.Hash.ToArray(), l.ToArray());
            var snapshot   = store.GetSnapshot();
            var cache      = new MPTCache(snapshot, Prefix);
            var resolved_b = cache.Resolve(b.Hash);

            Assert.AreEqual(b.Hash, resolved_b.Hash);
            Assert.AreEqual(l.Hash, resolved_b.Children[1].Hash);
            var resolved_l = cache.Resolve(l.Hash);

            Assert.AreEqual(l.Value.ToHexString(), resolved_l.Value.ToHexString());
        }
示例#11
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());
        }
示例#12
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());
        }