Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resize accounts and fix serde issue #19

Merged
merged 5 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
711 changes: 482 additions & 229 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ rand = "0.8.5"
serde = { version = "^1.0", features = ["derive"] }
serde_json = "1.0.102"
serde_with = "3.9.0"
serde-big-array = "0.5.1"
shank = "0.4.2"
shank_idl = "0.4.2"
spl-math = { version = "0.3.0", features = ["no-entrypoint"] }
Expand Down
8 changes: 4 additions & 4 deletions clients/js/jito_tip_router/accounts/ballotBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ export function getBallotBoxEncoder(): Encoder<BallotBoxArgs> {
['operatorsVoted', getU64Encoder()],
['uniqueBallots', getU64Encoder()],
['winningBallot', getBallotEncoder()],
['operatorVotes', getArrayEncoder(getOperatorVoteEncoder(), { size: 16 })],
['ballotTallies', getArrayEncoder(getBallotTallyEncoder(), { size: 16 })],
['operatorVotes', getArrayEncoder(getOperatorVoteEncoder(), { size: 256 })],
['ballotTallies', getArrayEncoder(getBallotTallyEncoder(), { size: 256 })],
]);
}

Expand All @@ -108,8 +108,8 @@ export function getBallotBoxDecoder(): Decoder<BallotBox> {
['operatorsVoted', getU64Decoder()],
['uniqueBallots', getU64Decoder()],
['winningBallot', getBallotDecoder()],
['operatorVotes', getArrayDecoder(getOperatorVoteDecoder(), { size: 16 })],
['ballotTallies', getArrayDecoder(getBallotTallyDecoder(), { size: 16 })],
['operatorVotes', getArrayDecoder(getOperatorVoteDecoder(), { size: 256 })],
['ballotTallies', getArrayDecoder(getBallotTallyDecoder(), { size: 256 })],
]);
}

Expand Down
4 changes: 2 additions & 2 deletions clients/js/jito_tip_router/accounts/baseRewardRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function getBaseRewardRouterEncoder(): Encoder<BaseRewardRouterArgs> {
],
[
'ncnFeeGroupRewardRoutes',
getArrayEncoder(getNcnRewardRouteEncoder(), { size: 32 }),
getArrayEncoder(getNcnRewardRouteEncoder(), { size: 256 }),
],
]);
}
Expand All @@ -122,7 +122,7 @@ export function getBaseRewardRouterDecoder(): Decoder<BaseRewardRouter> {
],
[
'ncnFeeGroupRewardRoutes',
getArrayDecoder(getNcnRewardRouteDecoder(), { size: 32 }),
getArrayDecoder(getNcnRewardRouteDecoder(), { size: 256 }),
],
]);
}
Expand Down
4 changes: 2 additions & 2 deletions clients/js/jito_tip_router/accounts/ncnRewardRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export function getNcnRewardRouterEncoder(): Encoder<NcnRewardRouterArgs> {
['reserved', getArrayEncoder(getU8Encoder(), { size: 128 })],
[
'vaultRewardRoutes',
getArrayEncoder(getVaultRewardRouteEncoder(), { size: 32 }),
getArrayEncoder(getVaultRewardRouteEncoder(), { size: 64 }),
],
]);
}
Expand All @@ -114,7 +114,7 @@ export function getNcnRewardRouterDecoder(): Decoder<NcnRewardRouter> {
['reserved', getArrayDecoder(getU8Decoder(), { size: 128 })],
[
'vaultRewardRoutes',
getArrayDecoder(getVaultRewardRouteDecoder(), { size: 32 }),
getArrayDecoder(getVaultRewardRouteDecoder(), { size: 64 }),
],
]);
}
Expand Down
4 changes: 2 additions & 2 deletions clients/js/jito_tip_router/accounts/operatorSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export function getOperatorSnapshotEncoder(): Encoder<OperatorSnapshotArgs> {
['reserved', getArrayEncoder(getU8Encoder(), { size: 256 })],
[
'vaultOperatorStakeWeight',
getArrayEncoder(getVaultOperatorStakeWeightEncoder(), { size: 32 }),
getArrayEncoder(getVaultOperatorStakeWeightEncoder(), { size: 64 }),
],
]);
}
Expand All @@ -134,7 +134,7 @@ export function getOperatorSnapshotDecoder(): Decoder<OperatorSnapshot> {
['reserved', getArrayDecoder(getU8Decoder(), { size: 256 })],
[
'vaultOperatorStakeWeight',
getArrayDecoder(getVaultOperatorStakeWeightDecoder(), { size: 32 }),
getArrayDecoder(getVaultOperatorStakeWeightDecoder(), { size: 64 }),
],
]);
}
Expand Down
8 changes: 4 additions & 4 deletions clients/js/jito_tip_router/accounts/trackedMints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export function getTrackedMintsEncoder(): Encoder<TrackedMintsArgs> {
['discriminator', getU64Encoder()],
['ncn', getAddressEncoder()],
['bump', getU8Encoder()],
['reserved', getArrayEncoder(getU8Encoder(), { size: 7 })],
['stMintList', getArrayEncoder(getMintEntryEncoder(), { size: 16 })],
['reserved', getArrayEncoder(getU8Encoder(), { size: 127 })],
['stMintList', getArrayEncoder(getMintEntryEncoder(), { size: 64 })],
]);
}

Expand All @@ -72,8 +72,8 @@ export function getTrackedMintsDecoder(): Decoder<TrackedMints> {
['discriminator', getU64Decoder()],
['ncn', getAddressDecoder()],
['bump', getU8Decoder()],
['reserved', getArrayDecoder(getU8Decoder(), { size: 7 })],
['stMintList', getArrayDecoder(getMintEntryDecoder(), { size: 16 })],
['reserved', getArrayDecoder(getU8Decoder(), { size: 127 })],
['stMintList', getArrayDecoder(getMintEntryDecoder(), { size: 64 })],
]);
}

Expand Down
1 change: 1 addition & 0 deletions clients/rust/jito_tip_router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bytemuck = { workspace = true }
num-derive = { workspace = true }
num-traits = { workspace = true }
serde = { workspace = true }
serde-big-array = { workspace = true }
serde_with = { workspace = true }
solana-program = { workspace = true }
solana-sdk = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ pub struct BallotBox {
pub bump: u8,
pub slot_created: u64,
pub slot_consensus_reached: u64,
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::Bytes>"))]
#[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))]
pub reserved: [u8; 128],
pub operators_voted: u64,
pub unique_ballots: u64,
pub winning_ballot: Ballot,
pub operator_votes: [OperatorVote; 16],
pub ballot_tallies: [BallotTally; 16],
#[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))]
pub operator_votes: [OperatorVote; 256],
#[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))]
pub ballot_tallies: [BallotTally; 256],
}

impl BallotBox {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ pub struct BaseRewardRouter {
pub total_rewards: u64,
pub reward_pool: u64,
pub rewards_processed: u64,
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::Bytes>"))]
#[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))]
pub reserved: [u8; 128],
pub base_fee_group_rewards: [BaseRewardRouterRewards; 8],
pub ncn_fee_group_rewards: [BaseRewardRouterRewards; 8],
pub ncn_fee_group_reward_routes: [NcnRewardRoute; 32],
#[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))]
pub ncn_fee_group_reward_routes: [NcnRewardRoute; 256],
}

impl BaseRewardRouter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct EpochSnapshot {
pub operators_registered: u64,
pub valid_operator_vault_delegations: u64,
pub stake_weights: StakeWeights,
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::Bytes>"))]
#[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))]
pub reserved: [u8; 128],
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct NcnConfig {
pub epochs_before_stall: u64,
pub fee_config: FeeConfig,
pub bump: u8,
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::Bytes>"))]
#[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))]
pub reserved: [u8; 127],
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ pub struct NcnRewardRouter {
pub reward_pool: u64,
pub rewards_processed: u64,
pub operator_rewards: u64,
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::Bytes>"))]
#[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))]
pub reserved: [u8; 128],
pub vault_reward_routes: [VaultRewardRoute; 32],
#[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))]
pub vault_reward_routes: [VaultRewardRoute; 64],
}

impl NcnRewardRouter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ pub struct OperatorSnapshot {
pub vault_operator_delegations_registered: u64,
pub valid_operator_vault_delegations: u64,
pub stake_weights: StakeWeights,
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::Bytes>"))]
#[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))]
pub reserved: [u8; 256],
pub vault_operator_stake_weight: [VaultOperatorStakeWeight; 32],
#[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))]
pub vault_operator_stake_weight: [VaultOperatorStakeWeight; 64],
}

impl OperatorSnapshot {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ pub struct TrackedMints {
)]
pub ncn: Pubkey,
pub bump: u8,
pub reserved: [u8; 7],
pub st_mint_list: [MintEntry; 16],
#[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))]
pub reserved: [u8; 127],
#[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))]
pub st_mint_list: [MintEntry; 64],
}

impl TrackedMints {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct WeightTable {
pub ncn_epoch: u64,
pub slot_created: u64,
pub bump: u8,
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::Bytes>"))]
#[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))]
pub reserved: [u8; 128],
pub table: [WeightEntry; 32],
}
Expand Down
2 changes: 1 addition & 1 deletion clients/rust/jito_tip_router/src/generated/types/ballot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ use borsh::{BorshDeserialize, BorshSerialize};
pub struct Ballot {
pub merkle_root: [u8; 32],
pub is_valid: bool,
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::Bytes>"))]
#[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))]
pub reserved: [u8; 63],
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::generated::types::Fees;
pub struct FeeConfig {
pub block_engine_fee_bps: u16,
pub base_fee_wallets: [Pubkey; 8],
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::Bytes>"))]
#[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))]
pub reserved: [u8; 128],
pub fee1: Fees,
pub fee2: Fees,
Expand Down
2 changes: 1 addition & 1 deletion clients/rust/jito_tip_router/src/generated/types/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::generated::types::Fee;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Fees {
pub activation_epoch: u64,
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::Bytes>"))]
#[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))]
pub reserved: [u8; 128],
pub base_fee_groups_bps: [Fee; 8],
pub ncn_fee_groups_bps: [Fee; 8],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ pub struct OperatorVote {
pub slot_voted: u64,
pub stake_weights: StakeWeights,
pub ballot_index: u16,
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::Bytes>"))]
#[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))]
pub reserved: [u8; 64],
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ pub struct WeightEntry {
pub weight: u128,
pub slot_set: u64,
pub slot_updated: u64,
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::Bytes>"))]
#[cfg_attr(feature = "serde", serde(with = "serde_big_array::BigArray"))]
pub reserved: [u8; 128],
}
2 changes: 2 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jito-restaking-sdk = { workspace = true }
jito-vault-core = { workspace = true }
jito-vault-sdk = { workspace = true }
meta-merkle-tree = { workspace = true }
serde = { workspace = true }
serde_with = { workspace = true }
shank = { workspace = true }
solana-program = { workspace = true }
spl-associated-token-account = { workspace = true }
Expand Down
37 changes: 29 additions & 8 deletions core/src/ballot_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use solana_program::{
use spl_math::precise_number::PreciseNumber;

use crate::{
constants::{precise_consensus, DEFAULT_CONSENSUS_REACHED_SLOT},
constants::{precise_consensus, DEFAULT_CONSENSUS_REACHED_SLOT, MAX_OPERATORS},
discriminators::Discriminators,
error::TipRouterError,
stake_weight::StakeWeights,
Expand Down Expand Up @@ -212,9 +212,8 @@ pub struct BallotBox {

winning_ballot: Ballot,

//TODO fix 32 -> MAX_OPERATORS
operator_votes: [OperatorVote; 16],
ballot_tallies: [BallotTally; 16],
operator_votes: [OperatorVote; 256],
ballot_tallies: [BallotTally; 256],
}

impl Discriminator for BallotBox {
Expand All @@ -232,9 +231,8 @@ impl BallotBox {
operators_voted: PodU64::from(0),
unique_ballots: PodU64::from(0),
winning_ballot: Ballot::default(),
//TODO fix 32 -> MAX_OPERATORS
operator_votes: [OperatorVote::default(); 16],
ballot_tallies: [BallotTally::default(); 16],
operator_votes: [OperatorVote::default(); MAX_OPERATORS],
ballot_tallies: [BallotTally::default(); MAX_OPERATORS],
reserved: [0; 128],
}
}
Expand Down Expand Up @@ -355,7 +353,7 @@ impl BallotBox {
self.winning_ballot.is_valid()
}

pub const fn operator_votes(&self) -> &[OperatorVote; 16] {
pub const fn operator_votes(&self) -> &[OperatorVote; MAX_OPERATORS] {
&self.operator_votes
}

Expand Down Expand Up @@ -572,6 +570,29 @@ impl BallotBox {
mod tests {
use super::*;

#[test]
fn test_len() {
use std::mem::size_of;

let expected_total = size_of::<Pubkey>() // ncn
+ size_of::<PodU64>() // epoch
+ 1 // bump
+ size_of::<PodU64>() // slot_created
+ size_of::<PodU64>() // slot_consensus_reached
+ 128 // reserved
+ size_of::<PodU64>() // operators_voted
+ size_of::<PodU64>() // unique_ballots
+ size_of::<Ballot>() // winning_ballot
+ size_of::<OperatorVote>() * MAX_OPERATORS // operator_votes
+ size_of::<BallotTally>() * MAX_OPERATORS; // ballot_tallies

assert_eq!(size_of::<BallotBox>(), expected_total);

let ballot_box = BallotBox::new(Pubkey::default(), 0, 0, 0);
assert_eq!(ballot_box.operator_votes.len(), MAX_OPERATORS);
assert_eq!(ballot_box.ballot_tallies.len(), MAX_OPERATORS);
}

#[test]
#[ignore] // TODO?
fn test_verify_merkle_root() {
Expand Down
27 changes: 22 additions & 5 deletions core/src/base_reward_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use solana_program::{
use spl_math::precise_number::PreciseNumber;

use crate::{
ballot_box::BallotBox, base_fee_group::BaseFeeGroup, discriminators::Discriminators,
error::TipRouterError, fees::Fees, ncn_fee_group::NcnFeeGroup,
ballot_box::BallotBox, base_fee_group::BaseFeeGroup, constants::MAX_OPERATORS,
discriminators::Discriminators, error::TipRouterError, fees::Fees, ncn_fee_group::NcnFeeGroup,
};

// PDA'd ["epoch_reward_router", NCN, NCN_EPOCH_SLOT]
Expand All @@ -36,8 +36,7 @@ pub struct BaseRewardRouter {
base_fee_group_rewards: [BaseRewardRouterRewards; 8],
ncn_fee_group_rewards: [BaseRewardRouterRewards; 8],

//TODO change to 256
ncn_fee_group_reward_routes: [NcnRewardRoute; 32],
ncn_fee_group_reward_routes: [NcnRewardRoute; 256],
}

impl Discriminator for BaseRewardRouter {
Expand All @@ -59,7 +58,7 @@ impl BaseRewardRouter {
NcnFeeGroup::FEE_GROUP_COUNT],
ncn_fee_group_rewards: [BaseRewardRouterRewards::default();
NcnFeeGroup::FEE_GROUP_COUNT],
ncn_fee_group_reward_routes: [NcnRewardRoute::default(); 32],
ncn_fee_group_reward_routes: [NcnRewardRoute::default(); MAX_OPERATORS],
}
}

Expand Down Expand Up @@ -659,6 +658,24 @@ mod tests {
use solana_program::pubkey::Pubkey;

use super::*;
#[test]
fn test_len() {
use std::mem::size_of;

let expected_total = size_of::<Pubkey>() // ncn
+ size_of::<PodU64>() // ncn_epoch
+ 1 // bump
+ size_of::<PodU64>() // slot_created
+ size_of::<PodU64>() // total_rewards
+ size_of::<PodU64>() // reward_pool
+ size_of::<PodU64>() // rewards_processed
+ 128 // reserved
+ size_of::<BaseRewardRouterRewards>() * NcnFeeGroup::FEE_GROUP_COUNT // base_fee_group_rewards
+ size_of::<BaseRewardRouterRewards>() * NcnFeeGroup::FEE_GROUP_COUNT // ncn_fee_group_rewards
+ size_of::<NcnRewardRoute>() * MAX_OPERATORS; // ncn_fee_group_reward_routes

assert_eq!(size_of::<BaseRewardRouter>(), expected_total);
}

#[test]
fn test_route_incoming_rewards() {
Expand Down
1 change: 1 addition & 0 deletions core/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::error::TipRouterError;

pub const MAX_FEE_BPS: u64 = 10_000;
pub const MAX_VAULT_OPERATOR_DELEGATIONS: usize = 64;
pub const MAX_OPERATORS: usize = 256;
const PRECISE_CONSENSUS_NUMERATOR: u128 = 2;
const PRECISE_CONSENSUS_DENOMINATOR: u128 = 3;
pub fn precise_consensus() -> Result<PreciseNumber, TipRouterError> {
Expand Down
Loading
Loading