コード例 #1
0
 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);
                     }
                 }
             }
         }
     }
 }
コード例 #2
0
        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);
                            }
                        }
                    }
                }
            }
        }
コード例 #3
0
        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);
        }
コード例 #4
0
ファイル: LogFinder.cs プロジェクト: phahulin/nethermind
        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;
                }
            }
        }
コード例 #5
0
ファイル: LogFinder.cs プロジェクト: iCodeSometime/nethermind
        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);
                    }
                }
            }
        }
コード例 #6
0
ファイル: LogFinder.cs プロジェクト: iCodeSometime/nethermind
        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;
                    }
                }
            }
        }
コード例 #7
0
        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());
        }
コード例 #8
0
ファイル: LogFinder.cs プロジェクト: RohitGupta027/nethermind
        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;
                    }
                }
            }
        }
コード例 #9
0
        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>());
        }
コード例 #10
0
        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);
                    }
                }
            }
        }
コード例 #11
0
        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>());
        }
コード例 #12
0
ファイル: LogFinder.cs プロジェクト: vicnaum/nethermind
        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());
        }
コード例 #13
0
ファイル: LogFinder.cs プロジェクト: RohitGupta027/nethermind
 private IEnumerable <FilterLog> FindLogsInBlock(LogFilter filter, BlockHeader block) =>
 filter.Matches(block.Bloom)
         ? FindLogsInBlock(filter, block.Hash, block.Number)
         : Enumerable.Empty <FilterLog>();
コード例 #14
0
 private IEnumerable <FilterLog> FindLogsInBlock(LogFilter filter, BlockHeader block, CancellationToken cancellationToken) =>
 filter.Matches(block.Bloom)
         ? FindLogsInBlock(filter, block.Hash, block.Number, cancellationToken)
         : Enumerable.Empty <FilterLog>();