OSDN Git Service

restore Tx back to Tx pool when chain is reorganized
[bytom/bytom.git] / protocol / block.go
index ac46aed..97ea244 100644 (file)
 package protocol
 
 import (
-       "context"
-       "time"
+       log "github.com/sirupsen/logrus"
 
        "github.com/bytom/errors"
-       "github.com/bytom/log"
        "github.com/bytom/protocol/bc"
-       "github.com/bytom/protocol/bc/legacy"
+       "github.com/bytom/protocol/bc/types"
        "github.com/bytom/protocol/state"
        "github.com/bytom/protocol/validation"
 )
 
-// maxBlockTxs limits the number of transactions
-// included in each block.
-const maxBlockTxs = 10000
-
-// saveSnapshotFrequency stores how often to save a state
-// snapshot to the Store.
-const saveSnapshotFrequency = time.Hour
-
 var (
        // ErrBadBlock is returned when a block is invalid.
        ErrBadBlock = errors.New("invalid block")
-
-       // ErrStaleState is returned when the Chain does not have a current
-       // blockchain state.
-       ErrStaleState = errors.New("stale blockchain state")
-
        // ErrBadStateRoot is returned when the computed assets merkle root
        // disagrees with the one declared in a block header.
        ErrBadStateRoot = errors.New("invalid state merkle root")
 )
 
-// GetBlock returns the block at the given height, if there is one,
-// otherwise it returns an error.
-func (c *Chain) GetBlock(height uint64) (*legacy.Block, error) {
-       return c.store.GetBlock(height)
+// BlockExist check is a block in chain or orphan
+func (c *Chain) BlockExist(hash *bc.Hash) bool {
+       return c.index.BlockExist(hash) || c.orphanManage.BlockExist(hash)
 }
 
-// ValidateBlock validates an incoming block in advance of applying it
-// to a snapshot (with ApplyValidBlock) and committing it to the
-// blockchain (with CommitAppliedBlock).
-func (c *Chain) ValidateBlock(block, prev *legacy.Block) error {
-       blockEnts := legacy.MapBlock(block)
-       prevEnts := legacy.MapBlock(prev)
-       err := validation.ValidateBlock(blockEnts, prevEnts)
-       if err != nil {
-               return errors.Sub(ErrBadBlock, err)
+// GetBlockByHash return a block by given hash
+func (c *Chain) GetBlockByHash(hash *bc.Hash) (*types.Block, error) {
+       return c.store.GetBlock(hash)
+}
+
+// GetBlockByHeight return a block header by given height
+func (c *Chain) GetBlockByHeight(height uint64) (*types.Block, error) {
+       node := c.index.NodeByHeight(height)
+       if node == nil {
+               return nil, errors.New("can't find block in given height")
        }
-       return errors.Sub(ErrBadBlock, err)
+       return c.store.GetBlock(&node.Hash)
 }
 
-// ApplyValidBlock creates an updated snapshot without validating the
-// block.
-func (c *Chain) ApplyValidBlock(block *legacy.Block) (*state.Snapshot, error) {
-       //TODO replace with a pre-defined init blo
-       var newSnapshot *state.Snapshot
-       if c.state.snapshot == nil {
-               newSnapshot = state.Empty()
-       } else {
-               newSnapshot = state.Copy(c.state.snapshot)
+// GetHeaderByHash return a block header by given hash
+func (c *Chain) GetHeaderByHash(hash *bc.Hash) (*types.BlockHeader, error) {
+       node := c.index.GetNode(hash)
+       if node == nil {
+               return nil, errors.New("can't find block header in given hash")
        }
+       return node.BlockHeader(), nil
+}
 
-       err := newSnapshot.ApplyBlock(legacy.MapBlock(block))
-       if err != nil {
-               return nil, err
+// GetHeaderByHeight return a block header by given height
+func (c *Chain) GetHeaderByHeight(height uint64) (*types.BlockHeader, error) {
+       node := c.index.NodeByHeight(height)
+       if node == nil {
+               return nil, errors.New("can't find block header in given height")
+       }
+       return node.BlockHeader(), nil
+}
+
+func (c *Chain) calcReorganizeNodes(node *state.BlockNode) ([]*state.BlockNode, []*state.BlockNode) {
+       var attachNodes []*state.BlockNode
+       var detachNodes []*state.BlockNode
+
+       attachNode := node
+       for c.index.NodeByHeight(attachNode.Height) != attachNode {
+               attachNodes = append([]*state.BlockNode{attachNode}, attachNodes...)
+               attachNode = attachNode.Parent
        }
-       //fmt.Printf("want %v, ger %v \n", block.BlockHeader.AssetsMerkleRoot, newSnapshot.Tree.RootHash())
-       if block.AssetsMerkleRoot != newSnapshot.Tree.RootHash() {
-               return nil, ErrBadStateRoot
+
+       detachNode := c.bestNode
+       for detachNode != attachNode {
+               detachNodes = append(detachNodes, detachNode)
+               detachNode = detachNode.Parent
        }
-       return newSnapshot, nil
+       return attachNodes, detachNodes
 }
 
-// CommitBlock commits a block to the blockchain. The block
-// must already have been applied with ApplyValidBlock or
-// ApplyNewBlock, which will have produced the new snapshot that's
-// required here.
-//
-// This function saves the block to the store and sometimes (not more
-// often than saveSnapshotFrequency) saves the state tree to the
-// store. New-block callbacks (via asynchronous block-processor pins)
-// are triggered.
-//
-// TODO(bobg): rename to CommitAppliedBlock for clarity (deferred from https://github.com/chain/chain/pull/788)
-func (c *Chain) CommitAppliedBlock(ctx context.Context, block *legacy.Block, snapshot *state.Snapshot) error {
-       // SaveBlock is the linearization point. Once the block is committed
-       // to persistent storage, the block has been applied and everything
-       // else can be derived from that block.
-       err := c.store.SaveBlock(block)
-       if err != nil {
-               return errors.Wrap(err, "storing block")
+func (c *Chain) connectBlock(block *types.Block) (err error) {
+       bcBlock := types.MapBlock(block)
+       if bcBlock.TransactionStatus, err = c.store.GetTransactionStatus(&bcBlock.ID); err != nil {
+               return err
+       }
+
+       utxoView := state.NewUtxoViewpoint()
+       if err := c.store.GetTransactionsUtxo(utxoView, bcBlock.Transactions); err != nil {
+               return err
+       }
+       if err := utxoView.ApplyBlock(bcBlock, bcBlock.TransactionStatus); err != nil {
+               return err
        }
-       if block.Time().After(c.lastQueuedSnapshot.Add(saveSnapshotFrequency)) {
-               c.queueSnapshot(ctx, block.Height, block.Time(), snapshot)
+
+       node := c.index.GetNode(&bcBlock.ID)
+       if err := c.setState(node, utxoView); err != nil {
+               return err
+       }
+
+       for _, tx := range block.Transactions {
+               c.txPool.RemoveTransaction(&tx.Tx.ID)
+       }
+       return nil
+}
+
+func (c *Chain) reorganizeChain(node *state.BlockNode) error {
+       attachNodes, detachNodes := c.calcReorganizeNodes(node)
+       utxoView := state.NewUtxoViewpoint()
+
+       txsToRestore := map[bc.Hash]*types.Tx{}
+       for _, detachNode := range detachNodes {
+               b, err := c.store.GetBlock(&detachNode.Hash)
+               if err != nil {
+                       return err
+               }
+
+               detachBlock := types.MapBlock(b)
+               if err := c.store.GetTransactionsUtxo(utxoView, detachBlock.Transactions); err != nil {
+                       return err
+               }
+               txStatus, err := c.GetTransactionStatus(&detachBlock.ID)
+               if err != nil {
+                       return err
+               }
+               if err := utxoView.DetachBlock(detachBlock, txStatus); err != nil {
+                       return err
+               }
+
+               for _, tx := range b.Transactions {
+                       txsToRestore[tx.ID] = tx
+               }
+               log.WithFields(log.Fields{"module": logModule, "height": node.Height, "hash": node.Hash.String()}).Debug("detach from mainchain")
+       }
+
+       txsToRemove := map[bc.Hash]*types.Tx{}
+       for _, attachNode := range attachNodes {
+               b, err := c.store.GetBlock(&attachNode.Hash)
+               if err != nil {
+                       return err
+               }
+
+               attachBlock := types.MapBlock(b)
+               if err := c.store.GetTransactionsUtxo(utxoView, attachBlock.Transactions); err != nil {
+                       return err
+               }
+               txStatus, err := c.GetTransactionStatus(&attachBlock.ID)
+               if err != nil {
+                       return err
+               }
+               if err := utxoView.ApplyBlock(attachBlock, txStatus); err != nil {
+                       return err
+               }
+
+               for _, tx := range b.Transactions {
+                       if _, ok := txsToRestore[tx.ID]; !ok {
+                               txsToRemove[tx.ID] = tx
+                       } else {
+                               delete(txsToRestore, tx.ID)
+                       }
+               }
+
+               log.WithFields(log.Fields{"module": logModule, "height": node.Height, "hash": node.Hash.String()}).Debug("attach from mainchain")
        }
 
-       err = c.store.FinalizeBlock(ctx, block.Height)
+       if err := c.setState(node, utxoView); err != nil {
+               return err
+       }
+
+       for txHash := range txsToRemove {
+               c.txPool.RemoveTransaction(&txHash)
+       }
+
+       for _, tx := range txsToRestore {
+               // the number of restored Tx should be very small or most of time ZERO
+               // Error returned from validation is ignored, tx could still be lost if validation fails.
+               // TODO: adjust tx timestamp so that it won't starve in pool.
+               if _, err := c.ValidateTx(tx); err != nil {
+                       log.WithFields(log.Fields{"module": logModule, "tx_id": tx.Tx.ID.String(), "error": err}).Info("restore tx fail")
+               }
+       }
+
+       if len(txsToRestore) > 0 {
+               log.WithFields(log.Fields{"module": logModule, "num": len(txsToRestore)}).Debug("restore txs back to pool")
+       }
+
+       return nil
+}
+
+// SaveBlock will validate and save block into storage
+func (c *Chain) saveBlock(block *types.Block) error {
+       bcBlock := types.MapBlock(block)
+       parent := c.index.GetNode(&block.PreviousBlockHash)
+
+       if err := validation.ValidateBlock(bcBlock, parent); err != nil {
+               return errors.Sub(ErrBadBlock, err)
+       }
+       if err := c.store.SaveBlock(block, bcBlock.TransactionStatus); err != nil {
+               return err
+       }
+
+       c.orphanManage.Delete(&bcBlock.ID)
+       node, err := state.NewBlockNode(&block.BlockHeader, parent)
        if err != nil {
-               return errors.Wrap(err, "finalizing block")
-       }
-
-       // c.setState will update the local blockchain state and height.
-       // When c.store is a txdb.Store, and c has been initialized with a
-       // channel from txdb.ListenBlocks, then the above call to
-       // c.store.FinalizeBlock will have done a postgresql NOTIFY and
-       // that will wake up the goroutine in NewChain, which also calls
-       // setHeight.  But duplicate calls with the same blockheight are
-       // harmless; and the following call is required in the cases where
-       // it's not redundant.
-       c.setState(block, snapshot)
+               return err
+       }
+
+       c.index.AddNode(node)
        return nil
 }
 
-func (c *Chain) queueSnapshot(ctx context.Context, height uint64, timestamp time.Time, s *state.Snapshot) {
-       // Non-blockingly queue the snapshot for storage.
-       ps := pendingSnapshot{height: height, snapshot: s}
-       select {
-       case c.pendingSnapshots <- ps:
-               c.lastQueuedSnapshot = timestamp
-       default:
-               // Skip it; saving snapshots is taking longer than the snapshotting period.
-               log.Printf(ctx, "snapshot storage is taking too long; last queued at %s",
-                       c.lastQueuedSnapshot)
+func (c *Chain) saveSubBlock(block *types.Block) *types.Block {
+       blockHash := block.Hash()
+       prevOrphans, ok := c.orphanManage.GetPrevOrphans(&blockHash)
+       if !ok {
+               return block
+       }
+
+       bestBlock := block
+       for _, prevOrphan := range prevOrphans {
+               orphanBlock, ok := c.orphanManage.Get(prevOrphan)
+               if !ok {
+                       log.WithFields(log.Fields{"module": logModule, "hash": prevOrphan.String()}).Warning("saveSubBlock fail to get block from orphanManage")
+                       continue
+               }
+               if err := c.saveBlock(orphanBlock); err != nil {
+                       log.WithFields(log.Fields{"module": logModule, "hash": prevOrphan.String(), "height": orphanBlock.Height}).Warning("saveSubBlock fail to save block")
+                       continue
+               }
+
+               if subBestBlock := c.saveSubBlock(orphanBlock); subBestBlock.Height > bestBlock.Height {
+                       bestBlock = subBestBlock
+               }
        }
+       return bestBlock
 }
 
-func (c *Chain) setHeight(h uint64) {
-       // We call setHeight from two places independently:
-       // CommitBlock and the Postgres LISTEN goroutine.
-       // This means we can get here twice for each block,
-       // and any of them might be arbitrarily delayed,
-       // which means h might be from the past.
-       // Detect and discard these duplicate calls.
+type processBlockResponse struct {
+       isOrphan bool
+       err      error
+}
+
+type processBlockMsg struct {
+       block *types.Block
+       reply chan processBlockResponse
+}
 
-       c.state.cond.L.Lock()
-       defer c.state.cond.L.Unlock()
+// ProcessBlock is the entry for chain update
+func (c *Chain) ProcessBlock(block *types.Block) (bool, error) {
+       reply := make(chan processBlockResponse, 1)
+       c.processBlockCh <- &processBlockMsg{block: block, reply: reply}
+       response := <-reply
+       return response.isOrphan, response.err
+}
 
-       if h <= c.state.height {
-               return
+func (c *Chain) blockProcesser() {
+       for msg := range c.processBlockCh {
+               isOrphan, err := c.processBlock(msg.block)
+               msg.reply <- processBlockResponse{isOrphan: isOrphan, err: err}
        }
-       c.state.height = h
-       c.state.cond.Broadcast()
 }
 
-func NewInitialBlock(timestamp time.Time) (*legacy.Block, error) {
-       // TODO(kr): move this into a lower-level package (e.g. chain/protocol/bc)
-       // so that other packages (e.g. chain/protocol/validation) unit tests can
-       // call this function.
-       root, err := bc.MerkleRoot(nil) // calculate the zero value of the tx merkle root
-       if err != nil {
-               return nil, errors.Wrap(err, "calculating zero value of tx merkle root")
+// ProcessBlock is the entry for handle block insert
+func (c *Chain) processBlock(block *types.Block) (bool, error) {
+       blockHash := block.Hash()
+       if c.BlockExist(&blockHash) {
+               log.WithFields(log.Fields{"module": logModule, "hash": blockHash.String(), "height": block.Height}).Info("block has been processed")
+               return c.orphanManage.BlockExist(&blockHash), nil
+       }
+
+       if parent := c.index.GetNode(&block.PreviousBlockHash); parent == nil {
+               c.orphanManage.Add(block)
+               return true, nil
+       }
+
+       if err := c.saveBlock(block); err != nil {
+               return false, err
+       }
+
+       bestBlock := c.saveSubBlock(block)
+       bestBlockHash := bestBlock.Hash()
+       bestNode := c.index.GetNode(&bestBlockHash)
+
+       if bestNode.Parent == c.bestNode {
+               log.WithFields(log.Fields{"module": logModule}).Debug("append block to the end of mainchain")
+               return false, c.connectBlock(bestBlock)
        }
 
-       b := &legacy.Block{
-               BlockHeader: legacy.BlockHeader{
-                       Version:     1,
-                       Height:      1,
-                       TimestampMS: bc.Millis(timestamp),
-                       BlockCommitment: legacy.BlockCommitment{
-                               TransactionsMerkleRoot: root,
-                       },
-               },
-               Transactions: []*legacy.Tx{},
+       if bestNode.Height > c.bestNode.Height && bestNode.WorkSum.Cmp(c.bestNode.WorkSum) >= 0 {
+               log.WithFields(log.Fields{"module": logModule}).Debug("start to reorganize chain")
+               return false, c.reorganizeChain(bestNode)
        }
-       return b, nil
+       return false, nil
 }