From 82956df523fd75d0c3b4a49f571eee60935fc32e Mon Sep 17 00:00:00 2001 From: obscuren Date: Sun, 22 Mar 2015 15:38:01 +0100 Subject: Get work / submit work partially implemented. * WIP missing arguments for submitting new work * GetWork **done** --- rpc/api.go | 23 +++++++++++++++--- rpc/miner_agest.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 rpc/miner_agest.go (limited to 'rpc') diff --git a/rpc/api.go b/rpc/api.go index 34d4ff0fc..d5e18eec8 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 000000000..f883ef54e --- /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()} + } +} -- cgit v1.2.3