diff options
Diffstat (limited to 'rpc')
-rw-r--r-- | rpc/api.go | 124 | ||||
-rw-r--r-- | rpc/args.go | 39 | ||||
-rw-r--r-- | rpc/jeth.go | 27 |
3 files changed, 124 insertions, 66 deletions
diff --git a/rpc/api.go b/rpc/api.go index 34d4ff0fc..427032995 100644 --- a/rpc/api.go +++ b/rpc/api.go @@ -19,29 +19,29 @@ type EthereumApi struct { db common.Database } -func NewEthereumApi(eth *xeth.XEth, dataDir string) *EthereumApi { +func NewEthereumApi(xeth *xeth.XEth, dataDir string) *EthereumApi { // What about when dataDir is empty? db, _ := ethdb.NewLDBDatabase(path.Join(dataDir, "dapps")) api := &EthereumApi{ - eth: eth, + eth: xeth, db: db, } return api } -func (self *EthereumApi) xeth() *xeth.XEth { - self.xethMu.RLock() - defer self.xethMu.RUnlock() +func (api *EthereumApi) xeth() *xeth.XEth { + api.xethMu.RLock() + defer api.xethMu.RUnlock() - return self.eth + return api.eth } -func (self *EthereumApi) xethAtStateNum(num int64) *xeth.XEth { - return self.xeth().AtStateNum(num) +func (api *EthereumApi) xethAtStateNum(num int64) *xeth.XEth { + return api.xeth().AtStateNum(num) } -func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error { +func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error { // Spec at https://github.com/ethereum/wiki/wiki/Generic-JSON-RPC rpclogger.Debugf("%s %s", req.Method, req.Params) @@ -53,31 +53,31 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error } *reply = common.ToHex(crypto.Sha3(common.FromHex(args.Data))) case "web3_clientVersion": - *reply = p.xeth().Backend().Version() + *reply = api.xeth().Backend().Version() case "net_version": return NewNotImplementedError(req.Method) case "net_listening": - *reply = p.xeth().IsListening() + *reply = api.xeth().IsListening() case "net_peerCount": - v := p.xeth().PeerCount() + v := api.xeth().PeerCount() *reply = common.ToHex(big.NewInt(int64(v)).Bytes()) case "eth_coinbase": // TODO handling of empty coinbase due to lack of accounts - res := p.xeth().Coinbase() + res := api.xeth().Coinbase() if res == "0x" || res == "0x0" { *reply = nil } else { *reply = res } case "eth_mining": - *reply = p.xeth().IsMining() + *reply = api.xeth().IsMining() case "eth_gasPrice": - v := p.xeth().DefaultGas() + v := api.xeth().DefaultGas() *reply = common.ToHex(v.Bytes()) case "eth_accounts": - *reply = p.xeth().Accounts() + *reply = api.xeth().Accounts() case "eth_blockNumber": - v := p.xeth().Backend().ChainManager().CurrentBlock().Number() + v := api.xeth().Backend().ChainManager().CurrentBlock().Number() *reply = common.ToHex(v.Bytes()) case "eth_getBalance": args := new(GetBalanceArgs) @@ -89,7 +89,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } - v := p.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address).Balance() + v := api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address).Balance() *reply = common.ToHex(v.Bytes()) case "eth_getStorage", "eth_storageAt": args := new(GetStorageArgs) @@ -101,7 +101,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } - *reply = p.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address).Storage() + *reply = api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address).Storage() case "eth_getStorageAt": args := new(GetStorageAtArgs) if err := json.Unmarshal(req.Params, &args); err != nil { @@ -111,7 +111,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } - state := p.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address) + state := api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address) value := state.StorageString(args.Key) *reply = common.Bytes2Hex(value.Bytes()) @@ -126,14 +126,14 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } - *reply = p.xethAtStateNum(args.BlockNumber).TxCountAt(args.Address) + *reply = api.xethAtStateNum(args.BlockNumber).TxCountAt(args.Address) case "eth_getBlockTransactionCountByHash": args := new(GetBlockByHashArgs) if err := json.Unmarshal(req.Params, &args); err != nil { return err } - block := NewBlockRes(p.xeth().EthBlockByHash(args.BlockHash)) + block := NewBlockRes(api.xeth().EthBlockByHash(args.BlockHash)) *reply = common.ToHex(big.NewInt(int64(len(block.Transactions))).Bytes()) case "eth_getBlockTransactionCountByNumber": args := new(GetBlockByNumberArgs) @@ -141,7 +141,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } - block := NewBlockRes(p.xeth().EthBlockByNumber(args.BlockNumber)) + block := NewBlockRes(api.xeth().EthBlockByNumber(args.BlockNumber)) *reply = common.ToHex(big.NewInt(int64(len(block.Transactions))).Bytes()) case "eth_getUncleCountByBlockHash": args := new(GetBlockByHashArgs) @@ -149,7 +149,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } - block := p.xeth().EthBlockByHash(args.BlockHash) + block := api.xeth().EthBlockByHash(args.BlockHash) br := NewBlockRes(block) *reply = common.ToHex(big.NewInt(int64(len(br.Uncles))).Bytes()) case "eth_getUncleCountByBlockNumber": @@ -158,7 +158,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } - block := p.xeth().EthBlockByNumber(args.BlockNumber) + block := api.xeth().EthBlockByNumber(args.BlockNumber) br := NewBlockRes(block) *reply = common.ToHex(big.NewInt(int64(len(br.Uncles))).Bytes()) case "eth_getData", "eth_getCode": @@ -169,7 +169,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error if err := args.requirements(); err != nil { return err } - *reply = p.xethAtStateNum(args.BlockNumber).CodeAt(args.Address) + *reply = api.xethAtStateNum(args.BlockNumber).CodeAt(args.Address) case "eth_sendTransaction", "eth_transact": args := new(NewTxArgs) if err := json.Unmarshal(req.Params, &args); err != nil { @@ -180,7 +180,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } - v, err := p.xeth().Transact(args.From, args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data) + v, err := api.xeth().Transact(args.From, args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data) if err != nil { return err } @@ -191,7 +191,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } - v, err := p.xethAtStateNum(args.BlockNumber).Call(args.From, args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data) + v, err := api.xethAtStateNum(args.BlockNumber).Call(args.From, args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data) if err != nil { return err } @@ -205,7 +205,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } - block := p.xeth().EthBlockByHash(args.BlockHash) + block := api.xeth().EthBlockByHash(args.BlockHash) br := NewBlockRes(block) br.fullTx = args.IncludeTxs @@ -216,7 +216,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } - block := p.xeth().EthBlockByNumber(args.BlockNumber) + block := api.xeth().EthBlockByNumber(args.BlockNumber) br := NewBlockRes(block) br.fullTx = args.IncludeTxs @@ -226,7 +226,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error args := new(HashIndexArgs) if err := json.Unmarshal(req.Params, &args); err != nil { } - tx := p.xeth().EthTransactionByHash(args.Hash) + tx := api.xeth().EthTransactionByHash(args.Hash) if tx != nil { *reply = NewTransactionRes(tx) } @@ -236,7 +236,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } - block := p.xeth().EthBlockByHash(args.Hash) + block := api.xeth().EthBlockByHash(args.Hash) br := NewBlockRes(block) br.fullTx = true @@ -250,7 +250,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } - block := p.xeth().EthBlockByNumber(args.BlockNumber) + block := api.xeth().EthBlockByNumber(args.BlockNumber) v := NewBlockRes(block) v.fullTx = true @@ -264,14 +264,14 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } - br := NewBlockRes(p.xeth().EthBlockByHash(args.Hash)) + br := NewBlockRes(api.xeth().EthBlockByHash(args.Hash)) if args.Index > int64(len(br.Uncles)) || args.Index < 0 { return NewValidationError("Index", "does not exist") } uhash := br.Uncles[args.Index].Hex() - uncle := NewBlockRes(p.xeth().EthBlockByHash(uhash)) + uncle := NewBlockRes(api.xeth().EthBlockByHash(uhash)) *reply = uncle case "eth_getUncleByBlockNumberAndIndex": @@ -280,7 +280,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } - block := p.xeth().EthBlockByNumber(args.BlockNumber) + block := api.xeth().EthBlockByNumber(args.BlockNumber) v := NewBlockRes(block) v.fullTx = true @@ -289,7 +289,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error } uhash := v.Uncles[args.Index].Hex() - uncle := NewBlockRes(p.xeth().EthBlockByHash(uhash)) + uncle := NewBlockRes(api.xeth().EthBlockByHash(uhash)) *reply = uncle case "eth_getCompilers": @@ -304,7 +304,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error } opts := toFilterOptions(args) - id := p.xeth().RegisterFilter(opts) + id := api.xeth().RegisterFilter(opts) *reply = common.ToHex(big.NewInt(int64(id)).Bytes()) case "eth_newBlockFilter": args := new(FilterStringArgs) @@ -315,35 +315,42 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } - id := p.xeth().NewFilterString(args.Word) + id := api.xeth().NewFilterString(args.Word) *reply = common.ToHex(big.NewInt(int64(id)).Bytes()) case "eth_uninstallFilter": args := new(FilterIdArgs) if err := json.Unmarshal(req.Params, &args); err != nil { return err } - *reply = p.xeth().UninstallFilter(args.Id) + *reply = api.xeth().UninstallFilter(args.Id) case "eth_getFilterChanges": args := new(FilterIdArgs) if err := json.Unmarshal(req.Params, &args); err != nil { return err } - *reply = NewLogsRes(p.xeth().FilterChanged(args.Id)) + *reply = NewLogsRes(api.xeth().FilterChanged(args.Id)) case "eth_getFilterLogs": args := new(FilterIdArgs) if err := json.Unmarshal(req.Params, &args); err != nil { return err } - *reply = NewLogsRes(p.xeth().Logs(args.Id)) + *reply = NewLogsRes(api.xeth().Logs(args.Id)) case "eth_getLogs": args := new(BlockFilterArgs) if err := json.Unmarshal(req.Params, &args); err != nil { return err } opts := toFilterOptions(args) - *reply = NewLogsRes(p.xeth().AllLogs(opts)) - case "eth_getWork", "eth_submitWork": - return NewNotImplementedError(req.Method) + *reply = NewLogsRes(api.xeth().AllLogs(opts)) + case "eth_getWork": + api.xeth().SetMining(true) + *reply = api.xeth().RemoteMining().GetWork() + case "eth_submitWork": + args := new(SubmitWorkArgs) + if err := json.Unmarshal(req.Params, &args); err != nil { + return err + } + *reply = api.xeth().RemoteMining().SubmitWork(args.Nonce, args.Digest, args.Header) case "db_putString": args := new(DbArgs) if err := json.Unmarshal(req.Params, &args); err != nil { @@ -354,7 +361,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } - p.db.Put([]byte(args.Database+args.Key), []byte(args.Value)) + api.db.Put([]byte(args.Database+args.Key), []byte(args.Value)) *reply = true case "db_getString": args := new(DbArgs) @@ -366,7 +373,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } - res, _ := p.db.Get([]byte(args.Database + args.Key)) + res, _ := api.db.Get([]byte(args.Database + args.Key)) *reply = string(res) case "db_putHex", "db_getHex": return NewNotImplementedError(req.Method) @@ -376,26 +383,26 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } - err := p.xeth().Whisper().Post(args.Payload, args.To, args.From, args.Topics, args.Priority, args.Ttl) + err := api.xeth().Whisper().Post(args.Payload, args.To, args.From, args.Topics, args.Priority, args.Ttl) if err != nil { return err } *reply = true case "shh_newIdentity": - *reply = p.xeth().Whisper().NewIdentity() + *reply = api.xeth().Whisper().NewIdentity() // case "shh_removeIdentity": // args := new(WhisperIdentityArgs) // if err := json.Unmarshal(req.Params, &args); err != nil { // return err // } - // *reply = p.xeth().Whisper().RemoveIdentity(args.Identity) + // *reply = api.xeth().Whisper().RemoveIdentity(args.Identity) case "shh_hasIdentity": args := new(WhisperIdentityArgs) if err := json.Unmarshal(req.Params, &args); err != nil { return err } - *reply = p.xeth().Whisper().HasIdentity(args.Identity) + *reply = api.xeth().Whisper().HasIdentity(args.Identity) case "shh_newGroup", "shh_addToGroup": return NewNotImplementedError(req.Method) case "shh_newFilter": @@ -407,45 +414,46 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error opts.From = args.From opts.To = args.To opts.Topics = args.Topics - id := p.xeth().NewWhisperFilter(opts) + id := api.xeth().NewWhisperFilter(opts) *reply = common.ToHex(big.NewInt(int64(id)).Bytes()) case "shh_uninstallFilter": args := new(FilterIdArgs) if err := json.Unmarshal(req.Params, &args); err != nil { return err } - *reply = p.xeth().UninstallWhisperFilter(args.Id) + *reply = api.xeth().UninstallWhisperFilter(args.Id) case "shh_getFilterChanges": args := new(FilterIdArgs) if err := json.Unmarshal(req.Params, &args); err != nil { return err } - *reply = p.xeth().MessagesChanged(args.Id) + *reply = api.xeth().MessagesChanged(args.Id) case "shh_getMessages": args := new(FilterIdArgs) if err := json.Unmarshal(req.Params, &args); err != nil { return err } - *reply = p.xeth().Whisper().Messages(args.Id) + *reply = api.xeth().Whisper().Messages(args.Id) + // case "eth_register": // // Placeholder for actual type // args := new(HashIndexArgs) // if err := json.Unmarshal(req.Params, &args); err != nil { // return err // } - // *reply = p.xeth().Register(args.Hash) + // *reply = api.xeth().Register(args.Hash) // case "eth_unregister": // args := new(HashIndexArgs) // if err := json.Unmarshal(req.Params, &args); err != nil { // return err // } - // *reply = p.xeth().Unregister(args.Hash) + // *reply = api.xeth().Unregister(args.Hash) // case "eth_watchTx": // args := new(HashIndexArgs) // if err := json.Unmarshal(req.Params, &args); err != nil { // return err // } - // *reply = p.xeth().PullWatchTx(args.Hash) + // *reply = api.xeth().PullWatchTx(args.Hash) default: return NewNotImplementedError(req.Method) } diff --git a/rpc/args.go b/rpc/args.go index e50c9b1f5..504e67c07 100644 --- a/rpc/args.go +++ b/rpc/args.go @@ -686,3 +686,42 @@ func (args *WhisperFilterArgs) UnmarshalJSON(b []byte) (err error) { return nil } + +type SubmitWorkArgs struct { + Nonce uint64 + Header common.Hash + Digest common.Hash +} + +func (args *SubmitWorkArgs) UnmarshalJSON(b []byte) (err error) { + var obj []interface{} + if err = json.Unmarshal(b, &obj); err != nil { + return NewDecodeParamError(err.Error()) + } + + if len(obj) < 3 { + return NewInsufficientParamsError(len(obj), 3) + } + + var objstr string + var ok bool + if objstr, ok = obj[0].(string); !ok { + return NewDecodeParamError("Nonce is not a string") + } + + args.Nonce = common.BytesToNumber(common.Hex2Bytes(objstr)) + + if objstr, ok = obj[1].(string); !ok { + return NewDecodeParamError("Header is not a string") + } + + args.Header = common.HexToHash(objstr) + + if objstr, ok = obj[2].(string); !ok { + return NewDecodeParamError("Digest is not a string") + } + + args.Digest = common.HexToHash(objstr) + + return nil +} 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 } |