Search in sources :

Example 1 with TransactionWithMetadata

use of org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata 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 2 with TransactionWithMetadata

use of org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata 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 3 with TransactionWithMetadata

use of org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata 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)

Example 4 with TransactionWithMetadata

use of org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata 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 5 with TransactionWithMetadata

use of org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata 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)

Aggregations

TransactionWithMetadata (org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata)33 Hash (org.hyperledger.besu.datatypes.Hash)24 Test (org.junit.Test)19 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)10 BlockDataGenerator (org.hyperledger.besu.ethereum.core.BlockDataGenerator)10 JsonObject (io.vertx.core.json.JsonObject)9 RequestBody (okhttp3.RequestBody)9 Response (okhttp3.Response)9 BlockWithMetadata (org.hyperledger.besu.ethereum.api.query.BlockWithMetadata)9 JsonRpcRequestContext (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext)8 Transaction (org.hyperledger.besu.ethereum.core.Transaction)8 ArrayList (java.util.ArrayList)7 Block (org.hyperledger.besu.ethereum.core.Block)6 JsonRpcRequest (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest)5 JsonRpcResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse)5 BlockchainQueries (org.hyperledger.besu.ethereum.api.query.BlockchainQueries)4 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)4 Supplier (java.util.function.Supplier)3 Bytes (org.apache.tuweni.bytes.Bytes)3 Bytes32 (org.apache.tuweni.bytes.Bytes32)3