Skip to content

Commit 414f416

Browse files
committed
refactor(snark_block_verify): Combine all action types of the snark_block_verify module into the parent action type
1 parent 67b21e8 commit 414f416

File tree

7 files changed

+91
-198
lines changed

7 files changed

+91
-198
lines changed

node/src/consensus/consensus_effects.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::transition_frontier::sync::{
44
use crate::watched_accounts::WatchedAccountsLedgerInitialStateGetInitAction;
55
use crate::Store;
66
use crate::{
7-
snark::block_verify::SnarkBlockVerifyInitAction,
7+
snark::block_verify::SnarkBlockVerifyAction,
88
watched_accounts::WatchedAccountsBlockTransactionsIncludedAction,
99
};
1010

@@ -16,7 +16,7 @@ pub fn consensus_effects<S: crate::Service>(store: &mut Store<S>, action: Consen
1616
match action {
1717
ConsensusAction::BlockReceived { hash, block, .. } => {
1818
let req_id = store.state().snark.block_verify.next_req_id();
19-
store.dispatch(SnarkBlockVerifyInitAction {
19+
store.dispatch(SnarkBlockVerifyAction::Init {
2020
req_id,
2121
block: (hash.clone(), block).into(),
2222
});

node/src/event_source/event_source_effects.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use crate::rpc::{
4545
RpcSnarkerJobCommitAction, RpcSnarkerJobSpecAction, RpcSnarkersWorkersGetAction,
4646
RpcSyncStatsGetAction,
4747
};
48-
use crate::snark::block_verify::{SnarkBlockVerifyErrorAction, SnarkBlockVerifySuccessAction};
48+
use crate::snark::block_verify::SnarkBlockVerifyAction;
4949
use crate::snark::work_verify::{SnarkWorkVerifyErrorAction, SnarkWorkVerifySuccessAction};
5050
use crate::snark::SnarkEvent;
5151
use crate::{Service, Store};
@@ -237,10 +237,10 @@ pub fn event_source_effects<S: Service>(store: &mut Store<S>, action: EventSourc
237237
Event::Snark(event) => match event {
238238
SnarkEvent::BlockVerify(req_id, result) => match result {
239239
Err(error) => {
240-
store.dispatch(SnarkBlockVerifyErrorAction { req_id, error });
240+
store.dispatch(SnarkBlockVerifyAction::Error { req_id, error });
241241
}
242242
Ok(()) => {
243-
store.dispatch(SnarkBlockVerifySuccessAction { req_id });
243+
store.dispatch(SnarkBlockVerifyAction::Success { req_id });
244244
}
245245
},
246246
SnarkEvent::WorkVerify(req_id, result) => match result {
Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,13 @@
11
use super::*;
22

3-
impl redux::EnablingCondition<crate::State> for SnarkBlockVerifyInitAction {
3+
impl redux::EnablingCondition<crate::State> for SnarkBlockVerifyAction {
44
fn is_enabled(&self, state: &crate::State) -> bool {
55
self.is_enabled(&state.snark)
66
}
77
}
88

9-
impl redux::EnablingCondition<crate::State> for SnarkBlockVerifyPendingAction {
10-
fn is_enabled(&self, state: &crate::State) -> bool {
11-
self.is_enabled(&state.snark)
12-
}
13-
}
14-
15-
impl redux::EnablingCondition<crate::State> for SnarkBlockVerifyErrorAction {
16-
fn is_enabled(&self, state: &crate::State) -> bool {
17-
self.is_enabled(&state.snark)
18-
}
19-
}
20-
21-
impl redux::EnablingCondition<crate::State> for SnarkBlockVerifySuccessAction {
22-
fn is_enabled(&self, state: &crate::State) -> bool {
23-
self.is_enabled(&state.snark)
24-
}
25-
}
26-
27-
impl redux::EnablingCondition<crate::State> for SnarkBlockVerifyFinishAction {
28-
fn is_enabled(&self, state: &crate::State) -> bool {
29-
self.is_enabled(&state.snark)
30-
}
31-
}
32-
33-
impl From<SnarkBlockVerifyInitAction> for crate::Action {
34-
fn from(a: SnarkBlockVerifyInitAction) -> Self {
35-
Self::Snark(a.into())
36-
}
37-
}
38-
39-
impl From<SnarkBlockVerifyErrorAction> for crate::Action {
40-
fn from(a: SnarkBlockVerifyErrorAction) -> Self {
41-
Self::Snark(a.into())
42-
}
43-
}
44-
45-
impl From<SnarkBlockVerifySuccessAction> for crate::Action {
46-
fn from(a: SnarkBlockVerifySuccessAction) -> Self {
47-
Self::Snark(a.into())
9+
impl From<SnarkBlockVerifyAction> for crate::Action {
10+
fn from(value: SnarkBlockVerifyAction) -> Self {
11+
Self::Snark(value.into())
4812
}
4913
}

node/src/snark/snark_effects.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,16 @@ pub fn snark_effects<S: Service>(store: &mut Store<S>, action: SnarkActionWithMe
1111
let (action, meta) = action.split();
1212

1313
match action {
14-
SnarkAction::BlockVerify(a) => match a {
15-
SnarkBlockVerifyAction::Init(a) => {
16-
a.effects(&meta, store);
17-
}
18-
SnarkBlockVerifyAction::Pending(_) => {}
19-
SnarkBlockVerifyAction::Error(a) => {
20-
a.effects(&meta, store);
21-
}
22-
SnarkBlockVerifyAction::Success(a) => {
23-
let req = store.state().snark.block_verify.jobs.get(a.req_id);
14+
SnarkAction::BlockVerify(a) => {
15+
if let SnarkBlockVerifyAction::Success { req_id } = a {
16+
let req = store.state().snark.block_verify.jobs.get(req_id);
2417
let Some(req) = req else { return };
2518
store.dispatch(ConsensusAction::BlockSnarkVerifySuccess {
2619
hash: req.block().hash_ref().clone(),
2720
});
28-
a.effects(&meta, store);
2921
}
30-
SnarkBlockVerifyAction::Finish(_) => {}
31-
},
22+
a.effects(&meta, store);
23+
}
3224
SnarkAction::WorkVerify(a) => match a {
3325
SnarkWorkVerifyAction::Init(a) => {
3426
a.effects(&meta, store);

snark/src/block_verify/snark_block_verify_actions.rs

Lines changed: 45 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -5,100 +5,53 @@ use super::{SnarkBlockVerifyError, SnarkBlockVerifyId, VerifiableBlockWithHash};
55
pub type SnarkBlockVerifyActionWithMeta = redux::ActionWithMeta<SnarkBlockVerifyAction>;
66
pub type SnarkBlockVerifyActionWithMetaRef<'a> = redux::ActionWithMeta<&'a SnarkBlockVerifyAction>;
77

8-
#[derive(derive_more::From, Serialize, Deserialize, Debug, Clone)]
9-
pub enum SnarkBlockVerifyAction {
10-
Init(SnarkBlockVerifyInitAction),
11-
Pending(SnarkBlockVerifyPendingAction),
12-
Error(SnarkBlockVerifyErrorAction),
13-
Success(SnarkBlockVerifySuccessAction),
14-
Finish(SnarkBlockVerifyFinishAction),
15-
}
16-
17-
#[derive(Serialize, Deserialize, Debug, Clone)]
18-
pub struct SnarkBlockVerifyInitAction {
19-
pub req_id: SnarkBlockVerifyId,
20-
pub block: VerifiableBlockWithHash,
21-
}
22-
23-
impl redux::EnablingCondition<crate::SnarkState> for SnarkBlockVerifyInitAction {
24-
fn is_enabled(&self, state: &crate::SnarkState) -> bool {
25-
state.block_verify.jobs.next_req_id() == self.req_id
26-
}
27-
}
28-
298
#[derive(Serialize, Deserialize, Debug, Clone)]
30-
pub struct SnarkBlockVerifyPendingAction {
31-
pub req_id: SnarkBlockVerifyId,
32-
}
33-
34-
impl redux::EnablingCondition<crate::SnarkState> for SnarkBlockVerifyPendingAction {
35-
fn is_enabled(&self, state: &crate::SnarkState) -> bool {
36-
state
37-
.block_verify
38-
.jobs
39-
.get(self.req_id)
40-
.map_or(false, |v| v.is_init())
41-
}
42-
}
43-
44-
#[derive(Serialize, Deserialize, Debug, Clone)]
45-
pub struct SnarkBlockVerifyErrorAction {
46-
pub req_id: SnarkBlockVerifyId,
47-
pub error: SnarkBlockVerifyError,
48-
}
49-
50-
impl redux::EnablingCondition<crate::SnarkState> for SnarkBlockVerifyErrorAction {
51-
fn is_enabled(&self, state: &crate::SnarkState) -> bool {
52-
state
53-
.block_verify
54-
.jobs
55-
.get(self.req_id)
56-
.map_or(false, |v| v.is_pending())
57-
}
58-
}
59-
60-
#[derive(Serialize, Deserialize, Debug, Clone)]
61-
pub struct SnarkBlockVerifySuccessAction {
62-
pub req_id: SnarkBlockVerifyId,
63-
}
64-
65-
impl redux::EnablingCondition<crate::SnarkState> for SnarkBlockVerifySuccessAction {
66-
fn is_enabled(&self, state: &crate::SnarkState) -> bool {
67-
state
68-
.block_verify
69-
.jobs
70-
.get(self.req_id)
71-
.map_or(false, |v| v.is_pending())
72-
}
73-
}
74-
75-
#[derive(Serialize, Deserialize, Debug, Clone)]
76-
pub struct SnarkBlockVerifyFinishAction {
77-
pub req_id: SnarkBlockVerifyId,
78-
}
79-
80-
impl redux::EnablingCondition<crate::SnarkState> for SnarkBlockVerifyFinishAction {
9+
pub enum SnarkBlockVerifyAction {
10+
Init {
11+
req_id: SnarkBlockVerifyId,
12+
block: VerifiableBlockWithHash,
13+
},
14+
Pending {
15+
req_id: SnarkBlockVerifyId,
16+
},
17+
Error {
18+
req_id: SnarkBlockVerifyId,
19+
error: SnarkBlockVerifyError,
20+
},
21+
Success {
22+
req_id: SnarkBlockVerifyId,
23+
},
24+
Finish {
25+
req_id: SnarkBlockVerifyId,
26+
},
27+
}
28+
29+
impl redux::EnablingCondition<crate::SnarkState> for SnarkBlockVerifyAction {
8130
fn is_enabled(&self, state: &crate::SnarkState) -> bool {
82-
state
83-
.block_verify
84-
.jobs
85-
.get(self.req_id)
86-
.map_or(false, |v| v.is_finished())
87-
}
88-
}
89-
90-
macro_rules! impl_into_snark_action {
91-
($a:ty) => {
92-
impl From<$a> for crate::SnarkAction {
93-
fn from(value: $a) -> Self {
94-
Self::BlockVerify(value.into())
31+
match self {
32+
SnarkBlockVerifyAction::Init { req_id, .. } => {
33+
state.block_verify.jobs.next_req_id() == *req_id
9534
}
35+
SnarkBlockVerifyAction::Pending { req_id } => state
36+
.block_verify
37+
.jobs
38+
.get(*req_id)
39+
.map_or(false, |v| v.is_init()),
40+
SnarkBlockVerifyAction::Error { req_id, .. } => state
41+
.block_verify
42+
.jobs
43+
.get(*req_id)
44+
.map_or(false, |v| v.is_pending()),
45+
SnarkBlockVerifyAction::Success { req_id } => state
46+
.block_verify
47+
.jobs
48+
.get(*req_id)
49+
.map_or(false, |v| v.is_pending()),
50+
SnarkBlockVerifyAction::Finish { req_id } => state
51+
.block_verify
52+
.jobs
53+
.get(*req_id)
54+
.map_or(false, |v| v.is_finished()),
9655
}
97-
};
56+
}
9857
}
99-
100-
impl_into_snark_action!(SnarkBlockVerifyInitAction);
101-
impl_into_snark_action!(SnarkBlockVerifyPendingAction);
102-
impl_into_snark_action!(SnarkBlockVerifyErrorAction);
103-
impl_into_snark_action!(SnarkBlockVerifySuccessAction);
104-
impl_into_snark_action!(SnarkBlockVerifyFinishAction);
Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,31 @@
11
use redux::ActionMeta;
22

3-
use super::{
4-
SnarkBlockVerifyErrorAction, SnarkBlockVerifyFinishAction, SnarkBlockVerifyInitAction,
5-
SnarkBlockVerifyPendingAction, SnarkBlockVerifyService, SnarkBlockVerifySuccessAction,
6-
};
3+
use super::{SnarkBlockVerifyAction, SnarkBlockVerifyService};
74

8-
impl SnarkBlockVerifyInitAction {
9-
pub fn effects<Store, S>(&self, _: &ActionMeta, store: &mut Store)
10-
where
11-
Store: crate::SnarkStore<S>,
12-
Store::Service: SnarkBlockVerifyService,
13-
SnarkBlockVerifyPendingAction: redux::EnablingCondition<S>,
14-
{
15-
let req_id = self.req_id;
16-
let verifier_index = store.state().block_verify.verifier_index.clone();
17-
let verifier_srs = store.state().block_verify.verifier_srs.clone();
18-
store
19-
.service()
20-
.verify_init(req_id, verifier_index, verifier_srs, self.block.clone());
21-
store.dispatch(SnarkBlockVerifyPendingAction { req_id });
22-
}
23-
}
24-
25-
impl SnarkBlockVerifyErrorAction {
26-
pub fn effects<Store, S>(self, _: &ActionMeta, store: &mut Store)
27-
where
28-
Store: crate::SnarkStore<S>,
29-
Store::Service: SnarkBlockVerifyService,
30-
SnarkBlockVerifyFinishAction: redux::EnablingCondition<S>,
31-
{
32-
let req_id = self.req_id;
33-
store.dispatch(SnarkBlockVerifyFinishAction { req_id });
34-
}
35-
}
36-
37-
impl SnarkBlockVerifySuccessAction {
5+
impl SnarkBlockVerifyAction {
386
pub fn effects<Store, S>(self, _: &ActionMeta, store: &mut Store)
397
where
408
Store: crate::SnarkStore<S>,
419
Store::Service: SnarkBlockVerifyService,
42-
SnarkBlockVerifyFinishAction: redux::EnablingCondition<S>,
10+
SnarkBlockVerifyAction: redux::EnablingCondition<S>,
4311
{
44-
let req_id = self.req_id;
45-
store.dispatch(SnarkBlockVerifyFinishAction { req_id });
12+
match self {
13+
SnarkBlockVerifyAction::Init { req_id, block, .. } => {
14+
let verifier_index = store.state().block_verify.verifier_index.clone();
15+
let verifier_srs = store.state().block_verify.verifier_srs.clone();
16+
store
17+
.service()
18+
.verify_init(req_id, verifier_index, verifier_srs, block);
19+
store.dispatch(SnarkBlockVerifyAction::Pending { req_id });
20+
}
21+
SnarkBlockVerifyAction::Error { req_id, .. } => {
22+
store.dispatch(SnarkBlockVerifyAction::Finish { req_id });
23+
}
24+
SnarkBlockVerifyAction::Success { req_id, .. } => {
25+
store.dispatch(SnarkBlockVerifyAction::Finish { req_id });
26+
}
27+
SnarkBlockVerifyAction::Pending { .. } => {}
28+
SnarkBlockVerifyAction::Finish { .. } => {}
29+
}
4630
}
4731
}

snark/src/block_verify/snark_block_verify_reducer.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ impl SnarkBlockVerifyState {
77
pub fn reducer(&mut self, action: SnarkBlockVerifyActionWithMetaRef<'_>) {
88
let (action, meta) = action.split();
99
match action {
10-
SnarkBlockVerifyAction::Init(action) => {
10+
SnarkBlockVerifyAction::Init { block, .. } => {
1111
self.jobs.add(SnarkBlockVerifyStatus::Init {
1212
time: meta.time(),
13-
block: action.block.clone(),
13+
block: block.clone(),
1414
});
1515
}
16-
SnarkBlockVerifyAction::Pending(action) => {
17-
if let Some(req) = self.jobs.get_mut(action.req_id) {
16+
SnarkBlockVerifyAction::Pending { req_id, .. } => {
17+
if let Some(req) = self.jobs.get_mut(*req_id) {
1818
*req = match req {
1919
SnarkBlockVerifyStatus::Init { block, .. } => {
2020
SnarkBlockVerifyStatus::Pending {
@@ -26,22 +26,22 @@ impl SnarkBlockVerifyState {
2626
};
2727
}
2828
}
29-
SnarkBlockVerifyAction::Error(action) => {
30-
if let Some(req) = self.jobs.get_mut(action.req_id) {
29+
SnarkBlockVerifyAction::Error { req_id, error, .. } => {
30+
if let Some(req) = self.jobs.get_mut(*req_id) {
3131
*req = match req {
3232
SnarkBlockVerifyStatus::Pending { block, .. } => {
3333
SnarkBlockVerifyStatus::Error {
3434
time: meta.time(),
3535
block: block.clone(),
36-
error: action.error.clone(),
36+
error: error.clone(),
3737
}
3838
}
3939
_ => return,
4040
};
4141
}
4242
}
43-
SnarkBlockVerifyAction::Success(action) => {
44-
if let Some(req) = self.jobs.get_mut(action.req_id) {
43+
SnarkBlockVerifyAction::Success { req_id, .. } => {
44+
if let Some(req) = self.jobs.get_mut(*req_id) {
4545
*req = match req {
4646
SnarkBlockVerifyStatus::Pending { block, .. } => {
4747
SnarkBlockVerifyStatus::Success {
@@ -53,8 +53,8 @@ impl SnarkBlockVerifyState {
5353
};
5454
}
5555
}
56-
SnarkBlockVerifyAction::Finish(action) => {
57-
self.jobs.remove(action.req_id);
56+
SnarkBlockVerifyAction::Finish { req_id, .. } => {
57+
self.jobs.remove(*req_id);
5858
}
5959
}
6060
}

0 commit comments

Comments
 (0)