aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/dexon-foundation/dexon-consensus/core/agreement.go
diff options
context:
space:
mode:
authorJimmy Hu <jimmy.hu@dexon.org>2018-11-05 13:58:18 +0800
committerWei-Ning Huang <w@byzantine-lab.io>2019-06-12 17:27:17 +0800
commit0542a6a1b13c3c1235f68a2d0cca23976c241ca9 (patch)
tree6294dc62ee8c19e09d4592915d9c843472d24c77 /vendor/github.com/dexon-foundation/dexon-consensus/core/agreement.go
parent23fc54de4f886978578207acc15f469abecc46b7 (diff)
downloadgo-tangerine-0542a6a1b13c3c1235f68a2d0cca23976c241ca9.tar
go-tangerine-0542a6a1b13c3c1235f68a2d0cca23976c241ca9.tar.gz
go-tangerine-0542a6a1b13c3c1235f68a2d0cca23976c241ca9.tar.bz2
go-tangerine-0542a6a1b13c3c1235f68a2d0cca23976c241ca9.tar.lz
go-tangerine-0542a6a1b13c3c1235f68a2d0cca23976c241ca9.tar.xz
go-tangerine-0542a6a1b13c3c1235f68a2d0cca23976c241ca9.tar.zst
go-tangerine-0542a6a1b13c3c1235f68a2d0cca23976c241ca9.zip
vendor: sync to latest core
Diffstat (limited to 'vendor/github.com/dexon-foundation/dexon-consensus/core/agreement.go')
-rw-r--r--vendor/github.com/dexon-foundation/dexon-consensus/core/agreement.go45
1 files changed, 29 insertions, 16 deletions
diff --git a/vendor/github.com/dexon-foundation/dexon-consensus/core/agreement.go b/vendor/github.com/dexon-foundation/dexon-consensus/core/agreement.go
index d6875bc45..f31b7efba 100644
--- a/vendor/github.com/dexon-foundation/dexon-consensus/core/agreement.go
+++ b/vendor/github.com/dexon-foundation/dexon-consensus/core/agreement.go
@@ -21,6 +21,7 @@ import (
"fmt"
"math"
"sync"
+ "sync/atomic"
"time"
"github.com/dexon-foundation/dexon-consensus/common"
@@ -103,7 +104,7 @@ type agreementData struct {
type agreement struct {
state agreementState
data *agreementData
- aID types.Position
+ aID *atomic.Value
notarySet map[types.NodeID]struct{}
hasOutput bool
lock sync.RWMutex
@@ -126,6 +127,7 @@ func newAgreement(
ID: ID,
leader: leader,
},
+ aID: &atomic.Value{},
candidateBlock: make(map[common.Hash]*types.Block),
fastForward: make(chan uint64, 1),
authModule: authModule,
@@ -138,9 +140,15 @@ func newAgreement(
func (a *agreement) restart(
notarySet map[types.NodeID]struct{}, aID types.Position, crs common.Hash) {
- func() {
+ if !func() bool {
a.lock.Lock()
defer a.lock.Unlock()
+ if !isStop(aID) {
+ oldAID := a.agreementID()
+ if !isStop(oldAID) && !aID.Newer(&oldAID) {
+ return false
+ }
+ }
a.data.lock.Lock()
defer a.data.lock.Unlock()
a.data.blocksLock.Lock()
@@ -152,14 +160,17 @@ func (a *agreement) restart(
a.data.requiredVote = len(notarySet)/3*2 + 1
a.data.leader.restart(crs)
a.data.lockValue = nullBlockHash
- a.data.lockRound = 1
+ a.data.lockRound = 0
a.fastForward = make(chan uint64, 1)
a.hasOutput = false
a.state = newInitialState(a.data)
a.notarySet = notarySet
a.candidateBlock = make(map[common.Hash]*types.Block)
- a.aID = *aID.Clone()
- }()
+ a.aID.Store(aID)
+ return true
+ }() {
+ return
+ }
if isStop(aID) {
return
@@ -226,14 +237,15 @@ func (a *agreement) clocks() int {
// pullVotes returns if current agreement requires more votes to continue.
func (a *agreement) pullVotes() bool {
- return a.state.state() == statePullVote
+ a.data.lock.RLock()
+ defer a.data.lock.RUnlock()
+ return a.state.state() == statePullVote ||
+ (a.state.state() == statePreCommit && (a.data.period%3) == 0)
}
// agreementID returns the current agreementID.
func (a *agreement) agreementID() types.Position {
- a.lock.RLock()
- defer a.lock.RUnlock()
- return a.aID
+ return a.aID.Load().(types.Position)
}
// nextState is called at the specific clock time.
@@ -288,10 +300,11 @@ func (a *agreement) processVote(vote *types.Vote) error {
if err := a.sanityCheck(vote); err != nil {
return err
}
- if vote.Position != a.aID {
+ aID := a.agreementID()
+ if vote.Position != aID {
// Agreement module has stopped.
- if !isStop(a.aID) {
- if a.aID.Newer(&vote.Position) {
+ if !isStop(aID) {
+ if aID.Newer(&vote.Position) {
return nil
}
}
@@ -335,7 +348,6 @@ func (a *agreement) processVote(vote *types.Vote) error {
vote.BlockHash != a.data.lockValue {
a.data.lockValue = hash
a.data.lockRound = vote.Period
- a.fastForward <- a.data.period + 1
return nil
}
// Condition 2.
@@ -403,10 +415,11 @@ func (a *agreement) processBlock(block *types.Block) error {
a.data.blocksLock.Lock()
defer a.data.blocksLock.Unlock()
- if block.Position != a.aID {
+ aID := a.agreementID()
+ if block.Position != aID {
// Agreement module has stopped.
- if !isStop(a.aID) {
- if a.aID.Newer(&block.Position) {
+ if !isStop(aID) {
+ if aID.Newer(&block.Position) {
return nil
}
}