diff options
Diffstat (limited to 'rpc/api.go')
-rw-r--r-- | rpc/api.go | 113 |
1 files changed, 75 insertions, 38 deletions
diff --git a/rpc/api.go b/rpc/api.go index 8bbe80bd8..2137de356 100644 --- a/rpc/api.go +++ b/rpc/api.go @@ -23,7 +23,7 @@ import ( var ( defaultGasPrice = big.NewInt(150000000000) defaultGas = big.NewInt(500000) - filterTickerTime = 15 * time.Second + filterTickerTime = 5 * time.Minute ) type EthereumApi struct { @@ -167,13 +167,16 @@ func (self *EthereumApi) NewFilter(args *FilterOptions, reply *interface{}) erro } func (self *EthereumApi) UninstallFilter(id int, reply *interface{}) error { - delete(self.logs, id) + if _, ok := self.logs[id]; ok { + delete(self.logs, id) + } + self.filterManager.UninstallFilter(id) *reply = true return nil } -func (self *EthereumApi) NewFilterString(args string, reply *interface{}) error { +func (self *EthereumApi) NewFilterString(args *FilterStringArgs, reply *interface{}) error { var id int filter := core.NewFilter(self.xeth().Backend()) @@ -183,10 +186,14 @@ func (self *EthereumApi) NewFilterString(args string, reply *interface{}) error self.logs[id].add(&state.StateLog{}) } - if args == "pending" { + + switch args.Word { + case "pending": filter.PendingCallback = callback - } else if args == "chain" { + case "latest": filter.BlockCallback = callback + default: + return NewValidationError("Word", "Must be `latest` or `pending`") } id = self.filterManager.InstallFilter(filter) @@ -234,7 +241,7 @@ func (p *EthereumApi) Transact(args *NewTxArgs, reply *interface{}) (err error) // p.register[args.From] = append(p.register[args.From], args) //} else { /* - account := accounts.Get(fromHex(args.From)) + account := accounts.Get(ethutil.FromHex(args.From)) if account != nil { if account.Unlocked() { if !unlockAccount(account) { @@ -242,7 +249,7 @@ func (p *EthereumApi) Transact(args *NewTxArgs, reply *interface{}) (err error) } } - result, _ := account.Transact(fromHex(args.To), fromHex(args.Value), fromHex(args.Gas), fromHex(args.GasPrice), fromHex(args.Data)) + result, _ := account.Transact(ethutil.FromHex(args.To), ethutil.FromHex(args.Value), ethutil.FromHex(args.Gas), ethutil.FromHex(args.GasPrice), ethutil.FromHex(args.Data)) if len(result) > 0 { *reply = toHex(result) } @@ -377,7 +384,13 @@ func (p *EthereumApi) NewWhisperFilter(args *WhisperFilterArgs, reply *interface } id = p.xeth().Whisper().Watch(opts) p.messages[id] = &whisperFilter{timeout: time.Now()} - *reply = id + *reply = toHex(big.NewInt(int64(id)).Bytes()) + return nil +} + +func (p *EthereumApi) UninstallWhisperFilter(id int, reply *interface{}) error { + delete(p.messages, id) + *reply = true return nil } @@ -412,6 +425,14 @@ func (p *EthereumApi) WhisperMessages(id int, reply *interface{}) error { return nil } +func (p *EthereumApi) GetTransactionByHash(hash string, reply *interface{}) error { + tx := p.xeth().EthTransactionByHash(hash) + if tx != nil { + *reply = NewTransactionRes(tx) + } + return nil +} + func (p *EthereumApi) GetBlockByHash(blockhash string, includetx bool) (*BlockRes, error) { block := p.xeth().EthBlockByHash(blockhash) br := NewBlockRes(block) @@ -459,13 +480,23 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error if err := json.Unmarshal(req.Params, &args); err != nil { return err } - *reply = toHex(crypto.Sha3(fromHex(args.Data))) + *reply = toHex(crypto.Sha3(ethutil.FromHex(args.Data))) + case "web3_clientVersion": + *reply = p.xeth().Backend().Version() + case "net_version": + return NewNotImplementedError(req.Method) case "net_listening": *reply = p.xeth().IsListening() case "net_peerCount": *reply = toHex(big.NewInt(int64(p.xeth().PeerCount())).Bytes()) case "eth_coinbase": - *reply = p.xeth().Coinbase() + // TODO handling of empty coinbase due to lack of accounts + res := p.xeth().Coinbase() + if res == "0x" || res == "0x0" { + *reply = nil + } else { + *reply = res + } case "eth_mining": *reply = p.xeth().IsMining() case "eth_gasPrice": @@ -542,7 +573,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } *reply = toHex(big.NewInt(v).Bytes()) - case "eth_getData": + case "eth_getData", "eth_getCode": args := new(GetDataArgs) if err := json.Unmarshal(req.Params, &args); err != nil { return err @@ -561,7 +592,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error } return p.Call(args, reply) case "eth_flush": - return errNotImplemented + return NewNotImplementedError(req.Method) case "eth_getBlockByHash": args := new(GetBlockByHashArgs) if err := json.Unmarshal(req.Params, &args); err != nil { @@ -585,19 +616,23 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error } *reply = v case "eth_getTransactionByHash": - return errNotImplemented + // HashIndexArgs used, but only the "Hash" part we need. + args := new(HashIndexArgs) + if err := json.Unmarshal(req.Params, &args); err != nil { + } + return p.GetTransactionByHash(args.Hash, reply) case "eth_getTransactionByBlockHashAndIndex": args := new(HashIndexArgs) if err := json.Unmarshal(req.Params, &args); err != nil { return err } - v, err := p.GetBlockByHash(args.BlockHash, true) + v, err := p.GetBlockByHash(args.Hash, true) if err != nil { return err } if args.Index > int64(len(v.Transactions)) || args.Index < 0 { - return NewErrorWithMessage(errDecodeArgs, "Transaction index does not exist") + return NewValidationError("Index", "does not exist") } *reply = v.Transactions[args.Index] case "eth_getTransactionByBlockNumberAndIndex": @@ -611,7 +646,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } if args.Index > int64(len(v.Transactions)) || args.Index < 0 { - return NewErrorWithMessage(errDecodeArgs, "Transaction index does not exist") + return NewValidationError("Index", "does not exist") } *reply = v.Transactions[args.Index] case "eth_getUncleByBlockHashAndIndex": @@ -620,12 +655,12 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } - v, err := p.GetBlockByHash(args.BlockHash, false) + v, err := p.GetBlockByHash(args.Hash, false) if err != nil { return err } if args.Index > int64(len(v.Uncles)) || args.Index < 0 { - return NewErrorWithMessage(errDecodeArgs, "Uncle index does not exist") + return NewValidationError("Index", "does not exist") } uncle, err := p.GetBlockByHash(toHex(v.Uncles[args.Index]), false) @@ -644,7 +679,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } if args.Index > int64(len(v.Uncles)) || args.Index < 0 { - return NewErrorWithMessage(errDecodeArgs, "Uncle index does not exist") + return NewValidationError("Index", "does not exist") } uncle, err := p.GetBlockByHash(toHex(v.Uncles[args.Index]), false) @@ -654,10 +689,8 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error *reply = uncle case "eth_getCompilers": return p.GetCompilers(reply) - case "eth_compileSolidity": - case "eth_compileLLL": - case "eth_compileSerpent": - return errNotImplemented + case "eth_compileSolidity", "eth_compileLLL", "eth_compileSerpent": + return NewNotImplementedError(req.Method) case "eth_newFilter": args := new(FilterOptions) if err := json.Unmarshal(req.Params, &args); err != nil { @@ -669,7 +702,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error if err := json.Unmarshal(req.Params, &args); err != nil { return err } - return p.NewFilterString(args.Word, reply) + return p.NewFilterString(args, reply) case "eth_uninstallFilter": args := new(FilterIdArgs) if err := json.Unmarshal(req.Params, &args); err != nil { @@ -694,21 +727,22 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } return p.AllLogs(args, reply) - case "eth_getWork": - case "eth_submitWork": - return errNotImplemented - case "db_put": + case "eth_getWork", "eth_submitWork": + return NewNotImplementedError(req.Method) + case "db_putString": args := new(DbArgs) if err := json.Unmarshal(req.Params, &args); err != nil { return err } return p.DbPut(args, reply) - case "db_get": + case "db_getString": args := new(DbArgs) if err := json.Unmarshal(req.Params, &args); err != nil { return err } return p.DbGet(args, reply) + case "db_putHex", "db_getHex": + return NewNotImplementedError(req.Method) case "shh_post": args := new(WhisperMessageArgs) if err := json.Unmarshal(req.Params, &args); err != nil { @@ -723,9 +757,8 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return err } return p.HasWhisperIdentity(args.Identity, reply) - case "shh_newGroup": - case "shh_addToGroup": - return errNotImplemented + case "shh_newGroup", "shh_addToGroup": + return NewNotImplementedError(req.Method) case "shh_newFilter": args := new(WhisperFilterArgs) if err := json.Unmarshal(req.Params, &args); err != nil { @@ -733,7 +766,11 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error } return p.NewWhisperFilter(args, reply) case "shh_uninstallFilter": - return errNotImplemented + args := new(FilterIdArgs) + if err := json.Unmarshal(req.Params, &args); err != nil { + return err + } + return p.UninstallWhisperFilter(args.Id, reply) case "shh_getFilterChanges": args := new(FilterIdArgs) if err := json.Unmarshal(req.Params, &args); err != nil { @@ -765,7 +802,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error // } // return p.WatchTx(args, reply) default: - return NewErrorWithMessage(errNotImplemented, req.Method) + return NewNotImplementedError(req.Method) } rpclogger.DebugDetailf("Reply: %T %s", reply, reply) @@ -784,12 +821,12 @@ func toFilterOptions(options *FilterOptions) core.FilterOptions { // Convert optional address slice/string to byte slice if str, ok := options.Address.(string); ok { - opts.Address = [][]byte{fromHex(str)} + opts.Address = [][]byte{ethutil.FromHex(str)} } else if slice, ok := options.Address.([]interface{}); ok { bslice := make([][]byte, len(slice)) for i, addr := range slice { if saddr, ok := addr.(string); ok { - bslice[i] = fromHex(saddr) + bslice[i] = ethutil.FromHex(saddr) } } opts.Address = bslice @@ -803,11 +840,11 @@ func toFilterOptions(options *FilterOptions) core.FilterOptions { if slice, ok := topicDat.([]interface{}); ok { topics[i] = make([][]byte, len(slice)) for j, topic := range slice { - topics[i][j] = fromHex(topic.(string)) + topics[i][j] = ethutil.FromHex(topic.(string)) } } else if str, ok := topicDat.(string); ok { topics[i] = make([][]byte, 1) - topics[i][0] = fromHex(str) + topics[i][0] = ethutil.FromHex(str) } } opts.Topics = topics |