|
| 1 | +use light_instruction_decoder::{DecoderRegistry, InstructionDecoder}; |
| 2 | +use sha2::{Digest, Sha256}; |
| 3 | +use solana_instruction::AccountMeta; |
| 4 | +use solana_pubkey::Pubkey; |
| 5 | + |
| 6 | +const COUNTER_PROGRAM_ID: Pubkey = |
| 7 | + solana_pubkey::pubkey!("Counter111111111111111111111111111111111111"); |
| 8 | + |
| 9 | +fn anchor_discriminator(name: &str) -> [u8; 8] { |
| 10 | + let mut hasher = Sha256::new(); |
| 11 | + hasher.update(format!("global:{name}").as_bytes()); |
| 12 | + let hash = hasher.finalize(); |
| 13 | + let mut disc = [0u8; 8]; |
| 14 | + disc.copy_from_slice(&hash[..8]); |
| 15 | + disc |
| 16 | +} |
| 17 | + |
| 18 | +fn make_accounts(names: &[&str]) -> Vec<AccountMeta> { |
| 19 | + names |
| 20 | + .iter() |
| 21 | + .map(|_| AccountMeta::new(Pubkey::new_unique(), false)) |
| 22 | + .collect() |
| 23 | +} |
| 24 | + |
| 25 | +#[test] |
| 26 | +fn test_decoder_program_id_and_name() { |
| 27 | + let decoder = counter::CounterInstructionDecoder; |
| 28 | + assert_eq!(decoder.program_id(), COUNTER_PROGRAM_ID); |
| 29 | + assert_eq!(decoder.program_name(), "Counter"); |
| 30 | +} |
| 31 | + |
| 32 | +#[test] |
| 33 | +fn test_decoder_can_be_registered() { |
| 34 | + let mut registry = DecoderRegistry::new(); |
| 35 | + registry.register(Box::new(counter::CounterInstructionDecoder)); |
| 36 | + assert!(registry.has_decoder(&COUNTER_PROGRAM_ID)); |
| 37 | +} |
| 38 | + |
| 39 | +#[test] |
| 40 | +fn test_decoder_decodes_initialize() { |
| 41 | + let decoder = counter::CounterInstructionDecoder; |
| 42 | + let disc = anchor_discriminator("initialize"); |
| 43 | + let accounts = make_accounts(&["counter", "authority", "system_program"]); |
| 44 | + |
| 45 | + let result = decoder.decode(&disc, &accounts); |
| 46 | + assert!(result.is_some()); |
| 47 | + |
| 48 | + let decoded = result.unwrap(); |
| 49 | + assert_eq!(decoded.name, "Initialize"); |
| 50 | + assert_eq!( |
| 51 | + decoded.account_names, |
| 52 | + vec!["counter", "authority", "system_program"] |
| 53 | + ); |
| 54 | +} |
| 55 | + |
| 56 | +#[test] |
| 57 | +fn test_decoder_decodes_set_with_params() { |
| 58 | + let decoder = counter::CounterInstructionDecoder; |
| 59 | + let disc = anchor_discriminator("set"); |
| 60 | + |
| 61 | + let value: u64 = 42; |
| 62 | + let mut data = disc.to_vec(); |
| 63 | + data.extend_from_slice(&value.to_le_bytes()); |
| 64 | + |
| 65 | + let accounts = make_accounts(&["counter", "authority"]); |
| 66 | + |
| 67 | + let result = decoder.decode(&data, &accounts); |
| 68 | + assert!(result.is_some()); |
| 69 | + |
| 70 | + let decoded = result.unwrap(); |
| 71 | + assert_eq!(decoded.name, "Set"); |
| 72 | + assert!(decoded.fields.iter().any(|f| f.name == "value" && f.value == "42")); |
| 73 | +} |
| 74 | + |
| 75 | +#[test] |
| 76 | +fn test_decoder_returns_none_for_unknown() { |
| 77 | + let decoder = counter::CounterInstructionDecoder; |
| 78 | + let bogus = [0xFF; 8]; |
| 79 | + let accounts = make_accounts(&["a"]); |
| 80 | + |
| 81 | + let result = decoder.decode(&bogus, &accounts); |
| 82 | + assert!(result.is_none()); |
| 83 | +} |
| 84 | + |
| 85 | +#[test] |
| 86 | +fn test_discriminators_match_anchor() { |
| 87 | + let decoder = counter::CounterInstructionDecoder; |
| 88 | + |
| 89 | + let instructions = [ |
| 90 | + "initialize", |
| 91 | + "increment", |
| 92 | + "decrement", |
| 93 | + "set", |
| 94 | + "configure", |
| 95 | + ]; |
| 96 | + |
| 97 | + let expected_names = [ |
| 98 | + "Initialize", |
| 99 | + "Increment", |
| 100 | + "Decrement", |
| 101 | + "Set", |
| 102 | + "Configure", |
| 103 | + ]; |
| 104 | + |
| 105 | + for (ix_name, expected_name) in instructions.iter().zip(expected_names.iter()) { |
| 106 | + let disc = anchor_discriminator(ix_name); |
| 107 | + let accounts = make_accounts(&["a", "b", "c"]); |
| 108 | + |
| 109 | + let result = decoder.decode(&disc, &accounts); |
| 110 | + assert!( |
| 111 | + result.is_some(), |
| 112 | + "Decoder should recognize discriminator for '{ix_name}'" |
| 113 | + ); |
| 114 | + assert_eq!( |
| 115 | + result.unwrap().name, |
| 116 | + *expected_name, |
| 117 | + "Instruction name mismatch for '{ix_name}'" |
| 118 | + ); |
| 119 | + } |
| 120 | +} |
0 commit comments