use of org.hyperledger.besu.ethereum.chain.TransactionLocation in project besu by hyperledger.
the class BlockchainQueries method transactionByHash.
/**
* Given a transaction hash, returns the associated transaction.
*
* @param transactionHash The hash of the target transaction.
* @return The transaction associated with the given hash.
*/
public Optional<TransactionWithMetadata> transactionByHash(final Hash transactionHash) {
final Optional<TransactionLocation> maybeLocation = blockchain.getTransactionLocation(transactionHash);
if (maybeLocation.isEmpty()) {
return Optional.empty();
}
final TransactionLocation loc = maybeLocation.get();
final Hash blockHash = loc.getBlockHash();
// getTransactionLocation should not return if the TX or block doesn't exist, so throwing
// on a missing optional is appropriate.
final BlockHeader header = blockchain.getBlockHeader(blockHash).orElseThrow();
final Transaction transaction = blockchain.getTransactionByHash(transactionHash).orElseThrow();
return Optional.of(new TransactionWithMetadata(transaction, header.getNumber(), header.getBaseFee(), blockHash, loc.getTransactionIndex()));
}
use of org.hyperledger.besu.ethereum.chain.TransactionLocation in project besu by hyperledger.
the class BlockchainQueries method transactionReceiptByTransactionHash.
/**
* Returns the transaction receipt associated with the given transaction hash.
*
* @param transactionHash The hash of the transaction that corresponds to the receipt to retrieve.
* @return The transaction receipt associated with the referenced transaction.
*/
public Optional<TransactionReceiptWithMetadata> transactionReceiptByTransactionHash(final Hash transactionHash) {
final Optional<TransactionLocation> maybeLocation = blockchain.getTransactionLocation(transactionHash);
if (maybeLocation.isEmpty()) {
return Optional.empty();
}
// getTransactionLocation should not return if the TX or block doesn't exist, so throwing
// on a missing optional is appropriate.
final TransactionLocation location = maybeLocation.get();
final Block block = blockchain.getBlockByHash(location.getBlockHash()).orElseThrow();
final Transaction transaction = block.getBody().getTransactions().get(location.getTransactionIndex());
final Hash blockhash = location.getBlockHash();
final BlockHeader header = block.getHeader();
final List<TransactionReceipt> transactionReceipts = blockchain.getTxReceipts(blockhash).orElseThrow();
final TransactionReceipt transactionReceipt = transactionReceipts.get(location.getTransactionIndex());
long gasUsed = transactionReceipt.getCumulativeGasUsed();
if (location.getTransactionIndex() > 0) {
gasUsed = gasUsed - transactionReceipts.get(location.getTransactionIndex() - 1).getCumulativeGasUsed();
}
return Optional.of(TransactionReceiptWithMetadata.create(transactionReceipt, transaction, transactionHash, location.getTransactionIndex(), gasUsed, header.getBaseFee(), blockhash, header.getNumber()));
}
use of org.hyperledger.besu.ethereum.chain.TransactionLocation in project besu by hyperledger.
the class PrivateTransactionLocatorTest method createExecutedPrivateTransactionWithVersion.
private ExecutedPrivateTransaction createExecutedPrivateTransactionWithVersion(final Transaction pmt, final PrivateTransaction privateTransaction) {
final BlockHeader blockHeader = createBlockHeader();
final TransactionLocation pmtLocation = createPmtLocation(pmt, blockHeader);
final String privacyGroupId = privateTransaction.getPrivacyGroupId().orElseThrow().toBase64String();
final Bytes32 txVersion = Bytes32.wrap(Bytes.random(32));
mockEnclaveForExistingPayload(privacyGroupId, pmt, privateTransaction, Optional.of(txVersion));
return new ExecutedPrivateTransaction(blockHeader.getHash(), blockHeader.getNumber(), pmt.getHash(), pmtLocation.getTransactionIndex(), privacyGroupId, privateTransaction);
}
use of org.hyperledger.besu.ethereum.chain.TransactionLocation in project besu by hyperledger.
the class PrivateTransactionLocatorTest method createExecutedPrivateTransactionFromAddBlob.
private ExecutedPrivateTransaction createExecutedPrivateTransactionFromAddBlob(final Transaction pmt, final PrivateTransaction privateTransaction) {
final BlockHeader blockHeader = createBlockHeader();
final TransactionLocation pmtLocation = createPmtLocation(pmt, blockHeader);
final String privacyGroupId = privateTransaction.getPrivacyGroupId().orElseThrow().toBase64String();
mockStorageWithPrivacyGroupBlockHeaderMap(privacyGroupId, blockHeader);
final Bytes32 addDataKey = Bytes32.random();
when(privateStateStorage.getAddDataKey(any(Bytes32.class))).thenReturn(Optional.of(addDataKey));
mockEnclaveForNonExistingPayload(pmt);
mockEnclaveForAddBlob(pmt, privateTransaction, addDataKey);
return new ExecutedPrivateTransaction(blockHeader.getHash(), blockHeader.getNumber(), pmt.getHash(), pmtLocation.getTransactionIndex(), privacyGroupId, privateTransaction);
}
use of org.hyperledger.besu.ethereum.chain.TransactionLocation in project besu by hyperledger.
the class PrivacyBlockProcessorTest method mustPerformRehydration.
@Test
public void mustPerformRehydration() {
final BlockDataGenerator blockDataGenerator = new BlockDataGenerator();
final Blockchain blockchain = mock(Blockchain.class);
final MutableWorldState mutableWorldState = mock(MutableWorldState.class);
when(mutableWorldState.updater()).thenReturn(mock(WorldUpdater.class));
final Block firstBlock = blockDataGenerator.block(BlockDataGenerator.BlockOptions.create().addTransaction(PrivateTransactionDataFixture.privateMarkerTransactionOnchain()));
final Block secondBlock = blockDataGenerator.block(BlockDataGenerator.BlockOptions.create().addTransaction(PrivateTransactionDataFixture.privateMarkerTransactionOnchainAdd()));
when(enclave.receive(any())).thenReturn(PrivateTransactionDataFixture.generateAddToGroupReceiveResponse(PrivateTransactionDataFixture.privateTransactionBesu(), PrivateTransactionDataFixture.privateMarkerTransactionOnchain()));
when(blockchain.getTransactionLocation(any())).thenReturn(Optional.of(new TransactionLocation(firstBlock.getHash(), 0)));
when(blockchain.getBlockByHash(any())).thenReturn(Optional.of(firstBlock));
when(blockchain.getBlockHeader(any())).thenReturn(Optional.of(firstBlock.getHeader()));
final ProtocolSpec protocolSpec = mockProtocolSpec();
when(protocolSchedule.getByBlockNumber(anyLong())).thenReturn(protocolSpec);
when(publicWorldStateArchive.getMutable(any(), any())).thenReturn(Optional.of(mutableWorldState));
final MutableWorldState mockPrivateStateArchive = mockPrivateStateArchive();
when(privateWorldStateArchive.getMutable(any(), any())).thenReturn(Optional.of(mockPrivateStateArchive));
final PrivacyGroupHeadBlockMap expected = new PrivacyGroupHeadBlockMap(Collections.singletonMap(VALID_BASE64_ENCLAVE_KEY, firstBlock.getHash()));
privateStateStorage.updater().putPrivacyGroupHeadBlockMap(firstBlock.getHash(), expected).putPrivateBlockMetadata(firstBlock.getHash(), VALID_BASE64_ENCLAVE_KEY, PrivateBlockMetadata.empty()).commit();
privacyBlockProcessor.processBlock(blockchain, mutableWorldState, secondBlock);
verify(blockProcessor).processBlock(eq(blockchain), eq(mutableWorldState), eq(secondBlock.getHeader()), eq(secondBlock.getBody().getTransactions()), eq(secondBlock.getBody().getOmmers()), any());
}
Aggregations