1 // Copyright (c) 2014-2016 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
11 log "github.com/sirupsen/logrus"
13 "github.com/bytom/account"
14 "github.com/bytom/blockchain/txbuilder"
15 "github.com/bytom/consensus"
16 "github.com/bytom/consensus/difficulty"
17 "github.com/bytom/errors"
18 "github.com/bytom/protocol"
19 "github.com/bytom/protocol/bc"
20 "github.com/bytom/protocol/bc/types"
21 "github.com/bytom/protocol/state"
22 "github.com/bytom/protocol/validation"
23 "github.com/bytom/protocol/vm/vmutil"
26 // createCoinbaseTx returns a coinbase transaction paying an appropriate subsidy
27 // based on the passed block height to the provided address. When the address
28 // is nil, the coinbase transaction will instead be redeemable by anyone.
29 func createCoinbaseTx(accountManager *account.Manager, amount uint64, blockHeight uint64) (tx *types.Tx, err error) {
30 amount += consensus.BlockSubsidy(blockHeight)
33 if accountManager == nil {
34 script, err = vmutil.DefaultCoinbaseProgram()
36 script, err = accountManager.GetCoinbaseControlProgram()
42 builder := txbuilder.NewBuilder(time.Now())
43 if err = builder.AddInput(types.NewCoinbaseInput([]byte(string(blockHeight))), &txbuilder.SigningInstruction{}); err != nil {
46 if err = builder.AddOutput(types.NewTxOutput(*consensus.BTMAssetID, amount, script)); err != nil {
49 _, txData, err := builder.Build()
56 Tx: types.MapTx(txData),
61 // NewBlockTemplate returns a new block template that is ready to be solved
62 func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager *account.Manager) (b *types.Block, err error) {
63 view := state.NewUtxoViewpoint()
64 txStatus := bc.NewTransactionStatus()
65 txEntries := []*bc.Tx{nil}
69 // get preblock info for generate next block
70 preBlock := c.BestBlock()
71 preBcBlock := types.MapBlock(preBlock)
72 nextBlockHeight := preBlock.BlockHeader.Height + 1
74 var compareDiffBH *types.BlockHeader
75 if compareDiffBlock, err := c.GetBlockByHeight(nextBlockHeight - consensus.BlocksPerRetarget); err == nil {
76 compareDiffBH = &compareDiffBlock.BlockHeader
80 BlockHeader: types.BlockHeader{
82 Height: nextBlockHeight,
83 PreviousBlockHash: preBlock.Hash(),
84 Timestamp: uint64(time.Now().Unix()),
85 BlockCommitment: types.BlockCommitment{},
86 Bits: difficulty.CalcNextRequiredDifficulty(&preBlock.BlockHeader, compareDiffBH),
88 Transactions: []*types.Tx{nil},
90 bcBlock := &bc.Block{BlockHeader: &bc.BlockHeader{Height: nextBlockHeight}}
92 txs := txPool.GetTransactions()
93 sort.Sort(ByTime(txs))
94 for _, txDesc := range txs {
98 if err := c.GetTransactionsUtxo(view, []*bc.Tx{tx}); err != nil {
99 log.WithField("error", err).Error("mining block generate skip tx due to")
100 txPool.RemoveTransaction(&tx.ID)
104 gasStatus, err := validation.ValidateTx(tx, preBcBlock)
106 if !gasStatus.GasVaild {
107 log.WithField("error", err).Error("mining block generate skip tx due to")
108 txPool.RemoveTransaction(&tx.ID)
114 if gasUsed+uint64(gasStatus.GasUsed) > consensus.MaxBlockGas {
118 if err := view.ApplyTransaction(bcBlock, tx, gasOnlyTx); err != nil {
119 log.WithField("error", err).Error("mining block generate skip tx due to")
120 txPool.RemoveTransaction(&tx.ID)
124 txStatus.SetStatus(len(b.Transactions), gasOnlyTx)
125 b.Transactions = append(b.Transactions, txDesc.Tx)
126 txEntries = append(txEntries, tx)
127 gasUsed += uint64(gasStatus.GasUsed)
130 if gasUsed == consensus.MaxBlockGas {
135 // creater coinbase transaction
136 b.Transactions[0], err = createCoinbaseTx(accountManager, txFee, nextBlockHeight)
138 return nil, errors.Wrap(err, "fail on createCoinbaseTx")
140 txEntries[0] = b.Transactions[0].Tx
142 b.BlockHeader.BlockCommitment.TransactionsMerkleRoot, err = bc.MerkleRoot(txEntries)
143 b.BlockHeader.BlockCommitment.TransactionStatusHash = bc.EntryID(txStatus)