diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2018-09-03 10:01:40 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-03 10:01:40 +0800 |
commit | c8d3092208f73ee991a123052a71b7dbf7fffc27 (patch) | |
tree | fdcb3b90b859531e042dbbdd6bcd7b0cca40e86a /core/types | |
parent | 9491ddae81640e04f8aed38e2cddeb64cc62d55b (diff) | |
download | dexon-consensus-c8d3092208f73ee991a123052a71b7dbf7fffc27.tar dexon-consensus-c8d3092208f73ee991a123052a71b7dbf7fffc27.tar.gz dexon-consensus-c8d3092208f73ee991a123052a71b7dbf7fffc27.tar.bz2 dexon-consensus-c8d3092208f73ee991a123052a71b7dbf7fffc27.tar.lz dexon-consensus-c8d3092208f73ee991a123052a71b7dbf7fffc27.tar.xz dexon-consensus-c8d3092208f73ee991a123052a71b7dbf7fffc27.tar.zst dexon-consensus-c8d3092208f73ee991a123052a71b7dbf7fffc27.zip |
core: Add a new structure `Position` and move `ShardID`, `ChainID` and `Height` from `Block` (#89)
Diffstat (limited to 'core/types')
-rw-r--r-- | core/types/block.go | 14 | ||||
-rw-r--r-- | core/types/block_test.go | 20 | ||||
-rw-r--r-- | core/types/position.go | 25 | ||||
-rw-r--r-- | core/types/vote.go | 18 |
4 files changed, 56 insertions, 21 deletions
diff --git a/core/types/block.go b/core/types/block.go index f13d868..ab2ef84 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -65,9 +65,7 @@ type Block struct { ProposerID ValidatorID `json:"proposer_id"` ParentHash common.Hash `json:"parent_hash"` Hash common.Hash `json:"hash"` - ShardID uint64 `json:"shard_id"` - ChainID uint64 `json:"chain_id"` - Height uint64 `json:"height"` + Position Position `json:"position"` Timestamps map[ValidatorID]time.Time `json:"timestamps"` Acks map[common.Hash]struct{} `json:"acks"` Payloads [][]byte `json:"payloads"` @@ -88,9 +86,9 @@ func (b *Block) Clone() (bcopy *Block) { bcopy.ProposerID = b.ProposerID bcopy.ParentHash = b.ParentHash bcopy.Hash = b.Hash - bcopy.ShardID = b.ShardID - bcopy.ChainID = b.ChainID - bcopy.Height = b.Height + bcopy.Position.ShardID = b.Position.ShardID + bcopy.Position.ChainID = b.Position.ChainID + bcopy.Position.Height = b.Position.Height bcopy.Signature = b.Signature.Clone() bcopy.CRSSignature = b.CRSSignature.Clone() bcopy.Notary.Timestamp = b.Notary.Timestamp @@ -118,7 +116,7 @@ func (b *Block) Clone() (bcopy *Block) { // IsGenesis checks if the block is a genesisBlock func (b *Block) IsGenesis() bool { - return b.Height == 0 && b.ParentHash == common.Hash{} + return b.Position.Height == 0 && b.ParentHash == common.Hash{} } // ByHash is the helper type for sorting slice of blocks by hash. @@ -144,7 +142,7 @@ func (b ByHeight) Len() int { } func (b ByHeight) Less(i int, j int) bool { - return b[i].Height < b[j].Height + return b[i].Position.Height < b[j].Position.Height } func (b ByHeight) Swap(i int, j int) { diff --git a/core/types/block_test.go b/core/types/block_test.go index 2087373..4763ad1 100644 --- a/core/types/block_test.go +++ b/core/types/block_test.go @@ -49,10 +49,10 @@ func (s *BlockTestSuite) TestSortByHash() { } func (s *BlockTestSuite) TestSortByHeight() { - b0 := &Block{Height: 0} - b1 := &Block{Height: 1} - b2 := &Block{Height: 2} - b3 := &Block{Height: 3} + b0 := &Block{Position: Position{Height: 0}} + b1 := &Block{Position: Position{Height: 1}} + b2 := &Block{Position: Position{Height: 2}} + b3 := &Block{Position: Position{Height: 3}} blocks := []*Block{b3, b2, b1, b0} sort.Sort(ByHeight(blocks)) @@ -64,17 +64,23 @@ func (s *BlockTestSuite) TestSortByHeight() { func (s *BlockTestSuite) TestGenesisBlock() { b0 := &Block{ - Height: 0, + Position: Position{ + Height: 0, + }, ParentHash: common.Hash{}, } s.True(b0.IsGenesis()) b1 := &Block{ - Height: 1, + Position: Position{ + Height: 1, + }, ParentHash: common.Hash{}, } s.False(b1.IsGenesis()) b2 := &Block{ - Height: 0, + Position: Position{ + Height: 0, + }, ParentHash: common.NewRandomHash(), } s.False(b2.IsGenesis()) diff --git a/core/types/position.go b/core/types/position.go new file mode 100644 index 0000000..a894c4b --- /dev/null +++ b/core/types/position.go @@ -0,0 +1,25 @@ +// 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/>. + +package types + +// Position describes the position in the block lattice of an entity. +type Position struct { + ShardID uint32 `json:"shard_id"` + ChainID uint32 `json:"chain_id"` + Height uint64 `json:"height"` +} diff --git a/core/types/vote.go b/core/types/vote.go index 1247f84..158c209 100644 --- a/core/types/vote.go +++ b/core/types/vote.go @@ -38,11 +38,12 @@ const ( // Vote is the vote structure defined in Crypto Shuffle Algorithm. type Vote struct { - ProposerID ValidatorID - Type VoteType - BlockHash common.Hash - Period uint64 - Signature crypto.Signature + ProposerID ValidatorID `json:"proposer_id"` + Type VoteType `json:"type"` + BlockHash common.Hash `json:"block_hash"` + Period uint64 `json:"period"` + Position Position `json:"position"` + Signature crypto.Signature `json:"signature"` } func (v *Vote) String() string { @@ -57,6 +58,11 @@ func (v *Vote) Clone() *Vote { Type: v.Type, BlockHash: v.BlockHash, Period: v.Period, - Signature: v.Signature.Clone(), + Position: Position{ + ShardID: v.Position.ShardID, + ChainID: v.Position.ChainID, + Height: v.Position.Height, + }, + Signature: v.Signature.Clone(), } } |