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.
12 log "github.com/sirupsen/logrus"
14 "github.com/bytom/account"
15 "github.com/bytom/blockchain/txbuilder"
16 "github.com/bytom/consensus"
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(
44 append([]byte{0x00}, []byte(strconv.FormatUint(blockHeight, 10))...),
45 ), &txbuilder.SigningInstruction{}); err != nil {
48 if err = builder.AddOutput(types.NewTxOutput(*consensus.BTMAssetID, amount, script)); err != nil {
51 _, txData, err := builder.Build()
56 byteData, err := txData.MarshalText()
60 txData.SerializedSize = uint64(len(byteData))
64 Tx: types.MapTx(txData),
69 // NewBlockTemplate returns a new block template that is ready to be solved
70 func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager *account.Manager) (b *types.Block, err error) {
71 view := state.NewUtxoViewpoint()
72 txStatus := bc.NewTransactionStatus()
73 txStatus.SetStatus(0, false)
74 txEntries := []*bc.Tx{nil}
78 // get preblock info for generate next block
79 preBlockHeader := c.BestBlockHeader()
80 preBlockHash := preBlockHeader.Hash()
81 nextBlockHeight := preBlockHeader.Height + 1
82 nextBits, err := c.CalcNextBits(&preBlockHash)
88 BlockHeader: types.BlockHeader{
90 Height: nextBlockHeight,
91 PreviousBlockHash: preBlockHash,
92 Timestamp: uint64(time.Now().Unix()),
93 BlockCommitment: types.BlockCommitment{},
97 bcBlock := &bc.Block{BlockHeader: &bc.BlockHeader{Height: nextBlockHeight}}
98 b.Transactions = []*types.Tx{nil}
100 txs := txPool.GetTransactions()
101 sort.Sort(byTime(txs))
102 for _, txDesc := range txs {
106 if err := c.GetTransactionsUtxo(view, []*bc.Tx{tx}); err != nil {
107 log.WithField("error", err).Error("mining block generate skip tx due to")
108 txPool.RemoveTransaction(&tx.ID)
112 gasStatus, err := validation.ValidateTx(tx, bcBlock)
114 if !gasStatus.GasValid {
115 log.WithField("error", err).Error("mining block generate skip tx due to")
116 txPool.RemoveTransaction(&tx.ID)
122 if gasUsed+uint64(gasStatus.GasUsed) > consensus.MaxBlockGas {
126 if err := view.ApplyTransaction(bcBlock, tx, gasOnlyTx); err != nil {
127 log.WithField("error", err).Error("mining block generate skip tx due to")
128 txPool.RemoveTransaction(&tx.ID)
132 txStatus.SetStatus(len(b.Transactions), gasOnlyTx)
133 b.Transactions = append(b.Transactions, txDesc.Tx)
134 txEntries = append(txEntries, tx)
135 gasUsed += uint64(gasStatus.GasUsed)
138 if gasUsed == consensus.MaxBlockGas {
143 // creater coinbase transaction
144 b.Transactions[0], err = createCoinbaseTx(accountManager, txFee, nextBlockHeight)
146 return nil, errors.Wrap(err, "fail on createCoinbaseTx")
148 txEntries[0] = b.Transactions[0].Tx
150 b.BlockHeader.BlockCommitment.TransactionsMerkleRoot, err = bc.TxMerkleRoot(txEntries)
155 b.BlockHeader.BlockCommitment.TransactionStatusHash, err = bc.TxStatusMerkleRoot(txStatus.VerifyStatus)