From d5329b2a0661182da6f60dac27cbb88a91ac74d0 Mon Sep 17 00:00:00 2001 From: garyschulte Date: Fri, 3 Feb 2023 19:34:58 -0800 Subject: [PATCH] Keep Worldstate Storage open for Bonsai archive latest layer (#5039) * bonsai layered worldstate subscription * unsubscribe from worldstatestorage on close of BonsaiLayeredWorldState * minor txpool logging improvements Signed-off-by: garyschulte --- CHANGELOG.md | 1 + .../bonsai/BonsaiInMemoryWorldState.java | 7 ++++++- .../bonsai/BonsaiLayeredWorldState.java | 20 +++++++++++++++++++ .../AbstractPendingTransactionsSorter.java | 6 +++++- 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b85ae7e8987..946105444bab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ tests are updated to use EC private keys instead of RSA keys. ### Bug Fixes - Mitigation fix for stale bonsai code storage leading to log rolling issues on contract recreates [#4906](https://github.com/hyperledger/besu/pull/4906) +- Ensure latest cached layered worldstate is subscribed to storage, fix problem with RPC calls using 'latest' [#5039](https://github.com/hyperledger/besu/pull/5039) ## 23.1.0-RC1 diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/BonsaiInMemoryWorldState.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/BonsaiInMemoryWorldState.java index 64ee2c42fbe3..f54be5222020 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/BonsaiInMemoryWorldState.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/BonsaiInMemoryWorldState.java @@ -150,7 +150,12 @@ public void persist(final BlockHeader blockHeader) { final Hash newWorldStateRootHash = rootHash(localUpdater); archive .getTrieLogManager() - .saveTrieLog(archive, localUpdater, newWorldStateRootHash, blockHeader, this); + .saveTrieLog( + archive, + localUpdater, + newWorldStateRootHash, + blockHeader, + (BonsaiPersistedWorldState) this.copy()); worldStateRootHash = newWorldStateRootHash; worldStateBlockHash = blockHeader.getBlockHash(); isPersisted = true; diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/BonsaiLayeredWorldState.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/BonsaiLayeredWorldState.java index 222aef38ee6e..dc64bc756c6e 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/BonsaiLayeredWorldState.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/BonsaiLayeredWorldState.java @@ -75,9 +75,29 @@ public Optional getNextWorldView() { } public void setNextWorldView(final Optional nextWorldView) { + maybeUnSubscribe(); this.nextWorldView = nextWorldView; } + private void maybeUnSubscribe() { + nextWorldView + .filter(WorldState.class::isInstance) + .map(WorldState.class::cast) + .ifPresent( + ws -> { + try { + ws.close(); + } catch (final Exception e) { + // no-op + } + }); + } + + @Override + public void close() throws Exception { + maybeUnSubscribe(); + } + public TrieLogLayer getTrieLog() { return trieLog; } diff --git a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/sorter/AbstractPendingTransactionsSorter.java b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/sorter/AbstractPendingTransactionsSorter.java index 1f3c938ea99c..cea31614f290 100644 --- a/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/sorter/AbstractPendingTransactionsSorter.java +++ b/ethereum/eth/src/main/java/org/hyperledger/besu/ethereum/eth/transactions/sorter/AbstractPendingTransactionsSorter.java @@ -428,7 +428,11 @@ private TransactionAddedStatus addTransaction( LOG, "Transaction {} not added because nonce too far in the future for sender {}", transaction::toTraceLog, - maybeSenderAccount::toString); + () -> + maybeSenderAccount + .map(Account::getAddress) + .map(Address::toString) + .orElse("unknown")); return NONCE_TOO_FAR_IN_FUTURE_FOR_SENDER; }