OSDN Git Service

Mov late node sync test (#529)
[bytom/vapor.git] / application / mov / mov_core.go
index a6357bc..4a08cf5 100644 (file)
@@ -11,11 +11,13 @@ import (
        "github.com/bytom/vapor/consensus/segwit"
        dbm "github.com/bytom/vapor/database/leveldb"
        "github.com/bytom/vapor/errors"
+       "github.com/bytom/vapor/protocol"
        "github.com/bytom/vapor/protocol/bc"
        "github.com/bytom/vapor/protocol/bc/types"
 )
 
 var (
+       errChainStatusHasAlreadyInit    = errors.New("mov chain status has already initialized")
        errInvalidTradePairs            = errors.New("The trade pairs in the tx input is invalid")
        errStatusFailMustFalse          = errors.New("status fail of transaction does not allow to be true")
        errInputProgramMustP2WMCScript  = errors.New("input program of trade tx must p2wmc script")
@@ -57,7 +59,7 @@ func (m *Core) ApplyBlock(block *types.Block) error {
 
        if block.Height == m.startBlockHeight {
                blockHash := block.Hash()
-               return m.movStore.InitDBState(block.Height, &blockHash)
+               return m.InitChainStatus(&blockHash)
        }
 
        if err := m.validateMatchedTxSequence(block.Transactions); err != nil {
@@ -98,6 +100,10 @@ func (m *Core) BeforeProposalBlock(txs []*types.Tx, blockHeight uint64, gasLeft
 // ChainStatus return the current block height and block hash in dex core
 func (m *Core) ChainStatus() (uint64, *bc.Hash, error) {
        state, err := m.movStore.GetMovDatabaseState()
+       if err == database.ErrNotInitDBState {
+               return 0, nil, protocol.ErrNotInitSubProtocolChainStatus
+       }
+
        if err != nil {
                return 0, nil, err
        }
@@ -112,6 +118,11 @@ func (m *Core) DetachBlock(block *types.Block) error {
                return nil
        }
 
+       if block.Height == m.startBlockHeight {
+               m.movStore.Clear()
+               return nil
+       }
+
        deleteOrders, addOrders, err := decodeTxsOrders(block.Transactions)
        if err != nil {
                return err
@@ -120,6 +131,15 @@ func (m *Core) DetachBlock(block *types.Block) error {
        return m.movStore.ProcessOrders(addOrders, deleteOrders, &block.BlockHeader)
 }
 
+// InitChainStatus used to init the start block height and start block hash to store
+func (m *Core) InitChainStatus(startHash *bc.Hash) error {
+       if _, err := m.movStore.GetMovDatabaseState(); err == nil {
+               return errChainStatusHasAlreadyInit
+       }
+
+       return m.movStore.InitDBState(m.startBlockHeight, startHash)
+}
+
 // IsDust block the transaction that are not generated by the match engine
 func (m *Core) IsDust(tx *types.Tx) bool {
        for _, input := range tx.Inputs {
@@ -162,11 +182,11 @@ func (m *Core) ValidateTx(tx *types.Tx, verifyResult *bc.TxVerifyResult, blockHe
        }
 
        if common.IsMatchedTx(tx) {
-               if err := validateMatchedTx(tx, verifyResult, blockHeight); err != nil {
+               if err := validateMatchedTx(tx, blockHeight); err != nil {
                        return err
                }
        } else if common.IsCancelOrderTx(tx) {
-               if err := validateCancelOrderTx(tx, verifyResult); err != nil {
+               if err := validateCancelOrderTx(tx); err != nil {
                        return err
                }
        }
@@ -220,7 +240,7 @@ func calcFeeAmount(matchedTx *types.Tx) (map[bc.AssetID]*matchedTxFee, error) {
        return assetFeeMap, nil
 }
 
-func validateCancelOrderTx(tx *types.Tx, verifyResult *bc.TxVerifyResult) error {
+func validateCancelOrderTx(tx *types.Tx) error {
        for _, input := range tx.Inputs {
                if !segwit.IsP2WMCScript(input.ControlProgram()) {
                        return errInputProgramMustP2WMCScript
@@ -253,7 +273,7 @@ func validateMagneticContractArgs(fromAssetAmount bc.AssetAmount, program []byte
        return nil
 }
 
-func validateMatchedTx(tx *types.Tx, verifyResult *bc.TxVerifyResult, blockHeight uint64) error {
+func validateMatchedTx(tx *types.Tx, blockHeight uint64) error {
        fromAssetIDMap := make(map[string]bool)
        toAssetIDMap := make(map[string]bool)
        for i, input := range tx.Inputs {