Skip to content

Commit 4736ab4

Browse files
committed
WIP: Add GlobalStateEvaluator struct
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent bb08906 commit 4736ab4

File tree

2 files changed

+302
-145
lines changed

2 files changed

+302
-145
lines changed

stacks-signer/src/v0/signer.rs

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ use clarity::util::sleep_ms;
3535
use clarity::util::tests::TestFlag;
3636
use libsigner::v0::messages::{
3737
BlockAccepted, BlockRejection, BlockResponse, MessageSlotID, MockProposal, MockSignature,
38-
RejectReason, RejectReasonPrefix, SignerMessage,
38+
RejectReason, RejectReasonPrefix, SignerMessage, StateMachineUpdate,
3939
};
4040
use libsigner::{BlockProposal, SignerEvent};
4141
use stacks_common::types::chainstate::{StacksAddress, StacksPublicKey};
4242
use stacks_common::util::get_epoch_time_secs;
4343
use stacks_common::util::secp256k1::MessageSignature;
4444
use stacks_common::{debug, error, info, warn};
4545

46-
use super::signer_state::LocalStateMachine;
46+
use super::signer_state::{GlobalStateEvaluator, LocalStateMachine};
4747
use crate::chainstate::{ProposalEvalConfig, SortitionMinerStatus, SortitionsView};
4848
use crate::client::{ClientError, SignerSlotID, StackerDB, StacksClient};
4949
use crate::config::{SignerConfig, SignerConfigMode};
@@ -109,6 +109,8 @@ pub struct Signer {
109109
pub block_proposal_max_age_secs: u64,
110110
/// The signer's local state machine used in signer set agreement
111111
pub local_state_machine: LocalStateMachine,
112+
/// The signer's global state evaluator
113+
pub global_state_evaluator: GlobalStateEvaluator,
112114
}
113115

114116
impl std::fmt::Display for SignerMode {
@@ -137,7 +139,7 @@ impl SignerTrait<SignerMessage> for Signer {
137139

138140
debug!("Reward cycle #{} {mode}", signer_config.reward_cycle);
139141

140-
let signer_db =
142+
let mut signer_db =
141143
SignerDb::new(&signer_config.db_path).expect("Failed to connect to signer Db");
142144
let proposal_config = ProposalEvalConfig::from(&signer_config);
143145

@@ -150,6 +152,17 @@ impl SignerTrait<SignerMessage> for Signer {
150152
signer_config.mainnet,
151153
&StacksPublicKey::from_private(&signer_config.stacks_private_key),
152154
);
155+
156+
let updates = signer_db
157+
.get_signer_state_machine_updates(signer_config.reward_cycle)
158+
.inspect_err(|e| {
159+
warn!("An error occurred retrieving state machine updates from the db: {e}")
160+
})
161+
.unwrap_or_default();
162+
let global_state_evaluator = GlobalStateEvaluator::new(
163+
updates,
164+
signer_config.signer_entries.signer_addr_to_weight.clone(),
165+
);
153166
Self {
154167
private_key: signer_config.stacks_private_key,
155168
stacks_address,
@@ -166,6 +179,7 @@ impl SignerTrait<SignerMessage> for Signer {
166179
block_proposal_validation_timeout: signer_config.block_proposal_validation_timeout,
167180
block_proposal_max_age_secs: signer_config.block_proposal_max_age_secs,
168181
local_state_machine: signer_state,
182+
global_state_evaluator,
169183
}
170184
}
171185

@@ -246,21 +260,7 @@ impl SignerTrait<SignerMessage> for Signer {
246260
sortition_state,
247261
),
248262
SignerMessage::StateMachineUpdate(update) => {
249-
// TODO: should make note of this update view point to determine if there is an agreed upon global state
250-
// We don't need to check the message parity again because of the check at the start of process_event
251-
if let Err(e) = self.signer_db.insert_state_machine_update(
252-
self.reward_cycle,
253-
&StacksAddress::p2pkh(self.mainnet, signer_public_key),
254-
update,
255-
) {
256-
warn!("{self}: Failed to update global state in signerdb: {e}");
257-
}
258-
self.local_state_machine.capitulate_miner_view(
259-
&mut self.signer_db,
260-
self.reward_cycle,
261-
self.stacks_address,
262-
&self.signer_weights,
263-
);
263+
self.handle_state_machine_update(signer_public_key, update);
264264
}
265265
_ => {}
266266
}
@@ -584,6 +584,31 @@ impl Signer {
584584
self.impl_send_block_response(block_response)
585585
}
586586

587+
/// Handle signer state update message
588+
fn handle_state_machine_update(
589+
&mut self,
590+
signer_public_key: &Secp256k1PublicKey,
591+
update: &StateMachineUpdate,
592+
) {
593+
let address = StacksAddress::p2pkh(self.mainnet, signer_public_key);
594+
// Store the state machine update so we can reload it if we crash
595+
if let Err(e) =
596+
self.signer_db
597+
.insert_state_machine_update(self.reward_cycle, &address, update)
598+
{
599+
warn!("{self}: Failed to update global state in signerdb: {e}");
600+
}
601+
self.global_state_evaluator
602+
.insert_update(address, update.clone());
603+
604+
// See if this update means we should capitulate our viewpoint...
605+
self.local_state_machine.capitulate_viewpoint(
606+
&mut self.signer_db,
607+
&mut self.global_state_evaluator,
608+
self.stacks_address,
609+
);
610+
}
611+
587612
/// Handle block proposal messages submitted to signers stackerdb
588613
fn handle_block_proposal(
589614
&mut self,

0 commit comments

Comments
 (0)