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/errors"
17 "github.com/bytom/protocol"
18 "github.com/bytom/protocol/bc"
19 "github.com/bytom/protocol/bc/types"
20 "github.com/bytom/protocol/state"
21 "github.com/bytom/protocol/validation"
22 "github.com/bytom/protocol/vm/vmutil"
25 // createCoinbaseTx returns a coinbase transaction paying an appropriate subsidy
26 // based on the passed block height to the provided address. When the address
27 // is nil, the coinbase transaction will instead be redeemable by anyone.
28 func createCoinbaseTx(accountManager *account.Manager, amount uint64, blockHeight uint64) (tx *types.Tx, err error) {
29 amount += consensus.BlockSubsidy(blockHeight)
32 if accountManager == nil {
33 script, err = vmutil.DefaultCoinbaseProgram()
35 script, err = accountManager.GetCoinbaseControlProgram()
41 builder := txbuilder.NewBuilder(time.Now())
42 if err = builder.AddInput(types.NewCoinbaseInput([]byte(string(blockHeight))), &txbuilder.SigningInstruction{}); err != nil {
45 if err = builder.AddOutput(types.NewTxOutput(*consensus.BTMAssetID, amount, script)); err != nil {
48 _, txData, err := builder.Build()
55 Tx: types.MapTx(txData),
60 // NewBlockTemplate returns a new block template that is ready to be solved
61 func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager *account.Manager) (b *types.Block, err error) {
62 view := state.NewUtxoViewpoint()
63 txStatus := bc.NewTransactionStatus()
64 txStatus.SetStatus(0, false)
65 txEntries := []*bc.Tx{nil}
69 // get preblock info for generate next block
70 preBlockHeader := c.BestBlockHeader()
71 preBlockHash := preBlockHeader.Hash()
72 nextBlockHeight := preBlockHeader.Height + 1
73 nextBits, err := c.CalcNextBits(&preBlockHash)
79 BlockHeader: types.BlockHeader{
81 Height: nextBlockHeight,
82 PreviousBlockHash: preBlockHash,
83 Timestamp: uint64(time.Now().Unix()),
84 BlockCommitment: types.BlockCommitment{},
88 bcBlock := &bc.Block{BlockHeader: &bc.BlockHeader{Height: nextBlockHeight}}
89 b.Transactions = []*types.Tx{nil}
91 txs := txPool.GetTransactions()
92 sort.Sort(ByTime(txs))
93 for _, txDesc := range txs {
97 if err := c.GetTransactionsUtxo(view, []*bc.Tx{tx}); err != nil {
98 log.WithField("error", err).Error("mining block generate skip tx due to")
99 txPool.RemoveTransaction(&tx.ID)
103 gasStatus, err := validation.ValidateTx(tx, bcBlock)
105 if !gasStatus.GasVaild {
106 log.WithField("error", err).Error("mining block generate skip tx due to")
107 txPool.RemoveTransaction(&tx.ID)
113 if gasUsed+uint64(gasStatus.GasUsed) > consensus.MaxBlockGas {
117 if err := view.ApplyTransaction(bcBlock, tx, gasOnlyTx); err != nil {
118 log.WithField("error", err).Error("mining block generate skip tx due to")
119 txPool.RemoveTransaction(&tx.ID)
123 txStatus.SetStatus(len(b.Transactions), gasOnlyTx)
124 b.Transactions = append(b.Transactions, txDesc.Tx)
125 txEntries = append(txEntries, tx)
126 gasUsed += uint64(gasStatus.GasUsed)
129 if gasUsed == consensus.MaxBlockGas {
134 // creater coinbase transaction
135 b.Transactions[0], err = createCoinbaseTx(accountManager, txFee, nextBlockHeight)
137 return nil, errors.Wrap(err, "fail on createCoinbaseTx")
139 txEntries[0] = b.Transactions[0].Tx
141 b.BlockHeader.BlockCommitment.TransactionsMerkleRoot, err = bc.TxMerkleRoot(txEntries)
146 b.BlockHeader.BlockCommitment.TransactionStatusHash, err = bc.TxStatusMerkleRoot(txStatus.VerifyStatus)