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

fix(examples): Start N Blocks Back from Tip #349

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions crates/derive/src/online/alloy_providers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,19 @@ impl AlloyL2ChainProvider {
u64::from_str_radix(&chain_id, 16).map_err(|e| anyhow!(e))
}

/// Returns the latest L2 block number.
pub async fn latest_block_number(&mut self) -> Result<u64> {
let b: TransportResult<alloc::string::String> =
self.inner.raw_request("eth_blockNumber".into(), ()).await;
match b {
Ok(s) => {
let s = alloc::string::String::from(s.trim_start_matches("0x"));
u64::from_str_radix(&s, 16).map_err(|e| anyhow!(e))
}
Err(e) => Err(anyhow!(e)),
}
}

/// Creates a new [AlloyL2ChainProvider] from the provided [reqwest::Url].
pub fn new_http(url: reqwest::Url, rollup_config: Arc<RollupConfig>) -> Self {
let inner = ReqwestProvider::new_http(url);
Expand Down
25 changes: 18 additions & 7 deletions examples/trusted-sync/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@
START="${START_L2_BLOCK:-0}"
METRICS="${METRICS_URL:-127.0.0.1:9000}"

/usr/local/bin/trusted-sync \
--l1-rpc-url $L1_RPC_URL \
--l2-rpc-url $L2_RPC_URL \
--beacon-url $BEACON_URL \
--metrics-url $METRICS \
--start-l2-block $START \
-vvv
# If the `START_BLOCKS_FROM_TIP` environment variable is set, we will start syncing from the tip of the chain.
if [ -n "$START_BLOCKS_FROM_TIP" ]; then
/usr/local/bin/trusted-sync \
--l1-rpc-url $L1_RPC_URL \
--l2-rpc-url $L2_RPC_URL \
--beacon-url $BEACON_URL \
--metrics-url $METRICS \
--start-blocks-from-tip $START_BLOCKS_FROM_TIP \
-vvv
else
/usr/local/bin/trusted-sync \
--l1-rpc-url $L1_RPC_URL \
--l2-rpc-url $L2_RPC_URL \
--beacon-url $BEACON_URL \
--metrics-url $METRICS \
--start-l2-block $START \
-vvv
fi
3 changes: 3 additions & 0 deletions examples/trusted-sync/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ pub struct Cli {
/// Whether to enable Loki Metrics.
#[clap(long, help = "Enable Loki metrics")]
pub loki_metrics: bool,
/// Start blocks from tip.
#[clap(long, help = "Number of blocks prior to tip to start from")]
pub start_blocks_from_tip: Option<u64>,
}

impl Cli {
Expand Down
12 changes: 11 additions & 1 deletion examples/trusted-sync/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,18 @@ async fn sync(cli: cli::Cli) -> Result<()> {
// Construct the pipeline
let mut l1_provider = AlloyChainProvider::new_http(l1_rpc_url);

let start =
let mut start =
cli.start_l2_block.filter(|n| *n >= cfg.genesis.l2.number).unwrap_or(cfg.genesis.l2.number);

// If the start block from tip cli flag is specified, find the latest l2 block number
// and subtract the specified number of blocks to get the start block number.
if let Some(blocks) = cli.start_blocks_from_tip {
let latest = l2_provider.latest_block_number().await?;
start = latest.saturating_sub(blocks);
refcell marked this conversation as resolved.
Show resolved Hide resolved
info!(target: LOG_TARGET, "Starting {} blocks from tip at L2 block number: {}", blocks, start);
}

println!("Starting from L2 block number: {}", start);
let mut l2_provider = AlloyL2ChainProvider::new_http(l2_rpc_url.clone(), cfg.clone());
let attributes =
StatefulAttributesBuilder::new(cfg.clone(), l2_provider.clone(), l1_provider.clone());
Expand Down
4 changes: 4 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ docker-run-ts:
-e L1_RPC_URL=$L1_RPC_URL \
-e L2_RPC_URL=$L2_RPC_URL \
-e BEACON_URL=$BEACON_URL \
-e METRICS_URL=$METRICS_URL \
-e START_L2_BLOCK=$START_L2_BLOCK \
-e START_BLOCKS_FROM_TIP=$START_BLOCKS_FROM_TIP \
trusted-sync

# Run the `trusted-sync` docker container with Loki logging
Expand All @@ -110,6 +113,7 @@ docker-run-ts-with-loki:
-e LOKI_URL=$LOKI_URL \
-e METRICS_URL=$METRICS_URL \
-e START_L2_BLOCK=$START_L2_BLOCK \
-e START_BLOCKS_FROM_TIP=$START_BLOCKS_FROM_TIP \
trusted-sync

# Build the `kona-client` prestate artifacts for the latest release.
Expand Down
Loading