OSDN Git Service

Peer add announces new block message num limit
[bytom/vapor.git] / proposal / proposal.go
index d2d045e..50d85d1 100644 (file)
@@ -24,7 +24,7 @@ const logModule = "mining"
 // 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, blockHeight uint64) (tx *types.Tx, err error) {
+func createCoinbaseTx(accountManager *account.Manager, blockHeight uint64, rewards []state.CoinbaseReward) (tx *types.Tx, err error) {
        arbitrary := append([]byte{0x00}, []byte(strconv.FormatUint(blockHeight, 10))...)
        var script []byte
        if accountManager == nil {
@@ -37,7 +37,7 @@ func createCoinbaseTx(accountManager *account.Manager, blockHeight uint64) (tx *
                return nil, err
        }
 
-       if len(arbitrary) > consensus.CoinbaseArbitrarySizeLimit {
+       if len(arbitrary) > consensus.ActiveNetParams.CoinbaseArbitrarySizeLimit {
                return nil, validation.ErrCoinbaseArbitraryOversize
        }
 
@@ -49,11 +49,23 @@ func createCoinbaseTx(accountManager *account.Manager, blockHeight uint64) (tx *
                return nil, err
        }
 
+       for _, r := range rewards {
+               if err = builder.AddOutput(types.NewIntraChainOutput(*consensus.BTMAssetID, r.Amount, r.ControlProgram)); err != nil {
+                       return nil, err
+               }
+       }
+
        _, txData, err := builder.Build()
        if err != nil {
                return nil, err
        }
 
+       byteData, err := txData.MarshalText()
+       if err != nil {
+               return nil, err
+       }
+
+       txData.SerializedSize = uint64(len(byteData))
        tx = &types.Tx{
                TxData: *txData,
                Tx:     types.MapTx(txData),
@@ -61,22 +73,6 @@ func createCoinbaseTx(accountManager *account.Manager, blockHeight uint64) (tx *
        return tx, nil
 }
 
-// restructCoinbaseTx build coinbase transaction with aggregate outputs when it achieved the specified block height
-func restructCoinbaseTx(tx *types.Tx, rewards []state.CoinbaseReward) error {
-       for _, r := range rewards {
-               tx.Outputs = append(tx.Outputs, types.NewIntraChainOutput(*consensus.BTMAssetID, r.Amount, r.ControlProgram))
-       }
-
-       byteData, err := tx.TxData.MarshalText()
-       if err != nil {
-               return err
-       }
-
-       tx.TxData.SerializedSize = uint64(len(byteData))
-       tx.Tx = types.MapTx(&tx.TxData)
-       return nil
-}
-
 // NewBlockTemplate returns a new block template that is ready to be solved
 func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager *account.Manager, timestamp uint64) (b *types.Block, err error) {
        view := state.NewUtxoViewpoint()
@@ -99,7 +95,7 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
                        PreviousBlockHash: preBlockHash,
                        Timestamp:         timestamp,
                        BlockCommitment:   types.BlockCommitment{},
-                       BlockWitness:      types.BlockWitness{Witness: make([][]byte, consensus.NumOfConsensusNode)},
+                       BlockWitness:      types.BlockWitness{Witness: make([][]byte, consensus.ActiveNetParams.NumOfConsensusNode)},
                },
        }
        bcBlock := &bc.Block{BlockHeader: &bc.BlockHeader{Height: nextBlockHeight}}
@@ -133,7 +129,7 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
                        continue
                }
 
-               if gasUsed+uint64(gasStatus.GasUsed) > consensus.MaxBlockGas {
+               if gasUsed+uint64(gasStatus.GasUsed) > consensus.ActiveNetParams.MaxBlockGas {
                        break
                }
 
@@ -149,34 +145,25 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
                b.Transactions = append(b.Transactions, txDesc.Tx)
                txEntries = append(txEntries, tx)
                gasUsed += uint64(gasStatus.GasUsed)
-               if gasUsed == consensus.MaxBlockGas {
+               if gasUsed == consensus.ActiveNetParams.MaxBlockGas {
                        break
                }
 
        }
 
-       // create coinbase transaction
-       b.Transactions[0], err = createCoinbaseTx(accountManager, nextBlockHeight)
-       if err != nil {
-               return nil, errors.Wrap(err, "fail on createCoinbaseTx")
-       }
-
        consensusResult, err := c.GetConsensusResultByHash(&preBlockHash)
        if err != nil {
                return nil, err
        }
 
-       if err := consensusResult.AttachCoinbaseReward(b); err != nil {
-               return nil, err
-       }
-
-       rewards, err := consensusResult.GetCoinbaseRewards(nextBlockHeight)
+       rewards, err := consensusResult.GetCoinbaseRewards(preBlockHeader.Height)
        if err != nil {
                return nil, err
        }
 
-       // restruct coinbase transaction
-       if err = restructCoinbaseTx(b.Transactions[0], rewards); err != nil {
+       // create coinbase transaction
+       b.Transactions[0], err = createCoinbaseTx(accountManager, nextBlockHeight, rewards)
+       if err != nil {
                return nil, errors.Wrap(err, "fail on createCoinbaseTx")
        }