Commit 2f55a1d7 authored by Bas van Kervel's avatar Bas van Kervel Committed by Bas van Kervel

restructured eth rpc API

parent 6609d45e
package api
import "github.com/ethereum/go-ethereum/rpc/shared"
// Ethereum RPC API interface
type EthereumApi interface {
// Execute the given request and returns the response or an error
Execute(*shared.Request) (interface{}, error)
// List of supported RCP methods this API provides
Methods() []string
}
package api
import (
"testing"
"github.com/ethereum/go-ethereum/rpc/codec"
)
func TestParseApiString(t *testing.T) {
apis, err := ParseApiString("", codec.JSON, nil, nil)
if err == nil {
t.Errorf("Expected an err from parsing empty API string but got nil")
}
if len(apis) != 0 {
t.Errorf("Expected 0 apis from empty API string")
}
apis, err = ParseApiString("eth", codec.JSON, nil, nil)
if err != nil {
t.Errorf("Expected nil err from parsing empty API string but got %v", err)
}
if len(apis) != 1 {
t.Errorf("Expected 1 apis but got %d - %v", apis, apis)
}
apis, err = ParseApiString("eth,eth", codec.JSON, nil, nil)
if err != nil {
t.Errorf("Expected nil err from parsing empty API string but got \"%v\"", err)
}
if len(apis) != 2 {
t.Errorf("Expected 2 apis but got %d - %v", apis, apis)
}
apis, err = ParseApiString("eth,invalid", codec.JSON, nil, nil)
if err == nil {
t.Errorf("Expected an err but got no err")
}
}
This diff is collapsed.
This diff is collapsed.
package api
// JS api provided by web3.js
This diff is collapsed.
package api
import (
"strings"
"fmt"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/rpc/codec"
"github.com/ethereum/go-ethereum/xeth"
)
const (
EthApiName = "eth"
)
// Parse a comma separated API string to individual api's
func ParseApiString(apistr string, codec codec.Codec, xeth *xeth.XEth, eth *eth.Ethereum) ([]EthereumApi, error) {
if len(strings.TrimSpace(apistr)) == 0 {
return nil, fmt.Errorf("Empty apistr provided")
}
names := strings.Split(apistr, ",")
apis := make([]EthereumApi, len(names))
for i, name := range names {
switch strings.ToLower(strings.TrimSpace(name)) {
case EthApiName:
apis[i] = NewEthApi(xeth, codec)
default:
return nil, fmt.Errorf("Unknown API '%s'", name)
}
}
return apis, nil
}
package codec
import (
"net"
"strconv"
"github.com/ethereum/go-ethereum/rpc/shared"
)
type Codec int
// (de)serialization support for rpc interface
type ApiCoder interface {
// Parse message to request from underlying stream
ReadRequest() (*shared.Request, error)
// Parse response message from underlying stream
ReadResponse() (interface{}, error)
// Encode response to encoded form in underlying stream
WriteResponse(interface{}) error
// Decode single message from data
Decode([]byte, interface{}) error
// Encode msg to encoded form
Encode(msg interface{}) ([]byte, error)
// close the underlying stream
Close()
}
// supported codecs
const (
JSON Codec = iota
nCodecs
)
var (
// collection with supported coders
coders = make([]func(net.Conn) ApiCoder, nCodecs)
)
// create a new coder instance
func (c Codec) New(conn net.Conn) ApiCoder {
switch c {
case JSON:
return NewJsonCoder(conn)
}
panic("codec: request for codec #" + strconv.Itoa(int(c)) + " is unavailable")
}
package codec
import (
"encoding/json"
"net"
"github.com/ethereum/go-ethereum/rpc/shared"
)
const (
MAX_RESPONSE_SIZE = 64 * 1024
)
// Json serialization support
type JsonCodec struct {
c net.Conn
d *json.Decoder
e *json.Encoder
}
// Create new JSON coder instance
func NewJsonCoder(conn net.Conn) ApiCoder {
return &JsonCodec{
c: conn,
d: json.NewDecoder(conn),
e: json.NewEncoder(conn),
}
}
// Serialize obj to JSON and write it to conn
func (self *JsonCodec) ReadRequest() (*shared.Request, error) {
req := shared.Request{}
err := self.d.Decode(&req)
if err == nil {
return &req, nil
}
return nil, err
}
func (self *JsonCodec) ReadResponse() (interface{}, error) {
var err error
buf := make([]byte, MAX_RESPONSE_SIZE)
n, _ := self.c.Read(buf)
var failure shared.ErrorResponse
if err = json.Unmarshal(buf[:n], &failure); err == nil && failure.Error != nil {
return failure, nil
}
var success shared.SuccessResponse
if err = json.Unmarshal(buf[:n], &success); err == nil {
return success, nil
}
return nil, err
}
// Encode response to encoded form in underlying stream
func (self *JsonCodec) Decode(data []byte, msg interface{}) error {
return json.Unmarshal(data, msg)
}
func (self *JsonCodec) Encode(msg interface{}) ([]byte, error) {
return json.Marshal(msg)
}
// Parse JSON data from conn to obj
func (self *JsonCodec) WriteResponse(res interface{}) error {
return self.e.Encode(&res)
}
// Close decoder and encoder
func (self *JsonCodec) Close() {
self.c.Close()
}
package shared
import "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 {
have int
want int
}
func (e *InsufficientParamsError) Error() string {
return fmt.Sprintf("insufficient params, want %d have %d", e.want, e.have)
}
func NewInsufficientParamsError(have int, want int) *InsufficientParamsError {
return &InsufficientParamsError{
have: have,
want: want,
}
}
type NotImplementedError struct {
Method string
}
func (e *NotImplementedError) Error() string {
return fmt.Sprintf("%s method not implemented", e.Method)
}
func NewNotImplementedError(method string) *NotImplementedError {
return &NotImplementedError{
Method: method,
}
}
type DecodeParamError struct {
err string
}
func (e *DecodeParamError) Error() string {
return fmt.Sprintf("could not decode, %s", e.err)
}
func NewDecodeParamError(errstr string) error {
return &DecodeParamError{
err: errstr,
}
}
type ValidationError struct {
ParamName string
msg string
}
func (e *ValidationError) Error() string {
return fmt.Sprintf("%s not valid, %s", e.ParamName, e.msg)
}
func NewValidationError(param string, msg string) error {
return &ValidationError{
ParamName: param,
msg: msg,
}
}
type NotAvailableError struct {
Method string
Reason string
}
func (e *NotAvailableError) Error() string {
return fmt.Sprintf("%s method not available: %s", e.Method, e.Reason)
}
func NewNotAvailableError(method string, reason string) *NotAvailableError {
return &NotAvailableError{
Method: method,
Reason: reason,
}
}
package shared
import "encoding/json"
// RPC request
type Request struct {
Id interface{} `json:"id"`
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params json.RawMessage `json:"params"`
}
// RPC response
type Response struct {
Id interface{} `json:"id"`
Jsonrpc string `json:"jsonrpc"`
}
// RPC success response
type SuccessResponse struct {
Id interface{} `json:"id"`
Jsonrpc string `json:"jsonrpc"`
Result interface{} `json:"result"`
}
// RPC error response
type ErrorResponse struct {
Id interface{} `json:"id"`
Jsonrpc string `json:"jsonrpc"`
Error *ErrorObject `json:"error"`
}
// RPC error response details
type ErrorObject struct {
Code int `json:"code"`
Message string `json:"message"`
// Data interface{} `json:"data"`
}
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