aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJimmy Hu <jimmy.hu@dexon.org>2018-10-27 10:13:09 +0800
committerGitHub <noreply@github.com>2018-10-27 10:13:09 +0800
commit47c29f1bc537f2108755f943e190a9f9a58c1ac0 (patch)
tree8f6185f05ead65f2db33313fb750988199c23482
parent8aec64250e6d9dbdc263f11716a7575c3f3af789 (diff)
downloaddexon-consensus-47c29f1bc537f2108755f943e190a9f9a58c1ac0.tar
dexon-consensus-47c29f1bc537f2108755f943e190a9f9a58c1ac0.tar.gz
dexon-consensus-47c29f1bc537f2108755f943e190a9f9a58c1ac0.tar.bz2
dexon-consensus-47c29f1bc537f2108755f943e190a9f9a58c1ac0.tar.lz
dexon-consensus-47c29f1bc537f2108755f943e190a9f9a58c1ac0.tar.xz
dexon-consensus-47c29f1bc537f2108755f943e190a9f9a58c1ac0.tar.zst
dexon-consensus-47c29f1bc537f2108755f943e190a9f9a58c1ac0.zip
core: Remove atomic (#266)
-rw-r--r--core/agreement.go10
-rw-r--r--core/types/position.go15
2 files changed, 11 insertions, 14 deletions
diff --git a/core/agreement.go b/core/agreement.go
index 13f39ce..cab896c 100644
--- a/core/agreement.go
+++ b/core/agreement.go
@@ -21,7 +21,6 @@ import (
"fmt"
"math"
"sync"
- "sync/atomic"
"time"
"github.com/dexon-foundation/dexon-consensus-core/common"
@@ -102,7 +101,7 @@ type agreementData struct {
type agreement struct {
state agreementState
data *agreementData
- aID *atomic.Value
+ aID types.Position
notarySet map[types.NodeID]struct{}
hasOutput bool
lock sync.RWMutex
@@ -126,7 +125,6 @@ func newAgreement(
ID: ID,
leader: leader,
},
- aID: &atomic.Value{},
candidateBlock: make(map[common.Hash]*types.Block),
fastForward: make(chan uint64, 1),
authModule: authModule,
@@ -159,7 +157,7 @@ func (a *agreement) restart(
a.state = newInitialState(a.data)
a.notarySet = notarySet
a.candidateBlock = make(map[common.Hash]*types.Block)
- a.aID.Store(aID)
+ a.aID = *aID.Clone()
}()
expireTime := time.Now().Add(-10 * time.Second)
@@ -219,7 +217,9 @@ func (a *agreement) clocks() int {
// agreementID returns the current agreementID.
func (a *agreement) agreementID() types.Position {
- return a.aID.Load().(types.Position)
+ a.lock.RLock()
+ defer a.lock.RUnlock()
+ return a.aID
}
// nextState is called at the specific clock time.
diff --git a/core/types/position.go b/core/types/position.go
index d821d16..8e7e852 100644
--- a/core/types/position.go
+++ b/core/types/position.go
@@ -18,15 +18,9 @@
package types
import (
- "errors"
"fmt"
)
-// ErrComparePositionOnDifferentChains raised when attempting to
-// compare two positions with different chain ID.
-var ErrComparePositionOnDifferentChains = errors.New(
- "position on different chain")
-
// Position describes the position in the block lattice of an entity.
type Position struct {
ChainID uint32 `json:"chain_id"`
@@ -42,7 +36,8 @@ func (pos *Position) String() string {
// are different.
func (pos *Position) Equal(other *Position) bool {
if pos.ChainID != other.ChainID {
- panic(ErrComparePositionOnDifferentChains)
+ panic(fmt.Errorf("unexpected chainID %d, should be %d",
+ other.ChainID, pos.ChainID))
}
return pos.Round == other.Round && pos.Height == other.Height
}
@@ -51,7 +46,8 @@ func (pos *Position) Equal(other *Position) bool {
// If two blocks on different chain compared by this function, it would panic.
func (pos *Position) Newer(other *Position) bool {
if pos.ChainID != other.ChainID {
- panic(ErrComparePositionOnDifferentChains)
+ panic(fmt.Errorf("unexpected chainID %d, should be %d",
+ other.ChainID, pos.ChainID))
}
return pos.Round > other.Round ||
(pos.Round == other.Round && pos.Height > other.Height)
@@ -61,7 +57,8 @@ func (pos *Position) Newer(other *Position) bool {
// If two blocks on different chain compared by this function, it would panic.
func (pos *Position) Older(other *Position) bool {
if pos.ChainID != other.ChainID {
- panic(ErrComparePositionOnDifferentChains)
+ panic(fmt.Errorf("unexpected chainID %d, should be %d",
+ other.ChainID, pos.ChainID))
}
return pos.Round < other.Round ||
(pos.Round == other.Round && pos.Height < other.Height)