"strconv"
"time"
+ "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"
+ "github.com/vapor/crypto/ed25519/chainkd"
"github.com/vapor/errors"
"github.com/vapor/protocol"
"github.com/vapor/protocol/bc"
arbitrary := append([]byte{0x00}, []byte(strconv.FormatUint(blockHeight, 10))...)
var script []byte
- if accountManager == nil {
- script, err = vmutil.DefaultCoinbaseProgram()
- } else {
- script, err = accountManager.GetCoinbaseControlProgram()
- arbitrary = append(arbitrary, accountManager.GetCoinbaseArbitrary()...)
- }
+ address, _ := common.DecodeAddress(config.CommonConfig.Consensus.Dpos.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
}
}
// 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, engine engine.Engine) (b *types.Block, err error) {
view := state.NewUtxoViewpoint()
txStatus := bc.NewTransactionStatus()
if err := txStatus.SetStatus(0, false); err != nil {
preBlockHash := preBlockHeader.Hash()
nextBlockHeight := preBlockHeader.Height + 1
- b = &types.Block{
- BlockHeader: types.BlockHeader{
- Version: 1,
- Height: nextBlockHeight,
- PreviousBlockHash: preBlockHash,
- Timestamp: uint64(time.Now().Unix()),
- BlockCommitment: types.BlockCommitment{},
- },
+ 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,
+ //Extra: make([]byte, 32+65),
+ }
+
+ if err := engine.Prepare(c, &header); err != nil {
+ log.Error("Failed to prepare header for mining", "err", err)
+ return nil, err
}
+
+ b = &types.Block{}
bcBlock := &bc.Block{BlockHeader: &bc.BlockHeader{Height: nextBlockHeight}}
b.Transactions = []*types.Tx{nil}
blkGenSkipTxForErr(txPool, &tx.ID, err)
continue
}
-
gasStatus, err := validation.ValidateTx(tx, bcBlock)
if err != nil {
if !gasStatus.GasValid {
txEntries = append(txEntries, tx)
gasUsed += uint64(gasStatus.GasUsed)
txFee += txDesc.Fee
-
if gasUsed == consensus.MaxBlockGas {
break
}
}
+
if txFee == 0 {
+ return nil, nil
+ }
+
+ 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)
if err != nil {