OSDN Git Service

fix dead lock
[bytom/vapor.git] / protocol / protocol.go
index dc0b241..59880a7 100644 (file)
@@ -21,9 +21,10 @@ const (
 
 type Protocoler interface {
        Name() string
-       Status() (uint64, *bc.Hash)
-       ValidateBlock(block *bc.Block) error
-       ValidateTxs(txs []*bc.Tx) error
+       BeforeProposalBlock(capacity int, nodeProgram []byte) ([]*types.Tx, error)
+       ChainStatus() (uint64, *bc.Hash, error)
+       ValidateBlock(block *types.Block, verifyResults []*bc.TxVerifyResult) error
+       ValidateTxs(txs []*types.Tx, verifyResults []*bc.TxVerifyResult) error
        ApplyBlock(block *types.Block) error
        DetachBlock(block *types.Block) error
 }
@@ -165,6 +166,10 @@ func (c *Chain) InMainChain(hash bc.Hash) bool {
        return *blockHash == hash
 }
 
+func (c *Chain) SubProtocols() []Protocoler {
+       return c.subProtocols
+}
+
 // trace back to the tail of the chain from the given block header
 func (c *Chain) traceLongestChainTail(blockHeader *types.BlockHeader) (*types.BlockHeader, error) {
        longestTail, workQueue := blockHeader, []*types.BlockHeader{blockHeader}
@@ -202,8 +207,12 @@ func (c *Chain) markTransactions(txs ...*types.Tx) {
 }
 
 func (c *Chain) syncProtocolStatus(subProtocol Protocoler) error {
-       protocolHeight, protocolHash := subProtocol.Status()
-       if protocolHeight == c.BestBlockHeight() && protocolHash == c.BestBlockHash() {
+       protocolHeight, protocolHash, err := subProtocol.ChainStatus()
+       if err != nil {
+               return errors.Wrap(err, "failed on get sub protocol status")
+       }
+
+       if protocolHeight == c.bestBlockHeader.Height && *protocolHash == c.bestBlockHeader.Hash() {
                return nil
        }
 
@@ -217,7 +226,10 @@ func (c *Chain) syncProtocolStatus(subProtocol Protocoler) error {
                        return errors.Wrap(err, subProtocol.Name(), "sub protocol detach block err")
                }
 
-               protocolHeight, protocolHash = subProtocol.Status()
+               protocolHeight, protocolHash, err = subProtocol.ChainStatus()
+               if err != nil {
+                       return errors.Wrap(err, "failed on get sub protocol status")
+               }
        }
 
        for height := protocolHeight + 1; height <= c.BestBlockHeight(); height++ {
@@ -230,7 +242,11 @@ func (c *Chain) syncProtocolStatus(subProtocol Protocoler) error {
                        return errors.Wrap(err, subProtocol.Name(), "sub protocol apply block err")
                }
 
-               protocolHeight, protocolHash = subProtocol.Status()
+               protocolHeight, protocolHash, err = subProtocol.ChainStatus()
+               if err != nil {
+                       return errors.Wrap(err, "failed on get sub protocol status")
+               }
+
                if *protocolHash != block.Hash() {
                        return errors.Wrap(errors.New("sub protocol status sync err"), subProtocol.Name())
                }