OSDN Git Service

Sync for late node (#527)
[bytom/vapor.git] / application / mov / mov_core.go
index c4f7760..f91f742 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
        }
@@ -120,6 +126,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 {