Skip to content

Commit

Permalink
fix: test case
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkAfCod authored and GrapeBaBa committed Mar 12, 2024
1 parent f4e99d4 commit d87b21c
Show file tree
Hide file tree
Showing 16 changed files with 244 additions and 105 deletions.
10 changes: 7 additions & 3 deletions hildr-node/src/main/java/io/optimism/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,18 @@ public class Cli implements Runnable {
description = "network type, or rollup.json file path, support: optimism-goerli, base-goerli")
String network;

@Option(names = "--l1-rpc-url", required = true, description = "The base chain RPC URL")
@Option(names = "--l1-rpc-url", required = true, description = "The l1 chain RPC URL")
String l1RpcUrl;

@Option(names = "--l1-ws-rpc-url", required = true, description = "The base chain WS RPC URL")
@Option(names = "--l1-ws-rpc-url", required = true, description = "The l1 chain WS RPC URL")
String l1WsRpcUrl;

@Option(names = "--l1-beacon-url", required = false, description = "The base chain beacon client RPC URL")
@Option(names = "--l1-beacon-url", required = true, description = "The l1 chain beacon client RPC URL")
String l1BeaconUrl;

@Option(names = "--l1-beacon-archiver-url", required = false, description = "The l1 beacon chain archiver RPC URL")
String l1BeaconArchiverUrl;

@Option(names = "--l2-rpc-url", required = true, description = "The L2 engine RPC URL")
String l2RpcUrl;

Expand Down Expand Up @@ -201,6 +204,7 @@ private Config.CliConfig from(Cli cli) {
cli.l1RpcUrl,
cli.l1WsRpcUrl,
cli.l1BeaconUrl,
cli.l1BeaconArchiverUrl,
cli.l2RpcUrl,
cli.l2EngineUrl,
StringUtils.trim(Cli.this.getJwtSecret()),
Expand Down
46 changes: 27 additions & 19 deletions hildr-node/src/main/java/io/optimism/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,25 @@
/**
* The type Config.
*
* @param l1RpcUrl L1 chain rpc url.
* @param l1WsRpcUrl L1 chain websocket rpc url.
* @param l1BeaconUrl L1 chain websocket rpc url.
* @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 rpcPort The rpc port.
* @param devnet The flag of devnet.
* @param checkpointSyncUrl The checkpoint sync url.
* @param l1RpcUrl L1 chain rpc url.
* @param l1WsRpcUrl L1 chain websocket rpc url.
* @param l1BeaconUrl L1 beacon chain rpc url.
* @param l1BeaconArchiverUrl L1 beacon chain archiver rpc url.
* @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 rpcPort The rpc port.
* @param devnet The flag of devnet.
* @param checkpointSyncUrl The checkpoint sync url.
* @author grapebaba
* @since 0.1.0
*/
public record Config(
String l1RpcUrl,
String l1WsRpcUrl,
String l1BeaconUrl,
String l1BeaconArchiverUrl,
String l2RpcUrl,
String l2EngineUrl,
String jwtSecret,
Expand Down Expand Up @@ -121,6 +123,7 @@ private static MapConfigSource getMapConfigSource() {
defaultProvider.put("config.l1RpcUrl", "");
defaultProvider.put("config.l1WsRpcUrl", "");
defaultProvider.put("config.l1BeaconUrl", "");
defaultProvider.put("config.l1BeaconArchiverUrl", "");
defaultProvider.put("config.jwtSecret", "");
defaultProvider.put("config.checkpointSyncUrl", "");
defaultProvider.put("config.rpcPort", "9545");
Expand All @@ -130,20 +133,22 @@ private static MapConfigSource getMapConfigSource() {
/**
* The type Cli config.
*
* @param l1RpcUrl L1 chain rpc url.
* @param l1WsRpcUrl L1 chain websocket rpc url.
* @param l1BeaconUrl L1 chain beacon client API rpc url.
* @param l2RpcUrl L2 chain rpc url.
* @param l2EngineUrl L2 engine API url.
* @param jwtSecret L2 engine API jwt secret.
* @param checkpointSyncUrl The checkpoint sync url.
* @param rpcPort The rpc port.
* @param devnet The devnet flag.
* @param l1RpcUrl L1 chain rpc url.
* @param l1WsRpcUrl L1 chain websocket rpc url.
* @param l1BeaconUrl L1 chain beacon client API rpc url.
* @param l1BeaconArchiverUrl L1 chain beacon archiver API rpc url.
* @param l2RpcUrl L2 chain rpc url.
* @param l2EngineUrl L2 engine API url.
* @param jwtSecret L2 engine API jwt secret.
* @param checkpointSyncUrl The checkpoint sync url.
* @param rpcPort The rpc port.
* @param devnet The devnet flag.
*/
public record CliConfig(
String l1RpcUrl,
String l1WsRpcUrl,
String l1BeaconUrl,
String l1BeaconArchiverUrl,
String l2RpcUrl,
String l2EngineUrl,
String jwtSecret,
Expand All @@ -167,6 +172,9 @@ public Map<String, String> toConfigMap() {
if (StringUtils.isNotEmpty(l1BeaconUrl)) {
map.put("config.l1BeaconUrl", l1BeaconUrl);
}
if (StringUtils.isNotEmpty(l1BeaconArchiverUrl)) {
map.put("config.l1BeaconArchiverUrl", l1BeaconArchiverUrl);
}
if (StringUtils.isNotEmpty(l2RpcUrl)) {
map.put("config.l2RpcUrl", l2RpcUrl);
}
Expand Down
47 changes: 41 additions & 6 deletions hildr-node/src/main/java/io/optimism/l1/BeaconBlobFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -42,6 +43,8 @@ public class BeaconBlobFetcher {

private final String sidecarsMethod;

private final String archiverSidecarsMethod;

private final OkHttpClient httpClient;

private final ObjectMapper mapper;
Expand All @@ -52,13 +55,25 @@ public class BeaconBlobFetcher {

/**
* Beacon blob info fetcher constructor.
*
* @param beaconUrl L1 beacon client url
*/
public BeaconBlobFetcher(String beaconUrl) {
this(beaconUrl, null);
}

/**
* Beacon blob info fetcher constructor.
*
* @param beaconUrl L1 beacon client url
* @param beaconArchiverUrl L1 beacon archiver client url
*/
public BeaconBlobFetcher(String beaconUrl, String beaconArchiverUrl) {
this.genesisMethod = GENESIS_METHOD_FORMAT.formatted(beaconUrl);
this.specMethod = SPEC_METHOD_FORMAT.formatted(beaconUrl);
this.sidecarsMethod = SIDECARS_METHOD_PREFIX_FORMAT.formatted(beaconUrl);
this.archiverSidecarsMethod = StringUtils.isEmpty(beaconArchiverUrl)
? null
: SIDECARS_METHOD_PREFIX_FORMAT.formatted(beaconArchiverUrl);
this.httpClient = HttpClientProvider.create();
this.mapper = new ObjectMapper();
this.mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Expand Down Expand Up @@ -110,14 +125,34 @@ public List<BlobSidecar> getBlobSidecards(String blockId, final List<BigInteger>
var params = indices == null || indices.isEmpty()
? null
: Map.of("indices", indices.stream().map(BigInteger::toString).collect(Collectors.joining(",")));
var req = new Request.Builder()
.get()
.url(this.sidecarsMethod + "/" + blockId + prepareQueryParams(params))
.build();
var res = this.send(req, new TypeReference<BeaconApiResponse<List<BlobSidecar>>>() {});
var postfix = "%s%s".formatted(blockId, prepareQueryParams(params));
var res = getBlobSidecars("%s/%s".formatted(this.sidecarsMethod, postfix));
if (res.getData() != null && res.getData().isEmpty()) {
return res.getData();
}
if (this.archiverSidecarsMethod != null) {
LOGGER.debug(
"blob sidecars may be pruned, try blob archiver sidecars method: blockId = {}, indices = {}",
blockId,
indices);
var archiverRes = getBlobSidecars("%s/%s".formatted(this.archiverSidecarsMethod, postfix));
if (archiverRes.getData() != null && !archiverRes.getData().isEmpty()) {
return archiverRes.getData();
}
} else {
LOGGER.debug(
"blob archiver sidecars method is empty, skip retry: block Id = {}, indices = {}",
blockId,
indices);
}
return res.getData();
}

private BeaconApiResponse<List<BlobSidecar>> getBlobSidecars(String url) {
var req = new Request.Builder().get().url(url).build();
return this.send(req, new TypeReference<BeaconApiResponse<List<BlobSidecar>>>() {});
}

private <T> T send(final Request req, final TypeReference<T> typeRef) {
Call call = this.httpClient.newCall(req);
try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
Expand Down
2 changes: 1 addition & 1 deletion hildr-node/src/main/java/io/optimism/l1/InnerWatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public InnerWatcher(
this.config = config;
this.provider = Web3jProvider.createClient(config.l1RpcUrl());
this.wsProvider = Web3jProvider.createClient(config.l1WsRpcUrl());
this.beaconFetcher = new BeaconBlobFetcher(config.l1BeaconUrl());
this.beaconFetcher = new BeaconBlobFetcher(config.l1BeaconUrl(), config.l1BeaconArchiverUrl());
this.l2StartBlock = l2StartBlock;
this.devnet = config.devnet() != null && config.devnet();

Expand Down
2 changes: 1 addition & 1 deletion hildr-node/src/test/java/io/optimism/HildrTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class HildrTest {
*/
@Test
void appHasGreeting() throws JsonProcessingException {
CliConfig cliConfig = new CliConfig("test", "test", "test", "test", "test", null, null, null, false);
CliConfig cliConfig = new CliConfig("test", "test", "test", "test", "test", "test", "test", null, null, false);
TomlMapper mapper = new TomlMapper();
String cliConfigStr = mapper.writerFor(CliConfig.class).writeValueAsString(cliConfig);

Expand Down
2 changes: 1 addition & 1 deletion hildr-node/src/test/java/io/optimism/TestConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static Config createConfig() {
var l1BeaconRpcUrl = l1RpcBeaconUrlFormat.formatted(envs.get(ETH_API_ENV));
var l2RpcUrl = l2RpcUrlFormat.formatted(envs.get(OPT_API_ENV));
Config.CliConfig cliConfig = new Config.CliConfig(
l1RpcUrl, l1WsRpcUrl, l1BeaconRpcUrl, l2RpcUrl, null, "testjwt", null, null, false);
l1RpcUrl, l1WsRpcUrl, l1BeaconRpcUrl, l1BeaconRpcUrl, l2RpcUrl, null, "testjwt", null, null, false);
return Config.create(null, cliConfig, Config.ChainConfig.optimismGoerli());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ConfigTest {
*/
@Test
void create() {
CliConfig cliConfig = new CliConfig(null, null, null, null, "testjwt", null, null, null, false);
CliConfig cliConfig = new CliConfig(null, null, null, null, null, null, "testjwt", null, null, false);
Config config = Config.create(
Paths.get("src", "test", "resources", "test.toml"), cliConfig, ChainConfig.optimismGoerli());
assertEquals("https://example2.com", config.l2RpcUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void testReadChannelData() {
}

private Tuple2<Channels<BatcherTransactions>, MessagePassingQueue<BatcherTransactionMessage>> createStage() {
Config config = new Config("", "", "", "", "", null, null, 9545, false, ChainConfig.optimismGoerli());
Config config = new Config("", "", "", "", "", "", null, null, 9545, false, ChainConfig.optimismGoerli());
MessagePassingQueue<BatcherTransactionMessage> transactionMessageMessagePassingQueue =
new MpscGrowableArrayQueue<>(4096);
Channels<BatcherTransactions> channels =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void testNewDriverFromFinalizedHead() throws IOException, ExecutionException, In
l1rpc,
l1WsRpc,
l1BeaconRpc,
l1BeaconRpc,
l2rpc,
null,
"d195a64e08587a3f1560686448867220c2727550ce3e0c95c7200d0ade0f9167",
Expand Down
88 changes: 28 additions & 60 deletions hildr-node/src/test/java/io/optimism/driver/HeadInfoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import java.io.IOException;
import java.math.BigInteger;
import java.net.URL;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.web3j.protocol.Web3j;
Expand Down Expand Up @@ -73,71 +78,34 @@ void shouldFailConversionFromBlockToHeadInfoIfMissingL1DepositedTx() throws Json
@Test
@DisplayName("should convert from a block to head info")
@SuppressWarnings("checkstyle:LineLength")
void shouldConvertFromBlockToHeadInfo() throws JsonProcessingException {
void shouldConvertFromBlockToHeadInfo() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();

String rawBlock =
"""
{
"hash": "0x2e4f4aff36bb7951be9742ad349fb1db84643c6bbac5014f3d196fd88fe333eb",
"parentHash": "0xeccf4c06ad0d27be1cadee5720a509d31a9de0462b52f2cf6045d9a73c9aa504",
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"miner": "0x4200000000000000000000000000000000000011",
"stateRoot": "0x5905b2423f299a29db41e377d7ceadf4baa49eed04e1b72957e8c0985e04e730",
"transactionsRoot": "0x030e481411042a769edde83d790d583ed69f9d3098d4a78d00e008f749fcfd97",
"receiptsRoot": "0x29079b696c12a19999f3bb303fddb6fc12fb701f427678cca24954b91080ada3",
"number": "0x7fe52f",
"gasUsed": "0xb711",
"gasLimit": "0x17d7840",
"extraData": "0x",
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"timestamp": "0x644434c2",
"difficulty": "0x0",
"totalDifficulty": "0x0",
"sealFields": [],
"uncles": [],
"transactions": [
{
"hash": "0x661df2908a63c9701ef4f9bc1d62432f08cbdc8c6fe6012af49405c00de5f69d",
"nonce": "0x41ed06",
"blockHash": "0x2e4f4aff36bb7951be9742ad349fb1db84643c6bbac5014f3d196fd88fe333eb",
"blockNumber": "0x7fe52f",
"transactionIndex": "0x0",
"from": "0xdeaddeaddeaddeaddeaddeaddeaddeaddead0001",
"to": "0x4200000000000000000000000000000000000015",
"value": "0x0",
"gasPrice": "0x0",
"gas": "0xf4240",
"input": "0x015d8eb900000000000000000000000000000000000000000000000000000000008768240000000000000000000000000000000000000000000000000000000064443450000000000000000000000000000000000000000000000000000000000000000e0444c991c5fe1d7291ff34b3f5c3b44ee861f021396d33ba3255b83df30e357d00000000000000000000000000000000000000000000000000000000000000050000000000000000000000007431310e026b69bfc676c0013e12a1a11411eec9000000000000000000000000000000000000000000000000000000000000083400000000000000000000000000000000000000000000000000000000000f4240",
"v": "0x0",
"r": "0x0",
"s": "0x0",
"type": "0x7e",
"mint": "0x0",
"sourceHash": "0x34ad504eea583add76d3b9d249965356ef6ca344d6766644c929357331bb0dc9"
}
],
"size": "0x365",
"mixHash": "0x7aeec5550a9b0616701e49ab835af5f10eadba2a0582016f0e256c9cace0c046",
"nonce": "0x0000000000000000",
"baseFeePerGas": "0x32"
}""";

EthBlock.Block block = objectMapper.readValue(rawBlock, EthBlock.Block.class);
List<String> rawBlockFiles = List.of("bedrock_raw_block.txt", "ectone_raw_block.txt");
for (String path : rawBlockFiles) {
URL url = Resources.getResource(path);
String testData = Resources.toString(url, Charsets.UTF_8);
Map map = objectMapper.readValue(testData, Map.class);

HeadInfo headInfo = HeadInfo.from(block);
EthBlock.Block block = objectMapper.convertValue(map.get("test_block_data"), EthBlock.Block.class);

assertEquals(
"0x2e4f4aff36bb7951be9742ad349fb1db84643c6bbac5014f3d196fd88fe333eb",
headInfo.l2BlockInfo().hash());
assertEquals(BigInteger.valueOf(8381743L), headInfo.l2BlockInfo().number());
assertEquals(BigInteger.valueOf(1682191554L), headInfo.l2BlockInfo().timestamp());
HeadInfo headInfo = HeadInfo.from(block);
assertEquals(map.get("assert_l2_block_hash"), headInfo.l2BlockInfo().hash());
assertEquals(
new BigInteger((String) map.get("assert_l2_block_number")),
headInfo.l2BlockInfo().number());
assertEquals(
new BigInteger((String) map.get("assert_l2_block_timestamp")),
headInfo.l2BlockInfo().timestamp());

assertEquals(
"0x0444c991c5fe1d7291ff34b3f5c3b44ee861f021396d33ba3255b83df30e357d",
headInfo.l1Epoch().hash());
assertEquals(BigInteger.valueOf(8874020L), headInfo.l1Epoch().number());
assertEquals(BigInteger.valueOf(1682191440L), headInfo.l1Epoch().timestamp());
assertEquals(map.get("assert_l1_block_hash"), headInfo.l1Epoch().hash());
assertEquals(
new BigInteger((String) map.get("assert_l1_block_number")),
headInfo.l1Epoch().number());
assertEquals(
new BigInteger((String) map.get("assert_l1_block_timestamp")),
headInfo.l1Epoch().timestamp());
}
}

@Test
Expand Down
Loading

0 comments on commit d87b21c

Please sign in to comment.