Skip to content
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

feat: support snap sync mode #160

Merged
merged 15 commits into from
Apr 30, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.optimism.type.L1BlockInfo;
import io.optimism.utilities.derive.stages.Frame;
import io.optimism.utilities.derive.stages.SingularBatch;
import io.optimism.utilities.encoding.TxEncoder;
import java.io.IOException;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -304,7 +305,7 @@ private Tuple2<L1BlockInfo, SingularBatch> blockToBatch(EthBlock.Block block) {
if (DEPOSIT_TX_TYPE.equalsIgnoreCase(txObj.getType())) {
continue;
}
txDataList.add(txObj.getInput());
txDataList.add(Numeric.toHexString(TxEncoder.encode(txObj)));
}
return new Tuple2(
l1Info,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.optimism.batcher.exception.Web3jCallException;
import io.optimism.batcher.telemetry.BatcherMetrics;
import io.optimism.type.BlockId;
import io.optimism.type.Epoch;
import io.optimism.type.Genesis;
import io.optimism.type.L1BlockInfo;
import io.optimism.type.L1BlockRef;
Expand Down Expand Up @@ -148,7 +149,7 @@ Tuple2<BlockId, BlockId> calculateL2BlockRangeToStore() {
}

L2BlockRef l2BlockToBlockRef(final EthBlock.Block block, Genesis genesis) {
BlockId l1Origin;
Epoch l1Origin;
BigInteger sequenceNumber;
if (block.getNumber().equals(genesis.l2().number())) {
if (!block.getHash().equals(genesis.l2().hash())) {
Expand All @@ -172,7 +173,7 @@ L2BlockRef l2BlockToBlockRef(final EthBlock.Block block, Genesis genesis) {
}
final byte[] input = Numeric.hexStringToByteArray(tx.getInput());
L1BlockInfo info = L1BlockInfo.from(input);
l1Origin = info.toId();
l1Origin = info.toEpoch();
sequenceNumber = info.sequenceNumber();
}
return new L2BlockRef(
Expand Down
1 change: 1 addition & 0 deletions hildr-node/src/main/java/io/optimism/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ private Config.CliConfig from(Cli cli) {
StringUtils.trim(Cli.this.getJwtSecret()),
cli.checkpointSyncUrl,
cli.rpcPort,
cli.syncMode,
cli.devnet);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.optimism.common;

import io.optimism.type.Epoch;
import io.optimism.type.L1BlockInfo;
import java.math.BigInteger;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -33,6 +34,14 @@ public record AttributesDepositedCall(
BigInteger blobBaseFeeScalar,
BigInteger blobBaseFee) {

/**
* Create Epoch from attributes deposited call.
* @return the epoch
*/
public Epoch toEpoch() {
return new Epoch(number, hash, timestamp, sequenceNumber);
}

/**
* Create AttributesDepositedCall from attributes deposited call.
*
Expand Down
4 changes: 4 additions & 0 deletions hildr-node/src/main/java/io/optimism/common/BlockInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.math.BigInteger;
import java.util.Objects;
import org.web3j.protocol.core.methods.response.EthBlock.Block;
import org.web3j.utils.Numeric;

/**
* The type BlockInfo.
Expand All @@ -19,6 +20,9 @@
*/
public record BlockInfo(String hash, BigInteger number, String parentHash, BigInteger timestamp) {

public static final BlockInfo EMPTY = new BlockInfo(
Numeric.toHexString(new byte[32]), BigInteger.ZERO, Numeric.toHexString(new byte[32]), BigInteger.ZERO);

/**
* From block info.
*
Expand Down
28 changes: 24 additions & 4 deletions hildr-node/src/main/java/io/optimism/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.optimism.common.BlockInfo;
import io.optimism.common.Epoch;
import io.optimism.type.Epoch;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
Expand Down 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 All @@ -58,6 +59,7 @@ public record Config(
String checkpointSyncUrl,
Integer rpcPort,
Boolean devnet,
SyncMode syncMode,
ChainConfig chainConfig) {

/**
Expand Down Expand Up @@ -142,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 All @@ -154,6 +157,7 @@ public record CliConfig(
String jwtSecret,
String checkpointSyncUrl,
Integer rpcPort,
SyncMode syncMode,
Boolean devnet) {

/**
Expand Down Expand Up @@ -190,6 +194,9 @@ public Map<String, String> toConfigMap() {
if (rpcPort != null) {
map.put("config.rpcPort", rpcPort.toString());
}
if (syncMode != null) {
map.put("config.syncMode", syncMode.toString());
}
map.put("config.devnet", String.valueOf(devnet != null && devnet));
return map;
}
Expand Down Expand Up @@ -659,7 +666,19 @@ public enum SyncMode {
/**
* Checkpoint sync mode.
*/
Checkpoint;
Checkpoint,
/**
* Execution layer sync mode.
*/
ExecutionLayer;

/**
* is execution layer sync mode
* @return true if execution layer sync mode, otherwise false.
*/
public boolean isEl() {
return this == ExecutionLayer;
}

/**
* From sync mode.
Expand All @@ -673,6 +692,7 @@ public static SyncMode from(String value) {
case "challenge" -> Challenge;
case "full" -> Full;
case "checkpoint" -> Checkpoint;
case "execution-layer" -> ExecutionLayer;
default -> throw new RuntimeException("invalid sync mode");
};
}
Expand Down
12 changes: 6 additions & 6 deletions hildr-node/src/main/java/io/optimism/derive/State.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package io.optimism.derive;

import io.optimism.common.BlockInfo;
import io.optimism.common.Epoch;
import io.optimism.config.Config;
import io.optimism.driver.HeadInfo;
import io.optimism.driver.L1AttributesDepositedTxNotFoundException;
import io.optimism.l1.L1Info;
import io.optimism.type.Epoch;
import java.math.BigInteger;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.StructuredTaskScope;
import java.util.function.Function;
import java.util.function.BiFunction;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -36,7 +36,7 @@ public class State {

private final TreeMap<BigInteger, Tuple2<BlockInfo, Epoch>> l2Refs;

private final Function<BigInteger, Tuple2<BlockInfo, Epoch>> l2Fetcher;
private final BiFunction<DefaultBlockParameter, Boolean, Tuple2<BlockInfo, Epoch>> l2Fetcher;

private BlockInfo safeHead;

Expand All @@ -62,7 +62,7 @@ public State(
TreeMap<String, L1Info> l1Info,
TreeMap<BigInteger, String> l1Hashes,
TreeMap<BigInteger, Tuple2<BlockInfo, Epoch>> l2Refs,
Function<BigInteger, Tuple2<BlockInfo, Epoch>> l2Fetcher,
BiFunction<DefaultBlockParameter, Boolean, Tuple2<BlockInfo, Epoch>> l2Fetcher,
BlockInfo safeHead,
Epoch safeEpoch,
BigInteger currentEpochNum,
Expand All @@ -89,7 +89,7 @@ public State(
*/
public static State create(
TreeMap<BigInteger, Tuple2<BlockInfo, Epoch>> l2Refs,
Function<BigInteger, Tuple2<BlockInfo, Epoch>> l2Fetcher,
BiFunction<DefaultBlockParameter, Boolean, Tuple2<BlockInfo, Epoch>> l2Fetcher,
BlockInfo finalizedHead,
Epoch finalizedEpoch,
Config config) {
Expand Down Expand Up @@ -144,7 +144,7 @@ public Tuple2<BlockInfo, Epoch> l2Info(BigInteger timestamp) {
return cache;
}
LOGGER.warn("L2 refs cache not contains, will fetch from geth: blockNum = {}", blockNum);
var res = l2Fetcher.apply(blockNum);
var res = l2Fetcher.apply(DefaultBlockParameter.valueOf(blockNum), true);
this.l2Refs.put(res.component1().number(), res);
return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import io.optimism.common.BlockInfo;
import io.optimism.common.BlockNotIncludedException;
import io.optimism.common.Epoch;
import io.optimism.config.Config;
import io.optimism.config.Config.SystemAccounts;
import io.optimism.derive.EctoneUpgradeTransactions;
import io.optimism.derive.PurgeableIterator;
import io.optimism.derive.State;
import io.optimism.engine.ExecutionPayload.PayloadAttributes;
import io.optimism.l1.L1Info;
import io.optimism.type.Epoch;
import io.optimism.utilities.derive.stages.Batch;
import io.optimism.utilities.derive.stages.SingularBatch;
import io.optimism.utilities.gas.GasCalculator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import com.google.common.collect.Lists;
import io.optimism.common.BlockInfo;
import io.optimism.common.Epoch;
import io.optimism.config.Config;
import io.optimism.derive.PurgeableIterator;
import io.optimism.derive.State;
import io.optimism.derive.stages.Channels.Channel;
import io.optimism.l1.L1Info;
import io.optimism.type.Epoch;
import io.optimism.utilities.derive.stages.Batch;
import io.optimism.utilities.derive.stages.BatchType;
import io.optimism.utilities.derive.stages.IBatch;
Expand Down Expand Up @@ -243,6 +243,7 @@ private BatchStatus singularBatchStatus(final Batch batchWrapper) {
return BatchStatus.Future;
}
case -1 -> {
LOGGER.warn("invalid batch timestamp, excepted={}, actual={}", nextTimestamp, batch.getTimestamp());
return BatchStatus.Drop;
}
default -> {}
Expand Down Expand Up @@ -336,7 +337,11 @@ private BatchStatus spanBatchStatus(final Batch batchWrapper) {

// check batch timestamp
if (spanEndTimestamp.compareTo(nextTimestamp) < 0) {
LOGGER.warn("past batch");
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
Loading
Loading