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/errors"
16 "github.com/bytom/protocol"
17 "github.com/bytom/protocol/bc"
18 "github.com/bytom/protocol/bc/legacy"
19 "github.com/bytom/protocol/state"
20 "github.com/bytom/protocol/validation"
21 "github.com/bytom/protocol/vm/vmutil"
24 // createCoinbaseTx returns a coinbase transaction paying an appropriate subsidy
25 // based on the passed block height to the provided address. When the address
26 // is nil, the coinbase transaction will instead be redeemable by anyone.
27 func createCoinbaseTx(accountManager *account.Manager, amount uint64, blockHeight uint64) (tx *legacy.Tx, err error) {
28 amount += consensus.BlockSubsidy(blockHeight)
29 unlockHeight := blockHeight + consensus.CoinbasePendingBlockNumber
32 if accountManager == nil {
33 script, err = vmutil.CoinbaseProgram(nil, 0, unlockHeight)
35 script, err = accountManager.GetCoinbaseControlProgram(unlockHeight)
41 builder := txbuilder.NewBuilder(time.Now())
42 builder.AddOutput(legacy.NewTxOutput(*consensus.BTMAssetID, amount, script, nil))
43 _, txData, err := builder.Build()
50 Tx: legacy.MapTx(txData),
55 // NewBlockTemplate returns a new block template that is ready to be solved
56 func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager *account.Manager) (*legacy.Block, error) {
57 // Extend the most recently known best block.
59 preBlock, snap := c.State()
60 newSnap := state.Copy(snap)
62 nextBlockHeight := preBlock.BlockHeader.Height + 1
63 preBcBlock := legacy.MapBlock(preBlock)
64 txDescs := txPool.GetTransactions()
65 txEntries := make([]*bc.Tx, 0, len(txDescs))
66 blockWeight := uint64(0)
69 var compareDiffBH *legacy.BlockHeader
70 if compareDiffBlock, err := c.GetBlockByHeight(nextBlockHeight - consensus.BlocksPerRetarget); err == nil {
71 compareDiffBH = &compareDiffBlock.BlockHeader
75 BlockHeader: legacy.BlockHeader{
77 Height: nextBlockHeight,
78 PreviousBlockHash: preBlock.Hash(),
79 TimestampMS: bc.Millis(time.Now()),
80 BlockCommitment: legacy.BlockCommitment{},
81 Bits: consensus.CalcNextRequiredDifficulty(&preBlock.BlockHeader, compareDiffBH),
83 Transactions: make([]*legacy.Tx, 0, len(txDescs)),
85 newSnap.PruneNonces(b.BlockHeader.TimestampMS)
87 appendTx := func(tx *legacy.Tx, weight, fee uint64) {
88 b.Transactions = append([]*legacy.Tx{tx}, b.Transactions...)
89 txEntries = append([]*bc.Tx{tx.Tx}, txEntries...)
94 for _, txDesc := range txDescs {
96 if blockWeight+txDesc.Weight > consensus.MaxBlockSzie-consensus.MaxTxSize {
99 if err := newSnap.ApplyTx(tx); err != nil {
100 log.WithField("error", err).Error("mining block generate skip tx due to")
101 txPool.RemoveTransaction(&tx.ID)
104 if _, err := validation.ValidateTx(tx, preBcBlock); err != nil {
105 log.WithField("error", err).Error("mining block generate skip tx due to")
106 txPool.RemoveTransaction(&tx.ID)
110 appendTx(txDesc.Tx, txDesc.Weight, txDesc.Fee)
113 cbTx, err := createCoinbaseTx(accountManager, txFee, nextBlockHeight)
115 return nil, errors.Wrap(err, "fail on createCoinbaseTx")
117 if err := newSnap.ApplyTx(cbTx.Tx); err != nil {
118 return nil, errors.Wrap(err, "fail on append coinbase transaction to snap")
122 b.BlockHeader.BlockCommitment.AssetsMerkleRoot = newSnap.Tree.RootHash()
123 b.BlockHeader.BlockCommitment.TransactionsMerkleRoot, err = bc.MerkleRoot(txEntries)
125 return nil, errors.Wrap(err, "calculating tx merkle root")