OSDN Git Service

Merge branch 'dev' into dev-verify
[bytom/bytom.git] / mining / mining.go
index adb474a..e85326e 100644 (file)
@@ -13,7 +13,6 @@ import (
        "github.com/bytom/account"
        "github.com/bytom/blockchain/txbuilder"
        "github.com/bytom/consensus"
-       "github.com/bytom/consensus/difficulty"
        "github.com/bytom/errors"
        "github.com/bytom/protocol"
        "github.com/bytom/protocol/bc"
@@ -51,6 +50,12 @@ func createCoinbaseTx(accountManager *account.Manager, amount uint64, blockHeigh
                return
        }
 
+       byteData, err := txData.MarshalText()
+       if err != nil {
+               return
+       }
+       txData.SerializedSize = uint64(len(byteData))
+
        tx = &types.Tx{
                TxData: *txData,
                Tx:     types.MapTx(txData),
@@ -68,27 +73,26 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
        txFee := uint64(0)
 
        // get preblock info for generate next block
-       preBlock := c.BestBlock()
-       preBcBlock := types.MapBlock(preBlock)
-       nextBlockHeight := preBlock.Height + 1
-
-       var compareDiffBH *types.BlockHeader
-       if compareDiffBlock, err := c.GetBlockByHeight(preBlock.Height - consensus.BlocksPerRetarget); err == nil {
-               compareDiffBH = &compareDiffBlock.BlockHeader
+       preBlockHeader := c.BestBlockHeader()
+       preBlockHash := preBlockHeader.Hash()
+       nextBlockHeight := preBlockHeader.Height + 1
+       nextBits, err := c.CalcNextBits(&preBlockHash)
+       if err != nil {
+               return nil, err
        }
 
        b = &types.Block{
                BlockHeader: types.BlockHeader{
                        Version:           1,
                        Height:            nextBlockHeight,
-                       PreviousBlockHash: preBlock.Hash(),
+                       PreviousBlockHash: preBlockHash,
                        Timestamp:         uint64(time.Now().Unix()),
                        BlockCommitment:   types.BlockCommitment{},
-                       Bits:              difficulty.CalcNextRequiredDifficulty(&preBlock.BlockHeader, compareDiffBH),
+                       Bits:              nextBits,
                },
-               Transactions: []*types.Tx{nil},
        }
        bcBlock := &bc.Block{BlockHeader: &bc.BlockHeader{Height: nextBlockHeight}}
+       b.Transactions = []*types.Tx{nil}
 
        txs := txPool.GetTransactions()
        sort.Sort(ByTime(txs))
@@ -102,9 +106,9 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
                        continue
                }
 
-               gasStatus, err := validation.ValidateTx(tx, preBcBlock)
+               gasStatus, err := validation.ValidateTx(tx, bcBlock)
                if err != nil {
-                       if !gasStatus.GasVaild {
+                       if !gasStatus.GasValid {
                                log.WithField("error", err).Error("mining block generate skip tx due to")
                                txPool.RemoveTransaction(&tx.ID)
                                continue