aboutsummaryrefslogtreecommitdiffstats
path: root/core/types/position.go
diff options
context:
space:
mode:
authorMission Liao <mission.liao@dexon.org>2018-10-12 18:59:05 +0800
committerGitHub <noreply@github.com>2018-10-12 18:59:05 +0800
commit48f5fdb27e3218e2476b27ae99bcf242533b3bc3 (patch)
tree9926478b8dc6129d67a7da2d6fdfde84b96420c6 /core/types/position.go
parent490fa1e9ce2b661e4c8b612bd53f20123346353b (diff)
downloadtangerine-consensus-48f5fdb27e3218e2476b27ae99bcf242533b3bc3.tar
tangerine-consensus-48f5fdb27e3218e2476b27ae99bcf242533b3bc3.tar.gz
tangerine-consensus-48f5fdb27e3218e2476b27ae99bcf242533b3bc3.tar.bz2
tangerine-consensus-48f5fdb27e3218e2476b27ae99bcf242533b3bc3.tar.lz
tangerine-consensus-48f5fdb27e3218e2476b27ae99bcf242533b3bc3.tar.xz
tangerine-consensus-48f5fdb27e3218e2476b27ae99bcf242533b3bc3.tar.zst
tangerine-consensus-48f5fdb27e3218e2476b27ae99bcf242533b3bc3.zip
core: latticeData supports config change (#190)
* Add test for num of chains changes. * Return error in latticeData.prepareBlock * Compare two positions * Modify chainStatus from height-based to index-based. * Fix consensus to use round variable * Remove sanity check in chainStatus * Fixup: refine sanity check - verify if round switching is required or not by chainTip's config. - make the logic in sanity check more clear - pospone acking relationship checking, they are more expensive to check.
Diffstat (limited to 'core/types/position.go')
-rw-r--r--core/types/position.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/core/types/position.go b/core/types/position.go
index af57b29..4e254ba 100644
--- a/core/types/position.go
+++ b/core/types/position.go
@@ -17,9 +17,44 @@
package types
+import "errors"
+
+// 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"`
Round uint64 `json:"round"`
Height uint64 `json:"height"`
}
+
+// Equal checks if two positions are equal, it panics when their chainIDs
+// are different.
+func (pos *Position) Equal(other *Position) bool {
+ if pos.ChainID != other.ChainID {
+ panic(ErrComparePositionOnDifferentChains)
+ }
+ return pos.Round == other.Round && pos.Height == other.Height
+}
+
+// Newer checks if one block is newer than another one on the same chain.
+// 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)
+ }
+ return pos.Round > other.Round ||
+ (pos.Round == other.Round && pos.Height > other.Height)
+}
+
+// Clone a position instance.
+func (pos *Position) Clone() *Position {
+ return &Position{
+ ChainID: pos.ChainID,
+ Round: pos.Round,
+ Height: pos.Height,
+ }
+}