tycho-rpc 0.3.7

Public RPC service.
Documentation
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
use std::borrow::Cow;
use std::sync::OnceLock;

use axum::extract::State;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use bytes::Bytes;
use tycho_block_util::message::validate_external_message;
use tycho_types::cell::HashBytes;
use tycho_types::models::*;
use tycho_types::prelude::*;
use tycho_util::metrics::HistogramGuard;

pub use self::cache::ProtoEndpointCache;
use self::protos::rpc::{self, Request, request, response};
use crate::endpoint::proto::extractor::{
    ProtoErrorResponse, ProtoOkResponse, Protobuf, ProtobufRef,
};
use crate::state::{LoadedAccountState, RpcState, RpcStateError};
use crate::util::error_codes::*;

mod cache;
mod extractor;
mod protos;

macro_rules! declare_proto_methods {
    ($($name:ident),*$(,)?) => {
        trait RequestExt {
            fn method_name(&self) -> &'static str;
        }

        impl RequestExt for request::Call {
            fn method_name(&self) -> &'static str {
                match self {
                    $(request::Call::$name { .. } => stringify!($name)),*
                }
            }
        }
    };
}

declare_proto_methods! {
    GetCapabilities,
    GetLatestKeyBlock,
    GetBlockchainConfig,
    GetStatus,
    GetTimings,
    SendMessage,
    GetContractState,
    GetLibraryCell,
    GetAccountsByCodeHash,
    GetTransactionsList,
    GetTransaction,
    GetDstTransaction,
    GetTransactionBlockId,
    GetKeyBlockProof,
    GetBlockProof,
    GetBlockData,
}

pub async fn route(State(state): State<RpcState>, Protobuf(req): Protobuf<Request>) -> Response {
    let Some(call) = req.call else {
        return ProtoErrorResponse {
            code: METHOD_NOT_FOUND_CODE,
            message: "unknown method".into(),
        }
        .into_response();
    };

    let label = [("method", call.method_name())];
    let _hist = HistogramGuard::begin_with_labels("tycho_jrpc_request_time", &label);
    match call {
        request::Call::GetCapabilities(()) => {
            let result = get_capabilities(&state);
            (StatusCode::OK, ProtobufRef(result)).into_response()
        }
        request::Call::GetLatestKeyBlock(()) => {
            match &*state.proto_cache().load_latest_key_block() {
                Some(config) => config.as_ref().clone().into_response(),
                None => error_to_response(RpcStateError::NotReady),
            }
        }
        request::Call::GetBlockchainConfig(()) => {
            match &*state.proto_cache().load_blockchain_config() {
                Some(config) => config.as_ref().clone().into_response(),
                None => error_to_response(RpcStateError::NotReady),
            }
        }
        request::Call::GetStatus(()) => {
            let result = response::Result::GetStatus(response::GetStatus {
                ready: state.is_ready(),
            });
            ok_to_response(result)
        }
        request::Call::GetTimings(()) => {
            if state.is_ready() {
                let timings = state.load_timings();
                let result = response::Result::GetTimings(response::GetTimings {
                    last_mc_block_seqno: timings.last_mc_block_seqno,
                    last_mc_utime: timings.last_mc_utime,
                    mc_time_diff: timings.mc_time_diff,
                    smallest_known_lt: timings.smallest_known_lt,
                });
                ok_to_response(result)
            } else {
                error_to_response(RpcStateError::NotReady)
            }
        }
        request::Call::SendMessage(p) => {
            if let Err(e) = validate_external_message(&p.message).await {
                return ProtoErrorResponse {
                    code: INVALID_BOC_CODE,
                    message: e.to_string().into(),
                }
                .into_response();
            }

            state.broadcast_external_message(&p.message).await;
            ok_to_response(response::Result::SendMessage(()))
        }
        request::Call::GetLibraryCell(p) => {
            let Some(hash) = hash_from_bytes(p.hash) else {
                return invalid_params_response("invalid hash");
            };

            let res = match state.proto_cache().get_library_cell_response(&hash) {
                Some(value) => value,
                None => match state.get_raw_library(&hash) {
                    Ok(cell) => state.proto_cache().insert_library_cell_response(hash, cell),
                    Err(e) => return error_to_response(RpcStateError::Internal(e)),
                },
            };
            res.into_response()
        }
        request::Call::GetContractState(p) => {
            let Some(address) = addr_from_bytes(p.address) else {
                return invalid_params_response("invalid address");
            };

            let item = match state.get_account_state(&address) {
                Ok(item) => item,
                Err(e) => return error_to_response(e),
            };

            let response = match &item {
                &LoadedAccountState::NotFound { timings, .. } => response::GetContractState {
                    state: Some(response::get_contract_state::State::NotExists(
                        response::get_contract_state::NotExist {
                            gen_timings: Some(
                                response::get_contract_state::not_exist::GenTimings::Known(
                                    response::get_contract_state::Timings {
                                        gen_lt: timings.gen_lt,
                                        gen_utime: timings.gen_utime,
                                    },
                                ),
                            ),
                        },
                    )),
                },
                LoadedAccountState::Found { state, timings, .. }
                    if Some(state.last_trans_lt) <= p.last_transaction_lt =>
                {
                    response::GetContractState {
                        state: Some(response::get_contract_state::State::Unchanged(
                            response::get_contract_state::Timings {
                                gen_lt: timings.gen_lt,
                                gen_utime: timings.gen_utime,
                            },
                        )),
                    }
                }
                LoadedAccountState::Found { state, timings, .. } => {
                    let timings = response::get_contract_state::Timings {
                        gen_lt: timings.gen_lt,
                        gen_utime: timings.gen_utime,
                    };

                    let state = match state.load_account() {
                        Ok(Some(loaded)) => {
                            let account = match serialize_account(&loaded) {
                                Ok(account) => account,
                                Err(e) => {
                                    return error_to_response(RpcStateError::Internal(e.into()));
                                }
                            };
                            let last_transaction_id =
                                response::get_contract_state::exists::LastTransactionId::Exact(
                                    response::get_contract_state::exists::Exact {
                                        lt: state.last_trans_lt,
                                        hash: Bytes::copy_from_slice(
                                            state.last_trans_hash.as_slice(),
                                        ),
                                    },
                                );
                            response::get_contract_state::State::Exists(
                                response::get_contract_state::Exists {
                                    account,
                                    gen_timings: Some(timings),
                                    last_transaction_id: Some(last_transaction_id),
                                },
                            )
                        }
                        Ok(None) => response::get_contract_state::State::NotExists(
                            response::get_contract_state::NotExist {
                                gen_timings: Some(
                                    response::get_contract_state::not_exist::GenTimings::Known(
                                        response::get_contract_state::Timings {
                                            gen_lt: timings.gen_lt,
                                            gen_utime: timings.gen_utime,
                                        },
                                    ),
                                ),
                            },
                        ),
                        Err(e) => return error_to_response(RpcStateError::Internal(e.into())),
                    };

                    response::GetContractState { state: Some(state) }
                }
            };

            ok_to_response(response::Result::GetContractState(response))
        }
        request::Call::GetAccountsByCodeHash(p) => {
            if p.limit == 0 {
                let result = response::Result::GetAccounts(response::GetAccountsByCodeHash {
                    account: Vec::new(),
                });
                return ok_to_response(result);
            } else if p.limit > MAX_LIMIT {
                return too_large_limit_response();
            }

            let Some(code_hash) = hash_from_bytes(p.code_hash) else {
                return invalid_params_response("invalid code hash");
            };

            let continuation = match p.continuation.map(addr_from_bytes) {
                Some(Some(continuation)) => Some(continuation),
                Some(None) => return invalid_params_response("invalid continuation"),
                None => None,
            };

            match state.get_accounts_by_code_hash(&code_hash, continuation.as_ref(), None) {
                Ok(list) => {
                    let result = response::Result::GetAccounts(response::GetAccountsByCodeHash {
                        account: list
                            .into_raw()
                            .take(p.limit as usize)
                            .map(|addr| Bytes::copy_from_slice(&addr))
                            .collect(),
                    });
                    ok_to_response(result)
                }
                Err(e) => error_to_response(e),
            }
        }
        request::Call::GetTransactionsList(p) => {
            if p.limit == 0 {
                let result = response::Result::GetTransactionsList(response::GetTransactionsList {
                    transactions: Vec::new(),
                });
                return ok_to_response(result);
            } else if p.limit > MAX_LIMIT {
                return too_large_limit_response();
            }

            let Some(account) = addr_from_bytes(p.account) else {
                return invalid_params_response("invalid address");
            };

            match state.get_transactions(&account, None, p.last_transaction_lt, true, None) {
                Ok(list) => {
                    let transactions = list
                        .map(|data| {
                            let data = Bytes::copy_from_slice(data);
                            Some(data)
                        })
                        .take(p.limit as usize)
                        .collect();
                    ok_to_response(response::Result::GetTransactionsList(
                        response::GetTransactionsList { transactions },
                    ))
                }
                Err(e) => error_to_response(e),
            }
        }
        request::Call::GetTransaction(p) => {
            let Some(hash) = hash_from_bytes(p.id) else {
                return invalid_params_response("invalid tx id");
            };

            match state.get_transaction(&hash, None) {
                Ok(tx) => ok_to_response(response::Result::GetRawTransaction(
                    response::GetRawTransaction {
                        transaction: tx.map(|slice| Bytes::copy_from_slice(slice.as_ref())),
                    },
                )),
                Err(e) => error_to_response(e),
            }
        }
        request::Call::GetDstTransaction(p) => {
            let Some(hash) = hash_from_bytes(p.message_hash) else {
                return invalid_params_response("invalid msg id");
            };

            match state.get_dst_transaction(&hash, None) {
                Ok(tx) => ok_to_response(response::Result::GetRawTransaction(
                    response::GetRawTransaction {
                        transaction: tx.map(|slice| Bytes::copy_from_slice(slice.as_ref())),
                    },
                )),
                Err(e) => error_to_response(e),
            }
        }
        request::Call::GetTransactionBlockId(p) => {
            let Some(hash) = hash_from_bytes(p.id) else {
                return invalid_params_response("invalid tx id");
            };

            match state.get_transaction_info(&hash, None) {
                Ok(info) => ok_to_response(response::Result::GetTransactionBlockId(
                    response::GetTransactionBlockId {
                        block_id: info.map(|info| make_response_block_id(info.block_id)),
                    },
                )),
                Err(e) => error_to_response(e),
            }
        }
        request::Call::GetKeyBlockProof(p) => {
            let res = match state.proto_cache().get_key_block_proof_response(p.seqno) {
                Some(value) => value,
                None => {
                    let proof = state
                        .get_key_block_proof(p.seqno)
                        .await
                        .map(|(block_id, r)| (block_id, Bytes::copy_from_slice(r.as_ref())));

                    state
                        .proto_cache()
                        .insert_key_block_proof_response(p.seqno, proof)
                }
            };
            res.into_response()
        }
        request::Call::GetBlockProof(p) => {
            if !state.config().allow_huge_requests {
                return error_to_response(RpcStateError::NotSupported);
            }

            let Some(block_id) = get_block_id(p) else {
                return invalid_params_response("invalid block id");
            };

            let proof = state.get_block_proof(&block_id).await;
            ok_to_response(response::Result::GetBlockProof(response::BlockProof {
                proof: proof.map(Bytes::from_owner),
            }))
        }
        request::Call::GetBlockData(p) => {
            if !state.config().allow_huge_requests {
                return error_to_response(RpcStateError::NotSupported);
            }

            let Some(block_id) = get_block_id(p) else {
                return invalid_params_response("invalid block id");
            };

            // TODO: Rework rate limiting for this request.
            let _permit = state.acquire_download_block_permit().await;

            let Some(data) = state.get_block_data(&block_id).await else {
                return ok_to_response(response::Result::GetBlockData(response::BlockData {
                    data: None,
                }));
            };

            tycho_util::sync::rayon_run(move || {
                ok_to_response(response::Result::GetBlockData(response::BlockData {
                    data: Some(Bytes::from_owner(data)),
                }))
            })
            .await
        }
    }
}

// NOTE: `RpcState` full/not-full state is determined only once at startup,
// so it is ok to cache the response.
fn get_capabilities(state: &RpcState) -> &'static rpc::Response {
    static RESULT: OnceLock<rpc::Response> = OnceLock::new();
    RESULT.get_or_init(|| {
        // FIXME: Why strings when we have enums in the proto?

        let mut capabilities = vec![
            "getCapabilities",
            "getLatestKeyBlock",
            "getBlockchainConfig",
            "getStatus",
            "getTimings",
            "getContractState",
            "sendMessage",
            "getLibraryCell",
            "getKeyBlockProof",
        ];

        if state.config().allow_huge_requests {
            capabilities.extend(["getBlockProof", "getBlockData"]);
        }

        if state.is_full() {
            capabilities.extend([
                "getTransactionsList",
                "getTransaction",
                "getDstTransaction",
                "getAccountsByCodeHash",
                "getTransactionBlockId",
            ]);
        }

        rpc::Response {
            result: Some(response::Result::GetCapabilities(
                response::GetCapabilities {
                    capabilities: capabilities.into_iter().map(|s| s.into()).collect(),
                },
            )),
        }
    })
}

fn ok_to_response(result: response::Result) -> Response {
    ProtoOkResponse::new(result).into_response()
}

fn error_to_response(e: RpcStateError) -> Response {
    let (code, message) = match e {
        RpcStateError::NotReady => (NOT_READY_CODE, Cow::Borrowed("not ready")),
        RpcStateError::NotSupported => (NOT_SUPPORTED_CODE, Cow::Borrowed("method not supported")),
        RpcStateError::Internal(e) => (INTERNAL_ERROR_CODE, e.to_string().into()),
        RpcStateError::BadRequest(e) => (INVALID_PARAMS_CODE, e.to_string().into()),
    };

    ProtoErrorResponse { code, message }.into_response()
}

fn too_large_limit_response() -> Response {
    ProtoErrorResponse {
        code: TOO_LARGE_LIMIT_CODE,
        message: Cow::Borrowed("limit is too large"),
    }
    .into_response()
}

fn invalid_params_response(reason: &'static str) -> Response {
    ProtoErrorResponse {
        code: INVALID_PARAMS_CODE,
        message: Cow::Borrowed(reason),
    }
    .into_response()
}

fn addr_from_bytes(bytes: Bytes) -> Option<StdAddr> {
    (bytes.len() == 33)
        .then(|| StdAddr::new(bytes[0] as i8, HashBytes(bytes[1..33].try_into().unwrap())))
}

fn hash_from_bytes(bytes: Bytes) -> Option<HashBytes> {
    (bytes.len() == 32).then(|| HashBytes::from_slice(&bytes))
}

fn get_block_id(block_id: request::GetBlock) -> Option<BlockId> {
    Some(BlockId {
        shard: ShardIdent::new(block_id.workchain, block_id.shard)?,
        seqno: block_id.seqno,
        root_hash: hash_from_bytes(block_id.root_hash)?,
        file_hash: hash_from_bytes(block_id.file_hash)?,
    })
}

fn make_response_block_id(id: BlockId) -> response::BlockId {
    response::BlockId {
        workchain: id.shard.workchain(),
        shard: id.shard.prefix(),
        seqno: id.seqno,
        root_hash: Bytes::copy_from_slice(id.root_hash.as_ref()),
        file_hash: Bytes::copy_from_slice(id.file_hash.as_ref()),
    }
}

fn serialize_account(account: &Account) -> Result<Bytes, tycho_types::error::Error> {
    let cell = crate::models::serialize_account(account)?;
    Ok(Boc::encode(cell).into())
}

const MAX_LIMIT: u32 = 100;