Exemplo n.º 1
0
        public void TestFindLeadNode()
        {
            // r.Key = 0x0a0c
            // b.Key = 0x00
            // l1.Key = 0x01
            var mpt     = new MPTTrie <TestKey, TestValue>(mptdb.GetSnapshot(), root.Hash);
            var prefix  = new byte[] { 0xac, 0x01 }; // =  FromNibbles(path = { 0x0a, 0x0c, 0x00, 0x01 });
            var results = mpt.Find(prefix).ToArray();

            Assert.AreEqual(1, results.Count());

            prefix = new byte[] { 0xac }; // =  FromNibbles(path = { 0x0a, 0x0c });
            Assert.ThrowsException <InvalidOperationException>(() => mpt.Find(prefix).ToArray());
        }
Exemplo n.º 2
0
        public void TestFindStatesIssue652()
        {
            var snapshot = new TestSnapshot();
            var mpt      = new MPTTrie <TestKey, TestValue>(snapshot, null);

            mpt.Put("abc1".HexToBytes(), "01".HexToBytes());
            mpt.Put("abc3".HexToBytes(), "02".HexToBytes());
            var r = mpt.Find("ab".HexToBytes(), "abd2".HexToBytes()).ToList();

            Assert.AreEqual(0, r.Count);
            r = mpt.Find("ab".HexToBytes(), "abb2".HexToBytes()).ToList();
            Assert.AreEqual(2, r.Count);
            r = mpt.Find("ab".HexToBytes(), "abc2".HexToBytes()).ToList();
            Assert.AreEqual(1, r.Count);
        }
Exemplo n.º 3
0
        public void TestFindWithFrom()
        {
            var snapshot = new TestSnapshot();
            var mpt      = new MPTTrie <TestKey, TestValue>(snapshot, null);

            mpt.Put("aa".HexToBytes(), "02".HexToBytes());
            mpt.Put("aa10".HexToBytes(), "03".HexToBytes());
            mpt.Put("aa50".HexToBytes(), "04".HexToBytes());
            var r = mpt.Find("aa".HexToBytes()).ToList();

            Assert.AreEqual(3, r.Count);
            r = mpt.Find("aa".HexToBytes(), "aa30".HexToBytes()).ToList();
            Assert.AreEqual(1, r.Count);
            r = mpt.Find("aa".HexToBytes(), "aa60".HexToBytes()).ToList();
            Assert.AreEqual(0, r.Count);
            r = mpt.Find("aa".HexToBytes(), "aa10".HexToBytes()).ToList();
            Assert.AreEqual(1, r.Count);
        }
Exemplo n.º 4
0
        public void TestFind()
        {
            var store    = new MemoryStore();
            var snapshot = store.GetSnapshot();
            var mpt1     = new MPTTrie <TestKey, TestValue>(snapshot, null);
            var results  = mpt1.Find(ReadOnlySpan <byte> .Empty).ToArray();

            Assert.AreEqual(0, results.Count());
            var mpt2 = new MPTTrie <TestKey, TestValue>(snapshot, null);

            Assert.IsTrue(mpt2.Put(new byte[] { 0xab, 0xcd, 0xef }, new byte[] { 0x01 }));
            Assert.IsTrue(mpt2.Put(new byte[] { 0xab, 0xcd, 0xe1 }, new byte[] { 0x02 }));
            Assert.IsTrue(mpt2.Put(new byte[] { 0xab }, new byte[] { 0x03 }));
            results = mpt2.Find(ReadOnlySpan <byte> .Empty).ToArray();
            Assert.AreEqual(3, results.Count());
            results = mpt2.Find(new byte[] { 0xab }).ToArray();
            Assert.AreEqual(3, results.Count());
            results = mpt2.Find(new byte[] { 0xab, 0xcd }).ToArray();
            Assert.AreEqual(2, results.Count());
            results = mpt2.Find(new byte[] { 0xac }).ToArray();
            Assert.AreEqual(0, results.Count());
        }
Exemplo n.º 5
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());
        }
Exemplo n.º 6
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());
        }
Exemplo n.º 7
0
        public JObject FindStates(JArray _params)
        {
            var root_hash = UInt256.Parse(_params[0].AsString());

            if (!Settings.Default.FullState && StateStore.Singleton.CurrentLocalRootHash != root_hash)
            {
                throw new RpcException(-100, "Old state not supported");
            }
            var script_hash = UInt160.Parse(_params[1].AsString());
            var prefix      = Convert.FromBase64String(_params[2].AsString());

            byte[] key = Array.Empty <byte>();
            if (3 < _params.Count)
            {
                key = Convert.FromBase64String(_params[3].AsString());
            }
            int count = Settings.Default.MaxFindResultItems;

            if (4 < _params.Count)
            {
                count = int.Parse(_params[4].AsString());
            }
            if (Settings.Default.MaxFindResultItems < count)
            {
                count = Settings.Default.MaxFindResultItems;
            }
            using var store = StateStore.Singleton.GetStoreSnapshot();
            var trie     = new MPTTrie <StorageKey, StorageItem>(store, root_hash);
            var contract = GetHistoricalContractState(trie, script_hash);

            if (contract is null)
            {
                throw new RpcException(-100, "Unknown contract");
            }
            StorageKey pkey = new()
            {
                Id  = contract.Id,
                Key = prefix,
            };
            StorageKey fkey = new()
            {
                Id  = pkey.Id,
                Key = key,
            };
            JObject json = new();
            JArray  jarr = new();
            int     i    = 0;

            foreach (var(ikey, ivalue) in trie.Find(pkey.ToArray(), 0 < key.Length ? fkey.ToArray() : null))
            {
                if (count < i)
                {
                    break;
                }
                if (i < count)
                {
                    JObject j = new();
                    j["key"]   = Convert.ToBase64String(ikey.Key);
                    j["value"] = Convert.ToBase64String(ivalue.Value);
                    jarr.Add(j);
                }
                i++;
            }
            ;
            if (0 < jarr.Count)
            {
                json["firstProof"] = GetProof(trie, contract.Id, Convert.FromBase64String(jarr.First()["key"].AsString()));
            }
            if (1 < jarr.Count)
            {
                json["lastProof"] = GetProof(trie, contract.Id, Convert.FromBase64String(jarr.Last()["key"].AsString()));
            }
            json["truncated"] = count < i;
            json["results"]   = jarr;
            return(json);
        }