diff --git a/bot/Cargo.toml b/bot/Cargo.toml index 7f52d1d..06301ba 100644 --- a/bot/Cargo.toml +++ b/bot/Cargo.toml @@ -25,4 +25,8 @@ starknet = "0.6.0" starknet-id = { git = "https://github.com/starknet-id/starknet-id.rs.git", branch = "master" } serde_derive = "1.0.183" env_logger = "0.10.0" +tonic = "0.10.0" +prost = "0.12.1" +[build-dependencies] +tonic-build = "0.10.0" diff --git a/bot/build.rs b/bot/build.rs new file mode 100644 index 0000000..89858f3 --- /dev/null +++ b/bot/build.rs @@ -0,0 +1,6 @@ +fn main() -> Result<(), Box> { + tonic_build::configure() + .build_server(false) + .compile(&["proto/status.proto"], &["proto"])?; + Ok(()) +} \ No newline at end of file diff --git a/bot/proto/status.proto b/bot/proto/status.proto new file mode 100644 index 0000000..0419b35 --- /dev/null +++ b/bot/proto/status.proto @@ -0,0 +1,34 @@ +// Apibara Sink status server +syntax = "proto3"; + +package apibara.sink.v1; + +service Status { + // Get Sink status. + rpc GetStatus(GetStatusRequest) returns (GetStatusResponse); +} + +// Request for the `GetStatus` method. +message GetStatusRequest {} + +// Response for the `GetStatus` method. +message GetStatusResponse { + // The status of the sink. + SinkStatus status = 1; + // The starting block. + optional uint64 starting_block = 2; + // The current block. + optional uint64 current_block = 3; + // The current head of the chain. + optional uint64 head_block = 4; + // The reason why the sink is not running. + optional string reason = 5; +} + +enum SinkStatus { + SINK_STATUS_UNKNOWN = 0; + // The sink is running. + SINK_STATUS_RUNNING = 1; + // The sink has errored. + SINK_STATUS_ERRORED = 2; +} \ No newline at end of file diff --git a/bot/src/main.rs b/bot/src/main.rs index f95de93..ad39365 100644 --- a/bot/src/main.rs +++ b/bot/src/main.rs @@ -1,5 +1,7 @@ use std::{borrow::Cow, sync::Arc}; +use self::status::status_client::StatusClient; +use self::status::GetStatusRequest; use bot::renew_domains; use bson::doc; use mongodb::{options::ClientOptions, Client as mongoClient}; @@ -9,9 +11,14 @@ use starknet::{ providers::Provider, signers::{LocalWallet, SigningKey}, }; +use starknet_id::decode; use starknet_utils::create_jsonrpc_client; use tokio::time::sleep; +pub mod status { + tonic::include_proto!("apibara.sink.v1"); +} + mod bot; mod config; mod indexer_status; @@ -97,42 +104,40 @@ async fn main() { starknet::accounts::ExecutionEncoding::Legacy, ); + let mut indexer_client = StatusClient::connect("http://localhost:8118") + .await + .unwrap(); + logger.info("Started"); - // todo: passed to false to now, until we have a way to check if the indexer is up to date let mut need_to_check_status = true; + loop { if need_to_check_status { logger.info("Checking indexer status"); - match indexer_status::get_status_from_endpoint(&conf).await { - Ok(block) => { - println!("Block: {}", block); - match indexer_status::check_block_status(&conf, block).await { - Ok(status) => { - if status { - need_to_check_status = false; - logger.info("Indexer is up to date, starting renewals") - } else { - tokio::time::sleep(std::time::Duration::from_secs(5)).await; - continue; - } - } - Err(error) => { - logger.severe(format!( - "Error while checking block status: {}, retrying in 5 seconds", - error - )); - tokio::time::sleep(std::time::Duration::from_secs(5)).await; - } - } + let request = tonic::Request::new(GetStatusRequest {}); + match indexer_client.get_status(request).await { + Ok(response) => { + println!("RESPONSE={:?}", response.into_inner()); + tokio::time::sleep(std::time::Duration::from_secs(5)).await; + // if status { + // need_to_check_status = false; + // logger.info("Indexer is up to date, starting renewals") + // } else { + // tokio::time::sleep(std::time::Duration::from_secs(5)).await; + // continue; + // } } Err(error) => { - println!( - "Error getting indexer status, retrying in 5 seconds: {}", + println!("ERROR={:?}", error); + logger.severe(format!( + "Error while checking block status: {}, retrying in 5 seconds", error - ); + )); tokio::time::sleep(std::time::Duration::from_secs(5)).await; } } + + tokio::time::sleep(std::time::Duration::from_secs(5)).await; } else { println!("[bot] Checking domains to renew"); match bot::get_domains_ready_for_renewal(&conf, &shared_state, &logger).await { @@ -150,7 +155,7 @@ async fn main() { .for_each(|(d, r)| { logger.info(format!( "- `Renewal: {}` by `{:#x}`", - &starknet::id::decode(*d), + &decode(*d), r )) });