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.
10 log "github.com/sirupsen/logrus"
12 "github.com/bytom/blockchain/account"
13 "github.com/bytom/blockchain/txbuilder"
14 "github.com/bytom/consensus"
15 "github.com/bytom/consensus/algorithm"
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/legacy"
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 *legacy.Tx, err error) {
30 amount += consensus.BlockSubsidy(blockHeight)
31 unlockHeight := blockHeight + consensus.CoinbasePendingBlockNumber
34 if accountManager == nil {
35 script, err = vmutil.CoinbaseProgram(nil, 0, unlockHeight)
37 script, err = accountManager.GetCoinbaseControlProgram(unlockHeight)
43 builder := txbuilder.NewBuilder(time.Now())
44 builder.AddOutput(legacy.NewTxOutput(*consensus.BTMAssetID, amount, script, nil))
45 _, txData, err := builder.Build()
52 Tx: legacy.MapTx(txData),
57 // NewBlockTemplate returns a new block template that is ready to be solved
58 func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager *account.Manager) (*legacy.Block, error) {
59 // Extend the most recently known best block.
61 preBlock := c.BestBlock()
62 view := state.NewUtxoViewpoint()
64 preBcBlock := legacy.MapBlock(preBlock)
65 nextBlockHeight := preBlock.BlockHeader.Height + 1
66 nextBlockSeed := algorithm.CreateSeed(nextBlockHeight, preBcBlock.Seed, []*bc.Hash{&preBcBlock.ID})
67 txDescs := txPool.GetTransactions()
68 txEntries := make([]*bc.Tx, 0, len(txDescs))
69 blockWeight := uint64(0)
72 var compareDiffBH *legacy.BlockHeader
73 if compareDiffBlock, err := c.GetBlockByHeight(nextBlockHeight - consensus.BlocksPerRetarget); err == nil {
74 compareDiffBH = &compareDiffBlock.BlockHeader
78 BlockHeader: legacy.BlockHeader{
80 Height: nextBlockHeight,
81 PreviousBlockHash: preBlock.Hash(),
83 TimestampMS: bc.Millis(time.Now()),
84 BlockCommitment: legacy.BlockCommitment{},
85 Bits: difficulty.CalcNextRequiredDifficulty(&preBlock.BlockHeader, compareDiffBH),
87 Transactions: make([]*legacy.Tx, 0, len(txDescs)),
90 appendTx := func(tx *legacy.Tx, weight, fee uint64) {
91 b.Transactions = append([]*legacy.Tx{tx}, b.Transactions...)
92 txEntries = append([]*bc.Tx{tx.Tx}, txEntries...)
97 bcBlock := legacy.MapBlock(b)
98 for _, txDesc := range txDescs {
100 if blockWeight+txDesc.Weight > consensus.MaxBlockSzie-consensus.MaxTxSize {
103 if err := c.GetTransactionsUtxo(view, []*bc.Tx{tx}); err != nil {
104 log.WithField("error", err).Error("mining block generate skip tx due to")
105 txPool.RemoveTransaction(&tx.ID)
108 if err := view.ApplyTransaction(bcBlock, tx); err != nil {
109 log.WithField("error", err).Error("mining block generate skip tx due to")
110 txPool.RemoveTransaction(&tx.ID)
113 if _, err := validation.ValidateTx(tx, preBcBlock); err != nil {
114 log.WithField("error", err).Error("mining block generate skip tx due to")
115 txPool.RemoveTransaction(&tx.ID)
119 appendTx(txDesc.Tx, txDesc.Weight, txDesc.Fee)
122 cbTx, err := createCoinbaseTx(accountManager, txFee, nextBlockHeight)
124 return nil, errors.Wrap(err, "fail on createCoinbaseTx")
128 b.BlockHeader.BlockCommitment.TransactionsMerkleRoot, err = bc.MerkleRoot(txEntries)
130 return nil, errors.Wrap(err, "calculating tx merkle root")