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

Refactor stages #211

Merged
merged 11 commits into from
Oct 21, 2024
33 changes: 31 additions & 2 deletions src/main/java/io/optimism/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ public record ChainConfig(
BigInteger ecotoneTime,
BigInteger fjordTime,
BigInteger graniteTime,
BigInteger holeceneTime,
BigInteger blockTime,
String l2Tol1MessagePasser) {

Expand Down Expand Up @@ -370,9 +371,31 @@ public boolean isGranite(BigInteger time) {
* @return true if the time is the granite activation block, otherwise false.
*/
public boolean isGraniteActivationBlock(BigInteger time) {
return isFjord(time)
return isGranite(time)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是bug吧,应该放到单独的pr里面提交,需要立即发布版本的

&& time.compareTo(blockTime) >= 0
&& time.subtract(blockTime).compareTo(fjordTime) < 0;
&& time.subtract(blockTime).compareTo(graniteTime) < 0;
}

/**
* Check if the time is the holocene activation block.
*
* @param time the block timestamp
* @return true if the time is the holocene activation block, otherwise false.
*/
public boolean isHolocene(BigInteger time) {
return graniteTime.compareTo(BigInteger.ZERO) >= 0 && time.compareTo(graniteTime) >= 0;
}

/**
* Check if the time is the holocene activation block.
*
* @param time the block timestamp
* @return true if the time is the holocene activation block, otherwise false.
*/
public boolean isHoloceneActivationBlock(BigInteger time) {
return isHolocene(time)
&& time.compareTo(blockTime) >= 0
&& time.subtract(blockTime).compareTo(holeceneTime) < 0;
}

/**
Expand Down Expand Up @@ -453,6 +476,7 @@ public static ChainConfig optimism() {
BigInteger.valueOf(1710374401L),
BigInteger.valueOf(1720627201L),
BigInteger.valueOf(1726070401L),
BigInteger.valueOf(-1L),
BigInteger.valueOf(2L),
"0x4200000000000000000000000000000000000016");
}
Expand Down Expand Up @@ -495,6 +519,7 @@ public static ChainConfig base() {
BigInteger.valueOf(1710374401L),
BigInteger.valueOf(1720627201L),
BigInteger.valueOf(1726070401L),
BigInteger.valueOf(-1L),
BigInteger.valueOf(2L),
"0x4200000000000000000000000000000000000016");
}
Expand Down Expand Up @@ -537,6 +562,7 @@ public static ChainConfig optimismSepolia() {
BigInteger.valueOf(1708534800L),
BigInteger.valueOf(1716998400L),
BigInteger.valueOf(1723478400L),
BigInteger.valueOf(-1L),
BigInteger.valueOf(2L),
"0x4200000000000000000000000000000000000016");
}
Expand Down Expand Up @@ -579,6 +605,7 @@ public static ChainConfig baseSepolia() {
BigInteger.valueOf(1708534800L),
BigInteger.valueOf(1716998400L),
BigInteger.valueOf(1723478400L),
BigInteger.valueOf(-1L),
BigInteger.valueOf(2L),
"0x4200000000000000000000000000000000000016");
}
Expand Down Expand Up @@ -636,6 +663,7 @@ public static ChainConfig fromExternal(ExternalChainConfig external) {
external.ecotoneTime == null ? BigInteger.valueOf(-1L) : external.ecotoneTime,
external.fjordTime == null ? BigInteger.valueOf(-1L) : external.fjordTime,
external.graniteTime == null ? BigInteger.valueOf(-1L) : external.graniteTime,
external.holoceneTime == null ? BigInteger.valueOf(-1L) : external.holoceneTime,
external.blockTime,
"0x4200000000000000000000000000000000000016");
}
Expand Down Expand Up @@ -898,6 +926,7 @@ public record ExternalChainConfig(
BigInteger ecotoneTime,
BigInteger fjordTime,
BigInteger graniteTime,
BigInteger holoceneTime,
String batchInboxAddress,
String depositContractAddress,
String l1SystemConfigAddress) {}
Expand Down
24 changes: 8 additions & 16 deletions src/main/java/io/optimism/derive/stages/Attributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -369,22 +369,14 @@ public static AttributesDeposited decode(String txInput) {
if (input.length != 260) {
throw new IllegalArgumentException("bedrock deposit tx input length is not 164 bytes");
}
int offset = 4;
BigInteger l1BlockNum = Numeric.toBigInt(Arrays.copyOfRange(input, offset, offset + 32));
offset += 32;
BigInteger l1BlockTime = Numeric.toBigInt(Arrays.copyOfRange(input, offset, offset + 32));
offset += 32;
BigInteger baseFee = Numeric.toBigInt(Arrays.copyOfRange(input, offset, offset + 32));
offset += 32;
String l1BlockHash = Numeric.toHexString(Arrays.copyOfRange(input, offset, offset + 32));
offset += 32;
BigInteger seqNum = Numeric.toBigInt(Arrays.copyOfRange(input, offset, offset + 32));
offset += 32;
String batcherHash = Numeric.toHexString(Arrays.copyOfRange(input, offset, offset + 32));
offset += 32;
BigInteger l1FeeOverhead = Numeric.toBigInt(Arrays.copyOfRange(input, offset, offset + 32));
offset += 32;
BigInteger l1FeeScalar = Numeric.toBigInt(Arrays.copyOfRange(input, offset, offset + 32));
BigInteger l1BlockNum = Numeric.toBigInt(Arrays.copyOfRange(input, 28, 36));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个改动之前的代码好啊,全是magic number

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bedrock的有效数据位很少,现在只读有效的数据位。
如果用offset计算,每次跳的位数不同,并且读下一个字段的数据长度也不一样,反而不好理解。

BigInteger l1BlockTime = Numeric.toBigInt(Arrays.copyOfRange(input, 60, 68));
BigInteger baseFee = Numeric.toBigInt(Arrays.copyOfRange(input, 92, 100));
String l1BlockHash = Numeric.toHexString(Arrays.copyOfRange(input, 100, 132));
BigInteger seqNum = Numeric.toBigInt(Arrays.copyOfRange(input, 156, 164));
String batcherHash = Numeric.toHexString(Arrays.copyOfRange(input, 176, 196));
BigInteger l1FeeOverhead = Numeric.toBigInt(Arrays.copyOfRange(input, 196, 228));
BigInteger l1FeeScalar = Numeric.toBigInt(Arrays.copyOfRange(input, 228, 260));
return new AttributesDeposited(
l1BlockNum,
l1BlockTime,
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/optimism/utilities/LruCacheProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.optimism.utilities;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

public class LruCacheProvider {

static <K, V> Cache<K, V> create() {
return CacheBuilder.newBuilder().maximumSize(1000L).build();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

magic number 1000L 看是不是要加个配置

}
}
10 changes: 10 additions & 0 deletions src/main/java/io/optimism/v2/derive/datasource/BlobProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.optimism.v2.derive.datasource;

import io.optimism.types.BlobSidecar;
import io.optimism.types.BlockInfo;
import java.util.List;

public interface BlobProvider {

BlobSidecar getBlobs(BlockInfo l1Info, List<String> blobHashes);
}
18 changes: 18 additions & 0 deletions src/main/java/io/optimism/v2/derive/datasource/ChainProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.optimism.v2.derive.datasource;

import io.optimism.v2.derive.types.BlockInfo;
import java.math.BigInteger;
import java.util.List;
import org.web3j.protocol.core.methods.response.EthBlock;
import org.web3j.protocol.core.methods.response.TransactionReceipt;

public interface ChainProvider {

EthBlock.Block headerByHash(String hash);

BlockInfo blockInfoByNumber(BigInteger num);

List<TransactionReceipt> receiptsByHash(String hash);

EthBlock.Block blockInfoNTxsByHash(String hash);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.optimism.v2.derive.datasource;

import io.optimism.types.BlockInfo;
import io.optimism.v2.derive.stages.DataIter;

public interface DataAvailabilityProvider {

DataIter openData(BlockInfo l1Ref, String batcherAddr);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.optimism.v2.derive.datasource;

import io.optimism.config.Config;
import io.optimism.rpc.response.OpEthBlock;
import io.optimism.types.L2BlockRef;
import io.optimism.types.SystemConfig;
import java.math.BigInteger;

public interface L2ChainProvider {

L2BlockRef l2BlockInfoByNumber(BigInteger num);

OpEthBlock blockByNum(BigInteger num);

SystemConfig systemConfigByNumber(BigInteger num, Config.ChainConfig chainConfig);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.optimism.v2.derive.datasource.impl;

import io.optimism.v2.derive.stages.FrameQueueProvider;
import io.optimism.v2.derive.stages.OriginAdvancer;
import io.optimism.v2.derive.stages.OriginProvider;
import io.optimism.v2.derive.stages.ResettableStage;
import io.optimism.v2.derive.types.BlockInfo;
import io.optimism.v2.derive.types.SystemConfig;

public class L1Retrieval implements FrameQueueProvider, OriginProvider, OriginAdvancer, ResettableStage {

@Override
public void advanceOrigin() {}

@Override
public BlockInfo origin() {
return null;
}

@Override
public void reset(BlockInfo base, SystemConfig config) {}

@Override
public byte[] next() {
return new byte[0];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package io.optimism.v2.derive.datasource.impl;

import io.optimism.config.Config;
import io.optimism.exceptions.ResetException;
import io.optimism.v2.derive.datasource.ChainProvider;
import io.optimism.v2.derive.exception.PipelineEofException;
import io.optimism.v2.derive.exception.PipelineProviderException;
import io.optimism.v2.derive.stages.L1RetrievalProvider;
import io.optimism.v2.derive.stages.OriginAdvancer;
import io.optimism.v2.derive.stages.OriginProvider;
import io.optimism.v2.derive.stages.ResettableStage;
import io.optimism.v2.derive.types.BlockInfo;
import io.optimism.v2.derive.types.SystemConfig;
import java.math.BigInteger;
import java.util.List;
import org.web3j.protocol.core.methods.response.TransactionReceipt;

public class L1Traversal implements L1RetrievalProvider, OriginProvider, OriginAdvancer, ResettableStage {

private final Config.ChainConfig rollupConfig;

private ChainProvider dataSource;

private BlockInfo block;

private boolean done;

private SystemConfig curSysConfig;

public L1Traversal(Config.ChainConfig rollupConfig, ChainProvider provider) {
this.rollupConfig = rollupConfig;
this.dataSource = provider;
}

@Override
public BlockInfo nextL1Block() {
return this.block;
}

@Override
public String batcherAddr() {
return this.curSysConfig.batcherAddr();
}

@Override
public void advanceOrigin() {
if (this.block == null) {
throw new PipelineEofException();
}

var block = this.block;
var nextL1Origin = this.dataSource.blockInfoByNumber(block.number().add(BigInteger.ONE));
if (nextL1Origin == null) {
throw new PipelineProviderException();
}
if (block.hash().equals(nextL1Origin.hash())) {
throw new ResetException("reorg detected");
}

List<TransactionReceipt> txReceipts = this.dataSource.receiptsByHash(nextL1Origin.hash());
updateWithReceipts(
txReceipts,
this.rollupConfig.systemConfigContract(),
this.rollupConfig.isEcotone(nextL1Origin.timestamp()));

this.block = nextL1Origin;
this.done = false;

this.rollupConfig.isHoloceneActivationBlock(nextL1Origin.timestamp());
}

private void updateWithReceipts(List<TransactionReceipt> txReceipts, String sysConfigAddr, boolean ecotone) {}

@Override
public BlockInfo origin() {
return this.block;
}

@Override
public void reset(BlockInfo base, SystemConfig config) {
this.block = base;
this.curSysConfig = config;
// metrics record stage reset for l1 traversal
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.optimism.v2.derive.datasource.impl;

import io.optimism.config.Config;
import io.optimism.rpc.response.OpEthBlock;
import io.optimism.types.L2BlockRef;
import io.optimism.types.SystemConfig;
import io.optimism.v2.derive.datasource.L2ChainProvider;
import java.math.BigInteger;

public class L2ChainFetcher implements L2ChainProvider {
@Override
public L2BlockRef l2BlockInfoByNumber(BigInteger num) {
return null;
}

@Override
public OpEthBlock blockByNum(BigInteger num) {
return null;
}

@Override
public SystemConfig systemConfigByNumber(BigInteger num, Config.ChainConfig chainConfig) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.optimism.v2.derive.exception;

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注释看起来没有原来规范,建议都用ide或者ai生成

* Exception thrown when there is an error in the pipeline.
*/
public class PipelineEofException extends RuntimeException {
/** Constructs a PipelineEofException. */
public PipelineEofException() {
super("pipeline EOF error");
}

/**
* Constructs a PipelineEofException with a custom message.
*
* @param message the custom error message
*/
public PipelineEofException(String message) {
super(message);
}

/**
* Constructs a new PipelineEofException with the specified detail message and cause.
*
* @param message the detail message
* @param cause the cause
*/
public PipelineEofException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.optimism.v2.derive.exception;

/**
* Exception thrown when there is an error in the pipeline provider.
*/
public class PipelineProviderException extends RuntimeException {

public PipelineProviderException() {
super("pipeline provider error");
}

/**
* Constructs a new PipelineProviderException with the specified detail message.
*
* @param message the detail message
*/
public PipelineProviderException(String message) {
super(message);
}

/**
* Constructs a new PipelineProviderException with the specified detail message and cause.
*
* @param message the detail message
* @param cause the cause
*/
public PipelineProviderException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.optimism.v2.derive.pipeline;

public class DerivationPipeline {

DerivationPipeline() {}
}
Loading
Loading