public static IEnumerable <LogEntry> FindLogs( this BlockHeader blockHeader, TxReceipt[] receipts, LogFilter logFilter, FindOrder receiptFindOrder = FindOrder.Ascending, // iterating forwards, by default we are interested in all items in order of appearance FindOrder logsFindOrder = FindOrder.Ascending) // iterating forwards, by default we are interested in all items in order of appearance { if (logFilter.Matches(blockHeader.Bloom)) { for (int i = 0; i < receipts.Length; i++) { TxReceipt receipt = GetItemAt(receipts, i, receiptFindOrder); if (logFilter.Matches(receipt.Bloom)) { for (int j = 0; j < receipt.Logs.Length; j++) { var receiptLog = GetItemAt(receipt.Logs, j, logsFindOrder); if (logFilter.Accepts(receiptLog)) { yield return(receiptLog); } } } } } }
private IEnumerable <FilterLog> GetFilterLogs(BlockHeader blockHeader, TxReceipt[] receipts) { if (_filter.Matches(blockHeader.Bloom)) { int logIndex = 0; for (int i = 0; i < receipts.Length; i++) { TxReceipt receipt = receipts[i]; if (_filter.Matches(receipt.Bloom)) { int transactionLogIndex = 0; for (int j = 0; j < receipt.Logs.Length; j++) { var receiptLog = receipt.Logs[j]; if (_filter.Accepts(receiptLog)) { FilterLog filterLog = new( logIndex++, transactionLogIndex++, receipt, receiptLog); yield return(filterLog); } } } } } }
private List <FilterLog> GetFilterLogs(ReceiptsEventArgs e) { List <FilterLog> filterLogs = new(); if (_filter.Matches(e.BlockHeader.Bloom)) { int logIndex = 0; for (int i = 0; i < e.TxReceipts.Length; i++) { TxReceipt receipt = e.TxReceipts[i]; if (_filter.Matches(receipt.Bloom)) { int transactionLogIndex = 0; for (int j = 0; j < receipt.Logs.Length; j++) { var receiptLog = receipt.Logs[j]; if (_filter.Accepts(receiptLog)) { FilterLog filterLog = new( logIndex++, transactionLogIndex++, receipt, receiptLog); filterLogs.Add(filterLog); } } } } } return(filterLogs); }
private void FindLogsInBlock(LogFilter filter, Block currentBlock, List <FilterLog> results) { var receipts = _receiptStorage.FindForBlock(currentBlock); long logIndexInBlock = 0; foreach (var receipt in receipts) { if (receipt == null) { continue; } if (filter.Matches(receipt.Bloom)) { foreach (var log in receipt.Logs) { if (filter.Accepts(log)) { results.Add(new FilterLog(logIndexInBlock, receipt, log)); } logIndexInBlock++; } } else { logIndexInBlock += receipt.Logs.Length; } } }
private IEnumerable <FilterLog> FilterLogsWithBloomsIndex(LogFilter filter, BlockHeader fromBlock, BlockHeader toBlock) { Block FindBlock(long blockNumber) { var block = _blockFinder.FindBlock(blockNumber); if (block == null) { if (_logger.IsError) { _logger.Error($"Could not find block {blockNumber} in database. eth_getLogs will return incomplete results."); } } return(block); } var enumeration = _bloomStorage.GetBlooms(fromBlock.Number, toBlock.Number); foreach (var bloom in enumeration) { if (filter.Matches(bloom) && enumeration.TryGetBlockNumber(out var blockNumber)) { foreach (var filterLog in FindLogsInBlock(filter, FindBlock(blockNumber))) { yield return(filterLog); } } } }
private IEnumerable <FilterLog> FindLogsInBlock(LogFilter filter, Block block) { if (block != null) { var receipts = _receiptStorage.FindForBlock(block, _receiptsRecovery); long logIndexInBlock = 0; foreach (var receipt in receipts) { if (receipt == null) { continue; } if (filter.Matches(receipt.Bloom)) { for (var index = 0; index < receipt.Logs.Length; index++) { var log = receipt.Logs[index]; if (filter.Accepts(log)) { yield return(new FilterLog(logIndexInBlock, index, receipt, log)); } logIndexInBlock++; } } else { logIndexInBlock += receipt.Logs.Length; } } } }
public FilterLog[] FindLogs(LogFilter filter) { var toBlock = _blockFinder.GetBlock(filter.ToBlock); if (toBlock is null) { return(Array.Empty <FilterLog>()); } var fromBlock = _blockFinder.GetBlock(filter.FromBlock); List <FilterLog> results = new List <FilterLog>(); while (toBlock.Number >= (fromBlock?.Number ?? long.MaxValue)) { if (filter.Matches(toBlock.Bloom)) { FindLogsInBlock(filter, toBlock, results); } if (!TryGetParentBlock(toBlock, out toBlock)) { break; } } return(results.ToArray()); }
private IEnumerable <FilterLog> FilterLogsInBlockHighMemoryAllocation(LogFilter filter, Keccak blockHash, long blockNumber) { TxReceipt[] GetReceipts(Keccak hash, long number) { var canUseHash = _receiptFinder.CanGetReceiptsByHash(number); if (canUseHash) { return(_receiptFinder.Get(hash)); } else { var block = _blockFinder.FindBlock(blockHash, BlockTreeLookupOptions.TotalDifficultyNotNeeded); return(block == null ? null : _receiptFinder.Get(block)); } } void RecoverReceiptsData(Keccak hash, TxReceipt[] receipts) { if (_receiptsRecovery.NeedRecover(receipts)) { var block = _blockFinder.FindBlock(hash, BlockTreeLookupOptions.TotalDifficultyNotNeeded); if (block != null) { _receiptsRecovery.TryRecover(block, receipts); } } } var receipts = GetReceipts(blockHash, blockNumber); long logIndexInBlock = 0; if (receipts != null) { for (var i = 0; i < receipts.Length; i++) { var receipt = receipts[i]; if (filter.Matches(receipt.Bloom)) { for (var j = 0; j < receipt.Logs.Length; j++) { var log = receipt.Logs[j]; if (filter.Accepts(log)) { RecoverReceiptsData(blockHash, receipts); yield return(new FilterLog(logIndexInBlock, j, receipt, log)); } logIndexInBlock++; } } else { logIndexInBlock += receipt.Logs.Length; } } } }
private static IEnumerable <FilterLog> FilterLogsInBlockLowMemoryAllocation(LogFilter filter, ref ReceiptsIterator iterator, CancellationToken cancellationToken) { List <FilterLog> logList = null; using (iterator) { long logIndexInBlock = 0; while (iterator.TryGetNext(out var receipt)) { cancellationToken.ThrowIfCancellationRequested(); LogEntriesIterator logsIterator = receipt.Logs == null ? new LogEntriesIterator(receipt.LogsRlp) : new LogEntriesIterator(receipt.Logs); if (filter.Matches(ref receipt.Bloom)) { while (logsIterator.TryGetNext(out var log)) { cancellationToken.ThrowIfCancellationRequested(); if (filter.Accepts(ref log)) { logList ??= new List <FilterLog>(); Keccak[] topics = log.Topics; if (topics == null) { var topicsValueDecoderContext = new Rlp.ValueDecoderContext(log.TopicsRlp); topics = KeccakDecoder.Instance.DecodeArray(ref topicsValueDecoderContext); } logList.Add(new FilterLog( logIndexInBlock, logsIterator.Index, receipt.BlockNumber, receipt.BlockHash.ToKeccak(), receipt.Index, receipt.TxHash.ToKeccak(), log.LoggersAddress.ToAddress(), log.Data.ToArray(), topics)); } logIndexInBlock++; } } else { while (logsIterator.TrySkipNext()) { logIndexInBlock++; } } } } return(logList ?? (IEnumerable <FilterLog>)Array.Empty <FilterLog>()); }
private IEnumerable <FilterLog> FilterLogsWithBloomsIndex(LogFilter filter, BlockHeader fromBlock, BlockHeader toBlock) { var enumeration = _bloomStorage.GetBlooms(fromBlock.Number, toBlock.Number); foreach (var bloom in enumeration) { if (filter.Matches(bloom) && enumeration.TryGetBlockNumber(out var blockNumber)) { foreach (var filterLog in FindLogsInBlock(filter, _blockFinder.FindBlock(blockNumber))) { yield return(filterLog); } } } }
private static IEnumerable <FilterLog> FilterLogsInBlockLowMemoryAllocation(LogFilter filter, ref ReceiptsIterator iterator) { List <FilterLog> logList = null; using (iterator) { long logIndexInBlock = 0; while (iterator.TryGetNext(out var receipt)) { LogEntriesIterator logsIterator = new LogEntriesIterator(receipt.Logs); if (filter.Matches(ref receipt.BloomStruct)) { while (logsIterator.TryGetNext(out var log)) { if (filter.Accepts(ref log)) { logList ??= new List <FilterLog>(); var topicsValueDecoderContext = new Rlp.ValueDecoderContext(log.Topics); logList.Add(new FilterLog( logIndexInBlock, logsIterator.Index, receipt.BlockNumber, receipt.BlockHash.ToKeccak(), receipt.Index, receipt.TxHash.ToKeccak(), log.LoggersAddress.ToAddress(), log.Data.ToArray(), KeccakDecoder.Instance.DecodeArray(ref topicsValueDecoderContext))); } logIndexInBlock++; } } else { while (logsIterator.TrySkipNext()) { logIndexInBlock++; } } } } return(logList ?? (IEnumerable <FilterLog>)Array.Empty <FilterLog>()); }
public FilterLog[] FindLogs(LogFilter filter) { var toBlock = _blockFinder.GetBlock(filter.ToBlock); var fromBlock = _blockFinder.GetBlock(filter.FromBlock); List <FilterLog> results = new List <FilterLog>(); while (toBlock.Number >= fromBlock.Number) { if (filter.Matches(toBlock.Bloom)) { FindLogsInBlock(filter, toBlock, results); } if (!TryGetParentBlock(toBlock, out toBlock)) { break; } } return(results.ToArray()); }
private IEnumerable <FilterLog> FindLogsInBlock(LogFilter filter, BlockHeader block) => filter.Matches(block.Bloom) ? FindLogsInBlock(filter, block.Hash, block.Number) : Enumerable.Empty <FilterLog>();
private IEnumerable <FilterLog> FindLogsInBlock(LogFilter filter, BlockHeader block, CancellationToken cancellationToken) => filter.Matches(block.Bloom) ? FindLogsInBlock(filter, block.Hash, block.Number, cancellationToken) : Enumerable.Empty <FilterLog>();