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

feat: v0.1.3 #12

Merged
merged 6 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <rani@decent.land>"]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub struct InfoServerResponse {
last_block: Option<u64>,
total_archived_blocks: u64,
archiver_balance: U256,
blocks_behind_live_blockheight: u64,
archiver_address: String,
network_name: String,
network_chain_id: u32,
Expand Down
2 changes: 2 additions & 0 deletions Shuttle.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
name = "wvm-archiver"

assets = [
".env"
]
2 changes: 1 addition & 1 deletion networks/goat.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
25 changes: 3 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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())
}
30 changes: 29 additions & 1 deletion src/utils/archive_block.rs
Original file line number Diff line number Diff line change
@@ -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<u64>) -> Result<String, Error> {
let network = Network::config();
Expand All @@ -24,3 +26,29 @@ pub async fn archive(block_number: Option<u64>) -> Result<String, Error> {
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()));
}
}
}
10 changes: 9 additions & 1 deletion src/utils/get_block.rs
Original file line number Diff line number Diff line change
@@ -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<Option<Block<H256>>, ProviderError> {
Expand All @@ -12,3 +12,11 @@ pub async fn by_number(number: u64) -> Result<Option<Block<H256>>, 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
}
5 changes: 5 additions & 0 deletions src/utils/schema.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -124,6 +125,7 @@ pub struct InfoServerResponse {
first_block: Option<u64>,
last_block: Option<u64>,
total_archived_blocks: u64,
blocks_behind_live_blockheight: u64,
archiver_balance: U256,
archiver_address: String,
network_name: String,
Expand All @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions src/utils/server_handlers.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -21,7 +19,7 @@ pub async fn handle_info() -> Json<Value> {

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)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/transaction.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down