Example #1
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);
            }
        }
Example #2
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);
            }
        }
Example #3
0
        public void TestLeafSerializeAsChild()
        {
            var l      = MPTNode.NewLeaf(Encoding.ASCII.GetBytes("leaf"));
            var expect = "03" + Crypto.Hash256(new byte[] { 0x02, 0x04 }.Concat(Encoding.ASCII.GetBytes("leaf")).ToArray()).ToHexString();

            Assert.AreEqual(expect, NodeToArrayAsChild(l).ToHexString());
        }
Example #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);
        }
Example #5
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);
        }
Example #6
0
        public void TestCloneLeaf()
        {
            var l = MPTNode.NewLeaf(Encoding.ASCII.GetBytes("leaf"));
            var n = l.Clone();

            n.Value = Encoding.ASCII.GetBytes("value");
            Assert.AreEqual("leaf", Encoding.ASCII.GetString(l.Value));
        }
Example #7
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);
        }
Example #8
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);
        }
Example #9
0
        public void TestLeafSerialize()
        {
            var n      = MPTNode.NewLeaf(Encoding.ASCII.GetBytes("leaf"));
            var expect = "02" + "04" + Encoding.ASCII.GetBytes("leaf").ToHexString();

            Assert.AreEqual(expect, n.ToArrayWithoutReference().ToHexString());
            expect += "01";
            Assert.AreEqual(expect, n.ToArray().ToHexString());
            Assert.AreEqual(7, n.Size);
        }
Example #10
0
        public void TestEmptyLeaf()
        {
            var leaf = MPTNode.NewLeaf(Array.Empty <byte>());
            var data = leaf.ToArray();

            Assert.AreEqual(3, data.Length);
            var l = data.AsSerializable <MPTNode>();

            Assert.AreEqual(NodeType.LeafNode, l.Type);
            Assert.AreEqual(0, l.Value.Length);
        }
Example #11
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);
        }
Example #12
0
        public void TestResolveLeaf()
        {
            var n     = MPTNode.NewLeaf(Encoding.ASCII.GetBytes("leaf"));
            var store = new MemoryStore();

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

            Assert.AreEqual(n.Hash, resolved.Hash);
            Assert.AreEqual(n.Value.ToHexString(), resolved.Value.ToHexString());
        }
Example #13
0
        public void TestReference2()
        {
            var l        = MPTNode.NewLeaf(Encoding.ASCII.GetBytes("leaf"));
            var store    = new MemoryStore();
            var snapshot = store.GetSnapshot();
            var cache    = new MPTCache(snapshot, Prefix);

            cache.PutNode(l);
            cache.PutNode(l);
            cache.DeleteNode(l.Hash);
            var rl = cache.Resolve(l.Hash);

            Assert.AreEqual(1, rl.Reference);
        }
Example #14
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);
        }
Example #15
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 #16
0
        public void TestGetAndChangedLeaf()
        {
            var l     = MPTNode.NewLeaf(Encoding.ASCII.GetBytes("leaf"));
            var store = new MemoryStore();

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

            Assert.AreEqual(l.Hash, rl.Hash);
            Assert.AreEqual("leaf", Encoding.ASCII.GetString(rl.Value));
            rl.Value = new byte[] { 0x01 };
            rl.SetDirty();
            var rl1 = cache.Resolve(l.Hash);

            Assert.AreEqual(l.Hash, rl1.Hash);
            Assert.AreEqual("leaf", Encoding.ASCII.GetString(rl1.Value));
        }
Example #17
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());
        }
Example #18
0
        public void TestReference1()
        {
            var l        = MPTNode.NewLeaf(Encoding.ASCII.GetBytes("leaf"));
            var store    = new MemoryStore();
            var snapshot = store.GetSnapshot();
            var cache    = new MPTCache(snapshot, Prefix);

            cache.PutNode(l);
            cache.Commit();
            snapshot.Commit();
            var snapshot1 = store.GetSnapshot();
            var cache1    = new MPTCache(snapshot1, Prefix);

            cache1.PutNode(l);
            cache1.Commit();
            snapshot1.Commit();
            var snapshot2 = store.GetSnapshot();
            var cache2    = new MPTCache(snapshot2, Prefix);
            var rl        = cache2.Resolve(l.Hash);

            Assert.AreEqual(2, rl.Reference);
        }
Example #19
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());
        }
Example #20
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());
        }
Example #21
0
 public void TestNewLeafException()
 {
     Assert.ThrowsException <ArgumentNullException>(() => MPTNode.NewLeaf(null));
 }
Example #22
0
 public void TestNewLeafException()
 {
     Assert.ThrowsException <ArgumentNullException>(() => MPTNode.NewLeaf(null));
     Assert.ThrowsException <InvalidOperationException>(() => MPTNode.NewLeaf(Array.Empty <byte>()));
 }