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
2 changes: 1 addition & 1 deletion src/main/java/io/optimism/driver/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import io.optimism.network.OpStackNetwork;
import io.optimism.rpc.RpcMethod;
import io.optimism.rpc.RpcServer;
import io.optimism.rpc.Web3jProvider;
import io.optimism.rpc.internal.result.SyncStatusResult;
import io.optimism.telemetry.InnerMetrics;
import io.optimism.telemetry.TracerTaskWrapper;
Expand All @@ -33,6 +32,7 @@
import io.optimism.types.RollupConfigResult;
import io.optimism.types.SystemConfig;
import io.optimism.utilities.encoding.TxDecoder;
import io.optimism.utilities.web3j.Web3jProvider;
import java.math.BigInteger;
import java.time.Duration;
import java.util.HashMap;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/optimism/engine/EngineApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
import io.optimism.config.Config;
import io.optimism.rpc.Web3jProvider;
import io.optimism.types.ExecutionPayload;
import io.optimism.types.ExecutionPayload.PayloadAttributes;
import io.optimism.types.ForkChoiceUpdate.ForkchoiceState;
import io.optimism.utilities.web3j.Web3jProvider;
import java.io.IOException;
import java.math.BigInteger;
import java.security.Key;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/optimism/l1/InnerWatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import io.optimism.exceptions.BlockNotIncludedException;
import io.optimism.exceptions.DepositsNotFoundException;
import io.optimism.exceptions.HildrServiceExecutionException;
import io.optimism.rpc.Web3jProvider;
import io.optimism.telemetry.TracerTaskWrapper;
import io.optimism.types.BeaconSignedBlockHeader;
import io.optimism.types.BlobSidecar;
Expand All @@ -22,6 +21,7 @@
import io.optimism.types.SystemConfigUpdate;
import io.optimism.types.enums.Logging;
import io.optimism.utilities.blob.BlobCodec;
import io.optimism.utilities.web3j.Web3jProvider;
import io.reactivex.disposables.Disposable;
import java.math.BigInteger;
import java.time.Duration;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/optimism/rpc/methods/OutputAtBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import io.optimism.exceptions.HildrServiceExecutionException;
import io.optimism.rpc.RpcMethod;
import io.optimism.rpc.Web3jProvider;
import io.optimism.rpc.internal.JsonRpcRequestContext;
import io.optimism.rpc.internal.response.JsonRpcResponse;
import io.optimism.rpc.internal.response.JsonRpcSuccessResponse;
import io.optimism.rpc.internal.result.EthGetProof;
import io.optimism.rpc.internal.result.OutputRootResult;
import io.optimism.telemetry.TracerTaskWrapper;
import io.optimism.utilities.web3j.Web3jProvider;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/optimism/runner/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
import io.optimism.exceptions.SyncUrlMissingException;
import io.optimism.exceptions.TransactionNotFoundException;
import io.optimism.exceptions.TrustedPeerAddedException;
import io.optimism.rpc.Web3jProvider;
import io.optimism.rpc.response.OpEthBlock;
import io.optimism.telemetry.TracerTaskWrapper;
import io.optimism.types.ExecutionPayload;
import io.optimism.types.ExecutionPayload.Status;
import io.optimism.types.ForkChoiceUpdate.ForkchoiceState;
import io.optimism.utilities.web3j.Web3jProvider;
import java.math.BigInteger;
import java.time.Duration;
import java.util.Arrays;
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/io/optimism/utilities/LruCacheProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.optimism.utilities;

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

public class LruCacheProvider {

public static final long DEFAULT_CACHE_SIZE = 300L;

public static <K, V> Cache<K, V> create(long size) {
return CacheBuilder.newBuilder().maximumSize(size).build();
}

public static <K, V> Cache<K, V> create() {
return CacheBuilder.newBuilder().maximumSize(DEFAULT_CACHE_SIZE).build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.optimism.rpc;
package io.optimism.utilities.web3j;

import ch.qos.logback.classic.Level;
import io.optimism.rpc.HttpClientProvider;
import io.optimism.rpc.RetryRateLimitInterceptor;
import java.net.ConnectException;
import java.util.function.Consumer;
import okhttp3.OkHttpClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.optimism.rpc;
package io.optimism.utilities.web3j;

import io.optimism.exceptions.Web3jCallException;
import io.optimism.rpc.response.OpEthBlock;
import io.optimism.telemetry.TracerTaskWrapper;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.StructuredTaskScope;
Expand All @@ -15,10 +17,13 @@
import org.web3j.crypto.RawTransaction;
import org.web3j.crypto.TransactionEncoder;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.Web3jService;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.core.methods.request.Transaction;
import org.web3j.protocol.core.methods.response.EthBlock;
import org.web3j.protocol.core.methods.response.EthGetBlockReceipts;
import org.web3j.protocol.core.methods.response.EthGetTransactionReceipt;
import org.web3j.protocol.core.methods.response.EthSendTransaction;
import org.web3j.utils.Numeric;
Expand Down Expand Up @@ -74,27 +79,82 @@ public static EthGetTransactionReceipt getTxReceipt(final Web3j client, final St
}
}

/**
* Get the transaction receipts of the given block num.
*
* @param client the web3j client
* @param blockNum the block number
* @return the transaction receipts
*/
public static EthGetBlockReceipts getBlockReceipts(final Web3j client, final DefaultBlockParameter blockNum) {
try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
var receiptsFuture =
scope.fork(() -> client.ethGetBlockReceipts(blockNum).send());
scope.join();
scope.throwIfFailed();
return receiptsFuture.get();
} catch (InterruptedException | ExecutionException e) {
Thread.currentThread().interrupt();
throw new Web3jCallException("failed to get TxReceipt", e);
}
}

/**
* Poll the block by the given parameter.
* @param client the web3j client
* @param parameter the block parameter
* @param returnFullTransactionObjects whether to return full transaction objects
* @return the block
*/
public static EthBlock pollBlock(
public static EthBlock pollBlockByNum(
final Web3j client, final DefaultBlockParameter parameter, final boolean returnFullTransactionObjects) {
try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
var receiptFuture = scope.fork(() -> client.ethGetBlockByNumber(parameter, returnFullTransactionObjects)
var blockFuture = scope.fork(() -> client.ethGetBlockByNumber(parameter, returnFullTransactionObjects)
.send());
scope.join();
scope.throwIfFailed();
return receiptFuture.get();
return blockFuture.get();
} catch (InterruptedException | ExecutionException e) {
Thread.currentThread().interrupt();
throw new Web3jCallException("failed to get block by number", e);
}
}

public static EthBlock pollBlockByHash(
final Web3j client, final String blockHash, final boolean returnFullTransactionObjects) {
try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
var blockFuture = scope.fork(() -> client.ethGetBlockByHash(blockHash, returnFullTransactionObjects)
.send());
scope.join();
scope.throwIfFailed();
return blockFuture.get();
} catch (InterruptedException | ExecutionException e) {
Thread.currentThread().interrupt();
throw new Web3jCallException("failed to get block by number", e);
}
}

public static OpEthBlock pollOpBlockByNum(
final Web3jService client,
final DefaultBlockParameter parameter,
final boolean returnFullTransactionObjects) {
OpEthBlock block;
try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
StructuredTaskScope.Subtask<OpEthBlock> blockFuture = scope.fork(TracerTaskWrapper.wrap(() -> new Request<>(
"eth_getBlockByNumber",
Arrays.asList(parameter.getValue(), returnFullTransactionObjects),
client,
OpEthBlock.class)
.send()));
scope.join();
scope.throwIfFailed();
return blockFuture.get();
} catch (InterruptedException | ExecutionException e) {
Thread.currentThread().interrupt();
throw new Web3jCallException("failed to get optimism block by number", e);
}
}

/**
* Execute the contract.
* @param client the web3j client
Expand Down
Loading
Loading