Skip to content

Commit

Permalink
creating weight table
Browse files Browse the repository at this point in the history
  • Loading branch information
coachchucksol committed Oct 11, 2024
1 parent 27f1141 commit db8f6e8
Show file tree
Hide file tree
Showing 10 changed files with 727 additions and 530 deletions.
1,035 changes: 593 additions & 442 deletions Cargo.lock

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ jito-weight-table-sdk = { path = "./weight_table_sdk", version="0.0.1"}
jito-reward-core = { path = "./reward_core", version = "=0.0.1" }
jito-reward-program = { path = "./reward_program", version = "=0.0.1" }
jito-reward-sdk = { path = "./reward_sdk", version = "=0.0.1" }
jito-bytemuck = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.1" }
jito-account-traits-derive = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.1" }
jito-jsm-core = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.1" }
jito-restaking-client = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.1" }
jito-restaking-core = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.1" }
jito-restaking-program = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.1" }
jito-restaking-sdk = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.1" }
jito-vault-client = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.1" }
jito-vault-core = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.1" }
jito-vault-program = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.1" }
jito-vault-sdk = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.1" }
jito-bytemuck = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.2" }
jito-account-traits-derive = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.2" }
jito-jsm-core = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.2" }
jito-restaking-client = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.2" }
jito-restaking-core = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.2" }
jito-restaking-program = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.2" }
jito-restaking-sdk = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.2" }
jito-vault-client = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.2" }
jito-vault-core = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.2" }
jito-vault-program = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.2" }
jito-vault-sdk = { git = "https://github.com/jito-foundation/restaking.git", version = "=0.0.2" }
1 change: 1 addition & 0 deletions weight_table_core/src/discriminators.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const WEIGHT_TABLE_DISCRIMINATOR: u8 = 0x01;
3 changes: 3 additions & 0 deletions weight_table_core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ pub enum WeightTableError {
ArithmeticOverflow = 0x2101,
#[error("Modulo Overflow")]
ModuloOverflow = 0x2102,

#[error("Incorrect weight table admin")]
IncorrectWeightTableAdmin = 0x2200,
}

impl<T> DecodeError<T> for WeightTableError {
Expand Down
1 change: 1 addition & 0 deletions weight_table_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// pub mod redacted_config;
pub mod discriminators;
pub mod error;
pub mod weight;
pub mod weight_table;
44 changes: 0 additions & 44 deletions weight_table_core/src/redacted_config.rs

This file was deleted.

20 changes: 16 additions & 4 deletions weight_table_core/src/weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Weight {
self.numerator() == 0
}

fn gcd(&self) -> Result<u64, WeightTableError> {
fn greatest_common_denominator(&self) -> Result<u64, WeightTableError> {
let mut n: u64 = self.numerator();
let mut d: u64 = self.denominator();

Expand All @@ -60,7 +60,7 @@ impl Weight {
}

fn simplify(&self) -> Result<Self, WeightTableError> {
let gcd_value = self.gcd()?;
let gcd_value = self.greatest_common_denominator()?;

if gcd_value == 1 {
return Ok(*self);
Expand Down Expand Up @@ -282,8 +282,20 @@ mod tests {

#[test]
fn test_gcd() {
assert_eq!(Weight::new(6, 8).unwrap().gcd().unwrap(), 2);
assert_eq!(Weight::new(17, 23).unwrap().gcd().unwrap(), 1);
assert_eq!(
Weight::new(6, 8)
.unwrap()
.greatest_common_denominator()
.unwrap(),
2
);
assert_eq!(
Weight::new(17, 23)
.unwrap()
.greatest_common_denominator()
.unwrap(),
1
);

let bad_weight = Weight::new(1, 0).unwrap_err();
assert!(matches!(bad_weight, WeightTableError::DenominatorIsZero));
Expand Down
51 changes: 28 additions & 23 deletions weight_table_core/src/weight_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,58 @@ use jito_bytemuck::{types::PodU64, AccountDeserialize, Discriminator};
use shank::{ShankAccount, ShankType};
use solana_program::pubkey::Pubkey;

use crate::{error::WeightTableError, weight::Weight};
use crate::{discriminators::WEIGHT_TABLE_DISCRIMINATOR, error::WeightTableError, weight::Weight};

// PDA'd ["WEIGHT_TABLE", NCN, NCN_EPOCH_SLOT]
#[derive(
Debug, Clone, Copy, Zeroable, ShankType, Pod, Default, AccountDeserialize, ShankAccount,
)]
#[derive(Debug, Clone, Copy, Zeroable, ShankType, Pod, AccountDeserialize, ShankAccount)]
#[repr(C)]
pub struct WeightTable {
/// The NCN on-chain program is the signer to create and update this account,
/// this pushes the responsibility of managing the account to the NCN program.
pub ncn: Pubkey,

/// The slot starting the NCN epoch, the epoch length is determined by the restaking program config.
pub ncn_epoch_slot: PodU64,
/// The NCN epoch for which the weight table is valid
pub ncn_epoch: PodU64,

/// Anything non-zero means the table is finalized and cannot be updated.
pub finalized: u8,
finalized: u8,

/// Bump seed for the PDA
pub bump: u8,

/// Reserved space
reserved: [u8; 128],

/// The weight table
pub table: [WeightEntry; 32],
}

impl Discriminator for WeightTable {
const DISCRIMINATOR: u8 = 2;
const DISCRIMINATOR: u8 = WEIGHT_TABLE_DISCRIMINATOR;
}

impl WeightTable {
pub const MAX_TABLE_ENTRIES: usize = 32;
pub const NOT_FINALIZED: u8 = 0;
pub const FINALIZED: u8 = 0xFF;

pub fn new(ncn: Pubkey, ncn_epoch_slot: u64) -> Self {
pub fn new(ncn: Pubkey, ncn_epoch: u64, bump: u8) -> Self {
Self {
ncn,
ncn_epoch_slot: PodU64::from(ncn_epoch_slot),
ncn_epoch: PodU64::from(ncn_epoch),
finalized: Self::NOT_FINALIZED,
bump,
reserved: [0; 128],
table: [WeightEntry::default(); Self::MAX_TABLE_ENTRIES],
}
}

pub fn seeds(ncn: &Pubkey, ncn_epoch_slot: u64) -> Vec<Vec<u8>> {
pub fn seeds(ncn: &Pubkey, ncn_epoch: u64) -> Vec<Vec<u8>> {
Vec::from_iter(
[
b"WEIGHT_TABLE".to_vec(),
ncn.to_bytes().to_vec(),
ncn_epoch_slot.to_le_bytes().to_vec(),
ncn_epoch.to_le_bytes().to_vec(),
]
.iter()
.cloned(),
Expand All @@ -57,9 +64,9 @@ impl WeightTable {
pub fn find_program_address(
program_id: &Pubkey,
ncn: &Pubkey,
ncn_epoch_slot: u64,
ncn_epoch: u64,
) -> (Pubkey, u8, Vec<Vec<u8>>) {
let seeds = Self::seeds(ncn, ncn_epoch_slot);
let seeds = Self::seeds(ncn, ncn_epoch);
let seeds_iter: Vec<_> = seeds.iter().map(|s| s.as_slice()).collect();
let (pda, bump) = Pubkey::find_program_address(&seeds_iter, program_id);
(pda, bump, seeds)
Expand All @@ -85,6 +92,7 @@ impl WeightTable {
match entry {
Some(entry) => {
entry.weight = weight.into();

if entry.mint == Pubkey::default() {
entry.mint = *mint;
}
Expand All @@ -95,11 +103,8 @@ impl WeightTable {
Ok(())
}

pub fn is_finalized(&self, current_slot: u64, epoch_length: u64) -> bool {
let finalized = self.finalized != Self::NOT_FINALIZED;
let epoch_over = current_slot >= u64::from(self.ncn_epoch_slot) + epoch_length;

finalized || epoch_over
pub fn finalized(&self) -> bool {
self.finalized != Self::NOT_FINALIZED
}

pub fn finalize(&mut self) {
Expand Down Expand Up @@ -133,15 +138,15 @@ mod tests {
#[test]
fn test_weight_table_new() {
let ncn = Pubkey::new_unique();
let table = WeightTable::new(ncn, 0);
let table = WeightTable::new(ncn, 0, 0);
assert_eq!(table.entry_count(), 0);
}

#[test]
fn test_weight_table_entry_count() {
let ncn = Pubkey::new_unique();

let mut table = WeightTable::new(ncn, 0);
let mut table = WeightTable::new(ncn, 0, 0);
let mint1 = Pubkey::new_unique();
let mint2 = Pubkey::new_unique();

Expand All @@ -160,7 +165,7 @@ mod tests {
fn test_weight_table_find_weight() {
let ncn = Pubkey::new_unique();

let mut table = WeightTable::new(ncn, 0);
let mut table = WeightTable::new(ncn, 0, 0);
let mint1 = Pubkey::new_unique();
let mint2 = Pubkey::new_unique();

Expand All @@ -180,7 +185,7 @@ mod tests {
fn test_weight_table_set_weight() {
let ncn = Pubkey::new_unique();

let mut table = WeightTable::new(ncn, 0);
let mut table = WeightTable::new(ncn, 0, 0);
let mint = Pubkey::new_unique();

// Set initial weight
Expand Down
1 change: 1 addition & 0 deletions weight_table_program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jito-vault-core = { workspace = true }
jito-restaking-sdk = { workspace = true }
jito-restaking-core= { workspace = true }
jito-restaking-program = { workspace = true }
jito-weight-table-core = { workspace = true }
shank = { workspace = true }
solana-program = { workspace = true }
spl-associated-token-account = { workspace = true }
Expand Down
79 changes: 73 additions & 6 deletions weight_table_program/src/initialize_weight_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,86 @@ use jito_jsm_core::{
create_account,
loader::{load_signer, load_system_account, load_system_program},
};
use jito_restaking_core::ncn::Ncn;
use jito_restaking_program::ID as RESTAKING_PROGRAM_ID;
use jito_reward_core::reward_config::RewardConfig;
use jito_restaking_core::{config::Config, ncn::Ncn};
use jito_weight_table_core::{error::WeightTableError, weight_table::WeightTable};
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, msg, program_error::ProgramError,
pubkey::Pubkey, rent::Rent, sysvar::Sysvar,
account_info::AccountInfo, clock::Clock, entrypoint::ProgramResult, msg,
program_error::ProgramError, pubkey::Pubkey, rent::Rent, sysvar::Sysvar,
};

/// Initializes a Weight Table
pub fn process_initialize_weight_table(
program_id: &Pubkey,
accounts: &[AccountInfo],
) -> ProgramResult {
//TODO
let [restaking_config, ncn, weight_table, weight_table_admin, restaking_program_id, system_program] =
accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
};
Config::load(restaking_program_id.key, restaking_config, false)?;
let ncn_epoch_length = {
let config_data = restaking_config.data.borrow();
let config = Config::try_from_slice_unchecked(&config_data)?;
config.epoch_length()
};

Ncn::load(restaking_program_id.key, ncn, false)?;
let ncn_weight_table_admin = {
//TODO switch to weight table admin when that is merged
let ncn_data = ncn.data.borrow();
let ncn = Ncn::try_from_slice_unchecked(&ncn_data)?;
ncn.admin
};

load_system_account(weight_table, true)?;
load_signer(weight_table_admin, true)?;
load_system_program(system_program)?;

if restaking_program_id.key.ne(&jito_restaking_program::id()) {
msg!("Incorrect restaking program ID");
return Err(ProgramError::InvalidAccountData);
}

if ncn_weight_table_admin.ne(&weight_table_admin.key) {
msg!("Vault update delegations ticket is not at the correct PDA");
return Err(WeightTableError::IncorrectWeightTableAdmin.into());
}

let current_slot = Clock::get()?.slot;
let ncn_epoch = current_slot
.checked_div(ncn_epoch_length)
.ok_or(WeightTableError::DenominatorIsZero)?;

let (weight_table_pubkey, weight_table_bump, mut weight_table_seeds) =
WeightTable::find_program_address(program_id, ncn.key, ncn_epoch);
weight_table_seeds.push(vec![weight_table_bump]);

if weight_table_pubkey.ne(weight_table.key) {
msg!("Incorrect weight table PDA");
return Err(ProgramError::InvalidAccountData);
}

msg!(
"Initializing Weight Table {} for NCN: {} at epoch: {}",
weight_table.key,
ncn.key,
ncn_epoch
);
create_account(
weight_table_admin,
weight_table,
system_program,
program_id,
&Rent::get()?,
8_u64.checked_add(size_of::<WeightTable>() as u64).unwrap(),
&weight_table_seeds,
)?;

let mut weight_table_data = weight_table.try_borrow_mut_data()?;
weight_table_data[0] = WeightTable::DISCRIMINATOR;
let weight_table_account = WeightTable::try_from_slice_unchecked_mut(&mut weight_table_data)?;
*weight_table_account = WeightTable::new(*ncn.key, ncn_epoch, weight_table_bump);

Ok(())
}

0 comments on commit db8f6e8

Please sign in to comment.