|
|
@ -2,7 +2,6 @@ package consensus |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"math/big" |
|
|
|
"math/big" |
|
|
|
"sync" |
|
|
|
|
|
|
|
"time" |
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/harmony-one/harmony/internal/chain" |
|
|
|
"github.com/harmony-one/harmony/internal/chain" |
|
|
@ -25,26 +24,21 @@ const MaxViewIDDiff = 249 |
|
|
|
|
|
|
|
|
|
|
|
// State contains current mode and current viewID
|
|
|
|
// State contains current mode and current viewID
|
|
|
|
type State struct { |
|
|
|
type State struct { |
|
|
|
mode Mode |
|
|
|
mode Mode |
|
|
|
modeMux sync.RWMutex |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// current view id in normal mode
|
|
|
|
// current view id in normal mode
|
|
|
|
// it changes per successful consensus
|
|
|
|
// it changes per successful consensus
|
|
|
|
blockViewID uint64 |
|
|
|
blockViewID uint64 |
|
|
|
cViewMux sync.RWMutex |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// view changing id is used during view change mode
|
|
|
|
// view changing id is used during view change mode
|
|
|
|
// it is the next view id
|
|
|
|
// it is the next view id
|
|
|
|
viewChangingID uint64 |
|
|
|
viewChangingID uint64 |
|
|
|
|
|
|
|
|
|
|
|
viewMux sync.RWMutex |
|
|
|
|
|
|
|
isBackup bool |
|
|
|
isBackup bool |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Mode return the current node mode
|
|
|
|
// Mode return the current node mode
|
|
|
|
func (pm *State) Mode() Mode { |
|
|
|
func (pm *State) Mode() Mode { |
|
|
|
pm.modeMux.RLock() |
|
|
|
|
|
|
|
defer pm.modeMux.RUnlock() |
|
|
|
|
|
|
|
return pm.mode |
|
|
|
return pm.mode |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -54,22 +48,16 @@ func (pm *State) SetMode(s Mode) { |
|
|
|
s = NormalBackup |
|
|
|
s = NormalBackup |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pm.modeMux.Lock() |
|
|
|
|
|
|
|
defer pm.modeMux.Unlock() |
|
|
|
|
|
|
|
pm.mode = s |
|
|
|
pm.mode = s |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GetCurBlockViewID return the current view id
|
|
|
|
// GetCurBlockViewID return the current view id
|
|
|
|
func (pm *State) GetCurBlockViewID() uint64 { |
|
|
|
func (pm *State) GetCurBlockViewID() uint64 { |
|
|
|
pm.cViewMux.RLock() |
|
|
|
|
|
|
|
defer pm.cViewMux.RUnlock() |
|
|
|
|
|
|
|
return pm.blockViewID |
|
|
|
return pm.blockViewID |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// SetCurBlockViewID sets the current view id
|
|
|
|
// SetCurBlockViewID sets the current view id
|
|
|
|
func (pm *State) SetCurBlockViewID(viewID uint64) uint64 { |
|
|
|
func (pm *State) SetCurBlockViewID(viewID uint64) uint64 { |
|
|
|
pm.cViewMux.Lock() |
|
|
|
|
|
|
|
defer pm.cViewMux.Unlock() |
|
|
|
|
|
|
|
pm.blockViewID = viewID |
|
|
|
pm.blockViewID = viewID |
|
|
|
return pm.blockViewID |
|
|
|
return pm.blockViewID |
|
|
|
} |
|
|
|
} |
|
|
@ -77,26 +65,18 @@ func (pm *State) SetCurBlockViewID(viewID uint64) uint64 { |
|
|
|
// GetViewChangingID return the current view changing id
|
|
|
|
// GetViewChangingID return the current view changing id
|
|
|
|
// It is meaningful during view change mode
|
|
|
|
// It is meaningful during view change mode
|
|
|
|
func (pm *State) GetViewChangingID() uint64 { |
|
|
|
func (pm *State) GetViewChangingID() uint64 { |
|
|
|
pm.viewMux.RLock() |
|
|
|
|
|
|
|
defer pm.viewMux.RUnlock() |
|
|
|
|
|
|
|
return pm.viewChangingID |
|
|
|
return pm.viewChangingID |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// SetViewChangingID set the current view changing id
|
|
|
|
// SetViewChangingID set the current view changing id
|
|
|
|
// It is meaningful during view change mode
|
|
|
|
// It is meaningful during view change mode
|
|
|
|
func (pm *State) SetViewChangingID(id uint64) { |
|
|
|
func (pm *State) SetViewChangingID(id uint64) { |
|
|
|
pm.viewMux.Lock() |
|
|
|
|
|
|
|
defer pm.viewMux.Unlock() |
|
|
|
|
|
|
|
pm.viewChangingID = id |
|
|
|
pm.viewChangingID = id |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GetViewChangeDuraion return the duration of the current view change
|
|
|
|
// GetViewChangeDuraion return the duration of the current view change
|
|
|
|
// It increase in the power of difference betweeen view changing ID and current view ID
|
|
|
|
// It increase in the power of difference betweeen view changing ID and current view ID
|
|
|
|
func (pm *State) GetViewChangeDuraion() time.Duration { |
|
|
|
func (pm *State) GetViewChangeDuraion() time.Duration { |
|
|
|
pm.viewMux.RLock() |
|
|
|
|
|
|
|
pm.cViewMux.RLock() |
|
|
|
|
|
|
|
defer pm.viewMux.RUnlock() |
|
|
|
|
|
|
|
defer pm.cViewMux.RUnlock() |
|
|
|
|
|
|
|
diff := int64(pm.viewChangingID - pm.blockViewID) |
|
|
|
diff := int64(pm.viewChangingID - pm.blockViewID) |
|
|
|
return time.Duration(diff * diff * int64(viewChangeDuration)) |
|
|
|
return time.Duration(diff * diff * int64(viewChangeDuration)) |
|
|
|
} |
|
|
|
} |
|
|
|