aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common/types.go24
-rw-r--r--core/lattice-data.go1
-rw-r--r--core/lattice.go9
-rw-r--r--core/lattice_test.go11
4 files changed, 44 insertions, 1 deletions
diff --git a/common/types.go b/common/types.go
index bd15c5b..1bda164 100644
--- a/common/types.go
+++ b/common/types.go
@@ -1,6 +1,23 @@
// Copyright 2018 The dexon-consensus-core Authors
// This file is part of the dexon-consensus-core library.
//
+// The dexon-consensus-core library is free software: you can redistribute it
+// and/or modify it under the terms of the GNU Lesser General Public License as
+// published by the Free Software Foundation, either version 3 of the License,
+// or (at your option) any later version.
+//
+// The dexon-consensus-core library is distributed in the hope that it will be
+// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
+// General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the dexon-consensus-core library. If not, see
+// <http://www.gnu.org/licenses/>.
+
+// Copyright 2018 The dexon-consensus-core Authors
+// This file is part of the dexon-consensus-core library.
+//
// The dexon-consensus-core library is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the License,
@@ -41,6 +58,11 @@ func (h Hash) Equal(hp Hash) bool {
return h == hp
}
+// Less compares if current hash is lesser.
+func (h Hash) Less(hp Hash) bool {
+ return bytes.Compare(h[:], hp[:]) < 0
+}
+
// MarshalText implements the encoding.TextMarhsaler interface.
func (h Hash) MarshalText() ([]byte, error) {
result := make([]byte, hex.EncodedLen(HashLength))
@@ -58,7 +80,7 @@ func (h *Hash) UnmarshalText(text []byte) error {
type Hashes []Hash
func (hs Hashes) Len() int { return len(hs) }
-func (hs Hashes) Less(i, j int) bool { return bytes.Compare(hs[i][:], hs[j][:]) < 0 }
+func (hs Hashes) Less(i, j int) bool { return hs[i].Less(hs[j]) }
func (hs Hashes) Swap(i, j int) { hs[i], hs[j] = hs[j], hs[i] }
// SortedHashes is a slice of hashes sorted in ascending order.
diff --git a/core/lattice-data.go b/core/lattice-data.go
index e5d173d..7eec7f2 100644
--- a/core/lattice-data.go
+++ b/core/lattice-data.go
@@ -40,6 +40,7 @@ var (
ErrForkBlock = fmt.Errorf("fork block")
ErrNotAckParent = fmt.Errorf("not ack parent")
ErrDoubleAck = fmt.Errorf("double ack")
+ ErrAcksNotSorted = fmt.Errorf("acks not sorted")
ErrInvalidBlockHeight = fmt.Errorf("invalid block height")
ErrAlreadyInLattice = fmt.Errorf("block already in lattice")
ErrIncorrectBlockTime = fmt.Errorf("block timestampe is incorrect")
diff --git a/core/lattice.go b/core/lattice.go
index 530fbab..27d38d8 100644
--- a/core/lattice.go
+++ b/core/lattice.go
@@ -102,6 +102,15 @@ func (s *Lattice) SanityCheck(b *types.Block) (err error) {
err = ErrIncorrectHash
return
}
+ for i := range b.Acks {
+ if i == 0 {
+ continue
+ }
+ if !b.Acks[i-1].Less(b.Acks[i]) {
+ err = ErrAcksNotSorted
+ return
+ }
+ }
// Check the signer.
pubKey, err := crypto.SigToPub(b.Hash, b.Signature)
if err != nil {
diff --git a/core/lattice_test.go b/core/lattice_test.go
index 2115d1a..bf65684 100644
--- a/core/lattice_test.go
+++ b/core/lattice_test.go
@@ -220,6 +220,17 @@ func (s *LatticeTestSuite) TestSanityCheck() {
b.Signature, err = auth.prvKey.Sign(common.NewRandomHash())
req.NoError(err)
req.Equal(lattice.SanityCheck(b), ErrIncorrectSignature)
+ // A block with un-sorted acks should not pass sanity check.
+ b.Acks = common.NewSortedHashes(common.Hashes{
+ common.NewRandomHash(),
+ common.NewRandomHash(),
+ common.NewRandomHash(),
+ common.NewRandomHash(),
+ common.NewRandomHash(),
+ })
+ b.Acks[0], b.Acks[1] = b.Acks[1], b.Acks[0]
+ req.NoError(auth.SignBlock(b))
+ req.Equal(lattice.SanityCheck(b), ErrAcksNotSorted)
// A block with incorrect hash should not pass sanity check.
b.Hash = common.NewRandomHash()
req.Equal(lattice.SanityCheck(b), ErrIncorrectHash)