OSDN Git Service

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