Search in sources :

Example 1 with TransactionLocation

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()));
}
Also used : Transaction(org.hyperledger.besu.ethereum.core.Transaction) TransactionLocation(org.hyperledger.besu.ethereum.chain.TransactionLocation) Hash(org.hyperledger.besu.datatypes.Hash) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Example 2 with TransactionLocation

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()));
}
Also used : Transaction(org.hyperledger.besu.ethereum.core.Transaction) TransactionLocation(org.hyperledger.besu.ethereum.chain.TransactionLocation) TransactionReceipt(org.hyperledger.besu.ethereum.core.TransactionReceipt) Block(org.hyperledger.besu.ethereum.core.Block) Hash(org.hyperledger.besu.datatypes.Hash) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Example 3 with TransactionLocation

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);
}
Also used : TransactionLocation(org.hyperledger.besu.ethereum.chain.TransactionLocation) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Bytes32(org.apache.tuweni.bytes.Bytes32)

Example 4 with TransactionLocation

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);
}
Also used : TransactionLocation(org.hyperledger.besu.ethereum.chain.TransactionLocation) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Bytes32(org.apache.tuweni.bytes.Bytes32)

Example 5 with TransactionLocation

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());
}
Also used : MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) Blockchain(org.hyperledger.besu.ethereum.chain.Blockchain) WorldUpdater(org.hyperledger.besu.evm.worldstate.WorldUpdater) TransactionLocation(org.hyperledger.besu.ethereum.chain.TransactionLocation) Block(org.hyperledger.besu.ethereum.core.Block) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) PrivacyGroupHeadBlockMap(org.hyperledger.besu.ethereum.privacy.storage.PrivacyGroupHeadBlockMap) Test(org.junit.Test)

Aggregations

TransactionLocation (org.hyperledger.besu.ethereum.chain.TransactionLocation)12 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)9 Transaction (org.hyperledger.besu.ethereum.core.Transaction)6 Hash (org.hyperledger.besu.datatypes.Hash)5 Bytes32 (org.apache.tuweni.bytes.Bytes32)3 Block (org.hyperledger.besu.ethereum.core.Block)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 Bytes (org.apache.tuweni.bytes.Bytes)2 Blockchain (org.hyperledger.besu.ethereum.chain.Blockchain)2 MutableWorldState (org.hyperledger.besu.ethereum.core.MutableWorldState)2 PrivacyGroupHeadBlockMap (org.hyperledger.besu.ethereum.privacy.storage.PrivacyGroupHeadBlockMap)2 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 SendResponse (org.hyperledger.besu.enclave.types.SendResponse)1 JsonRpcRequest (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest)1 JsonRpcRequestContext (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext)1 PrivGetPrivateTransaction (org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivGetPrivateTransaction)1 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)1