X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=api%2Fminer.go;h=a7029e97de7759843bf1f9acea1873a99190fc3e;hb=refs%2Fheads%2Fvote_reward_readme;hp=410729f7ab4218c60ad6391316e54b5c7e99d4f9;hpb=db158dcf09436b003defd333f1a665e7e051d820;p=bytom%2Fvapor.git diff --git a/api/miner.go b/api/miner.go index 410729f7..a7029e97 100644 --- a/api/miner.go +++ b/api/miner.go @@ -7,22 +7,9 @@ import ( chainjson "github.com/vapor/encoding/json" "github.com/vapor/errors" "github.com/vapor/event" - "github.com/vapor/protocol/bc" "github.com/vapor/protocol/bc/types" ) -// BlockHeaderJSON struct provides support for get work in json format, when it also follows -// BlockHeader structure -type BlockHeaderJSON struct { - Version uint64 `json:"version"` // The version of the block. - Height uint64 `json:"height"` // The height of the block. - PreviousBlockHash bc.Hash `json:"previous_block_hash"` // The hash of the previous block. - Timestamp uint64 `json:"timestamp"` // The time of the block in seconds. - Nonce uint64 `json:"nonce"` // Nonce used to generate the block. - Bits uint64 `json:"bits"` // Difficulty target for the block. - BlockCommitment *types.BlockCommitment `json:"block_commitment"` // Block commitment -} - type CoinbaseArbitrary struct { Arbitrary chainjson.HexBytes `json:"arbitrary"` } @@ -51,24 +38,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"` @@ -85,110 +54,13 @@ func (a *API) submitBlock(ctx context.Context, req *SubmitBlockReq) Response { return NewErrorResponse(errors.New("block submitted is orphan")) } - if err = a.eventDispatcher.Post(event.NewMinedBlockEvent{Block: *req.Block}); err != nil { + if err = a.eventDispatcher.Post(event.NewProposedBlockEvent{Block: *req.Block}); err != nil { return NewErrorResponse(err) } return NewSuccessResponse(true) } -// SubmitWorkReq is req struct for submit-work API -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 { @@ -202,7 +74,7 @@ func (a *API) setMining(in struct { } func (a *API) startMining() Response { - a.cpuMiner.Start() + a.blockProposer.Start() if !a.IsMining() { return NewErrorResponse(errors.New("Failed to start mining")) } @@ -210,7 +82,7 @@ func (a *API) startMining() Response { } func (a *API) stopMining() Response { - a.cpuMiner.Stop() + a.blockProposer.Stop() if a.IsMining() { return NewErrorResponse(errors.New("Failed to stop mining")) }