@@ -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 ,
@@ -455,20 +455,7 @@ impl Signer {
455
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.
456
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
457
457
let signer_signature_hash = block_proposal. block . header . signer_signature_hash ( ) ;
458
- if let Some ( block_info) = self
459
- . signer_db
460
- . block_lookup ( & signer_signature_hash)
461
- . expect ( "Failed to connect to signer DB" )
462
- {
463
- // Redundant check, but just in case the block proposal reward cycle was incorrectly overwritten, check again
464
- if block_info. reward_cycle != self . reward_cycle {
465
- // We are not signing for this reward cycle. Ignore the block.
466
- debug ! (
467
- "{self}: Received a block proposal for a different reward cycle. Ignore it." ;
468
- "requested_reward_cycle" => block_proposal. reward_cycle
469
- ) ;
470
- return ;
471
- }
458
+ if let Some ( block_info) = self . block_lookup_by_reward_cycle ( & signer_signature_hash) {
472
459
let Some ( block_response) = self . determine_response ( & block_info) else {
473
460
// We are still waiting for a response for this block. Do nothing.
474
461
debug ! ( "{self}: Received a block proposal for a block we are already validating." ;
@@ -677,32 +664,15 @@ impl Signer {
677
664
self . submitted_block_proposal = None ;
678
665
}
679
666
// 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 for a block from 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
- }
667
+ let Some ( mut block_info) = self . block_lookup_by_reward_cycle ( & signer_signature_hash) else {
668
+ // We have not seen this block before. Why are we getting a response for it?
669
+ debug ! ( "{self}: Received a block validate response for a block we have not seen before. Ignoring..." ) ;
670
+ return None ;
705
671
} ;
672
+ if block_info. is_locally_finalized ( ) {
673
+ debug ! ( "{self}: Received block validation for a block that is already marked as {}. Ignoring..." , block_info. state) ;
674
+ return None ;
675
+ }
706
676
707
677
if let Some ( block_response) = self . check_block_against_signer_db_state ( & block_info. block ) {
708
678
// The signer db state has changed. We no longer view this block as valid. Override the validation response.
@@ -770,32 +740,15 @@ impl Signer {
770
740
{
771
741
self . submitted_block_proposal = None ;
772
742
}
773
- let mut block_info = match self . signer_db . block_lookup ( & signer_signature_hash) {
774
- Ok ( Some ( block_info) ) => {
775
- if block_info. reward_cycle != self . reward_cycle {
776
- // We are not signing for this reward cycle. Ignore the block.
777
- debug ! (
778
- "{self}: Received a block validation for a block from a different reward cycle. Ignore it." ;
779
- "requested_reward_cycle" => block_info. reward_cycle
780
- ) ;
781
- return None ;
782
- }
783
- if block_info. is_locally_finalized ( ) {
784
- debug ! ( "{self}: Received block validation for a block that is already marked as {}. Ignoring..." , block_info. state) ;
785
- return None ;
786
- }
787
- block_info
788
- }
789
- Ok ( None ) => {
790
- // We have not seen this block before. Why are we getting a response for it?
791
- debug ! ( "{self}: Received a block validate response for a block we have not seen before. Ignoring..." ) ;
792
- return None ;
793
- }
794
- Err ( e) => {
795
- error ! ( "{self}: Failed to lookup block in signer db: {e:?}" ) ;
796
- return None ;
797
- }
743
+ let Some ( mut block_info) = self . block_lookup_by_reward_cycle ( & signer_signature_hash) else {
744
+ // We have not seen this block before. Why are we getting a response for it?
745
+ debug ! ( "{self}: Received a block validate response for a block we have not seen before. Ignoring..." ) ;
746
+ return None ;
798
747
} ;
748
+ if block_info. is_locally_finalized ( ) {
749
+ debug ! ( "{self}: Received block validation for a block that is already marked as {}. Ignoring..." , block_info. state) ;
750
+ return None ;
751
+ }
799
752
if let Err ( e) = block_info. mark_locally_rejected ( ) {
800
753
if !block_info. has_reached_consensus ( ) {
801
754
warn ! ( "{self}: Failed to mark block as locally rejected: {e:?}" , ) ;
@@ -871,9 +824,7 @@ impl Signer {
871
824
// For mutability reasons, we need to take the block_info out of the map and add it back after processing
872
825
let mut block_info = match self . signer_db . block_lookup ( & signature_sighash) {
873
826
Ok ( Some ( block_info) ) => {
874
- if block_info. state == BlockState :: GloballyRejected
875
- || block_info. state == BlockState :: GloballyAccepted
876
- {
827
+ if block_info. has_reached_consensus ( ) {
877
828
// The block has already reached consensus.
878
829
return ;
879
830
}
@@ -950,33 +901,16 @@ impl Signer {
950
901
let block_hash = & rejection. signer_signature_hash ;
951
902
let signature = & rejection. signature ;
952
903
953
- let mut block_info = match self . signer_db . block_lookup ( block_hash) {
954
- Ok ( Some ( block_info) ) => {
955
- if block_info. reward_cycle != self . reward_cycle {
956
- // We are not signing for this reward cycle. Ignore the block.
957
- debug ! (
958
- "{self}: Received a block rejection for a block from a different reward cycle. Ignore it." ;
959
- "requested_reward_cycle" => block_info. reward_cycle
960
- ) ;
961
- return ;
962
- }
963
- if block_info. state == BlockState :: GloballyRejected
964
- || block_info. state == BlockState :: GloballyAccepted
965
- {
966
- debug ! ( "{self}: Received block rejection for a block that is already marked as {}. Ignoring..." , block_info. state) ;
967
- return ;
968
- }
969
- block_info
970
- }
971
- Ok ( None ) => {
972
- debug ! ( "{self}: Received block rejection for a block we have not seen before. Ignoring..." ) ;
973
- return ;
974
- }
975
- Err ( e) => {
976
- warn ! ( "{self}: Failed to load block state: {e:?}" , ) ;
977
- return ;
978
- }
904
+ let Some ( mut block_info) = self . block_lookup_by_reward_cycle ( block_hash) else {
905
+ debug ! (
906
+ "{self}: Received block rejection for a block we have not seen before. Ignoring..."
907
+ ) ;
908
+ return ;
979
909
} ;
910
+ if block_info. has_reached_consensus ( ) {
911
+ debug ! ( "{self}: Received block rejection for a block that is already marked as {}. Ignoring..." , block_info. state) ;
912
+ return ;
913
+ }
980
914
981
915
// recover public key
982
916
let Ok ( public_key) = rejection. recover_public_key ( ) else {
@@ -1062,33 +996,16 @@ impl Signer {
1062
996
"{self}: Received a block-accept signature: ({block_hash}, {signature}, {})" ,
1063
997
metadata. server_version
1064
998
) ;
1065
- let mut block_info = match self . signer_db . block_lookup ( block_hash) {
1066
- Ok ( Some ( block_info) ) => {
1067
- if block_info. reward_cycle != self . reward_cycle {
1068
- // We are not signing for this reward cycle. Ignore the block.
1069
- debug ! (
1070
- "{self}: Received a block signature for a block from a different reward cycle. Ignore it." ;
1071
- "requested_reward_cycle" => block_info. reward_cycle
1072
- ) ;
1073
- return ;
1074
- }
1075
- if block_info. state == BlockState :: GloballyRejected
1076
- || block_info. state == BlockState :: GloballyAccepted
1077
- {
1078
- debug ! ( "{self}: Received block signature for a block that is already marked as {}. Ignoring..." , block_info. state) ;
1079
- return ;
1080
- }
1081
- block_info
1082
- }
1083
- Ok ( None ) => {
1084
- debug ! ( "{self}: Received block signature for a block we have not seen before. Ignoring..." ) ;
1085
- return ;
1086
- }
1087
- Err ( e) => {
1088
- warn ! ( "{self}: Failed to load block state: {e:?}" , ) ;
1089
- return ;
1090
- }
999
+ let Some ( mut block_info) = self . block_lookup_by_reward_cycle ( block_hash) else {
1000
+ debug ! (
1001
+ "{self}: Received block signature for a block we have not seen before. Ignoring..."
1002
+ ) ;
1003
+ return ;
1091
1004
} ;
1005
+ if block_info. has_reached_consensus ( ) {
1006
+ debug ! ( "{self}: Received block signature for a block that is already marked as {}. Ignoring..." , block_info. state) ;
1007
+ return ;
1008
+ }
1092
1009
1093
1010
// recover public key
1094
1011
let Ok ( public_key) = Secp256k1PublicKey :: recover_to_pubkey ( block_hash. bits ( ) , signature)
@@ -1236,4 +1153,24 @@ impl Signer {
1236
1153
error ! ( "{self}: Failed to insert block into signer-db: {e:?}" ) ;
1237
1154
panic ! ( "{self} Failed to write block to signerdb: {e}" ) ;
1238
1155
}
1156
+
1157
+ /// Helper for getting the block info from the db while accommodating for reward cycle
1158
+ pub fn block_lookup_by_reward_cycle (
1159
+ & self ,
1160
+ block_hash : & Sha512Trunc256Sum ,
1161
+ ) -> Option < BlockInfo > {
1162
+ let block_info = self
1163
+ . signer_db
1164
+ . block_lookup ( block_hash)
1165
+ . inspect_err ( |e| {
1166
+ error ! ( "{self}: Failed to lookup block hash {block_hash} in signer db: {e:?}" ) ;
1167
+ } )
1168
+ . ok ( )
1169
+ . flatten ( ) ?;
1170
+ if block_info. reward_cycle == self . reward_cycle {
1171
+ Some ( block_info)
1172
+ } else {
1173
+ None
1174
+ }
1175
+ }
1239
1176
}
0 commit comments