@@ -2,7 +2,6 @@ package consensus
22
33import (
44 "math/big"
5- "sync/atomic"
65 "time"
76
87 "github.com/ethereum/go-ethereum/common"
6564// Signs the consensus message and returns the marshaled message.
6665func (consensus * Consensus ) signAndMarshalConsensusMessage (message * msg_pb.Message ,
6766 priKey * bls_core.SecretKey ) ([]byte , error ) {
68- if err := consensus . signConsensusMessage (message , priKey ); err != nil {
67+ if err := signConsensusMessage (message , priKey ); err != nil {
6968 return empty , err
7069 }
7170 marshaledMessage , err := protobuf .Marshal (message )
@@ -113,30 +112,30 @@ func (consensus *Consensus) updatePublicKeys(pubKeys, allowlist []bls_cosi.Publi
113112}
114113
115114// Sign on the hash of the message
116- func ( consensus * Consensus ) signMessage (message []byte , priKey * bls_core.SecretKey ) []byte {
115+ func signMessage (message []byte , priKey * bls_core.SecretKey ) []byte {
117116 hash := hash .Keccak256 (message )
118117 signature := priKey .SignHash (hash [:])
119118 return signature .Serialize ()
120119}
121120
122121// Sign on the consensus message signature field.
123- func ( consensus * Consensus ) signConsensusMessage (message * msg_pb.Message ,
122+ func signConsensusMessage (message * msg_pb.Message ,
124123 priKey * bls_core.SecretKey ) error {
125124 message .Signature = nil
126125 marshaledMessage , err := protobuf .Marshal (message )
127126 if err != nil {
128127 return err
129128 }
130129 // 64 byte of signature on previous data
131- signature := consensus . signMessage (marshaledMessage , priKey )
130+ signature := signMessage (marshaledMessage , priKey )
132131 message .Signature = signature
133132 return nil
134133}
135134
136135// UpdateBitmaps update the bitmaps for prepare and commit phase
137136func (consensus * Consensus ) updateBitmaps () {
138137 consensus .getLogger ().Debug ().
139- Str ("MessageType" , consensus .phase .String ()).
138+ Str ("MessageType" , consensus .current . phase .String ()).
140139 Msg ("[UpdateBitmaps] Updating consensus bitmaps" )
141140 members := consensus .decider .Participants ()
142141 prepareBitmap := bls_cosi .NewMask (members )
@@ -199,8 +198,8 @@ func (consensus *Consensus) sendLastSignPower() {
199198func (consensus * Consensus ) resetState () {
200199 consensus .switchPhase ("ResetState" , FBFTAnnounce )
201200
202- consensus .blockHash = [32 ]byte {}
203- consensus .block = []byte {}
201+ consensus .current . blockHash = [32 ]byte {}
202+ consensus .current . block = []byte {}
204203 consensus .decider .ResetPrepareAndCommitVotes ()
205204 if consensus .prepareBitmap != nil {
206205 consensus .prepareBitmap .Clear ()
@@ -295,23 +294,23 @@ func (consensus *Consensus) checkViewID(msg *FBFTMessage) error {
295294
296295// SetBlockNum sets the blockNum in consensus object, called at node bootstrap
297296func (consensus * Consensus ) SetBlockNum (blockNum uint64 ) {
298- atomic . StoreUint64 ( & consensus .blockNum , blockNum )
297+ consensus .setBlockNum ( blockNum )
299298}
300299
301300// SetBlockNum sets the blockNum in consensus object, called at node bootstrap
302301func (consensus * Consensus ) setBlockNum (blockNum uint64 ) {
303- atomic . StoreUint64 ( & consensus .blockNum , blockNum )
302+ consensus .current . setBlockNum ( blockNum )
304303}
305304
306305// ReadSignatureBitmapPayload read the payload for signature and bitmap; offset is the beginning position of reading
307306func (consensus * Consensus ) ReadSignatureBitmapPayload (recvPayload []byte , offset int ) (* bls_core.Sign , * bls_cosi.Mask , error ) {
308307 consensus .mutex .RLock ()
309308 members := consensus .decider .Participants ()
310309 consensus .mutex .RUnlock ()
311- return consensus . readSignatureBitmapPayload (recvPayload , offset , members )
310+ return readSignatureBitmapPayload (recvPayload , offset , members )
312311}
313312
314- func ( consensus * Consensus ) readSignatureBitmapPayload (recvPayload []byte , offset int , members multibls.PublicKeys ) (* bls_core.Sign , * bls_cosi.Mask , error ) {
313+ func readSignatureBitmapPayload (recvPayload []byte , offset int , members multibls.PublicKeys ) (* bls_core.Sign , * bls_cosi.Mask , error ) {
315314 if offset + bls .BLSSignatureSizeInBytes > len (recvPayload ) {
316315 return nil , nil , errors .New ("payload not have enough length" )
317316 }
@@ -596,12 +595,7 @@ func (consensus *Consensus) GetFinality() int64 {
596595
597596// switchPhase will switch FBFTPhase to desired phase.
598597func (consensus * Consensus ) switchPhase (subject string , desired FBFTPhase ) {
599- consensus .getLogger ().Info ().
600- Str ("from:" , consensus .phase .String ()).
601- Str ("to:" , desired .String ()).
602- Str ("switchPhase:" , subject )
603-
604- consensus .phase = desired
598+ consensus .current .switchPhase (subject , desired )
605599}
606600
607601var (
@@ -623,15 +617,15 @@ func (consensus *Consensus) selfCommit(payload []byte) error {
623617 return errGetPreparedBlock
624618 }
625619
626- aggSig , mask , err := consensus . readSignatureBitmapPayload (payload , 32 , consensus .decider .Participants ())
620+ aggSig , mask , err := readSignatureBitmapPayload (payload , 32 , consensus .decider .Participants ())
627621 if err != nil {
628622 return errReadBitmapPayload
629623 }
630624
631625 // Have to keep the block hash so the leader can finish the commit phase of prepared block
632626 consensus .resetState ()
633627
634- copy (consensus .blockHash [:], blockHash [:])
628+ copy (consensus .current . blockHash [:], blockHash [:])
635629 consensus .switchPhase ("selfCommit" , FBFTCommit )
636630 consensus .aggregatedPrepareSig = aggSig
637631 consensus .prepareBitmap = mask
@@ -651,7 +645,7 @@ func (consensus *Consensus) selfCommit(payload []byte) error {
651645 quorum .Commit ,
652646 []* bls_cosi.PublicKeyWrapper {key .Pub },
653647 key .Pri .SignHash (commitPayload ),
654- common .BytesToHash (consensus .blockHash [:]),
648+ common .BytesToHash (consensus .current . blockHash [:]),
655649 block .NumberU64 (),
656650 block .Header ().ViewID ().Uint64 (),
657651 ); err != nil {
@@ -697,9 +691,9 @@ func (consensus *Consensus) GetLogger() *zerolog.Logger {
697691func (consensus * Consensus ) getLogger () * zerolog.Logger {
698692 logger := utils .Logger ().With ().
699693 Uint32 ("shardID" , consensus .ShardID ).
700- Uint64 ("myBlock" , consensus .blockNum ).
701- Uint64 ("myViewID" , consensus .getCurBlockViewID ()).
702- Str ("phase" , consensus .phase .String ()).
694+ Uint64 ("myBlock" , consensus .current . getBlockNum () ).
695+ Uint64 ("myViewID" , consensus .current . getCurBlockViewID ()).
696+ Str ("phase" , consensus .current . phase .String ()).
703697 Str ("mode" , consensus .current .Mode ().String ()).
704698 Logger ()
705699 return & logger
0 commit comments