121 lines
4.5 KiB
Rust
121 lines
4.5 KiB
Rust
// file: kb-lib/tests/external_executor_api.rs
|
|
// version: 3
|
|
|
|
//! 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<kb_lib::ExApiExecutionPlan> {
|
|
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<kb_lib::ExApiPreparedExecutionPlan> {
|
|
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
|
|
);
|
|
}
|