diff options
Diffstat (limited to 'rpc')
-rw-r--r-- | rpc/api.go | 23 | ||||
-rw-r--r-- | rpc/jeth.go | 27 | ||||
-rw-r--r-- | rpc/miner_agest.go | 70 |
3 files changed, 109 insertions, 11 deletions
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/jeth.go b/rpc/jeth.go index 11d4599c9..4e83be8a6 100644 --- a/rpc/jeth.go +++ b/rpc/jeth.go @@ -3,22 +3,29 @@ package rpc import ( "encoding/json" // "fmt" - "github.com/obscuren/otto" + "github.com/ethereum/go-ethereum/jsre" + "github.com/robertkrimen/otto" ) type Jeth struct { ethApi *EthereumApi toVal func(interface{}) otto.Value + re *jsre.JSRE } -func NewJeth(ethApi *EthereumApi, toVal func(interface{}) otto.Value) *Jeth { - return &Jeth{ethApi, toVal} +func NewJeth(ethApi *EthereumApi, toVal func(interface{}) otto.Value, re *jsre.JSRE) *Jeth { + return &Jeth{ethApi, toVal, re} } -func (self *Jeth) err(code int, msg string, id interface{}) otto.Value { +func (self *Jeth) err(code int, msg string, id interface{}) (response otto.Value) { rpcerr := &RpcErrorObject{code, msg} - rpcresponse := &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: id, Error: rpcerr} - return self.toVal(rpcresponse) + self.re.Set("ret_jsonrpc", jsonrpcver) + self.re.Set("ret_id", id) + self.re.Set("ret_error", rpcerr) + response, _ = self.re.Run(` + ret_response = { jsonrpc: ret_jsonrpc, id: ret_id, error: ret_error }; + `) + return } func (self *Jeth) Send(call otto.FunctionCall) (response otto.Value) { @@ -37,7 +44,11 @@ func (self *Jeth) Send(call otto.FunctionCall) (response otto.Value) { if err != nil { return self.err(-32603, err.Error(), req.Id) } - rpcresponse := &RpcSuccessResponse{Jsonrpc: jsonrpcver, Id: req.Id, Result: respif} - response = self.toVal(rpcresponse) + self.re.Set("ret_jsonrpc", jsonrpcver) + self.re.Set("ret_id", req.Id) + self.re.Set("ret_result", respif) + response, err = self.re.Run(` + ret_response = { jsonrpc: ret_jsonrpc, id: ret_id, result: ret_result }; + `) return } diff --git a/rpc/miner_agest.go b/rpc/miner_agest.go new file mode 100644 index 000000000..64dba82a6 --- /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 != nil && a.work != nil && a.currentWork.Hash() == a.work.Hash() { + a.returnCh <- miner.Work{a.currentWork.Number().Uint64(), nonce, mixDigest.Bytes(), seedHash.Bytes()} + } +} |