Skip to content

Commit

Permalink
Fix usage of SnarkUserCommandVerifyService
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiencs committed Apr 11, 2024
1 parent b31fced commit 168b71a
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 19 deletions.
6 changes: 4 additions & 2 deletions node/native/src/service.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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<WithStatus<verifiable::UserCommand>>,
verifier_index: Arc<VerifierIndex>,
verifier_srs: Arc<Mutex<VerifierSRS>>,
Expand All @@ -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());
});
}
}
Expand Down
21 changes: 20 additions & 1 deletion node/src/action_kind.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -320,6 +321,11 @@ pub enum ActionKind {
SnarkPoolCandidateWorkVerifyNext,
SnarkPoolCandidateWorkVerifyPending,
SnarkPoolCandidateWorkVerifySuccess,
SnarkUserCommandVerifyError,
SnarkUserCommandVerifyFinish,
SnarkUserCommandVerifyInit,
SnarkUserCommandVerifyPending,
SnarkUserCommandVerifySuccess,
SnarkWorkVerifyError,
SnarkWorkVerifyFinish,
SnarkWorkVerifyInit,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
}
}
}
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions 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;
Expand Down Expand Up @@ -276,6 +277,16 @@ pub fn event_source_effects<S: Service>(store: &mut Store<S>, 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) => {
Expand Down
1 change: 0 additions & 1 deletion node/src/ledger/ledger_service.rs
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions node/src/service.rs
Expand Up @@ -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;

Expand All @@ -27,6 +28,7 @@ pub trait Service:
+ EventSourceService
+ SnarkBlockVerifyService
+ SnarkWorkVerifyService
+ SnarkUserCommandVerifyService
+ P2pConnectionService
+ P2pDisconnectionService
+ P2pChannelsService
Expand Down
1 change: 1 addition & 0 deletions 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;
Expand Down
3 changes: 3 additions & 0 deletions node/src/snark/snark_effects.rs
Expand Up @@ -59,5 +59,8 @@ pub fn snark_effects<S: Service>(store: &mut Store<S>, action: SnarkActionWithMe
}
a.effects(&meta, store);
}
SnarkAction::UserCommandVerify(a) => {
a.effects(&meta, store);
}
}
}
3 changes: 3 additions & 0 deletions node/src/snark/user_command_verify/mod.rs
@@ -0,0 +1,3 @@
pub use ::snark::user_command_verify::*;

mod snark_user_command_verify_actions;
@@ -0,0 +1,13 @@
use super::*;

impl redux::EnablingCondition<crate::State> for SnarkUserCommandVerifyAction {
fn is_enabled(&self, state: &crate::State, time: redux::Timestamp) -> bool {
self.is_enabled(&state.snark, time)
}
}

impl From<SnarkUserCommandVerifyAction> for crate::Action {
fn from(value: SnarkUserCommandVerifyAction) -> Self {
Self::Snark(value.into())
}
}
3 changes: 2 additions & 1 deletion node/src/transaction_pool/mod.rs
Expand Up @@ -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};

Expand Down Expand Up @@ -159,6 +159,7 @@ pub trait TransactionPoolLedgerService: redux::Service {
pub trait VerifyUserCommandsService: redux::Service {
fn verify_init(
&mut self,
req_id: SnarkUserCommandVerifyId,
commands: Vec<WithStatus<verifiable::UserCommand>>,
verifier_index: Arc<VerifierIndex>,
verifier_srs: Arc<Mutex<VerifierSRS>>,
Expand Down
20 changes: 20 additions & 0 deletions node/testing/src/service/mod.rs
Expand Up @@ -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::{
Expand All @@ -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};
Expand Down Expand Up @@ -366,6 +368,24 @@ impl SnarkBlockVerifyService for NodeTestingService {
}
}

impl SnarkUserCommandVerifyService for NodeTestingService {
fn verify_init(
&mut self,
req_id: SnarkUserCommandVerifyId,
commands: Vec<WithStatus<verifiable::UserCommand>>,
verifier_index: Arc<VerifierIndex>,
verifier_srs: Arc<Mutex<VerifierSRS>>,
) {
SnarkUserCommandVerifyService::verify_init(
&mut self.real,
req_id,
commands,
verifier_index,
verifier_srs,
)
}
}

impl SnarkWorkVerifyService for NodeTestingService {
fn verify_init(
&mut self,
Expand Down
13 changes: 10 additions & 3 deletions snark/src/snark_event.rs
@@ -1,16 +1,17 @@
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 {
BlockVerify(SnarkBlockVerifyId, Result<(), SnarkBlockVerifyError>),
WorkVerify(SnarkWorkVerifyId, Result<(), SnarkWorkVerifyError>),
UserCommandVerify(
SnarkUserCommandVerifyId,
Result<(), SnarkUserCommandVerifyError>,
Vec<Result<valid::UserCommand, String>>,
),
}

Expand All @@ -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
)
}
}
}
Expand Down
@@ -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};
Expand All @@ -16,7 +16,7 @@ pub enum SnarkUserCommandVerifyAction {
Init {
req_id: SnarkUserCommandVerifyId,
#[serde(skip)] // TODO
commands: Vec<verifiable::UserCommand>,
commands: Vec<WithStatus<verifiable::UserCommand>>,
sender: String,
},
Pending {
Expand Down
Expand Up @@ -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, .. } => {
Expand Down
@@ -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};

Expand All @@ -10,8 +10,8 @@ pub trait SnarkUserCommandVerifyService: redux::Service {
fn verify_init(
&mut self,
req_id: SnarkUserCommandVerifyId,
commands: Vec<WithStatus<verifiable::UserCommand>>,
verifier_index: Arc<VerifierIndex>,
verifier_srs: Arc<Mutex<VerifierSRS>>,
commands: Vec<verifiable::UserCommand>,
);
}
12 changes: 6 additions & 6 deletions 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;
Expand Down Expand Up @@ -46,28 +46,28 @@ pub enum SnarkUserCommandVerifyStatus {
Init {
time: redux::Timestamp,
#[serde(skip)] // TODO
commands: Vec<verifiable::UserCommand>,
commands: Vec<WithStatus<verifiable::UserCommand>>,
// TODO(binier): move p2p/src/identity to shared crate and use
// `PeerId` here.
sender: String,
},
Pending {
time: redux::Timestamp,
#[serde(skip)] // TODO
commands: Vec<verifiable::UserCommand>,
commands: Vec<WithStatus<verifiable::UserCommand>>,
sender: String,
},
Error {
time: redux::Timestamp,
#[serde(skip)] // TODO
commands: Vec<verifiable::UserCommand>,
commands: Vec<WithStatus<verifiable::UserCommand>>,
sender: String,
error: SnarkUserCommandVerifyError,
},
Success {
time: redux::Timestamp,
#[serde(skip)] // TODO
commands: Vec<verifiable::UserCommand>,
commands: Vec<WithStatus<verifiable::UserCommand>>,
sender: String,
},
}
Expand All @@ -85,7 +85,7 @@ impl SnarkUserCommandVerifyStatus {
matches!(self, Self::Error { .. } | Self::Success { .. })
}

pub fn commands(&self) -> &[verifiable::UserCommand] {
pub fn commands(&self) -> &[WithStatus<verifiable::UserCommand>] {
match self {
Self::Init { commands, .. } => commands,
Self::Pending { commands, .. } => commands,
Expand Down

0 comments on commit 168b71a

Please sign in to comment.