From 14b91441825d6b990527e947c021a5311e951c25 Mon Sep 17 00:00:00 2001 From: Mission Liao Date: Wed, 24 Oct 2018 19:40:52 +0800 Subject: core: fix stuffs (#250) * fix discontinuous finalization height * remove types.Block.Witness.Timestamp field * add field: types.Block.Finalization.ParentHash * fix log format of CRS * switch round and chain in log of types.Position. --- core/compaction-chain.go | 4 ++-- core/compaction-chain_test.go | 14 +++++++++++++ core/consensus.go | 4 ++-- core/crypto.go | 5 ----- core/nonblocking_test.go | 2 +- core/types/block.go | 46 ++++++++++--------------------------------- core/types/block_test.go | 5 ++--- core/types/position.go | 2 +- simulation/app.go | 3 +-- 9 files changed, 33 insertions(+), 52 deletions(-) diff --git a/core/compaction-chain.go b/core/compaction-chain.go index 5b13f7f..451cb13 100644 --- a/core/compaction-chain.go +++ b/core/compaction-chain.go @@ -132,6 +132,7 @@ func (cc *compactionChain) extractBlocks() []*types.Block { cc.pendingBlocks = cc.pendingBlocks[1:] block := cc.pendingBlocks[0] + block.Finalization.ParentHash = prevBlock.Hash block.Finalization.Height = prevBlock.Finalization.Height + 1 deliveringBlocks = append(deliveringBlocks, block) prevBlock = block @@ -204,8 +205,7 @@ func (cc *compactionChain) extractFinalizedBlocks() []*types.Block { continue } // Fork resolution: choose block with smaller hash. - if prevBlock.Finalization.Height == - b.Finalization.Height { + if prevBlock.Finalization.Height == b.Finalization.Height { //TODO(jimmy-dexon): remove this panic after test. if true { // workaround to `go vet` error diff --git a/core/compaction-chain_test.go b/core/compaction-chain_test.go index f4860fb..3e588b3 100644 --- a/core/compaction-chain_test.go +++ b/core/compaction-chain_test.go @@ -166,6 +166,8 @@ func (s *CompactionChainTestSuite) TestExtractBlocks() { if i > 1 { s.Equal(delivered[i-1].Finalization.Height+1, delivered[i].Finalization.Height) + s.Equal(delivered[i-1].Hash, + delivered[i].Finalization.ParentHash) } s.Equal(block.Hash, blocks[i].Hash) } @@ -243,6 +245,9 @@ func (s *CompactionChainTestSuite) TestSyncFinalizedBlock() { }, } now = now.Add(100 * time.Millisecond) + if idx > 0 { + blocks[idx].Finalization.ParentHash = blocks[idx-1].Hash + } } cc.processFinalizedBlock(blocks[1]) cc.processFinalizedBlock(blocks[3]) @@ -330,6 +335,9 @@ func (s *CompactionChainTestSuite) TestSync() { }, } now = now.Add(100 * time.Millisecond) + if idx > 0 { + blocks[idx].Finalization.ParentHash = blocks[idx-1].Hash + } if idx > 10 { blocks[idx].Finalization.Height = 0 } @@ -353,6 +361,7 @@ func (s *CompactionChainTestSuite) TestSync() { confirmed := cc.extractBlocks() s.Require().Len(confirmed, 1) s.Equal(confirmed[0].Hash, blocks[12].Hash) + s.Equal(blocks[11].Hash, blocks[12].Finalization.ParentHash) s.Equal(uint64(13), blocks[12].Finalization.Height) for i := 13; i < 20; i++ { s.Require().NoError(cc.processBlock(blocks[i])) @@ -362,6 +371,7 @@ func (s *CompactionChainTestSuite) TestSync() { offset := 13 for i, b := range confirmed { s.Equal(blocks[offset+i].Hash, b.Hash) + s.Equal(blocks[offset+i-1].Hash, b.Finalization.ParentHash) s.Equal(uint64(offset+i+1), b.Finalization.Height) } } @@ -388,6 +398,9 @@ func (s *CompactionChainTestSuite) TestBootstrapSync() { }, } now = now.Add(100 * time.Millisecond) + if idx > 0 { + blocks[idx].Finalization.ParentHash = blocks[idx-1].Hash + } if idx > 2 { blocks[idx].Finalization.Height = 0 } @@ -411,6 +424,7 @@ func (s *CompactionChainTestSuite) TestBootstrapSync() { confirmed = cc.extractBlocks() s.Require().Len(confirmed, 1) s.Equal(confirmed[0].Hash, blocks[3].Hash) + s.Equal(blocks[2].Hash, blocks[3].Finalization.ParentHash) s.Equal(uint64(4), blocks[3].Finalization.Height) } diff --git a/core/consensus.go b/core/consensus.go index 0385558..a311851 100644 --- a/core/consensus.go +++ b/core/consensus.go @@ -545,7 +545,7 @@ func (con *Consensus) runCRS() { } else { con.logger.Debug("Calling Governance.ProposeCRS", "round", con.round+1, - "crs", crs) + "crs", hex.EncodeToString(crs)) con.gov.ProposeCRS(con.round+1, crs) } } @@ -846,7 +846,7 @@ func (con *Consensus) processBlock(block *types.Block) (err error) { deliveredBlocks = con.ccModule.extractBlocks() for _, b := range deliveredBlocks { if err = con.db.Put(*b); err != nil { - return + panic(err) } // TODO(mission): clone types.FinalizationResult con.app.BlockDelivered(b.Hash, b.Finalization) diff --git a/core/crypto.go b/core/crypto.go index f3870a5..f792c09 100644 --- a/core/crypto.go +++ b/core/crypto.go @@ -26,15 +26,10 @@ import ( ) func hashWitness(witness *types.Witness) (common.Hash, error) { - binaryTimestamp, err := witness.Timestamp.UTC().MarshalBinary() - if err != nil { - return common.Hash{}, err - } binaryHeight := make([]byte, 8) binary.LittleEndian.PutUint64(binaryHeight, witness.Height) return crypto.Keccak256Hash( binaryHeight, - binaryTimestamp, witness.Data), nil } diff --git a/core/nonblocking_test.go b/core/nonblocking_test.go index 2ab40ea..637ec43 100644 --- a/core/nonblocking_test.go +++ b/core/nonblocking_test.go @@ -135,7 +135,7 @@ func (s *NonBlockingTestSuite) TestNonBlocking() { for _, hash := range hashes { nbModule.BlockConfirmed(types.Block{ Hash: hash, - Witness: types.Witness{Timestamp: time.Now().UTC()}, + Witness: types.Witness{}, }) nbModule.StronglyAcked(hash) nbModule.BlockDelivered(hash, types.FinalizationResult{}) diff --git a/core/types/block.go b/core/types/block.go index 29b1c84..e12e0d5 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -77,12 +77,14 @@ func (t *rlpTimestamp) DecodeRLP(s *rlp.Stream) error { // FinalizationResult represents the result of DEXON consensus algorithm. type FinalizationResult struct { - Randomness []byte `json:"randomness"` - Timestamp time.Time `json:"timestamp"` - Height uint64 `json:"height"` + ParentHash common.Hash `json:"parent_hash"` + Randomness []byte `json:"randomness"` + Timestamp time.Time `json:"timestamp"` + Height uint64 `json:"height"` } type rlpFinalizationResult struct { + ParentHash common.Hash Randomness []byte Timestamp *rlpTimestamp Height uint64 @@ -91,6 +93,7 @@ type rlpFinalizationResult struct { // EncodeRLP implements rlp.Encoder func (f *FinalizationResult) EncodeRLP(w io.Writer) error { return rlp.Encode(w, &rlpFinalizationResult{ + ParentHash: f.ParentHash, Randomness: f.Randomness, Timestamp: &rlpTimestamp{f.Timestamp}, Height: f.Height, @@ -103,6 +106,7 @@ func (f *FinalizationResult) DecodeRLP(s *rlp.Stream) error { err := s.Decode(&dec) if err == nil { *f = FinalizationResult{ + ParentHash: dec.ParentHash, Randomness: dec.Randomness, Timestamp: dec.Timestamp.Time, Height: dec.Height, @@ -113,38 +117,8 @@ func (f *FinalizationResult) DecodeRLP(s *rlp.Stream) error { // Witness represents the consensus information on the compaction chain. type Witness struct { - Timestamp time.Time `json:"timestamp"` - Height uint64 `json:"height"` - Data []byte `json:"data"` -} - -type rlpWitness struct { - Timestamp *rlpTimestamp - Height uint64 - Data []byte -} - -// EncodeRLP implements rlp.Encoder -func (w *Witness) EncodeRLP(writer io.Writer) error { - return rlp.Encode(writer, rlpWitness{ - Timestamp: &rlpTimestamp{w.Timestamp}, - Height: w.Height, - Data: w.Data, - }) -} - -// DecodeRLP implements rlp.Decoder -func (w *Witness) DecodeRLP(s *rlp.Stream) error { - var dec rlpWitness - err := s.Decode(&dec) - if err == nil { - *w = Witness{ - Timestamp: dec.Timestamp.Time, - Height: dec.Height, - Data: dec.Data, - } - } - return err + Height uint64 `json:"height"` + Data []byte `json:"data"` } // RecycleBlock put unused block into cache, which might be reused if @@ -245,9 +219,9 @@ func (b *Block) Clone() (bcopy *Block) { bcopy.Position.Height = b.Position.Height bcopy.Signature = b.Signature.Clone() bcopy.CRSSignature = b.CRSSignature.Clone() + bcopy.Finalization.ParentHash = b.Finalization.ParentHash bcopy.Finalization.Timestamp = b.Finalization.Timestamp bcopy.Finalization.Height = b.Finalization.Height - bcopy.Witness.Timestamp = b.Witness.Timestamp bcopy.Witness.Height = b.Witness.Height bcopy.Witness.Data = make([]byte, len(b.Witness.Data)) copy(bcopy.Witness.Data, b.Witness.Data) diff --git a/core/types/block_test.go b/core/types/block_test.go index 758619d..7bf6a75 100644 --- a/core/types/block_test.go +++ b/core/types/block_test.go @@ -55,9 +55,8 @@ func (s *BlockTestSuite) createRandomBlock() *Block { }), Timestamp: time.Now().UTC(), Witness: Witness{ - Height: rand.Uint64(), - Timestamp: time.Now().UTC(), - Data: s.randomBytes(), + Height: rand.Uint64(), + Data: s.randomBytes(), }, Finalization: FinalizationResult{ Timestamp: time.Now().UTC(), diff --git a/core/types/position.go b/core/types/position.go index 51de405..f41be32 100644 --- a/core/types/position.go +++ b/core/types/position.go @@ -35,7 +35,7 @@ type Position struct { } func (pos *Position) String() string { - return fmt.Sprintf("pos[%d:%d:%d]", pos.ChainID, pos.Round, pos.Height) + return fmt.Sprintf("pos[%d:%d:%d]", pos.Round, pos.ChainID, pos.Height) } // Equal checks if two positions are equal, it panics when their chainIDs diff --git a/simulation/app.go b/simulation/app.go index 419bb0a..f074dec 100644 --- a/simulation/app.go +++ b/simulation/app.go @@ -147,8 +147,7 @@ func (a *simApp) BlockDelivered( a.latestWitnessReady.L.Lock() defer a.latestWitnessReady.L.Unlock() a.latestWitness = types.Witness{ - Timestamp: result.Timestamp, - Height: result.Height, + Height: result.Height, } a.latestWitnessReady.Broadcast() }() -- cgit v1.2.3