// file: kb-lib/tests/external_executor_api.rs // version: 4 //! Downstream-style compilation contract for externally implemented executors. struct ExternalExecutor; struct ExternalTypedExecutor; impl kb_lib::ExApiInstructionExecutor for ExternalExecutor { fn executor_name(&self) -> &'static str { return "external_executor"; } fn executor_version(&self) -> &'static str { return "1"; } fn program_ids(&self) -> &'static [&'static str] { return &[]; } fn supports_request( &self, _request: &kb_lib::ExApiExecutionRequest, ) -> kb_lib::ExApiExecutionSupport { return kb_lib::ExApiExecutionSupport::No; } fn build_plan( &self, request: &kb_lib::ExApiExecutionRequest, ) -> kb_core::Result { return std::result::Result::Ok(kb_lib::ExApiExecutionPlan { executor_name: self.executor_name().to_string(), instruction_count: 0, payload_json: request.payload_json.clone(), }); } } impl kb_lib::ExApiTypedInstructionExecutor for ExternalTypedExecutor { type Intent = kb_lib::MdPubkey; fn capability( &self, _program_id: &kb_lib::MdProgramId, operation_code: &str, ) -> kb_lib::ExApiExecutionCapability { return kb_lib::ExApiExecutionCapability::unsupported("external_executor", operation_code); } fn build_prepared_plan( &self, intent: &Self::Intent, ) -> kb_core::Result { return std::result::Result::Ok(kb_lib::ExApiPreparedExecutionPlan { executor_name: "external_typed_executor".to_string(), executor_version: "1".to_string(), intent_id: "external-intent".to_string(), operation_code: "external-operation".to_string(), fee_payer: intent.clone(), instructions: std::vec::Vec::new(), required_signers: std::vec::Vec::new(), policy: kb_lib::ExApiExecutionPolicy::default(), requested_spend_lamports: 0, requested_compute_unit_price_micro_lamports: std::option::Option::None, }); } } #[test] fn external_executor_uses_only_the_public_kb_lib_contract() { let executor: &dyn kb_lib::ExApiInstructionExecutor = &ExternalExecutor; assert_eq!(executor.executor_name(), "external_executor"); assert_eq!(executor.executor_version(), "1"); assert!(executor.program_ids().is_empty()); let typed_executor = ExternalTypedExecutor; let program_id = kb_lib::MdProgramId("external-program".to_string()); let capability = kb_lib::ExApiTypedInstructionExecutor::capability( &typed_executor, &program_id, "external-operation", ); assert!(!capability.is_supported()); let fee_payer = kb_lib::MdPubkey("external-fee-payer".to_string()); let plan = kb_lib::ExApiTypedInstructionExecutor::build_prepared_plan(&typed_executor, &fee_payer) .unwrap_or_else(|error| panic!("external typed executor failed: {error}")); assert_eq!(plan.fee_payer, fee_payer); assert!(plan.instructions.is_empty()); assert!(plan.required_signers.is_empty()); assert_eq!(plan.policy.simulation, kb_lib::ExApiExecutionSimulationPolicy::Required); assert!(plan.policy.dry_run); let solana_core_executor = kb_lib::ExSolanaCoreExecutor; let solana_core_program_id = kb_lib::MdProgramId(kb_program_ids::COMPUTE_BUDGET_PROGRAM_ID.to_string()); let solana_core_capability = kb_lib::ExApiTypedInstructionExecutor::capability( &solana_core_executor, &solana_core_program_id, kb_lib::EX_SOLANA_CORE_COMPUTE_BUDGET_SET_UNIT_LIMIT_OPERATION, ); assert!(solana_core_capability.is_supported()); let solana_core_intent = kb_lib::ExSolanaCoreExecutionIntent { intent_id: "external-solana-core".to_string(), fee_payer: kb_lib::MdPubkey(kb_program_ids::SYSTEM_PROGRAM_ID.to_string()), policy: kb_lib::ExApiExecutionPolicy::default(), operation: kb_lib::ExSolanaCoreOperation::ComputeBudgetSetUnitLimit { units: 500_000 }, }; let solana_core_plan = kb_lib::ExApiTypedInstructionExecutor::build_prepared_plan( &solana_core_executor, &solana_core_intent, ) .unwrap_or_else(|error| panic!("Solana core executor failed: {error}")); assert_eq!(solana_core_plan.instructions.len(), 1); assert_eq!( solana_core_plan.operation_code, kb_lib::EX_SOLANA_CORE_COMPUTE_BUDGET_SET_UNIT_LIMIT_OPERATION ); let memo_executor = kb_lib::ExSplMemoExecutor; let memo_program_id = kb_lib::MdProgramId(kb_program_ids::SPL_MEMO_V4_PROGRAM_ID.to_string()); let memo_capability = kb_lib::ExApiTypedInstructionExecutor::capability( &memo_executor, &memo_program_id, kb_lib::EX_SPL_MEMO_ADD_MEMO_OPERATION, ); assert!(memo_capability.is_supported()); let memo_intent = kb_lib::ExSplMemoExecutionIntent { intent_id: "external-spl-memo".to_string(), fee_payer: kb_lib::MdPubkey(kb_program_ids::SYSTEM_PROGRAM_ID.to_string()), policy: kb_lib::ExApiExecutionPolicy { cost_limit: kb_lib::ExApiExecutionCostLimit { max_spend_lamports: std::option::Option::Some(0), max_fee_lamports: std::option::Option::Some(10_000), max_compute_unit_price_micro_lamports: std::option::Option::None, }, authorized_signers: vec![kb_lib::MdPubkey( kb_program_ids::SYSTEM_PROGRAM_ID.to_string(), )], post_execution_validation: kb_lib::ExApiPostExecutionValidationPolicy { canonical_insert_required: true, core_extraction_required: true, decode_replay_required: true, materialization_required: true, }, ..kb_lib::ExApiExecutionPolicy::default() }, operation: kb_lib::ExSplMemoOperation::AddMemo { generation: kb_lib::ExSplMemoGeneration::V4, message: "external memo".to_string(), signers: std::vec::Vec::new(), }, }; let memo_plan = kb_lib::ExApiTypedInstructionExecutor::build_prepared_plan(&memo_executor, &memo_intent) .unwrap_or_else(|error| panic!("SPL Memo executor failed: {error}")); assert_eq!(memo_plan.instructions.len(), 1); assert_eq!( kb_lib::ExSafetyChecker .evaluate_prepared_plan(&memo_plan) .unwrap_or_else(|error| panic!("safety evaluation failed: {error}")) .decision, kb_lib::ExSafetyDecision::Allow ); }