-
Notifications
You must be signed in to change notification settings - Fork 985
8337 modify chain data pruner #8506
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
Changes from 13 commits
daecbb7
749fe64
388f631
eed10b3
5d893b5
5b12159
1931d3f
17c8c16
9063878
a0c3ce9
e9e5cb0
823c930
d87b84a
24a954e
7ec3015
faa9a0f
cb73c1a
ec9ba28
51173f6
b7611b7
458ceea
db8e25c
0b7a74a
53580c9
7148efe
9c9bb01
210b06c
e0c5554
2b8cf7e
7182cf8
02390e5
84eaf2c
c197b1e
f5c3c65
5769dea
d7f062f
883ac25
a106767
be85913
c74540c
229dfdb
18479be
3dcfc69
67f7978
b03d76d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,11 +25,13 @@ | |
| /** The Chain pruning CLI options. */ | ||
| public class ChainPruningOptions implements CLIOptions<ChainPrunerConfiguration> { | ||
| private static final String CHAIN_PRUNING_ENABLED_FLAG = "--Xchain-pruning-enabled"; | ||
| private static final String PRE_MERGE_PRUNING_ENABLED_FLAG = "--Xpre-merge-pruning-enabled"; | ||
Matilda-Clerke marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private static final String CHAIN_PRUNING_BLOCKS_RETAINED_FLAG = | ||
| "--Xchain-pruning-blocks-retained"; | ||
| private static final String CHAIN_PRUNING_BLOCKS_RETAINED_LIMIT_FLAG = | ||
| "--Xchain-pruning-blocks-retained-limit"; | ||
| private static final String CHAIN_PRUNING_FREQUENCY_FLAG = "--Xchain-pruning-frequency"; | ||
| private static final String PRE_MERGE_PRUNING_QUANTITY_FLAG = "--Xpre-merge-pruning-quantity"; | ||
|
|
||
| /** | ||
| * The "CHAIN_DATA_PRUNING_MIN_BLOCKS_RETAINED_LIMIT" field sets the minimum limit for the | ||
|
|
@@ -42,20 +44,32 @@ public class ChainPruningOptions implements CLIOptions<ChainPrunerConfiguration> | |
| /** The constant DEFAULT_CHAIN_DATA_PRUNING_FREQUENCY. */ | ||
| public static final int DEFAULT_CHAIN_DATA_PRUNING_FREQUENCY = 256; | ||
|
|
||
| /** The constant DEFAULT_PRE_MERGE_PRUNING_QUANTITY. */ | ||
| public static final int DEFAULT_PRE_MERGE_PRUNING_QUANTITY = 256; | ||
|
|
||
| @CommandLine.Option( | ||
| hidden = true, | ||
| names = {CHAIN_PRUNING_ENABLED_FLAG}, | ||
| description = | ||
| "Enable the chain pruner to actively prune old chain data (default: ${DEFAULT-VALUE})") | ||
| private final Boolean chainDataPruningEnabled = Boolean.FALSE; | ||
|
|
||
| @CommandLine.Option( | ||
| hidden = true, | ||
| names = {PRE_MERGE_PRUNING_ENABLED_FLAG}, | ||
| description = | ||
| "Enable the chain pruner to actively prune pre-merge blocks, but not headers (default: ${DEFAULT-VALUE})") | ||
| private final Boolean preMergePruningEnabled = Boolean.FALSE; | ||
|
||
|
|
||
| @CommandLine.Option( | ||
| hidden = true, | ||
| names = {CHAIN_PRUNING_BLOCKS_RETAINED_FLAG}, | ||
| description = | ||
| "The number of recent blocks for which to keep the chain data. Should be >= " | ||
| + CHAIN_DATA_PRUNING_MIN_BLOCKS_RETAINED_LIMIT | ||
| + " (default: ${DEFAULT-VALUE})") | ||
| + " (default: ${DEFAULT-VALUE}). Unused if " | ||
Matilda-Clerke marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| + PRE_MERGE_PRUNING_ENABLED_FLAG | ||
| + " is enabled") | ||
| private final Long chainDataPruningBlocksRetained = CHAIN_DATA_PRUNING_MIN_BLOCKS_RETAINED_LIMIT; | ||
|
|
||
| @CommandLine.Option( | ||
|
|
@@ -65,7 +79,9 @@ public class ChainPruningOptions implements CLIOptions<ChainPrunerConfiguration> | |
| "Allows setting the limit below which no more blocks can be pruned. This prevents setting a value lower than this for " | ||
| + CHAIN_PRUNING_BLOCKS_RETAINED_FLAG | ||
| + ". This flag should be used with caution as reducing the limit may have unintended side effects." | ||
| + " (default: ${DEFAULT-VALUE})") | ||
| + " (default: ${DEFAULT-VALUE}). Unused if " | ||
Matilda-Clerke marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| + PRE_MERGE_PRUNING_ENABLED_FLAG | ||
| + " is enabled") | ||
| private final Long chainDataPruningBlocksRetainedLimit = | ||
| CHAIN_DATA_PRUNING_MIN_BLOCKS_RETAINED_LIMIT; | ||
|
|
||
|
|
@@ -77,6 +93,14 @@ public class ChainPruningOptions implements CLIOptions<ChainPrunerConfiguration> | |
| private final PositiveNumber chainDataPruningBlocksFrequency = | ||
| PositiveNumber.fromInt(DEFAULT_CHAIN_DATA_PRUNING_FREQUENCY); | ||
|
|
||
| @CommandLine.Option( | ||
| hidden = true, | ||
| names = {PRE_MERGE_PRUNING_QUANTITY_FLAG}, | ||
| description = | ||
| "The number of pre-merge blocks to prune per pruning operation. Must be non-negative (default: ${DEFAULT-VALUE})") | ||
| private final PositiveNumber preMergePruningBlocksQuantity = | ||
| PositiveNumber.fromInt(DEFAULT_PRE_MERGE_PRUNING_QUANTITY); | ||
|
|
||
| /** Default Constructor. */ | ||
| ChainPruningOptions() {} | ||
|
|
||
|
|
@@ -98,6 +122,15 @@ public Boolean getChainDataPruningEnabled() { | |
| return chainDataPruningEnabled; | ||
| } | ||
|
|
||
| /** | ||
| * Gets pre-merge pruning enabled | ||
| * | ||
| * @return the pre-merge pruning enabled | ||
| */ | ||
| public Boolean getPreMergePruningEnabled() { | ||
| return preMergePruningEnabled; | ||
| } | ||
|
|
||
| /** | ||
| * Gets chain data pruning blocks retained. | ||
| * | ||
|
|
@@ -120,21 +153,27 @@ public Long getChainDataPruningBlocksRetainedLimit() { | |
| public ChainPrunerConfiguration toDomainObject() { | ||
| return new ChainPrunerConfiguration( | ||
| chainDataPruningEnabled, | ||
| preMergePruningEnabled, | ||
| chainDataPruningBlocksRetained, | ||
| chainDataPruningBlocksRetainedLimit, | ||
| chainDataPruningBlocksFrequency.getValue()); | ||
| chainDataPruningBlocksFrequency.getValue(), | ||
| preMergePruningBlocksQuantity.getValue()); | ||
| } | ||
|
|
||
| @Override | ||
| public List<String> getCLIOptions() { | ||
| return Arrays.asList( | ||
| CHAIN_PRUNING_ENABLED_FLAG, | ||
| chainDataPruningEnabled.toString(), | ||
| PRE_MERGE_PRUNING_ENABLED_FLAG, | ||
| preMergePruningEnabled.toString(), | ||
| CHAIN_PRUNING_BLOCKS_RETAINED_FLAG, | ||
| chainDataPruningBlocksRetained.toString(), | ||
| CHAIN_PRUNING_BLOCKS_RETAINED_LIMIT_FLAG, | ||
| chainDataPruningBlocksRetainedLimit.toString(), | ||
| CHAIN_PRUNING_FREQUENCY_FLAG, | ||
| chainDataPruningBlocksFrequency.toString()); | ||
| chainDataPruningBlocksFrequency.toString(), | ||
| PRE_MERGE_PRUNING_QUANTITY_FLAG, | ||
| preMergePruningBlocksQuantity.toString()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
|
|
||
| import static com.google.common.base.Preconditions.checkNotNull; | ||
|
|
||
| import org.hyperledger.besu.cli.config.NetworkName; | ||
| import org.hyperledger.besu.components.BesuComponent; | ||
| import org.hyperledger.besu.config.CheckpointConfigOptions; | ||
| import org.hyperledger.besu.config.GenesisConfig; | ||
|
|
@@ -115,8 +116,10 @@ | |
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.OptionalLong; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import java.util.function.Supplier; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
@@ -125,6 +128,9 @@ | |
| public abstract class BesuControllerBuilder implements MiningParameterOverrides { | ||
| private static final Logger LOG = LoggerFactory.getLogger(BesuControllerBuilder.class); | ||
|
|
||
| private static final long MAINNET_MERGE_BLOCK_NUMBER = 15_537_393; | ||
| private static final long SEPOLIA_MERGE_BLOCK_NUMBER = 1_735_371; | ||
Matilda-Clerke marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** The genesis file */ | ||
| protected GenesisConfig genesisConfig; | ||
|
|
||
|
|
@@ -695,14 +701,27 @@ public BesuController build() { | |
| final boolean fullSyncDisabled = !SyncMode.isFullSync(syncConfig.getSyncMode()); | ||
| final SyncState syncState = new SyncState(blockchain, ethPeers, fullSyncDisabled, checkpoint); | ||
|
|
||
| if (chainPrunerConfiguration.getChainPruningEnabled()) { | ||
| final ChainDataPruner chainDataPruner = createChainPruner(blockchainStorage); | ||
| blockchain.observeBlockAdded(chainDataPruner); | ||
| LOG.info( | ||
| "Chain data pruning enabled with recent blocks retained to be: " | ||
| + chainPrunerConfiguration.getChainPruningBlocksRetained() | ||
| + " and frequency to be: " | ||
| + chainPrunerConfiguration.getChainPruningBlocksFrequency()); | ||
| if (chainPrunerConfiguration.chainPruningEnabled() | ||
| || chainPrunerConfiguration.preMergePruningEnabled()) { | ||
| LOG.info("Adding ChainDataPruner to observe block added events"); | ||
| final AtomicLong chainDataPrunerObserverId = new AtomicLong(); | ||
| final ChainDataPruner chainDataPruner = | ||
| createChainPruner( | ||
| blockchainStorage, () -> blockchain.removeObserver(chainDataPrunerObserverId.get())); | ||
| chainDataPrunerObserverId.set(blockchain.observeBlockAdded(chainDataPruner)); | ||
| if (chainPrunerConfiguration.chainPruningEnabled()) { | ||
| LOG.info( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good user feedback. |
||
| "Chain data pruning enabled with recent blocks retained to be: " | ||
| + chainPrunerConfiguration.chainPruningBlocksRetained() | ||
| + " and frequency to be: " | ||
| + chainPrunerConfiguration.blocksFrequency()); | ||
| } else if (chainPrunerConfiguration.preMergePruningEnabled()) { | ||
| LOG.info( | ||
| "Pre-merge block pruning enabled with frequency: " | ||
| + chainPrunerConfiguration.blocksFrequency() | ||
| + " and quantity: " | ||
| + chainPrunerConfiguration.preMergePruningBlocksQuantity()); | ||
| } | ||
| } | ||
|
|
||
| final TransactionPool transactionPool = | ||
|
|
@@ -1159,14 +1178,32 @@ yield new ForestWorldStateArchive( | |
| }; | ||
| } | ||
|
|
||
| private ChainDataPruner createChainPruner(final BlockchainStorage blockchainStorage) { | ||
| private ChainDataPruner createChainPruner( | ||
| final BlockchainStorage blockchainStorage, final Runnable unsubscribeRunnable) { | ||
| NetworkName network = | ||
| Stream.of(NetworkName.values()) | ||
Matilda-Clerke marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .filter((n) -> n.getNetworkId().equals(networkId)) | ||
| .findAny() | ||
| .orElseThrow(() -> new RuntimeException("Unrecognised network")); | ||
| return new ChainDataPruner( | ||
| blockchainStorage, | ||
| unsubscribeRunnable, | ||
| new ChainDataPrunerStorage( | ||
| storageProvider.getStorageBySegmentIdentifier( | ||
| KeyValueSegmentIdentifier.CHAIN_PRUNER_STATE)), | ||
| chainPrunerConfiguration.getChainPruningBlocksRetained(), | ||
| chainPrunerConfiguration.getChainPruningBlocksFrequency(), | ||
| switch (network) { | ||
| case MAINNET -> MAINNET_MERGE_BLOCK_NUMBER; | ||
| case SEPOLIA -> SEPOLIA_MERGE_BLOCK_NUMBER; | ||
| default -> 0; | ||
|
||
| }, | ||
| chainPrunerConfiguration.chainPruningEnabled() | ||
| ? ChainDataPruner.Mode.CHAIN_PRUNING | ||
| : (chainPrunerConfiguration.preMergePruningEnabled() | ||
| ? ChainDataPruner.Mode.PRE_MERGE_PRUNING | ||
| : null), | ||
| chainPrunerConfiguration.chainPruningBlocksRetained(), | ||
| chainPrunerConfiguration.blocksFrequency(), | ||
| chainPrunerConfiguration.preMergePruningBlocksQuantity(), | ||
| MonitoredExecutors.newBoundedThreadPool( | ||
| ChainDataPruner.class.getSimpleName(), | ||
| 1, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.