Skip to content

Commit a2db787

Browse files
authored
Merge pull request #25 from weaveVM/alphanet-v3
Alphanet v3
2 parents 42c69ac + 27bdde1 commit a2db787

10 files changed

+184
-98
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.2.4"
3+
version = "0.2.5"
44
edition = "2021"
55
description = "EL data pipeline for WVM testnet v0"
66
authors = ["charmful0x <rani@decent.land>"]

README.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,16 @@ As mentioned, PlanetScale is used for cloud indexing, which allows a WeaveVM Arc
105105
### WeaveVM Archiver node instance info
106106

107107
```bash
108-
curl -X GET https://the_network.wvm.network/info
108+
curl -X GET https://the_network.wvm.network/v1/info
109109
```
110110
**returns:**
111111

112112
```rs
113113
pub struct InfoServerResponse {
114-
first_archived_block: Option<u64>,
115-
last_archived_block: Option<u64>,
114+
first_livesync_archived_block: Option<u64>,
115+
last_livesync_archived_block: Option<u64>,
116+
first_backfill_archived_block: Option<u64>,
117+
last_backfill_archived_block: Option<u64>,
116118
livesync_start_block: u64,
117119
total_archived_blocks: u64,
118120
blocks_behind_live_blockheight: u64,
@@ -129,7 +131,7 @@ pub struct InfoServerResponse {
129131
### WeaveVM Archiver all networks info:
130132

131133
```bash
132-
curl -X GET https://the_network.wvm.network/all-networks-info
134+
curl -X GET https://the_network.wvm.network/v1/all-networks-info
133135
```
134136

135137
**returns:**
@@ -141,13 +143,13 @@ Vec<Network>
141143
### Retrieve the WVM archive TXID for a given EVM block ID
142144

143145
```bash
144-
curl -X GET https://the_network.wvm.network/block/$BLOCK_ID
146+
curl -X GET https://the_network.wvm.network/v1/block/$BLOCK_ID
145147
```
146148

147149
### Decode the WVM archived block data for a given EVM block ID (return original block data in JSON format)
148150

149151
```bash
150-
curl -X GET https://the_network.wvm.network/block/raw/$BLOCK_ID
152+
curl -X GET https://the_network.wvm.network/v1/block/raw/$BLOCK_ID
151153
```
152154

153155
## License

db_schema.sql

+7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
DROP TABLE IF EXISTS WeaveVMArchiver;
2+
DROP TABLE IF EXISTS WeaveVMArchiverBackfill;
23

34
CREATE TABLE IF NOT EXISTS WeaveVMArchiver (
45
Id INT AUTO_INCREMENT PRIMARY KEY,
56
NetworkBlockId INT UNIQUE,
67
WeaveVMArchiveTxid VARCHAR(66) UNIQUE
78
);
9+
10+
CREATE TABLE IF NOT EXISTS WeaveVMArchiverBackfill (
11+
Id INT AUTO_INCREMENT PRIMARY KEY,
12+
NetworkBlockId INT UNIQUE,
13+
WeaveVMArchiveTxid VARCHAR(66) UNIQUE
14+
);

src/main.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::utils::archive_block::sprint_blocks_archiving;
2-
use crate::utils::backfill_genesis::backfill_from_genesis;
32
use crate::utils::schema::Network;
43
use crate::utils::server_handlers::{
54
handle_all_networks_info, handle_block, handle_block_raw, handle_info, handle_weave_gm,
@@ -16,18 +15,18 @@ async fn main() -> shuttle_axum::ShuttleAxum {
1615
// server routes
1716
let router = Router::new()
1817
.route("/", get(handle_weave_gm))
19-
.route("/info", get(handle_info))
20-
.route("/block/:id", get(handle_block))
21-
.route("/block/raw/:id", get(handle_block_raw))
22-
.route("/all-networks-info", get(handle_all_networks_info));
18+
.route("/v1/info", get(handle_info))
19+
.route("/v1/block/:id", get(handle_block))
20+
.route("/v1/block/raw/:id", get(handle_block_raw))
21+
.route("/v1/all-networks-info", get(handle_all_networks_info));
2322

2423
// poll blocks & sprint archiving in parallel
2524
task::spawn(async move {
26-
sprint_blocks_archiving().await;
25+
sprint_blocks_archiving(false).await;
2726
});
2827
// backfill blocks from genesis till network.start_block
2928
task::spawn(async move {
30-
backfill_from_genesis().await.unwrap();
29+
sprint_blocks_archiving(true).await;
3130
});
3231
Ok(router.into())
3332
}

src/utils/archive_block.rs

+21-17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::utils::env_var::get_env_var;
12
use crate::utils::get_block::{by_number, get_current_block_number};
23
use crate::utils::planetscale::{ps_archive_block, ps_get_latest_block_id};
34
use crate::utils::schema::{Block, Network};
@@ -7,9 +8,19 @@ use std::{thread, time::Duration};
78

89
pub async fn archive(block_number: Option<u64>, is_backfill: bool) -> Result<String, Error> {
910
let network = Network::config();
10-
let start_block = network.start_block;
11-
let block_to_archive = block_number.unwrap_or(start_block);
11+
let block_to_archive = block_number.unwrap_or(if is_backfill {
12+
get_env_var("backfill_start_block")
13+
.unwrap()
14+
.parse::<u64>()
15+
.unwrap()
16+
} else {
17+
network.start_block
18+
});
1219

20+
// assert to ensure backfill and livesync dont collide at the continuity checkpoint
21+
if is_backfill {
22+
assert!(block_number.unwrap() < network.start_block)
23+
}
1324
// fetch block
1425
let block_data = by_number(block_to_archive).await.unwrap();
1526
// serialize response into Block struct
@@ -20,9 +31,6 @@ pub async fn archive(block_number: Option<u64>, is_backfill: bool) -> Result<Str
2031
// brotli compress the borsh serialized block
2132
let brotli_res = Block::brotli_compress(&borsh_res);
2233

23-
// println!("borsh vec length: {:?}", borsh_res.len());
24-
// println!("brotli vec length: {:?}", brotli_res.len());
25-
2634
let txid = if is_backfill {
2735
send_wvm_calldata_backfill(brotli_res).await.unwrap()
2836
} else {
@@ -32,27 +40,23 @@ pub async fn archive(block_number: Option<u64>, is_backfill: bool) -> Result<Str
3240
Ok(txid)
3341
}
3442

35-
pub async fn sprint_blocks_archiving() {
43+
pub async fn sprint_blocks_archiving(is_backfill: bool) {
3644
let network = Network::config();
3745
let block_time = network.block_time;
3846
let mut current_block_number = get_current_block_number().await.as_u64();
39-
let ps_latest_archived_block = ps_get_latest_block_id().await;
40-
// it defaults to network.start_block if planestcale fails
41-
let mut start_block = if ps_latest_archived_block < network.start_block {
42-
network.start_block
43-
} else {
44-
ps_latest_archived_block
45-
};
47+
// it defaults to network.start_block or env.backfill_start_block
48+
// based on is_backfill if planestcale fails
49+
let mut start_block = ps_get_latest_block_id(is_backfill).await;
4650

4751
loop {
4852
if start_block < current_block_number - 1 {
4953
println!("\n{}", "#".repeat(100));
5054
println!(
51-
"\nARCHIVING BLOCK #{} of Network {} -- ChainId: {}\n",
52-
start_block, network.name, network.network_chain_id
55+
"\nARCHIVING BLOCK #{} of Network {} -- ChainId: {} -- IS_BACKFILL: {}\n",
56+
start_block, network.name, network.network_chain_id, is_backfill
5357
);
54-
let archive_txid = archive(Some(start_block), false).await.unwrap();
55-
let _ = ps_archive_block(&start_block, &archive_txid).await;
58+
let archive_txid = archive(Some(start_block), is_backfill).await.unwrap();
59+
let _ = ps_archive_block(&start_block, &archive_txid, is_backfill).await;
5660
start_block += 1;
5761
println!("\n{}", "#".repeat(100));
5862
} else {

src/utils/backfill_genesis.rs

+26-26
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
use crate::utils::archive_block::archive;
2-
use crate::utils::env_var::get_env_var;
3-
use crate::utils::planetscale::ps_archive_block;
4-
use crate::utils::schema::Network;
5-
use anyhow::{Error, Ok};
1+
// use crate::utils::archive_block::archive;
2+
// use crate::utils::env_var::get_env_var;
3+
// use crate::utils::planetscale::ps_archive_block;
4+
// use crate::utils::schema::Network;
5+
// use anyhow::{Error, Ok};
66

7-
pub async fn backfill_from_genesis() -> Result<(), Error> {
8-
let network = Network::config();
9-
let config_start_block = network.start_block;
10-
let backfill_block_start: String = get_env_var("backfill_start_block").unwrap_or(0.to_string());
11-
let backfill_blocks: Vec<u64> =
12-
(backfill_block_start.parse::<u64>().unwrap()..=config_start_block).collect();
7+
// pub async fn backfill_from_genesis() -> Result<(), Error> {
8+
// let network = Network::config();
9+
// let config_start_block = network.start_block;
10+
// let backfill_block_start: String = get_env_var("backfill_start_block").unwrap_or(0.to_string());
11+
// let backfill_blocks: Vec<u64> =
12+
// (backfill_block_start.parse::<u64>().unwrap()..=config_start_block).collect();
1313

14-
if config_start_block == 0 {
15-
return Ok(());
16-
}
14+
// if config_start_block == 0 {
15+
// return Ok(());
16+
// }
1717

18-
for &block_number in backfill_blocks.iter() {
19-
println!("\n{}", "#".repeat(100));
20-
println!(
21-
"\nARCHIVING **BACKFILL** BLOCK #{} of Network {} -- ChainId: {}\n",
22-
&block_number, network.name, network.network_chain_id
23-
);
24-
let archive_txid = archive(Some(block_number), true).await.unwrap();
25-
let _ = ps_archive_block(&block_number, &archive_txid).await;
26-
println!("\n{}", "#".repeat(100));
27-
}
18+
// for &block_number in backfill_blocks.iter() {
19+
// println!("\n{}", "#".repeat(100));
20+
// println!(
21+
// "\nARCHIVING **BACKFILL** BLOCK #{} of Network {} -- ChainId: {}\n",
22+
// &block_number, network.name, network.network_chain_id
23+
// );
24+
// let archive_txid = archive(Some(block_number), true).await.unwrap();
25+
// let _ = ps_archive_block(&block_number, &archive_txid, true).await;
26+
// println!("\n{}", "#".repeat(100));
27+
// }
2828

29-
Ok(())
30-
}
29+
// Ok(())
30+
// }

src/utils/planetscale.rs

+83-24
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,27 @@ async fn ps_init() -> PSConnection {
1717
pub async fn ps_archive_block(
1818
network_block_id: &u64,
1919
wvm_calldata_txid: &str,
20+
is_backfill: bool,
2021
) -> Result<(), Error> {
2122
// format to the table VAR(66) limitation
2223
let wvm_calldata_txid = wvm_calldata_txid.trim_matches('"');
2324
let conn = ps_init().await;
25+
let mut ps_table_name = get_env_var("ps_table_name").unwrap();
2426

25-
let res = query(
26-
"INSERT INTO WeaveVMArchiverMetis(NetworkBlockId, WeaveVMArchiveTxid) VALUES($0, \"$1\")",
27-
)
28-
.bind(network_block_id)
29-
.bind(wvm_calldata_txid)
30-
.execute(&conn)
31-
.await;
27+
if is_backfill {
28+
ps_table_name = format!("{}{}", ps_table_name, "Backfill")
29+
}
30+
31+
let query_str = format!(
32+
"INSERT INTO {}(NetworkBlockId, WeaveVMArchiveTxid) VALUES($0, \"$1\")",
33+
ps_table_name
34+
);
35+
36+
let res = query(&query_str)
37+
.bind(network_block_id)
38+
.bind(wvm_calldata_txid)
39+
.execute(&conn)
40+
.await;
3241

3342
match res {
3443
Ok(result) => {
@@ -42,45 +51,86 @@ pub async fn ps_archive_block(
4251
}
4352
}
4453

45-
pub async fn ps_get_latest_block_id() -> u64 {
54+
pub async fn ps_get_latest_block_id(is_backfill: bool) -> u64 {
4655
let network = Network::config();
4756
let conn = ps_init().await;
4857

49-
let latest_archived: u64 =
50-
query("SELECT MAX(NetworkBlockId) AS LatestNetworkBlockId FROM WeaveVMArchiverMetis;")
51-
.fetch_scalar(&conn)
52-
.await
53-
.unwrap_or(network.start_block);
58+
let mut ps_table_name = get_env_var("ps_table_name").unwrap();
59+
if is_backfill {
60+
ps_table_name = format!("{}{}", ps_table_name, "Backfill")
61+
}
62+
63+
let query_str = format!(
64+
"SELECT MAX(NetworkBlockId) AS LatestNetworkBlockId FROM {};",
65+
ps_table_name
66+
);
67+
68+
let default_start_block = if is_backfill {
69+
get_env_var("backfill_start_block")
70+
.unwrap()
71+
.parse::<u64>()
72+
.unwrap()
73+
} else {
74+
network.start_block
75+
};
76+
77+
let latest_archived: u64 = query(&query_str)
78+
.fetch_scalar(&conn)
79+
.await
80+
.unwrap_or(default_start_block);
5481
// return latest archived block in planetscale + 1
5582
// so the process can start archiving from latest_archived + 1
5683
latest_archived + 1
5784
}
5885

5986
pub async fn ps_get_archived_block_txid(id: u64) -> Value {
6087
let conn = ps_init().await;
88+
let network = Network::config();
89+
let ps_table_name = get_env_var("ps_table_name").unwrap();
6190

62-
let query_formatted = format!(
63-
"SELECT WeaveVMArchiveTxid FROM WeaveVMArchiverMetis WHERE NetworkBlockId = {}",
64-
id
91+
let query_formatted_livesync = format!(
92+
"SELECT WeaveVMArchiveTxid FROM {} WHERE NetworkBlockId = {}",
93+
ps_table_name, id
94+
);
95+
96+
let query_formatted_backfill = format!(
97+
"SELECT WeaveVMArchiveTxid FROM {}Backfill WHERE NetworkBlockId = {}",
98+
ps_table_name, id
6599
);
100+
101+
// query from tables based on block id existence in the livesync of backfill
102+
let query_formatted = if id >= network.start_block {
103+
query_formatted_livesync
104+
} else {
105+
query_formatted_backfill
106+
};
107+
66108
let txid: PsGetBlockTxid = query(&query_formatted).fetch_one(&conn).await.unwrap();
67109

68110
let res = serde_json::json!(txid);
69111
res
70112
}
71113

72-
pub async fn ps_get_blocks_extremes(extreme: &str) -> Value {
114+
pub async fn ps_get_blocks_extremes(extreme: &str, is_backfill: bool) -> Value {
73115
let conn = ps_init().await;
74116

117+
let mut ps_table_name = get_env_var("ps_table_name").unwrap();
118+
119+
ps_table_name = if is_backfill {
120+
format!("{ps_table_name}Backfill")
121+
} else {
122+
ps_table_name
123+
};
124+
75125
let query_type = match extreme {
76126
"first" => "ASC",
77127
"last" => "DESC",
78128
_ => panic!("invalid extreme value. Use 'first' or 'last'."),
79129
};
80130

81131
let query_formatted = format!(
82-
"SELECT NetworkBlockId FROM WeaveVMArchiverMetis ORDER BY NetworkBlockId {} LIMIT 1;",
83-
query_type
132+
"SELECT NetworkBlockId FROM {} ORDER BY NetworkBlockId {} LIMIT 1;",
133+
ps_table_name, query_type
84134
);
85135

86136
let query: PsGetExtremeBlock = query(&query_formatted).fetch_one(&conn).await.unwrap();
@@ -89,10 +139,19 @@ pub async fn ps_get_blocks_extremes(extreme: &str) -> Value {
89139
res
90140
}
91141

92-
pub async fn ps_get_archived_blocks_count() -> PsGetTotalBlocksCount {
142+
pub async fn ps_get_archived_blocks_count() -> u64 {
93143
let conn = ps_init().await;
94-
95-
let query_formatted = "SELECT MAX(Id) FROM WeaveVMArchiverMetis;";
96-
let count: PsGetTotalBlocksCount = query(&query_formatted).fetch_one(&conn).await.unwrap();
97-
count
144+
let ps_table_name = get_env_var("ps_table_name").unwrap();
145+
146+
let query_formatted_livesync = format!("SELECT MAX(Id) FROM {};", ps_table_name);
147+
let query_formatted_backfill = format!("SELECT MAX(Id) FROM {}Backfill;", ps_table_name);
148+
let count_livesync: PsGetTotalBlocksCount = query(&query_formatted_livesync)
149+
.fetch_one(&conn)
150+
.await
151+
.unwrap();
152+
let count_backfill: PsGetTotalBlocksCount = query(&query_formatted_backfill)
153+
.fetch_one(&conn)
154+
.await
155+
.unwrap();
156+
count_livesync.count + count_backfill.count
98157
}

0 commit comments

Comments
 (0)