@@ -24,7 +24,7 @@ use blockstack_lib::net::api::postblock_proposal::{
24
24
use blockstack_lib:: util_lib:: db:: Error as DBError ;
25
25
use clarity:: types:: chainstate:: StacksPrivateKey ;
26
26
use clarity:: types:: { PrivateKey , StacksEpochId } ;
27
- use clarity:: util:: hash:: MerkleHashFunc ;
27
+ use clarity:: util:: hash:: { MerkleHashFunc , Sha512Trunc256Sum } ;
28
28
use clarity:: util:: secp256k1:: Secp256k1PublicKey ;
29
29
use libsigner:: v0:: messages:: {
30
30
BlockAccepted , BlockRejection , BlockResponse , MessageSlotID , MockProposal , MockSignature ,
@@ -128,6 +128,17 @@ impl SignerTrait<SignerMessage> for Signer {
128
128
debug ! ( "{self}: No event received" ) ;
129
129
return ;
130
130
} ;
131
+ if self . reward_cycle > current_reward_cycle
132
+ && !matches ! (
133
+ event,
134
+ SignerEvent :: StatusCheck | SignerEvent :: NewBurnBlock { .. }
135
+ )
136
+ {
137
+ // The reward cycle has not yet started for this signer instance
138
+ // Do not process any events other than status checks or new burn blocks
139
+ debug ! ( "{self}: Signer reward cycle has not yet started. Ignoring event." ) ;
140
+ return ;
141
+ }
131
142
match event {
132
143
SignerEvent :: BlockValidationResponse ( block_validate_response) => {
133
144
debug ! ( "{self}: Received a block proposal result from the stacks node..." ) ;
@@ -444,11 +455,7 @@ impl Signer {
444
455
// TODO: should add a check to ignore an old burn block height if we know its outdated. Would require us to store the burn block height we last saw on the side.
445
456
// the signer needs to be able to determine whether or not the block they're about to sign would conflict with an already-signed Stacks block
446
457
let signer_signature_hash = block_proposal. block . header . signer_signature_hash ( ) ;
447
- if let Some ( block_info) = self
448
- . signer_db
449
- . block_lookup ( & signer_signature_hash)
450
- . expect ( "Failed to connect to signer DB" )
451
- {
458
+ if let Some ( block_info) = self . block_lookup_by_reward_cycle ( & signer_signature_hash) {
452
459
let Some ( block_response) = self . determine_response ( & block_info) else {
453
460
// We are still waiting for a response for this block. Do nothing.
454
461
debug ! ( "{self}: Received a block proposal for a block we are already validating." ;
@@ -677,32 +684,15 @@ impl Signer {
677
684
self . submitted_block_proposal = None ;
678
685
}
679
686
// For mutability reasons, we need to take the block_info out of the map and add it back after processing
680
- let mut block_info = match self . signer_db . block_lookup ( & signer_signature_hash) {
681
- Ok ( Some ( block_info) ) => {
682
- if block_info. reward_cycle != self . reward_cycle {
683
- // We are not signing for this reward cycle. Ignore the block.
684
- debug ! (
685
- "{self}: Received a block validation response for a different reward cycle. Ignore it." ;
686
- "requested_reward_cycle" => block_info. reward_cycle,
687
- ) ;
688
- return None ;
689
- }
690
- if block_info. is_locally_finalized ( ) {
691
- debug ! ( "{self}: Received block validation for a block that is already marked as {}. Ignoring..." , block_info. state) ;
692
- return None ;
693
- }
694
- block_info
695
- }
696
- Ok ( None ) => {
697
- // We have not seen this block before. Why are we getting a response for it?
698
- debug ! ( "{self}: Received a block validate response for a block we have not seen before. Ignoring..." ) ;
699
- return None ;
700
- }
701
- Err ( e) => {
702
- error ! ( "{self}: Failed to lookup block in signer db: {e:?}" , ) ;
703
- return None ;
704
- }
687
+ let Some ( mut block_info) = self . block_lookup_by_reward_cycle ( & signer_signature_hash) else {
688
+ // We have not seen this block before. Why are we getting a response for it?
689
+ debug ! ( "{self}: Received a block validate response for a block we have not seen before. Ignoring..." ) ;
690
+ return None ;
705
691
} ;
692
+ if block_info. is_locally_finalized ( ) {
693
+ debug ! ( "{self}: Received block validation for a block that is already marked as {}. Ignoring..." , block_info. state) ;
694
+ return None ;
695
+ }
706
696
707
697
if let Some ( block_response) =
708
698
self . check_block_against_signer_db_state ( stacks_client, & block_info. block )
@@ -772,32 +762,15 @@ impl Signer {
772
762
{
773
763
self . submitted_block_proposal = None ;
774
764
}
775
- let mut block_info = match self . signer_db . block_lookup ( & signer_signature_hash) {
776
- Ok ( Some ( block_info) ) => {
777
- if block_info. reward_cycle != self . reward_cycle {
778
- // We are not signing for this reward cycle. Ignore the block.
779
- debug ! (
780
- "{self}: Received a block validation response for a different reward cycle. Ignore it." ;
781
- "requested_reward_cycle" => block_info. reward_cycle,
782
- ) ;
783
- return None ;
784
- }
785
- if block_info. is_locally_finalized ( ) {
786
- debug ! ( "{self}: Received block validation for a block that is already marked as {}. Ignoring..." , block_info. state) ;
787
- return None ;
788
- }
789
- block_info
790
- }
791
- Ok ( None ) => {
792
- // We have not seen this block before. Why are we getting a response for it?
793
- debug ! ( "{self}: Received a block validate response for a block we have not seen before. Ignoring..." ) ;
794
- return None ;
795
- }
796
- Err ( e) => {
797
- error ! ( "{self}: Failed to lookup block in signer db: {e:?}" ) ;
798
- return None ;
799
- }
765
+ let Some ( mut block_info) = self . block_lookup_by_reward_cycle ( & signer_signature_hash) else {
766
+ // We have not seen this block before. Why are we getting a response for it?
767
+ debug ! ( "{self}: Received a block validate response for a block we have not seen before. Ignoring..." ) ;
768
+ return None ;
800
769
} ;
770
+ if block_info. is_locally_finalized ( ) {
771
+ debug ! ( "{self}: Received block validation for a block that is already marked as {}. Ignoring..." , block_info. state) ;
772
+ return None ;
773
+ }
801
774
if let Err ( e) = block_info. mark_locally_rejected ( ) {
802
775
if !block_info. has_reached_consensus ( ) {
803
776
warn ! ( "{self}: Failed to mark block as locally rejected: {e:?}" , ) ;
@@ -873,9 +846,7 @@ impl Signer {
873
846
// For mutability reasons, we need to take the block_info out of the map and add it back after processing
874
847
let mut block_info = match self . signer_db . block_lookup ( & signature_sighash) {
875
848
Ok ( Some ( block_info) ) => {
876
- if block_info. state == BlockState :: GloballyRejected
877
- || block_info. state == BlockState :: GloballyAccepted
878
- {
849
+ if block_info. has_reached_consensus ( ) {
879
850
// The block has already reached consensus.
880
851
return ;
881
852
}
@@ -952,25 +923,16 @@ impl Signer {
952
923
let block_hash = & rejection. signer_signature_hash ;
953
924
let signature = & rejection. signature ;
954
925
955
- let mut block_info = match self . signer_db . block_lookup ( block_hash) {
956
- Ok ( Some ( block_info) ) => {
957
- if block_info. state == BlockState :: GloballyRejected
958
- || block_info. state == BlockState :: GloballyAccepted
959
- {
960
- debug ! ( "{self}: Received block rejection for a block that is already marked as {}. Ignoring..." , block_info. state) ;
961
- return ;
962
- }
963
- block_info
964
- }
965
- Ok ( None ) => {
966
- debug ! ( "{self}: Received block rejection for a block we have not seen before. Ignoring..." ) ;
967
- return ;
968
- }
969
- Err ( e) => {
970
- warn ! ( "{self}: Failed to load block state: {e:?}" , ) ;
971
- return ;
972
- }
926
+ let Some ( mut block_info) = self . block_lookup_by_reward_cycle ( block_hash) else {
927
+ debug ! (
928
+ "{self}: Received block rejection for a block we have not seen before. Ignoring..."
929
+ ) ;
930
+ return ;
973
931
} ;
932
+ if block_info. has_reached_consensus ( ) {
933
+ debug ! ( "{self}: Received block rejection for a block that is already marked as {}. Ignoring..." , block_info. state) ;
934
+ return ;
935
+ }
974
936
975
937
// recover public key
976
938
let Ok ( public_key) = rejection. recover_public_key ( ) else {
@@ -1056,23 +1018,15 @@ impl Signer {
1056
1018
"{self}: Received a block-accept signature: ({block_hash}, {signature}, {})" ,
1057
1019
metadata. server_version
1058
1020
) ;
1059
-
1060
- // Have we already processed this block?
1061
- match self . signer_db . get_block_state ( block_hash) {
1062
- Ok ( Some ( state) ) => {
1063
- if state == BlockState :: GloballyAccepted || state == BlockState :: GloballyRejected {
1064
- debug ! ( "{self}: Received block signature for a block that is already marked as {}. Ignoring..." , state) ;
1065
- return ;
1066
- }
1067
- }
1068
- Ok ( None ) => {
1069
- debug ! ( "{self}: Received block signature for a block we have not seen before. Ignoring..." ) ;
1070
- return ;
1071
- }
1072
- Err ( e) => {
1073
- warn ! ( "{self}: Failed to load block state: {e:?}" , ) ;
1074
- return ;
1075
- }
1021
+ let Some ( mut block_info) = self . block_lookup_by_reward_cycle ( block_hash) else {
1022
+ debug ! (
1023
+ "{self}: Received block signature for a block we have not seen before. Ignoring..."
1024
+ ) ;
1025
+ return ;
1026
+ } ;
1027
+ if block_info. has_reached_consensus ( ) {
1028
+ debug ! ( "{self}: Received block signature for a block that is already marked as {}. Ignoring..." , block_info. state) ;
1029
+ return ;
1076
1030
}
1077
1031
1078
1032
// recover public key
@@ -1140,12 +1094,6 @@ impl Signer {
1140
1094
}
1141
1095
1142
1096
// have enough signatures to broadcast!
1143
- let Ok ( Some ( mut block_info) ) = self . signer_db . block_lookup ( block_hash) . inspect_err ( |e| {
1144
- warn ! ( "{self}: Failed to load block {block_hash}: {e:?})" ) ;
1145
- } ) else {
1146
- warn ! ( "{self}: No such block {block_hash}" ) ;
1147
- return ;
1148
- } ;
1149
1097
// move block to LOCALLY accepted state.
1150
1098
// It is only considered globally accepted IFF we receive a new block event confirming it OR see the chain tip of the node advance to it.
1151
1099
if let Err ( e) = block_info. mark_locally_accepted ( true ) {
@@ -1227,4 +1175,24 @@ impl Signer {
1227
1175
error ! ( "{self}: Failed to insert block into signer-db: {e:?}" ) ;
1228
1176
panic ! ( "{self} Failed to write block to signerdb: {e}" ) ;
1229
1177
}
1178
+
1179
+ /// Helper for getting the block info from the db while accommodating for reward cycle
1180
+ pub fn block_lookup_by_reward_cycle (
1181
+ & self ,
1182
+ block_hash : & Sha512Trunc256Sum ,
1183
+ ) -> Option < BlockInfo > {
1184
+ let block_info = self
1185
+ . signer_db
1186
+ . block_lookup ( block_hash)
1187
+ . inspect_err ( |e| {
1188
+ error ! ( "{self}: Failed to lookup block hash {block_hash} in signer db: {e:?}" ) ;
1189
+ } )
1190
+ . ok ( )
1191
+ . flatten ( ) ?;
1192
+ if block_info. reward_cycle == self . reward_cycle {
1193
+ Some ( block_info)
1194
+ } else {
1195
+ None
1196
+ }
1197
+ }
1230
1198
}
0 commit comments