Search in sources :

Example 21 with Block

use of org.hyperledger.besu.ethereum.core.Block in project besu by hyperledger.

the class JsonRpcHttpServiceTest method getBlockByHashWithTransactions.

@Test
public void getBlockByHashWithTransactions() throws Exception {
    // Setup mocks to return a block
    final BlockDataGenerator gen = new BlockDataGenerator();
    final Block block = gen.block();
    final BlockWithMetadata<TransactionWithMetadata, Hash> blockWMetadata = blockWithMetadata(block);
    final Hash blockHash = block.getHeader().getHash();
    when(blockchainQueries.blockByHash(eq(blockHash))).thenReturn(Optional.of(blockWMetadata));
    final String id = "123";
    final RequestBody body = RequestBody.create(JSON, "{\"jsonrpc\":\"2.0\",\"id\":" + Json.encode(id) + ",\"method\":\"eth_getBlockByHash\", \"params\": [\"" + blockHash + "\",true]}");
    try (final Response resp = client.newCall(buildPostRequest(body)).execute()) {
        assertThat(resp.code()).isEqualTo(200);
        // Check general format of result
        final String respBody = resp.body().string();
        final JsonObject json = new JsonObject(respBody);
        testHelper.assertValidJsonRpcResult(json, id);
        // Check result
        final JsonObject result = json.getJsonObject("result");
        verifyBlockResult(block, blockWMetadata.getTotalDifficulty(), result, false);
    }
}
Also used : Response(okhttp3.Response) TransactionWithMetadata(org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata) Block(org.hyperledger.besu.ethereum.core.Block) JsonObject(io.vertx.core.json.JsonObject) Hash(org.hyperledger.besu.datatypes.Hash) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 22 with Block

use of org.hyperledger.besu.ethereum.core.Block in project besu by hyperledger.

the class JsonRpcHttpServiceTest method getBlockByNumberForLatest.

@Test
public void getBlockByNumberForLatest() throws Exception {
    final String id = "123";
    final RequestBody body = RequestBody.create(JSON, "{\"jsonrpc\":\"2.0\",\"id\":" + Json.encode(id) + ",\"method\":\"eth_getBlockByNumber\", \"params\": [\"latest\",true]}");
    // Setup mocks to return a block
    final BlockDataGenerator gen = new BlockDataGenerator();
    final Block block = gen.genesisBlock();
    final BlockWithMetadata<TransactionWithMetadata, Hash> blockWithMetadata = blockWithMetadata(block);
    when(blockchainQueries.headBlockNumber()).thenReturn(0L);
    when(blockchainQueries.blockByNumber(eq(0L))).thenReturn(Optional.of(blockWithMetadata));
    when(blockchainQueries.getBlockchain()).thenReturn(blockchain);
    when(blockchain.getBlockHeader(blockchainQueries.headBlockNumber())).thenReturn(Optional.of(block.getHeader()));
    WorldStateArchive state = mock(WorldStateArchive.class);
    when(state.isWorldStateAvailable(any(Hash.class), any(Hash.class))).thenReturn(true);
    when(blockchainQueries.getWorldStateArchive()).thenReturn(state);
    try (final Response resp = client.newCall(buildPostRequest(body)).execute()) {
        assertThat(resp.code()).isEqualTo(200);
        // Check general format of result
        final String respBody = resp.body().string();
        final JsonObject json = new JsonObject(respBody);
        testHelper.assertValidJsonRpcResult(json, id);
        // Check result
        final JsonObject result = json.getJsonObject("result");
        verifyBlockResult(block, blockWithMetadata.getTotalDifficulty(), result, false);
    }
}
Also used : Response(okhttp3.Response) WorldStateArchive(org.hyperledger.besu.ethereum.worldstate.WorldStateArchive) TransactionWithMetadata(org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata) Block(org.hyperledger.besu.ethereum.core.Block) JsonObject(io.vertx.core.json.JsonObject) Hash(org.hyperledger.besu.datatypes.Hash) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 23 with Block

use of org.hyperledger.besu.ethereum.core.Block in project besu by hyperledger.

the class JsonRpcHttpServiceTest method getBlockByNumberForEarliest.

@Test
public void getBlockByNumberForEarliest() throws Exception {
    // Setup mocks to return a block
    final BlockDataGenerator gen = new BlockDataGenerator();
    final Block block = gen.genesisBlock();
    final BlockWithMetadata<TransactionWithMetadata, Hash> blockWithMetadata = blockWithMetadata(block);
    when(blockchainQueries.blockByNumber(eq(BlockHeader.GENESIS_BLOCK_NUMBER))).thenReturn(Optional.of(blockWithMetadata));
    final String id = "123";
    final RequestBody body = RequestBody.create(JSON, "{\"jsonrpc\":\"2.0\",\"id\":" + Json.encode(id) + ",\"method\":\"eth_getBlockByNumber\", \"params\": [\"earliest\",true]}");
    try (final Response resp = client.newCall(buildPostRequest(body)).execute()) {
        assertThat(resp.code()).isEqualTo(200);
        // Check general format of result
        final String respBody = resp.body().string();
        final JsonObject json = new JsonObject(respBody);
        testHelper.assertValidJsonRpcResult(json, id);
        // Check result
        final JsonObject result = json.getJsonObject("result");
        verifyBlockResult(block, blockWithMetadata.getTotalDifficulty(), result, false);
    }
}
Also used : Response(okhttp3.Response) TransactionWithMetadata(org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata) Block(org.hyperledger.besu.ethereum.core.Block) JsonObject(io.vertx.core.json.JsonObject) Hash(org.hyperledger.besu.datatypes.Hash) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 24 with Block

use of org.hyperledger.besu.ethereum.core.Block 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 25 with Block

use of org.hyperledger.besu.ethereum.core.Block in project besu by hyperledger.

the class StateBackupService method backupChainData.

private void backupChainData() throws IOException {
    try (final RollingFileWriter headerWriter = new RollingFileWriter(this::headerFileName, backupStatus.compressed);
        final RollingFileWriter bodyWriter = new RollingFileWriter(this::bodyFileName, backupStatus.compressed);
        final RollingFileWriter receiptsWriter = new RollingFileWriter(this::receiptFileName, backupStatus.compressed)) {
        for (long blockNumber = 0; blockNumber <= backupStatus.targetBlock; blockNumber++) {
            final Optional<Block> block = blockchain.getBlockByNumber(blockNumber);
            checkState(block.isPresent(), "Block data for %s was not found in the archive", blockNumber);
            final Optional<List<TransactionReceipt>> receipts = blockchain.getTxReceipts(block.get().getHash());
            checkState(receipts.isPresent(), "Receipts for %s was not found in the archive", blockNumber);
            final BytesValueRLPOutput headerOutput = new BytesValueRLPOutput();
            block.get().getHeader().writeTo(headerOutput);
            headerWriter.writeBytes(headerOutput.encoded().toArrayUnsafe());
            final BytesValueRLPOutput bodyOutput = new BytesValueRLPOutput();
            block.get().getBody().writeTo(bodyOutput);
            bodyWriter.writeBytes(bodyOutput.encoded().toArrayUnsafe());
            final BytesValueRLPOutput receiptsOutput = new BytesValueRLPOutput();
            receiptsOutput.writeList(receipts.get(), TransactionReceipt::writeToWithRevertReason);
            receiptsWriter.writeBytes(receiptsOutput.encoded().toArrayUnsafe());
            backupStatus.storedBlock = blockNumber;
        }
    }
}
Also used : TransactionReceipt(org.hyperledger.besu.ethereum.core.TransactionReceipt) Block(org.hyperledger.besu.ethereum.core.Block) List(java.util.List) RollingFileWriter(org.hyperledger.besu.util.io.RollingFileWriter) BytesValueRLPOutput(org.hyperledger.besu.ethereum.rlp.BytesValueRLPOutput)

Aggregations

Block (org.hyperledger.besu.ethereum.core.Block)425 Test (org.junit.Test)236 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)118 BlockDataGenerator (org.hyperledger.besu.ethereum.core.BlockDataGenerator)91 List (java.util.List)57 Hash (org.hyperledger.besu.datatypes.Hash)54 TransactionReceipt (org.hyperledger.besu.ethereum.core.TransactionReceipt)52 BlockBody (org.hyperledger.besu.ethereum.core.BlockBody)47 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)40 ArrayList (java.util.ArrayList)37 Test (org.junit.jupiter.api.Test)37 ConsensusRoundIdentifier (org.hyperledger.besu.consensus.common.bft.ConsensusRoundIdentifier)36 Transaction (org.hyperledger.besu.ethereum.core.Transaction)34 RespondingEthPeer (org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer)34 Optional (java.util.Optional)33 Bytes (org.apache.tuweni.bytes.Bytes)33 MutableBlockchain (org.hyperledger.besu.ethereum.chain.MutableBlockchain)31 ProtocolSchedule (org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule)31 Address (org.hyperledger.besu.datatypes.Address)28 Difficulty (org.hyperledger.besu.ethereum.core.Difficulty)28