Skip to content

Commit

Permalink
feat: add --genesis-state-hash-cache-enabled paramater (hyperledger#6758
Browse files Browse the repository at this point in the history
)

Signed-off-by: lyfsn <dev.wangyu@proton.me>
  • Loading branch information
lyfsn authored and macfarla committed Apr 26, 2024
1 parent 9abf373 commit 8289f34
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 4 deletions.
8 changes: 7 additions & 1 deletion besu/src/main/java/org/hyperledger/besu/cli/BesuCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@ public class BesuCommand implements DefaultCommandValues, Runnable {
"Genesis file for your custom network. Setting this option requires --network-id to be set. (Cannot be used with --network)")
private final File genesisFile = null;

@Option(
names = {"--genesis-state-hash-cache-enabled"},
description = "Use genesis state hash from data on startup if specified")
private final Boolean genesisStateHashCacheEnabled = false;

@Option(
names = "--identity",
paramLabel = "<String>",
Expand Down Expand Up @@ -1817,7 +1822,8 @@ public BesuControllerBuilder getControllerBuilder() {
.maxRemotelyInitiatedPeers(maxRemoteInitiatedPeers)
.randomPeerPriority(p2PDiscoveryOptionGroup.randomPeerPriority)
.chainPruningConfiguration(unstableChainPruningOptions.toDomainObject())
.cacheLastBlocks(numberOfblocksToCache);
.cacheLastBlocks(numberOfblocksToCache)
.genesisStateHashCacheEnabled(genesisStateHashCacheEnabled);
}

private JsonRpcConfiguration createEngineJsonRpcConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ public abstract class BesuControllerBuilder implements MiningParameterOverrides
.map(conf -> conf.getConfigOptions(genesisConfigOverrides))
.orElseThrow();

/** The is genesis state hash from data. */
protected boolean genesisStateHashCacheEnabled;

/** The Sync config. */
protected SynchronizerConfiguration syncConfig;

Expand Down Expand Up @@ -239,6 +242,18 @@ public BesuControllerBuilder genesisConfigFile(final GenesisConfigFile genesisCo
return this;
}

/**
* Genesis state hash from data besu controller builder.
*
* @param genesisStateHashCacheEnabled the is genesis state hash from data
* @return the besu controller builder
*/
public BesuControllerBuilder genesisStateHashCacheEnabled(
final Boolean genesisStateHashCacheEnabled) {
this.genesisStateHashCacheEnabled = genesisStateHashCacheEnabled;
return this;
}

/**
* Synchronizer configuration besu controller builder.
*
Expand Down Expand Up @@ -548,11 +563,30 @@ public BesuController build() {
prepForBuild();

final ProtocolSchedule protocolSchedule = createProtocolSchedule();
final GenesisState genesisState =
GenesisState.fromConfig(dataStorageConfiguration, genesisConfig, protocolSchedule);
final GenesisState genesisState;

final VariablesStorage variablesStorage = storageProvider.createVariablesStorage();

Optional<Hash> genesisStateHash = Optional.empty();
if (variablesStorage != null && this.genesisStateHashCacheEnabled) {
genesisStateHash = variablesStorage.getGenesisStateHash();
}

if (genesisStateHash.isPresent()) {
genesisState =
GenesisState.fromConfig(genesisStateHash.get(), genesisConfig, protocolSchedule);
} else {
genesisState =
GenesisState.fromConfig(dataStorageConfiguration, genesisConfig, protocolSchedule);
if (variablesStorage != null) {
VariablesStorage.Updater updater = variablesStorage.updater();
if (updater != null) {
updater.setGenesisStateHash(genesisState.getBlock().getHeader().getStateRoot());
updater.commit();
}
}
}

final WorldStateStorageCoordinator worldStateStorageCoordinator =
storageProvider.createWorldStateStorageCoordinator(dataStorageConfiguration);

Expand Down
17 changes: 17 additions & 0 deletions besu/src/test/java/org/hyperledger/besu/cli/BesuCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2375,4 +2375,21 @@ public void cacheLastBlocksOptionShouldWork() {
assertThat(commandOutput.toString(UTF_8)).isEmpty();
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}

@Test
public void genesisStateHashCacheEnabledShouldWork() throws IOException {
final Path genesisFile = createFakeGenesisFile(GENESIS_VALID_JSON);
final ArgumentCaptor<EthNetworkConfig> networkArg =
ArgumentCaptor.forClass(EthNetworkConfig.class);

parseCommand(
"--genesis-file", genesisFile.toString(), "--genesis-state-hash-cache-enabled=true");

verify(mockControllerBuilderFactory).fromEthNetworkConfig(networkArg.capture(), any(), any());
verify(mockControllerBuilder).build();
verify(mockControllerBuilder).genesisStateHashCacheEnabled(eq(true));

assertThat(commandOutput.toString(UTF_8)).isEmpty();
assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ public void initMocks() throws Exception {
when(mockControllerBuilder.besuComponent(any(BesuComponent.class)))
.thenReturn(mockControllerBuilder);
when(mockControllerBuilder.cacheLastBlocks(any())).thenReturn(mockControllerBuilder);
when(mockControllerBuilder.genesisStateHashCacheEnabled(any()))
.thenReturn(mockControllerBuilder);

// doReturn used because of generic BesuController
doReturn(mockController).when(mockControllerBuilder).build();
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 @@ -55,6 +55,7 @@ discovery-dns-url="enrtree://AM5FCQLWIZX2QFPNJAP7VUERCCRNGRHWZG3YYHIUV7BVDQ5FDPR
# chain
network="MAINNET"
genesis-file="~/genesis.json"
genesis-state-hash-cache-enabled=false
sync-mode="fast"
fast-sync-min-peers=5
network-id=303
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,24 @@ public static GenesisState fromConfig(
return new GenesisState(block, genesisAccounts);
}

/**
* Construct a {@link GenesisState} from a JSON object.
*
* @param genesisStateHash The hash of the genesis state.
* @param config A {@link GenesisConfigFile} describing the genesis block.
* @param protocolSchedule A protocol Schedule associated with
* @return A new {@link GenesisState}.
*/
public static GenesisState fromConfig(
final Hash genesisStateHash,
final GenesisConfigFile config,
final ProtocolSchedule protocolSchedule) {
final List<GenesisAccount> genesisAccounts = parseAllocations(config).toList();
final Block block =
new Block(buildHeader(config, genesisStateHash, protocolSchedule), buildBody(config));
return new GenesisState(block, genesisAccounts);
}

private static BlockBody buildBody(final GenesisConfigFile config) {
final Optional<List<Withdrawal>> withdrawals =
isShanghaiAtGenesis(config) ? Optional.of(emptyList()) : Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ enum Keys {
FORK_HEADS("forkHeads"),
FINALIZED_BLOCK_HASH("finalizedBlockHash"),
SAFE_BLOCK_HASH("safeBlockHash"),
SEQ_NO_STORE("local-enr-seqno");
SEQ_NO_STORE("local-enr-seqno"),
GENESIS_STATE_HASH("genesisStateHash");

private final String key;
private final byte[] byteArray;
Expand Down Expand Up @@ -65,6 +66,8 @@ public String toString() {

Optional<Bytes> getLocalEnrSeqno();

Optional<Hash> getGenesisStateHash();

Updater updater();

interface Updater {
Expand All @@ -79,6 +82,8 @@ interface Updater {

void setLocalEnrSeqno(Bytes nodeRecord);

void setGenesisStateHash(Hash genesisStateHash);

void removeAll();

void commit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public Optional<Bytes> getLocalEnrSeqno() {
return getVariable(SEQ_NO_STORE).map(Bytes::wrap);
}

@Override
public Optional<Hash> getGenesisStateHash() {
return getVariable(Keys.GENESIS_STATE_HASH).map(this::bytesToHash);
}

@Override
public Updater updater() {
return new Updater(variables.startTransaction());
Expand Down Expand Up @@ -115,6 +120,11 @@ public void setLocalEnrSeqno(final Bytes nodeRecord) {
setVariable(SEQ_NO_STORE, nodeRecord);
}

@Override
public void setGenesisStateHash(final Hash genesisStateHash) {
setVariable(Keys.GENESIS_STATE_HASH, genesisStateHash);
}

@Override
public void removeAll() {
removeVariable(CHAIN_HEAD_HASH);
Expand Down

0 comments on commit 8289f34

Please sign in to comment.