OSDN Git Service

Merge pull request #1829 from Bytom/dashboard
[bytom/bytom.git] / mining / mining.go
index e85326e..fa736d3 100644 (file)
@@ -1,58 +1,63 @@
-// Copyright (c) 2014-2016 The btcsuite developers
-// Use of this source code is governed by an ISC
-// license that can be found in the LICENSE file.
-
 package mining
 
 import (
        "sort"
+       "strconv"
        "time"
 
        log "github.com/sirupsen/logrus"
 
-       "github.com/bytom/account"
-       "github.com/bytom/blockchain/txbuilder"
-       "github.com/bytom/consensus"
-       "github.com/bytom/errors"
-       "github.com/bytom/protocol"
-       "github.com/bytom/protocol/bc"
-       "github.com/bytom/protocol/bc/types"
-       "github.com/bytom/protocol/state"
-       "github.com/bytom/protocol/validation"
-       "github.com/bytom/protocol/vm/vmutil"
+       "github.com/bytom/bytom/account"
+       "github.com/bytom/bytom/blockchain/txbuilder"
+       "github.com/bytom/bytom/consensus"
+       "github.com/bytom/bytom/errors"
+       "github.com/bytom/bytom/protocol"
+       "github.com/bytom/bytom/protocol/bc"
+       "github.com/bytom/bytom/protocol/bc/types"
+       "github.com/bytom/bytom/protocol/state"
+       "github.com/bytom/bytom/protocol/validation"
+       "github.com/bytom/bytom/protocol/vm/vmutil"
 )
 
+const logModule = "mining"
+
 // 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 *types.Tx, err error) {
        amount += consensus.BlockSubsidy(blockHeight)
+       arbitrary := append([]byte{0x00}, []byte(strconv.FormatUint(blockHeight, 10))...)
 
        var script []byte
        if accountManager == nil {
                script, err = vmutil.DefaultCoinbaseProgram()
        } else {
                script, err = accountManager.GetCoinbaseControlProgram()
+               arbitrary = append(arbitrary, accountManager.GetCoinbaseArbitrary()...)
        }
        if err != nil {
-               return
+               return nil, err
+       }
+
+       if len(arbitrary) > consensus.CoinbaseArbitrarySizeLimit {
+               return nil, validation.ErrCoinbaseArbitraryOversize
        }
 
        builder := txbuilder.NewBuilder(time.Now())
-       if err = builder.AddInput(types.NewCoinbaseInput([]byte(string(blockHeight))), &txbuilder.SigningInstruction{}); err != nil {
-               return
+       if err = builder.AddInput(types.NewCoinbaseInput(arbitrary), &txbuilder.SigningInstruction{}); err != nil {
+               return nil, err
        }
        if err = builder.AddOutput(types.NewTxOutput(*consensus.BTMAssetID, amount, script)); err != nil {
-               return
+               return nil, err
        }
        _, txData, err := builder.Build()
        if err != nil {
-               return
+               return nil, err
        }
 
        byteData, err := txData.MarshalText()
        if err != nil {
-               return
+               return nil, err
        }
        txData.SerializedSize = uint64(len(byteData))
 
@@ -60,14 +65,16 @@ func createCoinbaseTx(accountManager *account.Manager, amount uint64, blockHeigh
                TxData: *txData,
                Tx:     types.MapTx(txData),
        }
-       return
+       return tx, nil
 }
 
 // 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) {
        view := state.NewUtxoViewpoint()
        txStatus := bc.NewTransactionStatus()
-       txStatus.SetStatus(0, false)
+       if err := txStatus.SetStatus(0, false); err != nil {
+               return nil, err
+       }
        txEntries := []*bc.Tx{nil}
        gasUsed := uint64(0)
        txFee := uint64(0)
@@ -95,22 +102,20 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
        b.Transactions = []*types.Tx{nil}
 
        txs := txPool.GetTransactions()
-       sort.Sort(ByTime(txs))
+       sort.Sort(byTime(txs))
        for _, txDesc := range txs {
                tx := txDesc.Tx.Tx
                gasOnlyTx := false
 
                if err := c.GetTransactionsUtxo(view, []*bc.Tx{tx}); err != nil {
-                       log.WithField("error", err).Error("mining block generate skip tx due to")
-                       txPool.RemoveTransaction(&tx.ID)
+                       blkGenSkipTxForErr(txPool, &tx.ID, err)
                        continue
                }
 
                gasStatus, err := validation.ValidateTx(tx, bcBlock)
                if err != nil {
                        if !gasStatus.GasValid {
-                               log.WithField("error", err).Error("mining block generate skip tx due to")
-                               txPool.RemoveTransaction(&tx.ID)
+                               blkGenSkipTxForErr(txPool, &tx.ID, err)
                                continue
                        }
                        gasOnlyTx = true
@@ -121,12 +126,14 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
                }
 
                if err := view.ApplyTransaction(bcBlock, tx, gasOnlyTx); err != nil {
-                       log.WithField("error", err).Error("mining block generate skip tx due to")
-                       txPool.RemoveTransaction(&tx.ID)
+                       blkGenSkipTxForErr(txPool, &tx.ID, err)
                        continue
                }
 
-               txStatus.SetStatus(len(b.Transactions), gasOnlyTx)
+               if err := txStatus.SetStatus(len(b.Transactions), gasOnlyTx); err != nil {
+                       return nil, err
+               }
+
                b.Transactions = append(b.Transactions, txDesc.Tx)
                txEntries = append(txEntries, tx)
                gasUsed += uint64(gasStatus.GasUsed)
@@ -144,11 +151,16 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
        }
        txEntries[0] = b.Transactions[0].Tx
 
-       b.BlockHeader.BlockCommitment.TransactionsMerkleRoot, err = bc.TxMerkleRoot(txEntries)
+       b.BlockHeader.BlockCommitment.TransactionsMerkleRoot, err = types.TxMerkleRoot(txEntries)
        if err != nil {
                return nil, err
        }
 
-       b.BlockHeader.BlockCommitment.TransactionStatusHash, err = bc.TxStatusMerkleRoot(txStatus.VerifyStatus)
+       b.BlockHeader.BlockCommitment.TransactionStatusHash, err = types.TxStatusMerkleRoot(txStatus.VerifyStatus)
        return b, err
 }
+
+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)
+}