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);
}
}
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);
}
}
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);
}
}
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()));
}
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;
}
}
}
Aggregations