aboutsummaryrefslogtreecommitdiffstats
path: root/core/types
diff options
context:
space:
mode:
authorJimmy Hu <jimmy.hu@dexon.org>2018-10-25 16:59:30 +0800
committerGitHub <noreply@github.com>2018-10-25 16:59:30 +0800
commit04eeac10e6c690e62ae57ef0e2bdf4618b8782d1 (patch)
treee0b95167d1f42a9304fb9e924378464edbb517e9 /core/types
parent233f1e8de99bf2a0023f05d1c67e48cc770621df (diff)
downloaddexon-consensus-04eeac10e6c690e62ae57ef0e2bdf4618b8782d1.tar
dexon-consensus-04eeac10e6c690e62ae57ef0e2bdf4618b8782d1.tar.gz
dexon-consensus-04eeac10e6c690e62ae57ef0e2bdf4618b8782d1.tar.bz2
dexon-consensus-04eeac10e6c690e62ae57ef0e2bdf4618b8782d1.tar.lz
dexon-consensus-04eeac10e6c690e62ae57ef0e2bdf4618b8782d1.tar.xz
dexon-consensus-04eeac10e6c690e62ae57ef0e2bdf4618b8782d1.tar.zst
dexon-consensus-04eeac10e6c690e62ae57ef0e2bdf4618b8782d1.zip
core: lattice sync (#257)
Diffstat (limited to 'core/types')
-rw-r--r--core/types/position.go10
-rw-r--r--core/types/position_test.go31
2 files changed, 41 insertions, 0 deletions
diff --git a/core/types/position.go b/core/types/position.go
index f41be32..d821d16 100644
--- a/core/types/position.go
+++ b/core/types/position.go
@@ -57,6 +57,16 @@ func (pos *Position) Newer(other *Position) bool {
(pos.Round == other.Round && pos.Height > other.Height)
}
+// Older checks if one block is older than another one on the same chain.
+// 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)
+ }
+ return pos.Round < other.Round ||
+ (pos.Round == other.Round && pos.Height < other.Height)
+}
+
// Clone a position instance.
func (pos *Position) Clone() *Position {
return &Position{
diff --git a/core/types/position_test.go b/core/types/position_test.go
index 48f8dbd..1ef1813 100644
--- a/core/types/position_test.go
+++ b/core/types/position_test.go
@@ -59,6 +59,37 @@ func (s *PositionTestSuite) TestNewer() {
}))
}
+func (s *PositionTestSuite) TestOlder() {
+ pos := Position{
+ Round: 1,
+ ChainID: 1,
+ Height: 1,
+ }
+ s.Panics(func() {
+ pos.Older(&Position{ChainID: 2})
+ })
+ s.False(pos.Older(&Position{
+ Round: 0,
+ ChainID: 1,
+ Height: 0,
+ }))
+ s.False(pos.Older(&Position{
+ Round: 1,
+ ChainID: 1,
+ Height: 0,
+ }))
+ s.True(pos.Older(&Position{
+ Round: 2,
+ ChainID: 1,
+ Height: 0,
+ }))
+ s.True(pos.Older(&Position{
+ Round: 1,
+ ChainID: 1,
+ Height: 100,
+ }))
+}
+
func (s *PositionTestSuite) TestSearchInAsendingOrder() {
positions := []*Position{
&Position{Round: 0, Height: 1},