From 168b71aa7166e879351d21521c055f786f1459e4 Mon Sep 17 00:00:00 2001 From: Sebastien Chapuis Date: Thu, 11 Apr 2024 13:22:25 +0200 Subject: [PATCH] Fix usage of `SnarkUserCommandVerifyService` --- node/native/src/service.rs | 6 ++++-- node/src/action_kind.rs | 21 ++++++++++++++++++- node/src/event_source/event_source_effects.rs | 11 ++++++++++ node/src/ledger/ledger_service.rs | 1 - node/src/service.rs | 2 ++ node/src/snark/mod.rs | 1 + node/src/snark/snark_effects.rs | 3 +++ node/src/snark/user_command_verify/mod.rs | 3 +++ .../snark_user_command_verify_actions.rs | 13 ++++++++++++ node/src/transaction_pool/mod.rs | 3 ++- node/testing/src/service/mod.rs | 20 ++++++++++++++++++ snark/src/snark_event.rs | 13 +++++++++--- .../snark_user_command_verify_actions.rs | 4 ++-- .../snark_user_command_verify_effects.rs | 2 +- .../snark_user_command_verify_service.rs | 4 ++-- .../snark_user_command_verify_state.rs | 12 +++++------ 16 files changed, 100 insertions(+), 19 deletions(-) create mode 100644 node/src/snark/user_command_verify/mod.rs create mode 100644 node/src/snark/user_command_verify/snark_user_command_verify_actions.rs diff --git a/node/native/src/service.rs b/node/native/src/service.rs index f07c0750c..f9903075f 100644 --- a/node/native/src/service.rs +++ b/node/native/src/service.rs @@ -9,6 +9,7 @@ use libp2p::identity::Keypair; use mina_p2p_messages::v2::{LedgerProofProdStableV2, TransactionSnarkWorkTStableV2Proofs}; #[cfg(not(feature = "p2p-libp2p"))] use node::p2p::service_impl::mio::MioService; +use node::snark::user_command_verify::{SnarkUserCommandVerifyId, SnarkUserCommandVerifyService}; use node::transaction_pool::VerifyUserCommandsService; use rand::prelude::*; use redux::ActionMeta; @@ -264,9 +265,10 @@ impl SnarkBlockVerifyService for NodeService { } } -impl VerifyUserCommandsService for NodeService { +impl SnarkUserCommandVerifyService for NodeService { fn verify_init( &mut self, + req_id: SnarkUserCommandVerifyId, commands: Vec>, verifier_index: Arc, verifier_srs: Arc>, @@ -287,7 +289,7 @@ impl VerifyUserCommandsService for NodeService { } }) .collect(); - // let _ = tx.send(SnarkEvent::WorkVerify(req_id, result).into()); + let _ = tx.send(SnarkEvent::UserCommandVerify(req_id, verifieds).into()); }); } } diff --git a/node/src/action_kind.rs b/node/src/action_kind.rs index c85895b4c..a1045dc6e 100644 --- a/node/src/action_kind.rs +++ b/node/src/action_kind.rs @@ -45,6 +45,7 @@ use crate::p2p::peer::P2pPeerAction; use crate::p2p::P2pAction; use crate::rpc::RpcAction; use crate::snark::block_verify::SnarkBlockVerifyAction; +use crate::snark::user_command_verify::SnarkUserCommandVerifyAction; use crate::snark::work_verify::SnarkWorkVerifyAction; use crate::snark::SnarkAction; use crate::snark_pool::candidate::SnarkPoolCandidateAction; @@ -320,6 +321,11 @@ pub enum ActionKind { SnarkPoolCandidateWorkVerifyNext, SnarkPoolCandidateWorkVerifyPending, SnarkPoolCandidateWorkVerifySuccess, + SnarkUserCommandVerifyError, + SnarkUserCommandVerifyFinish, + SnarkUserCommandVerifyInit, + SnarkUserCommandVerifyPending, + SnarkUserCommandVerifySuccess, SnarkWorkVerifyError, SnarkWorkVerifyFinish, SnarkWorkVerifyInit, @@ -415,7 +421,7 @@ pub enum ActionKind { } impl ActionKind { - pub const COUNT: u16 = 347; + pub const COUNT: u16 = 352; } impl std::fmt::Display for ActionKind { @@ -479,6 +485,7 @@ impl ActionKindGet for SnarkAction { match self { Self::BlockVerify(a) => a.kind(), Self::WorkVerify(a) => a.kind(), + Self::UserCommandVerify(a) => a.kind(), } } } @@ -775,6 +782,18 @@ impl ActionKindGet for SnarkWorkVerifyAction { } } +impl ActionKindGet for SnarkUserCommandVerifyAction { + fn kind(&self) -> ActionKind { + match self { + Self::Init { .. } => ActionKind::SnarkUserCommandVerifyInit, + Self::Pending { .. } => ActionKind::SnarkUserCommandVerifyPending, + Self::Error { .. } => ActionKind::SnarkUserCommandVerifyError, + Self::Success { .. } => ActionKind::SnarkUserCommandVerifySuccess, + Self::Finish { .. } => ActionKind::SnarkUserCommandVerifyFinish, + } + } +} + impl ActionKindGet for TransitionFrontierGenesisAction { fn kind(&self) -> ActionKind { match self { diff --git a/node/src/event_source/event_source_effects.rs b/node/src/event_source/event_source_effects.rs index 97283d56c..d3b56dfe6 100644 --- a/node/src/event_source/event_source_effects.rs +++ b/node/src/event_source/event_source_effects.rs @@ -1,6 +1,7 @@ use p2p::channels::snark::P2pChannelsSnarkAction; use p2p::listen::P2pListenAction; use p2p::P2pListenEvent; +use snark::user_command_verify::{SnarkUserCommandVerifyAction, SnarkUserCommandVerifyError}; use crate::action::CheckTimeoutsAction; use crate::block_producer::vrf_evaluator::BlockProducerVrfEvaluatorAction; @@ -276,6 +277,16 @@ pub fn event_source_effects(store: &mut Store, action: EventSourc store.dispatch(SnarkWorkVerifyAction::Success { req_id }); } }, + SnarkEvent::UserCommandVerify(req_id, result) => { + if result.iter().any(|res| res.is_err()) { + store.dispatch(SnarkUserCommandVerifyAction::Error { + req_id, + error: SnarkUserCommandVerifyError::VerificationFailed, + }); + } else { + store.dispatch(SnarkUserCommandVerifyAction::Success { req_id }); + } + } }, Event::Rpc(rpc_id, e) => match e { RpcRequest::StateGet(filter) => { diff --git a/node/src/ledger/ledger_service.rs b/node/src/ledger/ledger_service.rs index d88f1836a..47f3818b3 100644 --- a/node/src/ledger/ledger_service.rs +++ b/node/src/ledger/ledger_service.rs @@ -45,7 +45,6 @@ use mina_signer::CompressedPubKey; use openmina_core::block::ArcBlockWithHash; use openmina_node_account::AccountPublicKey; -use crate::block_producer::vrf_evaluator::BlockProducerVrfEvaluatorLedgerService; use crate::block_producer::vrf_evaluator::DelegatorTable; use crate::block_producer::{ BlockProducerLedgerService, BlockProducerWonSlot, StagedLedgerDiffCreateOutput, diff --git a/node/src/service.rs b/node/src/service.rs index e73d5f3fc..06eb7f78e 100644 --- a/node/src/service.rs +++ b/node/src/service.rs @@ -19,6 +19,7 @@ pub use crate::transition_frontier::sync::ledger::snarked::TransitionFrontierSyn pub use crate::transition_frontier::sync::ledger::staged::TransitionFrontierSyncLedgerStagedService; pub use crate::transition_frontier::TransitionFrontierService; pub use redux::TimeService; +use snark::user_command_verify::SnarkUserCommandVerifyService; use crate::stats::Stats; @@ -27,6 +28,7 @@ pub trait Service: + EventSourceService + SnarkBlockVerifyService + SnarkWorkVerifyService + + SnarkUserCommandVerifyService + P2pConnectionService + P2pDisconnectionService + P2pChannelsService diff --git a/node/src/snark/mod.rs b/node/src/snark/mod.rs index 418841c4d..eba3fa8fc 100644 --- a/node/src/snark/mod.rs +++ b/node/src/snark/mod.rs @@ -1,6 +1,7 @@ pub use ::snark::*; pub mod block_verify; +pub mod user_command_verify; pub mod work_verify; mod snark_effects; diff --git a/node/src/snark/snark_effects.rs b/node/src/snark/snark_effects.rs index 75bd5541f..51e6dab73 100644 --- a/node/src/snark/snark_effects.rs +++ b/node/src/snark/snark_effects.rs @@ -59,5 +59,8 @@ pub fn snark_effects(store: &mut Store, action: SnarkActionWithMe } a.effects(&meta, store); } + SnarkAction::UserCommandVerify(a) => { + a.effects(&meta, store); + } } } diff --git a/node/src/snark/user_command_verify/mod.rs b/node/src/snark/user_command_verify/mod.rs new file mode 100644 index 000000000..efead2cea --- /dev/null +++ b/node/src/snark/user_command_verify/mod.rs @@ -0,0 +1,3 @@ +pub use ::snark::user_command_verify::*; + +mod snark_user_command_verify_actions; diff --git a/node/src/snark/user_command_verify/snark_user_command_verify_actions.rs b/node/src/snark/user_command_verify/snark_user_command_verify_actions.rs new file mode 100644 index 000000000..8b83d1565 --- /dev/null +++ b/node/src/snark/user_command_verify/snark_user_command_verify_actions.rs @@ -0,0 +1,13 @@ +use super::*; + +impl redux::EnablingCondition for SnarkUserCommandVerifyAction { + fn is_enabled(&self, state: &crate::State, time: redux::Timestamp) -> bool { + self.is_enabled(&state.snark, time) + } +} + +impl From for crate::Action { + fn from(value: SnarkUserCommandVerifyAction) -> Self { + Self::Snark(value.into()) + } +} diff --git a/node/src/transaction_pool/mod.rs b/node/src/transaction_pool/mod.rs index c5dbd0b45..5f8f371de 100644 --- a/node/src/transaction_pool/mod.rs +++ b/node/src/transaction_pool/mod.rs @@ -9,7 +9,7 @@ use ledger::{ Account, AccountId, BaseLedger, Mask, }; use mina_p2p_messages::v2::LedgerHash; -use snark::{VerifierIndex, VerifierSRS}; +use snark::{user_command_verify::SnarkUserCommandVerifyId, VerifierIndex, VerifierSRS}; use crate::{Service, Store}; @@ -159,6 +159,7 @@ pub trait TransactionPoolLedgerService: redux::Service { pub trait VerifyUserCommandsService: redux::Service { fn verify_init( &mut self, + req_id: SnarkUserCommandVerifyId, commands: Vec>, verifier_index: Arc, verifier_srs: Arc>, diff --git a/node/testing/src/service/mod.rs b/node/testing/src/service/mod.rs index 0a3fd3b7b..af6051440 100644 --- a/node/testing/src/service/mod.rs +++ b/node/testing/src/service/mod.rs @@ -7,6 +7,7 @@ use std::{collections::BTreeMap, ffi::OsStr, sync::Arc}; use ledger::dummy::dummy_transaction_proof; use ledger::proofs::transaction::ProofError; use ledger::scan_state::scan_state::transaction_snark::SokMessage; +use ledger::scan_state::transaction_logic::{verifiable, WithStatus}; use ledger::Mask; use mina_p2p_messages::string::ByteString; use mina_p2p_messages::v2::{ @@ -32,6 +33,7 @@ use node::service::{ use node::snark::block_verify::{ SnarkBlockVerifyId, SnarkBlockVerifyService, VerifiableBlockWithHash, }; +use node::snark::user_command_verify::{SnarkUserCommandVerifyId, SnarkUserCommandVerifyService}; use node::snark::work_verify::{SnarkWorkVerifyId, SnarkWorkVerifyService}; use node::snark::{SnarkEvent, VerifierIndex, VerifierSRS}; use node::snark_pool::{JobState, SnarkPoolService}; @@ -366,6 +368,24 @@ impl SnarkBlockVerifyService for NodeTestingService { } } +impl SnarkUserCommandVerifyService for NodeTestingService { + fn verify_init( + &mut self, + req_id: SnarkUserCommandVerifyId, + commands: Vec>, + verifier_index: Arc, + verifier_srs: Arc>, + ) { + SnarkUserCommandVerifyService::verify_init( + &mut self.real, + req_id, + commands, + verifier_index, + verifier_srs, + ) + } +} + impl SnarkWorkVerifyService for NodeTestingService { fn verify_init( &mut self, diff --git a/snark/src/snark_event.rs b/snark/src/snark_event.rs index ff6e7bc4a..3e0e0c9fa 100644 --- a/snark/src/snark_event.rs +++ b/snark/src/snark_event.rs @@ -1,8 +1,9 @@ +use ledger::scan_state::transaction_logic::valid; use serde::{Deserialize, Serialize}; use super::block_verify::{SnarkBlockVerifyError, SnarkBlockVerifyId}; use super::work_verify::{SnarkWorkVerifyError, SnarkWorkVerifyId}; -use crate::user_command_verify::{SnarkUserCommandVerifyError, SnarkUserCommandVerifyId}; +use crate::user_command_verify::SnarkUserCommandVerifyId; #[derive(Serialize, Deserialize, Debug, Clone)] pub enum SnarkEvent { @@ -10,7 +11,7 @@ pub enum SnarkEvent { WorkVerify(SnarkWorkVerifyId, Result<(), SnarkWorkVerifyError>), UserCommandVerify( SnarkUserCommandVerifyId, - Result<(), SnarkUserCommandVerifyError>, + Vec>, ), } @@ -32,7 +33,13 @@ impl std::fmt::Display for SnarkEvent { write!(f, "WorkVerify, {id}, {}", res_kind(res)) } Self::UserCommandVerify(id, res) => { - write!(f, "UserCommandVerify, {id}, {}", res_kind(res)) + let n_failed = res.iter().filter(|res| res.is_err()).count(); + let n_success = res.len() - n_failed; + write!( + f, + "UserCommandVerify, {id}, n_success={} n_failed={}", + n_success, n_failed + ) } } } diff --git a/snark/src/user_command_verify/snark_user_command_verify_actions.rs b/snark/src/user_command_verify/snark_user_command_verify_actions.rs index d743ea3bc..f8af50a25 100644 --- a/snark/src/user_command_verify/snark_user_command_verify_actions.rs +++ b/snark/src/user_command_verify/snark_user_command_verify_actions.rs @@ -1,4 +1,4 @@ -use ledger::scan_state::transaction_logic::verifiable; +use ledger::scan_state::transaction_logic::{verifiable, WithStatus}; use serde::{Deserialize, Serialize}; use openmina_core::{snark::Snark, ActionEvent}; @@ -16,7 +16,7 @@ pub enum SnarkUserCommandVerifyAction { Init { req_id: SnarkUserCommandVerifyId, #[serde(skip)] // TODO - commands: Vec, + commands: Vec>, sender: String, }, Pending { diff --git a/snark/src/user_command_verify/snark_user_command_verify_effects.rs b/snark/src/user_command_verify/snark_user_command_verify_effects.rs index 2341f52e0..ff5f0fcb8 100644 --- a/snark/src/user_command_verify/snark_user_command_verify_effects.rs +++ b/snark/src/user_command_verify/snark_user_command_verify_effects.rs @@ -17,7 +17,7 @@ impl SnarkUserCommandVerifyAction { let verifier_srs = store.state().work_verify.verifier_srs.clone(); store .service() - .verify_init(req_id, verifier_index, verifier_srs, commands); + .verify_init(req_id, commands, verifier_index, verifier_srs); store.dispatch(SnarkUserCommandVerifyAction::Pending { req_id }); } SnarkUserCommandVerifyAction::Error { req_id, .. } => { diff --git a/snark/src/user_command_verify/snark_user_command_verify_service.rs b/snark/src/user_command_verify/snark_user_command_verify_service.rs index 95c8ea859..5fd7fc0c9 100644 --- a/snark/src/user_command_verify/snark_user_command_verify_service.rs +++ b/snark/src/user_command_verify/snark_user_command_verify_service.rs @@ -1,6 +1,6 @@ use std::sync::{Arc, Mutex}; -use ledger::scan_state::transaction_logic::verifiable; +use ledger::scan_state::transaction_logic::{verifiable, WithStatus}; use crate::{VerifierIndex, VerifierSRS}; @@ -10,8 +10,8 @@ pub trait SnarkUserCommandVerifyService: redux::Service { fn verify_init( &mut self, req_id: SnarkUserCommandVerifyId, + commands: Vec>, verifier_index: Arc, verifier_srs: Arc>, - commands: Vec, ); } diff --git a/snark/src/user_command_verify/snark_user_command_verify_state.rs b/snark/src/user_command_verify/snark_user_command_verify_state.rs index 1c0ee3b54..590d3297e 100644 --- a/snark/src/user_command_verify/snark_user_command_verify_state.rs +++ b/snark/src/user_command_verify/snark_user_command_verify_state.rs @@ -1,6 +1,6 @@ use std::sync::{Arc, Mutex}; -use ledger::scan_state::transaction_logic::verifiable; +use ledger::scan_state::transaction_logic::{verifiable, WithStatus}; use serde::{Deserialize, Serialize}; use openmina_core::requests::PendingRequests; @@ -46,7 +46,7 @@ pub enum SnarkUserCommandVerifyStatus { Init { time: redux::Timestamp, #[serde(skip)] // TODO - commands: Vec, + commands: Vec>, // TODO(binier): move p2p/src/identity to shared crate and use // `PeerId` here. sender: String, @@ -54,20 +54,20 @@ pub enum SnarkUserCommandVerifyStatus { Pending { time: redux::Timestamp, #[serde(skip)] // TODO - commands: Vec, + commands: Vec>, sender: String, }, Error { time: redux::Timestamp, #[serde(skip)] // TODO - commands: Vec, + commands: Vec>, sender: String, error: SnarkUserCommandVerifyError, }, Success { time: redux::Timestamp, #[serde(skip)] // TODO - commands: Vec, + commands: Vec>, sender: String, }, } @@ -85,7 +85,7 @@ impl SnarkUserCommandVerifyStatus { matches!(self, Self::Error { .. } | Self::Success { .. }) } - pub fn commands(&self) -> &[verifiable::UserCommand] { + pub fn commands(&self) -> &[WithStatus] { match self { Self::Init { commands, .. } => commands, Self::Pending { commands, .. } => commands,