OSDN Git Service

add calc matchedtx gas func
authorshenao78 <shenao.78@163.com>
Thu, 31 Oct 2019 07:04:53 +0000 (15:04 +0800)
committershenao78 <shenao.78@163.com>
Thu, 31 Oct 2019 07:04:53 +0000 (15:04 +0800)
application/mov/mov_core.go
proposal/proposal.go
protocol/protocol.go

index c54a477..a3cbb28 100644 (file)
@@ -299,7 +299,7 @@ func (m *MovCore) DetachBlock(block *types.Block) error {
 }
 
 // BeforeProposalBlock return all transactions than can be matched, and the number of transactions cannot exceed the given capacity.
-func (m *MovCore) BeforeProposalBlock(nodeProgram []byte, gasLeft int64, calcGasUsed func(*types.Tx) (int64, error)) ([]*types.Tx, int64, error) {
+func (m *MovCore) BeforeProposalBlock(nodeProgram []byte, gasLeft int64) ([]*types.Tx, int64, error) {
        matchEngine := match.NewEngine(m.movStore, maxFeeRate, nodeProgram)
        tradePairMap := make(map[string]bool)
        tradePairIterator := database.NewTradePairIterator(m.movStore)
@@ -319,18 +319,20 @@ func (m *MovCore) BeforeProposalBlock(nodeProgram []byte, gasLeft int64, calcGas
                                return nil, 0, err
                        }
 
-                       gasUsed, err := calcGasUsed(matchedTx)
-                       if err != nil {
-                               return nil, 0, err
+                       gasUsed := calcMatchedTxGasUsed(matchedTx)
+                       if gasLeft - gasUsed >= 0 {
+                               packagedTxs = append(packagedTxs, matchedTx)
                        }
-
-                       packagedTxs = append(packagedTxs, matchedTx)
                        gasLeft -= gasUsed
                }
        }
        return packagedTxs, gasLeft, nil
 }
 
+func calcMatchedTxGasUsed(tx *types.Tx) int64 {
+       return int64(len(tx.Inputs)) * 150 + int64(tx.SerializedSize)
+}
+
 // IsDust block the transaction that are not generated by the match engine 
 func (m *MovCore) IsDust(tx *types.Tx) bool {
        for _, input := range tx.Inputs {
index 6a8264a..95aba63 100644 (file)
@@ -280,7 +280,7 @@ func getTxsFromSubProtocols(chain *protocol.Chain, accountManager *account.Manag
                        break
                }
 
-               subTxs, gasLeft, err = p.BeforeProposalBlock(cp, gasLeft, calcGasUsed(&bc.Block{BlockHeader: &bc.BlockHeader{Height: chain.BestBlockHeight() + 1}}))
+               subTxs, gasLeft, err = p.BeforeProposalBlock(cp, gasLeft)
                if err != nil {
                        log.WithFields(log.Fields{"module": logModule, "index": i, "error": err}).Error("failed on sub protocol txs package")
                        continue
@@ -291,17 +291,6 @@ func getTxsFromSubProtocols(chain *protocol.Chain, accountManager *account.Manag
        return result, nil
 }
 
-func calcGasUsed(bcBlock *bc.Block) func (*types.Tx) (int64, error) {
-       return func (tx *types.Tx) (int64, error) {
-               gasState, err := validation.ValidateTx(tx.Tx, bcBlock)
-               if err != nil {
-                       return 0, err
-               }
-
-               return gasState.GasUsed, nil
-       }
-}
-
 func blkGenSkipTxForErr(txPool *protocol.TxPool, txHash *bc.Hash, err error) {
        log.WithFields(log.Fields{"module": logModule, "error": err}).Error("mining block generation: skip tx due to")
        txPool.RemoveTransaction(txHash)
index 63eef59..168a9b3 100644 (file)
@@ -21,7 +21,7 @@ const (
 
 type Protocoler interface {
        Name() string
-       BeforeProposalBlock(nodeProgram []byte, gasLeft int64, calcGasUsed func(*types.Tx) (int64, error)) ([]*types.Tx, int64, error)
+       BeforeProposalBlock(nodeProgram []byte, gasLeft int64) ([]*types.Tx, int64, error)
        ChainStatus() (uint64, *bc.Hash, error)
        ValidateBlock(block *types.Block, verifyResults []*bc.TxVerifyResult) error
        ValidateTxs(txs []*types.Tx, verifyResults []*bc.TxVerifyResult) error