Skip to content

Commit

Permalink
doc: comments
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkAfCod committed Apr 25, 2024
1 parent c49ee4a commit c3cbea0
Show file tree
Hide file tree
Showing 19 changed files with 220 additions and 309 deletions.
10 changes: 8 additions & 2 deletions hildr-node/src/main/java/io/optimism/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@
* @param l2RpcUrl L2 chain rpc url.
* @param l2EngineUrl L2 engine API url.
* @param jwtSecret L2 engine API jwt secret.
* @param chainConfig The chain config.
* @param checkpointSyncUrl The checkpoint sync url.
* @param rpcPort The rpc port.
* @param devnet The flag of devnet.
* @param checkpointSyncUrl The checkpoint sync url.
* @param syncMode The sync mode
* @param chainConfig The chain config.
* @author grapebaba
* @since 0.1.0
*/
Expand Down Expand Up @@ -143,6 +144,7 @@ private static MapConfigSource getMapConfigSource() {
* @param jwtSecret L2 engine API jwt secret.
* @param checkpointSyncUrl The checkpoint sync url.
* @param rpcPort The rpc port.
* @param syncMode The sync mode.
* @param devnet The devnet flag.
*/
public record CliConfig(
Expand Down Expand Up @@ -670,6 +672,10 @@ public enum SyncMode {
*/
ExecutionLayer;

/**
* is execution layer sync mode
* @return true if execution layer sync mode, otherwise false.
*/
public boolean isEl() {
return this == ExecutionLayer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,11 @@ private BatchStatus spanBatchStatus(final Batch batchWrapper) {

// check batch timestamp
if (spanEndTimestamp.compareTo(nextTimestamp) < 0) {
LOGGER.warn("past batch: nextTimestamp = l2SafeHead({}) + blockTime({}), spanEndTimestamp({})",
l2SafeHead.timestamp(), this.config.chainConfig().blockTime(), spanEndTimestamp);
LOGGER.warn(
"past batch: nextTimestamp = l2SafeHead({}) + blockTime({}), spanEndTimestamp({})",
l2SafeHead.timestamp(),
this.config.chainConfig().blockTime(),
spanEndTimestamp);
return BatchStatus.Drop;
}
if (spanStartTimestamp.compareTo(nextTimestamp) > 0) {
Expand Down
27 changes: 17 additions & 10 deletions hildr-node/src/main/java/io/optimism/driver/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public class Driver<E extends Engine> extends AbstractExecutionThreadService {
*
* @param engineDriver the engine driver
* @param pipeline the pipeline
* @param l2Fetcher the L2 HeadInfo fetcher
* @param state the state
* @param chainWatcher the chain watcher
* @param unsafeBlockQueue the unsafe block queue
Expand Down Expand Up @@ -227,8 +228,8 @@ public static Driver<EngineApi> from(Config config, CountDownLatch latch)
l2Refs = io.optimism.derive.State.initL2Refs(finalizedHead.number(), config.chainConfig(), l2Provider);
}
var l2Fetcher = Driver.l2Fetcher(l2Provider);
AtomicReference<io.optimism.derive.State> state = new AtomicReference<>(io.optimism.derive.State.create(
l2Refs, l2Fetcher, finalizedHead, finalizedEpoch, config));
AtomicReference<io.optimism.derive.State> state = new AtomicReference<>(
io.optimism.derive.State.create(l2Refs, l2Fetcher, finalizedHead, finalizedEpoch, config));

EngineDriver<EngineApi> engineDriver = new EngineDriver<>(finalizedHead, finalizedEpoch, l2Provider, config);

Expand All @@ -242,7 +243,16 @@ public static Driver<EngineApi> from(Config config, CountDownLatch latch)

l2Provider.shutdown();
return new Driver<>(
engineDriver, pipeline, l2Fetcher, state, watcher, unsafeBlockQueue, rpcServer, latch, config, opStackNetwork);
engineDriver,
pipeline,
l2Fetcher,
state,
watcher,
unsafeBlockQueue,
rpcServer,
latch,
config,
opStackNetwork);
}

/**
Expand Down Expand Up @@ -381,7 +391,6 @@ protected void shutDown() {
this.engineDriver.stop();
LOGGER.info("engineDriver shut down.");
this.rpcServer.stop();
Web3jProvider.stop();
LOGGER.info("driver stopped.");
if (this.opStackNetwork != null && this.isP2PNetworkStarted.compareAndExchange(true, false)) {
this.opStackNetwork.stop();
Expand Down Expand Up @@ -668,15 +677,15 @@ private void fetchAndUpdateFinalizedHead() {
if (this.engineDriver.getFinalizedHead().number().compareTo(BigInteger.ZERO) == 0) {
blockParameter = FINALIZED;
} else {
blockParameter = DefaultBlockParameter.valueOf(this.engineDriver.getFinalizedHead().number());
blockParameter = DefaultBlockParameter.valueOf(
this.engineDriver.getFinalizedHead().number());
}
Tuple2<BlockInfo, Epoch> finalizedHead = l2Fetcher.apply(blockParameter, true);
this.engineDriver.updateFinalized(finalizedHead.component1(), finalizedHead.component2());
}



private static BiFunction<DefaultBlockParameter, Boolean, Tuple2<BlockInfo, Epoch>> l2Fetcher(final Web3j l2Provider) {
private static BiFunction<DefaultBlockParameter, Boolean, Tuple2<BlockInfo, Epoch>> l2Fetcher(
final Web3j l2Provider) {
return (blockParameter, returnFull) -> {
try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
StructuredTaskScope.Subtask<EthBlock> blockTask = scope.fork(TracerTaskWrapper.wrap(() -> l2Provider
Expand All @@ -698,8 +707,6 @@ private static BiFunction<DefaultBlockParameter, Boolean, Tuple2<BlockInfo, Epoc
};
}



/**
* The type Unfinalized block.
*
Expand Down
12 changes: 7 additions & 5 deletions hildr-node/src/main/java/io/optimism/driver/EngineDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ public Epoch getFinalizedEpoch() {
return finalizedEpoch;
}

/**
* check is engine syncing
* @return true if engine is syncing
*/
public boolean isEngineSyncing() {
return syncStatus.isEngineSyncing();
}
Expand Down Expand Up @@ -324,7 +328,7 @@ private void updateForkchoice() throws InterruptedException, ExecutionException
var forkChoiceUpdate = forkChoiceUpdateFuture.get();
if (forkChoiceUpdate.hasError()) {
throw new ForkchoiceUpdateException(
"could not accept new forkchoice: %s".formatted(forkChoiceUpdate.getError()));
"could not accept new forkchoice: %s".formatted(forkChoiceUpdate.getError()));
}
var forkChoiceUpdateStatus = forkChoiceUpdate.getForkChoiceUpdate().payloadStatus();
var updateStatus = forkChoiceUpdateStatus.getStatus();
Expand All @@ -335,14 +339,12 @@ private void updateForkchoice() throws InterruptedException, ExecutionException
// Allow SYNCING if engine P2P sync is enabled
if (updateStatus == Status.INVALID || forkChoiceUpdateStatus.getStatus() == Status.INVALID_BLOCK_HASH) {
throw new ForkchoiceUpdateException(String.format(
"could not accept new forkchoice: %s",
forkChoiceUpdateStatus.getValidationError()));
"could not accept new forkchoice: %s", forkChoiceUpdateStatus.getValidationError()));
}
} else {
if (updateStatus != Status.VALID) {
throw new ForkchoiceUpdateException(String.format(
"could not accept new forkchoice: %s",
forkChoiceUpdateStatus.getValidationError()));
"could not accept new forkchoice: %s", forkChoiceUpdateStatus.getValidationError()));
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions hildr-node/src/main/java/io/optimism/engine/EngineApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.function.Function;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.web3j.protocol.core.Request;
Expand Down Expand Up @@ -118,8 +117,7 @@ public EngineApi fromEnv(Config config) {
public EngineApi(final Config config, final String baseUrl, final String secretStr) {
this.config = config;
this.key = Keys.hmacShaKeyFor(Numeric.hexStringToByteArray(secretStr));
var filter = (Function<String, Boolean>) body -> body.contains("method\":\"engine_");
this.web3jService = (HttpService) Web3jProvider.create(baseUrl, filter).component2();
this.web3jService = (HttpService) Web3jProvider.create(baseUrl).component2();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public record ExecutionPayload(
BigInteger excessBlobGas,
String parentBeaconBlockRoot) {

/**
* Converts the ExecutionPayload to an L2BlockRef.
* @param config the chain config
* @return the L2BlockRef
*/
public L2BlockRef toL2BlockInfo(Config.ChainConfig config) {
final Epoch l1GenesisEpoch = config.l1StartEpoch();
final BlockInfo l2GenesisInfo = config.l2Genesis();
Expand Down
22 changes: 17 additions & 5 deletions hildr-node/src/main/java/io/optimism/l1/BeaconBlobFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.concurrent.StructuredTaskScope;
import java.util.stream.Collectors;
import okhttp3.Call;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
Expand Down Expand Up @@ -75,6 +76,12 @@ public BeaconBlobFetcher(String beaconUrl) {
* @param beaconArchiverUrl L1 beacon archiver client url
*/
public BeaconBlobFetcher(String beaconUrl, String beaconArchiverUrl) {
if (beaconUrl.endsWith("/")) {
beaconUrl = beaconUrl.replaceAll("/+$", "");
}
if (beaconArchiverUrl.endsWith("/")) {
beaconArchiverUrl = beaconArchiverUrl.replaceAll("/+$", "");
}
this.genesisMethod = GENESIS_METHOD_FORMAT.formatted(beaconUrl);
this.specMethod = SPEC_METHOD_FORMAT.formatted(beaconUrl);
this.sidecarsMethod = SIDECARS_METHOD_PREFIX_FORMAT.formatted(beaconUrl);
Expand Down Expand Up @@ -138,16 +145,21 @@ public List<BlobSidecar> getBlobSidecards(String blockId, final List<BigInteger>
return res.getData();
}
if (this.archiverSidecarsMethod != null) {
LOGGER.debug(
"blob sidecars may be pruned, try blob archiver sidecars method: blockId = {}, indices = {}",
LOGGER.info(
"blob sidecars may be pruned, try blob archiver sidecars method: blockId = {}, indices = {}, url = {}",
blockId,
indices);
indices,
"%s/%s".formatted(this.archiverSidecarsMethod, postfix));
var archiverRes = getBlobSidecars("%s/%s".formatted(this.archiverSidecarsMethod, postfix));
LOGGER.info(
"archiverUrl: {}; archiverRes return data: {}",
this.archiverSidecarsMethod,
archiverRes.getData() != null && !archiverRes.getData().isEmpty());
if (archiverRes.getData() != null && !archiverRes.getData().isEmpty()) {
return archiverRes.getData();
}
} else {
LOGGER.debug(
LOGGER.info(
"blob archiver sidecars method is empty, skip retry: block Id = {}, indices = {}",
blockId,
indices);
Expand All @@ -157,7 +169,7 @@ public List<BlobSidecar> getBlobSidecards(String blockId, final List<BigInteger>
}

private BeaconApiResponse<List<BlobSidecar>> getBlobSidecars(String url) {
var req = new Request.Builder().get().url(url).build();
var req = new Request.Builder().get().url(HttpUrl.parse(url)).build();
return this.send(req, new TypeReference<BeaconApiResponse<List<BlobSidecar>>>() {});
}

Expand Down
5 changes: 5 additions & 0 deletions hildr-node/src/main/java/io/optimism/runner/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ public void checkpointSync() throws ExecutionException, InterruptedException {
waitDriverRunning();
}

/**
* snap sync.
*
* @throws InterruptedException the interrupted exception
*/
public void executionLayerSync() throws InterruptedException {
LOGGER.info("execution layer sync");
waitDriverRunning();
Expand Down
29 changes: 0 additions & 29 deletions hildr-node/src/test/java/io/optimism/engine/EngineApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import io.optimism.engine.ForkChoiceUpdate.ForkChoiceUpdateRes;
import io.optimism.engine.ForkChoiceUpdate.ForkchoiceState;
import io.optimism.type.Epoch;
import io.optimism.utilities.rpc.Web3jProvider;
import java.io.IOException;
import java.math.BigInteger;
import java.security.Key;
Expand Down Expand Up @@ -178,32 +177,4 @@ void testJwts() {
- jwt.getBody().getIssuedAt().toInstant().getEpochSecond(),
60L);
}

@Test
void testLogging() throws IOException, InterruptedException {
TestConstants.createConfig();
if (!TestConstants.isConfiguredApiKeyEnv) {
return;
}
EngineApi engineApi = new EngineApi(
TestConstants.createConfig(),
"http://127.0.0.1:8552",
"bf549f5188556ce0951048ef467ec93067bc4ea21acebe46ef675cd4e8e015ff");
ForkchoiceState forkchoiceState = new ForkchoiceState("123", "123", "!@3");
PayloadAttributes payloadAttributes = new PayloadAttributes(
new BigInteger("123123"),
"123123",
"123",
List.of(""),
null,
true,
new BigInteger("1"),
new Epoch(new BigInteger("12"), "123", new BigInteger("1233145"), BigInteger.ZERO),
new BigInteger("1334"),
new BigInteger("321"),
null);
engineApi.forkchoiceUpdated(forkchoiceState, payloadAttributes);
Web3jProvider.stop();
Thread.sleep(3000);
}
}
Loading

0 comments on commit c3cbea0

Please sign in to comment.