diff --git a/Cargo.toml b/Cargo.toml index a90218b..854c676 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wvm-archiver" -version = "0.1.2" +version = "0.1.3" edition = "2021" description = "EL data pipeline for WVM testnet v0" authors = ["charmful0x "] diff --git a/README.md b/README.md index 30746d1..68c1d78 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,7 @@ pub struct InfoServerResponse { last_block: Option, total_archived_blocks: u64, archiver_balance: U256, + blocks_behind_live_blockheight: u64, archiver_address: String, network_name: String, network_chain_id: u32, diff --git a/Shuttle.toml b/Shuttle.toml index 6a39a98..746b941 100644 --- a/Shuttle.toml +++ b/Shuttle.toml @@ -1,3 +1,5 @@ +name = "wvm-archiver" + assets = [ ".env" ] \ No newline at end of file diff --git a/networks/goat.json b/networks/goat.json index 2f8d02b..7f56dd1 100644 --- a/networks/goat.json +++ b/networks/goat.json @@ -1,6 +1,6 @@ { "name": "GOATDev", - "network_chain_id": 3456, + "network_chain_id": 2345, "wvm_chain_id": 9496, "network_rpc": "http://3.15.141.150:8545", "wvm_rpc": "https://testnet-rpc.wvm.dev", diff --git a/src/main.rs b/src/main.rs index 3cb1b4f..57aad9c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,20 +1,13 @@ -use crate::utils::archive_block::archive; -use crate::utils::planetscale::{ps_archive_block, ps_get_latest_block_id}; +use crate::utils::archive_block::sprint_blocks_archiving; use crate::utils::schema::Network; use crate::utils::server_handlers::{handle_block, handle_block_raw, handle_info, handle_weave_gm}; use axum::{routing::get, Router}; -use std::thread; -use std::time::Duration; use tokio::task; mod utils; #[shuttle_runtime::main] async fn main() -> shuttle_axum::ShuttleAxum { let network = Network::config(); - let block_time = network.block_time; - let ps_latest_archived_block = ps_get_latest_block_id().await; - // it defaults to network.start_block if planestcale fails - let mut start_block = ps_latest_archived_block; println!("\n{:#?}\n\n", network); // server routes @@ -24,21 +17,9 @@ async fn main() -> shuttle_axum::ShuttleAxum { .route("/block/:id", get(handle_block)) .route("/block/raw/:id", get(handle_block_raw)); - // poll blocks & archive in parallel + // poll blocks & sprint archiving in parallel task::spawn(async move { - loop { - println!("\n{}", "#".repeat(100)); - println!( - "\nARCHIVING BLOCK #{} of Network {} -- ChainId: {}\n", - start_block, network.name, network.network_chain_id - ); - let archive_txid = archive(Some(start_block)).await.unwrap(); - let _ = ps_archive_block(&start_block, &archive_txid).await; - start_block += 1; - println!("\n{}", "#".repeat(100)); - thread::sleep(Duration::from_secs(block_time.into())); - } + sprint_blocks_archiving().await; }); - Ok(router.into()) } diff --git a/src/utils/archive_block.rs b/src/utils/archive_block.rs index 4c31fc5..8265884 100644 --- a/src/utils/archive_block.rs +++ b/src/utils/archive_block.rs @@ -1,7 +1,9 @@ -use crate::utils::get_block::by_number; +use crate::utils::get_block::{by_number, get_current_block_number}; +use crate::utils::planetscale::{ps_archive_block, ps_get_latest_block_id}; use crate::utils::schema::{Block, Network}; use crate::utils::transaction::send_wvm_calldata; use anyhow::Error; +use std::{thread, time::Duration}; pub async fn archive(block_number: Option) -> Result { let network = Network::config(); @@ -24,3 +26,29 @@ pub async fn archive(block_number: Option) -> Result { let txid = send_wvm_calldata(brotli_res).await.unwrap(); Ok(txid) } + +pub async fn sprint_blocks_archiving() { + let network = Network::config(); + let block_time = network.block_time; + let mut current_block_number = get_current_block_number().await.as_u64(); + let ps_latest_archived_block = ps_get_latest_block_id().await; + // it defaults to network.start_block if planestcale fails + let mut start_block = ps_latest_archived_block; + + loop { + if ps_latest_archived_block < current_block_number - 1 { + println!("\n{}", "#".repeat(100)); + println!( + "\nARCHIVING BLOCK #{} of Network {} -- ChainId: {}\n", + start_block, network.name, network.network_chain_id + ); + let archive_txid = archive(Some(start_block)).await.unwrap(); + let _ = ps_archive_block(&start_block, &archive_txid).await; + start_block += 1; + println!("\n{}", "#".repeat(100)); + } else { + current_block_number = get_current_block_number().await.as_u64(); + thread::sleep(Duration::from_secs(block_time.into())); + } + } +} diff --git a/src/utils/get_block.rs b/src/utils/get_block.rs index 186819a..c8eb778 100644 --- a/src/utils/get_block.rs +++ b/src/utils/get_block.rs @@ -1,5 +1,5 @@ use crate::utils::schema::Network; -use ethers_core::types::{Block, H256}; +use ethers_core::types::{Block, H256, U64}; use ethers_providers::{Middleware, ProviderError}; pub async fn by_number(number: u64) -> Result>, ProviderError> { @@ -12,3 +12,11 @@ pub async fn by_number(number: u64) -> Result>, ProviderError Err(e) => Err(e), } } + +pub async fn get_current_block_number() -> U64 { + let network: Network = Network::config(); + // connect to the target EVM provider + let provider = Network::provider(&network, false).await; + let block_number = provider.get_block_number().await.unwrap_or(0.into()); + block_number +} diff --git a/src/utils/schema.rs b/src/utils/schema.rs index 5e728ef..7d1c4ef 100644 --- a/src/utils/schema.rs +++ b/src/utils/schema.rs @@ -1,4 +1,5 @@ use crate::utils::env_var::get_env_var; +use crate::utils::get_block::get_current_block_number; use crate::utils::transaction::get_archiver_balance; use borsh::{from_slice, to_vec}; use borsh_derive::{BorshDeserialize, BorshSerialize}; @@ -124,6 +125,7 @@ pub struct InfoServerResponse { first_block: Option, last_block: Option, total_archived_blocks: u64, + blocks_behind_live_blockheight: u64, archiver_balance: U256, archiver_address: String, network_name: String, @@ -137,9 +139,12 @@ impl InfoServerResponse { let total_archived_blocks = last_block.unwrap_or(0) - first_block.unwrap_or(0); let archiver_balance = get_archiver_balance().await; let archiver_balance = Some(archiver_balance).unwrap(); + let current_live_block = get_current_block_number().await.as_u64(); + let blocks_behind_live_blockheight = current_live_block - last_block.unwrap_or(0); let instance: InfoServerResponse = InfoServerResponse { archiver_balance, + blocks_behind_live_blockheight, first_block, last_block, total_archived_blocks, diff --git a/src/utils/server_handlers.rs b/src/utils/server_handlers.rs index 30a3bbc..ea86d51 100644 --- a/src/utils/server_handlers.rs +++ b/src/utils/server_handlers.rs @@ -1,9 +1,7 @@ use crate::utils::planetscale::{ps_get_archived_block_txid, ps_get_blocks_extremes}; -use crate::utils::schema::Block; -use crate::utils::schema::InfoServerResponse; +use crate::utils::schema::{Block, InfoServerResponse}; use crate::utils::transaction::decode_wvm_tx_data; use axum::{extract::Path, response::Json}; -use serde::de::value; use serde_json::Value; pub async fn handle_weave_gm() -> &'static str { @@ -21,7 +19,7 @@ pub async fn handle_info() -> Json { let first_block = first.get("block_id").unwrap().as_u64(); let last_block = last.get("block_id").unwrap().as_u64(); - let stats_res = InfoServerResponse::new(first_block, last_block).await; + let stats_res: InfoServerResponse = InfoServerResponse::new(first_block, last_block).await; let res = serde_json::to_value(&stats_res).unwrap(); Json(res) diff --git a/src/utils/transaction.rs b/src/utils/transaction.rs index 36b66db..d412b42 100644 --- a/src/utils/transaction.rs +++ b/src/utils/transaction.rs @@ -1,7 +1,7 @@ use crate::utils::env_var::get_env_var; use crate::utils::schema::Block; use crate::utils::schema::Network; -use ethers::types::{Bytes, H256}; +use ethers::types::H256; use ethers::utils::hex; use ethers::{prelude::*, utils}; use ethers_providers::{Http, Provider};