OSDN Git Service

opt code
authorshenao78 <shenao.78@163.com>
Wed, 30 Oct 2019 06:53:43 +0000 (14:53 +0800)
committershenao78 <shenao.78@163.com>
Wed, 30 Oct 2019 06:53:43 +0000 (14:53 +0800)
application/mov/mov_core.go
consensus/general.go
node/node.go
proposal/proposal.go
protocol/block.go

index 246990d..91f6248 100644 (file)
@@ -32,13 +32,14 @@ var (
 // MovCore represent the core logic of the match module, which include generate match transactions before packing the block,
 // verify the match transaction in block is correct, and update the order table according to the transaction.
 type MovCore struct {
-       movStore database.MovStore
+       movStore         database.MovStore
+       startBlockHeight uint64
 }
 
 // NewMovCore return a instance of MovCore by path of mov db
-func NewMovCore(dbBackend, dbDir string) *MovCore {
+func NewMovCore(dbBackend, dbDir string, startBlockHeight uint64) *MovCore {
        movDB := dbm.NewDB("mov", dbBackend, dbDir)
-       return &MovCore{movStore: database.NewLevelDBMovStore(movDB)}
+       return &MovCore{movStore: database.NewLevelDBMovStore(movDB), startBlockHeight: startBlockHeight}
 }
 
 // Name return the name of current module
@@ -46,11 +47,6 @@ func (m *MovCore) Name() string {
        return "MOV"
 }
 
-// InitChainStatus init the start block height and start block hash, this method only needs to be called once in the initialization
-func (m *MovCore) InitChainStatus(blockHeight uint64, blockHash *bc.Hash) error {
-       return m.movStore.InitDBState(blockHeight, blockHash)
-}
-
 // ChainStatus return the current block height and block hash in dex core
 func (m *MovCore) ChainStatus() (uint64, *bc.Hash, error) {
        state, err := m.movStore.GetMovDatabaseState()
@@ -180,6 +176,16 @@ func validateMagneticContractArgs(inputAmount uint64, program []byte) error {
 // ApplyBlock parse pending order and cancel from the the transactions of block
 // and add pending order to the dex db, remove cancel order from dex db.
 func (m *MovCore) ApplyBlock(block *types.Block) error {
+       if block.Height < m.startBlockHeight {
+               return nil
+       }
+       if block.Height == m.startBlockHeight {
+               blockHash := block.Hash()
+               if err := m.movStore.InitDBState(block.Height, &blockHash); err != nil {
+                       return err
+               }
+       }
+
        if err := m.validateMatchedTxSequence(block.Transactions); err != nil {
                return err
        }
@@ -272,6 +278,9 @@ func getSortedTradePairsFromMatchedTx(tx *types.Tx) ([]*common.TradePair, error)
 // DetachBlock parse pending order and cancel from the the transactions of block
 // and add cancel order to the dex db, remove pending order from dex db.
 func (m *MovCore) DetachBlock(block *types.Block) error {
+       if block.Height <= m.startBlockHeight {
+               return nil
+       }
        deleteOrders, addOrders, err := applyTransactions(block.Transactions)
        if err != nil {
                return err
index b34f19d..5fd9478 100644 (file)
@@ -103,6 +103,7 @@ type Params struct {
        ProducerSubsidys []ProducerSubsidy
 
        SoftForkPoint map[uint64]uint64
+       MovStartHeight uint64
 }
 
 // ActiveNetParams is the active NetParams
index b84dd91..7e75267 100644 (file)
@@ -96,7 +96,7 @@ func NewNode(config *cfg.Config) *Node {
        accessTokens := accesstoken.NewStore(tokenDB)
 
        dispatcher := event.NewDispatcher()
-       movCore := mov.NewMovCore(config.DBBackend, config.DBDir())
+       movCore := mov.NewMovCore(config.DBBackend, config.DBDir(), consensus.ActiveNetParams.MovStartHeight)
        txPool := protocol.NewTxPool(store, []protocol.DustFilterer{movCore}, dispatcher)
        chain, err := protocol.NewChain(store, txPool, []protocol.Protocoler{movCore}, dispatcher)
        if err != nil {
@@ -107,12 +107,6 @@ func NewNode(config *cfg.Config) *Node {
                panic(err)
        }
 
-       if chain.BestBlockHeight() == 0 {
-               if err := movCore.InitChainStatus(0, chain.BestBlockHash()); err != nil {
-                       log.Fatalf("Failed to create Mov protocol:%v", err.Error())
-               }
-       }
-
        var accounts *account.Manager
        var assets *asset.Registry
        var wallet *w.Wallet
index 86ed311..4dc59dc 100644 (file)
@@ -104,19 +104,26 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
        bcBlock := &bc.Block{BlockHeader: &bc.BlockHeader{Height: nextBlockHeight}}
        b.Transactions = []*types.Tx{nil}
 
+       txs := txPool.GetTransactions()
+       sort.Sort(byTime(txs))
+
        entriesTxs := []*bc.Tx{}
-       txs, err := packageSubProtocolTxs(c, txPool, accountManager)
+       for _, txDesc := range txs {
+               entriesTxs = append(entriesTxs, txDesc.Tx.Tx)
+       }
+
+       subProtocolTxs, err := packageSubProtocolTxs(c, accountManager, maxBlockTxNum - len(entriesTxs))
        if err != nil {
                return nil, err
        }
-
-       for _, tx := range txs {
+       for _, tx := range subProtocolTxs {
                entriesTxs = append(entriesTxs, tx.Tx)
        }
 
        validateResults := validation.ValidateTxs(entriesTxs, bcBlock)
        for i, validateResult := range validateResults {
-               tx := txs[i].Tx
+               txDesc := txs[i]
+               tx := txDesc.Tx.Tx
                gasOnlyTx := false
 
                gasStatus := validateResult.GetGasState()
@@ -146,7 +153,7 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
                        return nil, err
                }
 
-               b.Transactions = append(b.Transactions, txs[i])
+               b.Transactions = append(b.Transactions, txDesc.Tx)
                txEntries = append(txEntries, tx)
                gasUsed += uint64(gasStatus.GasUsed)
                if gasUsed == consensus.ActiveNetParams.MaxBlockGas {
@@ -183,14 +190,8 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
        return b, err
 }
 
-func packageSubProtocolTxs(chain *protocol.Chain, txPool *protocol.TxPool, accountManager *account.Manager) ([]*types.Tx, error) {
+func packageSubProtocolTxs(chain *protocol.Chain, accountManager *account.Manager, capacity int) ([]*types.Tx, error) {
        var packageTxs []*types.Tx
-       txs := txPool.GetTransactions()
-       sort.Sort(byTime(txs))
-       for _, txDesc := range txs {
-               packageTxs = append(packageTxs, txDesc.Tx)
-       }
-       capacity := maxBlockTxNum - len(txs)
        cp, err := accountManager.GetCoinbaseControlProgram()
        if err != nil {
                return nil, err
index a02f1a6..21d254f 100644 (file)
@@ -148,6 +148,12 @@ func (c *Chain) reorganizeChain(blockHeader *types.BlockHeader) error {
                return err
        }
 
+       for _, p := range c.subProtocols {
+               if err := c.syncProtocolStatus(p); err != nil {
+                       return errors.Wrap(err, p.Name(), "sync sub protocol status")
+               }
+       }
+
        txsToRestore := map[bc.Hash]*types.Tx{}
        for _, detachBlockHeader := range detachBlockHeaders {
                detachHash := detachBlockHeader.Hash()
@@ -175,10 +181,6 @@ func (c *Chain) reorganizeChain(blockHeader *types.BlockHeader) error {
                }
 
                for _, p := range c.subProtocols {
-                       if err := c.syncProtocolStatus(p); err != nil {
-                               return errors.Wrap(err, p.Name(), "sync sub protocol status")
-                       }
-
                        if err := p.DetachBlock(b); err != nil {
                                return errors.Wrap(err, p.Name(), "sub protocol detach block")
                        }