Skip to content

Commit

Permalink
Use cached header for latest eth_getBlockByNumber (hyperledger#5292)
Browse files Browse the repository at this point in the history
While syncing, it is possible that we access the cached header's number before the block itself has had chance to be imported. In other words there is a mismatch between cached blockNumber and non-cached blockHeader that is being retrieved for the RPC.
Signed-off-by: Simon Dudley <simon.dudley@consensys.net>
  • Loading branch information
siladu authored and jflo committed Apr 5, 2023
1 parent 0df53b0 commit 5d4df3f
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
- Upgrade RocksDB version from 7.7.3 to 8.0.0. Besu Team [contributed](https://github.com/facebook/rocksdb/pull/11099) to this release to make disabling checksum verification work.

### Bug Fixes
- Fix eth_getBlockByNumber cache error for latest block when called during syncing [#5292](https://github.com/hyperledger/besu/pull/5292)
- Fix QBFT and IBFT unable to propose blocks on London when zeroBaseFee is used [#5276](https://github.com/hyperledger/besu/pull/5276)

- Make QBFT validator smart contract mode work with london fork [#5249](https://github.com/hyperledger/besu/issues/5249)

### Download Links
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResult;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.BlockResultFactory;
import org.hyperledger.besu.ethereum.api.query.BlockchainQueries;
import org.hyperledger.besu.ethereum.chain.Blockchain;
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.Synchronizer;

Expand Down Expand Up @@ -80,8 +79,7 @@ protected Object resultByBlockNumber(
protected Object latestResult(final JsonRpcRequestContext request) {

final long headBlockNumber = blockchainQueriesSupplier.get().headBlockNumber();
Blockchain chain = blockchainQueriesSupplier.get().getBlockchain();
BlockHeader headHeader = chain.getBlockHeader(headBlockNumber).orElse(null);
BlockHeader headHeader = blockchainQueriesSupplier.get().headBlockHeader();

Hash block = headHeader.getHash();
Hash stateRoot = headHeader.getStateRoot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1063,10 +1063,7 @@ public void getBlockByNumberForLatest() throws Exception {
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()));
when(blockchainQueries.headBlockHeader()).thenReturn(block.getHeader());
WorldStateArchive state = mock(WorldStateArchive.class);
when(state.isWorldStateAvailable(any(Hash.class), any(Hash.class))).thenReturn(true);
when(blockchainQueries.getWorldStateArchive()).thenReturn(state);
Expand Down Expand Up @@ -1097,11 +1094,8 @@ public void getBlockByNumberForPending() throws Exception {
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()));
when(blockchainQueries.headBlockHeader()).thenReturn(block.getHeader());
WorldStateArchive state = mock(WorldStateArchive.class);
when(state.isWorldStateAvailable(any(Hash.class), any(Hash.class))).thenReturn(true);
when(blockchainQueries.getWorldStateArchive()).thenReturn(state);
Expand Down

0 comments on commit 5d4df3f

Please sign in to comment.