Skip to content

Commit

Permalink
Cranker working
Browse files Browse the repository at this point in the history
  • Loading branch information
ebatsell committed Oct 30, 2024
1 parent b11807a commit 8ea1184
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
6 changes: 5 additions & 1 deletion crankers/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ async fn main() -> anyhow::Result<(), anyhow::Error> {

// All delegations are passed along. Delegation filtering logic is handled in `VaultHandler::crank`
let mut grouped_delegations: HashMap<Pubkey, Vec<(Pubkey, VaultOperatorDelegation)>> =
HashMap::new();
HashMap::from_iter(vaults_need_update.iter().map(|(vault, _)| (*vault, vec![])));
for (pubkey, delegation) in delegations {
if vaults_need_update
.iter()
Expand All @@ -105,13 +105,16 @@ async fn main() -> anyhow::Result<(), anyhow::Error> {
}
}

info!("Updating {} vaults", vaults_need_update.len());

for (vault, mut delegations) in grouped_delegations {
// Sort by VaultOperatorDelegation index for correct cranking order
delegations.sort_by_key(|(_pubkey, delegation)| delegation.index());
let operator_pubkeys: Vec<Pubkey> = delegations
.iter()
.map(|(_pubkey, delegation)| delegation.operator)
.collect();

let operators: Vec<Pubkey> = restaking_handler
.get_operators(&operator_pubkeys)
.await?
Expand All @@ -128,6 +131,7 @@ async fn main() -> anyhow::Result<(), anyhow::Error> {
}
}

info!("Sleeping for {} seconds", args.crank_interval);
// ---------- SLEEP (crank_interval)----------
tokio::time::sleep(Duration::from_secs(args.crank_interval)).await;
}
Expand Down
33 changes: 25 additions & 8 deletions crankers/src/vault_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ use std::time::Duration;

use anyhow::Context;
use jito_bytemuck::{AccountDeserialize, Discriminator};
use jito_vault_client::instructions::{
CloseVaultUpdateStateTrackerBuilder, CrankVaultUpdateStateTrackerBuilder,
InitializeVaultUpdateStateTrackerBuilder,
use jito_vault_client::{
instructions::{
CloseVaultUpdateStateTrackerBuilder, CrankVaultUpdateStateTrackerBuilder,
InitializeVaultUpdateStateTrackerBuilder,
},
types::WithdrawalAllocationMethod,
};
use jito_vault_core::{
vault::Vault, vault_operator_delegation::VaultOperatorDelegation,
Expand Down Expand Up @@ -248,26 +251,35 @@ impl<'a> VaultHandler<'a> {
let tracker_pubkey =
VaultUpdateStateTracker::find_program_address(&self.vault_program_id, vault, epoch).0;

log::info!("Updating vault: {vault}");

// Initialize
if self.get_update_state_tracker(vault, epoch).await.is_err() {
self.initialize_vault_update_state_tracker(vault, tracker_pubkey)
.await?;
}

log::info!("Initialized tracker for vault: {vault}, tracker: {tracker_pubkey}");

// Crank
self.crank(epoch, vault, operators, tracker_pubkey).await?;

log::info!("Cranked vault: {vault}");

// Close
let tracker = self.get_update_state_tracker(vault, epoch).await?;
if tracker.all_operators_updated(operators.len() as u64)? {
self.close_vault_update_state_tracker(vault, tracker_pubkey)
if operators.is_empty() || tracker.all_operators_updated(operators.len() as u64)? {
self.close_vault_update_state_tracker(vault, epoch, tracker_pubkey)
.await?;
} else {
let context = format!(
"Cranking failed to update all operators for vault: {vault}, tracker: {tracker_pubkey}"
);
return Err(anyhow::anyhow!(context));
}

log::info!("Closed tracker for vault: {vault}");

Ok(())
}

Expand All @@ -285,7 +297,9 @@ impl<'a> VaultHandler<'a> {
init_ix_builder
.config(self.config_address)
.vault(*vault)
.vault_update_state_tracker(tracker_pubkey);
.vault_update_state_tracker(tracker_pubkey)
.payer(self.payer.pubkey())
.withdrawal_allocation_method(WithdrawalAllocationMethod::Greedy);
let mut init_ix = init_ix_builder.instruction();
init_ix.program_id = self.vault_program_id;

Expand All @@ -309,7 +323,7 @@ impl<'a> VaultHandler<'a> {
) -> anyhow::Result<()> {
let tracker = self.get_update_state_tracker(vault, epoch).await?;

if tracker.all_operators_updated(operators.len() as u64)? {
if operators.is_empty() || tracker.all_operators_updated(operators.len() as u64)? {
return Ok(());
}

Expand Down Expand Up @@ -376,13 +390,16 @@ impl<'a> VaultHandler<'a> {
pub async fn close_vault_update_state_tracker(
&self,
vault: &Pubkey,
epoch: u64,
tracker_pubkey: Pubkey,
) -> anyhow::Result<()> {
let mut close_ix_builder = CloseVaultUpdateStateTrackerBuilder::new();
close_ix_builder
.config(self.config_address)
.vault(*vault)
.vault_update_state_tracker(tracker_pubkey);
.payer(self.payer.pubkey())
.vault_update_state_tracker(tracker_pubkey)
.ncn_epoch(epoch);
let mut close_ix = close_ix_builder.instruction();
close_ix.program_id = self.vault_program_id;

Expand Down

0 comments on commit 8ea1184

Please sign in to comment.