diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/compaction-chain.go | 7 | ||||
-rw-r--r-- | core/compaction-chain_test.go | 12 | ||||
-rw-r--r-- | core/consensus-timestamp.go | 4 | ||||
-rw-r--r-- | core/consensus-timestamp_test.go | 2 | ||||
-rw-r--r-- | core/consensus.go | 9 | ||||
-rw-r--r-- | core/consensus_test.go | 2 | ||||
-rw-r--r-- | core/crypto_test.go | 16 | ||||
-rw-r--r-- | core/interfaces.go | 4 | ||||
-rw-r--r-- | core/lattice.go | 2 | ||||
-rw-r--r-- | core/lattice_test.go | 2 | ||||
-rw-r--r-- | core/nonblocking.go | 21 | ||||
-rw-r--r-- | core/nonblocking_test.go | 15 | ||||
-rw-r--r-- | core/test/app.go | 11 | ||||
-rw-r--r-- | core/test/app_test.go | 5 | ||||
-rw-r--r-- | core/test/stopper_test.go | 6 | ||||
-rw-r--r-- | core/types/block.go | 37 | ||||
-rw-r--r-- | core/types/block_test.go | 14 |
17 files changed, 94 insertions, 75 deletions
diff --git a/core/compaction-chain.go b/core/compaction-chain.go index 6e6ce75..c176f65 100644 --- a/core/compaction-chain.go +++ b/core/compaction-chain.go @@ -64,7 +64,7 @@ func (cc *compactionChain) blockRegistered(hash common.Hash) (exist bool) { func (cc *compactionChain) processBlock(block *types.Block) error { prevBlock := cc.lastBlock() if prevBlock != nil { - block.ConsensusHeight = prevBlock.ConsensusHeight + 1 + block.Finalization.Height = prevBlock.Finalization.Height + 1 } cc.prevBlockLock.Lock() defer cc.prevBlockLock.Unlock() @@ -79,7 +79,8 @@ func (cc *compactionChain) extractBlocks() []*types.Block { deliveringBlocks := make([]*types.Block, 0) cc.blocksLock.Lock() defer cc.blocksLock.Unlock() - for len(cc.pendingBlocks) != 0 && len(cc.pendingBlocks[0].Randomness) != 0 { + for len(cc.pendingBlocks) != 0 && + len(cc.pendingBlocks[0].Finalization.Randomness) != 0 { var block *types.Block block, cc.pendingBlocks = cc.pendingBlocks[0], cc.pendingBlocks[1:] delete(cc.blocks, block.Hash) @@ -95,7 +96,7 @@ func (cc *compactionChain) processBlockRandomnessResult( } cc.blocksLock.Lock() defer cc.blocksLock.Unlock() - cc.blocks[rand.BlockHash].Randomness = rand.Randomness + cc.blocks[rand.BlockHash].Finalization.Randomness = rand.Randomness return nil } diff --git a/core/compaction-chain_test.go b/core/compaction-chain_test.go index 5ceb5c2..3366d5f 100644 --- a/core/compaction-chain_test.go +++ b/core/compaction-chain_test.go @@ -43,8 +43,10 @@ func (s *CompactionChainTestSuite) generateBlocks( blocks := make([]*types.Block, size) for idx := range blocks { blocks[idx] = &types.Block{ - Hash: common.NewRandomHash(), - ConsensusTimestamp: now, + Hash: common.NewRandomHash(), + Finalization: types.FinalizationResult{ + Timestamp: now, + }, } now = now.Add(100 * time.Millisecond) } @@ -61,8 +63,10 @@ func (s *CompactionChainTestSuite) TestProcessBlock() { blocks := make([]*types.Block, 10) for idx := range blocks { blocks[idx] = &types.Block{ - Hash: common.NewRandomHash(), - ConsensusTimestamp: now, + Hash: common.NewRandomHash(), + Finalization: types.FinalizationResult{ + Timestamp: now, + }, } now = now.Add(100 * time.Millisecond) } diff --git a/core/consensus-timestamp.go b/core/consensus-timestamp.go index 62298d9..b21b3ed 100644 --- a/core/consensus-timestamp.go +++ b/core/consensus-timestamp.go @@ -63,8 +63,8 @@ func (ct *consensusTimestamp) appendConfig( func (ct *consensusTimestamp) processBlocks(blocks []*types.Block) (err error) { for _, block := range blocks { if !block.IsGenesis() { - block.ConsensusTimestamp, err = getMedianTime(ct.chainTimestamps) - if err != nil { + if block.Finalization.Timestamp, err = + getMedianTime(ct.chainTimestamps); err != nil { return } } diff --git a/core/consensus-timestamp_test.go b/core/consensus-timestamp_test.go index 615142c..7128f16 100644 --- a/core/consensus-timestamp_test.go +++ b/core/consensus-timestamp_test.go @@ -82,7 +82,7 @@ func (s *ConsensusTimestampTest) extractTimestamps( if block.IsGenesis() { continue } - timestamps = append(timestamps, block.ConsensusTimestamp) + timestamps = append(timestamps, block.Finalization.Timestamp) } return timestamps } diff --git a/core/consensus.go b/core/consensus.go index 4b1b4d3..dcc6f38 100644 --- a/core/consensus.go +++ b/core/consensus.go @@ -673,18 +673,15 @@ func (con *Consensus) processBlock(block *types.Block) (err error) { if err = con.ccModule.processBlock(b); err != nil { return } - go con.event.NotifyTime(b.ConsensusTimestamp) + go con.event.NotifyTime(b.Finalization.Timestamp) } deliveredBlocks = con.ccModule.extractBlocks() for _, b := range deliveredBlocks { if err = con.db.Put(*b); err != nil { return } - con.nbModule.BlockDelivered(*b) - // TODO(mission): Find a way to safely recycle the block. - // We should deliver block directly to - // nonBlocking and let them recycle the - // block. + // TODO(mission): clone types.FinalizationResult + con.nbModule.BlockDelivered(b.Hash, b.Finalization) } if err = con.lattice.PurgeBlocks(deliveredBlocks); err != nil { return diff --git a/core/consensus_test.go b/core/consensus_test.go index 6163e6a..1b03321 100644 --- a/core/consensus_test.go +++ b/core/consensus_test.go @@ -195,7 +195,7 @@ func (s *ConsensusTestSuite) TestSimpleDeliverBlock() { broadcast := func(b *types.Block) { for _, obj := range objs { h := common.NewRandomHash() - b.Randomness = h[:] + b.Finalization.Randomness = h[:] obj.con.ccModule.registerBlock(b) req.Nil(obj.con.processBlock(b)) } diff --git a/core/crypto_test.go b/core/crypto_test.go index bbedeaf..b0161d9 100644 --- a/core/crypto_test.go +++ b/core/crypto_test.go @@ -40,10 +40,12 @@ func (s *CryptoTestSuite) prepareBlock(prevBlock *types.Block) *types.Block { now := time.Now().UTC() if prevBlock == nil { return &types.Block{ - Acks: common.NewSortedHashes(acks), - Timestamp: now, - ConsensusTimestamp: time.Now(), - ConsensusHeight: 0, + Acks: common.NewSortedHashes(acks), + Timestamp: now, + Finalization: types.FinalizationResult{ + Timestamp: time.Now(), + Height: 0, + }, } } s.Require().NotEqual(prevBlock.Hash, common.Hash{}) @@ -54,8 +56,10 @@ func (s *CryptoTestSuite) prepareBlock(prevBlock *types.Block) *types.Block { Position: types.Position{ Height: prevBlock.Position.Height + 1, }, - ConsensusTimestamp: time.Now(), - ConsensusHeight: prevBlock.ConsensusHeight + 1, + Finalization: types.FinalizationResult{ + Timestamp: time.Now(), + Height: prevBlock.Finalization.Height + 1, + }, } } diff --git a/core/interfaces.go b/core/interfaces.go index 98cbb4d..7a5859e 100644 --- a/core/interfaces.go +++ b/core/interfaces.go @@ -38,10 +38,10 @@ type Application interface { VerifyBlock(block *types.Block) bool // BlockConfirmed is called when a block is confirmed and added to lattice. - BlockConfirmed(blockHash common.Hash) + BlockConfirmed(block types.Block) // BlockDelivered is called when a block is add to the compaction chain. - BlockDelivered(block types.Block) + BlockDelivered(blockHash common.Hash, result types.FinalizationResult) } // Debug describes the application interface that requires diff --git a/core/lattice.go b/core/lattice.go index 6ecb88f..18e7ae6 100644 --- a/core/lattice.go +++ b/core/lattice.go @@ -154,7 +154,7 @@ func (s *Lattice) ProcessBlock( if s.debug != nil { s.debug.StronglyAcked(input.Hash) } - s.app.BlockConfirmed(input.Hash) + s.app.BlockConfirmed(*input.Clone()) // Purge blocks in pool with the same chainID and lower height. s.pool.purgeBlocks(input.Position.ChainID, input.Position.Height) // Replay tips in pool to check their validity. diff --git a/core/lattice_test.go b/core/lattice_test.go index 11caa11..3ba5ed5 100644 --- a/core/lattice_test.go +++ b/core/lattice_test.go @@ -78,7 +78,7 @@ func (mgr *testLatticeMgr) processBlock(b *types.Block) (err error) { if err = mgr.db.Put(*b); err != nil { return } - mgr.app.BlockDelivered(*b) + mgr.app.BlockDelivered(b.Hash, b.Finalization) } if err = mgr.lattice.PurgeBlocks(delivered); err != nil { return diff --git a/core/nonblocking.go b/core/nonblocking.go index 83c2351..9bedb4b 100644 --- a/core/nonblocking.go +++ b/core/nonblocking.go @@ -26,7 +26,7 @@ import ( ) type blockConfirmedEvent struct { - blockHash common.Hash + block *types.Block } type stronglyAckedEvent struct { @@ -39,7 +39,8 @@ type totalOrderingDeliveredEvent struct { } type blockDeliveredEvent struct { - block *types.Block + blockHash common.Hash + result *types.FinalizationResult } // nonBlocking implements these interfaces and is a decorator for @@ -94,11 +95,11 @@ func (nb *nonBlocking) run() { case stronglyAckedEvent: nb.debug.StronglyAcked(e.blockHash) case blockConfirmedEvent: - nb.app.BlockConfirmed(e.blockHash) + nb.app.BlockConfirmed(*e.block) case totalOrderingDeliveredEvent: nb.debug.TotalOrderingDelivered(e.blockHashes, e.early) case blockDeliveredEvent: - nb.app.BlockDelivered(*e.block) + nb.app.BlockDelivered(e.blockHash, *e.result) default: fmt.Printf("Unknown event %v.", e) } @@ -133,9 +134,9 @@ func (nb *nonBlocking) VerifyBlock(block *types.Block) bool { } // BlockConfirmed is called when a block is confirmed and added to lattice. -func (nb *nonBlocking) BlockConfirmed(blockHash common.Hash) { +func (nb *nonBlocking) BlockConfirmed(block types.Block) { if nb.debug != nil { - nb.addEvent(blockConfirmedEvent{blockHash}) + nb.addEvent(blockConfirmedEvent{&block}) } } @@ -156,6 +157,10 @@ func (nb *nonBlocking) TotalOrderingDelivered( } // BlockDelivered is called when a block is add to the compaction chain. -func (nb *nonBlocking) BlockDelivered(block types.Block) { - nb.addEvent(blockDeliveredEvent{&block}) +func (nb *nonBlocking) BlockDelivered( + blockHash common.Hash, result types.FinalizationResult) { + nb.addEvent(blockDeliveredEvent{ + blockHash: blockHash, + result: &result, + }) } diff --git a/core/nonblocking_test.go b/core/nonblocking_test.go index 7599456..8cd42d0 100644 --- a/core/nonblocking_test.go +++ b/core/nonblocking_test.go @@ -57,9 +57,9 @@ func (app *slowApp) VerifyBlock(_ *types.Block) bool { return true } -func (app *slowApp) BlockConfirmed(blockHash common.Hash) { +func (app *slowApp) BlockConfirmed(block types.Block) { time.Sleep(app.sleep) - app.blockConfirmed[blockHash] = struct{}{} + app.blockConfirmed[block.Hash] = struct{}{} } func (app *slowApp) StronglyAcked(blockHash common.Hash) { @@ -74,9 +74,10 @@ func (app *slowApp) TotalOrderingDelivered(blockHashes common.Hashes, early bool } } -func (app *slowApp) BlockDelivered(block types.Block) { +func (app *slowApp) BlockDelivered( + blockHash common.Hash, _ types.FinalizationResult) { time.Sleep(app.sleep) - app.blockDelivered[block.Hash] = struct{}{} + app.blockDelivered[blockHash] = struct{}{} } type NonBlockingTestSuite struct { @@ -96,12 +97,12 @@ func (s *NonBlockingTestSuite) TestNonBlocking() { // Start doing some 'heavy' job. for _, hash := range hashes { - nbModule.BlockConfirmed(hash) - nbModule.StronglyAcked(hash) - nbModule.BlockDelivered(types.Block{ + nbModule.BlockConfirmed(types.Block{ Hash: hash, Witness: types.Witness{Timestamp: time.Now().UTC()}, }) + nbModule.StronglyAcked(hash) + nbModule.BlockDelivered(hash, types.FinalizationResult{}) } nbModule.TotalOrderingDelivered(hashes, true) diff --git a/core/test/app.go b/core/test/app.go index bf14d1b..60bc26f 100644 --- a/core/test/app.go +++ b/core/test/app.go @@ -114,7 +114,7 @@ func (app *App) VerifyBlock(block *types.Block) bool { } // BlockConfirmed implements Application interface. -func (app *App) BlockConfirmed(_ common.Hash) { +func (app *App) BlockConfirmed(_ types.Block) { } // StronglyAcked implements Application interface. @@ -145,15 +145,16 @@ func (app *App) TotalOrderingDelivered(blockHashes common.Hashes, early bool) { } // BlockDelivered implements Application interface. -func (app *App) BlockDelivered(block types.Block) { +func (app *App) BlockDelivered( + blockHash common.Hash, result types.FinalizationResult) { app.deliveredLock.Lock() defer app.deliveredLock.Unlock() - app.Delivered[block.Hash] = &AppDeliveredRecord{ - ConsensusTime: block.ConsensusTimestamp, + app.Delivered[blockHash] = &AppDeliveredRecord{ + ConsensusTime: result.Timestamp, When: time.Now().UTC(), } - app.DeliverSequence = append(app.DeliverSequence, block.Hash) + app.DeliverSequence = append(app.DeliverSequence, blockHash) } // Compare performs these checks against another App instance diff --git a/core/test/app_test.go b/core/test/app_test.go index 21d4e24..a70fc82 100644 --- a/core/test/app_test.go +++ b/core/test/app_test.go @@ -80,9 +80,8 @@ func (s *AppTestSuite) deliverBlockWithTimeFromSequenceLength( func (s *AppTestSuite) deliverBlock( app *App, hash common.Hash, timestamp time.Time) { - app.BlockDelivered(types.Block{ - Hash: hash, - ConsensusTimestamp: timestamp, + app.BlockDelivered(hash, types.FinalizationResult{ + Timestamp: timestamp, }) } diff --git a/core/test/stopper_test.go b/core/test/stopper_test.go index df207ec..e9954b0 100644 --- a/core/test/stopper_test.go +++ b/core/test/stopper_test.go @@ -61,9 +61,9 @@ func (s *StopperTestSuite) TestStopByConfirmedBlocks() { } app.TotalOrderingDelivered(hashes, false) for _, h := range hashes { - app.BlockDelivered(types.Block{ - Hash: h, - ConsensusTimestamp: time.Time{}}) + app.BlockDelivered(h, types.FinalizationResult{ + Timestamp: time.Time{}, + }) } } } diff --git a/core/types/block.go b/core/types/block.go index fc69481..5996acf 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -39,6 +39,13 @@ var ( } ) +// FinalizationResult represents the result of DEXON consensus algorithm. +type FinalizationResult struct { + Randomness []byte `json:"randomness"` + Timestamp time.Time `json:"timestamp"` + Height uint64 `json:"height"` +} + // Witness represents the consensus information on the compaction chain. type Witness struct { Timestamp time.Time `json:"timestamp"` @@ -61,18 +68,16 @@ func NewBlock() (b *Block) { // Block represents a single event broadcasted on the network. type Block struct { - ProposerID NodeID `json:"proposer_id"` - ParentHash common.Hash `json:"parent_hash"` - Hash common.Hash `json:"hash"` - Position Position `json:"position"` - Timestamp time.Time `json:"timestamps"` - Acks common.SortedHashes `json:"acks"` - Payload []byte `json:"payload"` - Randomness []byte `json:"randomness"` - ConsensusTimestamp time.Time `json:"consensus_timestamp"` - ConsensusHeight uint64 `json:"consensus_height"` - Witness Witness `json:"witness"` - Signature crypto.Signature `json:"signature"` + ProposerID NodeID `json:"proposer_id"` + ParentHash common.Hash `json:"parent_hash"` + Hash common.Hash `json:"hash"` + Position Position `json:"position"` + Timestamp time.Time `json:"timestamps"` + Acks common.SortedHashes `json:"acks"` + Payload []byte `json:"payload"` + Witness Witness `json:"witness"` + Finalization FinalizationResult `json:"finalization"` + Signature crypto.Signature `json:"signature"` CRSSignature crypto.Signature `json:"crs_signature"` } @@ -92,8 +97,8 @@ func (b *Block) Clone() (bcopy *Block) { bcopy.Position.Height = b.Position.Height bcopy.Signature = b.Signature.Clone() bcopy.CRSSignature = b.CRSSignature.Clone() - bcopy.ConsensusTimestamp = b.ConsensusTimestamp - bcopy.ConsensusHeight = b.ConsensusHeight + 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)) @@ -103,8 +108,8 @@ func (b *Block) Clone() (bcopy *Block) { copy(bcopy.Acks, b.Acks) bcopy.Payload = make([]byte, len(b.Payload)) copy(bcopy.Payload, b.Payload) - bcopy.Randomness = make([]byte, len(b.Randomness)) - copy(bcopy.Randomness, b.Randomness) + bcopy.Finalization.Randomness = make([]byte, len(b.Finalization.Randomness)) + copy(bcopy.Finalization.Randomness, b.Finalization.Randomness) return } diff --git a/core/types/block_test.go b/core/types/block_test.go index 07d27ab..b03b785 100644 --- a/core/types/block_test.go +++ b/core/types/block_test.go @@ -57,12 +57,14 @@ func (s *BlockTestSuite) createRandomBlock() *Block { Timestamp: time.Now().Add(time.Duration(rand.Int())), Data: s.randomBytes(), }, - ConsensusHeight: rand.Uint64(), - ConsensusTimestamp: time.Now().Add(time.Duration(rand.Int())), - Payload: s.randomBytes(), - Randomness: s.randomBytes(), - Signature: crypto.Signature{Signature: s.randomBytes()}, - CRSSignature: crypto.Signature{Signature: s.randomBytes()}, + Finalization: FinalizationResult{ + Timestamp: time.Now().Add(time.Duration(rand.Int())), + Height: rand.Uint64(), + Randomness: s.randomBytes(), + }, + Payload: s.randomBytes(), + Signature: crypto.Signature{Signature: s.randomBytes()}, + CRSSignature: crypto.Signature{Signature: s.randomBytes()}, } return b } |