• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: Commit

Golang implemented sidechain for Bytom


Commit MetaInfo

Revisiondf33e58172dd5f99a7022610db6a5a32664ac2ee (tree)
Time2019-07-09 12:29:00
AuthorWei Wang <apollo.mobility@gmai...>
CommiterWei Wang

Log Message

restore Tx back to Tx pool when chain is reorganized

Change Summary

Incremental Difference

--- a/protocol/block.go
+++ b/protocol/block.go
@@ -138,6 +138,7 @@ func (c *Chain) reorganizeChain(blockHeader *types.BlockHeader) error {
138138 return err
139139 }
140140
141+ txsToRestore := map[bc.Hash]*types.Tx{}
141142 for _, detachBlockHeader := range detachBlockHeaders {
142143 detachHash := detachBlockHeader.Hash()
143144 b, err := c.store.GetBlock(&detachHash)
@@ -163,10 +164,15 @@ func (c *Chain) reorganizeChain(blockHeader *types.BlockHeader) error {
163164 return err
164165 }
165166
167+ for _, tx := range b.Transactions {
168+ txsToRestore[tx.ID] = tx
169+ }
170+
166171 blockHash := blockHeader.Hash()
167172 log.WithFields(log.Fields{"module": logModule, "height": blockHeader.Height, "hash": blockHash.String()}).Debug("detach from mainchain")
168173 }
169174
175+ txsToRemove := map[bc.Hash]*types.Tx{}
170176 irrBlockHeader := c.lastIrrBlockHeader
171177 for _, attachBlockHeader := range attachBlockHeaders {
172178 attachHash := attachBlockHeader.Hash()
@@ -201,6 +207,14 @@ func (c *Chain) reorganizeChain(blockHeader *types.BlockHeader) error {
201207 irrBlockHeader = attachBlockHeader
202208 }
203209
210+ for _, tx := range b.Transactions {
211+ if _, ok := txsToRestore[tx.ID]; !ok {
212+ txsToRemove[tx.ID] = tx
213+ } else {
214+ delete(txsToRestore, tx.ID)
215+ }
216+ }
217+
204218 blockHash := blockHeader.Hash()
205219 log.WithFields(log.Fields{"module": logModule, "height": blockHeader.Height, "hash": blockHash.String()}).Debug("attach from mainchain")
206220 }
@@ -210,6 +224,21 @@ func (c *Chain) reorganizeChain(blockHeader *types.BlockHeader) error {
210224 irrBlockHeader.Height <= c.lastIrrBlockHeader.Height {
211225 return errors.New("rollback block below the height of irreversible block")
212226 }
227+
228+ for txHash := range txsToRemove {
229+ c.txPool.RemoveTransaction(&txHash)
230+ }
231+
232+ for _, tx := range txsToRestore {
233+ // the number of restored Tx should be very small or most of time ZERO
234+ // Error returned from validation is ignored, tx could still be lost if validation fails.
235+ // TODO: adjust tx timestamp so that it won't starve in pool.
236+ c.ValidateTx(tx)
237+ }
238+
239+ if len(txsToRestore) > 0 {
240+ log.WithFields(log.Fields{"module": logModule, "num": len(txsToRestore)}).Debug("restore txs back to pool")
241+ }
213242 consensusResults = append(consensusResults, consensusResult.Fork())
214243 return c.setState(blockHeader, irrBlockHeader, attachBlockHeaders, utxoView, consensusResults)
215244 }
Show on old repository browser