Skip to content

Commit

Permalink
add funds checking chronicle functionality
Browse files Browse the repository at this point in the history
unregisters if chronicle have less funds before submitting transaction
check chronicle fund in start
  • Loading branch information
haider-rs committed Jul 12, 2024
1 parent 9622ae2 commit 88e0624
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 2 deletions.
2 changes: 2 additions & 0 deletions chronicle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct ChronicleConfig {
pub timechain_keyfile: PathBuf,
pub target_url: String,
pub target_keyfile: PathBuf,
pub target_min_balance: u128,
pub tss_keyshare_cache: PathBuf,
}

Expand Down Expand Up @@ -60,6 +61,7 @@ pub async fn run_chronicle(
let task_spawner_params = TaskSpawnerParams {
tss: tss_tx,
blockchain: chain,
min_balance: config.target_min_balance,
network: subchain,
network_id: config.network_id,
url: config.target_url,
Expand Down
4 changes: 4 additions & 0 deletions chronicle/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub struct ChronicleArgs {
/// key file for connector wallet
#[clap(long)]
pub target_keyfile: PathBuf,
/// key file for connector wallet
#[clap(long)]
pub target_min_balance: u128,
/// Metadata version to use to connect to timechain node.
#[clap(long)]
pub timechain_metadata: Option<MetadataVariant>,
Expand All @@ -48,6 +51,7 @@ impl ChronicleArgs {
network_id: self.network_id,
network_keyfile: self.network_keyfile,
network_port: self.network_port,
target_min_balance: self.target_min_balance,
timechain_metadata: self.timechain_metadata.unwrap_or_default(),
timechain_url: self.timechain_url,
timechain_keyfile: self.timechain_keyfile,
Expand Down
4 changes: 4 additions & 0 deletions chronicle/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,10 @@ impl Runtime for Mock {
Ok(())
}

async fn submit_unregister_member(&self) -> Result<()> {
Ok(())
}

async fn submit_heartbeat(&self) -> Result<()> {
Ok(())
}
Expand Down
34 changes: 32 additions & 2 deletions chronicle/src/tasks/spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rosetta_client::Wallet;
use rosetta_config_ethereum::{query::GetLogs, CallResult, FilterBlockOption};
use rosetta_core::{BlockOrIdentifier, ClientEvent};
use schnorr_evm::VerifyingKey;
use std::time::Duration;
use std::{
future::Future,
num::NonZeroU64,
Expand All @@ -22,11 +23,13 @@ use time_primitives::{
};
use time_primitives::{IGateway, Msg};
use tokio::sync::Mutex;
use tokio::time::sleep;

#[derive(Clone)]
pub struct TaskSpawnerParams<S> {
pub tss: mpsc::Sender<TssSigningRequest>,
pub blockchain: String,
pub min_balance: u128,
pub network: String,
pub network_id: NetworkId,
pub url: String,
Expand All @@ -38,6 +41,7 @@ pub struct TaskSpawnerParams<S> {
pub struct TaskSpawner<S> {
tss: mpsc::Sender<TssSigningRequest>,
wallet: Arc<Wallet>,
wallet_min_balance: u128,
wallet_guard: Arc<Mutex<()>>,
substrate: S,
network_id: NetworkId,
Expand All @@ -58,13 +62,29 @@ where
)
.await?,
);
Ok(Self {

let spawner = Self {
tss: params.tss,
wallet_min_balance: params.min_balance,
wallet,
wallet_guard: Arc::new(Mutex::new(())),
substrate: params.substrate,
network_id: params.network_id,
})
};

while !spawner.is_balance_available().await? {
sleep(Duration::from_secs(10)).await;
tracing::warn!("Chronicle balance is too low, retrying...");
}

Ok(spawner)
}

///
/// Checks if wallet have enough balance
async fn is_balance_available(&self) -> Result<bool> {
let balance = self.wallet.balance().await?;
Ok(balance > self.wallet_min_balance)
}

///
Expand Down Expand Up @@ -242,6 +262,16 @@ where
}

async fn write(self, task_id: TaskId, function: Function) -> Result<()> {
let is_balance_available = self.is_balance_available().await?;
if !is_balance_available {
// unregister member
if let Err(e) = self.substrate.submit_unregister_member().await {
tracing::error!("Failed to unregister member: {:?}", e);
};
tracing::warn!("Chronicle balance too low, exiting");
std::process::exit(1);
}

let submission = async move {
match function {
Function::EvmDeploy { bytecode } => {
Expand Down
2 changes: 2 additions & 0 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ pub trait Runtime: Clone + Send + Sync + 'static {
stake_amount: u128,
) -> Result<()>;

async fn submit_unregister_member(&self) -> Result<()>;

async fn submit_heartbeat(&self) -> Result<()>;

async fn submit_commitment(
Expand Down
12 changes: 12 additions & 0 deletions tc-subxt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub trait TxSubmitter: Clone + Send + Sync + 'static {

pub enum Tx {
RegisterMember { network: NetworkId, peer_id: PeerId, stake_amount: u128 },
UnregisterMember,
Heartbeat,
Commitment { shard_id: ShardId, commitment: Commitment, proof_of_knowledge: ProofOfKnowledge },
CreateTask { task: TaskDescriptorParams },
Expand Down Expand Up @@ -129,6 +130,10 @@ impl<T: TxSubmitter> SubxtWorker<T> {
);
self.create_signed_payload(&payload).await
},
Tx::UnregisterMember => {
let payload = metadata::tx().members().unregister_member();
self.create_signed_payload(&payload).await
},
Tx::Heartbeat => {
let payload = metadata::tx().members().send_heartbeat();
self.create_signed_payload(&payload).await
Expand Down Expand Up @@ -589,6 +594,13 @@ impl Runtime for SubxtClient {
Ok(())
}

async fn submit_unregister_member(&self) -> Result<()> {
let (tx, rx) = oneshot::channel();
self.tx.unbounded_send((Tx::UnregisterMember, tx))?;
rx.await?;
Ok(())
}

async fn submit_heartbeat(&self) -> Result<()> {
let (tx, rx) = oneshot::channel();
self.tx.unbounded_send((Tx::Heartbeat, tx))?;
Expand Down

0 comments on commit 88e0624

Please sign in to comment.