Skip to content

Commit 980eeff

Browse files
authored
Refactor to continuous background preimage generation and L1 finality caching (#30)
1 parent 43a7f04 commit 980eeff

30 files changed

+2786
-345
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- name: Install components
2222
run: |
2323
rustup component add rustfmt clippy
24-
cargo install cargo-machete --version 0.8.0
24+
cargo install cargo-machete --version 0.8.0 --locked
2525
- name: Check format
2626
run: cargo fmt --all -- --check
2727
- name: Check clippy
@@ -51,7 +51,7 @@ jobs:
5151
- name: Install Foundry
5252
uses: foundry-rs/foundry-toolchain@v1
5353
with:
54-
version: 'v1.0.0'
54+
version: 'v1.0.0'
5555
- name: Install kurtosis
5656
run: |
5757
sudo apt-get install jq

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ target/
2525
/.data
2626
*.bin
2727
*.json
28-
chain
28+
chain
29+
.preimage
30+
.finalized_l1

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ wait:
3737

3838
.PHONY: server-up
3939
server-up:
40+
mkdir -p .preimage && true
41+
mkdir -p .finalized_l1 && true
4042
@L2_ROLLUP_PORT=$$(jq -r '.l2RollupPort' hostPort.json);\
4143
L2_GETH_PORT=$$(jq -r '.l2GethPort' hostPort.json);\
4244
L1_GETH_PORT=$$(jq -r '.l1GethPort' hostPort.json);\
@@ -47,19 +49,21 @@ server-up:
4749
--l2=http://localhost:$$L2_GETH_PORT \
4850
--l1=http://localhost:$$L1_GETH_PORT \
4951
--beacon=http://localhost:$$L1_BEACON_PORT \
50-
--l1-chain-config=$$L1_CHAIN_CONFIG
52+
--l1-chain-config=$$L1_CHAIN_CONFIG \
53+
--initial-claimed-l2=103 \
54+
--ttl=1800 \
55+
--max-preimage-distance=100 \
56+
--purger-interval-seconds=100
5157

5258
.PHONY: test
5359
test:
5460
@L2_ROLLUP_PORT=$$(jq -r '.l2RollupPort' hostPort.json);\
5561
L2_GETH_PORT=$$(jq -r '.l2GethPort' hostPort.json);\
56-
L2_ROLLUP_PORT=$$L2_ROLLUP_PORT L2_GETH_PORT=$$L2_GETH_PORT cargo test --manifest-path=./server/Cargo.toml
62+
L2_ROLLUP_ADDR=http://localhost:$$L2_ROLLUP_PORT L2_GETH_ADDR=http://localhost:$$L2_GETH_PORT cargo test --manifest-path=./server/Cargo.toml
5763

58-
.PHONY: test-ignored
59-
test-ignored:
60-
@L2_ROLLUP_PORT=$$(jq -r '.l2RollupPort' hostPort.json);\
61-
L2_GETH_PORT=$$(jq -r '.l2GethPort' hostPort.json);\
62-
REQUEST_PATH=$(CURDIR)/tool/body.json L2_ROLLUP_PORT=$$L2_ROLLUP_PORT L2_GETH_PORT=$$L2_GETH_PORT cargo test --manifest-path=./server/Cargo.toml -- --ignored
64+
.PHONY: inspect
65+
inspect:
66+
cargo test --manifest-path=./server/Cargo.toml -- --ignored
6367

6468
.PHONY: devnet-down
6569
devnet-down:

server/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "optimism-preimage-maker"
3-
version = "0.1.3"
3+
version = "0.2.0"
44
edition = "2021"
55

66
[dependencies]
@@ -15,6 +15,7 @@ tracing = { version = "0.1", default-features = false }
1515
tracing-subscriber = { version = "0.3", features = ["fmt", "env-filter"] }
1616
axum = "0.7.9"
1717
tokio = { version="1.41", features=["full"] }
18+
base64 = "0.22.1"
1819

1920
# Kona
2021
kona-client = { workspace = true }
@@ -29,7 +30,6 @@ alloy-primitives = { workspace = true, features = ["map", "serde"] }
2930

3031
# optimism derivation
3132
optimism-derivation = { git = "https://github.com/datachainlab/optimism-elc", rev = "v0.1.10", default-features = false }
32-
base64 = "0.22.1"
3333

3434
[dev-dependencies]
3535
prost = "0.11.9"

server/src/client/beacon_client.rs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
use alloy_primitives::B256;
2+
use axum::async_trait;
3+
use reqwest::Response;
4+
5+
#[derive(Debug, Clone, serde::Deserialize)]
6+
pub struct LightClientFinalityUpdateResponse {
7+
pub data: LightClientFinalityUpdate,
8+
}
9+
10+
#[derive(Debug, Clone, serde::Deserialize)]
11+
pub struct LightClientFinalityUpdate {
12+
pub finalized_header: LightClientHeader,
13+
}
14+
15+
#[derive(Debug, Clone, serde::Deserialize)]
16+
pub struct LightClientHeader {
17+
pub execution: ExecutionPayloadHeader,
18+
}
19+
20+
#[derive(Debug, Clone, serde::Deserialize)]
21+
pub struct ExecutionPayloadHeader {
22+
pub block_hash: B256,
23+
#[serde(deserialize_with = "deserialize_u64_from_str")]
24+
pub block_number: u64,
25+
}
26+
27+
// serde helper to allow numbers encoded as strings
28+
fn deserialize_u64_from_str<'de, D>(deserializer: D) -> Result<u64, D::Error>
29+
where
30+
D: serde::Deserializer<'de>,
31+
{
32+
struct U64StringOrNumber;
33+
34+
impl<'de> serde::de::Visitor<'de> for U64StringOrNumber {
35+
type Value = u64;
36+
37+
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
38+
write!(f, "a u64 as number or string")
39+
}
40+
41+
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E> {
42+
Ok(v)
43+
}
44+
45+
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
46+
where
47+
E: serde::de::Error,
48+
{
49+
if v < 0 {
50+
return Err(E::custom("negative value for u64"));
51+
}
52+
Ok(v as u64)
53+
}
54+
55+
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
56+
where
57+
E: serde::de::Error,
58+
{
59+
v.parse::<u64>()
60+
.map_err(|e| E::custom(format!("invalid u64 string: {e}")))
61+
}
62+
63+
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
64+
where
65+
E: serde::de::Error,
66+
{
67+
self.visit_str(&v)
68+
}
69+
}
70+
71+
deserializer.deserialize_any(U64StringOrNumber)
72+
}
73+
74+
#[async_trait]
75+
pub trait BeaconClient: Send + Sync + 'static {
76+
async fn get_raw_light_client_finality_update(&self) -> anyhow::Result<String>;
77+
}
78+
79+
#[derive(Debug, Clone)]
80+
pub struct HttpBeaconClient {
81+
beacon_addr: String,
82+
client: reqwest::Client,
83+
}
84+
85+
impl HttpBeaconClient {
86+
pub fn new(beacon_addr: String, timeout: std::time::Duration) -> Self {
87+
Self {
88+
beacon_addr,
89+
client: reqwest::Client::builder()
90+
.timeout(timeout)
91+
.build()
92+
.expect("failed to build reqwest client"),
93+
}
94+
}
95+
96+
async fn check_response(&self, response: Response) -> anyhow::Result<Response> {
97+
if response.status().is_success() {
98+
Ok(response)
99+
} else {
100+
Err(anyhow::anyhow!(
101+
"Request failed with status: {} body={:?}",
102+
response.status(),
103+
response.text().await
104+
))
105+
}
106+
}
107+
}
108+
109+
#[async_trait]
110+
impl BeaconClient for HttpBeaconClient {
111+
async fn get_raw_light_client_finality_update(&self) -> anyhow::Result<String> {
112+
let response = self
113+
.client
114+
.get(format!(
115+
"{}/eth/v1/beacon/light_client/finality_update",
116+
self.beacon_addr
117+
))
118+
.send()
119+
.await?;
120+
let response = self.check_response(response).await?;
121+
response
122+
.text()
123+
.await
124+
.map_err(|e| anyhow::anyhow!("Failed to get finality update: {e:?}"))
125+
}
126+
}

0 commit comments

Comments
 (0)