OSDN Git Service

Merge pull request #99 from Bytom/dev
[bytom/bytom.git] / mining / mining.go
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.
4
5 package mining
6
7 import (
8         "time"
9
10         log "github.com/sirupsen/logrus"
11
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"
22 )
23
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
30
31         var script []byte
32         if accountManager == nil {
33                 script, err = vmutil.CoinbaseProgram(nil, 0, unlockHeight)
34         } else {
35                 script, err = accountManager.GetCoinbaseControlProgram(unlockHeight)
36         }
37         if err != nil {
38                 return
39         }
40
41         builder := txbuilder.NewBuilder(time.Now())
42         builder.AddOutput(legacy.NewTxOutput(*consensus.BTMAssetID, amount, script, nil))
43         _, txData, err := builder.Build()
44         if err != nil {
45                 return
46         }
47
48         tx = &legacy.Tx{
49                 TxData: *txData,
50                 Tx:     legacy.MapTx(txData),
51         }
52         return
53 }
54
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.
58         var err error
59         preBlock, snap := c.State()
60         newSnap := state.Copy(snap)
61
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)
67         txFee := uint64(0)
68
69         var compareDiffBH *legacy.BlockHeader
70         if compareDiffBlock, err := c.GetBlockByHeight(nextBlockHeight - consensus.BlocksPerRetarget); err == nil {
71                 compareDiffBH = &compareDiffBlock.BlockHeader
72         }
73
74         b := &legacy.Block{
75                 BlockHeader: legacy.BlockHeader{
76                         Version:           1,
77                         Height:            nextBlockHeight,
78                         PreviousBlockHash: preBlock.Hash(),
79                         TimestampMS:       bc.Millis(time.Now()),
80                         BlockCommitment:   legacy.BlockCommitment{},
81                         Bits:              consensus.CalcNextRequiredDifficulty(&preBlock.BlockHeader, compareDiffBH),
82                 },
83                 Transactions: make([]*legacy.Tx, 0, len(txDescs)),
84         }
85         newSnap.PruneNonces(b.BlockHeader.TimestampMS)
86
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...)
90                 blockWeight += weight
91                 txFee += fee
92         }
93
94         for _, txDesc := range txDescs {
95                 tx := txDesc.Tx.Tx
96                 if blockWeight+txDesc.Weight > consensus.MaxBlockSzie-consensus.MaxTxSize {
97                         break
98                 }
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)
102                         continue
103                 }
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)
107                         continue
108                 }
109
110                 appendTx(txDesc.Tx, txDesc.Weight, txDesc.Fee)
111         }
112
113         cbTx, err := createCoinbaseTx(accountManager, txFee, nextBlockHeight)
114         if err != nil {
115                 return nil, errors.Wrap(err, "fail on createCoinbaseTx")
116         }
117         if err := newSnap.ApplyTx(cbTx.Tx); err != nil {
118                 return nil, errors.Wrap(err, "fail on append coinbase transaction to snap")
119         }
120         appendTx(cbTx, 0, 0)
121
122         b.BlockHeader.BlockCommitment.AssetsMerkleRoot = newSnap.Tree.RootHash()
123         b.BlockHeader.BlockCommitment.TransactionsMerkleRoot, err = bc.MerkleRoot(txEntries)
124         if err != nil {
125                 return nil, errors.Wrap(err, "calculating tx merkle root")
126         }
127
128         return b, nil
129 }