Skip to content

Commit 8410d16

Browse files
authored
Merge pull request #12 from weaveVM/dev
feat: v0.1.3
2 parents f3e1a3b + 3449795 commit 8410d16

10 files changed

+54
-31
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "wvm-archiver"
3-
version = "0.1.2"
3+
version = "0.1.3"
44
edition = "2021"
55
description = "EL data pipeline for WVM testnet v0"
66
authors = ["charmful0x <rani@decent.land>"]

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ pub struct InfoServerResponse {
101101
last_block: Option<u64>,
102102
total_archived_blocks: u64,
103103
archiver_balance: U256,
104+
blocks_behind_live_blockheight: u64,
104105
archiver_address: String,
105106
network_name: String,
106107
network_chain_id: u32,

Shuttle.toml

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
name = "wvm-archiver"
2+
13
assets = [
24
".env"
35
]

networks/goat.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "GOATDev",
3-
"network_chain_id": 3456,
3+
"network_chain_id": 2345,
44
"wvm_chain_id": 9496,
55
"network_rpc": "http://3.15.141.150:8545",
66
"wvm_rpc": "https://testnet-rpc.wvm.dev",

src/main.rs

+3-22
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1-
use crate::utils::archive_block::archive;
2-
use crate::utils::planetscale::{ps_archive_block, ps_get_latest_block_id};
1+
use crate::utils::archive_block::sprint_blocks_archiving;
32
use crate::utils::schema::Network;
43
use crate::utils::server_handlers::{handle_block, handle_block_raw, handle_info, handle_weave_gm};
54
use axum::{routing::get, Router};
6-
use std::thread;
7-
use std::time::Duration;
85
use tokio::task;
96

107
mod utils;
118
#[shuttle_runtime::main]
129
async fn main() -> shuttle_axum::ShuttleAxum {
1310
let network = Network::config();
14-
let block_time = network.block_time;
15-
let ps_latest_archived_block = ps_get_latest_block_id().await;
16-
// it defaults to network.start_block if planestcale fails
17-
let mut start_block = ps_latest_archived_block;
1811

1912
println!("\n{:#?}\n\n", network);
2013
// server routes
@@ -24,21 +17,9 @@ async fn main() -> shuttle_axum::ShuttleAxum {
2417
.route("/block/:id", get(handle_block))
2518
.route("/block/raw/:id", get(handle_block_raw));
2619

27-
// poll blocks & archive in parallel
20+
// poll blocks & sprint archiving in parallel
2821
task::spawn(async move {
29-
loop {
30-
println!("\n{}", "#".repeat(100));
31-
println!(
32-
"\nARCHIVING BLOCK #{} of Network {} -- ChainId: {}\n",
33-
start_block, network.name, network.network_chain_id
34-
);
35-
let archive_txid = archive(Some(start_block)).await.unwrap();
36-
let _ = ps_archive_block(&start_block, &archive_txid).await;
37-
start_block += 1;
38-
println!("\n{}", "#".repeat(100));
39-
thread::sleep(Duration::from_secs(block_time.into()));
40-
}
22+
sprint_blocks_archiving().await;
4123
});
42-
4324
Ok(router.into())
4425
}

src/utils/archive_block.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
use crate::utils::get_block::by_number;
1+
use crate::utils::get_block::{by_number, get_current_block_number};
2+
use crate::utils::planetscale::{ps_archive_block, ps_get_latest_block_id};
23
use crate::utils::schema::{Block, Network};
34
use crate::utils::transaction::send_wvm_calldata;
45
use anyhow::Error;
6+
use std::{thread, time::Duration};
57

68
pub async fn archive(block_number: Option<u64>) -> Result<String, Error> {
79
let network = Network::config();
@@ -24,3 +26,29 @@ pub async fn archive(block_number: Option<u64>) -> Result<String, Error> {
2426
let txid = send_wvm_calldata(brotli_res).await.unwrap();
2527
Ok(txid)
2628
}
29+
30+
pub async fn sprint_blocks_archiving() {
31+
let network = Network::config();
32+
let block_time = network.block_time;
33+
let mut current_block_number = get_current_block_number().await.as_u64();
34+
let ps_latest_archived_block = ps_get_latest_block_id().await;
35+
// it defaults to network.start_block if planestcale fails
36+
let mut start_block = ps_latest_archived_block;
37+
38+
loop {
39+
if ps_latest_archived_block < current_block_number - 1 {
40+
println!("\n{}", "#".repeat(100));
41+
println!(
42+
"\nARCHIVING BLOCK #{} of Network {} -- ChainId: {}\n",
43+
start_block, network.name, network.network_chain_id
44+
);
45+
let archive_txid = archive(Some(start_block)).await.unwrap();
46+
let _ = ps_archive_block(&start_block, &archive_txid).await;
47+
start_block += 1;
48+
println!("\n{}", "#".repeat(100));
49+
} else {
50+
current_block_number = get_current_block_number().await.as_u64();
51+
thread::sleep(Duration::from_secs(block_time.into()));
52+
}
53+
}
54+
}

src/utils/get_block.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::utils::schema::Network;
2-
use ethers_core::types::{Block, H256};
2+
use ethers_core::types::{Block, H256, U64};
33
use ethers_providers::{Middleware, ProviderError};
44

55
pub async fn by_number(number: u64) -> Result<Option<Block<H256>>, ProviderError> {
@@ -12,3 +12,11 @@ pub async fn by_number(number: u64) -> Result<Option<Block<H256>>, ProviderError
1212
Err(e) => Err(e),
1313
}
1414
}
15+
16+
pub async fn get_current_block_number() -> U64 {
17+
let network: Network = Network::config();
18+
// connect to the target EVM provider
19+
let provider = Network::provider(&network, false).await;
20+
let block_number = provider.get_block_number().await.unwrap_or(0.into());
21+
block_number
22+
}

src/utils/schema.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::utils::env_var::get_env_var;
2+
use crate::utils::get_block::get_current_block_number;
23
use crate::utils::transaction::get_archiver_balance;
34
use borsh::{from_slice, to_vec};
45
use borsh_derive::{BorshDeserialize, BorshSerialize};
@@ -124,6 +125,7 @@ pub struct InfoServerResponse {
124125
first_block: Option<u64>,
125126
last_block: Option<u64>,
126127
total_archived_blocks: u64,
128+
blocks_behind_live_blockheight: u64,
127129
archiver_balance: U256,
128130
archiver_address: String,
129131
network_name: String,
@@ -137,9 +139,12 @@ impl InfoServerResponse {
137139
let total_archived_blocks = last_block.unwrap_or(0) - first_block.unwrap_or(0);
138140
let archiver_balance = get_archiver_balance().await;
139141
let archiver_balance = Some(archiver_balance).unwrap();
142+
let current_live_block = get_current_block_number().await.as_u64();
143+
let blocks_behind_live_blockheight = current_live_block - last_block.unwrap_or(0);
140144

141145
let instance: InfoServerResponse = InfoServerResponse {
142146
archiver_balance,
147+
blocks_behind_live_blockheight,
143148
first_block,
144149
last_block,
145150
total_archived_blocks,

src/utils/server_handlers.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use crate::utils::planetscale::{ps_get_archived_block_txid, ps_get_blocks_extremes};
2-
use crate::utils::schema::Block;
3-
use crate::utils::schema::InfoServerResponse;
2+
use crate::utils::schema::{Block, InfoServerResponse};
43
use crate::utils::transaction::decode_wvm_tx_data;
54
use axum::{extract::Path, response::Json};
6-
use serde::de::value;
75
use serde_json::Value;
86

97
pub async fn handle_weave_gm() -> &'static str {
@@ -21,7 +19,7 @@ pub async fn handle_info() -> Json<Value> {
2119

2220
let first_block = first.get("block_id").unwrap().as_u64();
2321
let last_block = last.get("block_id").unwrap().as_u64();
24-
let stats_res = InfoServerResponse::new(first_block, last_block).await;
22+
let stats_res: InfoServerResponse = InfoServerResponse::new(first_block, last_block).await;
2523

2624
let res = serde_json::to_value(&stats_res).unwrap();
2725
Json(res)

src/utils/transaction.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::utils::env_var::get_env_var;
22
use crate::utils::schema::Block;
33
use crate::utils::schema::Network;
4-
use ethers::types::{Bytes, H256};
4+
use ethers::types::H256;
55
use ethers::utils::hex;
66
use ethers::{prelude::*, utils};
77
use ethers_providers::{Http, Provider};

0 commit comments

Comments
 (0)