Skip to content

Commit

Permalink
feat: add call to indexer endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
irisdv committed Sep 20, 2023
1 parent 8bdff70 commit 45635ae
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 26 deletions.
4 changes: 4 additions & 0 deletions bot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 6 additions & 0 deletions bot/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_build::configure()
.build_server(false)
.compile(&["proto/status.proto"], &["proto"])?;
Ok(())
}
34 changes: 34 additions & 0 deletions bot/proto/status.proto
Original file line number Diff line number Diff line change
@@ -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;
}
57 changes: 31 additions & 26 deletions bot/src/main.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -150,7 +155,7 @@ async fn main() {
.for_each(|(d, r)| {
logger.info(format!(
"- `Renewal: {}` by `{:#x}`",
&starknet::id::decode(*d),
&decode(*d),
r
))
});
Expand Down

0 comments on commit 45635ae

Please sign in to comment.