OSDN Git Service

restruct
authoroys <oys@oysdeMBP.localdomain>
Fri, 5 Jul 2019 03:08:39 +0000 (11:08 +0800)
committeroys <oys@oysdeMBP.localdomain>
Fri, 5 Jul 2019 03:08:39 +0000 (11:08 +0800)
proposal/proposal.go

index 83507a8..8f91b42 100644 (file)
@@ -68,46 +68,19 @@ func createCoinbaseTx(accountManager *account.Manager, blockHeight uint64) (tx *
 }
 
 // restructCoinbaseTx build coinbase transaction with aggregate outputs when it achieved the specified block height
-func restructCoinbaseTx(tx *types.Tx, rewards []state.CoinbaseReward) (*types.Tx, error) {
-       var arbitrary []byte
-       if len(tx.InputIDs) > 0 {
-               e, ok := tx.Entries[tx.Tx.InputIDs[0]].(*bc.Coinbase)
-               if ok {
-                       arbitrary = e.Arbitrary
-               }
-       }
-
-       if arbitrary == nil {
-               return nil, errors.New("bad origin coinbase input")
-       }
-
-       builder := txbuilder.NewBuilder(time.Now())
-       if err := builder.AddInput(types.NewCoinbaseInput(arbitrary), &txbuilder.SigningInstruction{}); err != nil {
-               return nil, err
-       }
-
-       // add the aggregate coinbase rewards
+func restructCoinbaseTx(tx *types.Tx, rewards []state.CoinbaseReward) error {
        for _, r := range rewards {
-               if err := builder.AddOutput(types.NewIntraChainOutput(*consensus.BTMAssetID, r.Amount, r.ControlProgram)); err != nil {
-                       return nil, err
-               }
+               tx.Outputs = append(tx.Outputs, types.NewIntraChainOutput(*consensus.BTMAssetID, r.Amount, r.ControlProgram))
        }
 
-       _, txData, err := builder.Build()
+       byteData, err := tx.TxData.MarshalText()
        if err != nil {
-               return nil, err
+               return err
        }
 
-       byteData, err := txData.MarshalText()
-       if err != nil {
-               return nil, err
-       }
-       txData.SerializedSize = uint64(len(byteData))
-
-       return &types.Tx{
-               TxData: *txData,
-               Tx:     types.MapTx(txData),
-       }, nil
+       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
@@ -199,8 +172,7 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
                return nil, err
        }
 
-       coinbaseReceiver, err := consensusResult.AttachCoinbaseReward(b)
-       if err != nil {
+       if _, err := consensusResult.AttachCoinbaseReward(b); err != nil {
                return nil, err
        }
 
@@ -211,9 +183,7 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
 
        // restruct coinbase transaction
        if len(rewards) > 0 {
-               rewards = append([]state.CoinbaseReward{state.CoinbaseReward{ControlProgram: coinbaseReceiver}}, rewards...)
-               b.Transactions[0], err = restructCoinbaseTx(b.Transactions[0], rewards)
-               if err != nil {
+               if err = restructCoinbaseTx(b.Transactions[0], rewards); err != nil {
                        return nil, errors.Wrap(err, "fail on createCoinbaseTx")
                }
        }