Search in sources :

Example 1 with BlockDataGenerator

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

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

the class JsonRpcHttpServiceTest method ethGetStorageAtInvalidParameterStorageIndex.

@Test
public void ethGetStorageAtInvalidParameterStorageIndex() throws Exception {
    // Setup mocks to return a block
    final BlockDataGenerator gen = new BlockDataGenerator();
    final Address address = gen.address();
    final String id = "88";
    final RequestBody body = RequestBody.create(JSON, "{\"jsonrpc\":\"2.0\",\"id\":" + Json.encode(id) + ",\"method\":\"eth_getStorageAt\", \"params\": [\"" + address + "\",\"" + "blah" + "\",\"latest\"]}");
    try (final Response resp = client.newCall(buildPostRequest(body)).execute()) {
        assertThat(resp.code()).isEqualTo(400);
        // Check general format of result
        final JsonObject json = new JsonObject(resp.body().string());
        final JsonRpcError expectedError = JsonRpcError.INVALID_PARAMS;
        testHelper.assertValidJsonRpcError(json, id, expectedError.getCode(), expectedError.getMessage());
    }
}
Also used : Response(okhttp3.Response) Address(org.hyperledger.besu.datatypes.Address) InetSocketAddress(java.net.InetSocketAddress) JsonObject(io.vertx.core.json.JsonObject) JsonRpcError(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError) BlockDataGenerator(org.hyperledger.besu.ethereum.core.BlockDataGenerator) RequestBody(okhttp3.RequestBody) Test(org.junit.Test)

Example 3 with BlockDataGenerator

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

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

use of org.hyperledger.besu.ethereum.core.BlockDataGenerator 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)

Aggregations

BlockDataGenerator (org.hyperledger.besu.ethereum.core.BlockDataGenerator)106 Test (org.junit.Test)86 Block (org.hyperledger.besu.ethereum.core.Block)71 KeyValueStorage (org.hyperledger.besu.plugin.services.storage.KeyValueStorage)22 InMemoryKeyValueStorage (org.hyperledger.besu.services.kvstore.InMemoryKeyValueStorage)22 Hash (org.hyperledger.besu.datatypes.Hash)20 ArrayList (java.util.ArrayList)18 List (java.util.List)18 JsonObject (io.vertx.core.json.JsonObject)17 RequestBody (okhttp3.RequestBody)17 Response (okhttp3.Response)17 Transaction (org.hyperledger.besu.ethereum.core.Transaction)15 BlockOptions (org.hyperledger.besu.ethereum.core.BlockDataGenerator.BlockOptions)13 TransactionWithMetadata (org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata)11 TransactionReceipt (org.hyperledger.besu.ethereum.core.TransactionReceipt)11 Before (org.junit.Before)11 RespondingEthPeer (org.hyperledger.besu.ethereum.eth.manager.RespondingEthPeer)10 InetSocketAddress (java.net.InetSocketAddress)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 Address (org.hyperledger.besu.datatypes.Address)9