X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=api%2Fminer.go;h=a7029e97de7759843bf1f9acea1873a99190fc3e;hb=4bc8c34936504eb903d2e8b5cf78473d41681b12;hp=671084d7b30ac50fb98b917dbbc8cd24eac81476;hpb=08281341c2cb02ba11d4218576256688854790fc;p=bytom%2Fvapor.git diff --git a/api/miner.go b/api/miner.go index 671084d7..a7029e97 100644 --- a/api/miner.go +++ b/api/miner.go @@ -6,22 +6,10 @@ import ( chainjson "github.com/vapor/encoding/json" "github.com/vapor/errors" - "github.com/vapor/protocol/bc" + "github.com/vapor/event" "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"` } @@ -50,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"` @@ -79,112 +49,18 @@ func (a *API) submitBlock(ctx context.Context, req *SubmitBlockReq) Response { if err != nil { return NewErrorResponse(err) } + if isOrphan { return NewErrorResponse(errors.New("block submitted is orphan")) } - blockHash := req.Block.BlockHeader.Hash() - a.newBlockCh <- &blockHash - 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 { + if err = a.eventDispatcher.Post(event.NewProposedBlockEvent{Block: *req.Block}); 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 { @@ -198,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")) } @@ -206,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")) }