X-Git-Url: http://git.osdn.net/view?p=bytom%2Fvapor.git;a=blobdiff_plain;f=mining%2Fmining.go;h=9f9dece22db5ea6b19965816db1998aa2b8810eb;hp=61dfe032adc9580677eca6344a191d75172767e0;hb=a8fcbab90cbbfcc0acea0a4bd1389e39ece3da56;hpb=232f90ac69cd37fe0ca2fa10831a815440376008 diff --git a/mining/mining.go b/mining/mining.go index 61dfe032..9f9dece2 100644 --- a/mining/mining.go +++ b/mining/mining.go @@ -1,25 +1,15 @@ package mining import ( - "encoding/binary" - "encoding/json" "sort" "strconv" "time" - "github.com/vapor/protocol/vm" - - "github.com/vapor/common" - log "github.com/sirupsen/logrus" "github.com/vapor/account" "github.com/vapor/blockchain/txbuilder" - "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" "github.com/vapor/protocol/bc" @@ -29,18 +19,22 @@ import ( "github.com/vapor/protocol/vm/vmutil" ) +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, amount uint64, blockHeight uint64, delegateInfo interface{}, timestamp uint64) (tx *types.Tx, err error) { - //amount += consensus.BlockSubsidy(blockHeight) +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 - address, _ := common.DecodeAddress(config.CommonConfig.Consensus.Coinbase, &consensus.ActiveNetParams) - redeemContract := address.ScriptAddress() - script, _ = vmutil.P2WPKHProgram(redeemContract) - + if accountManager == nil { + script, err = vmutil.DefaultCoinbaseProgram() + } else { + script, err = accountManager.GetCoinbaseControlProgram() + arbitrary = append(arbitrary, accountManager.GetCoinbaseArbitrary()...) + } if err != nil { return nil, err } @@ -53,8 +47,7 @@ func createCoinbaseTx(accountManager *account.Manager, amount uint64, blockHeigh if err = builder.AddInput(types.NewCoinbaseInput(arbitrary), &txbuilder.SigningInstruction{}); err != nil { return nil, err } - - if err = builder.AddOutput(types.NewTxOutput(*consensus.BTMAssetID, amount, script)); err != nil { + if err = builder.AddOutput(types.NewIntraChainOutput(*consensus.BTMAssetID, amount, script)); err != nil { return nil, err } _, txData, err := builder.Build() @@ -67,38 +60,6 @@ func createCoinbaseTx(accountManager *account.Manager, amount uint64, blockHeigh return nil, err } txData.SerializedSize = uint64(len(byteData)) - delegates := dpos.DelegateInfoList{} - if delegateInfo != nil { - tmp := delegateInfo.(*dpos.DelegateInfo) - delegates.Delegate = *tmp - } - - var xPrv chainkd.XPrv - if config.CommonConfig.Consensus.XPrv == "" { - return nil, errors.New("Signer is empty") - } - xPrv.UnmarshalText([]byte(config.CommonConfig.Consensus.XPrv)) - - buf := [8]byte{} - binary.LittleEndian.PutUint64(buf[:], timestamp) - delegates.SigTime = xPrv.Sign(buf[:]) - delegates.Xpub = xPrv.XPub() - - data, err := json.Marshal(&delegates) - if err != nil { - 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{ TxData: *txData, @@ -108,7 +69,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 interface{}, blockTime uint64) (b *types.Block, err error) { +func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager *account.Manager) (b *types.Block, err error) { view := state.NewUtxoViewpoint() txStatus := bc.NewTransactionStatus() if err := txStatus.SetStatus(0, false); err != nil { @@ -123,15 +84,15 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager preBlockHash := preBlockHeader.Hash() nextBlockHeight := preBlockHeader.Height + 1 - header := types.BlockHeader{ - Version: 1, - Height: nextBlockHeight, - PreviousBlockHash: preBlockHash, - Timestamp: blockTime, - BlockCommitment: types.BlockCommitment{}, + b = &types.Block{ + BlockHeader: types.BlockHeader{ + Version: 1, + Height: nextBlockHeight, + PreviousBlockHash: preBlockHash, + Timestamp: uint64(time.Now().Unix()), + BlockCommitment: types.BlockCommitment{}, + }, } - - b = &types.Block{} bcBlock := &bc.Block{BlockHeader: &bc.BlockHeader{Height: nextBlockHeight}} b.Transactions = []*types.Tx{nil} @@ -145,6 +106,7 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager blkGenSkipTxForErr(txPool, &tx.ID, err) continue } + gasStatus, err := validation.ValidateTx(tx, bcBlock) if err != nil { if !gasStatus.GasValid { @@ -171,14 +133,14 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager txEntries = append(txEntries, tx) gasUsed += uint64(gasStatus.GasUsed) txFee += txDesc.Fee + if gasUsed == consensus.MaxBlockGas { break } } - b.BlockHeader = header // creater coinbase transaction - b.Transactions[0], err = createCoinbaseTx(accountManager, txFee, nextBlockHeight, delegateInfo, b.Timestamp) + b.Transactions[0], err = createCoinbaseTx(accountManager, txFee, nextBlockHeight) if err != nil { return nil, errors.Wrap(err, "fail on createCoinbaseTx") } @@ -194,6 +156,6 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager } func blkGenSkipTxForErr(txPool *protocol.TxPool, txHash *bc.Hash, err error) { - log.WithField("error", err).Error("mining block generation: skip tx due to") + log.WithFields(log.Fields{"module": logModule, "error": err}).Error("mining block generation: skip tx due to") txPool.RemoveTransaction(txHash) }