OSDN Git Service

Tx validate mining (#237)
[bytom/vapor.git] / proposal / proposal.go
index 364d9ff..befa559 100644 (file)
@@ -69,7 +69,7 @@ func createCoinbaseTx(accountManager *account.Manager, amount uint64, blockHeigh
 }
 
 // NewBlockTemplate returns a new block template that is ready to be solved
-func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager *account.Manager) (b *types.Block, err error) {
+func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager *account.Manager, timestamp uint64) (b *types.Block, err error) {
        view := state.NewUtxoViewpoint()
        txStatus := bc.NewTransactionStatus()
        if err := txStatus.SetStatus(0, false); err != nil {
@@ -89,8 +89,9 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
                        Version:           1,
                        Height:            nextBlockHeight,
                        PreviousBlockHash: preBlockHash,
-                       Timestamp:         uint64(time.Now().UnixNano() / int64(time.Millisecond)),
+                       Timestamp:         timestamp,
                        BlockCommitment:   types.BlockCommitment{},
+                       BlockWitness:      types.BlockWitness{Witness: make([][]byte, consensus.NumOfConsensusNode)},
                },
        }
        bcBlock := &bc.Block{BlockHeader: &bc.BlockHeader{Height: nextBlockHeight}}
@@ -98,17 +99,20 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
 
        txs := txPool.GetTransactions()
        sort.Sort(byTime(txs))
+
+       entriesTxs := []*bc.Tx{}
        for _, txDesc := range txs {
+               entriesTxs = append(entriesTxs, txDesc.Tx.Tx)
+       }
+
+       validateResults := validation.ValidateTxs(entriesTxs, bcBlock)
+       for i, validateResult := range validateResults {
+               txDesc := txs[i]
                tx := txDesc.Tx.Tx
                gasOnlyTx := false
 
-               if err := c.GetTransactionsUtxo(view, []*bc.Tx{tx}); err != nil {
-                       blkGenSkipTxForErr(txPool, &tx.ID, err)
-                       continue
-               }
-
-               gasStatus, err := validation.ValidateTx(tx, bcBlock)
-               if err != nil {
+               gasStatus := validateResult.GetGasState()
+               if validateResult.GetError() != nil {
                        if !gasStatus.GasValid {
                                blkGenSkipTxForErr(txPool, &tx.ID, err)
                                continue
@@ -116,6 +120,11 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
                        gasOnlyTx = true
                }
 
+               if err := c.GetTransactionsUtxo(view, []*bc.Tx{tx}); err != nil {
+                       blkGenSkipTxForErr(txPool, &tx.ID, err)
+                       continue
+               }
+
                if gasUsed+uint64(gasStatus.GasUsed) > consensus.MaxBlockGas {
                        break
                }
@@ -137,6 +146,7 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
                if gasUsed == consensus.MaxBlockGas {
                        break
                }
+
        }
 
        // creater coinbase transaction
@@ -153,7 +163,7 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
 
        b.BlockHeader.BlockCommitment.TransactionStatusHash, err = types.TxStatusMerkleRoot(txStatus.VerifyStatus)
 
-       _, err = c.GetBBFT().SignBlock(b)
+       _, err = c.SignBlock(b)
        return b, err
 }