@@ -19,12 +19,13 @@ use clarity::codec::read_next;
19
19
use hashbrown:: HashMap ;
20
20
use libsigner:: { MessageSlotID , SignerMessage , SignerSession , StackerDBSession } ;
21
21
use libstackerdb:: { StackerDBChunkAckData , StackerDBChunkData } ;
22
- use slog:: { slog_debug, slog_warn} ;
22
+ use slog:: { slog_debug, slog_info , slog_warn} ;
23
23
use stacks_common:: types:: chainstate:: StacksPrivateKey ;
24
- use stacks_common:: { debug, warn} ;
24
+ use stacks_common:: util:: hash:: to_hex;
25
+ use stacks_common:: { debug, info, warn} ;
25
26
26
27
use crate :: client:: { retry_with_exponential_backoff, ClientError } ;
27
- use crate :: config:: SignerConfig ;
28
+ use crate :: config:: { SignerConfig , SignerConfigMode } ;
28
29
29
30
/// The signer StackerDB slot ID, purposefully wrapped to prevent conflation with SignerID
30
31
#[ derive( Debug , Clone , PartialEq , Eq , Hash , Copy , PartialOrd , Ord ) ]
@@ -36,6 +37,12 @@ impl std::fmt::Display for SignerSlotID {
36
37
}
37
38
}
38
39
40
+ #[ derive( Debug ) ]
41
+ enum StackerDBMode {
42
+ DryRun ,
43
+ Normal { signer_slot_id : SignerSlotID } ,
44
+ }
45
+
39
46
/// The StackerDB client for communicating with the .signers contract
40
47
#[ derive( Debug ) ]
41
48
pub struct StackerDB < M : MessageSlotID + std:: cmp:: Eq > {
@@ -46,32 +53,42 @@ pub struct StackerDB<M: MessageSlotID + std::cmp::Eq> {
46
53
stacks_private_key : StacksPrivateKey ,
47
54
/// A map of a message ID to last chunk version for each session
48
55
slot_versions : HashMap < M , HashMap < SignerSlotID , u32 > > ,
49
- /// The signer slot ID -- the index into the signer list for this signer daemon's signing key.
50
- signer_slot_id : SignerSlotID ,
56
+ /// The running mode of the stackerdb (whether the signer is running in dry-run or
57
+ /// normal operation)
58
+ mode : StackerDBMode ,
51
59
/// The reward cycle of the connecting signer
52
60
reward_cycle : u64 ,
53
61
}
54
62
55
63
impl < M : MessageSlotID + ' static > From < & SignerConfig > for StackerDB < M > {
56
64
fn from ( config : & SignerConfig ) -> Self {
65
+ let mode = match config. signer_mode {
66
+ SignerConfigMode :: DryRun => StackerDBMode :: DryRun ,
67
+ SignerConfigMode :: Normal {
68
+ ref signer_slot_id, ..
69
+ } => StackerDBMode :: Normal {
70
+ signer_slot_id : * signer_slot_id,
71
+ } ,
72
+ } ;
73
+
57
74
Self :: new (
58
75
& config. node_host ,
59
76
config. stacks_private_key ,
60
77
config. mainnet ,
61
78
config. reward_cycle ,
62
- config . signer_slot_id ,
79
+ mode ,
63
80
)
64
81
}
65
82
}
66
83
67
84
impl < M : MessageSlotID + ' static > StackerDB < M > {
68
- /// Create a new StackerDB client
69
- pub fn new (
85
+ /// Create a new StackerDB client running in normal operation
86
+ fn new (
70
87
host : & str ,
71
88
stacks_private_key : StacksPrivateKey ,
72
89
is_mainnet : bool ,
73
90
reward_cycle : u64 ,
74
- signer_slot_id : SignerSlotID ,
91
+ signer_mode : StackerDBMode ,
75
92
) -> Self {
76
93
let mut signers_message_stackerdb_sessions = HashMap :: new ( ) ;
77
94
for msg_id in M :: all ( ) {
@@ -84,7 +101,7 @@ impl<M: MessageSlotID + 'static> StackerDB<M> {
84
101
signers_message_stackerdb_sessions,
85
102
stacks_private_key,
86
103
slot_versions : HashMap :: new ( ) ,
87
- signer_slot_id ,
104
+ mode : signer_mode ,
88
105
reward_cycle,
89
106
}
90
107
}
@@ -110,18 +127,33 @@ impl<M: MessageSlotID + 'static> StackerDB<M> {
110
127
msg_id : & M ,
111
128
message_bytes : Vec < u8 > ,
112
129
) -> Result < StackerDBChunkAckData , ClientError > {
113
- let slot_id = self . signer_slot_id ;
130
+ let StackerDBMode :: Normal {
131
+ signer_slot_id : slot_id,
132
+ } = & self . mode
133
+ else {
134
+ info ! (
135
+ "Dry-run signer would have sent a stackerdb message" ;
136
+ "message_id" => ?msg_id,
137
+ "message_bytes" => to_hex( & message_bytes)
138
+ ) ;
139
+ return Ok ( StackerDBChunkAckData {
140
+ accepted : true ,
141
+ reason : None ,
142
+ metadata : None ,
143
+ code : None ,
144
+ } ) ;
145
+ } ;
114
146
loop {
115
147
let mut slot_version = if let Some ( versions) = self . slot_versions . get_mut ( msg_id) {
116
- if let Some ( version) = versions. get ( & slot_id) {
148
+ if let Some ( version) = versions. get ( slot_id) {
117
149
* version
118
150
} else {
119
- versions. insert ( slot_id, 0 ) ;
151
+ versions. insert ( * slot_id, 0 ) ;
120
152
1
121
153
}
122
154
} else {
123
155
let mut versions = HashMap :: new ( ) ;
124
- versions. insert ( slot_id, 0 ) ;
156
+ versions. insert ( * slot_id, 0 ) ;
125
157
self . slot_versions . insert ( * msg_id, versions) ;
126
158
1
127
159
} ;
@@ -143,7 +175,7 @@ impl<M: MessageSlotID + 'static> StackerDB<M> {
143
175
144
176
if let Some ( versions) = self . slot_versions . get_mut ( msg_id) {
145
177
// NOTE: per the above, this is always executed
146
- versions. insert ( slot_id, slot_version. saturating_add ( 1 ) ) ;
178
+ versions. insert ( * slot_id, slot_version. saturating_add ( 1 ) ) ;
147
179
} else {
148
180
return Err ( ClientError :: NotConnected ) ;
149
181
}
@@ -165,7 +197,7 @@ impl<M: MessageSlotID + 'static> StackerDB<M> {
165
197
}
166
198
if let Some ( versions) = self . slot_versions . get_mut ( msg_id) {
167
199
// NOTE: per the above, this is always executed
168
- versions. insert ( slot_id, slot_version. saturating_add ( 1 ) ) ;
200
+ versions. insert ( * slot_id, slot_version. saturating_add ( 1 ) ) ;
169
201
} else {
170
202
return Err ( ClientError :: NotConnected ) ;
171
203
}
@@ -216,11 +248,6 @@ impl<M: MessageSlotID + 'static> StackerDB<M> {
216
248
u32:: try_from ( self . reward_cycle % 2 ) . expect ( "FATAL: reward cycle % 2 exceeds u32::MAX" )
217
249
}
218
250
219
- /// Retrieve the signer slot ID
220
- pub fn get_signer_slot_id ( & self ) -> SignerSlotID {
221
- self . signer_slot_id
222
- }
223
-
224
251
/// Get the session corresponding to the given message ID if it exists
225
252
pub fn get_session_mut ( & mut self , msg_id : & M ) -> Option < & mut StackerDBSession > {
226
253
self . signers_message_stackerdb_sessions . get_mut ( msg_id)
0 commit comments