Skip to content

Commit 1f8167a

Browse files
committed
feat:init archiver and storage
Signed-off-by: Chen Kai <281165273grape@gmail.com>
1 parent 8e649ca commit 1f8167a

File tree

15 files changed

+8591
-0
lines changed

15 files changed

+8591
-0
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[workspace]
2+
members = ["bin/archiver", "bin/api", "crates/config", "crates/storage"]
3+
default-members = ["bin/archiver", "bin/api"]
4+
resolver = "2"
5+
6+
[workspace.package]
7+
edition = "2021"
8+
license = "MIT"
9+
authors = ["grapebaba"]
10+
repository = "https://github.com/optimism-java/blob-archiver-rs"
11+
homepage = "https://github.com/optimism-java/blob-archiver-rs"
12+
exclude = ["**/target", "benches/", "tests"]
13+
14+
[workspace.dependencies]
15+
eth2 = { git = "https://github.com/sigp/lighthouse.git", tag = "v5.2.1" }
16+
17+
clap = "4"
18+
futures-util = "0.3"
19+
reqwest ="0.12"
20+
serde_json = "1.0.94"
21+
serde = "1.0"
22+
tracing = "0.1.40"
23+
eyre = "0.6.12"
24+
flate2 = "1.0.30"
25+
async-trait = "0.1.81"
26+
spin = { version = "0.9.8", features = ["mutex"] }
27+
28+
aws-config = { version = "1.5.4", features = ["behavior-version-latest"] }
29+
aws-sdk-s3 = "1.41.0"
30+
tokio = { version = "1.38.1", features = ["full"] }

bin/api/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "api"
3+
version = "0.1.0"
4+
edition.workspace = true
5+
license.workspace = true
6+
authors.workspace = true
7+
repository.workspace = true
8+
homepage.workspace = true
9+
exclude.workspace = true
10+
11+
[dependencies]

bin/api/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("Hello, world!");
3+
}

bin/archiver/Cargo.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "archiver"
3+
version = "0.1.0"
4+
edition.workspace = true
5+
license.workspace = true
6+
authors.workspace = true
7+
repository.workspace = true
8+
homepage.workspace = true
9+
exclude.workspace = true
10+
11+
[dependencies]
12+
eth2.workspace = true
13+
14+
clap.workspace = true
15+
futures-util.workspace = true
16+
reqwest.workspace = true
17+
serde_json.workspace = true
18+
serde.workspace = true
19+
tracing.workspace = true
20+
tokio.workspace = true
21+
blob-archiver-storage = { path = "../../crates/storage" }
22+

bin/archiver/src/archiver.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use eth2::{BeaconNodeHttpClient, Error};
2+
use eth2::types::{BlockId, MainnetEthSpec};
3+
use tracing::log::trace;
4+
use blob_archiver_storage::{BlobData, BlobSidecars, Header};
5+
6+
pub struct Archiver {
7+
pub beacon_client: BeaconNodeHttpClient,
8+
}
9+
10+
impl Archiver {
11+
pub fn new(beacon_client: BeaconNodeHttpClient) -> Self {
12+
Self { beacon_client }
13+
}
14+
15+
pub async fn persist_blobs_for_block(&self, block_id: BlockId) -> Result<(), Error> {
16+
let header_resp_opt = self.beacon_client.get_beacon_headers_block_id(block_id).await?;
17+
if let Some(header) = header_resp_opt {
18+
let beacon_client = self.beacon_client.clone();
19+
let blobs_resp_opt = beacon_client.get_blobs::<MainnetEthSpec>(BlockId::Root(header.data.root), None).await?;
20+
if let Some(blob_sidecars) = blobs_resp_opt {
21+
let blob_sidecar_list = blob_sidecars.data;
22+
let blob_data = BlobData::new(Header { beacon_block_hash: header.data.root }, BlobSidecars { data: blob_sidecar_list });
23+
trace!("Persisting blobs for block: {:?}", blob_data);
24+
return Ok(());
25+
}
26+
return Ok(());
27+
}
28+
29+
Ok(())
30+
}
31+
}
32+
33+
#[cfg(test)]
34+
mod tests {
35+
use std::str::FromStr;
36+
use std::time::Duration;
37+
38+
use eth2::{SensitiveUrl, Timeouts};
39+
40+
use super::*;
41+
42+
#[tokio::test]
43+
async fn test_persist_blobs_for_block() {
44+
let beacon_client = BeaconNodeHttpClient::new(SensitiveUrl::from_str("https://ethereum-beacon-api.publicnode.com").unwrap(), Timeouts::set_all(Duration::from_secs(30)));
45+
let archiver = Archiver::new(beacon_client);
46+
47+
let block_id = BlockId::Head;
48+
archiver.persist_blobs_for_block(block_id).await.unwrap();
49+
}
50+
}

bin/archiver/src/main.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use std::str::FromStr;
2+
use std::time::Duration;
3+
4+
use eth2::{BeaconNodeHttpClient, SensitiveUrl, Timeouts};
5+
use eth2::types::BlockId;
6+
7+
use crate::archiver::Archiver;
8+
9+
mod archiver;
10+
11+
#[tokio::main]
12+
async fn main() {
13+
let beacon_client = BeaconNodeHttpClient::new(SensitiveUrl::from_str("https://ethereum-beacon-api.publicnode.com").unwrap(), Timeouts::set_all(Duration::from_secs(30)));
14+
let archiver = Archiver::new(beacon_client);
15+
16+
let block_id = BlockId::Head;
17+
18+
archiver.persist_blobs_for_block(block_id).await.expect("TODO: panic message");
19+
}

crates/config/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "blob-archiver-config"
3+
version = "0.1.0"
4+
edition.workspace = true
5+
license.workspace = true
6+
authors.workspace = true
7+
repository.workspace = true
8+
homepage.workspace = true
9+
exclude.workspace = true
10+
11+
[dependencies]
12+
eth2.workspace = true
13+
eyre.workspace = true

crates/config/src/config.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use std::str::FromStr;
2+
use std::time::Duration;
3+
use eth2::types::Hash256;
4+
use anyhow::Result;
5+
6+
#[derive(Debug, Clone, Default, PartialEq, Eq)]
7+
pub struct BeaconConfig {
8+
pub beacon_url: String,
9+
pub beacon_client_timeout: Duration,
10+
pub enforce_json: bool,
11+
}
12+
13+
/// Configuration for the archiver.
14+
#[derive(Debug, Clone, Default, PartialEq, Eq)]
15+
pub struct ArchiverConfig {
16+
pub beacon: BeaconConfig,
17+
pub poll_interval: Duration,
18+
pub origin_block: Hash256,
19+
}

crates/config/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod config;
2+
3+
pub use config::{ArchiverConfig};

0 commit comments

Comments
 (0)