public void AddAccounts_OneLayer_GetRange() { SortedList <Keccak, TrieNode> list = new(); for (int i = 0; i < 100; i++) { byte[] accountBytes = TestItem.GenerateIndexedAccountRlp(i); Keccak randomKeccak = TestItem.GetRandomKeccak(); TrieNode leaf = TrieNodeFactory.CreateLeaf(new HexPrefix(true), accountBytes); list.Add(randomKeccak, leaf); } SnapStorage storage = new(); foreach (var item in list) { storage.AddLeafNode(item.Key, item.Value, 1001); } Keccak startingHash = list.Keys[20]; Keccak endHash = list.Keys[29]; var range1 = storage.GetRange(startingHash, endHash, 1001); var range2 = storage.GetRange(startingHash, endHash, 1002); Assert.AreEqual(10, range1.Length); Assert.IsTrue(AreEqual(20, 29, list, range1)); Assert.AreEqual(10, range2.Length); Assert.IsTrue(AreEqual(20, 29, list, range2)); }
public void AddAccounts_MultipleLayers_GetRange() { SortedList <Keccak, TrieNode> list = new(); for (int i = 0; i < 100; i++) { byte[] accountBytes = TestItem.GenerateIndexedAccountRlp(i); Keccak randomKeccak = TestItem.GetRandomKeccak(); TrieNode leaf = TrieNodeFactory.CreateLeaf(new HexPrefix(true), accountBytes); list.Add(randomKeccak, leaf); } SnapStorage storage = new(); // create bottom layer foreach (var item in list) { storage.AddLeafNode(item.Key, item.Value, 1001); } // block 1002 byte[] updateAccountBytes_1002 = TestItem.GenerateRandomAccountRlp(); TrieNode updateAccount_1002 = TrieNodeFactory.CreateLeaf(new HexPrefix(true), updateAccountBytes_1002); storage.AddLeafNode(list.Keys[21], updateAccount_1002, 1002); byte[] newAccountBytes_1002 = TestItem.GenerateRandomAccountRlp(); TrieNode newAccount_1002 = TrieNodeFactory.CreateLeaf(new HexPrefix(true), newAccountBytes_1002); Keccak newAddress = TestItem.GetRandomKeccak(); while (newAddress <= list.Keys[21] || newAddress >= list.Keys[22]) { newAddress = TestItem.GetRandomKeccak(); } storage.AddLeafNode(newAddress, newAccount_1002, 1002); // block 1003 storage.AddLeafNode(list.Keys[21], null, 1003); Keccak startingHash = list.Keys[20]; Keccak endHash = list.Keys[30]; var range_1001 = storage.GetRange(startingHash, endHash, 1001); var range_1002 = storage.GetRange(startingHash, endHash, 1002); var range_1003 = storage.GetRange(startingHash, endHash, 1003); Assert.AreEqual(11, range_1001.Length); Assert.IsTrue(AreEqual(20, 30, list, range_1001)); Assert.AreEqual(12, range_1002.Length); Assert.AreEqual(range_1002[1].node, updateAccount_1002); Assert.AreEqual(range_1002[2].node, newAccount_1002); Assert.AreEqual(11, range_1003.Length); Assert.AreEqual(range_1003[1].node, newAccount_1002); }
public async Task Can_read_dependent_items_from_state_db_while_waiting_for_dependencies() { StateDb codeDb = new StateDb(); StateDb stateDB = new StateDb(); MemDb tempDb = new MemDb(); SyncConfig syncConfig = new SyncConfig(); syncConfig.FastSync = true; IBlockTree blockTree = Substitute.For <IBlockTree>(); ISyncPeerPool pool = Substitute.For <ISyncPeerPool>(); SyncProgressResolver syncProgressResolver = new SyncProgressResolver(blockTree, NullReceiptStorage.Instance, stateDB, new MemDb(), syncConfig, LimboLogs.Instance); ISyncModeSelector syncModeSelector = new MultiSyncModeSelector(syncProgressResolver, pool, syncConfig, LimboLogs.Instance); StateSyncFeed stateSyncFeed = new StateSyncFeed(codeDb, stateDB, tempDb, syncModeSelector, blockTree, LimboLogs.Instance); // so we want to setup a trie in a structure of -> branch into two leaves // so we can respond with the branch node and with leaves missing // and we can prove that we can read the branch from the temp DB while it is still missing from the State DB AccountDecoder accountDecoder = new AccountDecoder(); TrieNode leaf = TrieNodeFactory.CreateLeaf(new HexPrefix(true, new byte[] { 1, 2, 3 }), accountDecoder.Encode(Account.TotallyEmpty).Bytes); TrieNode branch = TrieNodeFactory.CreateBranch(); branch.SetChild(1, leaf); branch.ResolveKey(true); // PatriciaTree tree = new PatriciaTree(); // tree = new PatriciaTree(); // tree.Set(branch.Keccak.Bytes, branch.Value); stateSyncFeed.ResetStateRoot(0, branch.Keccak); var request = await stateSyncFeed.PrepareRequest(); BuildRequestAndHandleResponse(branch, request, stateSyncFeed); byte[] value = tempDb.Get(branch.Keccak); value.Should().BeEquivalentTo(branch.FullRlp); byte[] valueFromState = stateDB.Get(branch.Keccak); valueFromState.Should().BeNull(); request = await stateSyncFeed.PrepareRequest(); BuildRequestAndHandleResponse(leaf, request, stateSyncFeed); value = tempDb.Get(branch.Keccak); value.Should().BeNull(); valueFromState = stateDB.Get(branch.Keccak); valueFromState.Should().BeEquivalentTo(branch.FullRlp); }
public void Extension_can_accept_visitors() { ITreeVisitor visitor = Substitute.For <ITreeVisitor>(); TrieVisitContext context = new(); TrieNode ignore = TrieNodeFactory.CreateLeaf(HexPrefix.Leaf("ccc"), Array.Empty <byte>()); TrieNode node = TrieNodeFactory.CreateExtension(HexPrefix.Extension("aa"), ignore); node.Accept(visitor, NullTrieNodeResolver.Instance, context); visitor.Received().VisitExtension(node, context); }
public void Leaf_with_contract_without_storage_and_empty_code_can_accept_visitors() { ITreeVisitor visitor = Substitute.For <ITreeVisitor>(); TrieVisitContext context = new(); Account account = new(1, 100, Keccak.EmptyTreeHash, Keccak.OfAnEmptyString); AccountDecoder decoder = new(); TrieNode node = TrieNodeFactory.CreateLeaf(HexPrefix.Leaf("aa"), decoder.Encode(account).Bytes); node.Accept(visitor, NullTrieNodeResolver.Instance, context); visitor.Received().VisitLeaf(node, context, node.Value); }
public void Leaf_with_simple_account_can_accept_visitors() { ITreeVisitor visitor = Substitute.For <ITreeVisitor>(); TrieVisitContext context = new(); Account account = new(100); AccountDecoder decoder = new(); TrieNode node = TrieNodeFactory.CreateLeaf(HexPrefix.Leaf("aa"), decoder.Encode(account).Bytes); node.Accept(visitor, NullTrieNodeResolver.Instance, context); visitor.Received().VisitLeaf(node, context, node.Value); }
public Context() { TiniestLeaf = new TrieNode(NodeType.Leaf); TiniestLeaf.Key = new HexPrefix(true, 5); TiniestLeaf.Value = new byte[] { 10 }; HeavyLeaf = new TrieNode(NodeType.Leaf); HeavyLeaf.Key = new HexPrefix(true, new byte[20]); HeavyLeaf.Value = Keccak.EmptyTreeHash.Bytes.Concat(Keccak.EmptyTreeHash.Bytes).ToArray(); Account account = new(100); AccountDecoder decoder = new(); AccountLeaf = TrieNodeFactory.CreateLeaf( HexPrefix.Leaf("bbb"), decoder.Encode(account).Bytes); }