Skip to content

Commit 3fa9ade

Browse files
committed
Cleanup and add tests
1 parent 97c1624 commit 3fa9ade

3 files changed

Lines changed: 74 additions & 63 deletions

File tree

test-integration/programs/schedulecommit/src/api.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,16 @@ pub fn schedule_commit_cpi_instruction(
180180
magic_context_id: Pubkey,
181181
players: &[Pubkey],
182182
committees: &[Pubkey],
183+
commit_type: ScheduleCommitType,
183184
) -> Instruction {
184185
schedule_commit_cpi_instruction_impl(
185186
payer,
186187
magic_program_id,
187188
magic_context_id,
188189
players,
189190
committees,
190-
ScheduleCommitCpiInstructionImplArgs {
191-
undelegate: false,
192-
commit_payer: false,
193-
},
191+
false,
192+
commit_type,
194193
)
195194
}
196195

@@ -247,10 +246,8 @@ pub fn schedule_commit_with_payer_cpi_instruction(
247246
magic_context_id,
248247
players,
249248
committees,
250-
ScheduleCommitCpiInstructionImplArgs {
251-
undelegate: false,
252-
commit_payer: true,
253-
},
249+
true,
250+
ScheduleCommitType::Commit,
254251
)
255252
}
256253

@@ -267,25 +264,19 @@ pub fn schedule_commit_and_undelegate_cpi_instruction(
267264
magic_context_id,
268265
players,
269266
committees,
270-
ScheduleCommitCpiInstructionImplArgs {
271-
undelegate: true,
272-
commit_payer: false,
273-
},
267+
false,
268+
ScheduleCommitType::CommitAndUndelegate,
274269
)
275270
}
276271

277-
struct ScheduleCommitCpiInstructionImplArgs {
278-
undelegate: bool,
279-
commit_payer: bool,
280-
}
281-
282272
fn schedule_commit_cpi_instruction_impl(
283273
payer: Pubkey,
284274
magic_program_id: Pubkey,
285275
magic_context_id: Pubkey,
286276
players: &[Pubkey],
287277
committees: &[Pubkey],
288-
args: ScheduleCommitCpiInstructionImplArgs,
278+
commit_payer: bool,
279+
commit_type: ScheduleCommitType,
289280
) -> Instruction {
290281
let program_id = crate::id();
291282
let mut account_metas = vec![
@@ -300,10 +291,10 @@ fn schedule_commit_cpi_instruction_impl(
300291
let cpi_args = ScheduleCommitCpiArgs {
301292
players: players.to_vec(),
302293
modify_accounts: true,
303-
undelegate: args.undelegate,
304-
commit_payer: args.commit_payer,
294+
commit_payer,
305295
};
306-
let ix = ScheduleCommitInstruction::ScheduleCommitCpi(cpi_args);
296+
let ix =
297+
ScheduleCommitInstruction::ScheduleCommitCpi(cpi_args, commit_type);
307298
Instruction::new_with_borsh(program_id, &ix, account_metas)
308299
}
309300

test-integration/programs/schedulecommit/src/lib.rs

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ pub struct ScheduleCommitCpiArgs {
6969
pub players: Vec<Pubkey>,
7070
/// If true, the accounts will be modified after the commit
7171
pub modify_accounts: bool,
72-
/// If true, the accounts will be undelegated after the commit
73-
pub undelegate: bool,
7472
/// If true, also commit the payer account
7573
pub commit_payer: bool,
7674
}
@@ -99,7 +97,7 @@ pub enum ScheduleCommitInstruction {
9997
/// - **1** `[]` MagicContext (used to record scheduled commit)
10098
/// - **2** `[]` MagicBlock Program (used to schedule commit)
10199
/// - **3..n** `[]` PDA accounts to be committed
102-
ScheduleCommitCpi(ScheduleCommitCpiArgs),
100+
ScheduleCommitCpi(ScheduleCommitCpiArgs, ScheduleCommitType),
103101

104102
/// Same instruction input like [ScheduleCommitInstruction::ScheduleCommitCpi].
105103
/// Behavior differs that it will modify the accounts after it
@@ -165,11 +163,54 @@ pub enum ScheduleCommitInstruction {
165163

166164
#[derive(BorshSerialize, BorshDeserialize, Debug, Clone, Copy)]
167165
pub enum ScheduleCommitType {
166+
Commit,
168167
CommitAndUndelegate,
169168
CommitFinalize,
170169
CommitFinalizeAndUndelegate,
171170
}
172171

172+
impl ScheduleCommitType {
173+
fn invoke_commit<'a, 'info>(
174+
self,
175+
payer: &'a AccountInfo<'info>,
176+
accounts: Vec<&'a AccountInfo<'info>>,
177+
magic_context: &'a AccountInfo<'info>,
178+
magic_program: &'a AccountInfo<'info>,
179+
) -> ProgramResult {
180+
match self {
181+
ScheduleCommitType::Commit => {
182+
commit_accounts(payer, accounts, magic_context, magic_program)?;
183+
}
184+
ScheduleCommitType::CommitAndUndelegate => {
185+
commit_and_undelegate_accounts(
186+
payer,
187+
accounts,
188+
magic_context,
189+
magic_program,
190+
)?;
191+
}
192+
ScheduleCommitType::CommitFinalize => {
193+
commit_finalize_accounts(
194+
payer,
195+
accounts,
196+
magic_context,
197+
magic_program,
198+
)?;
199+
}
200+
ScheduleCommitType::CommitFinalizeAndUndelegate => {
201+
commit_finalize_and_undelegate_accounts(
202+
payer,
203+
accounts,
204+
magic_context,
205+
magic_program,
206+
)?;
207+
}
208+
};
209+
210+
Ok(())
211+
}
212+
}
213+
173214
pub fn process_instruction<'a>(
174215
program_id: &'a Pubkey,
175216
accounts: &'a [AccountInfo<'a>],
@@ -197,14 +238,14 @@ pub fn process_instruction<'a>(
197238
match ix {
198239
Init => process_init(program_id, accounts),
199240
DelegateCpi(args) => process_delegate_cpi(accounts, args),
200-
ScheduleCommitCpi(args) => process_schedulecommit_cpi(
241+
ScheduleCommitCpi(args, commit_type) => process_schedulecommit_cpi(
201242
accounts,
202243
&args.players,
203244
ProcessSchedulecommitCpiArgs {
204245
modify_accounts: args.modify_accounts,
205-
undelegate: args.undelegate,
206246
commit_payer: args.commit_payer,
207247
},
248+
commit_type,
208249
),
209250
ScheduleCommitAndUndelegateCpiModAfter(players) => {
210251
process_schedulecommit_and_undelegation_cpi_with_mod_after(
@@ -469,32 +510,12 @@ pub fn process_schedulecommit_for_orderbook(
469510

470511
assert_is_signer(payer, "payer")?;
471512

472-
match commit_type {
473-
ScheduleCommitType::CommitAndUndelegate => {
474-
commit_and_undelegate_accounts(
475-
payer,
476-
vec![order_book_account],
477-
magic_context,
478-
magic_program,
479-
)?;
480-
}
481-
ScheduleCommitType::CommitFinalize => {
482-
commit_finalize_accounts(
483-
payer,
484-
vec![order_book_account],
485-
magic_context,
486-
magic_program,
487-
)?;
488-
}
489-
ScheduleCommitType::CommitFinalizeAndUndelegate => {
490-
commit_finalize_and_undelegate_accounts(
491-
payer,
492-
vec![order_book_account],
493-
magic_context,
494-
magic_program,
495-
)?;
496-
}
497-
};
513+
commit_type.invoke_commit(
514+
payer,
515+
vec![order_book_account],
516+
magic_context,
517+
magic_program,
518+
)?;
498519

499520
Ok(())
500521
}
@@ -541,7 +562,6 @@ pub fn process_delegate_cpi(
541562

542563
pub struct ProcessSchedulecommitCpiArgs {
543564
pub modify_accounts: bool,
544-
pub undelegate: bool,
545565
pub commit_payer: bool,
546566
}
547567

@@ -552,6 +572,7 @@ pub fn process_schedulecommit_cpi(
552572
accounts: &[AccountInfo],
553573
player_pubkeys: &[Pubkey],
554574
args: ProcessSchedulecommitCpiArgs,
575+
commit_type: ScheduleCommitType,
555576
) -> Result<(), ProgramError> {
556577
msg!("Processing schedulecommit_cpi instruction");
557578

@@ -604,16 +625,12 @@ pub fn process_schedulecommit_cpi(
604625
committees.iter().map(|x| x.key).collect::<Vec<_>>()
605626
);
606627

607-
if args.undelegate {
608-
commit_and_undelegate_accounts(
609-
payer,
610-
committees,
611-
magic_context,
612-
magic_program,
613-
)?;
614-
} else {
615-
commit_accounts(payer, committees, magic_context, magic_program)?;
616-
}
628+
commit_type.invoke_commit(
629+
payer,
630+
committees,
631+
magic_context,
632+
magic_program,
633+
)?;
617634

618635
Ok(())
619636
}

test-integration/schedulecommit/test-scenarios/tests/02_commit_and_undelegate.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,9 @@ fn run_test_for_commit_huge_order_book_account(
388388

389389
assert_one_committee_was_committed(&ctx, &res, true);
390390
match commit_type {
391+
ScheduleCommitType::Commit => {
392+
assert!(false);
393+
}
391394
ScheduleCommitType::CommitFinalize => {
392395
assert_one_committee_account_was_not_undelegated_on_chain(&ctx);
393396
}

0 commit comments

Comments
 (0)