Skip to content

Commit

Permalink
wip wtf
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalu committed Jul 31, 2024
1 parent 4cbdec1 commit 545f345
Show file tree
Hide file tree
Showing 16 changed files with 1,094 additions and 391 deletions.
446 changes: 313 additions & 133 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ jito-vault-sdk = { path = "vault_sdk", version = "=0.0.1" }
jito-vault-program = { path = "vault_program", version = "=0.0.1" }
jito-restaking-program = { path = "restaking_program", version = "=0.0.1" }
shank = "0.4.2"
solana-program = "~1.17"
solana-program-test = "~1.17"
solana-sdk = "~1.17"
solana-program = "~1.18"
solana-program-test = "~1.18"
solana-sdk = "~1.18"
solana-security-txt = "1.1.1"
spl-token = { version = "4.0.0", features = ["no-entrypoint"] }
spl-associated-token-account = { version = "2.2.0", features = ["no-entrypoint"] }
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ spl-associated-token-account = { workspace = true }
spl-token = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
[dependencies]
log = "0.4.21"
108 changes: 73 additions & 35 deletions integration_tests/tests/fixtures/fixture.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use std::fmt::{Debug, Formatter};

use borsh::BorshSerialize;
use solana_program::{
clock::Clock,
native_token::sol_to_lamports,
program_pack::Pack,
pubkey::Pubkey,
rent::Rent,
system_instruction::{create_account, transfer},
};
use solana_program_test::{processor, BanksClientError, ProgramTest, ProgramTestContext};
use solana_program_test::{
processor, BanksClient, BanksClientError, ProgramTest, ProgramTestContext,

Check failure on line 11 in integration_tests/tests/fixtures/fixture.rs

View workflow job for this annotation

GitHub Actions / coverage

unused import: `BanksClient`

Check failure on line 11 in integration_tests/tests/fixtures/fixture.rs

View workflow job for this annotation

GitHub Actions / cargo test

unused import: `BanksClient`
};
use solana_sdk::{
account::AccountSharedData,
commitment_config::CommitmentLevel,
signature::{Keypair, Signer},
transaction::Transaction,
Expand All @@ -20,6 +23,8 @@ use spl_token::{
instruction::initialize_mint2,
state::{Account, Mint},
};
use std::fmt::{Debug, Formatter};
use std::sync::Arc;

Check failure on line 27 in integration_tests/tests/fixtures/fixture.rs

View workflow job for this annotation

GitHub Actions / coverage

unused import: `std::sync::Arc`

Check failure on line 27 in integration_tests/tests/fixtures/fixture.rs

View workflow job for this annotation

GitHub Actions / cargo test

unused import: `std::sync::Arc`

use crate::fixtures::{restaking_client::RestakingProgramClient, vault_client::VaultProgramClient};

Expand Down Expand Up @@ -51,24 +56,56 @@ impl TestBuilder {
Self { context }
}

// pub async fn store_account<T: BorshSerialize>(
// &mut self,
// pubkey: &Pubkey,
// owner: &Pubkey,
// data: &T,
// ) -> Result<(), BanksClientError> {
// let rent: Rent = self.context.banks_client.get_sysvar().await?;
//
// let serialized = data.try_to_vec().unwrap();
// let mut data = AccountSharedData::new(
// rent.minimum_balance(serialized.len()),
// serialized.len(),
// owner,
// );
// data.set_data_from_slice(serialized.as_slice());
// self.context.set_account(pubkey, &data);
// Ok(())
// }
pub async fn store_borsh_account<T: BorshSerialize>(

Check failure on line 59 in integration_tests/tests/fixtures/fixture.rs

View workflow job for this annotation

GitHub Actions / coverage

methods `store_borsh_account`, `get_account`, `store_account`, and `warp_to_next_slot` are never used

Check failure on line 59 in integration_tests/tests/fixtures/fixture.rs

View workflow job for this annotation

GitHub Actions / cargo test

methods `store_borsh_account`, `get_account`, `store_account`, and `warp_to_next_slot` are never used
&mut self,
pubkey: &Pubkey,
owner: &Pubkey,
data: &T,
) -> Result<(), BanksClientError> {
let rent: Rent = self.context.banks_client.get_sysvar().await?;

let serialized = data.try_to_vec().unwrap();
let mut data = AccountSharedData::new(
rent.minimum_balance(serialized.len()),
serialized.len(),
owner,
);
data.set_data_from_slice(serialized.as_slice());
self.context.set_account(pubkey, &data);
Ok(())
}

pub async fn get_account(
&mut self,
pubkey: &Pubkey,
) -> Result<solana_sdk::account::Account, BanksClientError> {
let account = self
.context
.banks_client
.get_account_with_commitment(*pubkey, CommitmentLevel::Processed)
.await?
.unwrap();
Ok(account)
}

pub async fn store_account(
&mut self,
pubkey: &Pubkey,
owner: &Pubkey,
data: &[u8],
) -> Result<(), BanksClientError> {
let rent: Rent = self.context.banks_client.get_sysvar().await?;

let serialized = data.to_vec();
let mut data = AccountSharedData::new(
rent.minimum_balance(serialized.len()),
serialized.len(),
owner,
);
data.set_data_from_slice(serialized.as_slice());
self.context.set_account(pubkey, &data);
Ok(())
}

pub async fn transfer(&mut self, to: &Pubkey, sol: f64) -> Result<(), BanksClientError> {
let blockhash = self.context.banks_client.get_latest_blockhash().await?;
Expand All @@ -90,11 +127,6 @@ impl TestBuilder {
.await
}

// pub async fn get_mint(&mut self, mint: &Pubkey) -> Result<Mint, BanksClientError> {
// let account = self.context.banks_client.get_account(*mint).await?.unwrap();
// Ok(Mint::unpack(&account.data).unwrap())
// }

pub async fn get_token_account(
&mut self,
token_account: &Pubkey,
Expand Down Expand Up @@ -204,19 +236,25 @@ impl TestBuilder {
.await
}

// pub async fn warp_to_next_slot(&mut self) -> Result<(), BanksClientError> {
// let clock: Clock = self.context.banks_client.get_sysvar().await?;
// self.context
// .warp_to_slot(clock.slot.checked_add(1).unwrap())
// .map_err(|_| BanksClientError::ClientError("failed to warp slot"))?;
// Ok(())
// }
pub async fn warp_to_next_slot(&mut self) -> Result<(), BanksClientError> {
let clock: Clock = self.context.banks_client.get_sysvar().await?;
self.context
.warp_to_slot(clock.slot.checked_add(1).unwrap())
.map_err(|_| BanksClientError::ClientError("failed to warp slot"))?;
Ok(())
}

pub fn vault_program_client(&self) -> VaultProgramClient {
VaultProgramClient::new(self.context.banks_client.clone())
VaultProgramClient::new(
self.context.banks_client.clone(),
self.context.payer.insecure_clone(),
)
}

pub fn restaking_program_client(&self) -> RestakingProgramClient {
RestakingProgramClient::new(self.context.banks_client.clone())
RestakingProgramClient::new(
self.context.banks_client.clone(),
self.context.payer.insecure_clone(),
)
}
}
93 changes: 89 additions & 4 deletions integration_tests/tests/fixtures/restaking_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,38 @@ use jito_restaking_sdk::{
avs_add_operator, avs_add_vault, avs_add_vault_slasher, initialize_avs, initialize_config,
initialize_operator, operator_add_avs, operator_add_vault,
};
use solana_program::pubkey::Pubkey;
use solana_program::{native_token::sol_to_lamports, pubkey::Pubkey, system_instruction::transfer};
use solana_program_test::{BanksClient, BanksClientError};
use solana_sdk::{
commitment_config::CommitmentLevel,
signature::{Keypair, Signer},
transaction::Transaction,
};

pub struct AvsRoot {

Check failure on line 20 in integration_tests/tests/fixtures/restaking_client.rs

View workflow job for this annotation

GitHub Actions / coverage

struct `AvsRoot` is never constructed

Check failure on line 20 in integration_tests/tests/fixtures/restaking_client.rs

View workflow job for this annotation

GitHub Actions / cargo test

struct `AvsRoot` is never constructed
pub avs_pubkey: Pubkey,
pub avs_admin: Keypair,
}

pub struct RestakingProgramClient {
banks_client: BanksClient,
payer: Keypair,
}

impl RestakingProgramClient {
pub const fn new(banks_client: BanksClient) -> Self {
Self { banks_client }
pub const fn new(banks_client: BanksClient, payer: Keypair) -> Self {
Self {
banks_client,
payer,
}
}

pub async fn get_avs(&mut self, avs: &Pubkey) -> Result<Avs, BanksClientError> {
let account = self.banks_client.get_account(*avs).await?.unwrap();
let account = self
.banks_client
.get_account_with_commitment(*avs, CommitmentLevel::Processed)
.await?
.unwrap();

Ok(Avs::deserialize(&mut account.data.as_slice())?)
}
Expand Down Expand Up @@ -117,6 +130,17 @@ impl RestakingProgramClient {
)?)
}

pub async fn setup_config(&mut self) -> Result<Keypair, BanksClientError> {
let restaking_config_pubkey = Config::find_program_address(&jito_restaking_program::id()).0;
let restaking_config_admin = Keypair::new();

self._airdrop(&restaking_config_admin.pubkey(), 1.0).await?;
self.initialize_config(&restaking_config_pubkey, &restaking_config_admin)
.await?;

Ok(restaking_config_admin)
}

pub async fn initialize_config(
&mut self,
config: &Pubkey,
Expand All @@ -137,6 +161,52 @@ impl RestakingProgramClient {
.await
}

pub async fn setup_avs(&mut self) -> Result<AvsRoot, BanksClientError> {
let avs_admin = Keypair::new();
let avs_base = Keypair::new();

self._airdrop(&avs_admin.pubkey(), 1.0).await?;

let avs_pubkey =
Avs::find_program_address(&jito_restaking_program::id(), &avs_base.pubkey()).0;
self.initialize_avs(
&Config::find_program_address(&jito_restaking_program::id()).0,
&avs_pubkey,
&avs_admin,
&avs_base,
)
.await
.unwrap();

Ok(AvsRoot {
avs_pubkey,
avs_admin,
})
}

pub async fn avs_vault_opt_in(
&mut self,
avs_root: &AvsRoot,
vault: &Pubkey,
) -> Result<(), BanksClientError> {
let avs_vault_ticket = AvsVaultTicket::find_program_address(
&jito_restaking_program::id(),
&avs_root.avs_pubkey,
vault,
)
.0;

self.avs_add_vault(
&Config::find_program_address(&jito_restaking_program::id()).0,
&avs_root.avs_pubkey,
vault,
&avs_vault_ticket,
&avs_root.avs_admin,
&self.payer.insecure_clone(),
)
.await
}

pub async fn initialize_avs(
&mut self,
config: &Pubkey,
Expand Down Expand Up @@ -623,4 +693,19 @@ impl RestakingProgramClient {
)
.await
}

pub async fn _airdrop(&mut self, to: &Pubkey, sol: f64) -> Result<(), BanksClientError> {
let blockhash = self.banks_client.get_latest_blockhash().await?;
self.banks_client
.process_transaction_with_preflight_and_commitment(
Transaction::new_signed_with_payer(
&[transfer(&self.payer.pubkey(), to, sol_to_lamports(sol))],
Some(&self.payer.pubkey()),
&[&self.payer],
blockhash,
),
CommitmentLevel::Processed,
)
.await
}
}
Loading

0 comments on commit 545f345

Please sign in to comment.