solana_message/
inline_nonce.rs

1//! Inlined nonce instruction information to avoid a dependency on bincode and
2//! solana-system-interface
3use {
4    solana_address::Address,
5    solana_instruction::{AccountMeta, Instruction},
6    solana_sdk_ids::{system_program, sysvar},
7};
8
9/// Inlined `SystemInstruction::AdvanceNonceAccount` instruction data to avoid
10/// solana_system_interface and bincode deps
11const ADVANCE_NONCE_DATA: [u8; 4] = [4, 0, 0, 0];
12
13/// Check if the given instruction data is the same as
14/// `SystemInstruction::AdvanceNonceAccount`.
15///
16/// NOTE: It's possible for additional data to exist after the 4th byte, but
17/// users of this function only look at the first 4 bytes.
18pub fn is_advance_nonce_instruction_data(data: &[u8]) -> bool {
19    data.get(0..4) == Some(&ADVANCE_NONCE_DATA)
20}
21
22/// Inlined `advance_nonce_account` instruction creator to avoid
23/// solana_system_interface and bincode deps
24pub(crate) fn advance_nonce_account_instruction(
25    nonce_pubkey: &Address,
26    nonce_authority_pubkey: &Address,
27) -> Instruction {
28    Instruction::new_with_bytes(
29        system_program::id(),
30        &ADVANCE_NONCE_DATA,
31        vec![
32            AccountMeta::new(*nonce_pubkey, false),
33            #[allow(deprecated)]
34            AccountMeta::new_readonly(sysvar::recent_blockhashes::id(), false),
35            AccountMeta::new_readonly(*nonce_authority_pubkey, true),
36        ],
37    )
38}
39
40#[cfg(test)]
41mod test {
42    use {
43        super::*,
44        solana_system_interface::instruction::{advance_nonce_account, SystemInstruction},
45    };
46
47    #[test]
48    fn inline_instruction_data_matches_program() {
49        let nonce = Address::new_unique();
50        let nonce_authority = Address::new_unique();
51        assert_eq!(
52            advance_nonce_account_instruction(&nonce, &nonce_authority),
53            advance_nonce_account(&nonce, &nonce_authority),
54        );
55    }
56
57    #[test]
58    fn test_advance_nonce_ix_prefix() {
59        let advance_nonce_ix: SystemInstruction =
60            bincode::deserialize(&ADVANCE_NONCE_DATA).unwrap();
61        assert_eq!(advance_nonce_ix, SystemInstruction::AdvanceNonceAccount);
62    }
63}