1
- use super :: state_machine:: db_state:: DBState ;
2
- use super :: state_machine:: db_state:: DBStateError ;
1
+ use std:: fmt:: Display ;
2
+
3
+ use super :: actions_active as active;
4
+ use super :: state:: DBStateError ;
5
+ use super :: state:: State ;
3
6
4
7
use crate :: core:: beacon:: BeaconProcess ;
5
8
use crate :: key:: keys:: Identity ;
6
9
use crate :: key:: Scheme ;
7
10
8
11
use crate :: protobuf:: dkg:: packet:: Bundle ;
9
12
use crate :: protobuf:: dkg:: DkgStatusResponse ;
10
- use crate :: transport:: dkg:: DkgCommand ;
13
+ use crate :: transport:: dkg:: Command ;
11
14
use crate :: transport:: dkg:: GossipData ;
12
15
use crate :: transport:: dkg:: GossipPacket ;
13
16
14
17
use tokio:: sync:: mpsc;
15
18
use tokio:: sync:: oneshot;
16
19
use tracing:: debug;
17
20
use tracing:: error;
21
+ use tracing:: warn;
22
+
23
+ /// Callback for DKG actions
24
+ pub struct Callback < T > {
25
+ inner : oneshot:: Sender < Result < T , ActionsError > > ,
26
+ }
27
+
28
+ /// For diagnostic purposes, this should never happen
29
+ const ERR_SEND : & str = "dkg callback receiver is dropped" ;
18
30
19
- /// Callback for all dkg actions
20
- pub type DkgCallback < T > = tokio:: sync:: oneshot:: Sender < Result < T , DkgActionsError > > ;
31
+ impl < T > Callback < T > {
32
+ /// Sends a response back through the callback channel.
33
+ fn reply ( self , result : Result < T , ActionsError > ) {
34
+ if let Err ( err) = & result {
35
+ error ! ( "failed to proceed dkg request: {err}" )
36
+ }
37
+ if self . inner . send ( result) . is_err ( ) {
38
+ error ! ( "{ERR_SEND}" ) ;
39
+ } ;
40
+ }
41
+ }
21
42
22
- /// For diagnostic purposes, should be never possible
23
- const ERR_SEND : & str = "dkg receiver is dropped" ;
43
+ impl < T > From < oneshot:: Sender < Result < T , ActionsError > > > for Callback < T > {
44
+ fn from ( tx : oneshot:: Sender < Result < T , ActionsError > > ) -> Self {
45
+ Self { inner : tx }
46
+ }
47
+ }
24
48
25
49
pub struct DkgHandler {
26
- sender : mpsc:: Sender < DkgActions > ,
50
+ sender : mpsc:: Sender < Actions > ,
27
51
}
28
52
29
- pub enum DkgActions {
30
- Gossip ( GossipPacket , DkgCallback < ( ) > ) ,
31
- Shutdown ( DkgCallback < ( ) > ) ,
32
- Command ( DkgCommand , DkgCallback < ( ) > ) ,
33
- Broadcast ( Bundle , DkgCallback < ( ) > ) ,
34
- Status ( DkgCallback < DkgStatusResponse > ) ,
53
+ pub enum Actions {
54
+ Gossip ( GossipPacket , Callback < ( ) > ) ,
55
+ Shutdown ( Callback < ( ) > ) ,
56
+ Command ( Command , Callback < ( ) > ) ,
57
+ Broadcast ( Bundle , Callback < ( ) > ) ,
58
+ Status ( Callback < DkgStatusResponse > ) ,
35
59
}
36
60
37
61
#[ derive( thiserror:: Error , Debug ) ]
38
- pub enum DkgActionsError {
62
+ pub enum ActionsError {
39
63
#[ error( "db state error: {0}" ) ]
40
64
DBState ( #[ from] DBStateError ) ,
41
65
#[ error( "TODO: this dkg action is not implemented yet" ) ]
42
66
TODO ,
43
67
}
44
68
45
69
impl DkgHandler {
46
- pub async fn new_action ( & self , action : DkgActions ) {
70
+ pub async fn new_action ( & self , action : Actions ) {
47
71
if self . sender . send ( action) . await . is_err ( ) {
48
72
error ! ( "{ERR_SEND}" )
49
73
}
50
74
}
51
75
52
76
pub fn fresh_install < S : Scheme > ( bp : BeaconProcess < S > ) -> Self {
53
- let ( sender, mut receiver) = mpsc:: channel :: < DkgActions > ( 1 ) ;
77
+ let ( sender, mut receiver) = mpsc:: channel :: < Actions > ( 1 ) ;
54
78
let tracker = bp. tracker ( ) . clone ( ) ;
55
79
56
80
tracker. spawn ( async move {
57
81
let me = bp. keypair . public_identity ( ) ;
58
- let mut db_state = DBState :: < S > :: new_fresh ( bp. id ( ) ) ;
82
+ let mut db_state = State :: < S > :: new_fresh ( bp. id ( ) ) ;
59
83
60
84
while let Some ( actions) = receiver. recv ( ) . await {
61
85
handle_actions ( & mut db_state, me, actions) . await ;
@@ -68,59 +92,33 @@ impl DkgHandler {
68
92
}
69
93
}
70
94
71
- async fn handle_actions < S : Scheme > ( db : & mut DBState < S > , me : & Identity < S > , actions : DkgActions ) {
72
- match actions {
73
- DkgActions :: Shutdown ( callback) => todo_request ( callback , "shutdown" ) ,
74
- DkgActions :: Status ( callback) => status ( db, callback ) ,
75
- DkgActions :: Command ( command , callback) => handle_command ( command, callback ) . await ,
76
- DkgActions :: Gossip ( gossip , callback) => handle_gossip ( db, me, gossip , callback ) . await ,
77
- DkgActions :: Broadcast ( _bundle , callback) => todo_request ( callback , "broadcast" ) ,
95
+ async fn handle_actions < S : Scheme > ( db : & mut State < S > , me : & Identity < S > , request : Actions ) {
96
+ match request {
97
+ Actions :: Shutdown ( callback) => callback . reply ( todo_request ( "shutdown" ) ) ,
98
+ Actions :: Status ( callback) => callback . reply ( active :: status ( db) ) ,
99
+ Actions :: Command ( cmd , callback) => callback . reply ( active :: command ( cmd , db , me ) . await ) ,
100
+ Actions :: Gossip ( packet , callback) => callback . reply ( gossip ( db, me, packet ) . await ) ,
101
+ Actions :: Broadcast ( bundle , callback) => callback . reply ( todo_request ( & bundle . to_string ( ) ) ) ,
78
102
}
79
103
}
80
104
81
- /// Dkg status request
82
- fn status < S : Scheme > ( db : & mut DBState < S > , callback : DkgCallback < DkgStatusResponse > ) {
83
- if callback
84
- . send ( db. status ( ) . map_err ( |err| {
85
- error ! ( "dkg status request, id: {}, error: {err}" , db. id( ) ) ;
86
- err. into ( )
87
- } ) )
88
- . is_err ( )
89
- {
90
- error ! ( "dkg status request, id: {}, error: {ERR_SEND}" , db. id( ) ) ;
91
- } ;
92
- }
93
-
94
- async fn handle_command ( dkg_command : DkgCommand , callback : DkgCallback < ( ) > ) {
95
- let _metadata = & dkg_command. metadata ;
96
- todo_request ( callback, dkg_command. command . to_string ( ) . as_str ( ) )
97
- }
98
-
99
- async fn handle_gossip < S : Scheme > (
100
- db : & mut DBState < S > ,
105
+ async fn gossip < S : Scheme > (
106
+ db : & mut State < S > ,
101
107
me : & Identity < S > ,
102
- gossip : crate :: transport:: dkg:: GossipPacket ,
103
- callback : oneshot:: Sender < Result < ( ) , DkgActionsError > > ,
104
- ) {
105
- let metadata = & gossip. metadata ;
106
- match gossip. data {
107
- GossipData :: Proposal ( proposal) => {
108
- if callback
109
- . send ( db. proposed ( me, proposal, metadata) . map_err ( |err| {
110
- error ! ( "received dkg proposal, id: {}, error: {err}" , db. id( ) ) ;
111
- err. into ( )
112
- } ) )
113
- . is_err ( )
114
- {
115
- error ! ( "received dkg proposal, id: {}, error: {ERR_SEND}" , db. id( ) ) ;
116
- } ;
117
- }
118
- _ => todo_request ( callback, gossip. data . to_string ( ) . as_str ( ) ) ,
108
+ packet : GossipPacket ,
109
+ ) -> Result < ( ) , ActionsError > {
110
+ let meta = & packet. metadata ;
111
+ match packet. data {
112
+ GossipData :: Proposal ( proposal) => db. proposed ( me, proposal, meta) ?,
113
+ _ => todo_request ( & packet. data ) ?,
119
114
}
115
+
116
+ Ok ( ( ) )
120
117
}
121
118
122
- fn todo_request < T > ( callback : DkgCallback < T > , kind : & str ) {
123
- if callback. send ( Err ( DkgActionsError :: TODO ) ) . is_err ( ) {
124
- error ! ( "failed to proceed {kind}, error: {ERR_SEND}" , ) ;
125
- } ;
119
+ /// Temporary tracker for unfinished logic
120
+ pub fn todo_request < D : Display + ?Sized > ( kind : & D ) -> Result < ( ) , ActionsError > {
121
+ warn ! ( "received TODO request: {kind}" ) ;
122
+
123
+ Err ( ActionsError :: TODO )
126
124
}
0 commit comments