OSDN Git Service

Merge branch 'dev' into dev-verify
[bytom/bytom.git] / mining / mining.go
index 0bc8009..e85326e 100644 (file)
@@ -10,14 +10,13 @@ import (
 
        log "github.com/sirupsen/logrus"
 
-       "github.com/bytom/blockchain/account"
+       "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"
-       "github.com/bytom/protocol/bc/legacy"
+       "github.com/bytom/protocol/bc/types"
        "github.com/bytom/protocol/state"
        "github.com/bytom/protocol/validation"
        "github.com/bytom/protocol/vm/vmutil"
@@ -26,7 +25,7 @@ import (
 // createCoinbaseTx returns a coinbase transaction paying an appropriate subsidy
 // based on the passed block height to the provided address.  When the address
 // is nil, the coinbase transaction will instead be redeemable by anyone.
-func createCoinbaseTx(accountManager *account.Manager, amount uint64, blockHeight uint64) (tx *legacy.Tx, err error) {
+func createCoinbaseTx(accountManager *account.Manager, amount uint64, blockHeight uint64) (tx *types.Tx, err error) {
        amount += consensus.BlockSubsidy(blockHeight)
 
        var script []byte
@@ -40,10 +39,10 @@ func createCoinbaseTx(accountManager *account.Manager, amount uint64, blockHeigh
        }
 
        builder := txbuilder.NewBuilder(time.Now())
-       if err = builder.AddInput(legacy.NewCoinbaseInput([]byte(string(blockHeight))), &txbuilder.SigningInstruction{}); err != nil {
+       if err = builder.AddInput(types.NewCoinbaseInput([]byte(string(blockHeight))), &txbuilder.SigningInstruction{}); err != nil {
                return
        }
-       if err = builder.AddOutput(legacy.NewTxOutput(*consensus.BTMAssetID, amount, script)); err != nil {
+       if err = builder.AddOutput(types.NewTxOutput(*consensus.BTMAssetID, amount, script)); err != nil {
                return
        }
        _, txData, err := builder.Build()
@@ -51,43 +50,49 @@ func createCoinbaseTx(accountManager *account.Manager, amount uint64, blockHeigh
                return
        }
 
-       tx = &legacy.Tx{
+       byteData, err := txData.MarshalText()
+       if err != nil {
+               return
+       }
+       txData.SerializedSize = uint64(len(byteData))
+
+       tx = &types.Tx{
                TxData: *txData,
-               Tx:     legacy.MapTx(txData),
+               Tx:     types.MapTx(txData),
        }
        return
 }
 
 // NewBlockTemplate returns a new block template that is ready to be solved
-func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager *account.Manager) (b *legacy.Block, err error) {
+func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager *account.Manager) (b *types.Block, err error) {
        view := state.NewUtxoViewpoint()
        txStatus := bc.NewTransactionStatus()
+       txStatus.SetStatus(0, false)
        txEntries := []*bc.Tx{nil}
        gasUsed := uint64(0)
        txFee := uint64(0)
 
        // get preblock info for generate next block
-       preBlock := c.BestBlock()
-       preBcBlock := legacy.MapBlock(preBlock)
-       nextBlockHeight := preBlock.BlockHeader.Height + 1
-
-       var compareDiffBH *legacy.BlockHeader
-       if compareDiffBlock, err := c.GetBlockByHeight(nextBlockHeight - 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 = &legacy.Block{
-               BlockHeader: legacy.BlockHeader{
+       b = &types.Block{
+               BlockHeader: types.BlockHeader{
                        Version:           1,
                        Height:            nextBlockHeight,
-                       PreviousBlockHash: preBlock.Hash(),
+                       PreviousBlockHash: preBlockHash,
                        Timestamp:         uint64(time.Now().Unix()),
-                       BlockCommitment:   legacy.BlockCommitment{},
-                       Bits:              difficulty.CalcNextRequiredDifficulty(&preBlock.BlockHeader, compareDiffBH),
+                       BlockCommitment:   types.BlockCommitment{},
+                       Bits:              nextBits,
                },
-               Transactions: []*legacy.Tx{nil},
        }
        bcBlock := &bc.Block{BlockHeader: &bc.BlockHeader{Height: nextBlockHeight}}
+       b.Transactions = []*types.Tx{nil}
 
        txs := txPool.GetTransactions()
        sort.Sort(ByTime(txs))
@@ -101,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
@@ -139,7 +144,11 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
        }
        txEntries[0] = b.Transactions[0].Tx
 
-       b.BlockHeader.BlockCommitment.TransactionsMerkleRoot, err = bc.MerkleRoot(txEntries)
-       b.BlockHeader.BlockCommitment.TransactionStatusHash = bc.EntryID(txStatus)
+       b.BlockHeader.BlockCommitment.TransactionsMerkleRoot, err = bc.TxMerkleRoot(txEntries)
+       if err != nil {
+               return nil, err
+       }
+
+       b.BlockHeader.BlockCommitment.TransactionStatusHash, err = bc.TxStatusMerkleRoot(txStatus.VerifyStatus)
        return b, err
 }