OSDN Git Service

delete generate block for api
authormars <mars@bytom.io>
Mon, 4 Mar 2019 06:24:21 +0000 (14:24 +0800)
committermars <mars@bytom.io>
Mon, 4 Mar 2019 06:24:21 +0000 (14:24 +0800)
api/api.go
api/miner.go
mining/miner/miner.go
mining/mining.go
node/node.go

index b45a30b..937f8f8 100644 (file)
@@ -18,7 +18,6 @@ import (
        "github.com/vapor/dashboard/equity"
        "github.com/vapor/errors"
        "github.com/vapor/mining/miner"
-       "github.com/vapor/mining/miningpool"
        "github.com/vapor/net/http/authn"
        "github.com/vapor/net/http/gzip"
        "github.com/vapor/net/http/httpjson"
@@ -114,7 +113,6 @@ type API struct {
        txFeedTracker *txfeed.Tracker
        //cpuMiner        *cpuminer.CPUMiner
        miner           *miner.Miner
-       miningPool      *miningpool.MiningPool
        notificationMgr *websocket.WSNotificationManager
        newBlockCh      chan *bc.Hash
 }
@@ -170,7 +168,7 @@ func (a *API) StartServer(address string) {
 }
 
 // NewAPI create and initialize the API
-func NewAPI(sync *netsync.SyncManager, wallet *wallet.Wallet, txfeeds *txfeed.Tracker, miner *miner.Miner, miningPool *miningpool.MiningPool, chain *protocol.Chain, config *cfg.Config, token *accesstoken.CredentialStore, newBlockCh chan *bc.Hash, notificationMgr *websocket.WSNotificationManager) *API {
+func NewAPI(sync *netsync.SyncManager, wallet *wallet.Wallet, txfeeds *txfeed.Tracker, miner *miner.Miner, chain *protocol.Chain, config *cfg.Config, token *accesstoken.CredentialStore, newBlockCh chan *bc.Hash, notificationMgr *websocket.WSNotificationManager) *API {
        api := &API{
                sync:          sync,
                wallet:        wallet,
@@ -178,7 +176,6 @@ func NewAPI(sync *netsync.SyncManager, wallet *wallet.Wallet, txfeeds *txfeed.Tr
                accessTokens:  token,
                txFeedTracker: txfeeds,
                miner:         miner,
-               miningPool:    miningPool,
 
                newBlockCh:      newBlockCh,
                notificationMgr: notificationMgr,
@@ -295,12 +292,6 @@ func (a *API) buildHandler() {
        m.Handle("/is-mining", jsonHandler(a.isMining))
        m.Handle("/set-mining", jsonHandler(a.setMining))
 
-       m.Handle("/get-work", jsonHandler(a.getWork))
-       m.Handle("/get-work-json", jsonHandler(a.getWorkJSON))
-       m.Handle("/submit-block", jsonHandler(a.submitBlock))
-       m.Handle("/submit-work", jsonHandler(a.submitWork))
-       m.Handle("/submit-work-json", jsonHandler(a.submitWorkJSON))
-
        m.Handle("/verify-message", jsonHandler(a.verifyMessage))
        m.Handle("/compile", jsonHandler(a.compileEquity))
 
index 3ba3f06..71869d5 100644 (file)
@@ -50,24 +50,6 @@ func (a *API) setCoinbaseArbitrary(ctx context.Context, req CoinbaseArbitrary) R
        return a.getCoinbaseArbitrary()
 }
 
-// getWork gets work in compressed protobuf format
-func (a *API) getWork() Response {
-       work, err := a.GetWork()
-       if err != nil {
-               return NewErrorResponse(err)
-       }
-       return NewSuccessResponse(work)
-}
-
-// getWorkJSON gets work in json format
-func (a *API) getWorkJSON() Response {
-       work, err := a.GetWorkJSON()
-       if err != nil {
-               return NewErrorResponse(err)
-       }
-       return NewSuccessResponse(work)
-}
-
 // SubmitBlockReq is req struct for submit-block API
 type SubmitBlockReq struct {
        Block *types.Block `json:"raw_block"`
@@ -93,98 +75,23 @@ type SubmitWorkReq struct {
        BlockHeader *types.BlockHeader `json:"block_header"`
 }
 
-// submitWork submits work in compressed protobuf format
-func (a *API) submitWork(ctx context.Context, req *SubmitWorkReq) Response {
-       if err := a.SubmitWork(req.BlockHeader); err != nil {
-               return NewErrorResponse(err)
-       }
-       return NewSuccessResponse(true)
-}
-
 // SubmitWorkJSONReq is req struct for submit-work-json API
 type SubmitWorkJSONReq struct {
        BlockHeader *BlockHeaderJSON `json:"block_header"`
 }
 
-// submitWorkJSON submits work in json format
-func (a *API) submitWorkJSON(ctx context.Context, req *SubmitWorkJSONReq) Response {
-       bh := &types.BlockHeader{
-               Version:           req.BlockHeader.Version,
-               Height:            req.BlockHeader.Height,
-               PreviousBlockHash: req.BlockHeader.PreviousBlockHash,
-               Timestamp:         req.BlockHeader.Timestamp,
-               //Nonce:             req.BlockHeader.Nonce,
-               //Bits:              req.BlockHeader.Bits,
-               BlockCommitment: *req.BlockHeader.BlockCommitment,
-       }
-
-       if err := a.SubmitWork(bh); err != nil {
-               return NewErrorResponse(err)
-       }
-       return NewSuccessResponse(true)
-}
-
 // GetWorkResp is resp struct for get-work API
 type GetWorkResp struct {
        BlockHeader *types.BlockHeader `json:"block_header"`
        Seed        *bc.Hash           `json:"seed"`
 }
 
-// GetWork gets work in compressed protobuf format
-func (a *API) GetWork() (*GetWorkResp, error) {
-       bh, err := a.miningPool.GetWork()
-       if err != nil {
-               return nil, err
-       }
-
-       seed, err := a.chain.CalcNextSeed(&bh.PreviousBlockHash)
-       if err != nil {
-               return nil, err
-       }
-
-       return &GetWorkResp{
-               BlockHeader: bh,
-               Seed:        seed,
-       }, nil
-}
-
 // GetWorkJSONResp is resp struct for get-work-json API
 type GetWorkJSONResp struct {
        BlockHeader *BlockHeaderJSON `json:"block_header"`
        Seed        *bc.Hash         `json:"seed"`
 }
 
-// GetWorkJSON gets work in json format
-func (a *API) GetWorkJSON() (*GetWorkJSONResp, error) {
-       bh, err := a.miningPool.GetWork()
-       if err != nil {
-               return nil, err
-       }
-
-       seed, err := a.chain.CalcNextSeed(&bh.PreviousBlockHash)
-       if err != nil {
-               return nil, err
-       }
-
-       return &GetWorkJSONResp{
-               BlockHeader: &BlockHeaderJSON{
-                       Version:           bh.Version,
-                       Height:            bh.Height,
-                       PreviousBlockHash: bh.PreviousBlockHash,
-                       Timestamp:         bh.Timestamp,
-                       //                      Nonce:             bh.Nonce,
-                       //                      Bits:              bh.Bits,
-                       BlockCommitment: &bh.BlockCommitment,
-               },
-               Seed: seed,
-       }, nil
-}
-
-// SubmitWork tries to submit work to the chain
-func (a *API) SubmitWork(bh *types.BlockHeader) error {
-       return a.miningPool.SubmitWork(bh)
-}
-
 func (a *API) setMining(in struct {
        IsMining bool `json:"is_mining"`
 }) Response {
index dedd92a..f786ce7 100644 (file)
@@ -125,7 +125,7 @@ out:
                */
                isSeal := true
                if isSeal {
-                       block, err := mining.NewBlockTemplate1(m.chain, m.txPool, m.accountManager, m.engine)
+                       block, err := mining.NewBlockTemplate(m.chain, m.txPool, m.accountManager, m.engine)
                        if err != nil {
                                log.Errorf("Mining: failed on create NewBlockTemplate: %v", err)
                                time.Sleep(3 * time.Second)
index e2dfcac..5943904 100644 (file)
@@ -78,97 +78,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) (b *types.Block, err error) {
-       view := state.NewUtxoViewpoint()
-       txStatus := bc.NewTransactionStatus()
-       if err := txStatus.SetStatus(0, false); err != nil {
-               return nil, err
-       }
-       txEntries := []*bc.Tx{nil}
-       gasUsed := uint64(0)
-       txFee := uint64(0)
-
-       // get preblock info for generate next block
-       preBlockHeader := c.BestBlockHeader()
-       preBlockHash := preBlockHeader.Hash()
-       nextBlockHeight := preBlockHeader.Height + 1
-
-       header := types.BlockHeader{
-               Version:           1,
-               Height:            nextBlockHeight,
-               PreviousBlockHash: preBlockHash,
-               Timestamp:         uint64(time.Now().Unix()),
-               BlockCommitment:   types.BlockCommitment{},
-       }
-
-       b = &types.Block{BlockHeader: header}
-       bcBlock := &bc.Block{BlockHeader: &bc.BlockHeader{Height: nextBlockHeight}}
-       b.Transactions = []*types.Tx{nil}
-
-       txs := txPool.GetTransactions()
-       sort.Sort(byTime(txs))
-       for _, txDesc := range txs {
-               tx := txDesc.Tx.Tx
-               gasOnlyTx := false
-
-               if err := c.GetTransactionsUtxo(view, []*bc.Tx{tx}); err != nil {
-                       blkGenSkipTxForErr(txPool, &tx.ID, err)
-                       continue
-               }
-
-               gasStatus, err := validation.ValidateTx(tx, bcBlock)
-               if err != nil {
-                       if !gasStatus.GasValid {
-                               blkGenSkipTxForErr(txPool, &tx.ID, err)
-                               continue
-                       }
-                       gasOnlyTx = true
-               }
-
-               if gasUsed+uint64(gasStatus.GasUsed) > consensus.MaxBlockGas {
-                       break
-               }
-
-               if err := view.ApplyTransaction(bcBlock, tx, gasOnlyTx); err != nil {
-                       blkGenSkipTxForErr(txPool, &tx.ID, err)
-                       continue
-               }
-
-               if err := txStatus.SetStatus(len(b.Transactions), gasOnlyTx); err != nil {
-                       return nil, err
-               }
-
-               b.Transactions = append(b.Transactions, txDesc.Tx)
-               txEntries = append(txEntries, tx)
-               gasUsed += uint64(gasStatus.GasUsed)
-               txFee += txDesc.Fee
-
-               if gasUsed == consensus.MaxBlockGas {
-                       break
-               }
-       }
-       if txFee == 0 {
-               return nil, err
-       }
-
-       // creater coinbase transaction
-       b.Transactions[0], err = createCoinbaseTx(accountManager, txFee, nextBlockHeight)
-       if err != nil {
-               return nil, errors.Wrap(err, "fail on createCoinbaseTx")
-       }
-       txEntries[0] = b.Transactions[0].Tx
-
-       b.BlockHeader.BlockCommitment.TransactionsMerkleRoot, err = types.TxMerkleRoot(txEntries)
-       if err != nil {
-               return nil, err
-       }
-
-       b.BlockHeader.BlockCommitment.TransactionStatusHash, err = types.TxStatusMerkleRoot(txStatus.VerifyStatus)
-       return b, err
-}
-
-// NewBlockTemplate returns a new block template that is ready to be solved
-func NewBlockTemplate1(c *protocol.Chain, txPool *protocol.TxPool, accountManager *account.Manager, engine engine.Engine) (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 {
index d46f06d..8021a66 100644 (file)
@@ -33,7 +33,6 @@ import (
        "github.com/vapor/database/leveldb"
        "github.com/vapor/env"
        "github.com/vapor/mining/miner"
-       "github.com/vapor/mining/miningpool"
        "github.com/vapor/net/websocket"
        "github.com/vapor/netsync"
        "github.com/vapor/protocol"
@@ -67,7 +66,6 @@ type Node struct {
        //cpuMiner        *cpuminer.CPUMiner
        miner *miner.Miner
 
-       miningPool   *miningpool.MiningPool
        miningEnable bool
 
        newBlockCh chan *bc.Hash
@@ -174,7 +172,6 @@ func NewNode(config *cfg.Config) *Node {
        //node.cpuMiner = cpuminer.NewCPUMiner(chain, accounts, txPool, newBlockCh)
        consensusEngine = createConsensusEngine(config, store)
        node.miner = miner.NewMiner(chain, accounts, txPool, newBlockCh, consensusEngine)
-       node.miningPool = miningpool.NewMiningPool(chain, accounts, txPool, newBlockCh)
 
        node.BaseService = *cmn.NewBaseService(nil, "Node", node)
 
@@ -265,7 +262,7 @@ func launchWebBrowser(port string) {
 }
 
 func (n *Node) initAndstartApiServer() {
-       n.api = api.NewAPI(n.syncManager, n.wallet, n.txfeed, n.miner, n.miningPool, n.chain, n.config, n.accessTokens, n.newBlockCh, n.notificationMgr)
+       n.api = api.NewAPI(n.syncManager, n.wallet, n.txfeed, n.miner, n.chain, n.config, n.accessTokens, n.newBlockCh, n.notificationMgr)
 
        listenAddr := env.String("LISTEN", n.config.ApiAddress)
        env.Parse()
@@ -322,10 +319,6 @@ func (n *Node) SyncManager() *netsync.SyncManager {
        return n.syncManager
 }
 
-func (n *Node) MiningPool() *miningpool.MiningPool {
-       return n.miningPool
-}
-
 /**bytomdRPCCheck Check if bytomd connection via RPC is correctly working*/
 func bytomdRPCCheck() bool {
        type Req struct {