Skip to content

Commit

Permalink
feat: add --use-cached-genesis-state-hash paramater
Browse files Browse the repository at this point in the history
Signed-off-by: lyfsn <dev.wangyu@proton.me>
  • Loading branch information
lyfsn committed Mar 19, 2024
1 parent cc20169 commit ef29f33
Show file tree
Hide file tree
Showing 7 changed files with 76 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 @@ -360,6 +360,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 = {"--use-cached-genesis-state-hash"},
description = "Use genesis state hash from data on startup if specified")
private final Boolean useCachedGenesisStateHash = false;

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

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

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

/** The Sync config. */
protected SynchronizerConfiguration syncConfig;
/** The Ethereum wire protocol configuration. */
Expand Down Expand Up @@ -224,6 +227,17 @@ public BesuControllerBuilder genesisConfigFile(final GenesisConfigFile genesisCo
return this;
}

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

/**
* Synchronizer configuration besu controller builder.
*
Expand Down Expand Up @@ -555,11 +569,28 @@ 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.useCachedGenesisStateHash) {
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();
updater.setGenesisStateHash(genesisState.getBlock().getHeader().getStateRoot());
updater.commit();
}
}

final WorldStateStorageCoordinator worldStateStorageCoordinator =
storageProvider.createWorldStateStorageCoordinator(dataStorageConfiguration);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ public void initMocks() throws Exception {
when(mockControllerBuilder.besuComponent(any(BesuComponent.class)))
.thenReturn(mockControllerBuilder);
when(mockControllerBuilder.cacheLastBlocks(any())).thenReturn(mockControllerBuilder);
when(mockControllerBuilder.useCachedGenesisStateHash(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"
use-cached-genesis-state-hash=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 @@ -127,6 +127,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 ef29f33

Please sign in to comment.