Search in sources :

Example 16 with Block

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);
}
Also used : RLPInput(org.hyperledger.besu.ethereum.rlp.RLPInput) SignedData(org.hyperledger.besu.consensus.common.bft.payload.SignedData) RoundChangePayload(org.hyperledger.besu.consensus.qbft.payload.RoundChangePayload) Block(org.hyperledger.besu.ethereum.core.Block)

Example 17 with Block

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());
    }
}
Also used : RoundChange(org.hyperledger.besu.consensus.qbft.messagewrappers.RoundChange) Block(org.hyperledger.besu.ethereum.core.Block)

Example 18 with Block

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);
    }
}
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 19 with Block

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);
    }
}
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 20 with Block

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

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