use of org.hyperledger.besu.ethereum.core.Block in project besu by hyperledger.
the class RoundChange method decode.
public static RoundChange decode(final Bytes data, final BftExtraDataCodec bftExtraDataCodec) {
final RLPInput rlpIn = RLP.input(data);
rlpIn.enterList();
final SignedData<RoundChangePayload> payload = readPayload(rlpIn, RoundChangePayload::readFrom);
final Optional<Block> block;
if (rlpIn.nextIsList() && rlpIn.nextSize() == 0) {
rlpIn.skipNext();
block = Optional.empty();
} else {
block = Optional.of(Block.readFrom(rlpIn, BftBlockHeaderFunctions.forCommittedSeal(bftExtraDataCodec)));
}
final List<SignedData<PreparePayload>> prepares = rlpIn.readList(r -> readPayload(r, PreparePayload::readFrom));
rlpIn.leaveList();
return new RoundChange(payload, block, prepares);
}
use of org.hyperledger.besu.ethereum.core.Block in project besu by hyperledger.
the class MessageFactory method createRoundChange.
public RoundChange createRoundChange(final ConsensusRoundIdentifier roundIdentifier, final Optional<PreparedCertificate> preparedRoundData) {
final RoundChangePayload payload;
if (preparedRoundData.isPresent()) {
final Block preparedBlock = preparedRoundData.get().getBlock();
payload = new RoundChangePayload(roundIdentifier, Optional.of(new PreparedRoundMetadata(preparedBlock.getHash(), preparedRoundData.get().getRound())));
return new RoundChange(createSignedMessage(payload), Optional.of(preparedBlock), preparedRoundData.get().getPrepares());
} else {
payload = new RoundChangePayload(roundIdentifier, Optional.empty());
return new RoundChange(createSignedMessage(payload), Optional.empty(), Collections.emptyList());
}
}
use of org.hyperledger.besu.ethereum.core.Block in project besu by hyperledger.
the class JsonRpcHttpServiceTest method getBlockByNumberForPending.
@Test
public void getBlockByNumberForPending() throws Exception {
final String id = "123";
final RequestBody body = RequestBody.create(JSON, "{\"jsonrpc\":\"2.0\",\"id\":" + Json.encode(id) + ",\"method\":\"eth_getBlockByNumber\", \"params\": [\"pending\",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 getBlockByNumberWithTransactions.
@Test
public void getBlockByNumberWithTransactions() throws Exception {
// Setup mocks to return a block
final BlockDataGenerator gen = new BlockDataGenerator();
final Block block = gen.block();
final BlockWithMetadata<TransactionWithMetadata, Hash> blockWithMetadata = blockWithMetadata(block);
final long number = block.getHeader().getNumber();
when(blockchainQueries.blockByNumber(eq(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\": [\"0x" + Long.toString(number, 16) + "\",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 JsonRpcHttpServiceTest method getBlockByNumberForBlockNumberZero.
@Test
public void getBlockByNumberForBlockNumberZero() throws Exception {
final String id = "123";
final RequestBody body = RequestBody.create(JSON, "{\"jsonrpc\":\"2.0\",\"id\":" + Json.encode(id) + ",\"method\":\"eth_getBlockByNumber\", \"params\": [\"0x0\",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.blockByNumber(eq(0L))).thenReturn(Optional.of(blockWithMetadata));
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);
}
}
Aggregations