1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use crate::*;

use kv_log_macro::info;
use solana_client::rpc_config::{RpcAccountInfoConfig, RpcProgramAccountsConfig};
use solana_sdk::commitment_config::{CommitmentConfig, CommitmentLevel};
use solana_sdk::signer::Signer;
use std::sync::Arc;

#[derive(Default, Debug, Clone)]
pub struct FunctionRequestFilters {
    pub attestation_queue: Option<Pubkey>,
    pub authority: Option<Pubkey>,
    pub is_triggered: Option<bool>,
    pub is_active: Option<bool>,
    // Note: This will not catch functions for which the verifier fetching
    // is the secondary oracle. This is because the verifier is not stored
    // on the function account but on the queue account.
    pub queue_idx: Option<u32>,
}

impl FunctionRequestFilters {
    pub fn to_vec(&self) -> Vec<solana_client::rpc_filter::RpcFilterType> {
        let mut filters = vec![FunctionRequestAccountData::get_discriminator_filter()];

        // AttestationQueue Filter
        if let Some(attestation_queue) = &self.attestation_queue {
            filters.push(FunctionRequestAccountData::get_queue_filter(
                attestation_queue,
            ));
        }

        // Authority Filter
        if let Some(authority) = &self.authority {
            filters.push(FunctionRequestAccountData::get_authority_filter(authority));
        }

        // Combine filters for efficiency
        if self.is_triggered.is_some() && self.is_active.is_some() {
            filters.push(FunctionRequestAccountData::get_is_triggered_and_active_filter());
        } else {
            // Is Triggered Filter
            if let Some(is_triggered) = &self.is_triggered {
                if *is_triggered {
                    filters.push(FunctionRequestAccountData::get_is_triggered_filter());
                }
            }

            // Is Active Filter
            if let Some(is_active) = &self.is_active {
                if *is_active {
                    filters.push(FunctionRequestAccountData::get_is_active_filter());
                }
            }
        }

        // Queue Idx Filter
        if let Some(queue_idx) = &self.queue_idx {
            filters.push(FunctionRequestAccountData::get_queue_idx_filter(queue_idx));
        }

        filters
    }
}

impl FunctionRequestAccountData {
    /////////////////////////////////////////////////////////////
    /// Client Methods
    /////////////////////////////////////////////////////////////
    ///

    pub async fn get_program_accounts(
        rpc: &solana_client::nonblocking::rpc_client::RpcClient,
        filters: FunctionRequestFilters,
        commitment: Option<CommitmentLevel>,
    ) -> Result<Vec<(Pubkey, FunctionRequestAccountData)>, SbError> {
        let mut requests = vec![];

        let accounts = rpc
            .get_program_accounts_with_config(
                &SWITCHBOARD_ATTESTATION_PROGRAM_ID,
                RpcProgramAccountsConfig {
                    filters: Some(filters.to_vec()),
                    account_config: RpcAccountInfoConfig {
                        encoding: Some(solana_account_decoder::UiAccountEncoding::Base64Zstd),
                        commitment: Some(CommitmentConfig {
                            commitment: commitment.unwrap_or(CommitmentLevel::Processed),
                        }),
                        ..Default::default()
                    },
                    ..Default::default()
                },
            )
            .await
            .map_err(|e| SbError::CustomError {
                message: "Failed to get program accounts".to_string(),
                source: Arc::new(e),
            })?;

        for (pubkey, account) in accounts {
            if let Ok(request_data) =
                FunctionRequestAccountData::try_deserialize(&mut &account.data[..])
            {
                requests.push((pubkey, request_data));
            }
        }

        Ok(requests)
    }

    pub async fn get_or_create_from_seed(
        rpc: &solana_client::nonblocking::rpc_client::RpcClient,
        payer: std::sync::Arc<Keypair>,
        function: Pubkey,
        seed: Option<&str>,
        params: Option<FunctionRequestInitAndTriggerParams>,
    ) -> Result<Pubkey, SbError> {
        let params = params.unwrap_or_default();
        let request_keypair = crate::keypair_from_base_seed(
            format!("request-{}-{}", function, seed.unwrap_or("default")).as_str(),
            payer.secret().to_bytes().to_vec(),
            Some(params.try_to_vec().unwrap()),
            None,
        )
        .unwrap();
        let request_pubkey = request_keypair.pubkey();

        if let Err(SbError::AccountNotFound) =
            FunctionRequestAccountData::fetch_async(rpc, request_pubkey).await
        {
            info!(
                "[Request] creating new request account {} ...",
                request_pubkey
            );

            // Fetch function_data and determine if we need authority signer
            let function_data = FunctionAccountData::fetch_async(rpc, function)
                .await
                .unwrap();
            if function_data.requests_require_authorization != 0
                && payer.pubkey() != function_data.authority
            {
                return Err(SbError::Message("MissingAuthoritySigner"));
            }

            // Build the request account and trigger it
            let req_init_ixn = FunctionRequestInitAndTrigger::build_ix(
                &FunctionRequestInitAndTriggerAccounts {
                    request: request_pubkey,
                    authority: payer.pubkey(),
                    function,
                    function_authority: None,
                    attestation_queue: function_data.attestation_queue,
                    payer: payer.pubkey(),
                },
                &params,
            )
            .unwrap();

            let recent_blockhash = retry!(3, 500, rpc.get_latest_blockhash().await)
                .await
                .map_err(|e| SbError::SolanaBlockhashFetchError(Arc::new(e)))?;

            let tx = crate::ix_to_tx(
                &[req_init_ixn],
                &[&*payer, &request_keypair],
                recent_blockhash,
            )
            .unwrap();

            let signature = rpc.send_and_confirm_transaction(&tx).await.unwrap();

            info!(
                "[Request] request {} initialized. Tx Signature: {}",
                request_pubkey, signature
            );
        };

        Ok(request_pubkey)
    }

    /////////////////////////////////////////////////////////////
    /// Fetch Methods
    /////////////////////////////////////////////////////////////

    pub fn fetch(
        client: &solana_client::rpc_client::RpcClient,
        pubkey: Pubkey,
    ) -> std::result::Result<Self, switchboard_common::SbError> {
        crate::client::fetch_borsh_account(client, pubkey)
    }

    pub async fn fetch_async(
        client: &solana_client::nonblocking::rpc_client::RpcClient,
        pubkey: Pubkey,
    ) -> std::result::Result<Self, switchboard_common::SbError> {
        crate::client::fetch_borsh_account_async(client, pubkey).await
    }

    pub fn fetch_sync<T: solana_sdk::client::SyncClient>(
        client: &T,
        pubkey: Pubkey,
    ) -> std::result::Result<Self, switchboard_common::SbError> {
        crate::client::fetch_borsh_account_sync(client, pubkey)
    }

    //

    pub fn get_discriminator_filter() -> solana_client::rpc_filter::RpcFilterType {
        solana_client::rpc_filter::RpcFilterType::Memcmp(
            solana_client::rpc_filter::Memcmp::new_raw_bytes(
                0,
                FunctionRequestAccountData::discriminator().to_vec(),
            ),
        )
    }

    pub fn get_is_triggered_filter() -> solana_client::rpc_filter::RpcFilterType {
        solana_client::rpc_filter::RpcFilterType::Memcmp(
            solana_client::rpc_filter::Memcmp::new_raw_bytes(8, vec![1u8]),
        )
    }

    pub fn get_is_active_filter() -> solana_client::rpc_filter::RpcFilterType {
        solana_client::rpc_filter::RpcFilterType::Memcmp(
            solana_client::rpc_filter::Memcmp::new_raw_bytes(
                9,
                vec![RequestStatus::RequestPending as u8],
            ),
        )
    }

    pub fn get_is_triggered_and_active_filter() -> solana_client::rpc_filter::RpcFilterType {
        solana_client::rpc_filter::RpcFilterType::Memcmp(
            solana_client::rpc_filter::Memcmp::new_raw_bytes(
                8,
                vec![1u8, RequestStatus::RequestPending as u8],
            ),
        )
    }

    pub fn get_queue_filter(queue_pubkey: &Pubkey) -> solana_client::rpc_filter::RpcFilterType {
        solana_client::rpc_filter::RpcFilterType::Memcmp(
            solana_client::rpc_filter::Memcmp::new_raw_bytes(138, queue_pubkey.to_bytes().into()),
        )
    }

    pub fn get_queue_idx_filter(queue_idx: &u32) -> solana_client::rpc_filter::RpcFilterType {
        solana_client::rpc_filter::RpcFilterType::Memcmp(
            solana_client::rpc_filter::Memcmp::new_raw_bytes(275, queue_idx.to_le_bytes().to_vec()),
        )
    }

    pub fn get_authority_filter(
        authority_pubkey: &Pubkey,
    ) -> solana_client::rpc_filter::RpcFilterType {
        solana_client::rpc_filter::RpcFilterType::Memcmp(
            solana_client::rpc_filter::Memcmp::new_raw_bytes(
                10,
                authority_pubkey.to_bytes().into(),
            ),
        )
    }

    pub fn get_is_ready_filters(
        queue_pubkey: &Pubkey,
    ) -> Vec<solana_client::rpc_filter::RpcFilterType> {
        vec![
            FunctionRequestAccountData::get_discriminator_filter(),
            FunctionRequestAccountData::get_is_triggered_filter(),
            FunctionRequestAccountData::get_is_active_filter(),
            FunctionRequestAccountData::get_queue_filter(queue_pubkey),
        ]
    }

    pub fn calc_container_params_hash(&self) -> [u8; 32] {
        solana_program::hash::hash(&self.container_params).to_bytes()
    }
}