diff options
author | Bas van Kervel <bas@ethdev.com> | 2015-10-15 22:07:19 +0800 |
---|---|---|
committer | Bas van Kervel <bas@ethdev.com> | 2015-12-14 23:34:05 +0800 |
commit | eae81465c1c815c317cd30e4de6bdf4d59df2340 (patch) | |
tree | b6f4b7787967a58416171adb79fd12ac29d89577 /miner | |
parent | 8db9d44ca9fb6baf406256cae491c475de2f4989 (diff) | |
download | go-tangerine-eae81465c1c815c317cd30e4de6bdf4d59df2340.tar go-tangerine-eae81465c1c815c317cd30e4de6bdf4d59df2340.tar.gz go-tangerine-eae81465c1c815c317cd30e4de6bdf4d59df2340.tar.bz2 go-tangerine-eae81465c1c815c317cd30e4de6bdf4d59df2340.tar.lz go-tangerine-eae81465c1c815c317cd30e4de6bdf4d59df2340.tar.xz go-tangerine-eae81465c1c815c317cd30e4de6bdf4d59df2340.tar.zst go-tangerine-eae81465c1c815c317cd30e4de6bdf4d59df2340.zip |
rpc: new RPC implementation with pub/sub support
Diffstat (limited to 'miner')
-rw-r--r-- | miner/api.go | 72 | ||||
-rw-r--r-- | miner/worker.go | 1 |
2 files changed, 73 insertions, 0 deletions
diff --git a/miner/api.go b/miner/api.go new file mode 100644 index 000000000..65d106afd --- /dev/null +++ b/miner/api.go @@ -0,0 +1,72 @@ +// Copyright 2015 The go-ethereum Authors +// This file is part of go-ethereum. +// +// go-ethereum is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// go-ethereum is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. + +package miner + +import ( + "fmt" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/logger/glog" + rpc "github.com/ethereum/go-ethereum/rpc/v2" +) + +// PublicMinerAPI provides an API to control the miner. +// It offers only methods that operate on data that pose no security risk when it is publicly accessible. +type PublicMinerAPI struct { + miner *Miner + agent *RemoteAgent +} + +// NewPublicMinerAPI create a new PublicMinerAPI instance. +func NewPublicMinerAPI(miner *Miner) *PublicMinerAPI { + return &PublicMinerAPI{miner, NewRemoteAgent()} +} + +// Mining returns an indication if this node is currently mining. +func (s *PublicMinerAPI) Mining() bool { + return s.miner.Mining() +} + +// SubmitWork can be used by external miner to submit their POW solution. It returns an indication if the work was +// accepted. Note, this is not an indication if the provided work was valid! +func (s *PublicMinerAPI) SubmitWork(nonce rpc.HexNumber, solution, digest common.Hash) bool { + return s.agent.SubmitWork(nonce.Uint64(), digest, solution) +} + +// GetWork returns a work package for external miner. The work package consists of 3 strings +// result[0], 32 bytes hex encoded current block header pow-hash +// result[1], 32 bytes hex encoded seed hash used for DAG +// result[2], 32 bytes hex encoded boundary condition ("target"), 2^256/difficulty +func (s *PublicMinerAPI) GetWork() ([]string, error) { + if !s.Mining() { + s.miner.Start(s.miner.coinbase, 0) + } + if work, err := s.agent.GetWork(); err == nil { + return work[:], nil + } else { + glog.Infof("%v\n", err) + } + return nil, fmt.Errorf("mining not ready") +} + +// SubmitHashrate can be used for remote miners to submit their hash rate. This enables the node to report the combined +// hash rate of all miners which submit work through this node. It accepts the miner hash rate and an identifier which +// must be unique between nodes. +func (s *PublicMinerAPI) SubmitHashrate(hashrate rpc.HexNumber, id common.Hash) bool { + s.agent.SubmitHashrate(id, hashrate.Uint64()) + return true +} diff --git a/miner/worker.go b/miner/worker.go index 754a6fc48..1a411ae20 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -327,6 +327,7 @@ func (self *worker) wait() { go func(block *types.Block, logs vm.Logs, receipts []*types.Receipt) { self.mux.Post(core.NewMinedBlockEvent{block}) self.mux.Post(core.ChainEvent{block, block.Hash(), logs}) + if stat == core.CanonStatTy { self.mux.Post(core.ChainHeadEvent{block}) self.mux.Post(logs) |