-// Copyright (c) 2014-2016 The btcsuite developers
-// Use of this source code is governed by an ISC
-// license that can be found in the LICENSE file.
-
package mining
import (
+ "sort"
+ "strconv"
"time"
log "github.com/sirupsen/logrus"
- "github.com/bytom/blockchain/account"
+ "github.com/bytom/account"
"github.com/bytom/blockchain/txbuilder"
"github.com/bytom/consensus"
- "github.com/bytom/consensus/algorithm"
- "github.com/bytom/consensus/difficulty"
"github.com/bytom/errors"
"github.com/bytom/protocol"
"github.com/bytom/protocol/bc"
- "github.com/bytom/protocol/bc/legacy"
+ "github.com/bytom/protocol/bc/types"
"github.com/bytom/protocol/state"
"github.com/bytom/protocol/validation"
"github.com/bytom/protocol/vm/vmutil"
// createCoinbaseTx returns a coinbase transaction paying an appropriate subsidy
// based on the passed block height to the provided address. When the address
// is nil, the coinbase transaction will instead be redeemable by anyone.
-func createCoinbaseTx(accountManager *account.Manager, amount uint64, blockHeight uint64) (tx *legacy.Tx, err error) {
+func createCoinbaseTx(accountManager *account.Manager, amount uint64, blockHeight uint64) (tx *types.Tx, err error) {
amount += consensus.BlockSubsidy(blockHeight)
+ arbitrary := append([]byte{0x00}, []byte(strconv.FormatUint(blockHeight, 10))...)
var script []byte
if accountManager == nil {
script, err = vmutil.DefaultCoinbaseProgram()
} else {
script, err = accountManager.GetCoinbaseControlProgram()
+ arbitrary = append(arbitrary, accountManager.GetCoinbaseArbitrary()...)
}
if err != nil {
- return
+ return nil, err
+ }
+
+ if len(arbitrary) > consensus.CoinbaseArbitrarySizeLimit {
+ return nil, validation.ErrCoinbaseArbitraryOversize
}
builder := txbuilder.NewBuilder(time.Now())
- if err = builder.AddInput(legacy.NewCoinbaseInput([]byte(string(blockHeight)), nil), &txbuilder.SigningInstruction{}); err != nil {
- return
+ if err = builder.AddInput(types.NewCoinbaseInput(arbitrary), &txbuilder.SigningInstruction{}); err != nil {
+ return nil, err
}
- if err = builder.AddOutput(legacy.NewTxOutput(*consensus.BTMAssetID, amount, script, nil)); err != nil {
- return
+ if err = builder.AddOutput(types.NewTxOutput(*consensus.BTMAssetID, amount, script)); err != nil {
+ return nil, err
}
_, txData, err := builder.Build()
if err != nil {
- return
+ return nil, err
}
- tx = &legacy.Tx{
+ byteData, err := txData.MarshalText()
+ if err != nil {
+ return nil, err
+ }
+ txData.SerializedSize = uint64(len(byteData))
+
+ tx = &types.Tx{
TxData: *txData,
- Tx: legacy.MapTx(txData),
+ Tx: types.MapTx(txData),
}
- return
+ return tx, nil
}
// NewBlockTemplate returns a new block template that is ready to be solved
-func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager *account.Manager) (*legacy.Block, error) {
- // Extend the most recently known best block.
- var err error
- preBlock := c.BestBlock()
+func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager *account.Manager) (b *types.Block, err error) {
view := state.NewUtxoViewpoint()
-
- preBcBlock := legacy.MapBlock(preBlock)
- nextBlockHeight := preBlock.BlockHeader.Height + 1
- nextBlockSeed := algorithm.CreateSeed(nextBlockHeight, preBcBlock.Seed, []*bc.Hash{&preBcBlock.ID})
- txDescs := txPool.GetTransactions()
- txEntries := make([]*bc.Tx, 0, len(txDescs))
- blockWeight := uint64(0)
+ txStatus := bc.NewTransactionStatus()
+ if err := txStatus.SetStatus(0, false); err != nil {
+ return nil, err
+ }
+ txEntries := []*bc.Tx{nil}
+ gasUsed := uint64(0)
txFee := uint64(0)
- var compareDiffBH *legacy.BlockHeader
- if compareDiffBlock, err := c.GetBlockByHeight(nextBlockHeight - consensus.BlocksPerRetarget); err == nil {
- compareDiffBH = &compareDiffBlock.BlockHeader
+ // get preblock info for generate next block
+ preBlockHeader := c.BestBlockHeader()
+ preBlockHash := preBlockHeader.Hash()
+ nextBlockHeight := preBlockHeader.Height + 1
+ nextBits, err := c.CalcNextBits(&preBlockHash)
+ if err != nil {
+ return nil, err
}
- b := &legacy.Block{
- BlockHeader: legacy.BlockHeader{
+ b = &types.Block{
+ BlockHeader: types.BlockHeader{
Version: 1,
Height: nextBlockHeight,
- PreviousBlockHash: preBlock.Hash(),
- Seed: *nextBlockSeed,
- TimestampMS: bc.Millis(time.Now()),
- BlockCommitment: legacy.BlockCommitment{},
- Bits: difficulty.CalcNextRequiredDifficulty(&preBlock.BlockHeader, compareDiffBH),
+ PreviousBlockHash: preBlockHash,
+ Timestamp: uint64(time.Now().Unix()),
+ BlockCommitment: types.BlockCommitment{},
+ Bits: nextBits,
},
- Transactions: make([]*legacy.Tx, 0, len(txDescs)),
- }
-
- appendTx := func(tx *legacy.Tx, weight, fee uint64) {
- b.Transactions = append([]*legacy.Tx{tx}, b.Transactions...)
- txEntries = append([]*bc.Tx{tx.Tx}, txEntries...)
- blockWeight += weight
- txFee += fee
}
+ bcBlock := &bc.Block{BlockHeader: &bc.BlockHeader{Height: nextBlockHeight}}
+ b.Transactions = []*types.Tx{nil}
- bcBlock := legacy.MapBlock(b)
- for _, txDesc := range txDescs {
+ txs := txPool.GetTransactions()
+ sort.Sort(byTime(txs))
+ for _, txDesc := range txs {
tx := txDesc.Tx.Tx
- if blockWeight+txDesc.Weight > consensus.MaxBlockSzie-consensus.MaxTxSize {
- break
- }
+ gasOnlyTx := false
+
if err := c.GetTransactionsUtxo(view, []*bc.Tx{tx}); err != nil {
- log.WithField("error", err).Error("mining block generate skip tx due to")
- txPool.RemoveTransaction(&tx.ID)
+ blkGenSkipTxForErr(txPool, &tx.ID, err)
continue
}
- if err := view.ApplyTransaction(bcBlock, tx); err != nil {
- log.WithField("error", err).Error("mining block generate skip tx due to")
- txPool.RemoveTransaction(&tx.ID)
- continue
+
+ gasStatus, err := validation.ValidateTx(tx, bcBlock)
+ if err != nil {
+ if !gasStatus.GasValid {
+ blkGenSkipTxForErr(txPool, &tx.ID, err)
+ continue
+ }
+ gasOnlyTx = true
}
- if _, err := validation.ValidateTx(tx, preBcBlock); err != nil {
- log.WithField("error", err).Error("mining block generate skip tx due to")
- txPool.RemoveTransaction(&tx.ID)
+
+ if gasUsed+uint64(gasStatus.GasUsed) > consensus.MaxBlockGas {
+ break
+ }
+
+ if err := view.ApplyTransaction(bcBlock, tx, gasOnlyTx); err != nil {
+ blkGenSkipTxForErr(txPool, &tx.ID, err)
continue
}
- appendTx(txDesc.Tx, txDesc.Weight, txDesc.Fee)
+ if err := txStatus.SetStatus(len(b.Transactions), gasOnlyTx); err != nil {
+ return nil, err
+ }
+
+ b.Transactions = append(b.Transactions, txDesc.Tx)
+ txEntries = append(txEntries, tx)
+ gasUsed += uint64(gasStatus.GasUsed)
+ txFee += txDesc.Fee
+
+ if gasUsed == consensus.MaxBlockGas {
+ break
+ }
}
- cbTx, err := createCoinbaseTx(accountManager, txFee, nextBlockHeight)
+ // creater coinbase transaction
+ b.Transactions[0], err = createCoinbaseTx(accountManager, txFee, nextBlockHeight)
if err != nil {
return nil, errors.Wrap(err, "fail on createCoinbaseTx")
}
- appendTx(cbTx, 0, 0)
+ txEntries[0] = b.Transactions[0].Tx
- b.BlockHeader.BlockCommitment.TransactionsMerkleRoot, err = bc.MerkleRoot(txEntries)
+ b.BlockHeader.BlockCommitment.TransactionsMerkleRoot, err = types.TxMerkleRoot(txEntries)
if err != nil {
- return nil, errors.Wrap(err, "calculating tx merkle root")
+ return nil, err
}
- return b, nil
+ b.BlockHeader.BlockCommitment.TransactionStatusHash, err = types.TxStatusMerkleRoot(txStatus.VerifyStatus)
+ return b, err
+}
+
+func blkGenSkipTxForErr(txPool *protocol.TxPool, txHash *bc.Hash, err error) {
+ log.WithField("error", err).Error("mining block generation: skip tx due to")
+ txPool.RemoveTransaction(txHash)
}