surfpool_core/rpc/
utils.rs

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
use std::{any::type_name, io::Write, sync::Arc};

use base64::prelude::*;
use bincode::Options;
use jsonrpc_core::{Error, Result};
use solana_account::Account;
use solana_account_decoder::{UiAccount, UiAccountData, UiAccountEncoding};
use solana_client::{
    rpc_config::{RpcAccountInfoConfig, RpcTokenAccountsFilter},
    rpc_custom_error::RpcCustomError,
    rpc_filter::RpcFilterType,
    rpc_request::{TokenAccountsFilter, MAX_GET_CONFIRMED_SIGNATURES_FOR_ADDRESS2_LIMIT},
};
use solana_runtime::verify_precompiles::verify_precompiles;
use solana_sdk::{
    hash::Hash, packet::PACKET_DATA_SIZE, pubkey::Pubkey, signature::Signature,
    transaction::SanitizedTransaction,
};
use solana_transaction_status::TransactionBinaryEncoding;

fn optimize_filters(filters: &mut [RpcFilterType]) {
    filters.iter_mut().for_each(|filter_type| {
        if let RpcFilterType::Memcmp(compare) = filter_type {
            if let Err(err) = compare.convert_to_raw_bytes() {
                // All filters should have been previously verified
                warn!("Invalid filter: bytes could not be decoded, {err}");
            }
        }
    })
}

fn verify_transaction(
    transaction: &SanitizedTransaction,
    feature_set: &Arc<solana_feature_set::FeatureSet>,
) -> Result<()> {
    #[allow(clippy::question_mark)]
    if transaction.verify().is_err() {
        return Err(RpcCustomError::TransactionSignatureVerificationFailure.into());
    }

    let move_precompile_verification_to_svm =
        feature_set.is_active(&solana_feature_set::move_precompile_verification_to_svm::id());
    if !move_precompile_verification_to_svm {
        if let Err(e) = verify_precompiles(transaction, feature_set) {
            return Err(RpcCustomError::TransactionPrecompileVerificationFailure(e).into());
        }
    }

    Ok(())
}

fn verify_filter(input: &RpcFilterType) -> Result<()> {
    input
        .verify()
        .map_err(|e| Error::invalid_params(format!("Invalid param: {e:?}")))
}

pub fn verify_pubkey(input: &str) -> Result<Pubkey> {
    input
        .parse()
        .map_err(|e| Error::invalid_params(format!("Invalid param: {e:?}")))
}

fn verify_hash(input: &str) -> Result<Hash> {
    input
        .parse()
        .map_err(|e| Error::invalid_params(format!("Invalid param: {e:?}")))
}

fn verify_signature(input: &str) -> Result<Signature> {
    input
        .parse()
        .map_err(|e| Error::invalid_params(format!("Invalid param: {e:?}")))
}

fn verify_token_account_filter(
    token_account_filter: RpcTokenAccountsFilter,
) -> Result<TokenAccountsFilter> {
    match token_account_filter {
        RpcTokenAccountsFilter::Mint(mint_str) => {
            let mint = verify_pubkey(&mint_str)?;
            Ok(TokenAccountsFilter::Mint(mint))
        }
        RpcTokenAccountsFilter::ProgramId(program_id_str) => {
            let program_id = verify_pubkey(&program_id_str)?;
            Ok(TokenAccountsFilter::ProgramId(program_id))
        }
    }
}

fn verify_and_parse_signatures_for_address_params(
    address: String,
    before: Option<String>,
    until: Option<String>,
    limit: Option<usize>,
) -> Result<(Pubkey, Option<Signature>, Option<Signature>, usize)> {
    let address = verify_pubkey(&address)?;
    let before = before
        .map(|ref before| verify_signature(before))
        .transpose()?;
    let until = until.map(|ref until| verify_signature(until)).transpose()?;
    let limit = limit.unwrap_or(MAX_GET_CONFIRMED_SIGNATURES_FOR_ADDRESS2_LIMIT);

    if limit == 0 || limit > MAX_GET_CONFIRMED_SIGNATURES_FOR_ADDRESS2_LIMIT {
        return Err(Error::invalid_params(format!(
            "Invalid limit; max {MAX_GET_CONFIRMED_SIGNATURES_FOR_ADDRESS2_LIMIT}"
        )));
    }
    Ok((address, before, until, limit))
}

const MAX_BASE58_SIZE: usize = 1683; // Golden, bump if PACKET_DATA_SIZE changes
const MAX_BASE64_SIZE: usize = 1644; // Golden, bump if PACKET_DATA_SIZE changes
pub fn decode_and_deserialize<T>(
    encoded: String,
    encoding: TransactionBinaryEncoding,
) -> Result<(Vec<u8>, T)>
where
    T: serde::de::DeserializeOwned,
{
    let wire_output = match encoding {
        TransactionBinaryEncoding::Base58 => {
            if encoded.len() > MAX_BASE58_SIZE {
                return Err(Error::invalid_params(format!(
                    "base58 encoded {} too large: {} bytes (max: encoded/raw {}/{})",
                    type_name::<T>(),
                    encoded.len(),
                    MAX_BASE58_SIZE,
                    PACKET_DATA_SIZE,
                )));
            }
            bs58::decode(encoded)
                .into_vec()
                .map_err(|e| Error::invalid_params(format!("invalid base58 encoding: {e:?}")))?
        }
        TransactionBinaryEncoding::Base64 => {
            if encoded.len() > MAX_BASE64_SIZE {
                return Err(Error::invalid_params(format!(
                    "base64 encoded {} too large: {} bytes (max: encoded/raw {}/{})",
                    type_name::<T>(),
                    encoded.len(),
                    MAX_BASE64_SIZE,
                    PACKET_DATA_SIZE,
                )));
            }
            BASE64_STANDARD
                .decode(encoded)
                .map_err(|e| Error::invalid_params(format!("invalid base64 encoding: {e:?}")))?
        }
    };
    if wire_output.len() > PACKET_DATA_SIZE {
        return Err(Error::invalid_params(format!(
            "decoded {} too large: {} bytes (max: {} bytes)",
            type_name::<T>(),
            wire_output.len(),
            PACKET_DATA_SIZE
        )));
    }
    bincode::options()
        .with_limit(PACKET_DATA_SIZE as u64)
        .with_fixint_encoding()
        .allow_trailing_bytes()
        .deserialize_from(&wire_output[..])
        .map_err(|err| {
            Error::invalid_params(format!(
                "failed to deserialize {}: {}",
                type_name::<T>(),
                &err.to_string()
            ))
        })
        .map(|output| (wire_output, output))
}

pub fn transform_account_to_ui_account(
    account: &Option<Account>,
    config: &RpcAccountInfoConfig,
) -> Result<Option<UiAccount>> {
    if let Some(account) = account {
        Ok(Some(UiAccount {
            lamports: account.lamports,
            owner: account.owner.to_string(),
            data: {
                let account_data = if let Some(data_slice) = config.data_slice {
                    let end =
                        std::cmp::min(account.data.len(), data_slice.offset + data_slice.length);
                    account.data.clone()[data_slice.offset..end].to_vec()
                } else {
                    account.data.clone()
                };

                match config.encoding {
                    Some(UiAccountEncoding::Base58) => UiAccountData::Binary(
                        bs58::encode(account_data).into_string(),
                        UiAccountEncoding::Base58,
                    ),
                    Some(UiAccountEncoding::Base64) => UiAccountData::Binary(
                        BASE64_STANDARD.encode(account_data),
                        UiAccountEncoding::Base64,
                    ),
                    Some(UiAccountEncoding::Base64Zstd) => {
                        let mut data = Vec::with_capacity(account_data.len());

                        // Default compression level
                        match zstd::Encoder::new(&mut data, 0).and_then(|mut encoder| {
                            encoder
                                .write_all(&account_data)
                                .and_then(|_| encoder.finish())
                        }) {
                            Ok(_) => UiAccountData::Binary(
                                BASE64_STANDARD.encode(&data),
                                UiAccountEncoding::Base64Zstd,
                            ),
                            // Falling back on standard base64 encoding if compression failed
                            Err(err) => {
                                eprintln!("Zstd compression failed: {err}");
                                UiAccountData::Binary(
                                    BASE64_STANDARD.encode(&account_data),
                                    UiAccountEncoding::Base64,
                                )
                            }
                        }
                    }
                    None => UiAccountData::Binary(
                        bs58::encode(account_data.clone()).into_string(),
                        UiAccountEncoding::Base58,
                    ),
                    encoding => Err(jsonrpc_core::Error::invalid_params(format!(
                        "Encoding {encoding:?} is not supported yet."
                    )))?,
                }
            },
            executable: account.executable,
            rent_epoch: account.rent_epoch,
            space: Some(account.data.len() as u64),
        }))
    } else {
        Ok(None)
    }
}
// fn sanitize_transaction(
//     transaction: VersionedTransaction,
//     address_loader: impl AddressLoader,
//     reserved_account_keys: &HashSet<Pubkey>,
// ) -> Result<RuntimeTransaction<SanitizedTransaction>> {
//     RuntimeTransaction::try_create(
//         transaction,
//         MessageHash::Compute,
//         None,
//         address_loader,
//         reserved_account_keys,
//     )
//     .map_err(|err| Error::invalid_params(format!("invalid transaction: {err}")))
// }