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()); }
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()); }
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()); results = mpt2.Find(new byte[] { 0xab, 0xcd, 0xef, 0x00 }).ToArray(); Assert.AreEqual(0, results.Count()); }
public void TestTryGet() { var mpt = new MPTTrie(rootHash, mptdb); var result = mpt.TryGet("ac01".HexToBytes(), out byte[] value); Assert.IsTrue(result); Assert.AreEqual("abcd", value.ToHexString()); result = mpt.TryGet("ac99".HexToBytes(), out value); Assert.IsTrue(result); Assert.AreEqual("2222", value.ToHexString()); result = mpt.TryGet("ab99".HexToBytes(), out value); Assert.IsFalse(result); result = mpt.TryGet("ac39".HexToBytes(), out value); Assert.IsFalse(result); result = mpt.TryGet("ac02".HexToBytes(), out value); Assert.IsFalse(result); result = mpt.TryGet("ac9910".HexToBytes(), out value); Assert.AreEqual(false, result); }
private string VerifyProof(UInt256 root_hash, byte[] proof) { var proofs = new HashSet <byte[]>(); using MemoryStream ms = new MemoryStream(proof, false); using BinaryReader reader = new BinaryReader(ms, Utility.StrictUTF8); var key = reader.ReadVarBytes(MPTNode.MaxKeyLength); var count = reader.ReadVarInt(); for (ulong i = 0; i < count; i++) { proofs.Add(reader.ReadVarBytes()); } var skey = key.AsSerializable <StorageKey>(); var sitem = MPTTrie <StorageKey, StorageItem> .VerifyProof(root_hash, skey, proofs); if (sitem is null) { throw new RpcException(-100, "Verification failed"); } return(Convert.ToBase64String(sitem.Value)); }
public void TestPutCantResolve() { var mpt = new MPTTrie <TestKey, TestValue>(mptdb.GetSnapshot(), root.Hash); Assert.ThrowsException <InvalidOperationException>(() => mpt.Put("acf111".HexToBytes(), new byte[] { 1 })); }
public void TestTryGetResolve() { var mpt = new MPTTrie <TestKey, TestValue>(mptdb.GetSnapshot(), root.Hash); Assert.AreEqual(Encoding.ASCII.GetBytes("existing").ToHexString(), mpt["acae".HexToBytes()].ToString()); }
public StateSnapshot(IStore store) { snapshot = store.GetSnapshot(); Trie = new MPTTrie <StorageKey, StorageItem>(snapshot, CurrentLocalRootHash(), Settings.Default.FullState); }
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); }