@@ -22,10 +22,10 @@ use engine::Engine;
2222use keeper:: MissingAccount ;
2323use lru:: LruCache ;
2424use magicblock_aml:: RiskService ;
25- use magicblock_config:: config:: AllowedProgram ;
25+ use magicblock_config:: config:: { AllowedProgram , AmlCheckStrategy } ;
2626use magicblock_core:: token_programs:: {
27- ASSOCIATED_TOKEN_PROGRAM_ID , EATA_PROGRAM_ID , TOKEN_PROGRAM_ID , is_ata ,
28- normalize_native_token_account_for_local_clone,
27+ ASSOCIATED_TOKEN_PROGRAM_ID , EATA_PROGRAM_ID , TOKEN_2022_PROGRAM_ID ,
28+ TOKEN_PROGRAM_ID , is_ata , normalize_native_token_account_for_local_clone,
2929} ;
3030use magicblock_metrics:: metrics:: {
3131 self , AccountFetchContext , AccountFetchReason , BankPrecheckOutcome ,
@@ -333,6 +333,42 @@ fn log_companion_fetch_failure<E: std::fmt::Display + ?Sized>(
333333 ) ;
334334}
335335
336+ /// Programs whose presence in a post-delegation action triggers a risk check
337+ /// under [`AmlCheckStrategy::RelevantPrograms`]: SPL Token (legacy and 2022),
338+ /// the ephemeral SPL / eATA program (ESPL), and the Magic program.
339+ const RISK_RELEVANT_PROGRAMS : [ Pubkey ; 4 ] = [
340+ TOKEN_PROGRAM_ID ,
341+ TOKEN_2022_PROGRAM_ID ,
342+ EATA_PROGRAM_ID ,
343+ magicblock_magic_program_api:: ID ,
344+ ] ;
345+
346+ /// Decides whether the configured [`AmlCheckStrategy`] requires risk checking
347+ /// the signers of these post-delegation actions.
348+ fn delegation_actions_require_risk_check (
349+ strategy : AmlCheckStrategy ,
350+ delegation_actions : & DelegationActions ,
351+ ) -> bool {
352+ match strategy {
353+ AmlCheckStrategy :: AllSigners => true ,
354+ AmlCheckStrategy :: RelevantPrograms => delegation_actions
355+ . iter ( )
356+ . any ( instruction_involves_risk_relevant_program) ,
357+ }
358+ }
359+
360+ /// Returns true when a risk-relevant program is invoked by the instruction or
361+ /// referenced by any of its accounts (e.g. as the target of a CPI).
362+ fn instruction_involves_risk_relevant_program (
363+ instruction : & solana_instruction:: Instruction ,
364+ ) -> bool {
365+ RISK_RELEVANT_PROGRAMS . contains ( & instruction. program_id )
366+ || instruction
367+ . accounts
368+ . iter ( )
369+ . any ( |meta| RISK_RELEVANT_PROGRAMS . contains ( & meta. pubkey ) )
370+ }
371+
336372impl < T , U > FetchCloner < T , U >
337373where
338374 T : ChainRpcClient ,
@@ -1538,6 +1574,13 @@ where
15381574 return Ok ( ( ) ) ;
15391575 } ;
15401576
1577+ if !delegation_actions_require_risk_check (
1578+ risk_service. check_strategy ( ) ,
1579+ delegation_actions,
1580+ ) {
1581+ return Ok ( ( ) ) ;
1582+ }
1583+
15411584 let mut signers = delegation_actions
15421585 . iter ( )
15431586 . flat_map ( |instruction| {
0 commit comments