Skip to content

Commit

Permalink
Support multiple RPCs (#21)
Browse files Browse the repository at this point in the history
* Support multiple RPCs

* compiles

* error handling

* tests passing

* separate output directories

* edit configuration

* update scripts

* update readme
  • Loading branch information
Szegoo authored Feb 3, 2024
1 parent 4c056e1 commit 5ce0038
Show file tree
Hide file tree
Showing 26 changed files with 1,684 additions and 112 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/out
logs/
101 changes: 101 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,7 @@ A basic example of registering a parachain:

```
curl -X POST http://127.0.0.1:8000/register_para -H "Content-Type: application/json" -d '{
"para": {
"name": "Acala",
"rpc_url": "wss://acala-rpc.dwellir.com",
"para_id": 2000,
"relay_chain": "Polkadot"
}
"para": ["Polkadot", 2000]
}'
```

Expand Down
1 change: 1 addition & 0 deletions bin/tracker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ tokio = { version = "1", features = ["full"] }

types = { path = "../../types" }
shared = { path = "../../shared" }
clap = { version = "4.4.18", features = ["derive"] }
13 changes: 13 additions & 0 deletions bin/tracker/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use clap::Parser;

/// Arguments for the tracker.
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Args {
/// Specifies the index of the RPC to be used.
///
/// Multiple RPCs may be provided for each parachain on Kusama and Polkadot.
/// `rpc_index` selects which RPC from the list will be used.
#[arg(short, long)]
pub rpc_index: usize,
}
42 changes: 35 additions & 7 deletions bin/tracker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
//! The data stored is the 2D weight consumption per each dispatch class.
//! The data is stored in the CSV file within the following sequence:
//!
//! | block_number | timestamp | normal_dispatch_ref_time | operational_dispatch_ref_time | mandatory_dispatch_ref_time | normal_proof_size | operational_proof_size | mandatory_proof_size |
//! | block_number | timestamp | normal_dispatch_ref_time | operational_dispatch_ref_time | mandatory_dispatch_ref_time | normal_proof_size | operational_proof_size | mandatory_proof_size |
//! |--------------|-----------------------|---------------------------|-------------------------------|-----------------------------|-------------------|-------------------------|-----------------------|
//! | ... | ... | ... | ... | ... | ... | ... | ... |
//!
Expand All @@ -43,22 +43,29 @@

const LOG_TARGET: &str = "tracker";

use clap::Parser;
use shared::{consumption::write_consumption, registry::registered_paras, round_to};
use subxt::{blocks::Block, utils::H256, OnlineClient, PolkadotConfig};
use types::{Parachain, Timestamp, WeightConsumption};

mod cli;

#[subxt::subxt(runtime_metadata_path = "../../artifacts/metadata.scale")]
mod polkadot {}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();

let args = cli::Args::parse();

// Asynchronously subscribes to follow the latest finalized block of each parachain
// and continuously fetches the weight consumption.
let tasks: Vec<_> = registered_paras()
.into_iter()
.map(|para| tokio::spawn(async move { track_weight_consumption(para).await }))
.map(|para| {
tokio::spawn(async move { track_weight_consumption(para, args.rpc_index).await })
})
.collect();

for task in tasks {
Expand All @@ -68,22 +75,42 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

async fn track_weight_consumption(para: Parachain) {
if let Ok(api) = OnlineClient::<PolkadotConfig>::from_url(&para.rpc_url).await {
if let Err(err) = track_blocks(api, para).await {
async fn track_weight_consumption(para: Parachain, rpc_index: usize) {
let Some(rpc) = para.rpcs.get(rpc_index) else {
log::error!(
target: LOG_TARGET,
"Parachain {}-{} doesn't have an rpc with index: {}",
para.relay_chain, para.para_id, rpc_index,
);
return;
};

log::info!("Starting to track consumption for: {}-{}", para.relay_chain, para.para_id);
let result = OnlineClient::<PolkadotConfig>::from_url(rpc).await;

if let Ok(api) = result {
if let Err(err) = track_blocks(api, para, rpc_index).await {
log::error!(
target: LOG_TARGET,
"Failed to track new block: {:?}",
err
);
}
} else {
log::error!(
target: LOG_TARGET,
"Failed to create online client: {:?}",
result
);
}
}

async fn track_blocks(
api: OnlineClient<PolkadotConfig>,
para: Parachain,
rpc_index: usize,
) -> Result<(), Box<dyn std::error::Error>> {
log::info!("Subsciribing to finalized blocks for: {}", para.para_id);
let mut blocks_sub = api
.blocks()
.subscribe_finalized()
Expand All @@ -92,7 +119,7 @@ async fn track_blocks(

// Wait for new finalized blocks, then fetch and output the weight consumption accordingly.
while let Some(Ok(block)) = blocks_sub.next().await {
note_new_block(api.clone(), para.clone(), block).await?;
note_new_block(api.clone(), para.clone(), rpc_index, block).await?;
}

Ok(())
Expand All @@ -101,14 +128,15 @@ async fn track_blocks(
async fn note_new_block(
api: OnlineClient<PolkadotConfig>,
para: Parachain,
rpc_index: usize,
block: Block<PolkadotConfig, OnlineClient<PolkadotConfig>>,
) -> Result<(), Box<dyn std::error::Error>> {
let block_number = block.header().number;

let timestamp = timestamp_at(api.clone(), block.hash()).await?;
let consumption = weight_consumption(api, block_number, timestamp).await?;

write_consumption(para, consumption)?;
write_consumption(para, consumption, rpc_index)?;

Ok(())
}
Expand Down
Loading

0 comments on commit 5ce0038

Please sign in to comment.