public void VisitExtension(TrieNode node, TrieVisitContext trieVisitContext) { AddProofItem(node, trieVisitContext); _nodeToVisitFilter.Remove(node.Keccak); Keccak childHash = node.GetChildHash(0); if (trieVisitContext.IsStorage) { _storageNodeInfos[childHash] = new StorageNodeInfo(); _storageNodeInfos[childHash].PathIndex = _pathTraversalIndex + node.Path.Length; foreach (int storageIndex in _storageNodeInfos[node.Keccak].StorageIndices) { bool isPathMatched = IsPathMatched(node, _fullStoragePaths[storageIndex]); if (isPathMatched) { _storageNodeInfos[childHash].StorageIndices.Add(storageIndex); _nodeToVisitFilter.Add(childHash); // always accept so can optimize } } } if (IsPathMatched(node, _fullAccountPath)) { _nodeToVisitFilter.Add(childHash); // always accept so can optimize _pathTraversalIndex += node.Path.Length; } }
public void VisitBranch(TrieNode node, TrieVisitContext trieVisitContext) { AddProofBits(node); _visitingFilter.Remove(node.Keccak); _visitingFilter.Add(node.GetChildHash((byte)Prefix[_pathIndex])); _pathIndex++; }
public void VisitExtension(TrieNode node, TrieVisitContext trieVisitContext) { _nodesVisited++; if (trieVisitContext.IsStorage) { _ignoreThisOne.Add(node.GetChildHash(0)); } }
public void VisitTree(Keccak rootHash, TrieVisitContext trieVisitContext) { _stopwatch.Start(); if (_logger.IsWarn) { _logger.Warn($"Full Pruning Started on root hash {rootHash}: do not close the node until finished or progress will be lost."); } }
public void VisitMissingNode(Keccak nodeHash, TrieVisitContext trieVisitContext) { if (_logger.IsWarn) { _logger.Warn($"Full Pruning Failed: Missing node {nodeHash} at level {trieVisitContext.Level}."); } // if nodes are missing then state trie is not valid and we need to stop copying it _pruningContext.CancellationTokenSource.Cancel(); }
public void Unknown_node_with_missing_data_can_accept_visitor() { ITreeVisitor visitor = Substitute.For <ITreeVisitor>(); TrieVisitContext context = new TrieVisitContext(); TrieNode node = new TrieNode(NodeType.Unknown); node.Accept(visitor, NullTrieNodeResolver.Instance, context); visitor.Received().VisitMissingNode(node.Keccak, context); }
public void VisitExtension(TrieNode node, TrieVisitContext trieVisitContext) { AddProofBits(node); _visitingFilter.Remove(node.Keccak); Keccak childHash = node.GetChildHash(0); _visitingFilter.Add(childHash); // always accept so can optimize _pathIndex += node.Path.Length; }
public void Extension_can_accept_visitors() { ITreeVisitor visitor = Substitute.For <ITreeVisitor>(); TrieVisitContext context = new TrieVisitContext(); 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 TrieVisitContext(); Account account = new Account(1, 100, Keccak.EmptyTreeHash, Keccak.OfAnEmptyString); AccountDecoder decoder = new AccountDecoder(); 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 TrieVisitContext(); Account account = new Account(100); AccountDecoder decoder = new AccountDecoder(); 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 Extension_can_accept_visitors() { ITreeVisitor visitor = Substitute.For <ITreeVisitor>(); TrieVisitContext context = new TrieVisitContext(); PatriciaTree tree = new PatriciaTree(); TrieNode ignore = new TrieNode(NodeType.Unknown); TrieNode node = new TrieNode(NodeType.Extension); node.SetChild(0, ignore); node.Accept(visitor, tree, context); visitor.Received().VisitExtension(node, context); }
public void Leaf_with_simple_account_can_accept_visitors() { ITreeVisitor visitor = Substitute.For <ITreeVisitor>(); TrieVisitContext context = new TrieVisitContext(); PatriciaTree tree = new PatriciaTree(); Account account = new Account(100); AccountDecoder decoder = new AccountDecoder(); TrieNode node = new TrieNode(NodeType.Leaf); node.Value = decoder.Encode(account).Bytes; node.Accept(visitor, tree, context); visitor.Received().VisitLeaf(node, context, node.Value); }
public void Leaf_with_contract_without_storage_and_empty_code_can_accept_visitors() { ITreeVisitor visitor = Substitute.For <ITreeVisitor>(); TrieVisitContext context = new TrieVisitContext(); PatriciaTree tree = new PatriciaTree(); Account account = new Account(1, 100, Keccak.EmptyTreeHash, Keccak.OfAnEmptyString); AccountDecoder decoder = new AccountDecoder(); TrieNode node = new TrieNode(NodeType.Leaf); node.Value = decoder.Encode(account).Bytes; node.Accept(visitor, tree, context); visitor.Received().VisitLeaf(node, context, node.Value); }
public void Branch_can_accept_visitors() { ITreeVisitor visitor = Substitute.For <ITreeVisitor>(); TrieVisitContext context = new TrieVisitContext(); TrieNode node = new TrieNode(NodeType.Branch); for (int i = 0; i < 16; i++) { node.SetChild(i, null); } node.Accept(visitor, NullTrieNodeResolver.Instance, context); visitor.Received().VisitBranch(node, context); }
public void Extension_with_leaf_can_be_visited() { ITreeVisitor visitor = Substitute.For <ITreeVisitor>(); visitor.ShouldVisit(Arg.Any <Keccak>()).Returns(true); TrieVisitContext context = new TrieVisitContext(); PatriciaTree tree = new PatriciaTree(); TrieNode node = new TrieNode(NodeType.Extension); node.SetChild(0, _accountLeaf); node.Accept(visitor, tree, context); visitor.Received().VisitExtension(node, context); visitor.Received().VisitLeaf(_accountLeaf, context, _accountLeaf.Value); }
public void VisitLeaf(TrieNode node, TrieVisitContext trieVisitContext, byte[] value = null) { _nodesVisited++; if (trieVisitContext.IsStorage) { return; } AccountDecoder accountDecoder = new AccountDecoder(); Account account = accountDecoder.Decode(node.Value.AsRlpStream()); _balance += account.Balance; _accountsVisited++; _logger.Info($"Balance after visiting {_accountsVisited} accounts and {_nodesVisited} nodes: {_balance}"); }
public void VisitBranch(TrieNode node, TrieVisitContext trieVisitContext) { _logger.Info($"Balance after visiting {_accountsVisited} accounts and {_nodesVisited} nodes: {_balance}"); _nodesVisited++; if (trieVisitContext.IsStorage) { for (int i = 0; i < 16; i++) { Keccak childHash = node.GetChildHash(i); if (childHash != null) { _ignoreThisOne.Add(childHash); } } } }
private void AddEmpty(TrieNode node, TrieVisitContext trieVisitContext) { if (trieVisitContext.IsStorage) { if (_storageNodeInfos.ContainsKey(node.Keccak)) { foreach (int storageIndex in _storageNodeInfos[node.Keccak].StorageIndices) { _storageProofItems[storageIndex].Add(Bytes.Empty); } } } else { _accountProofItems.Add(Bytes.Empty); } }
public void VisitLeaf(TrieNode node, TrieVisitContext trieVisitContext, byte[] value) { AddProofItem(node, trieVisitContext); _nodeToVisitFilter.Remove(node.Keccak); if (trieVisitContext.IsStorage) { foreach (int storageIndex in _storageNodeInfos[node.Keccak].StorageIndices) { Nibble[] thisStoragePath = _fullStoragePaths[storageIndex]; bool isPathMatched = IsPathMatched(node, thisStoragePath); if (isPathMatched) { _accountProof.StorageProofs[storageIndex].Value = new RlpStream(node.Value).DecodeByteArray(); } } } else { Account account = _accountDecoder.Decode(new RlpStream(node.Value)); bool isPathMatched = IsPathMatched(node, _fullAccountPath); if (isPathMatched) { _accountProof.Nonce = account.Nonce; _accountProof.Balance = account.Balance; _accountProof.StorageRoot = account.StorageRoot; _accountProof.CodeHash = account.CodeHash; if (_fullStoragePaths.Length > 0) { _nodeToVisitFilter.Add(_accountProof.StorageRoot); _storageNodeInfos[_accountProof.StorageRoot] = new StorageNodeInfo(); _storageNodeInfos[_accountProof.StorageRoot].PathIndex = 0; for (int i = 0; i < _fullStoragePaths.Length; i++) { _storageNodeInfos[_accountProof.StorageRoot].StorageIndices.Add(i); } } } } _pathTraversalIndex = 0; }
public void VisitBranch(TrieNode node, TrieVisitContext trieVisitContext) { AddProofItem(node, trieVisitContext); _nodeToVisitFilter.Remove(node.Keccak); if (trieVisitContext.IsStorage) { HashSet <int> bumpedIndexes = new HashSet <int>(); foreach (int storageIndex in _storageNodeInfos[node.Keccak].StorageIndices) { Nibble childIndex = _fullStoragePaths[storageIndex][_pathTraversalIndex]; Keccak childHash = node.GetChildHash((byte)childIndex); if (childHash == null) { Console.WriteLine($"Empty at {storageIndex}"); AddEmpty(node, trieVisitContext); } else { if (!_storageNodeInfos.ContainsKey(childHash)) { _storageNodeInfos[childHash] = new StorageNodeInfo(); } if (!bumpedIndexes.Contains((byte)childIndex)) { bumpedIndexes.Add((byte)childIndex); _storageNodeInfos[childHash].PathIndex = _pathTraversalIndex + 1; } _storageNodeInfos[childHash].StorageIndices.Add(storageIndex); _nodeToVisitFilter.Add(childHash); } } } else { _nodeToVisitFilter.Add(node.GetChildHash((byte)_fullAccountPath[_pathTraversalIndex])); } _pathTraversalIndex++; }
public void Branch_with_children_can_be_visited() { ITreeVisitor visitor = Substitute.For <ITreeVisitor>(); visitor.ShouldVisit(Arg.Any <Keccak>()).Returns(true); TrieVisitContext context = new TrieVisitContext(); PatriciaTree tree = new PatriciaTree(); TrieNode node = new TrieNode(NodeType.Branch); for (int i = 0; i < 16; i++) { node.SetChild(i, _accountLeaf); } node.Accept(visitor, tree, context); visitor.Received().VisitBranch(node, context); visitor.Received(16).VisitLeaf(_accountLeaf, context, _accountLeaf.Value); }
public void Branch_with_children_can_be_visited() { Context ctx = new Context(); ITreeVisitor visitor = Substitute.For <ITreeVisitor>(); visitor.ShouldVisit(Arg.Any <Keccak>()).Returns(true); TrieVisitContext context = new TrieVisitContext(); TrieNode node = new TrieNode(NodeType.Branch); for (int i = 0; i < 16; i++) { node.SetChild(i, ctx.AccountLeaf); } node.Accept(visitor, NullTrieNodeResolver.Instance, context); visitor.Received().VisitBranch(node, context); visitor.Received(16).VisitLeaf(ctx.AccountLeaf, context, ctx.AccountLeaf.Value); }
public void VisitLeaf(TrieNode node, TrieVisitContext trieVisitContext, byte[] value = null) { }
public void VisitCode(Keccak codeHash, TrieVisitContext trieVisitContext) { _nodesVisited++; }
public void VisitMissingNode(Keccak nodeHash, TrieVisitContext trieVisitContext) { }
public void VisitTree(Keccak rootHash, TrieVisitContext trieVisitContext) { }
public void VisitLeaf(TrieNode node, TrieVisitContext trieVisitContext, byte[] value) { AddProofBits(node); _visitingFilter.Remove(node.Keccak); _pathIndex = 0; }
public void VisitCode(Keccak codeHash, TrieVisitContext trieVisitContext) { }
public void VisitCode(Keccak codeHash, TrieVisitContext trieVisitContext) { throw new InvalidOperationException($"{nameof(AccountProofCollector)} does never expect to visit code"); }
public void VisitMissingNode(Keccak nodeHash, TrieVisitContext trieVisitContext) { _logger.Warn($"Missing node {nodeHash}"); }