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

Backport #247 #251

Merged
merged 3 commits into from
Feb 14, 2023
Merged
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
122 changes: 106 additions & 16 deletions tip-distributor/src/stake_meta_generator_workflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,24 +166,36 @@ pub fn generate_stake_meta_collection(
// We assume that the rewards sitting in the tip program PDAs are cranked out by the time all of
// the rewards are claimed.
let tip_accounts = derive_tip_payment_pubkeys(tip_payment_program_id);
let account = bank.get_account(&tip_accounts.config_pda);
let maybe_tip_receiver: Option<Pubkey> = account
.and_then(|account| Config::try_deserialize(&mut account.data()).ok())
.map(|config| config.tip_receiver);
let account = bank
.get_account(&tip_accounts.config_pda)
.expect("config pda exists");

let config = Config::try_deserialize(&mut account.data()).expect("deserializes configuration");

let bb_commission_pct: u64 = config.block_builder_commission_pct;
let tip_receiver: Pubkey = config.tip_receiver;

// includes the block builder fee
let excess_tip_balances: u64 = tip_accounts
.tip_pdas
.iter()
.map(|pubkey| {
bank.get_account(pubkey)
.map(|acc| {
acc.lamports()
.checked_sub(bank.get_minimum_balance_for_rent_exemption(acc.data().len()))
.expect("tip balance underflow")
})
.unwrap_or_default()
let tip_account = bank.get_account(pubkey).expect("tip account exists");
tip_account
.lamports()
.checked_sub(bank.get_minimum_balance_for_rent_exemption(tip_account.data().len()))
.expect("tip balance underflow")
})
.sum();
// matches math in tip payment program
let block_builder_tips = excess_tip_balances
.checked_mul(bb_commission_pct)
.expect("block_builder_tips overflow")
.checked_div(100)
.expect("block_builder_tips division error");
let tip_receiver_fee = excess_tip_balances
.checked_sub(block_builder_tips)
.expect("tip_receiver_fee doesnt underflow");

let vote_pk_and_maybe_tdas: Vec<(
(Pubkey, &VoteAccount),
Expand All @@ -205,13 +217,11 @@ pub fn generate_stake_meta_collection(
.expect("deserialized TipDistributionAccount");
// this snapshot might have tips that weren't claimed by the time the epoch is over
// assume that it will eventually be cranked and credit the excess to this account
if maybe_tip_receiver.is_some()
&& tip_distribution_pubkey == maybe_tip_receiver.unwrap()
{
if tip_distribution_pubkey == tip_receiver {
account_data.set_lamports(
account_data
.lamports()
.checked_add(excess_tip_balances)
.checked_add(tip_receiver_fee)
.expect("tip overflow"),
);
}
Expand Down Expand Up @@ -338,6 +348,11 @@ mod tests {
},
solana_stake_program::stake_state,
tip_distribution::state::TipDistributionAccount,
tip_payment::{
InitBumps, TipPaymentAccount, CONFIG_ACCOUNT_SEED, TIP_ACCOUNT_SEED_0,
TIP_ACCOUNT_SEED_1, TIP_ACCOUNT_SEED_2, TIP_ACCOUNT_SEED_3, TIP_ACCOUNT_SEED_4,
TIP_ACCOUNT_SEED_5, TIP_ACCOUNT_SEED_6, TIP_ACCOUNT_SEED_7,
},
};

#[test]
Expand All @@ -363,6 +378,7 @@ mod tests {
/* 2. Seed the Bank with [TipDistributionAccount]'s */
let merkle_root_upload_authority = Pubkey::new_unique();
let tip_distribution_program_id = Pubkey::new_unique();
let tip_payment_program_id = Pubkey::new_unique();

let delegator_0 = Keypair::new();
let delegator_1 = Keypair::new();
Expand Down Expand Up @@ -631,6 +647,11 @@ mod tests {
let data_2 =
tda_to_account_shared_data(&tip_distribution_program_id, tip_distro_2_tips, tda_2);

let accounts_data = create_config_account_data(&tip_payment_program_id, &bank);
for (pubkey, data) in accounts_data {
bank.store_account(&pubkey, &data);
}

bank.store_account(&tip_distribution_account_0.0, &data_0);
bank.store_account(&tip_distribution_account_1.0, &data_1);
bank.store_account(&tip_distribution_account_2.0, &data_2);
Expand All @@ -639,7 +660,7 @@ mod tests {
let stake_meta_collection = generate_stake_meta_collection(
&bank,
&tip_distribution_program_id,
&Pubkey::new_unique(),
&tip_payment_program_id,
)
.unwrap();
assert_eq!(
Expand Down Expand Up @@ -842,4 +863,73 @@ mod tests {
account_data.set_data(data.to_vec());
account_data
}

fn create_config_account_data(
tip_payment_program_id: &Pubkey,
bank: &Bank,
) -> Vec<(Pubkey, AccountSharedData)> {
let mut account_datas = vec![];

let config_pda =
Pubkey::find_program_address(&[CONFIG_ACCOUNT_SEED], tip_payment_program_id);

let tip_accounts = [
Pubkey::find_program_address(&[TIP_ACCOUNT_SEED_0], tip_payment_program_id),
Pubkey::find_program_address(&[TIP_ACCOUNT_SEED_1], tip_payment_program_id),
Pubkey::find_program_address(&[TIP_ACCOUNT_SEED_2], tip_payment_program_id),
Pubkey::find_program_address(&[TIP_ACCOUNT_SEED_3], tip_payment_program_id),
Pubkey::find_program_address(&[TIP_ACCOUNT_SEED_4], tip_payment_program_id),
Pubkey::find_program_address(&[TIP_ACCOUNT_SEED_5], tip_payment_program_id),
Pubkey::find_program_address(&[TIP_ACCOUNT_SEED_6], tip_payment_program_id),
Pubkey::find_program_address(&[TIP_ACCOUNT_SEED_7], tip_payment_program_id),
];

let config = Config {
tip_receiver: Pubkey::new_unique(),
block_builder: Pubkey::new_unique(),
block_builder_commission_pct: 10,
bumps: InitBumps {
config: config_pda.1,
tip_payment_account_0: tip_accounts[0].1,
tip_payment_account_1: tip_accounts[1].1,
tip_payment_account_2: tip_accounts[2].1,
tip_payment_account_3: tip_accounts[3].1,
tip_payment_account_4: tip_accounts[4].1,
tip_payment_account_5: tip_accounts[5].1,
tip_payment_account_6: tip_accounts[6].1,
tip_payment_account_7: tip_accounts[7].1,
},
};

let mut config_account_data = AccountSharedData::new(
bank.get_minimum_balance_for_rent_exemption(Config::SIZE),
Config::SIZE,
tip_payment_program_id,
);

let mut config_data: [u8; Config::SIZE] = [0u8; Config::SIZE];
let mut config_cursor = std::io::Cursor::new(&mut config_data[..]);
config.try_serialize(&mut config_cursor).unwrap();
config_account_data.set_data(config_data.to_vec());
account_datas.push((config_pda.0, config_account_data));

account_datas.extend(tip_accounts.into_iter().map(|(pubkey, _)| {
let mut tip_account_data = AccountSharedData::new(
bank.get_minimum_balance_for_rent_exemption(TipPaymentAccount::SIZE),
TipPaymentAccount::SIZE,
tip_payment_program_id,
);

let mut data: [u8; TipPaymentAccount::SIZE] = [0u8; TipPaymentAccount::SIZE];
let mut cursor = std::io::Cursor::new(&mut data[..]);
TipPaymentAccount::default()
.try_serialize(&mut cursor)
.unwrap();
tip_account_data.set_data(data.to_vec());

(pubkey, tip_account_data)
}));

account_datas
}
}