Skip to content

Commit

Permalink
refactor: add some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkAfCod authored and GrapeBaBa committed Oct 21, 2024
1 parent b757922 commit 66651a2
Show file tree
Hide file tree
Showing 47 changed files with 865 additions and 102 deletions.
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
10 changes: 8 additions & 2 deletions src/main/java/io/optimism/utilities/LruCacheProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@

public class LruCacheProvider {

static <K, V> Cache<K, V> create() {
return CacheBuilder.newBuilder().maximumSize(1000L).build();
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

0 comments on commit 66651a2

Please sign in to comment.