Skip to content

Commit

Permalink
Add feature flag --receipt-compaction-enabled so this can be disabled
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Frame <[email protected]>
  • Loading branch information
jframe committed Feb 29, 2024
1 parent 726f8a3 commit 617ec3f
Show file tree
Hide file tree
Showing 25 changed files with 109 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.hyperledger.besu.cli.options.stable;

import static org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration.DEFAULT_BONSAI_MAX_LAYERS_TO_LOAD;
import static org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration.DEFAULT_RECEIPT_COMPACTION_ENABLED;
import static org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration.Unstable.DEFAULT_BONSAI_CODE_USING_CODE_HASH_ENABLED;
import static org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration.Unstable.DEFAULT_BONSAI_LIMIT_TRIE_LOGS_ENABLED;
import static org.hyperledger.besu.ethereum.worldstate.DataStorageConfiguration.Unstable.DEFAULT_BONSAI_TRIE_LOG_PRUNING_WINDOW_SIZE;
Expand Down Expand Up @@ -61,6 +62,12 @@ public class DataStorageOptions implements CLIOptions<DataStorageConfiguration>
arity = "1")
private Long bonsaiMaxLayersToLoad = DEFAULT_BONSAI_MAX_LAYERS_TO_LOAD;

@Option(
names = "--receipt-compaction-enabled",
description = "Enables compact storing of receipts (default: ${DEFAULT-VALUE}).",
arity = "1")
private Boolean receiptCompactionEnabled = DEFAULT_RECEIPT_COMPACTION_ENABLED;

@CommandLine.ArgGroup(validate = false)
private final DataStorageOptions.Unstable unstableOptions = new Unstable();

Expand Down Expand Up @@ -149,6 +156,7 @@ public static DataStorageOptions fromConfig(final DataStorageConfiguration domai
final DataStorageOptions dataStorageOptions = DataStorageOptions.create();
dataStorageOptions.dataStorageFormat = domainObject.getDataStorageFormat();
dataStorageOptions.bonsaiMaxLayersToLoad = domainObject.getBonsaiMaxLayersToLoad();
dataStorageOptions.receiptCompactionEnabled = domainObject.getReceiptCompactionEnabled();
dataStorageOptions.unstableOptions.bonsaiLimitTrieLogsEnabled =
domainObject.getUnstable().getBonsaiLimitTrieLogsEnabled();
dataStorageOptions.unstableOptions.bonsaiTrieLogPruningWindowSize =
Expand All @@ -164,6 +172,7 @@ public DataStorageConfiguration toDomainObject() {
return ImmutableDataStorageConfiguration.builder()
.dataStorageFormat(dataStorageFormat)
.bonsaiMaxLayersToLoad(bonsaiMaxLayersToLoad)
.receiptCompactionEnabled(receiptCompactionEnabled)
.unstable(
ImmutableDataStorageConfiguration.Unstable.builder()
.bonsaiLimitTrieLogsEnabled(unstableOptions.bonsaiLimitTrieLogsEnabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,10 @@ public BesuController build() {
storageProvider.createWorldStateStorage(dataStorageConfiguration);

final BlockchainStorage blockchainStorage =
storageProvider.createBlockchainStorage(protocolSchedule, variablesStorage);
storageProvider.createBlockchainStorage(
protocolSchedule,
variablesStorage,
dataStorageConfiguration.getReceiptCompactionEnabled());

final MutableBlockchain blockchain =
DefaultBlockchain.createMutable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,24 @@ public void bonsaiCodeUsingCodeHashEnabledCanBeDisabled() {
"false");
}

@Test
public void receiptCompactionCanBeEnabled() {
internalTestSuccess(
dataStorageConfiguration ->
assertThat(dataStorageConfiguration.getReceiptCompactionEnabled()).isEqualTo(true),
"--receipt-compaction-enabled",
"true");
}

@Test
public void receiptCompactionCanBeDisabled() {
internalTestSuccess(
dataStorageConfiguration ->
assertThat(dataStorageConfiguration.getReceiptCompactionEnabled()).isEqualTo(false),
"--receipt-compaction-enabled",
"false");
}

@Override
protected DataStorageConfiguration createDefaultDomainObject() {
return DataStorageConfiguration.DEFAULT_CONFIG;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.hyperledger.besu.controller;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -118,12 +119,13 @@ public void setup() {
when(ethashConfigOptions.getFixedDifficulty()).thenReturn(OptionalLong.empty());
when(storageProvider.getStorageBySegmentIdentifier(any()))
.thenReturn(new InMemoryKeyValueStorage());
when(storageProvider.createBlockchainStorage(any(), any()))
when(storageProvider.createBlockchainStorage(any(), any(), anyBoolean()))
.thenReturn(
new KeyValueStoragePrefixedKeyBlockchainStorage(
new InMemoryKeyValueStorage(),
new VariablesKeyValueStorage(new InMemoryKeyValueStorage()),
new MainnetBlockHeaderFunctions()));
new MainnetBlockHeaderFunctions(),
false));
when(synchronizerConfiguration.getDownloaderParallelism()).thenReturn(1);
when(synchronizerConfiguration.getTransactionsParallelism()).thenReturn(1);
when(synchronizerConfiguration.getComputationParallelism()).thenReturn(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.hyperledger.besu.ethereum.core.InMemoryKeyValueStorageProvider.createInMemoryBlockchain;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -127,12 +128,13 @@ public void setup() {
when(genesisConfigOptions.getTerminalBlockHash()).thenReturn(Optional.of(Hash.ZERO));
lenient().when(genesisConfigOptions.getTerminalBlockNumber()).thenReturn(OptionalLong.of(1L));
lenient()
.when(storageProvider.createBlockchainStorage(any(), any()))
.when(storageProvider.createBlockchainStorage(any(), any(), anyBoolean()))
.thenReturn(
new KeyValueStoragePrefixedKeyBlockchainStorage(
new InMemoryKeyValueStorage(),
new VariablesKeyValueStorage(new InMemoryKeyValueStorage()),
new MainnetBlockHeaderFunctions()));
new MainnetBlockHeaderFunctions(),
false));
lenient()
.when(storageProvider.getStorageBySegmentIdentifier(any()))
.thenReturn(new InMemoryKeyValueStorage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -107,12 +108,13 @@ public void setup() {
lenient().when(genesisConfigFile.getConfigOptions()).thenReturn(genesisConfigOptions);
lenient().when(genesisConfigOptions.getCheckpointOptions()).thenReturn(checkpointConfigOptions);
lenient()
.when(storageProvider.createBlockchainStorage(any(), any()))
.when(storageProvider.createBlockchainStorage(any(), any(), anyBoolean()))
.thenReturn(
new KeyValueStoragePrefixedKeyBlockchainStorage(
new InMemoryKeyValueStorage(),
new VariablesKeyValueStorage(new InMemoryKeyValueStorage()),
new MainnetBlockHeaderFunctions()));
new MainnetBlockHeaderFunctions(),
false));
lenient()
.when(
storageProvider.createWorldStateStorage(DataStorageConfiguration.DEFAULT_FOREST_CONFIG))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ public void setUp() {
new KeyValueStoragePrefixedKeyBlockchainStorage(
new InMemoryKeyValueStorage(),
new VariablesKeyValueStorage(new InMemoryKeyValueStorage()),
new MainnetBlockHeaderFunctions()),
new MainnetBlockHeaderFunctions(),
false),
new NoOpMetricsSystem(),
0);

Expand Down
1 change: 1 addition & 0 deletions besu/src/test/resources/everything_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ ethstats-cacert-file="./root.cert"
# Data storage
data-storage-format="BONSAI"
bonsai-historical-block-limit=512
receipt-compaction-enabled=true

# feature flags
Xsecp256k1-native-enabled=false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ private void backupChainData() throws IOException {
bodyWriter.writeBytes(bodyOutput.encoded().toArrayUnsafe());

final BytesValueRLPOutput receiptsOutput = new BytesValueRLPOutput();
receiptsOutput.writeList(receipts.get(), TransactionReceipt::writeToForStorage);
receiptsOutput.writeList(receipts.get(), (r, rlpOut) -> r.writeToForStorage(rlpOut, false));
receiptsWriter.writeBytes(receiptsOutput.encoded().toArrayUnsafe());

backupStatus.storedBlock = blockNumber;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public class NewBlockHeadersSubscriptionServiceTest {
new KeyValueStoragePrefixedKeyBlockchainStorage(
new InMemoryKeyValueStorage(),
new VariablesKeyValueStorage(new InMemoryKeyValueStorage()),
new MainnetBlockHeaderFunctions());
new MainnetBlockHeaderFunctions(),
false);
private final Block genesisBlock = gen.genesisBlock();
private final MutableBlockchain blockchain =
DefaultBlockchain.createMutable(genesisBlock, blockchainStorage, new NoOpMetricsSystem(), 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ public void setup() {
new KeyValueStoragePrefixedKeyBlockchainStorage(
new InMemoryKeyValueStorage(),
new VariablesKeyValueStorage(new InMemoryKeyValueStorage()),
new MainnetBlockHeaderFunctions()),
new MainnetBlockHeaderFunctions(),
false),
new NoOpMetricsSystem(),
0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ public void writeToForNetwork(final RLPOutput out) {
writeTo(out, false, false);
}

public void writeToForStorage(final RLPOutput out) {
writeTo(out, true, true);
public void writeToForStorage(final RLPOutput out, final boolean compacted) {
writeTo(out, true, compacted);
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public interface StorageProvider extends Closeable {
VariablesStorage createVariablesStorage();

BlockchainStorage createBlockchainStorage(
ProtocolSchedule protocolSchedule, VariablesStorage variablesStorage);
ProtocolSchedule protocolSchedule,
VariablesStorage variablesStorage,
final boolean receiptCompaction);

WorldStateStorage createWorldStateStorage(DataStorageConfiguration dataStorageFormat);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,17 @@ public class KeyValueStoragePrefixedKeyBlockchainStorage implements BlockchainSt
final KeyValueStorage blockchainStorage;
final VariablesStorage variablesStorage;
final BlockHeaderFunctions blockHeaderFunctions;
final boolean receiptCompaction;

public KeyValueStoragePrefixedKeyBlockchainStorage(
final KeyValueStorage blockchainStorage,
final VariablesStorage variablesStorage,
final BlockHeaderFunctions blockHeaderFunctions) {
final BlockHeaderFunctions blockHeaderFunctions,
final boolean receiptCompaction) {
this.blockchainStorage = blockchainStorage;
this.variablesStorage = variablesStorage;
this.blockHeaderFunctions = blockHeaderFunctions;
this.receiptCompaction = receiptCompaction;
migrateVariables();
}

Expand Down Expand Up @@ -125,7 +128,8 @@ public Optional<TransactionLocation> getTransactionLocation(final Hash transacti

@Override
public Updater updater() {
return new Updater(blockchainStorage.startTransaction(), variablesStorage.updater());
return new Updater(
blockchainStorage.startTransaction(), variablesStorage.updater(), receiptCompaction);
}

private List<TransactionReceipt> rlpDecodeTransactionReceipts(final Bytes bytes) {
Expand Down Expand Up @@ -253,12 +257,15 @@ public static class Updater implements BlockchainStorage.Updater {

private final KeyValueStorageTransaction blockchainTransaction;
private final VariablesStorage.Updater variablesUpdater;
private final boolean receiptCompaction;

Updater(
final KeyValueStorageTransaction blockchainTransaction,
final VariablesStorage.Updater variablesUpdater) {
final VariablesStorage.Updater variablesUpdater,
final boolean receiptCompaction) {
this.blockchainTransaction = blockchainTransaction;
this.variablesUpdater = variablesUpdater;
this.receiptCompaction = receiptCompaction;
}

@Override
Expand Down Expand Up @@ -365,7 +372,10 @@ private void remove(final Bytes prefix, final Bytes key) {
}

private Bytes rlpEncode(final List<TransactionReceipt> receipts) {
return RLP.encode(o -> o.writeList(receipts, TransactionReceipt::writeToForStorage));
return RLP.encode(
o ->
o.writeList(
receipts, (r, rlpOutput) -> r.writeToForStorage(rlpOutput, receiptCompaction)));
}

private void removeVariables() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ public VariablesStorage createVariablesStorage() {

@Override
public BlockchainStorage createBlockchainStorage(
final ProtocolSchedule protocolSchedule, final VariablesStorage variablesStorage) {
final ProtocolSchedule protocolSchedule,
final VariablesStorage variablesStorage,
final boolean receiptCompaction) {
return new KeyValueStoragePrefixedKeyBlockchainStorage(
getStorageBySegmentIdentifier(KeyValueSegmentIdentifier.BLOCKCHAIN),
variablesStorage,
ScheduleBasedBlockHeaderFunctions.create(protocolSchedule));
ScheduleBasedBlockHeaderFunctions.create(protocolSchedule),
receiptCompaction);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
public interface DataStorageConfiguration {

long DEFAULT_BONSAI_MAX_LAYERS_TO_LOAD = 512;
boolean DEFAULT_RECEIPT_COMPACTION_ENABLED = true;

DataStorageConfiguration DEFAULT_CONFIG =
ImmutableDataStorageConfiguration.builder()
Expand All @@ -48,6 +49,11 @@ public interface DataStorageConfiguration {

Long getBonsaiMaxLayersToLoad();

@Value.Default
default boolean getReceiptCompactionEnabled() {
return DEFAULT_RECEIPT_COMPACTION_ENABLED;
}

@Value.Default
default Unstable getUnstable() {
return Unstable.DEFAULT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ private ExecutionContextTestFixture(
new KeyValueStoragePrefixedKeyBlockchainStorage(
blockchainKeyValueStorage,
new VariablesKeyValueStorage(variablesKeyValueStorage),
new MainnetBlockHeaderFunctions()),
new MainnetBlockHeaderFunctions(),
false),
new NoOpMetricsSystem(),
0);
this.stateArchive = createInMemoryWorldStateArchive();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static MutableBlockchain createInMemoryBlockchain(
return DefaultBlockchain.createMutable(
genesisBlock,
new KeyValueStoragePrefixedKeyBlockchainStorage(
keyValueStorage, variablesStorage, blockHeaderFunctions),
keyValueStorage, variablesStorage, blockHeaderFunctions, false),
new NoOpMetricsSystem(),
0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public void singleChainPruning() {
new KeyValueStoragePrefixedKeyBlockchainStorage(
new InMemoryKeyValueStorage(),
new VariablesKeyValueStorage(new InMemoryKeyValueStorage()),
new MainnetBlockHeaderFunctions());
new MainnetBlockHeaderFunctions(),
false);
final ChainDataPruner chainDataPruner =
new ChainDataPruner(
blockchainStorage,
Expand Down Expand Up @@ -79,7 +80,8 @@ public void forkPruning() {
new KeyValueStoragePrefixedKeyBlockchainStorage(
new InMemoryKeyValueStorage(),
new VariablesKeyValueStorage(new InMemoryKeyValueStorage()),
new MainnetBlockHeaderFunctions());
new MainnetBlockHeaderFunctions(),
false);
final ChainDataPruner chainDataPruner =
new ChainDataPruner(
blockchainStorage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,8 @@ private BlockchainStorage createStorage(
return new KeyValueStoragePrefixedKeyBlockchainStorage(
kvStoreChain,
new VariablesKeyValueStorage(kvStorageVariables),
new MainnetBlockHeaderFunctions());
new MainnetBlockHeaderFunctions(),
false);
}

private DefaultBlockchain createMutableBlockchain(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void migrationToVariablesStorage() {

final var blockchainStorage =
new KeyValueStoragePrefixedKeyBlockchainStorage(
kvBlockchain, variablesStorage, blockHeaderFunctions);
kvBlockchain, variablesStorage, blockHeaderFunctions, false);

assertNoVariablesInStorage(kvBlockchain);
assertVariablesPresentInVariablesStorage(kvVariables, variableValues);
Expand All @@ -80,7 +80,7 @@ public void migrationToVariablesStorageWhenSomeVariablesDoNotExist() {

final var blockchainStorage =
new KeyValueStoragePrefixedKeyBlockchainStorage(
kvBlockchain, variablesStorage, blockHeaderFunctions);
kvBlockchain, variablesStorage, blockHeaderFunctions, false);

assertNoVariablesInStorage(kvBlockchain);
assertVariablesPresentInVariablesStorage(kvVariables, variableValues);
Expand All @@ -96,7 +96,7 @@ public void doesNothingIfVariablesAlreadyMigrated() {

final var blockchainStorage =
new KeyValueStoragePrefixedKeyBlockchainStorage(
kvBlockchain, variablesStorage, blockHeaderFunctions);
kvBlockchain, variablesStorage, blockHeaderFunctions, false);

assertNoVariablesInStorage(kvBlockchain);
assertVariablesPresentInVariablesStorage(kvVariables, variableValues);
Expand All @@ -114,6 +114,6 @@ public void failIfInconsistencyDetectedDuringVariablesMigration() {
IllegalStateException.class,
() ->
new KeyValueStoragePrefixedKeyBlockchainStorage(
kvBlockchain, variablesStorage, blockHeaderFunctions));
kvBlockchain, variablesStorage, blockHeaderFunctions, false));
}
}
Loading

0 comments on commit 617ec3f

Please sign in to comment.