Skip to content

Commit b043f82

Browse files
committed
feat(testing): integrate dummy snark worker
closes: #206
1 parent 9c7a72f commit b043f82

File tree

11 files changed

+228
-35
lines changed

11 files changed

+228
-35
lines changed

ledger/src/scan_state/conv.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,11 +616,17 @@ impl From<&Statement<SokDigest>> for MinaStateSnarkedLedgerStateWithSokStableV2
616616
.into(),
617617
supply_increase: (&value.supply_increase).into(),
618618
fee_excess: (&value.fee_excess).into(),
619-
sok_digest: MinaBaseZkappAccountZkappUriStableV1(value.sok_digest.as_slice().into()),
619+
sok_digest: (&value.sok_digest).into(),
620620
}
621621
}
622622
}
623623

624+
impl From<&SokDigest> for MinaBaseZkappAccountZkappUriStableV1 {
625+
fn from(value: &SokDigest) -> Self {
626+
Self(value.as_slice().into())
627+
}
628+
}
629+
624630
impl From<&MinaBaseTransactionStatusStableV2> for TransactionStatus {
625631
fn from(value: &MinaBaseTransactionStatusStableV2) -> Self {
626632
match value {

node/src/account/public_key.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ impl From<CompressedPubKey> for AccountPublicKey {
3535
}
3636
}
3737

38+
impl From<AccountPublicKey> for CompressedPubKey {
39+
fn from(value: AccountPublicKey) -> Self {
40+
Self {
41+
is_odd: value.0.is_odd,
42+
x: value.0.into_inner().x.into(),
43+
}
44+
}
45+
}
46+
3847
impl From<NonZeroCurvePoint> for AccountPublicKey {
3948
fn from(value: NonZeroCurvePoint) -> Self {
4049
Self(value)

node/testing/src/cluster/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl Cluster {
191191
},
192192
global: GlobalConfig {
193193
build: BuildEnv::get().into(),
194-
snarker: None,
194+
snarker: testing_config.snark_worker,
195195
},
196196
p2p: P2pConfig {
197197
libp2p_port: Some(libp2p_port),

node/testing/src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,15 @@ impl Command {
8787
.into_iter()
8888
.find(|s| <&'static str>::from(s) == name)
8989
{
90-
scenario.run_and_save_from_scratch(config).await;
90+
scenario.run_only_from_scratch(config).await;
91+
// scenario.run_and_save_from_scratch(config).await;
9192
} else {
9293
anyhow::bail!("no such scenario: \"{name}\"");
9394
}
9495
} else {
9596
for scenario in Scenarios::iter() {
96-
scenario.run_and_save_from_scratch(config.clone()).await;
97+
scenario.run_only_from_scratch(config.clone()).await;
98+
// scenario.run_and_save_from_scratch(config.clone()).await;
9799
}
98100
}
99101
Ok(())

node/testing/src/node/rust/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::time::Duration;
22

3-
use node::{account::AccountSecretKey, BlockProducerConfig};
3+
use node::{account::AccountSecretKey, BlockProducerConfig, SnarkerConfig};
44
use serde::{Deserialize, Serialize};
55

66
use crate::scenario::ListenerNode;
@@ -24,6 +24,7 @@ pub struct RustNodeTestingConfig {
2424
pub ask_initial_peers_interval: Duration,
2525
pub initial_peers: Vec<ListenerNode>,
2626
pub peer_id: TestPeerId,
27+
pub snark_worker: Option<SnarkerConfig>,
2728
pub block_producer: Option<RustNodeBlockProducerTestingConfig>,
2829
}
2930

@@ -43,6 +44,7 @@ impl RustNodeTestingConfig {
4344
initial_peers: Vec::new(),
4445
peer_id: TestPeerId::default(),
4546
block_producer: None,
47+
snark_worker: None,
4648
}
4749
}
4850

node/testing/src/scenarios/cluster_runner.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::{
33
time::Duration,
44
};
55

6+
use ledger::BaseLedger;
67
use node::{
78
account::{AccountPublicKey, AccountSecretKey},
89
event_source::Event,
@@ -338,7 +339,7 @@ impl<'a> ClusterRunner<'a> {
338339
self.cluster.debugger()
339340
}
340341

341-
/// Block producer accounts, ordered by total stake, smallest first.
342+
/// Block producer accounts, ordered by total stake, largest first.
342343
///
343344
/// Warning: caller must ensure we are using custom daemon json if
344345
/// this method is called, so that we have secret keys for
@@ -373,9 +374,36 @@ impl<'a> ClusterRunner<'a> {
373374
.collect::<Vec<_>>();
374375

375376
// order by stake
376-
block_producers.sort_by(|(_, s1), (_, s2)| s1.cmp(s2));
377+
block_producers.sort_by(|(_, s1), (_, s2)| s2.cmp(s1));
377378
block_producers
378379
}
380+
381+
pub fn accounts_with_sec_keys<'b>(
382+
&'b self,
383+
node_id: ClusterNodeId,
384+
) -> Box<dyn 'b + Iterator<Item = (AccountSecretKey, Box<ledger::Account>)>> {
385+
let Some(mask) = self.node(node_id).and_then(|node| {
386+
let best_tip = node.state().transition_frontier.best_tip()?;
387+
let ledger_hash = best_tip.staged_ledger_hash();
388+
let (mask, _) = LedgerService::ctx(node.service()).mask(ledger_hash)?;
389+
Some(mask)
390+
}) else {
391+
return Box::new(std::iter::empty());
392+
};
393+
394+
let depth = mask.depth() as usize;
395+
let num_accounts = mask.num_accounts() as u64;
396+
Box::new(
397+
(0..num_accounts)
398+
.map(ledger::AccountIndex)
399+
.filter_map(move |index| mask.get(ledger::Address::from_index(index, depth)))
400+
.filter_map(|account| {
401+
let pub_key = account.public_key.clone().into();
402+
let sec_key = self.get_account_sec_key(&pub_key)?;
403+
Some((sec_key.clone(), account))
404+
}),
405+
)
406+
}
379407
}
380408

381409
impl RunDecision {

node/testing/src/scenarios/simulation/small.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ impl SimulationSmall {
2323
daemon_json: DaemonJsonGenConfig::Counts { whales: 2, fish: 4 },
2424
seed_nodes: 2,
2525
normal_nodes: 2,
26-
run_until: SimulatorRunUntil::Epoch(8),
26+
snark_workers: 1,
27+
block_producers: 6,
28+
run_until: SimulatorRunUntil::Epoch(3),
2729
run_until_timeout: Duration::from_secs(30 * 60),
2830
});
2931
simulator.run(runner).await;

node/testing/src/scenarios/solo_node/sync_to_genesis.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ impl SoloNodeSyncToGenesis {
5050
ask_initial_peers_interval: Duration::from_secs(60 * 60),
5151
initial_peers: Vec::new(),
5252
peer_id: Default::default(),
53+
snark_worker: None,
5354
block_producer: None,
5455
});
5556

node/testing/src/service/mod.rs

Lines changed: 98 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,28 @@ mod rpc_service;
33
use std::time::Duration;
44
use std::{collections::BTreeMap, ffi::OsStr, sync::Arc};
55

6+
use ledger::dummy::dummy_transaction_proof;
7+
use ledger::scan_state::scan_state::transaction_snark::SokMessage;
68
use ledger::Mask;
7-
use mina_p2p_messages::v2::{CurrencyFeeStableV1, LedgerHash, NonZeroCurvePoint};
9+
use mina_p2p_messages::v2::{
10+
CurrencyFeeStableV1, LedgerHash, LedgerProofProdStableV2, MinaBaseZkappAccountZkappUriStableV1,
11+
MinaStateSnarkedLedgerStateWithSokStableV2, NonZeroCurvePoint,
12+
SnarkWorkerWorkerRpcsVersionedGetWorkV2TResponseA0Single, TransactionSnarkStableV2,
13+
TransactionSnarkWorkTStableV2Proofs,
14+
};
15+
use node::account::AccountPublicKey;
816
use node::block_producer::vrf_evaluator::VrfEvaluatorInput;
917
use node::core::channels::mpsc;
1018
use node::core::requests::{PendingRequests, RequestId};
1119
use node::core::snark::{Snark, SnarkJobId};
20+
use node::external_snark_worker::ExternalSnarkWorkerEvent;
1221
use node::recorder::Recorder;
1322
use node::service::BlockProducerVrfEvaluatorService;
1423
use node::snark::block_verify::{
1524
SnarkBlockVerifyId, SnarkBlockVerifyService, VerifiableBlockWithHash,
1625
};
1726
use node::snark::work_verify::{SnarkWorkVerifyId, SnarkWorkVerifyService};
18-
use node::snark::{VerifierIndex, VerifierSRS};
27+
use node::snark::{SnarkEvent, VerifierIndex, VerifierSRS};
1928
use node::snark_pool::{JobState, SnarkPoolService};
2029
use node::stats::Stats;
2130
use node::{
@@ -61,6 +70,8 @@ pub struct NodeTestingService {
6170
/// Events sent by the real service not yet received by state machine.
6271
pending_events: PendingRequests<PendingEventIdType, Event>,
6372
dyn_effects: Option<DynEffects>,
73+
74+
snarker_sok_digest: Option<MinaBaseZkappAccountZkappUriStableV1>,
6475
/// Once dropped, it will cause all threads associated to shutdown.
6576
_shutdown: mpsc::Receiver<()>,
6677
}
@@ -75,6 +86,7 @@ impl NodeTestingService {
7586
monotonic_time: Instant::now(),
7687
pending_events: PendingRequests::new(),
7788
dyn_effects: None,
89+
snarker_sok_digest: None,
7890
_shutdown,
7991
}
8092
}
@@ -117,6 +129,10 @@ impl NodeTestingService {
117129
self.dyn_effects.take()
118130
}
119131

132+
pub fn set_snarker_sok_digest(&mut self, digest: MinaBaseZkappAccountZkappUriStableV1) {
133+
self.snarker_sok_digest = Some(digest);
134+
}
135+
120136
pub fn pending_events(&mut self) -> impl Iterator<Item = (PendingEventId, &Event)> {
121137
while let Ok(req) = self.real.rpc.req_receiver().try_recv() {
122138
self.real.process_rpc_request(req);
@@ -278,13 +294,18 @@ impl SnarkBlockVerifyService for NodeTestingService {
278294
verifier_srs: Arc<VerifierSRS>,
279295
block: VerifiableBlockWithHash,
280296
) {
281-
SnarkBlockVerifyService::verify_init(
282-
&mut self.real,
283-
req_id,
284-
verifier_index,
285-
verifier_srs,
286-
block,
287-
)
297+
let _ = (verifier_index, verifier_srs, block);
298+
let _ = self
299+
.real
300+
.event_sender
301+
.send(SnarkEvent::BlockVerify(req_id, Ok(())).into());
302+
// SnarkBlockVerifyService::verify_init(
303+
// &mut self.real,
304+
// req_id,
305+
// verifier_index,
306+
// verifier_srs,
307+
// block,
308+
// )
288309
}
289310
}
290311

@@ -296,13 +317,18 @@ impl SnarkWorkVerifyService for NodeTestingService {
296317
verifier_srs: Arc<VerifierSRS>,
297318
work: Vec<Snark>,
298319
) {
299-
SnarkWorkVerifyService::verify_init(
300-
&mut self.real,
301-
req_id,
302-
verifier_index,
303-
verifier_srs,
304-
work,
305-
)
320+
let _ = (verifier_index, verifier_srs, work);
321+
let _ = self
322+
.real
323+
.event_sender
324+
.send(SnarkEvent::WorkVerify(req_id, Ok(())).into());
325+
// SnarkWorkVerifyService::verify_init(
326+
// &mut self.real,
327+
// req_id,
328+
// verifier_index,
329+
// verifier_srs,
330+
// work,
331+
// )
306332
}
307333
}
308334

@@ -329,22 +355,74 @@ impl ExternalSnarkWorkerService for NodeTestingService {
329355
public_key: NonZeroCurvePoint,
330356
fee: CurrencyFeeStableV1,
331357
) -> Result<(), node::external_snark_worker::ExternalSnarkWorkerError> {
332-
self.real.start(path, public_key, fee)
358+
let _ = path;
359+
360+
let pub_key = AccountPublicKey::from(public_key);
361+
let sok_message = SokMessage::create((&fee).into(), pub_key.into());
362+
self.set_snarker_sok_digest((&sok_message.digest()).into());
363+
let _ = self
364+
.real
365+
.event_sender
366+
.send(ExternalSnarkWorkerEvent::Started.into());
367+
Ok(())
368+
// self.real.start(path, public_key, fee)
333369
}
334370

335371
fn submit(
336372
&mut self,
337373
spec: SnarkWorkSpec,
338374
) -> Result<(), node::external_snark_worker::ExternalSnarkWorkerError> {
339-
self.real.submit(spec)
375+
let sok_digest = self.snarker_sok_digest.clone().unwrap();
376+
let make_dummy_proof = |spec| {
377+
let statement = match spec {
378+
SnarkWorkerWorkerRpcsVersionedGetWorkV2TResponseA0Single::Transition(v, _) => v.0,
379+
SnarkWorkerWorkerRpcsVersionedGetWorkV2TResponseA0Single::Merge(v) => v.0 .0,
380+
};
381+
382+
LedgerProofProdStableV2(TransactionSnarkStableV2 {
383+
statement: MinaStateSnarkedLedgerStateWithSokStableV2 {
384+
source: statement.source,
385+
target: statement.target,
386+
connecting_ledger_left: statement.connecting_ledger_left,
387+
connecting_ledger_right: statement.connecting_ledger_right,
388+
supply_increase: statement.supply_increase,
389+
fee_excess: statement.fee_excess,
390+
sok_digest: sok_digest.clone(),
391+
},
392+
proof: (*dummy_transaction_proof()).clone(),
393+
})
394+
};
395+
let res = match spec {
396+
SnarkWorkSpec::One(v) => TransactionSnarkWorkTStableV2Proofs::One(make_dummy_proof(v)),
397+
SnarkWorkSpec::Two((v1, v2)) => TransactionSnarkWorkTStableV2Proofs::Two((
398+
make_dummy_proof(v1),
399+
make_dummy_proof(v2),
400+
)),
401+
};
402+
let _ = self
403+
.real
404+
.event_sender
405+
.send(ExternalSnarkWorkerEvent::WorkResult(Arc::new(res)).into());
406+
Ok(())
407+
// self.real.submit(spec)
340408
}
341409

342410
fn cancel(&mut self) -> Result<(), node::external_snark_worker::ExternalSnarkWorkerError> {
343-
self.real.cancel()
411+
let _ = self
412+
.real
413+
.event_sender
414+
.send(ExternalSnarkWorkerEvent::WorkCancelled.into());
415+
Ok(())
416+
// self.real.cancel()
344417
}
345418

346419
fn kill(&mut self) -> Result<(), node::external_snark_worker::ExternalSnarkWorkerError> {
347-
self.real.kill()
420+
let _ = self
421+
.real
422+
.event_sender
423+
.send(ExternalSnarkWorkerEvent::Killed.into());
424+
Ok(())
425+
// self.real.kill()
348426
}
349427
}
350428

node/testing/src/simulator/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ pub struct SimulatorConfig {
99
pub daemon_json: DaemonJsonGenConfig,
1010
pub seed_nodes: usize,
1111
pub normal_nodes: usize,
12+
pub snark_workers: usize,
13+
pub block_producers: usize,
1214
pub run_until: SimulatorRunUntil,
1315
pub run_until_timeout: Duration,
1416
}

0 commit comments

Comments
 (0)