Skip to main content

mina_node/ledger/read/
mod.rs

1mod ledger_read_actions;
2use ledger::{Account, AccountId};
3pub use ledger_read_actions::*;
4
5mod ledger_read_state;
6use crate::p2p::{channels::rpc::P2pRpcId, PeerId};
7pub use ledger_read_state::*;
8use mina_core::{
9    block::AppliedBlock,
10    requests::{RequestId, RpcId, RpcIdType},
11};
12use redux::Callback;
13
14mod ledger_read_reducer;
15
16use std::{collections::BTreeMap, sync::Arc};
17
18use mina_p2p_messages::v2;
19use serde::{Deserialize, Serialize};
20
21use crate::{
22    account::AccountPublicKey,
23    block_producer::vrf_evaluator::DelegatorTable,
24    ledger::LedgerAddress,
25    p2p::channels::rpc::StagedLedgerAuxAndPendingCoinbases,
26    rpc::{AccountQuery, RpcScanStateSummaryScanStateJob},
27};
28
29#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone, Copy)]
30#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
31pub enum LedgerReadKind {
32    DelegatorTable,
33    GetNumAccounts,
34    GetAccounts,
35    GetChildHashesAtAddr,
36    GetChildAccountsAtAddr,
37    GetStagedLedgerAuxAndPendingCoinbases,
38    ScanStateSummary,
39    AccountsForRpc,
40    GetLedgerStatus,
41    GetAccountDelegators,
42}
43
44#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
45pub enum LedgerReadRequest {
46    /// Delegator table requested by vrf state machine.
47    DelegatorTable(v2::LedgerHash, AccountPublicKey),
48    // p2p rpcs
49    GetNumAccounts(v2::LedgerHash),
50    GetAccounts(v2::LedgerHash, Vec<AccountId>, Option<RpcId>),
51    GetChildHashesAtAddr(v2::LedgerHash, LedgerAddress),
52    GetChildAccountsAtAddr(v2::LedgerHash, LedgerAddress),
53    GetStagedLedgerAuxAndPendingCoinbases(LedgerReadStagedLedgerAuxAndPendingCoinbases),
54    // rpcs
55    ScanStateSummary(v2::MinaBaseStagedLedgerHashStableV1),
56    AccountsForRpc(RpcId, v2::LedgerHash, AccountQuery),
57    GetLedgerStatus(RpcId, v2::LedgerHash),
58    GetAccountDelegators(RpcId, v2::LedgerHash, AccountId),
59}
60
61#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
62pub struct LedgerStatus {
63    pub num_accounts: u64,
64    pub best_tip_staged_ledger_hash: v2::LedgerHash,
65}
66
67#[derive(Serialize, Deserialize, Debug, Clone)]
68pub enum LedgerReadResponse {
69    /// Delegator table requested by vrf state machine.
70    DelegatorTable(Option<DelegatorTable>),
71    // p2p rpcs
72    GetNumAccounts(Option<(u64, v2::LedgerHash)>),
73    GetAccounts(Vec<Account>, Option<RpcId>),
74    GetChildHashesAtAddr(Option<(v2::LedgerHash, v2::LedgerHash)>),
75    GetChildAccountsAtAddr(Option<Vec<v2::MinaBaseAccountBinableArgStableV2>>),
76    GetStagedLedgerAuxAndPendingCoinbases(Option<Arc<StagedLedgerAuxAndPendingCoinbases>>),
77    // rpcs
78    ScanStateSummary(Result<Vec<Vec<RpcScanStateSummaryScanStateJob>>, String>),
79    AccountsForRpc(RpcId, Vec<Account>, AccountQuery),
80    GetLedgerStatus(RpcId, Option<LedgerStatus>),
81    GetAccountDelegators(RpcId, Option<Vec<Account>>),
82}
83
84#[derive(Serialize, Deserialize, Debug, Clone)]
85pub struct LedgerReadStagedLedgerAuxAndPendingCoinbases {
86    pub ledger_hash: v2::MinaBaseStagedLedgerHashStableV1,
87    pub protocol_states: BTreeMap<v2::StateHash, v2::MinaStateProtocolStateValueStableV2>,
88}
89
90impl LedgerReadRequest {
91    pub fn kind(&self) -> LedgerReadKind {
92        match self {
93            Self::DelegatorTable(..) => LedgerReadKind::DelegatorTable,
94            Self::GetNumAccounts(..) => LedgerReadKind::GetNumAccounts,
95            Self::GetAccounts(..) => LedgerReadKind::GetAccounts,
96            Self::GetChildAccountsAtAddr(..) => LedgerReadKind::GetChildAccountsAtAddr,
97            Self::GetChildHashesAtAddr(..) => LedgerReadKind::GetChildHashesAtAddr,
98            Self::GetStagedLedgerAuxAndPendingCoinbases(..) => {
99                LedgerReadKind::GetStagedLedgerAuxAndPendingCoinbases
100            }
101            Self::ScanStateSummary(..) => LedgerReadKind::ScanStateSummary,
102            Self::AccountsForRpc(..) => LedgerReadKind::AccountsForRpc,
103            Self::GetLedgerStatus(..) => LedgerReadKind::GetLedgerStatus,
104            Self::GetAccountDelegators(..) => LedgerReadKind::GetAccountDelegators,
105        }
106    }
107
108    pub fn cost(&self) -> usize {
109        let cost = match self {
110            Self::DelegatorTable(..) => 100,
111            Self::GetNumAccounts(..) => 1,
112            Self::GetAccounts(..) => 10, // Not sure if 10 is a good number here
113            Self::GetChildAccountsAtAddr(_, addr) => {
114                let height_diff = super::LEDGER_DEPTH.saturating_sub(addr.length());
115                let max_accounts_count = 2_u32.pow(height_diff as u32);
116                (max_accounts_count / 4) as usize
117            }
118            Self::GetChildHashesAtAddr(..) => 1,
119            Self::GetStagedLedgerAuxAndPendingCoinbases(..) => 100,
120            Self::ScanStateSummary(..) => 100,
121            // TODO(adonagy): not sure
122            Self::AccountsForRpc(..) => 10,
123            Self::GetLedgerStatus(..) => 1,
124            Self::GetAccountDelegators(..) => 10,
125        };
126        cost.max(1)
127    }
128}
129
130impl LedgerReadResponse {
131    pub fn kind(&self) -> LedgerReadKind {
132        match self {
133            Self::DelegatorTable(..) => LedgerReadKind::DelegatorTable,
134            Self::GetNumAccounts(..) => LedgerReadKind::GetNumAccounts,
135            Self::GetAccounts(..) => LedgerReadKind::GetAccounts,
136            Self::GetChildAccountsAtAddr(..) => LedgerReadKind::GetChildAccountsAtAddr,
137            Self::GetChildHashesAtAddr(..) => LedgerReadKind::GetChildHashesAtAddr,
138            Self::GetStagedLedgerAuxAndPendingCoinbases(..) => {
139                LedgerReadKind::GetStagedLedgerAuxAndPendingCoinbases
140            }
141            Self::ScanStateSummary(..) => LedgerReadKind::ScanStateSummary,
142            Self::AccountsForRpc(..) => LedgerReadKind::AccountsForRpc,
143            Self::GetLedgerStatus(..) => LedgerReadKind::GetLedgerStatus,
144            Self::GetAccountDelegators(..) => LedgerReadKind::GetAccountDelegators,
145        }
146    }
147}
148
149impl PartialEq for LedgerReadStagedLedgerAuxAndPendingCoinbases {
150    fn eq(&self, other: &Self) -> bool {
151        self.ledger_hash == other.ledger_hash
152    }
153}
154
155#[derive(Serialize, Deserialize, Debug, Clone)]
156pub enum LedgerReadInitCallback {
157    RpcLedgerAccountsGetPending {
158        callback: Callback<RequestId<RpcIdType>>,
159        args: RequestId<RpcIdType>,
160    },
161    RpcScanStateSummaryGetPending {
162        callback: Callback<(RequestId<RpcIdType>, AppliedBlock)>,
163        args: (RequestId<RpcIdType>, AppliedBlock),
164    },
165    P2pChannelsResponsePending {
166        callback: Callback<(bool, P2pRpcId, PeerId)>,
167        args: (bool, P2pRpcId, PeerId),
168    },
169    RpcLedgerStatusGetPending {
170        callback: Callback<RequestId<RpcIdType>>,
171        args: RequestId<RpcIdType>,
172    },
173    RpcLedgerAccountDelegatorsGetPending {
174        callback: Callback<RequestId<RpcIdType>>,
175        args: RequestId<RpcIdType>,
176    },
177    None,
178}