Commit b9ca5eef authored by Jeffrey Wilcke's avatar Jeffrey Wilcke

Merge pull request #579 from tgerring/rpcargs

RPC Args
parents 696ff43d 43d521e9
...@@ -12,17 +12,6 @@ type AccountChange struct { ...@@ -12,17 +12,6 @@ type AccountChange struct {
Address, StateAddress []byte Address, StateAddress []byte
} }
type FilterOptions struct {
Earliest int64
Latest int64
Address []common.Address
Topics [][]common.Hash
Skip int
Max int
}
// Filtering interface // Filtering interface
type Filter struct { type Filter struct {
eth Backend eth Backend
...@@ -44,18 +33,6 @@ func NewFilter(eth Backend) *Filter { ...@@ -44,18 +33,6 @@ func NewFilter(eth Backend) *Filter {
return &Filter{eth: eth} return &Filter{eth: eth}
} }
// SetOptions copies the filter options to the filter it self. The reason for this "silly" copy
// is simply because named arguments in this case is extremely nice and readable.
func (self *Filter) SetOptions(options *FilterOptions) {
self.earliest = options.Earliest
self.latest = options.Latest
self.skip = options.Skip
self.max = options.Max
self.address = options.Address
self.topics = options.Topics
}
// Set the earliest and latest block for filtering. // Set the earliest and latest block for filtering.
// -1 = latest block (i.e., the current block) // -1 = latest block (i.e., the current block)
// hash = particular hash from-to // hash = particular hash from-to
......
...@@ -6,7 +6,6 @@ import ( ...@@ -6,7 +6,6 @@ import (
"sync" "sync"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/xeth" "github.com/ethereum/go-ethereum/xeth"
) )
...@@ -82,10 +81,6 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -82,10 +81,6 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
return err return err
} }
if err := args.requirements(); err != nil {
return err
}
v := api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address).Balance() v := api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address).Balance()
*reply = common.ToHex(v.Bytes()) *reply = common.ToHex(v.Bytes())
case "eth_getStorage", "eth_storageAt": case "eth_getStorage", "eth_storageAt":
...@@ -94,19 +89,12 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -94,19 +89,12 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
return err return err
} }
if err := args.requirements(); err != nil {
return err
}
*reply = api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address).Storage() *reply = api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address).Storage()
case "eth_getStorageAt": case "eth_getStorageAt":
args := new(GetStorageAtArgs) args := new(GetStorageAtArgs)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
return err return err
} }
if err := args.requirements(); err != nil {
return err
}
state := api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address) state := api.xethAtStateNum(args.BlockNumber).State().SafeGet(args.Address)
value := state.StorageString(args.Key) value := state.StorageString(args.Key)
...@@ -118,11 +106,6 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -118,11 +106,6 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
return err return err
} }
err := args.requirements()
if err != nil {
return err
}
*reply = api.xethAtStateNum(args.BlockNumber).TxCountAt(args.Address) *reply = api.xethAtStateNum(args.BlockNumber).TxCountAt(args.Address)
case "eth_getBlockTransactionCountByHash": case "eth_getBlockTransactionCountByHash":
args := new(GetBlockByHashArgs) args := new(GetBlockByHashArgs)
...@@ -163,9 +146,6 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -163,9 +146,6 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
return err return err
} }
if err := args.requirements(); err != nil {
return err
}
*reply = api.xethAtStateNum(args.BlockNumber).CodeAt(args.Address) *reply = api.xethAtStateNum(args.BlockNumber).CodeAt(args.Address)
case "eth_sendTransaction", "eth_transact": case "eth_sendTransaction", "eth_transact":
args := new(NewTxArgs) args := new(NewTxArgs)
...@@ -173,10 +153,6 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -173,10 +153,6 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
return err return err
} }
if err := args.requirements(); err != nil {
return err
}
v, err := api.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 { if err != nil {
return err return err
...@@ -267,8 +243,8 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -267,8 +243,8 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
return NewValidationError("Index", "does not exist") return NewValidationError("Index", "does not exist")
} }
uhash := br.Uncles[args.Index].Hex() uhash := br.Uncles[args.Index]
uncle := NewBlockRes(api.xeth().EthBlockByHash(uhash)) uncle := NewBlockRes(api.xeth().EthBlockByHash(uhash.Hex()))
*reply = uncle *reply = uncle
case "eth_getUncleByBlockNumberAndIndex": case "eth_getUncleByBlockNumberAndIndex":
...@@ -285,8 +261,8 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -285,8 +261,8 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
return NewValidationError("Index", "does not exist") return NewValidationError("Index", "does not exist")
} }
uhash := v.Uncles[args.Index].Hex() uhash := v.Uncles[args.Index]
uncle := NewBlockRes(api.xeth().EthBlockByHash(uhash)) uncle := NewBlockRes(api.xeth().EthBlockByHash(uhash.Hex()))
*reply = uncle *reply = uncle
case "eth_getCompilers": case "eth_getCompilers":
...@@ -300,18 +276,13 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -300,18 +276,13 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
return err return err
} }
opts := toFilterOptions(args) id := api.xeth().RegisterFilter(args.Earliest, args.Latest, args.Skip, args.Max, args.Address, args.Topics)
id := api.xeth().RegisterFilter(opts)
*reply = common.ToHex(big.NewInt(int64(id)).Bytes()) *reply = common.ToHex(big.NewInt(int64(id)).Bytes())
case "eth_newBlockFilter": case "eth_newBlockFilter":
args := new(FilterStringArgs) args := new(FilterStringArgs)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
return err return err
} }
if err := args.requirements(); err != nil {
return err
}
id := api.xeth().NewFilterString(args.Word) id := api.xeth().NewFilterString(args.Word)
*reply = common.ToHex(big.NewInt(int64(id)).Bytes()) *reply = common.ToHex(big.NewInt(int64(id)).Bytes())
case "eth_uninstallFilter": case "eth_uninstallFilter":
...@@ -337,8 +308,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -337,8 +308,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
return err return err
} }
opts := toFilterOptions(args) *reply = NewLogsRes(api.xeth().AllLogs(args.Earliest, args.Latest, args.Skip, args.Max, args.Address, args.Topics))
*reply = NewLogsRes(api.xeth().AllLogs(opts))
case "eth_getWork": case "eth_getWork":
api.xeth().SetMining(true) api.xeth().SetMining(true)
*reply = api.xeth().RemoteMining().GetWork() *reply = api.xeth().RemoteMining().GetWork()
...@@ -347,7 +317,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -347,7 +317,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
return err return err
} }
*reply = api.xeth().RemoteMining().SubmitWork(args.Nonce, args.Digest, args.Header) *reply = api.xeth().RemoteMining().SubmitWork(args.Nonce, common.HexToHash(args.Digest), common.HexToHash(args.Header))
case "db_putString": case "db_putString":
args := new(DbArgs) args := new(DbArgs)
if err := json.Unmarshal(req.Params, &args); err != nil { if err := json.Unmarshal(req.Params, &args); err != nil {
...@@ -433,7 +403,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -433,7 +403,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
return err return err
} }
opts := new(xeth.Options) opts := new(xeth.Options)
opts.From = args.From // opts.From = args.From
opts.To = args.To opts.To = args.To
opts.Topics = args.Topics opts.Topics = args.Topics
id := api.xeth().NewWhisperFilter(opts) id := api.xeth().NewWhisperFilter(opts)
...@@ -483,46 +453,3 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err ...@@ -483,46 +453,3 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
rpclogger.DebugDetailf("Reply: %T %s", reply, reply) rpclogger.DebugDetailf("Reply: %T %s", reply, reply)
return nil return nil
} }
func toFilterOptions(options *BlockFilterArgs) *core.FilterOptions {
var opts core.FilterOptions
// Convert optional address slice/string to byte slice
if str, ok := options.Address.(string); ok {
opts.Address = []common.Address{common.HexToAddress(str)}
} else if slice, ok := options.Address.([]interface{}); ok {
bslice := make([]common.Address, len(slice))
for i, addr := range slice {
if saddr, ok := addr.(string); ok {
bslice[i] = common.HexToAddress(saddr)
}
}
opts.Address = bslice
}
opts.Earliest = options.Earliest
opts.Latest = options.Latest
topics := make([][]common.Hash, len(options.Topics))
for i, topicDat := range options.Topics {
if slice, ok := topicDat.([]interface{}); ok {
topics[i] = make([]common.Hash, len(slice))
for j, topic := range slice {
topics[i][j] = common.HexToHash(topic.(string))
}
} else if str, ok := topicDat.(string); ok {
topics[i] = []common.Hash{common.HexToHash(str)}
}
}
opts.Topics = topics
return &opts
}
/*
Work() chan<- *types.Block
SetWorkCh(chan<- Work)
Stop()
Start()
Rate() uint64
*/
This diff is collapsed.
This diff is collapsed.
...@@ -21,6 +21,22 @@ import ( ...@@ -21,6 +21,22 @@ import (
"fmt" "fmt"
) )
type InvalidTypeError struct {
method string
msg string
}
func (e *InvalidTypeError) Error() string {
return fmt.Sprintf("invalid type on field %s: %s", e.method, e.msg)
}
func NewInvalidTypeError(method, msg string) *InvalidTypeError {
return &InvalidTypeError{
method: method,
msg: msg,
}
}
type InsufficientParamsError struct { type InsufficientParamsError struct {
have int have int
want int want int
......
...@@ -4,6 +4,15 @@ import ( ...@@ -4,6 +4,15 @@ import (
"testing" "testing"
) )
func TestInvalidTypeError(t *testing.T) {
err := NewInvalidTypeError("testField", "not string")
expected := "invalid type on field testField: not string"
if err.Error() != expected {
t.Error(err.Error())
}
}
func TestInsufficientParamsError(t *testing.T) { func TestInsufficientParamsError(t *testing.T) {
err := NewInsufficientParamsError(0, 1) err := NewInsufficientParamsError(0, 1)
expected := "insufficient params, want 1 have 0" expected := "insufficient params, want 1 have 0"
......
...@@ -110,6 +110,24 @@ func (self *XEth) stop() { ...@@ -110,6 +110,24 @@ func (self *XEth) stop() {
close(self.quit) close(self.quit)
} }
func cAddress(a []string) []common.Address {
bslice := make([]common.Address, len(a))
for i, addr := range a {
bslice[i] = common.HexToAddress(addr)
}
return bslice
}
func cTopics(t [][]string) [][]common.Hash {
topics := make([][]common.Hash, len(t))
for i, iv := range t {
for j, jv := range iv {
topics[i][j] = common.HexToHash(jv)
}
}
return topics
}
func (self *XEth) DefaultGas() *big.Int { return defaultGas } func (self *XEth) DefaultGas() *big.Int { return defaultGas }
func (self *XEth) DefaultGasPrice() *big.Int { return defaultGasPrice } func (self *XEth) DefaultGasPrice() *big.Int { return defaultGasPrice }
...@@ -228,15 +246,15 @@ func (self *XEth) IsMining() bool { ...@@ -228,15 +246,15 @@ func (self *XEth) IsMining() bool {
} }
func (self *XEth) EthVersion() string { func (self *XEth) EthVersion() string {
return string(self.backend.EthVersion()) return fmt.Sprintf("%d", self.backend.EthVersion())
} }
func (self *XEth) NetworkVersion() string { func (self *XEth) NetworkVersion() string {
return string(self.backend.NetVersion()) return fmt.Sprintf("%d", self.backend.NetVersion())
} }
func (self *XEth) WhisperVersion() string { func (self *XEth) WhisperVersion() string {
return string(self.backend.ShhVersion()) return fmt.Sprintf("%d", self.backend.ShhVersion())
} }
func (self *XEth) ClientVersion() string { func (self *XEth) ClientVersion() string {
...@@ -301,10 +319,15 @@ func (self *XEth) SecretToAddress(key string) string { ...@@ -301,10 +319,15 @@ func (self *XEth) SecretToAddress(key string) string {
return common.ToHex(pair.Address()) return common.ToHex(pair.Address())
} }
func (self *XEth) RegisterFilter(args *core.FilterOptions) int { func (self *XEth) RegisterFilter(earliest, latest int64, skip, max int, address []string, topics [][]string) int {
var id int var id int
filter := core.NewFilter(self.backend) filter := core.NewFilter(self.backend)
filter.SetOptions(args) filter.SetEarliestBlock(earliest)
filter.SetLatestBlock(latest)
filter.SetSkip(skip)
filter.SetMax(max)
filter.SetAddress(cAddress(address))
filter.SetTopics(cTopics(topics))
filter.LogsCallback = func(logs state.Logs) { filter.LogsCallback = func(logs state.Logs) {
self.logMut.Lock() self.logMut.Lock()
defer self.logMut.Unlock() defer self.logMut.Unlock()
...@@ -380,9 +403,14 @@ func (self *XEth) Logs(id int) state.Logs { ...@@ -380,9 +403,14 @@ func (self *XEth) Logs(id int) state.Logs {
return nil return nil
} }
func (self *XEth) AllLogs(args *core.FilterOptions) state.Logs { func (self *XEth) AllLogs(earliest, latest int64, skip, max int, address []string, topics [][]string) state.Logs {
filter := core.NewFilter(self.backend) filter := core.NewFilter(self.backend)
filter.SetOptions(args) filter.SetEarliestBlock(earliest)
filter.SetLatestBlock(latest)
filter.SetSkip(skip)
filter.SetMax(max)
filter.SetAddress(cAddress(address))
filter.SetTopics(cTopics(topics))
return filter.Find() return filter.Find()
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment