Skip to content

Commit 48228e2

Browse files
author
Adrian Nagy
committed
feat: Add ledger status RPC, expand deamonStatus
1 parent fb696b7 commit 48228e2

File tree

17 files changed

+221
-20
lines changed

17 files changed

+221
-20
lines changed

node/common/src/service/rpc/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use node::rpc::{
1616
RpcReadinessCheckResponse, RpcRequest, RpcSnarkPoolCompletedJobsResponse,
1717
RpcSnarkPoolPendingJobsGetResponse, RpcStateGetError, RpcStatusGetResponse,
1818
RpcTransactionInjectResponse, RpcTransactionPoolResponse, RpcTransactionStatusGetResponse,
19-
RpcTransitionFrontierUserCommandsResponse,
19+
RpcTransitionFrontierUserCommandsResponse, RpcLedgerStatusGetResponse,
2020
};
2121
use serde::{Deserialize, Serialize};
2222

@@ -326,6 +326,7 @@ impl node::rpc_effectful::RpcService for NodeService {
326326
);
327327
rpc_service_impl!(respond_genesis_block, RpcGenesisBlockResponse);
328328
rpc_service_impl!(respond_consensus_time_get, RpcConsensusTimeGetResponse);
329+
rpc_service_impl!(respond_ledger_status_get, RpcLedgerStatusGetResponse);
329330
}
330331

331332
#[cfg(test)]

node/native/src/graphql/constants.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,42 @@ impl GraphQLDaemonStatus {
120120
}))
121121
}
122122

123+
async fn ledger_merkle_root(&self, context: &Context) -> juniper::FieldResult<Option<String>> {
124+
let best_tip = context.get_or_fetch_best_tip().await;
125+
126+
Ok(best_tip.map(|best_tip| best_tip.merkle_root_hash().to_string()))
127+
// match best_tip {
128+
// Some(best_tip) => {
129+
// println!("best_tip_ledger_merkle_root {:?}", best_tip.merkle_root_hash());
130+
// let ledger_status = context
131+
// .get_or_fetch_ledger_status(best_tip.merkle_root_hash())
132+
// .await;
133+
// Ok(ledger_status
134+
// .map(|ledger_status| ledger_status.best_tip_staged_ledger_hash.to_string()))
135+
// }
136+
// None => Ok(None),
137+
// }
138+
}
139+
140+
async fn state_hash(&self, context: &Context) -> juniper::FieldResult<Option<String>> {
141+
let best_tip = context.get_or_fetch_best_tip().await;
142+
Ok(best_tip.map(|best_tip| best_tip.hash().to_string()))
143+
}
144+
145+
async fn num_accounts(&self, context: &Context) -> juniper::FieldResult<Option<i32>> {
146+
let best_tip = context.get_or_fetch_best_tip().await;
147+
148+
match best_tip {
149+
Some(best_tip) => {
150+
let ledger_status = context
151+
.get_or_fetch_ledger_status(best_tip.merkle_root_hash())
152+
.await;
153+
Ok(ledger_status.map(|ledger_status| ledger_status.num_accounts as i32))
154+
}
155+
None => Ok(None),
156+
}
157+
}
158+
123159
// highestUnvalidatedBlockLengthReceived
124160
async fn highest_unvalidated_block_length_received(
125161
&self,

node/native/src/graphql/mod.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ use juniper::{graphql_value, EmptySubscription, FieldError, GraphQLEnum, RootNod
33
use ledger::Account;
44
use mina_p2p_messages::v2::{
55
conv, MinaBaseSignedCommandStableV2, MinaBaseUserCommandStableV2,
6-
MinaBaseZkappCommandTStableV1WireStableV1, TokenIdKeyHash, TransactionHash,
6+
MinaBaseZkappCommandTStableV1WireStableV1, TokenIdKeyHash, TransactionHash, LedgerHash
77
};
88
use node::{
99
account::AccountPublicKey,
10+
ledger::read::LedgerStatus,
1011
rpc::{
1112
AccountQuery, GetBlockQuery, PooledCommandsQuery, RpcGenesisBlockResponse,
1213
RpcGetBlockResponse, RpcPooledUserCommandsResponse, RpcPooledZkappCommandsResponse,
1314
RpcRequest, RpcSnarkPoolCompletedJobsResponse, RpcSnarkPoolPendingJobsGetResponse,
1415
RpcSyncStatsGetResponse, RpcTransactionInjectResponse, RpcTransactionStatusGetResponse,
15-
SyncStatsQuery, RpcStatusGetResponse, RpcNodeStatus, RpcBestChainResponse
16+
SyncStatsQuery, RpcStatusGetResponse, RpcNodeStatus, RpcBestChainResponse,
17+
RpcLedgerStatusGetResponse
1618
},
1719
stats::sync::SyncKind,
1820
BuildEnv,
@@ -103,6 +105,7 @@ pub(crate) struct Context {
103105
rpc_sender: RpcSender,
104106
statemachine_status_cache: OnceCell<Option<RpcNodeStatus>>,
105107
best_tip_cache: OnceCell<Option<AppliedBlock>>,
108+
ledger_status_cache: OnceCell<Option<LedgerStatus>>,
106109
}
107110

108111
impl juniper::Context for Context {}
@@ -113,6 +116,7 @@ impl Context {
113116
rpc_sender,
114117
statemachine_status_cache: OnceCell::new(),
115118
best_tip_cache: OnceCell::new(),
119+
ledger_status_cache: OnceCell::new(),
116120
}
117121
}
118122

@@ -139,6 +143,21 @@ impl Context {
139143
.await
140144
.clone()
141145
}
146+
147+
pub(crate) async fn get_or_fetch_ledger_status(
148+
&self,
149+
ledger_hash: &LedgerHash,
150+
) -> RpcLedgerStatusGetResponse {
151+
self.ledger_status_cache
152+
.get_or_init(|| async {
153+
self.rpc_sender
154+
.oneshot_request(RpcRequest::LedgerStatusGet(ledger_hash.clone()))
155+
.await
156+
.flatten()
157+
})
158+
.await
159+
.clone()
160+
}
142161
}
143162

144163
#[derive(Clone, Copy, Debug, GraphQLEnum)]

node/src/action_kind.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,9 @@ pub enum ActionKind {
484484
RpcLedgerAccountsGetInit,
485485
RpcLedgerAccountsGetPending,
486486
RpcLedgerAccountsGetSuccess,
487+
RpcLedgerStatusGetInit,
488+
RpcLedgerStatusGetPending,
489+
RpcLedgerStatusGetSuccess,
487490
RpcMessageProgressGet,
488491
RpcP2pConnectionIncomingAnswerReady,
489492
RpcP2pConnectionIncomingError,
@@ -534,6 +537,7 @@ pub enum ActionKind {
534537
RpcEffectfulHealthCheck,
535538
RpcEffectfulHeartbeatGet,
536539
RpcEffectfulLedgerAccountsGetSuccess,
540+
RpcEffectfulLedgerStatusGetSuccess,
537541
RpcEffectfulMessageProgressGet,
538542
RpcEffectfulP2pConnectionIncomingError,
539543
RpcEffectfulP2pConnectionIncomingRespond,
@@ -730,7 +734,7 @@ pub enum ActionKind {
730734
}
731735

732736
impl ActionKind {
733-
pub const COUNT: u16 = 620;
737+
pub const COUNT: u16 = 624;
734738
}
735739

736740
impl std::fmt::Display for ActionKind {
@@ -1103,6 +1107,9 @@ impl ActionKindGet for RpcAction {
11031107
Self::TransactionStatusGet { .. } => ActionKind::RpcTransactionStatusGet,
11041108
Self::BlockGet { .. } => ActionKind::RpcBlockGet,
11051109
Self::ConsensusTimeGet { .. } => ActionKind::RpcConsensusTimeGet,
1110+
Self::LedgerStatusGetInit { .. } => ActionKind::RpcLedgerStatusGetInit,
1111+
Self::LedgerStatusGetPending { .. } => ActionKind::RpcLedgerStatusGetPending,
1112+
Self::LedgerStatusGetSuccess { .. } => ActionKind::RpcLedgerStatusGetSuccess,
11061113
Self::PooledUserCommands { .. } => ActionKind::RpcPooledUserCommands,
11071114
Self::PooledZkappCommands { .. } => ActionKind::RpcPooledZkappCommands,
11081115
Self::GenesisBlock { .. } => ActionKind::RpcGenesisBlock,
@@ -1180,6 +1187,7 @@ impl ActionKindGet for RpcEffectfulAction {
11801187
Self::PooledZkappCommands { .. } => ActionKind::RpcEffectfulPooledZkappCommands,
11811188
Self::GenesisBlock { .. } => ActionKind::RpcEffectfulGenesisBlock,
11821189
Self::ConsensusTimeGet { .. } => ActionKind::RpcEffectfulConsensusTimeGet,
1190+
Self::LedgerStatusGetSuccess { .. } => ActionKind::RpcEffectfulLedgerStatusGetSuccess,
11831191
}
11841192
}
11851193
}

node/src/event_source/event.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ impl std::fmt::Display for Event {
7979
RpcRequest::PooledZkappCommands(..) => write!(f, "PooledZkappCommands"),
8080
RpcRequest::GenesisBlockGet => write!(f, "GenesisBlock"),
8181
RpcRequest::ConsensusTimeGet(..) => write!(f, "ConsensusTimeGet"),
82+
RpcRequest::LedgerStatusGet(..) => write!(f, "LedgerStatusGet"),
8283
}
8384
}
8485
Self::ExternalSnarkWorker(event) => {

node/src/event_source/event_source_effects.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,19 @@ pub fn event_source_effects<S: Service>(store: &mut Store<S>, action: EventSourc
402402
}
403403
RpcRequest::PooledZkappCommands(query) => {
404404
store.dispatch(RpcAction::PooledZkappCommands { rpc_id, query });
405+
}
405406
RpcRequest::ConsensusTimeGet(query) => {
406407
store.dispatch(RpcAction::ConsensusTimeGet { rpc_id, query });
407408
}
408409
RpcRequest::GenesisBlockGet => {
409410
store.dispatch(RpcAction::GenesisBlock { rpc_id });
410411
}
412+
RpcRequest::LedgerStatusGet(ledger_hash) => {
413+
store.dispatch(RpcAction::LedgerStatusGetInit {
414+
rpc_id,
415+
ledger_hash,
416+
});
417+
}
411418
},
412419
Event::ExternalSnarkWorker(e) => match e {
413420
ExternalSnarkWorkerEvent::Started => {

node/src/ledger/ledger_manager.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{
2-
read::{LedgerReadId, LedgerReadRequest, LedgerReadResponse},
2+
read::{LedgerReadId, LedgerReadRequest, LedgerReadResponse, LedgerStatus},
33
write::{LedgerWriteRequest, LedgerWriteResponse},
4-
{LedgerCtx, LedgerService},
4+
LedgerCtx, LedgerService,
55
};
66
use crate::{
77
account::AccountPublicKey, ledger::LedgerAddress, rpc::AccountQuery,
@@ -250,6 +250,16 @@ impl LedgerRequest {
250250

251251
LedgerReadResponse::AccountsForRpc(rpc_id, res, account_query)
252252
}
253+
LedgerReadRequest::GetLedgerStatus(rpc_id, ledger_hash) => {
254+
let res = ledger_ctx.get_num_accounts(ledger_hash).map(
255+
|(num_accounts, ledger_hash)| LedgerStatus {
256+
num_accounts,
257+
best_tip_staged_ledger_hash: ledger_hash,
258+
},
259+
);
260+
261+
LedgerReadResponse::GetLedgerStatus(rpc_id, res)
262+
}
253263
},
254264
),
255265
LedgerRequest::AccountsSet {

node/src/ledger/read/ledger_read_reducer.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ impl LedgerReadState {
199199
account_query,
200200
});
201201
}
202+
(_, LedgerReadResponse::GetLedgerStatus(rpc_id, resp)) => {
203+
dispatcher.push(RpcAction::LedgerStatusGetSuccess {
204+
rpc_id,
205+
response: resp.clone(),
206+
});
207+
}
202208
}
203209
}
204210

node/src/ledger/read/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub enum LedgerReadKind {
3434
GetStagedLedgerAuxAndPendingCoinbases,
3535
ScanStateSummary,
3636
AccountsForRpc,
37+
GetLedgerStatus,
3738
}
3839

3940
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
@@ -49,6 +50,13 @@ pub enum LedgerReadRequest {
4950
// rpcs
5051
ScanStateSummary(v2::MinaBaseStagedLedgerHashStableV1),
5152
AccountsForRpc(RpcId, v2::LedgerHash, AccountQuery),
53+
GetLedgerStatus(RpcId, v2::LedgerHash),
54+
}
55+
56+
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
57+
pub struct LedgerStatus {
58+
pub num_accounts: u64,
59+
pub best_tip_staged_ledger_hash: v2::LedgerHash,
5260
}
5361

5462
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -64,6 +72,7 @@ pub enum LedgerReadResponse {
6472
// rpcs
6573
ScanStateSummary(Result<Vec<Vec<RpcScanStateSummaryScanStateJob>>, String>),
6674
AccountsForRpc(RpcId, Vec<Account>, AccountQuery),
75+
GetLedgerStatus(RpcId, Option<LedgerStatus>),
6776
}
6877

6978
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -85,6 +94,7 @@ impl LedgerReadRequest {
8594
}
8695
Self::ScanStateSummary(..) => LedgerReadKind::ScanStateSummary,
8796
Self::AccountsForRpc(..) => LedgerReadKind::AccountsForRpc,
97+
Self::GetLedgerStatus(..) => LedgerReadKind::GetLedgerStatus,
8898
}
8999
}
90100

@@ -103,6 +113,7 @@ impl LedgerReadRequest {
103113
Self::ScanStateSummary(..) => 100,
104114
// TODO(adonagy): not sure
105115
Self::AccountsForRpc(..) => 10,
116+
Self::GetLedgerStatus(..) => 1,
106117
};
107118
cost.max(1)
108119
}
@@ -121,6 +132,7 @@ impl LedgerReadResponse {
121132
}
122133
Self::ScanStateSummary(..) => LedgerReadKind::ScanStateSummary,
123134
Self::AccountsForRpc(..) => LedgerReadKind::AccountsForRpc,
135+
Self::GetLedgerStatus(..) => LedgerReadKind::GetLedgerStatus,
124136
}
125137
}
126138
}
@@ -145,5 +157,9 @@ pub enum LedgerReadInitCallback {
145157
callback: Callback<(bool, P2pRpcId, PeerId)>,
146158
args: (bool, P2pRpcId, PeerId),
147159
},
160+
RpcLedgerStatusGetPending {
161+
callback: Callback<RequestId<RpcIdType>>,
162+
args: RequestId<RpcIdType>,
163+
},
148164
None,
149165
}

node/src/ledger_effectful/ledger_effectful_effects.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ pub fn ledger_effectful_effects<S>(
4343
LedgerReadInitCallback::P2pChannelsResponsePending { callback, args } => {
4444
store.dispatch_callback(callback, args);
4545
}
46+
LedgerReadInitCallback::RpcLedgerStatusGetPending { callback, args } => {
47+
store.dispatch_callback(callback, args);
48+
}
4649
LedgerReadInitCallback::None => {}
4750
}
4851
}

0 commit comments

Comments
 (0)