Skip to content

Commit

Permalink
Add logging (#9)
Browse files Browse the repository at this point in the history
* Track timestamp

* fixes

* Add Logging

* fmt

* add logging to server

* revert paras file
  • Loading branch information
BoredApe8461 committed Dec 24, 2023
1 parent 11bad50 commit e1a634a
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 26 deletions.
40 changes: 40 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions bin/tracker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ edition = "2021"

[dependencies]
csv = "1.3.0"
log = "0.4"
env_logger = "0.10.1"
subxt = "0.32.1"
subxt-metadata = "0.32.1"
tokio = { version = "1", features = ["full"] }
Expand Down
69 changes: 50 additions & 19 deletions bin/tracker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,21 @@
//! The percentages themselves are stored by representing them as decimal numbers;
//! for example, 50.5% is stored as 0.505 with a precision of three decimals.

const LOG_TARGET: &str = "tracker";

use csv::WriterBuilder;
use shared::{file_path, parachains, round_to};
use std::fs::OpenOptions;
use subxt::{utils::H256, OnlineClient, PolkadotConfig};
use subxt::{blocks::Block, utils::H256, OnlineClient, PolkadotConfig};
use types::{Parachain, Timestamp, WeightConsumption};

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

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

// Asynchronously subscribes to follow the latest finalized block of each parachain
// and continuously fetches the weight consumption.
let tasks: Vec<_> = parachains()
Expand All @@ -68,32 +72,59 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

async fn track_weight_consumption(para: Parachain) {
if let Ok(api) = OnlineClient::<PolkadotConfig>::from_url(&para.rpc_url).await {
let mut blocks_sub = api
.blocks()
.subscribe_finalized()
.await
.expect("Failed to subscribe to finalized blocks");

// Wait for new finalized blocks, then fetch and output the weight consumption accordingly.
while let Some(Ok(block)) = blocks_sub.next().await {
let block_number = block.header().number;

// TODO: https://github.com/RegionX-Labs/CorespaceWeigher/issues/8
let timestamp = timestamp_at(api.clone(), block.hash()).await.unwrap_or_default();

if let Ok(consumption) = weight_consumption(api.clone(), block_number, timestamp).await
{
// TODO: https://github.com/RegionX-Labs/CorespaceWeigher/issues/8
let _ = write_consumption(para.clone(), consumption);
}
if let Err(err) = track_blocks(api, para).await {
log::error!(
target: LOG_TARGET,
"Failed to track new block: {:?}",
err
);
}
}
}

async fn track_blocks(
api: OnlineClient<PolkadotConfig>,
para: Parachain,
) -> Result<(), Box<dyn std::error::Error>> {
let mut blocks_sub = api
.blocks()
.subscribe_finalized()
.await
.map_err(|_| "Failed to subscribe to finalized 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?;
}

Ok(())
}

async fn note_new_block(
api: OnlineClient<PolkadotConfig>,
para: Parachain,
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)?;

Ok(())
}

fn write_consumption(
para: Parachain,
consumption: WeightConsumption,
) -> Result<(), std::io::Error> {
log::info!(
target: LOG_TARGET,
"Writing weight consumption for Para {}-{} for block: #{}",
para.relay_chain, para.para_id, consumption.block_number
);

let file_path = file_path(para);
let file = OpenOptions::new().write(true).create(true).append(true).open(file_path)?;

Expand Down
1 change: 1 addition & 0 deletions routes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"

[dependencies]
csv = "1.3.0"
log = "0.4"
rocket = { version = "0.5.0", features=["json"] }
rocket_cors = "0.6.0"
serde_json = "1.0.108"
Expand Down
2 changes: 2 additions & 0 deletions routes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use rocket::{http::Status, response::Responder, Request, Response};

const LOG_TARGET: &str = "server";

/// Web API for interacting with the Consumption Tracker service.
///
/// This API exposes two main endpoints:
Expand Down
28 changes: 21 additions & 7 deletions routes/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ use crate::*;
use rocket::{post, serde::json::Json};
use shared::{parachain, PARACHAINS};
use std::{
fs::OpenOptions,
fs::{File, OpenOptions},
io::{Read, Seek, Write},
};
use types::Parachain;

/// Register a parachain for resource utilization tracking.
#[post("/register_para", data = "<para>")]
pub fn register_para(para: Json<Parachain>) -> Result<String, Error> {
pub fn register_para(para: Json<Parachain>) -> Result<(), Error> {
let mut file = OpenOptions::new()
.read(true)
.write(true)
Expand All @@ -43,12 +43,26 @@ pub fn register_para(para: Json<Parachain>) -> Result<String, Error> {
}

paras.push(para.into_inner());
let json_data = serde_json::to_string_pretty(&paras).expect("Failed to serialize");

file.set_len(0).expect("Failed to truncate file");
file.seek(std::io::SeekFrom::Start(0)).expect("Failed to seek to the beginning");
if let Err(err) = update_paras_file(&mut file, paras) {
log::error!(
target: LOG_TARGET,
"Failed to register para: {:?}",
err
);
}

Ok(())
}

fn update_paras_file(file: &mut File, paras: Vec<Parachain>) -> Result<(), String> {
let json_data = serde_json::to_string_pretty(&paras).map_err(|_| "Failed to serialize")?;

file.set_len(0).map_err(|_| "Failed to truncate file")?;
file.seek(std::io::SeekFrom::Start(0))
.map_err(|_| "Failed to seek to the beginning")?;

file.write_all(json_data.as_bytes()).unwrap();
file.write_all(json_data.as_bytes()).map_err(|_| "Failed to write into file")?;

Ok(Default::default())
Ok(())
}

0 comments on commit e1a634a

Please sign in to comment.