From 82956df523fd75d0c3b4a49f571eee60935fc32e Mon Sep 17 00:00:00 2001 From: obscuren Date: Sun, 22 Mar 2015 15:38:01 +0100 Subject: [PATCH] Get work / submit work partially implemented. * WIP missing arguments for submitting new work * GetWork **done** --- eth/backend.go | 6 ++-- miner/miner.go | 8 ++++-- rpc/api.go | 23 +++++++++++++-- rpc/miner_agest.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++ xeth/xeth.go | 2 ++ 5 files changed, 101 insertions(+), 8 deletions(-) create mode 100644 rpc/miner_agest.go diff --git a/eth/backend.go b/eth/backend.go index 141c6c6055..c312f9b4b2 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -300,9 +300,9 @@ func (s *Ethereum) StartMining() error { return nil } -func (s *Ethereum) StopMining() { s.miner.Stop() } -func (s *Ethereum) IsMining() bool { return s.miner.Mining() } - +func (s *Ethereum) StopMining() { s.miner.Stop() } +func (s *Ethereum) IsMining() bool { return s.miner.Mining() } +func (s *Ethereum) Miner() *miner.Miner { return s.miner } func (s *Ethereum) Logger() logger.LogSystem { return s.logger } func (s *Ethereum) Name() string { return s.net.Name } func (s *Ethereum) AccountManager() *accounts.Manager { return s.accountManager } diff --git a/miner/miner.go b/miner/miner.go index ccc19c754c..d46fabc1ec 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -26,7 +26,7 @@ type Miner struct { func New(eth core.Backend, pow pow.PoW, minerThreads int) *Miner { // note: minerThreads is currently ignored because // ethash is not thread safe. - return &Miner{eth: eth, pow: pow} + return &Miner{eth: eth, pow: pow, worker: newWorker(common.Address{}, eth)} } func (self *Miner) Mining() bool { @@ -35,7 +35,7 @@ func (self *Miner) Mining() bool { func (self *Miner) Start(coinbase common.Address) { self.mining = true - self.worker = newWorker(coinbase, self.eth) + self.worker.coinbase = coinbase self.worker.register(NewCpuMiner(0, self.pow)) self.pow.(*ethash.Ethash).UpdateDAG() @@ -44,6 +44,10 @@ func (self *Miner) Start(coinbase common.Address) { self.worker.commitNewWork() } +func (self *Miner) Register(agent Agent) { + self.worker.register(agent) +} + func (self *Miner) Stop() { self.mining = false self.worker.stop() diff --git a/rpc/api.go b/rpc/api.go index 34d4ff0fc5..d5e18eec86 100644 --- a/rpc/api.go +++ b/rpc/api.go @@ -17,15 +17,20 @@ type EthereumApi struct { eth *xeth.XEth xethMu sync.RWMutex db common.Database + + // Miner agent + agent *Agent } func NewEthereumApi(eth *xeth.XEth, dataDir string) *EthereumApi { // What about when dataDir is empty? db, _ := ethdb.NewLDBDatabase(path.Join(dataDir, "dapps")) api := &EthereumApi{ - eth: eth, - db: db, + eth: eth, + db: db, + agent: NewAgent(), } + eth.Backend().Miner().Register(api.agent) return api } @@ -342,7 +347,13 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error } opts := toFilterOptions(args) *reply = NewLogsRes(p.xeth().AllLogs(opts)) - case "eth_getWork", "eth_submitWork": + case "eth_getWork": + *reply = p.getWork() + case "eth_submitWork": + // TODO what is the reply here? + // TODO what are the arguments? + p.agent.SetResult(0, common.Hash{}, common.Hash{}) + return NewNotImplementedError(req.Method) case "db_putString": args := new(DbArgs) @@ -427,6 +438,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } *reply = p.xeth().Whisper().Messages(args.Id) + // case "eth_register": // // Placeholder for actual type // args := new(HashIndexArgs) @@ -454,6 +466,11 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return nil } +func (p *EthereumApi) getWork() string { + p.xeth().SetMining(true) + return p.agent.GetWork().Hex() +} + func toFilterOptions(options *BlockFilterArgs) *core.FilterOptions { var opts core.FilterOptions diff --git a/rpc/miner_agest.go b/rpc/miner_agest.go new file mode 100644 index 0000000000..f883ef54ee --- /dev/null +++ b/rpc/miner_agest.go @@ -0,0 +1,70 @@ +package rpc + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/miner" +) + +type Agent struct { + work *types.Block + currentWork *types.Block + + quit chan struct{} + workCh chan *types.Block + returnCh chan<- miner.Work +} + +func NewAgent() *Agent { + agent := &Agent{} + go agent.run() + + return agent +} + +func (a *Agent) Work() chan<- *types.Block { + return a.workCh +} + +func (a *Agent) SetWorkCh(returnCh chan<- miner.Work) { + a.returnCh = returnCh +} + +func (a *Agent) Start() { + a.quit = make(chan struct{}) + a.workCh = make(chan *types.Block, 1) +} + +func (a *Agent) Stop() { + close(a.quit) + close(a.workCh) +} + +func (a *Agent) GetHashRate() int64 { return 0 } + +func (a *Agent) run() { +out: + for { + select { + case <-a.quit: + break out + case work := <-a.workCh: + a.work = work + } + } +} + +func (a *Agent) GetWork() common.Hash { + // XXX Wait here untill work != nil ? + if a.work != nil { + return a.work.HashNoNonce() + } + return common.Hash{} +} + +func (a *Agent) SetResult(nonce uint64, mixDigest, seedHash common.Hash) { + // Make sure the external miner was working on the right hash + if a.currentWork.Hash() == a.work.Hash() { + a.returnCh <- miner.Work{a.currentWork.Number().Uint64(), nonce, mixDigest.Bytes(), seedHash.Bytes()} + } +} diff --git a/xeth/xeth.go b/xeth/xeth.go index 9f183aa610..d3c3131d4b 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -17,6 +17,7 @@ import ( "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event/filter" "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/miner" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/whisper" @@ -43,6 +44,7 @@ type Backend interface { ExtraDb() common.Database EventMux() *event.TypeMux Whisper() *whisper.Whisper + Miner() *miner.Miner IsMining() bool StartMining() error