OSDN Git Service

Add the implementation for dppos
[bytom/vapor.git] / mining / mining.go
index dc47aaa..97590f5 100644 (file)
@@ -7,6 +7,8 @@ import (
        "strconv"
        "time"
 
+       "github.com/vapor/protocol/vm"
+
        "github.com/vapor/common"
 
        log "github.com/sirupsen/logrus"
@@ -16,6 +18,7 @@ import (
        "github.com/vapor/config"
        "github.com/vapor/consensus"
        engine "github.com/vapor/consensus/consensus"
+       dpos "github.com/vapor/consensus/consensus/dpos"
        "github.com/vapor/crypto/ed25519/chainkd"
        "github.com/vapor/errors"
        "github.com/vapor/protocol"
@@ -29,12 +32,12 @@ 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, delegateInfo engine.DelegateInfo, timestamp uint64) (tx *types.Tx, err error) {
+func createCoinbaseTx(accountManager *account.Manager, amount uint64, blockHeight uint64, delegateInfo interface{}, timestamp uint64) (tx *types.Tx, err error) {
        //amount += consensus.BlockSubsidy(blockHeight)
        arbitrary := append([]byte{0x00}, []byte(strconv.FormatUint(blockHeight, 10))...)
 
        var script []byte
-       address, _ := common.DecodeAddress(config.CommonConfig.Consensus.Dpos.Coinbase, &consensus.ActiveNetParams)
+       address, _ := common.DecodeAddress(config.CommonConfig.Consensus.Coinbase, &consensus.ActiveNetParams)
        redeemContract := address.ScriptAddress()
        script, _ = vmutil.P2WPKHProgram(redeemContract)
 
@@ -64,19 +67,21 @@ func createCoinbaseTx(accountManager *account.Manager, amount uint64, blockHeigh
                return nil, err
        }
        txData.SerializedSize = uint64(len(byteData))
-
-       delegates := engine.DelegateInfoList{}
-       delegates.Delegate = delegateInfo
+       delegates := dpos.DelegateInfoList{}
+       if delegateInfo != nil {
+               tmp := delegateInfo.(*dpos.DelegateInfo)
+               delegates.Delegate = *tmp
+       }
 
        var xPrv chainkd.XPrv
-       if config.CommonConfig.Consensus.Dpos.XPrv == "" {
+       if config.CommonConfig.Consensus.XPrv == "" {
                return nil, errors.New("Signer is empty")
        }
-       xPrv.UnmarshalText([]byte(config.CommonConfig.Consensus.Dpos.XPrv))
+       xPrv.UnmarshalText([]byte(config.CommonConfig.Consensus.XPrv))
 
        buf := [8]byte{}
        binary.LittleEndian.PutUint64(buf[:], timestamp)
-       delegates.SigTime = xPrv.Sign(buf)
+       delegates.SigTime = xPrv.Sign(buf[:])
        delegates.Xpub = xPrv.XPub()
 
        data, err := json.Marshal(&delegates)
@@ -84,6 +89,15 @@ func createCoinbaseTx(accountManager *account.Manager, amount uint64, blockHeigh
                return nil, err
        }
 
+       msg := dpos.DposMsg{
+               Type: vm.OP_DELEGATE,
+               Data: data,
+       }
+
+       data, err = json.Marshal(&msg)
+       if err != nil {
+               return nil, err
+       }
        txData.ReferenceData = data
 
        tx = &types.Tx{
@@ -94,7 +108,7 @@ func createCoinbaseTx(accountManager *account.Manager, amount uint64, blockHeigh
 }
 
 // NewBlockTemplate returns a new block template that is ready to be solved
-func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager *account.Manager, engine engine.Engine, delegateInfo engine.DelegateInfo) (b *types.Block, err error) {
+func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager *account.Manager, engine engine.Engine, delegateInfo interface{}) (b *types.Block, err error) {
        view := state.NewUtxoViewpoint()
        txStatus := bc.NewTransactionStatus()
        if err := txStatus.SetStatus(0, false); err != nil {
@@ -109,25 +123,12 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
        preBlockHash := preBlockHeader.Hash()
        nextBlockHeight := preBlockHeader.Height + 1
 
-       var xPrv chainkd.XPrv
-       if config.CommonConfig.Consensus.Dpos.XPrv == "" {
-               return nil, errors.New("Signer is empty")
-       }
-       xPrv.UnmarshalText([]byte(config.CommonConfig.Consensus.Dpos.XPrv))
-       xpub, _ := xPrv.XPub().MarshalText()
-
        header := types.BlockHeader{
                Version:           1,
                Height:            nextBlockHeight,
                PreviousBlockHash: preBlockHash,
                Timestamp:         uint64(time.Now().Unix()),
                BlockCommitment:   types.BlockCommitment{},
-               Coinbase:          xpub,
-       }
-
-       if err := engine.Prepare(c, &header); err != nil {
-               log.Error("Failed to prepare header for mining", "err", err)
-               return nil, err
        }
 
        b = &types.Block{}
@@ -175,13 +176,9 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
                }
        }
 
-       if err := engine.Finalize(c, &header, txEntries[1:]); err != nil {
-               return nil, err
-       }
-
        b.BlockHeader = header
        // creater coinbase transaction
-       b.Transactions[0], err = createCoinbaseTx(accountManager, txFee, nextBlockHeight, delegateInfo, bcBlock.Timestamp)
+       b.Transactions[0], err = createCoinbaseTx(accountManager, txFee, nextBlockHeight, delegateInfo, b.Timestamp)
        if err != nil {
                return nil, errors.Wrap(err, "fail on createCoinbaseTx")
        }