Skip to main content

cow_settlement_interface/pda/
buffer.rs

1//! Buffer PDA seed and address derivation.
2//!
3//! Each buffer is a per-token SPL token account that holds funds controlled
4//! by the settlement state PDA. It lives at a PDA keyed by the token mint, so
5//! there is exactly one buffer address per token.
6//!
7//! The token account stored at this address is initialized by the
8//! `CreateBuffer` instruction; its SPL `owner` (token authority) is the
9//! settlement state PDA (see [`crate::pda::state`]), the single authority
10//! controlling every buffer.
11//!
12//! The seeds start with [`SETTLEMENT_SEED`], which carries
13//! the cargo package major and minor version, so a version bump moves every buffer.
14//! Since only the state PDA can spend a buffer and that address moves too,
15//! buffers must be drained under the old version before a bump ships, or their
16//! contents are stranded.
17//!
18//! Unlike the order PDA, which stores its own bump (see
19//! [`crate::data::order::OrderAccount`]), a buffer is a fixed-size SPL token
20//! account with no room for one.
21
22use solana_account_view::AccountView;
23use solana_address::Address;
24use solana_program_error::ProgramError;
25use solana_pubkey::Pubkey;
26
27use crate::pda::{is_pda_with_signer_seeds, SETTLEMENT_SEED};
28use crate::SettlementError;
29
30/// Trailing seed identifying the buffer PDAs.
31pub const BUFFER_SEED: &[u8] = b"buffer";
32
33/// Canonical seed components for the buffer PDA holding the specified `mint`
34/// token.
35///
36/// `mint` is the raw 32-byte token mint address, so the same helper serves
37/// both the off-chain builder and the on-chain handler (which holds the mint
38/// as an `Address`).
39pub fn buffer_pda_seeds(mint: &[u8; 32]) -> [&[u8]; 3] {
40    [SETTLEMENT_SEED, mint, BUFFER_SEED]
41}
42
43/// Canonical seeds for re-deriving the buffer PDA for `mint` with `bump`.
44pub fn buffer_pda_signer_seeds<'a>(mint: &'a [u8; 32], bump: &'a [u8; 1]) -> [&'a [u8]; 4] {
45    let [s0, s1, s2] = buffer_pda_seeds(mint);
46    [s0, s1, s2, bump]
47}
48
49/// Derive the canonical buffer PDA address (and bump) for the token `mint`.
50pub fn find_buffer_pda(program_id: &Pubkey, mint: &Pubkey) -> (Pubkey, u8) {
51    Pubkey::find_program_address(&buffer_pda_seeds(mint.as_array()), program_id)
52}
53
54/// Confirm `buffer` matches the derived buffer PDA for `mint` and `bump`.
55#[must_use = "ignoring the output means ignoring the validation result"]
56pub fn validate_buffer_pda(
57    program_id: &Address,
58    buffer: &AccountView,
59    mint: &Address,
60    bump: u8,
61) -> Result<(), ProgramError> {
62    is_pda_with_signer_seeds(
63        buffer,
64        program_id,
65        buffer_pda_signer_seeds(&mint.to_bytes(), &[bump]),
66    )
67    .then_some(())
68    .ok_or(SettlementError::PushSourceNotBuffer.into())
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74    use crate::pda::tests::assert_distinct_versions_yield_distinct_pdas;
75
76    #[test]
77    fn find_buffer_pda_uses_canonical_seeds() {
78        let token = Pubkey::new_unique();
79
80        crate::pda::tests::assert_canonical_bump(
81            |program_id| find_buffer_pda(program_id, &token),
82            buffer_pda_seeds(token.as_array()),
83        );
84    }
85
86    #[test]
87    fn distinct_versions_yield_distinct_buffer_pdas() {
88        let mint = Pubkey::new_unique();
89        let (pda, _) = find_buffer_pda(&crate::ID, &mint);
90
91        assert_distinct_versions_yield_distinct_pdas(&pda, &[mint.as_array(), BUFFER_SEED]);
92    }
93
94    #[test]
95    fn accepts_a_valid_pda() {
96        let program_id = Pubkey::new_unique();
97        let mint = Pubkey::new_unique();
98        let (pda, bump) = find_buffer_pda(&program_id, &mint);
99
100        let buffer = crate::instruction::fixtures::fake_account(pda);
101        validate_buffer_pda(&program_id, &buffer, &mint, bump)
102            .expect("the canonical buffer PDA must be accepted");
103    }
104
105    #[test]
106    fn rejects_an_invalid_address() {
107        let program_id = Pubkey::new_unique();
108        let mint = Pubkey::new_unique();
109        let (_, bump) = find_buffer_pda(&program_id, &mint);
110
111        // An account sitting at some other address is not the buffer.
112        let buffer = crate::instruction::fixtures::fake_account(Pubkey::new_unique());
113        let err = validate_buffer_pda(&program_id, &buffer, &mint, bump)
114            .expect_err("a non-canonical address must be rejected");
115        assert_eq!(err, SettlementError::PushSourceNotBuffer.into());
116    }
117
118    #[test]
119    fn rejects_a_wrong_bump() {
120        let program_id = Pubkey::new_unique();
121        let mint = Pubkey::new_unique();
122        let (pda, bump) = find_buffer_pda(&program_id, &mint);
123
124        // The address is canonical but the carried bump doesn't derive it.
125        let buffer = crate::instruction::fixtures::fake_account(pda);
126        let err = validate_buffer_pda(&program_id, &buffer, &mint, bump ^ 1)
127            .expect_err("a wrong bump must be rejected");
128        assert_eq!(err, SettlementError::PushSourceNotBuffer.into());
129    }
130
131    mod proptest {
132        use ::proptest::prelude::*;
133
134        use super::*;
135
136        proptest! {
137            #[test]
138            fn distinct_tokens_yield_distinct_pdas(
139                program_id in any::<[u8; 32]>(),
140                token1 in any::<[u8; 32]>(),
141                token2 in any::<[u8; 32]>(),
142            ) {
143                prop_assume!(token1 != token2);
144                let program_id = Pubkey::new_from_array(program_id);
145                let (pda1, _) = find_buffer_pda(&program_id, &Pubkey::new_from_array(token1));
146                let (pda2, _) = find_buffer_pda(&program_id, &Pubkey::new_from_array(token2));
147                prop_assert_ne!(pda1, pda2);
148            }
149        }
150    }
151}