-
Notifications
You must be signed in to change notification settings - Fork 871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add eth_getBlockReceipts() JSON/RPC method #5771
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
335e2da
Initial commit with new JSON/RPC method
matthew1001 da1a323
Add unit tests
matthew1001 80c0dcb
Merge branch 'main' into get-block-receipts
matthew1001 21727e8
New copyright header
matthew1001 8e6565c
Update unit tests
matthew1001 5f6df78
Merge branch 'main' into get-block-receipts
matthew1001 6b33163
Fix unit tests
matthew1001 b7cdfa4
Update ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/j…
matthew1001 a78e7ac
Update CHANGELOG.md
matthew1001 76ba390
Merge branch 'main' into get-block-receipts
matthew1001 b5cb902
Update unit tests to check receipts against generated blockchain tran…
matthew1001 a355fcf
Add spec JSON/RPC tests
matthew1001 393e8a1
merge
macfarla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
.../java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetBlockReceipts.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright Hyperledger Besu contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods; | ||
|
||
import org.hyperledger.besu.datatypes.Hash; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.parameters.BlockParameterOrBlockHash; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockReceiptsResult; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.TransactionReceiptResult; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.TransactionReceiptRootResult; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.TransactionReceiptStatusResult; | ||
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; | ||
import org.hyperledger.besu.ethereum.api.query.TransactionReceiptWithMetadata; | ||
import org.hyperledger.besu.ethereum.api.query.TransactionWithMetadata; | ||
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; | ||
import org.hyperledger.besu.ethereum.mainnet.TransactionReceiptType; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import com.google.common.base.Suppliers; | ||
|
||
public class EthGetBlockReceipts extends AbstractBlockParameterOrBlockHashMethod { | ||
|
||
private final ProtocolSchedule protocolSchedule; | ||
|
||
public EthGetBlockReceipts( | ||
final BlockchainQueries blockchain, final ProtocolSchedule protocolSchedule) { | ||
super(Suppliers.ofInstance(blockchain)); | ||
this.protocolSchedule = protocolSchedule; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return RpcMethod.ETH_GET_BLOCK_RECEIPTS.getMethodName(); | ||
} | ||
|
||
@Override | ||
protected BlockParameterOrBlockHash blockParameterOrBlockHash( | ||
final JsonRpcRequestContext request) { | ||
return request.getRequiredParameter(0, BlockParameterOrBlockHash.class); | ||
} | ||
|
||
@Override | ||
protected Object resultByBlockHash(final JsonRpcRequestContext request, final Hash blockHash) { | ||
return getBlockReceiptsResult(blockHash); | ||
} | ||
|
||
/* | ||
* For a given transaction, get its receipt and if it exists, wrap in a transaction receipt of the correct type | ||
*/ | ||
private Optional<TransactionReceiptResult> txReceipt(final TransactionWithMetadata tx) { | ||
Optional<TransactionReceiptWithMetadata> receipt = | ||
blockchainQueries | ||
.get() | ||
.transactionReceiptByTransactionHash(tx.getTransaction().getHash(), protocolSchedule); | ||
if (receipt.isPresent()) { | ||
if (receipt.get().getReceipt().getTransactionReceiptType() == TransactionReceiptType.ROOT) { | ||
return Optional.of(new TransactionReceiptRootResult(receipt.get())); | ||
} else { | ||
return Optional.of(new TransactionReceiptStatusResult(receipt.get())); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
private BlockReceiptsResult getBlockReceiptsResult(final Hash blockHash) { | ||
final List<TransactionReceiptResult> receiptList = | ||
blockchainQueries | ||
.get() | ||
.blockByHash(blockHash) | ||
.map( | ||
block -> | ||
block.getTransactions().stream() | ||
.map(tx -> txReceipt(tx).get()) | ||
.collect(Collectors.toList())) | ||
.orElse(new ArrayList<>()); | ||
|
||
return new BlockReceiptsResult(receiptList); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
.../java/org/hyperledger/besu/ethereum/api/jsonrpc/internal/results/BlockReceiptsResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright Hyperledger Besu contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.results; | ||
|
||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.annotation.JsonValue; | ||
|
||
/** The result set from querying the receipts for a given block. */ | ||
public class BlockReceiptsResult { | ||
|
||
private final List<TransactionReceiptResult> results; | ||
|
||
public BlockReceiptsResult(final List<TransactionReceiptResult> receipts) { | ||
results = receipts; | ||
} | ||
|
||
@JsonValue | ||
public List<TransactionReceiptResult> getResults() { | ||
return results; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
195 changes: 195 additions & 0 deletions
195
...a/org/hyperledger/besu/ethereum/api/jsonrpc/internal/methods/EthGetBlockReceiptsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
/* | ||
* Copyright Hyperledger Besu contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryBlockchain; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
|
||
import org.hyperledger.besu.datatypes.Hash; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.RpcErrorType; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockReceiptsResult; | ||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.TransactionReceiptResult; | ||
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries; | ||
import org.hyperledger.besu.ethereum.chain.MutableBlockchain; | ||
import org.hyperledger.besu.ethereum.core.Block; | ||
import org.hyperledger.besu.ethereum.core.BlockDataGenerator; | ||
import org.hyperledger.besu.ethereum.core.Transaction; | ||
import org.hyperledger.besu.ethereum.core.TransactionReceipt; | ||
import org.hyperledger.besu.ethereum.mainnet.ProtocolSchedule; | ||
import org.hyperledger.besu.ethereum.worldstate.WorldStateArchive; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class EthGetBlockReceiptsTest { | ||
|
||
private static final int BLOCKCHAIN_LENGTH = 5; | ||
private static final String ZERO_HASH = String.valueOf(Hash.ZERO); | ||
private static final String HASH_63_CHARS_LONG = | ||
"0xd3d3d1340c085e1b14182e01fd0b7cc5b585dca77f809f78fcca3e1a165b189"; | ||
private static final String ETH_METHOD = "eth_getBlockReceipts"; | ||
private static final String JSON_RPC_VERSION = "2.0"; | ||
|
||
@Mock private BlockchainQueries blockchainQueries; | ||
@Mock private WorldStateArchive worldStateArchive; | ||
private MutableBlockchain blockchain; | ||
private static final BlockDataGenerator blockDataGenerator = new BlockDataGenerator(); | ||
private EthGetBlockReceipts method; | ||
private ProtocolSchedule protocolSchedule; | ||
final JsonRpcResponse blockNotFoundResponse = | ||
new JsonRpcErrorResponse(null, RpcErrorType.BLOCK_NOT_FOUND); | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
blockchain = createInMemoryBlockchain(blockDataGenerator.genesisBlock()); | ||
|
||
for (int i = 1; i < BLOCKCHAIN_LENGTH; i++) { | ||
final BlockDataGenerator.BlockOptions options = | ||
new BlockDataGenerator.BlockOptions() | ||
.setBlockNumber(i) | ||
.setParentHash(blockchain.getBlockHashByNumber(i - 1).orElseThrow()); | ||
final Block block = blockDataGenerator.block(options); | ||
final List<TransactionReceipt> receipts = blockDataGenerator.receipts(block); | ||
|
||
blockchain.appendBlock(block, receipts); | ||
} | ||
|
||
blockchainQueries = spy(new BlockchainQueries(blockchain, worldStateArchive)); | ||
protocolSchedule = mock(ProtocolSchedule.class); | ||
method = new EthGetBlockReceipts(blockchainQueries, protocolSchedule); | ||
} | ||
|
||
@Test | ||
public void returnsCorrectMethodName() { | ||
assertThat(method.getName()).isEqualTo(ETH_METHOD); | ||
} | ||
|
||
@Test | ||
public void exceptionWhenNoParamsSupplied() { | ||
assertThatThrownBy(() -> method.response(requestWithParams())) | ||
.isInstanceOf(InvalidJsonRpcParameters.class) | ||
.hasMessage("Missing required json rpc parameter at index 0"); | ||
verifyNoMoreInteractions(blockchainQueries); | ||
} | ||
|
||
@Test | ||
public void exceptionWhenBlockNumberTooLarge() { | ||
assertThatThrownBy(() -> method.response(requestWithParams("0x1212121212121212121212"))) | ||
.isInstanceOf(InvalidJsonRpcParameters.class); | ||
verifyNoMoreInteractions(blockchainQueries); | ||
} | ||
|
||
@Test | ||
public void twoReceiptsForLatestBlock() { | ||
|
||
// Read expected transactions from the generated blockchain | ||
final Transaction expectedTx1 = | ||
blockchain.getBlockByNumber(BLOCKCHAIN_LENGTH - 1).get().getBody().getTransactions().get(0); | ||
final Transaction expectedTx2 = | ||
blockchain.getBlockByNumber(BLOCKCHAIN_LENGTH - 1).get().getBody().getTransactions().get(1); | ||
|
||
/* Block generator defaults to 2 transactions per mocked block */ | ||
JsonRpcResponse actualResponse = method.response(requestWithParams("latest")); | ||
assertThat(actualResponse).isInstanceOf(JsonRpcSuccessResponse.class); | ||
final BlockReceiptsResult result = | ||
(BlockReceiptsResult) ((JsonRpcSuccessResponse) actualResponse).getResult(); | ||
|
||
assertThat(result.getResults().size()).isEqualTo(2); | ||
|
||
// Check TX1 receipt is correct | ||
TransactionReceiptResult tx1 = result.getResults().get(0); | ||
assertThat(tx1.getBlockNumber()).isEqualTo("0x" + (BLOCKCHAIN_LENGTH - 1)); | ||
assertThat(tx1.getEffectiveGasPrice()).isNotEmpty(); | ||
assertThat(tx1.getTo()).isEqualTo(expectedTx1.getTo().get().toString()); | ||
assertThat(tx1.getType()) | ||
.isEqualTo(String.format("0x%X", expectedTx1.getType().getEthSerializedType())); | ||
|
||
// Check TX2 receipt is correct | ||
TransactionReceiptResult tx2 = result.getResults().get(1); | ||
assertThat(tx2.getBlockNumber()).isEqualTo("0x" + (BLOCKCHAIN_LENGTH - 1)); | ||
assertThat(tx2.getEffectiveGasPrice()).isNotEmpty(); | ||
assertThat(tx2.getTo()).isEqualTo(expectedTx2.getTo().get().toString()); | ||
assertThat(tx2.getType()) | ||
.isEqualTo(String.format("0x%X", expectedTx2.getType().getEthSerializedType())); | ||
} | ||
|
||
@Test | ||
public void twoReceiptsForBlockOne() { | ||
|
||
// Read expected transactions from the generated blockchain | ||
final Transaction expectedTx1 = | ||
blockchain.getBlockByNumber(1).get().getBody().getTransactions().get(0); | ||
final Transaction expectedTx2 = | ||
blockchain.getBlockByNumber(1).get().getBody().getTransactions().get(1); | ||
|
||
/* Block generator defaults to 2 transactions per block */ | ||
JsonRpcResponse actualResponse = method.response(requestWithParams("0x01")); | ||
assertThat(actualResponse).isInstanceOf(JsonRpcSuccessResponse.class); | ||
final BlockReceiptsResult result = | ||
(BlockReceiptsResult) ((JsonRpcSuccessResponse) actualResponse).getResult(); | ||
|
||
assertThat(result.getResults().size()).isEqualTo(2); | ||
|
||
// Check TX1 receipt is correct | ||
TransactionReceiptResult tx1 = result.getResults().get(0); | ||
assertThat(tx1.getBlockNumber()).isEqualTo("0x1"); | ||
assertThat(tx1.getEffectiveGasPrice()).isNotEmpty(); | ||
assertThat(tx1.getTo()).isEqualTo(expectedTx1.getTo().get().toString()); | ||
assertThat(tx1.getType()) | ||
.isEqualTo(String.format("0x%X", expectedTx1.getType().getEthSerializedType())); | ||
|
||
// Check TX2 receipt is correct | ||
TransactionReceiptResult tx2 = result.getResults().get(1); | ||
assertThat(tx2.getBlockNumber()).isEqualTo("0x1"); | ||
assertThat(tx2.getEffectiveGasPrice()).isNotEmpty(); | ||
assertThat(tx2.getTo()).isEqualTo(expectedTx2.getTo().get().toString()); | ||
assertThat(tx2.getType()) | ||
.isEqualTo(String.format("0x%X", expectedTx2.getType().getEthSerializedType())); | ||
} | ||
|
||
@Test | ||
public void blockNotFoundWhenHash63CharsLong() { | ||
/* Valid hash with 63 chars in - should result in block not found */ | ||
JsonRpcResponse actualResponse = method.response(requestWithParams(HASH_63_CHARS_LONG)); | ||
assertThat(actualResponse).usingRecursiveComparison().isEqualTo(blockNotFoundResponse); | ||
} | ||
|
||
@Test | ||
public void blockNotFoundForZeroHash() { | ||
/* Zero hash - should result in block not found */ | ||
JsonRpcResponse actualResponse = method.response(requestWithParams(ZERO_HASH)); | ||
assertThat(actualResponse).usingRecursiveComparison().isEqualTo(blockNotFoundResponse); | ||
} | ||
|
||
private JsonRpcRequestContext requestWithParams(final Object... params) { | ||
return new JsonRpcRequestContext(new JsonRpcRequest(JSON_RPC_VERSION, ETH_METHOD, params)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also have a look at EthJsonRpcHttpBySpecTest - maybe add some "by spec" tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the pointer. I'd found the graphql spec tests but not spotted general JSON/RPC spec tests. Latest commit adds a variety to cover by hash, by number, and by tag.