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/errors"
17 "github.com/bytom/protocol"
18 "github.com/bytom/protocol/bc"
19 "github.com/bytom/protocol/bc/legacy"
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 *legacy.Tx, err error) {
29 amount += consensus.BlockSubsidy(blockHeight)
30 unlockHeight := blockHeight + consensus.CoinbasePendingBlockNumber
33 if accountManager == nil {
34 script, err = vmutil.CoinbaseProgram(nil, 0, unlockHeight)
36 script, err = accountManager.GetCoinbaseControlProgram(unlockHeight)
42 builder := txbuilder.NewBuilder(time.Now())
43 builder.AddOutput(legacy.NewTxOutput(*consensus.BTMAssetID, amount, script, nil))
44 _, txData, err := builder.Build()
51 Tx: legacy.MapTx(txData),
56 // NewBlockTemplate returns a new block template that is ready to be solved
57 func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager *account.Manager) (*legacy.Block, error) {
58 // Extend the most recently known best block.
60 preBlock, snap := c.State()
61 newSnap := state.Copy(snap)
63 preBcBlock := legacy.MapBlock(preBlock)
64 nextBlockHeight := preBlock.BlockHeader.Height + 1
65 nextBlockSeed := algorithm.CreateSeed(nextBlockHeight, preBcBlock.Seed, []*bc.Hash{&preBcBlock.ID})
66 txDescs := txPool.GetTransactions()
67 txEntries := make([]*bc.Tx, 0, len(txDescs))
68 blockWeight := uint64(0)
71 var compareDiffBH *legacy.BlockHeader
72 if compareDiffBlock, err := c.GetBlockByHeight(nextBlockHeight - consensus.BlocksPerRetarget); err == nil {
73 compareDiffBH = &compareDiffBlock.BlockHeader
77 BlockHeader: legacy.BlockHeader{
79 Height: nextBlockHeight,
80 PreviousBlockHash: preBlock.Hash(),
82 TimestampMS: bc.Millis(time.Now()),
83 BlockCommitment: legacy.BlockCommitment{},
84 Bits: consensus.CalcNextRequiredDifficulty(&preBlock.BlockHeader, compareDiffBH),
86 Transactions: make([]*legacy.Tx, 0, len(txDescs)),
88 newSnap.PruneNonces(b.BlockHeader.TimestampMS)
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 for _, txDesc := range txDescs {
99 if blockWeight+txDesc.Weight > consensus.MaxBlockSzie-consensus.MaxTxSize {
102 if err := newSnap.ApplyTx(tx); err != nil {
103 log.WithField("error", err).Error("mining block generate skip tx due to")
104 txPool.RemoveTransaction(&tx.ID)
107 if _, err := validation.ValidateTx(tx, preBcBlock); err != nil {
108 log.WithField("error", err).Error("mining block generate skip tx due to")
109 txPool.RemoveTransaction(&tx.ID)
113 appendTx(txDesc.Tx, txDesc.Weight, txDesc.Fee)
116 cbTx, err := createCoinbaseTx(accountManager, txFee, nextBlockHeight)
118 return nil, errors.Wrap(err, "fail on createCoinbaseTx")
120 if err := newSnap.ApplyTx(cbTx.Tx); err != nil {
121 return nil, errors.Wrap(err, "fail on append coinbase transaction to snap")
125 b.BlockHeader.BlockCommitment.AssetsMerkleRoot = newSnap.Tree.RootHash()
126 b.BlockHeader.BlockCommitment.TransactionsMerkleRoot, err = bc.MerkleRoot(txEntries)
128 return nil, errors.Wrap(err, "calculating tx merkle root")