public ResultWrapper <TransactionWithProof> proof_getTransactionByHash(Keccak txHash, bool includeHeader) { Keccak blockHash = _receiptFinder.FindBlockHash(txHash); if (blockHash == null) { return(ResultWrapper <TransactionWithProof> .Fail($"{txHash} receipt (transaction) could not be found", ErrorCodes.ResourceNotFound)); } SearchResult <Block> searchResult = _blockFinder.SearchForBlock(new BlockParameter(blockHash)); if (searchResult.IsError) { return(ResultWrapper <TransactionWithProof> .Fail(searchResult)); } Block block = searchResult.Object; TxReceipt receipt = _receiptFinder.Get(block).ForTransaction(txHash); Transaction[] txs = block.Transactions; Transaction transaction = txs[receipt.Index]; TransactionWithProof txWithProof = new TransactionWithProof(); txWithProof.Transaction = new TransactionForRpc(block.Hash, block.Number, receipt.Index, transaction); txWithProof.TxProof = BuildTxProofs(txs, _specProvider.GetSpec(block.Number), receipt.Index); if (includeHeader) { txWithProof.BlockHeader = _headerDecoder.Encode(block.Header).Bytes; } return(ResultWrapper <TransactionWithProof> .Success(txWithProof)); }
public TxReceipt[] Get(Block block) { var receipts = _innerFinder.Get(block); _receiptsRecovery.TryRecover(block, receipts); return(receipts); }
public void should_not_throw_if_receiptFinder_asked_for_not_existing_receipts_by_block() { Block block = Build.A.Block.WithNumber(0).WithTransactions(5, _specProvider).TestObject; TxReceipt[] receipts = _receiptFinder.Get(block); receipts.Should().BeEmpty(); }
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 void TestAddAndGetReceipt(IReceiptStorage storage, IReceiptFinder receiptFinder = null) { bool recoverSender = receiptFinder is not null; receiptFinder ??= storage; var transaction = GetSignedTransaction(); transaction.SenderAddress = null; var block = GetBlock(transaction); var receipt = GetReceipt(transaction, block); storage.Insert(block, receipt); var blockHash = storage.FindBlockHash(transaction.Hash); blockHash.Should().Be(block.Hash); var fetchedReceipt = receiptFinder.Get(block).ForTransaction(transaction.Hash); receipt.StatusCode.Should().Be(fetchedReceipt.StatusCode); receipt.PostTransactionState.Should().Be(fetchedReceipt.PostTransactionState); receipt.TxHash.Should().Be(transaction.Hash); if (recoverSender) { receipt.Sender.Should().BeEquivalentTo(TestItem.AddressA); } }
private TestCase <T> BuildTestCase <T>(ILocalDataSource <IEnumerable <T> > localDataSource, bool withContractSource = true, IComparer <T> keyComparer = null, IComparer <T> valueComparer = null) { IDataContract <T> dataContract = null; if (withContractSource) { dataContract = Substitute.For <IDataContract <T> >(); dataContract.IncrementalChanges.Returns(true); } IBlockTree blockTree = Substitute.For <IBlockTree>(); IReceiptFinder receiptsFinder = Substitute.For <IReceiptFinder>(); receiptsFinder.Get(Arg.Any <Block>()).Returns(Array.Empty <TxReceipt>()); return(new TestCase <T>() { DataContract = dataContract, BlockTree = blockTree, ReceiptFinder = receiptsFinder, ContractDataStore = keyComparer == null ? (IContractDataStore <T>) new ContractDataStoreWithLocalData <T>(new HashSetContractDataStoreCollection <T>(), dataContract, blockTree, receiptsFinder, LimboLogs.Instance, localDataSource) : new DictionaryContractDataStore <T>(new SortedListContractDataStoreCollection <T>(keyComparer, valueComparer), dataContract, blockTree, receiptsFinder, LimboLogs.Instance, localDataSource) }); }
public (TxReceipt Receipt, UInt256?EffectiveGasPrice) GetReceiptAndEffectiveGasPrice(Keccak txHash) { Keccak blockHash = _receiptFinder.FindBlockHash(txHash); if (blockHash != null) { Block block = _blockTree.FindBlock(blockHash, BlockTreeLookupOptions.TotalDifficultyNotNeeded); TxReceipt txReceipt = _receiptFinder.Get(block).ForTransaction(txHash); Transaction tx = block?.Transactions[txReceipt.Index]; bool is1559Enabled = _specProvider.GetSpec(block.Number).IsEip1559Enabled; UInt256 effectiveGasPrice = tx.CalculateEffectiveGasPrice(is1559Enabled, block.Header.BaseFeePerGas); return(txReceipt, effectiveGasPrice); } return(null, null); }
public (TxReceipt Receipt, Transaction Transaction) GetTransaction(Keccak txHash) { Keccak blockHash = _receiptFinder.FindBlockHash(txHash); if (blockHash != null) { Block block = _blockTree.FindBlock(blockHash, BlockTreeLookupOptions.TotalDifficultyNotNeeded); TxReceipt txReceipt = _receiptFinder.Get(block).ForTransaction(txHash); return(txReceipt, block?.Transactions[txReceipt.Index]); } if (_txPool.TryGetPendingTransaction(txHash, out var transaction)) { return(null, transaction); } return(null, null); }
public TxReceipt[][] GetReceipts(IList <Keccak> blockHashes) { var receipts = new TxReceipt[blockHashes.Count][]; for (int blockIndex = 0; blockIndex < blockHashes.Count; blockIndex++) { Keccak blockHash = blockHashes[blockIndex]; receipts[blockIndex] = blockHash != null?_receiptFinder.Get(blockHash) : Array.Empty <TxReceipt>(); } return(receipts); }
public (TxReceipt Receipt, UInt256?EffectiveGasPrice, int LogIndexStart) GetReceiptAndEffectiveGasPrice(Keccak txHash) { Keccak blockHash = _receiptFinder.FindBlockHash(txHash); if (blockHash != null) { Block?block = _processingEnv.BlockTree.FindBlock(blockHash, BlockTreeLookupOptions.RequireCanonical); if (block is not null) { TxReceipt[] txReceipts = _receiptFinder.Get(block); TxReceipt txReceipt = txReceipts.ForTransaction(txHash); int logIndexStart = txReceipts.GetBlockLogFirstIndex(txReceipt.Index); Transaction tx = block.Transactions[txReceipt.Index]; bool is1559Enabled = _specProvider.GetSpec(block.Number).IsEip1559Enabled; UInt256 effectiveGasPrice = tx.CalculateEffectiveGasPrice(is1559Enabled, block.Header.BaseFeePerGas); return(txReceipt, effectiveGasPrice, logIndexStart); } } return(null, null, 0); }
public ResultWrapper <ReceiptForRpc[]> parity_getBlockReceipts(BlockParameter blockParameter) { SearchResult <Block> searchResult = _blockFinder.SearchForBlock(blockParameter); if (searchResult.IsError) { return(ResultWrapper <ReceiptForRpc[]> .Fail(searchResult)); } Block block = searchResult.Object; TxReceipt[] receipts = _receiptFinder.Get(block) ?? new TxReceipt[block.Transactions.Length]; IEnumerable <ReceiptForRpc> result = receipts.Zip(block.Transactions, (r, t) => new ReceiptForRpc(t.Hash, r)); return(ResultWrapper <ReceiptForRpc[]> .Success(result.ToArray())); }
public ResultWrapper <ReceiptForRpc[]> parity_getBlockReceipts(BlockParameter blockParameter) { SearchResult <Block> searchResult = _blockFinder.SearchForBlock(blockParameter); if (searchResult.IsError) { return(ResultWrapper <ReceiptForRpc[]> .Fail(searchResult)); } Block block = searchResult.Object; TxReceipt[] receipts = _receiptFinder.Get(block) ?? new TxReceipt[block.Transactions.Length]; bool isEip1559Enabled = _specProvider.GetSpec(block.Number).IsEip1559Enabled; IEnumerable <ReceiptForRpc> result = receipts .Zip(block.Transactions, (r, t) => new ReceiptForRpc(t.Hash, r, t.CalculateEffectiveGasPrice(isEip1559Enabled, block.BaseFeePerGas), receipts.GetBlockLogFirstIndex(r.Index))); ReceiptForRpc[] resultAsArray = result.ToArray(); return(ResultWrapper <ReceiptForRpc[]> .Success(resultAsArray)); }
protected virtual TestCase <T> BuildTestCase <T>(IComparer <T> keyComparer = null, IComparer <T> valueComparer = null) { IDataContract <T> dataContract = Substitute.For <IDataContract <T> >(); dataContract.IncrementalChanges.Returns(true); IBlockTree blockTree = Substitute.For <IBlockTree>(); IReceiptFinder receiptsFinder = Substitute.For <IReceiptFinder>(); receiptsFinder.Get(Arg.Any <Block>()).Returns(Array.Empty <TxReceipt>()); return(new TestCase <T>() { DataContract = dataContract, BlockTree = blockTree, ReceiptFinder = receiptsFinder, ContractDataStore = keyComparer == null ? (IContractDataStore <T>) new ContractDataStore <T>(new HashSetContractDataStoreCollection <T>(), dataContract, blockTree, receiptsFinder, LimboLogs.Instance) : new DictionaryContractDataStore <T>(new SortedListContractDataStoreCollection <T>(keyComparer, valueComparer), dataContract, blockTree, receiptsFinder, LimboLogs.Instance) }); }
private PendingValidators TryGetInitChangeFromPastBlocks(Keccak blockHash) { PendingValidators pendingValidators = null; var lastFinalized = _blockFinalizationManager.GetLastLevelFinalizedBy(blockHash); var toBlock = Math.Max(lastFinalized, InitBlockNumber); var block = _blockTree.FindBlock(blockHash, BlockTreeLookupOptions.None); while (block?.Number >= toBlock) { var receipts = _receiptFinder.Get(block) ?? Array.Empty <TxReceipt>(); if (ValidatorContract.CheckInitiateChangeEvent(block.Header, receipts, out var potentialValidators)) { if (Validators.SequenceEqual(potentialValidators)) { break; } pendingValidators = new PendingValidators(block.Number, block.Hash, potentialValidators); } block = _blockTree.FindBlock(block.ParentHash, BlockTreeLookupOptions.None); } return(pendingValidators); }
public TxReceipt[] GetReceipts(Keccak blockHash) { return(blockHash != null?_receiptFinder.Get(blockHash) : Array.Empty <TxReceipt>()); }
public void Get_receipt_when_block_has_few_receipts(bool withHeader, string expectedResult) { IReceiptFinder _receiptFinder = Substitute.For <IReceiptFinder>(); LogEntry[] logEntries = new[] { Build.A.LogEntry.TestObject, Build.A.LogEntry.TestObject }; TxReceipt receipt1 = new TxReceipt() { Bloom = new Bloom(logEntries), Index = 0, Recipient = TestItem.AddressA, Sender = TestItem.AddressB, BlockHash = _blockTree.FindBlock(1).Hash, BlockNumber = 1, ContractAddress = TestItem.AddressC, GasUsed = 1000, TxHash = _blockTree.FindBlock(1).Transactions[0].Hash, StatusCode = 0, GasUsedTotal = 2000, Logs = logEntries }; TxReceipt receipt2 = new TxReceipt() { Bloom = new Bloom(logEntries), Index = 1, Recipient = TestItem.AddressC, Sender = TestItem.AddressD, BlockHash = _blockTree.FindBlock(1).Hash, BlockNumber = 1, ContractAddress = TestItem.AddressC, GasUsed = 1000, TxHash = _blockTree.FindBlock(1).Transactions[1].Hash, StatusCode = 0, GasUsedTotal = 2000, Logs = logEntries }; Block block = _blockTree.FindBlock(1); Keccak txHash = _blockTree.FindBlock(1).Transactions[1].Hash; TxReceipt[] receipts = { receipt1, receipt2 }; _receiptFinder.Get(Arg.Any <Block>()).Returns(receipts); _receiptFinder.Get(Arg.Any <Keccak>()).Returns(receipts); _receiptFinder.FindBlockHash(Arg.Any <Keccak>()).Returns(_blockTree.FindBlock(1).Hash); ProofModuleFactory moduleFactory = new ProofModuleFactory( _dbProvider, _blockTree, new TrieStore(_dbProvider.StateDb, LimboLogs.Instance).AsReadOnly(), new CompositeBlockPreprocessorStep(new RecoverSignatures(new EthereumEcdsa(ChainId.Mainnet, LimboLogs.Instance), NullTxPool.Instance, _specProvider, LimboLogs.Instance)), _receiptFinder, _specProvider, LimboLogs.Instance); _proofRpcModule = moduleFactory.Create(); ReceiptWithProof receiptWithProof = _proofRpcModule.proof_getTransactionReceipt(txHash, withHeader).Data; if (withHeader) { Assert.NotNull(receiptWithProof.BlockHeader); } else { Assert.Null(receiptWithProof.BlockHeader); } string response = RpcTest.TestSerializedRequest(_proofRpcModule, "proof_getTransactionReceipt", $"{txHash}", $"{withHeader}"); Assert.AreEqual(expectedResult, response); }
private void Refresh(Block block) { GetItemsFromContractAtBlock(block.Header, block.Header.ParentHash == _lastHash, _receiptFinder.Get(block)); }