OSDN Git Service

Revert "update master (#487)" (#518)
[bytom/bytom.git] / mining / mining.go
index 4166d23..114c695 100644 (file)
@@ -10,14 +10,14 @@ import (
 
        log "github.com/sirupsen/logrus"
 
-       "github.com/bytom/account"
+       "github.com/bytom/blockchain/account"
        "github.com/bytom/blockchain/txbuilder"
        "github.com/bytom/consensus"
        "github.com/bytom/consensus/difficulty"
        "github.com/bytom/errors"
        "github.com/bytom/protocol"
        "github.com/bytom/protocol/bc"
-       "github.com/bytom/protocol/bc/types"
+       "github.com/bytom/protocol/bc/legacy"
        "github.com/bytom/protocol/state"
        "github.com/bytom/protocol/validation"
        "github.com/bytom/protocol/vm/vmutil"
@@ -26,7 +26,7 @@ import (
 // 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 *types.Tx, err error) {
+func createCoinbaseTx(accountManager *account.Manager, amount uint64, blockHeight uint64) (tx *legacy.Tx, err error) {
        amount += consensus.BlockSubsidy(blockHeight)
 
        var script []byte
@@ -40,10 +40,10 @@ func createCoinbaseTx(accountManager *account.Manager, amount uint64, blockHeigh
        }
 
        builder := txbuilder.NewBuilder(time.Now())
-       if err = builder.AddInput(types.NewCoinbaseInput([]byte(string(blockHeight))), &txbuilder.SigningInstruction{}); err != nil {
+       if err = builder.AddInput(legacy.NewCoinbaseInput([]byte(string(blockHeight)), nil), &txbuilder.SigningInstruction{}); err != nil {
                return
        }
-       if err = builder.AddOutput(types.NewTxOutput(*consensus.BTMAssetID, amount, script)); err != nil {
+       if err = builder.AddOutput(legacy.NewTxOutput(*consensus.BTMAssetID, amount, script, nil)); err != nil {
                return
        }
        _, txData, err := builder.Build()
@@ -51,41 +51,41 @@ func createCoinbaseTx(accountManager *account.Manager, amount uint64, blockHeigh
                return
        }
 
-       tx = &types.Tx{
+       tx = &legacy.Tx{
                TxData: *txData,
-               Tx:     types.MapTx(txData),
+               Tx:     legacy.MapTx(txData),
        }
        return
 }
 
 // NewBlockTemplate returns a new block template that is ready to be solved
-func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager *account.Manager) (b *types.Block, err error) {
+func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager *account.Manager) (b *legacy.Block, err error) {
        view := state.NewUtxoViewpoint()
-       txStatus := bc.NewTransactionStatus()
        txEntries := []*bc.Tx{nil}
-       gasUsed := uint64(0)
+       blockWeight := uint64(0)
        txFee := uint64(0)
 
        // get preblock info for generate next block
        preBlock := c.BestBlock()
-       preBcBlock := types.MapBlock(preBlock)
+       preBcBlock := legacy.MapBlock(preBlock)
        nextBlockHeight := preBlock.BlockHeader.Height + 1
 
-       var compareDiffBH *types.BlockHeader
+       var compareDiffBH *legacy.BlockHeader
        if compareDiffBlock, err := c.GetBlockByHeight(nextBlockHeight - consensus.BlocksPerRetarget); err == nil {
                compareDiffBH = &compareDiffBlock.BlockHeader
        }
 
-       b = &types.Block{
-               BlockHeader: types.BlockHeader{
+       b = &legacy.Block{
+               BlockHeader: legacy.BlockHeader{
                        Version:           1,
                        Height:            nextBlockHeight,
                        PreviousBlockHash: preBlock.Hash(),
                        Timestamp:         uint64(time.Now().Unix()),
-                       BlockCommitment:   types.BlockCommitment{},
+                       TransactionStatus: *bc.NewTransactionStatus(),
+                       BlockCommitment:   legacy.BlockCommitment{},
                        Bits:              difficulty.CalcNextRequiredDifficulty(&preBlock.BlockHeader, compareDiffBH),
                },
-               Transactions: []*types.Tx{nil},
+               Transactions: []*legacy.Tx{nil},
        }
        bcBlock := &bc.Block{BlockHeader: &bc.BlockHeader{Height: nextBlockHeight}}
 
@@ -94,6 +94,9 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
        for _, txDesc := range txs {
                tx := txDesc.Tx.Tx
                gasOnlyTx := false
+               if blockWeight+txDesc.Weight > consensus.MaxBlockSzie-consensus.MaxTxSize {
+                       break
+               }
 
                if err := c.GetTransactionsUtxo(view, []*bc.Tx{tx}); err != nil {
                        log.WithField("error", err).Error("mining block generate skip tx due to")
@@ -101,9 +104,8 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
                        continue
                }
 
-               gasStatus, err := validation.ValidateTx(tx, preBcBlock)
-               if err != nil {
-                       if !gasStatus.GasVaild {
+               if _, gasVaild, err := validation.ValidateTx(tx, preBcBlock); err != nil {
+                       if !gasVaild {
                                log.WithField("error", err).Error("mining block generate skip tx due to")
                                txPool.RemoveTransaction(&tx.ID)
                                continue
@@ -111,25 +113,17 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
                        gasOnlyTx = true
                }
 
-               if gasUsed+uint64(gasStatus.GasUsed) > consensus.MaxBlockGas {
-                       break
-               }
-
                if err := view.ApplyTransaction(bcBlock, tx, gasOnlyTx); err != nil {
                        log.WithField("error", err).Error("mining block generate skip tx due to")
                        txPool.RemoveTransaction(&tx.ID)
                        continue
                }
 
-               txStatus.SetStatus(len(b.Transactions), gasOnlyTx)
+               b.BlockHeader.TransactionStatus.SetStatus(len(b.Transactions), gasOnlyTx)
                b.Transactions = append(b.Transactions, txDesc.Tx)
                txEntries = append(txEntries, tx)
-               gasUsed += uint64(gasStatus.GasUsed)
+               blockWeight += txDesc.Weight
                txFee += txDesc.Fee
-
-               if gasUsed == consensus.MaxBlockGas {
-                       break
-               }
        }
 
        // creater coinbase transaction
@@ -140,6 +134,5 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
        txEntries[0] = b.Transactions[0].Tx
 
        b.BlockHeader.BlockCommitment.TransactionsMerkleRoot, err = bc.MerkleRoot(txEntries)
-       b.BlockHeader.BlockCommitment.TransactionStatusHash = bc.EntryID(txStatus)
        return b, err
 }