[consensus] use atomic operation when set blocknum

Signed-off-by: Leo Chen <leo@harmony.one>
pull/3179/head
Leo Chen 5 years ago
parent 6212813c75
commit fec81d1998
  1. 5
      consensus/consensus_service.go
  2. 3
      consensus/consensus_v2.go

@ -2,6 +2,7 @@ package consensus
import ( import (
"math/big" "math/big"
"sync/atomic"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
protobuf "github.com/golang/protobuf/proto" protobuf "github.com/golang/protobuf/proto"
@ -275,9 +276,7 @@ func (consensus *Consensus) checkViewID(msg *FBFTMessage) error {
// SetBlockNum sets the blockNum in consensus object, called at node bootstrap // SetBlockNum sets the blockNum in consensus object, called at node bootstrap
func (consensus *Consensus) SetBlockNum(blockNum uint64) { func (consensus *Consensus) SetBlockNum(blockNum uint64) {
consensus.infoMutex.Lock() atomic.StoreUint64(&consensus.blockNum, blockNum)
defer consensus.infoMutex.Unlock()
consensus.blockNum = blockNum
} }
// ReadSignatureBitmapPayload read the payload for signature and bitmap; offset is the beginning position of reading // ReadSignatureBitmapPayload read the payload for signature and bitmap; offset is the beginning position of reading

@ -3,6 +3,7 @@ package consensus
import ( import (
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"sync/atomic"
"time" "time"
protobuf "github.com/golang/protobuf/proto" protobuf "github.com/golang/protobuf/proto"
@ -283,7 +284,7 @@ func (consensus *Consensus) tryCatchup() {
// TODO(Chao): Explain the reasoning for these code // TODO(Chao): Explain the reasoning for these code
consensus.blockHash = [32]byte{} consensus.blockHash = [32]byte{}
consensus.blockNum = consensus.blockNum + 1 atomic.AddUint64(&consensus.blockNum, 1)
consensus.viewID = committedMsg.ViewID + 1 consensus.viewID = committedMsg.ViewID + 1
consensus.LeaderPubKey = committedMsg.SenderPubkey consensus.LeaderPubKey = committedMsg.SenderPubkey

Loading…
Cancel
Save