// 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
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()
// 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
}
// 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
ProducerSubsidys []ProducerSubsidy
SoftForkPoint map[uint64]uint64
+ MovStartHeight uint64
}
// ActiveNetParams is the active NetParams
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 {
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
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()
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 {
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
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()
}
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")
}