From 048623a2e3fa09ca6816b2bcdade24d7896bd0ef Mon Sep 17 00:00:00 2001 From: Simon Dudley Date: Mon, 23 Oct 2023 13:29:10 +1000 Subject: [PATCH] Move TrieLogProvider setup code to AbstractTrieLogManager Signed-off-by: Simon Dudley --- .../cache/CachedWorldStorageManager.java | 80 ------------------- .../trielog/AbstractTrieLogManager.java | 76 +++++++++++++++++- 2 files changed, 74 insertions(+), 82 deletions(-) diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/cache/CachedWorldStorageManager.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/cache/CachedWorldStorageManager.java index d0f1be97d89..5a340e7e293 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/cache/CachedWorldStorageManager.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/cache/CachedWorldStorageManager.java @@ -21,16 +21,11 @@ import org.hyperledger.besu.ethereum.bonsai.storage.BonsaiWorldStateKeyValueStorage.BonsaiStorageSubscriber; import org.hyperledger.besu.ethereum.bonsai.storage.BonsaiWorldStateLayerStorage; import org.hyperledger.besu.ethereum.bonsai.trielog.AbstractTrieLogManager; -import org.hyperledger.besu.ethereum.bonsai.trielog.TrieLogFactoryImpl; import org.hyperledger.besu.ethereum.bonsai.worldview.BonsaiWorldState; import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.BlockHeader; import org.hyperledger.besu.metrics.ObservableMetricsSystem; import org.hyperledger.besu.plugin.BesuContext; -import org.hyperledger.besu.plugin.services.TrieLogService; -import org.hyperledger.besu.plugin.services.trielogs.TrieLog; -import org.hyperledger.besu.plugin.services.trielogs.TrieLogFactory; -import org.hyperledger.besu.plugin.services.trielogs.TrieLogProvider; import java.util.ArrayList; import java.util.Comparator; @@ -39,10 +34,7 @@ import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; -import java.util.stream.LongStream; -import java.util.stream.Stream; -import com.google.common.annotations.VisibleForTesting; import org.apache.tuweni.bytes.Bytes32; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -227,76 +219,4 @@ public void onClearTrieLog() { public void onCloseStorage() { this.cachedWorldStatesByHash.clear(); } - - @VisibleForTesting - @Override - protected TrieLogFactory setupTrieLogFactory(final BesuContext pluginContext) { - // if we have a TrieLogService from pluginContext, use it. - var trieLogServicez = - Optional.ofNullable(pluginContext) - .flatMap(context -> context.getService(TrieLogService.class)); - - if (trieLogServicez.isPresent()) { - var trieLogService = trieLogServicez.get(); - // push the TrieLogProvider into the TrieLogService - trieLogService.configureTrieLogProvider(getTrieLogProvider()); - - // configure plugin observers: - trieLogService.getObservers().forEach(trieLogObservers::subscribe); - - // return the TrieLogFactory implementation from the TrieLogService - return trieLogService.getTrieLogFactory(); - } else { - // Otherwise default to TrieLogFactoryImpl - return new TrieLogFactoryImpl(); - } - } - - @VisibleForTesting - TrieLogProvider getTrieLogProvider() { - return new TrieLogProvider() { - @Override - public Optional getTrieLogLayer(final Hash blockHash) { - return CachedWorldStorageManager.this.getTrieLogLayer(blockHash); - } - - @Override - public Optional getTrieLogLayer(final long blockNumber) { - return CachedWorldStorageManager.this - .blockchain - .getBlockHeader(blockNumber) - .map(BlockHeader::getHash) - .flatMap(CachedWorldStorageManager.this::getTrieLogLayer); - } - - @Override - public List getTrieLogsByRange( - final long fromBlockNumber, final long toBlockNumber) { - return rangeAsStream(fromBlockNumber, toBlockNumber) - .map(blockchain::getBlockHeader) - .map( - headerOpt -> - headerOpt.flatMap( - header -> - CachedWorldStorageManager.this - .getTrieLogLayer(header.getBlockHash()) - .map( - layer -> - new TrieLogRangeTuple( - header.getBlockHash(), header.getNumber(), layer)))) - .filter(Optional::isPresent) - .map(Optional::get) - .toList(); - } - - Stream rangeAsStream(final long fromBlockNumber, final long toBlockNumber) { - if (Math.abs(toBlockNumber - fromBlockNumber) > LOG_RANGE_LIMIT) { - throw new IllegalArgumentException("Requested Range too large"); - } - long left = Math.min(fromBlockNumber, toBlockNumber); - long right = Math.max(fromBlockNumber, toBlockNumber); - return LongStream.range(left, right).boxed(); - } - }; - } } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/trielog/AbstractTrieLogManager.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/trielog/AbstractTrieLogManager.java index 90aff386e83..7ab357c745c 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/trielog/AbstractTrieLogManager.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/trielog/AbstractTrieLogManager.java @@ -24,13 +24,18 @@ import org.hyperledger.besu.ethereum.chain.Blockchain; import org.hyperledger.besu.ethereum.core.BlockHeader; import org.hyperledger.besu.plugin.BesuContext; +import org.hyperledger.besu.plugin.services.TrieLogService; import org.hyperledger.besu.plugin.services.trielogs.TrieLog; import org.hyperledger.besu.plugin.services.trielogs.TrieLogEvent.TrieLogObserver; import org.hyperledger.besu.plugin.services.trielogs.TrieLogFactory; +import org.hyperledger.besu.plugin.services.trielogs.TrieLogProvider; import org.hyperledger.besu.util.Subscribers; +import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.stream.LongStream; +import java.util.stream.Stream; import com.google.common.annotations.VisibleForTesting; import org.apache.tuweni.bytes.Bytes32; @@ -63,8 +68,6 @@ protected AbstractTrieLogManager( this.trieLogFactory = setupTrieLogFactory(pluginContext); } - protected abstract TrieLogFactory setupTrieLogFactory(final BesuContext pluginContext); - @Override public synchronized void saveTrieLog( final BonsaiWorldStateUpdateAccumulator localUpdater, @@ -161,4 +164,73 @@ public synchronized long subscribe(final TrieLogObserver sub) { public synchronized void unsubscribe(final long id) { trieLogObservers.unsubscribe(id); } + + private TrieLogFactory setupTrieLogFactory(final BesuContext pluginContext) { + // if we have a TrieLogService from pluginContext, use it. + var trieLogServicez = + Optional.ofNullable(pluginContext) + .flatMap(context -> context.getService(TrieLogService.class)); + + if (trieLogServicez.isPresent()) { + var trieLogService = trieLogServicez.get(); + // push the TrieLogProvider into the TrieLogService + trieLogService.configureTrieLogProvider(getTrieLogProvider()); + + // configure plugin observers: + trieLogService.getObservers().forEach(trieLogObservers::subscribe); + + // return the TrieLogFactory implementation from the TrieLogService + return trieLogService.getTrieLogFactory(); + } else { + // Otherwise default to TrieLogFactoryImpl + return new TrieLogFactoryImpl(); + } + } + + private TrieLogProvider getTrieLogProvider() { + return new TrieLogProvider() { + @Override + public Optional getTrieLogLayer(final Hash blockHash) { + return AbstractTrieLogManager.this.getTrieLogLayer(blockHash); + } + + @Override + public Optional getTrieLogLayer(final long blockNumber) { + return AbstractTrieLogManager.this + .blockchain + .getBlockHeader(blockNumber) + .map(BlockHeader::getHash) + .flatMap(AbstractTrieLogManager.this::getTrieLogLayer); + } + + @Override + public List getTrieLogsByRange( + final long fromBlockNumber, final long toBlockNumber) { + return rangeAsStream(fromBlockNumber, toBlockNumber) + .map(blockchain::getBlockHeader) + .map( + headerOpt -> + headerOpt.flatMap( + header -> + AbstractTrieLogManager.this + .getTrieLogLayer(header.getBlockHash()) + .map( + layer -> + new TrieLogRangeTuple( + header.getBlockHash(), header.getNumber(), layer)))) + .filter(Optional::isPresent) + .map(Optional::get) + .toList(); + } + + Stream rangeAsStream(final long fromBlockNumber, final long toBlockNumber) { + if (Math.abs(toBlockNumber - fromBlockNumber) > LOG_RANGE_LIMIT) { + throw new IllegalArgumentException("Requested Range too large"); + } + long left = Math.min(fromBlockNumber, toBlockNumber); + long right = Math.max(fromBlockNumber, toBlockNumber); + return LongStream.range(left, right).boxed(); + } + }; + } }