• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: Commit

Golang implemented sidechain for Bytom


Commit MetaInfo

Revision2b9475d7d8a8344a7ae73e2744e7d4589e81b3ef (tree)
Time2019-07-05 19:02:10
AuthorWei Wang <apollo.mobility@gmai...>
CommiterWei Wang

Log Message

keep last irreversible block on the main chain

Only update the LIB pointer when newly irreversible block is on the main
chain. Emit warning otherwise.

Change Summary

Incremental Difference

--- a/netsync/chainmgr/block_keeper.go
+++ b/netsync/chainmgr/block_keeper.go
@@ -190,7 +190,7 @@ func (bk *blockKeeper) syncWorker() {
190190 continue
191191 }
192192
193- if err := bk.peers.BroadcastNewStatus(bk.chain.BestBlockHeader(), bk.chain.BestIrreversibleHeader()); err != nil {
193+ if err := bk.peers.BroadcastNewStatus(bk.chain.BestBlockHeader(), bk.chain.LastIrreversibleHeader()); err != nil {
194194 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("fail on syncWorker broadcast new status")
195195 }
196196 case <-bk.quit:
--- a/netsync/chainmgr/handle.go
+++ b/netsync/chainmgr/handle.go
@@ -25,7 +25,7 @@ const (
2525 // Chain is the interface for Bytom core
2626 type Chain interface {
2727 BestBlockHeader() *types.BlockHeader
28- BestIrreversibleHeader() *types.BlockHeader
28+ LastIrreversibleHeader() *types.BlockHeader
2929 BestBlockHeight() uint64
3030 GetBlockByHash(*bc.Hash) (*types.Block, error)
3131 GetBlockByHeight(uint64) (*types.Block, error)
@@ -351,7 +351,7 @@ func (m *Manager) SendStatus(peer peers.BasePeer) error {
351351 return errors.New("invalid peer")
352352 }
353353
354- if err := p.SendStatus(m.chain.BestBlockHeader(), m.chain.BestIrreversibleHeader()); err != nil {
354+ if err := p.SendStatus(m.chain.BestBlockHeader(), m.chain.LastIrreversibleHeader()); err != nil {
355355 m.peers.RemovePeer(p.ID())
356356 return err
357357 }
--- a/protocol/bbft.go
+++ b/protocol/bbft.go
@@ -67,7 +67,10 @@ func (c *Chain) IsBlocker(prevBlockHash *bc.Hash, pubKey string, timeStamp uint6
6767 // return whether a block become irreversible, if so, the chain module must update status
6868 func (c *Chain) ProcessBlockSignature(signature, xPub []byte, blockHash *bc.Hash) error {
6969 xpubStr := hex.EncodeToString(xPub[:])
70- blockHeader, _ := c.store.GetBlockHeader(blockHash)
70+ blockHeader, err := c.store.GetBlockHeader(blockHash)
71+ if err != nil {
72+ return err
73+ }
7174
7275 // save the signature if the block is not exist
7376 if blockHeader == nil {
@@ -99,7 +102,7 @@ func (c *Chain) ProcessBlockSignature(signature, xPub []byte, blockHash *bc.Hash
99102
100103 // validateSign verify the signatures of block, and return the number of correct signature
101104 // if some signature is invalid, they will be reset to nil
102-// if the block has not the signature of blocker, it will return error
105+// if the block does not have the signature of blocker, it will return error
103106 func (c *Chain) validateSign(block *types.Block) error {
104107 consensusNodeMap, err := c.getConsensusNodes(&block.PreviousBlockHash)
105108 if err != nil {
@@ -222,15 +225,21 @@ func (c *Chain) SignBlock(block *types.Block) ([]byte, error) {
222225
223226 func (c *Chain) updateBlockSignature(blockHeader *types.BlockHeader, nodeOrder uint64, signature []byte) error {
224227 blockHeader.Set(nodeOrder, signature)
225- if err := c.store.SaveBlockHeader(blockHeader); err != nil {
226- return err
227- }
228228
229- if c.isIrreversible(blockHeader) && blockHeader.Height > c.bestIrrBlockHeader.Height {
230- if err := c.store.SaveChainStatus(c.bestBlockHeader, blockHeader, []*types.BlockHeader{}, state.NewUtxoViewpoint(), []*state.VoteResult{}); err != nil {
231- return err
229+ if c.isIrreversible(blockHeader) && blockHeader.Height > c.lastIrrBlockHeader.Height {
230+ if c.InMainChain(blockHeader.Hash()) {
231+ if err := c.store.SaveChainStatus(c.bestBlockHeader,
232+ blockHeader,
233+ []*types.BlockHeader{},
234+ state.NewUtxoViewpoint(),
235+ []*state.VoteResult{}); err != nil {
236+ return err
237+ }
238+ c.lastIrrBlockHeader = blockHeader
239+ } else {
240+ // block is on a forked chain. Do nothing.
241+ log.WithFields(log.Fields{"module": logModule}).Warn("majority votes received on forked chain")
232242 }
233- c.bestIrrBlockHeader = blockHeader
234243 }
235244 return nil
236245 }
--- a/protocol/block.go
+++ b/protocol/block.go
@@ -110,7 +110,7 @@ func (c *Chain) connectBlock(block *types.Block) (err error) {
110110 return err
111111 }
112112
113- irrBlockHeader := c.bestIrrBlockHeader
113+ irrBlockHeader := c.lastIrrBlockHeader
114114 if c.isIrreversible(&block.BlockHeader) && block.Height > irrBlockHeader.Height {
115115 irrBlockHeader = &block.BlockHeader
116116 }
@@ -167,7 +167,7 @@ func (c *Chain) reorganizeChain(blockHeader *types.BlockHeader) error {
167167 log.WithFields(log.Fields{"module": logModule, "height": blockHeader.Height, "hash": blockHash.String()}).Debug("detach from mainchain")
168168 }
169169
170- irrBlockHeader := c.bestIrrBlockHeader
170+ irrBlockHeader := c.lastIrrBlockHeader
171171 for _, attachBlockHeader := range attachBlockHeaders {
172172 attachHash := attachBlockHeader.Hash()
173173 b, err := c.store.GetBlock(&attachHash)
@@ -205,7 +205,9 @@ func (c *Chain) reorganizeChain(blockHeader *types.BlockHeader) error {
205205 log.WithFields(log.Fields{"module": logModule, "height": blockHeader.Height, "hash": blockHash.String()}).Debug("attach from mainchain")
206206 }
207207
208- if len(detachBlockHeaders) > 0 && detachBlockHeaders[len(detachBlockHeaders)-1].Height <= c.bestIrrBlockHeader.Height && irrBlockHeader.Height <= c.bestIrrBlockHeader.Height {
208+ if len(detachBlockHeaders) > 0 &&
209+ detachBlockHeaders[len(detachBlockHeaders)-1].Height <= c.lastIrrBlockHeader.Height &&
210+ irrBlockHeader.Height <= c.lastIrrBlockHeader.Height {
209211 return errors.New("rollback block below the height of irreversible block")
210212 }
211213 voteResults = append(voteResults, voteResult.Fork())
--- a/protocol/protocol.go
+++ b/protocol/protocol.go
@@ -26,8 +26,8 @@ type Chain struct {
2626 eventDispatcher *event.Dispatcher
2727
2828 cond sync.Cond
29- bestBlockHeader *types.BlockHeader
30- bestIrrBlockHeader *types.BlockHeader
29+ bestBlockHeader *types.BlockHeader // the last block on current main chain
30+ lastIrrBlockHeader *types.BlockHeader // the last irreversible block
3131 }
3232
3333 // NewChain returns a new Chain using store as the underlying storage.
@@ -56,7 +56,7 @@ func NewChain(store Store, txPool *TxPool, eventDispatcher *event.Dispatcher) (*
5656 return nil, err
5757 }
5858
59- c.bestIrrBlockHeader, err = c.store.GetBlockHeader(storeStatus.IrreversibleHash)
59+ c.lastIrrBlockHeader, err = c.store.GetBlockHeader(storeStatus.IrreversibleHash)
6060 if err != nil {
6161 return nil, err
6262 }
@@ -101,7 +101,7 @@ func (c *Chain) BestBlockHeight() uint64 {
101101 return c.bestBlockHeader.Height
102102 }
103103
104-// BestBlockHash return the hash of the chain tail block
104+// BestBlockHash return the hash of the main chain tail block
105105 func (c *Chain) BestBlockHash() *bc.Hash {
106106 c.cond.L.Lock()
107107 defer c.cond.L.Unlock()
@@ -109,11 +109,11 @@ func (c *Chain) BestBlockHash() *bc.Hash {
109109 return &bestHash
110110 }
111111
112-// BestIrreversibleHeader returns the chain best irreversible block header
113-func (c *Chain) BestIrreversibleHeader() *types.BlockHeader {
112+// LastIrreversibleHeader returns the chain last irreversible block header
113+func (c *Chain) LastIrreversibleHeader() *types.BlockHeader {
114114 c.cond.L.Lock()
115115 defer c.cond.L.Unlock()
116- return c.bestIrrBlockHeader
116+ return c.lastIrrBlockHeader
117117 }
118118
119119 // BestBlockHeader returns the chain best block header
@@ -145,7 +145,7 @@ func (c *Chain) setState(blockHeader, irrBlockHeader *types.BlockHeader, mainBlo
145145 }
146146
147147 c.bestBlockHeader = blockHeader
148- c.bestIrrBlockHeader = irrBlockHeader
148+ c.lastIrrBlockHeader = irrBlockHeader
149149
150150 blockHash := blockHeader.Hash()
151151 log.WithFields(log.Fields{"module": logModule, "height": blockHeader.Height, "hash": blockHash.String()}).Debug("chain best status has been update")
--- a/test/mock/chain.go
+++ b/test/mock/chain.go
@@ -37,7 +37,7 @@ func (c *Chain) BestBlockHeight() uint64 {
3737 return c.bestBlockHeader.Height
3838 }
3939
40-func (c *Chain) BestIrreversibleHeader() *types.BlockHeader {
40+func (c *Chain) LastIrreversibleHeader() *types.BlockHeader {
4141 return c.bestBlockHeader
4242 }
4343
Show on old repository browser