Skip to content

Commit

Permalink
feat: support snap sync
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkAfCod committed Apr 23, 2024
1 parent 6e4cf0c commit 0f69aa8
Show file tree
Hide file tree
Showing 25 changed files with 270 additions and 59 deletions.
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
18 changes: 16 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 @@ -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 @@ -58,6 +58,7 @@ public record Config(
String checkpointSyncUrl,
Integer rpcPort,
Boolean devnet,
SyncMode syncMode,
ChainConfig chainConfig) {

/**
Expand Down Expand Up @@ -154,6 +155,7 @@ public record CliConfig(
String jwtSecret,
String checkpointSyncUrl,
Integer rpcPort,
SyncMode syncMode,
Boolean devnet) {

/**
Expand Down Expand Up @@ -190,6 +192,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 +664,15 @@ public enum SyncMode {
/**
* Checkpoint sync mode.
*/
Checkpoint;
Checkpoint,
/**
* Execution layer sync mode.
*/
ExecutionLayer;

public boolean isEl() {
return this == ExecutionLayer;
}

/**
* From sync mode.
Expand All @@ -673,6 +686,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
2 changes: 1 addition & 1 deletion hildr-node/src/main/java/io/optimism/derive/State.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
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;
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
27 changes: 14 additions & 13 deletions hildr-node/src/main/java/io/optimism/driver/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.AbstractExecutionThreadService;
import io.optimism.common.BlockInfo;
import io.optimism.common.Epoch;
import io.optimism.common.HildrServiceExecutionException;
import io.optimism.config.Config;
import io.optimism.derive.Pipeline;
Expand All @@ -25,6 +24,7 @@
import io.optimism.telemetry.InnerMetrics;
import io.optimism.type.BlockId;
import io.optimism.type.DepositTransaction;
import io.optimism.type.Epoch;
import io.optimism.type.Genesis;
import io.optimism.type.L1BlockInfo;
import io.optimism.type.L2BlockRef;
Expand Down Expand Up @@ -273,9 +273,7 @@ public RollupConfigResult getRollupConfig() {
curSysConfig.l1FeeScalar(),
curSysConfig.gasLimit());
var latestGenesis = new Genesis(
new BlockId(
chainConfig.l1StartEpoch().hash(),
chainConfig.l1StartEpoch().number()),
chainConfig.l1StartEpoch(),
new BlockId(
chainConfig.l2Genesis().hash(), chainConfig.l2Genesis().number()),
chainConfig.l2Genesis().timestamp(),
Expand Down Expand Up @@ -440,14 +438,16 @@ private void advanceSafeHead() throws ExecutionException, InterruptedException {
if (l1InclusionBlock == null) {
throw new InvalidAttributesException("attributes without inclusion block");
}

if (Driver.this.engineDriver.isEngineSyncing()) {
LOGGER.info("engine is syncing, skipping payload");
continue;
}
BigInteger seqNumber = payloadAttributes.seqNumber();
if (seqNumber == null) {
throw new InvalidAttributesException("attributes without seq number");
}

Driver.this.engineDriver.handleAttributes(payloadAttributes);

LOGGER.info(
"safe head updated: {} {}",
Driver.this.engineDriver.getSafeHead().number(),
Expand Down Expand Up @@ -577,7 +577,9 @@ private boolean synced() {
}

private void tryStartNetwork() {
if (this.synced() && this.opStackNetwork != null && !this.isP2PNetworkStarted.compareAndExchange(false, true)) {
if ((this.synced() || this.config.syncMode().isEl())
&& this.opStackNetwork != null
&& !this.isP2PNetworkStarted.compareAndExchange(false, true)) {
this.opStackNetwork.start();
}
}
Expand All @@ -603,7 +605,7 @@ private L2BlockRef unsafeL2SyncTarget() {
* @return L2BlockRef instance
*/
public static L2BlockRef payloadToRef(ExecutionPayload payload, Config.ChainConfig genesis) {
BlockId l1Origin;
Epoch l1Origin;
BigInteger sequenceNumber;
if (payload.blockNumber().compareTo(genesis.l2Genesis().number()) == 0) {
if (!payload.blockHash().equals(genesis.l2Genesis().hash())) {
Expand All @@ -613,18 +615,17 @@ public static L2BlockRef payloadToRef(ExecutionPayload payload, Config.ChainConf
payload.blockHash(),
genesis.l2Genesis().hash()));
}
l1Origin = new BlockId(
genesis.l1StartEpoch().hash(), genesis.l1StartEpoch().number());
l1Origin = genesis.l1StartEpoch();
sequenceNumber = BigInteger.ZERO;
} else {
if (payload.transactions().size() == 0) {
if (payload.transactions().isEmpty()) {
throw new RuntimeException(
String.format("l2 block is missing L1 info deposit tx, block hash: %s", payload.blockHash()));
}
DepositTransaction depositTx =
TxDecoder.decodeToDeposit(payload.transactions().get(0));
L1BlockInfo info = L1BlockInfo.from(Numeric.hexStringToByteArray(depositTx.getData()));
l1Origin = new BlockId(info.blockHash(), info.number());
l1Origin = info.toEpoch();
sequenceNumber = info.sequenceNumber();
}

Expand All @@ -633,7 +634,7 @@ public static L2BlockRef payloadToRef(ExecutionPayload payload, Config.ChainConf
payload.blockNumber(),
payload.parentHash(),
payload.timestamp(),
new BlockId(l1Origin.hash(), l1Origin.number()),
l1Origin,
sequenceNumber);
}

Expand Down
Loading

0 comments on commit 0f69aa8

Please sign in to comment.