Commit a4a4e9fc authored by Bas van Kervel's avatar Bas van Kervel

removed old rpc structure and added new inproc api client

parent 3e1d635f
This diff is collapsed.
......@@ -33,6 +33,10 @@ import (
"github.com/ethereum/go-ethereum/eth"
re "github.com/ethereum/go-ethereum/jsre"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/rpc/api"
"github.com/ethereum/go-ethereum/rpc/codec"
"github.com/ethereum/go-ethereum/rpc/comms"
"github.com/ethereum/go-ethereum/rpc/shared"
"github.com/ethereum/go-ethereum/xeth"
"github.com/peterh/liner"
"github.com/robertkrimen/otto"
......@@ -70,10 +74,70 @@ type jsre struct {
ps1 string
atexit func()
corsDomain string
client comms.EthereumClient
prompter
}
func newJSRE(ethereum *eth.Ethereum, libPath, corsDomain, ipcpath string, interactive bool, f xeth.Frontend) *jsre {
var (
loadedModulesMethods map[string][]string
)
func keywordCompleter(line string) []string {
results := make([]string, 0)
if strings.Contains(line, ".") {
elements := strings.Split(line, ".")
if len(elements) == 2 {
module := elements[0]
partialMethod := elements[1]
if methods, found := loadedModulesMethods[module]; found {
for _, method := range methods {
if strings.HasPrefix(method, partialMethod) { // e.g. debug.se
results = append(results, module+"."+method)
}
}
}
}
} else {
for module, methods := range loadedModulesMethods {
if line == module { // user typed in full module name, show all methods
for _, method := range methods {
results = append(results, module+"."+method)
}
} else if strings.HasPrefix(module, line) { // partial method name, e.g. admi
results = append(results, module)
}
}
}
return results
}
func apiWordCompleter(line string, pos int) (head string, completions []string, tail string) {
if len(line) == 0 {
return "", nil, ""
}
i := 0
for i = pos - 1; i > 0; i-- {
if line[i] == '.' || (line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z') {
continue
}
if i >= 3 && line[i] == '3' && line[i-3] == 'w' && line[i-2] == 'e' && line[i-1] == 'b' {
continue
}
i += 1
break
}
begin := line[:i]
keyword := line[i:pos]
end := line[pos:]
completionWords := keywordCompleter(keyword)
return begin, completionWords, end
}
func newJSRE(ethereum *eth.Ethereum, libPath, corsDomain string, client comms.EthereumClient, interactive bool, f xeth.Frontend) *jsre {
js := &jsre{ethereum: ethereum, ps1: "> "}
// set default cors domain used by startRpc from CLI flag
js.corsDomain = corsDomain
......@@ -82,10 +146,16 @@ func newJSRE(ethereum *eth.Ethereum, libPath, corsDomain, ipcpath string, intera
}
js.xeth = xeth.New(ethereum, f)
js.wait = js.xeth.UpdateState()
js.client = client
if clt, ok := js.client.(*comms.InProcClient); ok {
clt.Initialize(js.xeth, ethereum)
}
// update state in separare forever blocks
js.re = re.New(libPath)
js.apiBindings(ipcpath, f)
js.adminBindings()
if err := js.apiBindings(f); err != nil {
utils.Fatalf("Unable to connect - %v", err)
}
if !liner.TerminalSupported() || !interactive {
js.prompter = dumbterm{bufio.NewReader(os.Stdin)}
......@@ -93,6 +163,9 @@ func newJSRE(ethereum *eth.Ethereum, libPath, corsDomain, ipcpath string, intera
lr := liner.NewLiner()
js.withHistory(func(hist *os.File) { lr.ReadHistory(hist) })
lr.SetCtrlCAborts(true)
js.loadAutoCompletion()
lr.SetWordCompleter(apiWordCompleter)
lr.SetTabCompletionStyle(liner.TabPrints)
js.prompter = lr
js.atexit = func() {
js.withHistory(func(hist *os.File) { hist.Truncate(0); lr.WriteHistory(hist) })
......@@ -103,8 +176,117 @@ func newJSRE(ethereum *eth.Ethereum, libPath, corsDomain, ipcpath string, intera
return js
}
func (self *jsre) loadAutoCompletion() {
if modules, err := self.suportedApis(); err == nil {
loadedModulesMethods = make(map[string][]string)
for module, _ := range modules {
loadedModulesMethods[module] = api.AutoCompletion[module]
}
}
}
func (self *jsre) suportedApis() (map[string]string, error) {
req := shared.Request{
Id: 1,
Jsonrpc: "2.0",
Method: "modules",
}
err := self.client.Send(&req)
if err != nil {
return nil, err
}
res, err := self.client.Recv()
if err != nil {
return nil, err
}
if sucRes, ok := res.(map[string]string); ok {
if err == nil {
return sucRes, nil
}
}
return nil, fmt.Errorf("Unable to determine supported API's")
}
func (js *jsre) apiBindings(f xeth.Frontend) error {
apis, err := js.suportedApis()
if err != nil {
return err
}
apiNames := make([]string, 0, len(apis))
for a, _ := range apis {
apiNames = append(apiNames, a)
}
apiImpl, err := api.ParseApiString(strings.Join(apiNames, ","), codec.JSON, js.xeth, js.ethereum)
if err != nil {
utils.Fatalf("Unable to determine supported api's: %v", err)
}
jeth := rpc.NewJeth(api.Merge(apiImpl...), js.re, js.client)
js.re.Set("jeth", struct{}{})
t, _ := js.re.Get("jeth")
jethObj := t.Object()
jethObj.Set("send", jeth.Send)
jethObj.Set("sendAsync", jeth.Send)
err = js.re.Compile("bignumber.js", re.BigNumber_JS)
if err != nil {
utils.Fatalf("Error loading bignumber.js: %v", err)
}
err = js.re.Compile("ethereum.js", re.Web3_JS)
if err != nil {
utils.Fatalf("Error loading web3.js: %v", err)
}
_, err = js.re.Eval("var web3 = require('web3');")
if err != nil {
utils.Fatalf("Error requiring web3: %v", err)
}
_, err = js.re.Eval("web3.setProvider(jeth)")
if err != nil {
utils.Fatalf("Error setting web3 provider: %v", err)
}
// load only supported API's in javascript runtime
shortcuts := "var eth = web3.eth; "
for _, apiName := range apiNames {
if apiName == api.Web3ApiName || apiName == api.EthApiName {
continue // manually mapped
}
if err = js.re.Compile(fmt.Sprintf("%s.js", apiName), api.Javascript(apiName)); err == nil {
shortcuts += fmt.Sprintf("var %s = web3.%s; ", apiName, apiName)
} else {
utils.Fatalf("Error loading %s.js: %v", apiName, err)
}
}
_, err = js.re.Eval(shortcuts)
if err != nil {
utils.Fatalf("Error setting namespaces: %v", err)
}
js.re.Eval(globalRegistrar + "registrar = GlobalRegistrar.at(\"" + globalRegistrarAddr + "\");")
return nil
}
/*
func (js *jsre) apiBindings(ipcpath string, f xeth.Frontend) {
xe := xeth.New(js.ethereum, f)
apiNames, err := js.suportedApis(ipcpath)
if err != nil {
return
}
ethApi := rpc.NewEthereumApi(xe)
jeth := rpc.NewJeth(ethApi, js.re, ipcpath)
......@@ -146,6 +328,7 @@ var net = web3.net;
js.re.Eval(globalRegistrar + "registrar = GlobalRegistrar.at(\"" + globalRegistrarAddr + "\");")
}
*/
var ds, _ = docserver.New("/")
......
......@@ -38,6 +38,8 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/rpc/codec"
"github.com/ethereum/go-ethereum/rpc/comms"
"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
)
......@@ -310,12 +312,14 @@ func console(ctx *cli.Context) {
utils.Fatalf("%v", err)
}
client := comms.NewInProcClient(codec.JSON)
startEth(ctx, ethereum)
repl := newJSRE(
ethereum,
ctx.String(utils.JSpathFlag.Name),
ctx.GlobalString(utils.RPCCORSDomainFlag.Name),
utils.IpcSocketPath(ctx),
client,
true,
nil,
)
......@@ -332,12 +336,13 @@ func execJSFiles(ctx *cli.Context) {
utils.Fatalf("%v", err)
}
client := comms.NewInProcClient(codec.JSON)
startEth(ctx, ethereum)
repl := newJSRE(
ethereum,
ctx.String(utils.JSpathFlag.Name),
ctx.GlobalString(utils.RPCCORSDomainFlag.Name),
utils.IpcSocketPath(ctx),
client,
false,
nil,
)
......
......@@ -211,7 +211,7 @@ var (
RpcApiFlag = cli.StringFlag{
Name: "rpcapi",
Usage: "Specify the API's which are offered over the HTTP RPC interface",
Value: api.DefaultHttpRpcApis,
Value: comms.DefaultHttpRpcApis,
}
IPCDisabledFlag = cli.BoolFlag{
Name: "ipcdisable",
......@@ -220,7 +220,7 @@ var (
IPCApiFlag = cli.StringFlag{
Name: "ipcapi",
Usage: "Specify the API's which are offered over the IPC interface",
Value: api.DefaultIpcApis,
Value: comms.DefaultIpcApis,
}
IPCPathFlag = DirectoryFlag{
Name: "ipcpath",
......
......@@ -149,7 +149,7 @@ func (pool *TxPool) validateTx(tx *types.Transaction) error {
return ErrInvalidSender
}
// Make sure the account exist. Non existant accounts
// Make sure the account exist. Non existent accounts
// haven't got funds and well therefor never pass.
if !pool.currentState().HasAccount(from) {
return ErrNonExistentAccount
......
This diff is collapsed.
......@@ -18,15 +18,13 @@ const (
TxPoolApiName = "txpool"
PersonalApiName = "personal"
Web3ApiName = "web3"
JsonRpcVersion = "2.0"
)
var (
DefaultHttpRpcApis = strings.Join([]string{
DbApiName, EthApiName, NetApiName, Web3ApiName,
}, ",")
// List with all API's which are offered over the IPC interface by default
DefaultIpcApis = strings.Join([]string{
// All API's
AllApis = strings.Join([]string{
AdminApiName, DbApiName, EthApiName, DebugApiName, MinerApiName, NetApiName,
ShhApiName, TxPoolApiName, PersonalApiName, Web3ApiName,
}, ",")
......
package api
import (
"encoding/json"
"github.com/ethereum/go-ethereum/rpc/shared"
)
type CompileArgs struct {
Source string
}
func (args *CompileArgs) UnmarshalJSON(b []byte) (err error) {
var obj []interface{}
if err := json.Unmarshal(b, &obj); err != nil {
return shared.NewDecodeParamError(err.Error())
}
if len(obj) < 1 {
return shared.NewInsufficientParamsError(len(obj), 1)
}
argstr, ok := obj[0].(string)
if !ok {
return shared.NewInvalidTypeError("arg0", "is not a string")
}
args.Source = argstr
return nil
}
type FilterStringArgs struct {
Word string
}
func (args *FilterStringArgs) UnmarshalJSON(b []byte) (err error) {
var obj []interface{}
if err := json.Unmarshal(b, &obj); err != nil {
return shared.NewDecodeParamError(err.Error())
}
if len(obj) < 1 {
return shared.NewInsufficientParamsError(len(obj), 1)
}
var argstr string
argstr, ok := obj[0].(string)
if !ok {
return shared.NewInvalidTypeError("filter", "not a string")
}
switch argstr {
case "latest", "pending":
break
default:
return shared.NewValidationError("Word", "Must be `latest` or `pending`")
}
args.Word = argstr
return nil
}
\ No newline at end of file
package rpc
package api
import (
"bytes"
......@@ -6,6 +6,7 @@ import (
"fmt"
"math/big"
"testing"
"github.com/ethereum/go-ethereum/rpc/shared"
)
func TestBlockheightInvalidString(t *testing.T) {
......@@ -68,7 +69,7 @@ func ExpectValidationError(err error) string {
switch err.(type) {
case nil:
str = "Expected error but didn't get one"
case *ValidationError:
case *shared.ValidationError:
break
default:
str = fmt.Sprintf("Expected *rpc.ValidationError but got %T with message `%s`", err, err.Error())
......@@ -81,7 +82,7 @@ func ExpectInvalidTypeError(err error) string {
switch err.(type) {
case nil:
str = "Expected error but didn't get one"
case *InvalidTypeError:
case *shared.InvalidTypeError:
break
default:
str = fmt.Sprintf("Expected *rpc.InvalidTypeError but got %T with message `%s`", err, err.Error())
......@@ -94,7 +95,7 @@ func ExpectInsufficientParamsError(err error) string {
switch err.(type) {
case nil:
str = "Expected error but didn't get one"
case *InsufficientParamsError:
case *shared.InsufficientParamsError:
break
default:
str = fmt.Sprintf("Expected *rpc.InsufficientParamsError but got %T with message %s", err, err.Error())
......@@ -107,7 +108,7 @@ func ExpectDecodeParamError(err error) string {
switch err.(type) {
case nil:
str = "Expected error but didn't get one"
case *DecodeParamError:
case *shared.DecodeParamError:
break
default:
str = fmt.Sprintf("Expected *rpc.DecodeParamError but got %T with message `%s`", err, err.Error())
......
package rpc
import (
"encoding/json"
"strconv"
"testing"
"github.com/ethereum/go-ethereum/common/compiler"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/xeth"
)
func TestWeb3Sha3(t *testing.T) {
jsonstr := `{"jsonrpc":"2.0","method":"web3_sha3","params":["0x68656c6c6f20776f726c64"],"id":64}`
expected := "0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad"
api := &EthereumApi{}
var req RpcRequest
json.Unmarshal([]byte(jsonstr), &req)
var response interface{}
_ = api.GetRequestReply(&req, &response)
if response.(string) != expected {
t.Errorf("Expected %s got %s", expected, response)
}
}
const solcVersion = "0.9.23"
func TestCompileSolidity(t *testing.T) {
solc, err := compiler.New("")
if solc == nil {
t.Skip("no solc found: skip")
} else if solc.Version() != solcVersion {
t.Skip("WARNING: skipping test because of solc different version (%v, test written for %v, may need to update)", solc.Version(), solcVersion)
}
source := `contract test {\n` +
" /// @notice Will multiply `a` by 7." + `\n` +
` function multiply(uint a) returns(uint d) {\n` +
` return a * 7;\n` +
` }\n` +
`}\n`
jsonstr := `{"jsonrpc":"2.0","method":"eth_compileSolidity","params":["` + source + `"],"id":64}`
expCode := "0x605880600c6000396000f3006000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa114602e57005b603d6004803590602001506047565b8060005260206000f35b60006007820290506053565b91905056"
expAbiDefinition := `[{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"d","type":"uint256"}],"type":"function"}]`
expUserDoc := `{"methods":{"multiply(uint256)":{"notice":"Will multiply ` + "`a`" + ` by 7."}}}`
expDeveloperDoc := `{"methods":{}}`
expCompilerVersion := solc.Version()
expLanguage := "Solidity"
expLanguageVersion := "0"
expSource := source
api := NewEthereumApi(xeth.NewTest(&eth.Ethereum{}, nil))
var req RpcRequest
json.Unmarshal([]byte(jsonstr), &req)
var response interface{}
err = api.GetRequestReply(&req, &response)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
respjson, err := json.Marshal(response)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
var contracts = make(map[string]*compiler.Contract)
err = json.Unmarshal(respjson, &contracts)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if len(contracts) != 1 {
t.Errorf("expected one contract, got %v", len(contracts))
}
contract := contracts["test"]
if contract.Code != expCode {
t.Errorf("Expected \n%s got \n%s", expCode, contract.Code)
}
if strconv.Quote(contract.Info.Source) != `"`+expSource+`"` {
t.Errorf("Expected \n'%s' got \n'%s'", expSource, strconv.Quote(contract.Info.Source))
}
if contract.Info.Language != expLanguage {
t.Errorf("Expected %s got %s", expLanguage, contract.Info.Language)
}
if contract.Info.LanguageVersion != expLanguageVersion {
t.Errorf("Expected %s got %s", expLanguageVersion, contract.Info.LanguageVersion)
}
if contract.Info.CompilerVersion != expCompilerVersion {
t.Errorf("Expected %s got %s", expCompilerVersion, contract.Info.CompilerVersion)
}
userdoc, err := json.Marshal(contract.Info.UserDoc)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
devdoc, err := json.Marshal(contract.Info.DeveloperDoc)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
abidef, err := json.Marshal(contract.Info.AbiDefinition)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if string(abidef) != expAbiDefinition {
t.Errorf("Expected \n'%s' got \n'%s'", expAbiDefinition, string(abidef))
}
if string(userdoc) != expUserDoc {
t.Errorf("Expected \n'%s' got \n'%s'", expUserDoc, string(userdoc))
}
if string(devdoc) != expDeveloperDoc {
t.Errorf("Expected %s got %s", expDeveloperDoc, string(devdoc))
}
}
// func TestDbStr(t *testing.T) {
// jsonput := `{"jsonrpc":"2.0","method":"db_putString","params":["testDB","myKey","myString"],"id":64}`
// jsonget := `{"jsonrpc":"2.0","method":"db_getString","params":["testDB","myKey"],"id":64}`
// expected := "myString"
// xeth := &xeth.XEth{}
// api := NewEthereumApi(xeth)
// var response interface{}
// var req RpcRequest
// json.Unmarshal([]byte(jsonput), &req)
// _ = api.GetRequestReply(&req, &response)
// json.Unmarshal([]byte(jsonget), &req)
// _ = api.GetRequestReply(&req, &response)
// if response.(string) != expected {
// t.Errorf("Expected %s got %s", expected, response)
// }
// }
// func TestDbHexStr(t *testing.T) {
// jsonput := `{"jsonrpc":"2.0","method":"db_putHex","params":["testDB","beefKey","0xbeef"],"id":64}`
// jsonget := `{"jsonrpc":"2.0","method":"db_getHex","params":["testDB","beefKey"],"id":64}`
// expected := "0xbeef"
// xeth := &xeth.XEth{}
// api := NewEthereumApi(xeth)
// defer api.db.Close()
// var response interface{}
// var req RpcRequest
// json.Unmarshal([]byte(jsonput), &req)
// _ = api.GetRequestReply(&req, &response)
// json.Unmarshal([]byte(jsonget), &req)
// _ = api.GetRequestReply(&req, &response)
// if response.(string) != expected {
// t.Errorf("Expected %s got %s", expected, response)
// }
// }
// func TestFilterClose(t *testing.T) {
// t.Skip()
// api := &EthereumApi{
// logs: make(map[int]*logFilter),
// messages: make(map[int]*whisperFilter),
// quit: make(chan struct{}),
// }
// filterTickerTime = 1
// api.logs[0] = &logFilter{}
// api.messages[0] = &whisperFilter{}
// var wg sync.WaitGroup
// wg.Add(1)
// go api.start()
// go func() {
// select {
// case <-time.After(500 * time.Millisecond):
// api.stop()
// wg.Done()
// }
// }()
// wg.Wait()
// if len(api.logs) != 0 {
// t.Error("expected logs to be empty")
// }
// if len(api.messages) != 0 {
// t.Error("expected messages to be empty")
// }
// }
This diff is collapsed.
......@@ -9,16 +9,32 @@ import (
"github.com/ethereum/go-ethereum/rpc/api"
"github.com/ethereum/go-ethereum/rpc/codec"
"github.com/ethereum/go-ethereum/rpc/shared"
"strings"
)
const (
jsonrpcver = "2.0"
maxHttpSizeReqLength = 1024 * 1024 // 1MB
)
var (
// List with all API's which are offered over the in proc interface by default
DefaultInProcApis = api.AllApis
// List with all API's which are offered over the IPC interface by default
DefaultIpcApis = api.AllApis
// List with API's which are offered over thr HTTP/RPC interface by default
DefaultHttpRpcApis = strings.Join([]string{
api.DbApiName, api.EthApiName, api.NetApiName, api.Web3ApiName,
}, ",")
)
type EthereumClient interface {
// Close underlaying connection
Close()
// Send request
Send(interface{}) error
// Receive response
Recv() (interface{}, error)
}
......
......@@ -63,3 +63,27 @@ func StopHttp() {
httpListener = nil
}
}
type httpClient struct {
codec codec.ApiCoder
}
// Create a new in process client
func NewHttpClient(cfg HttpConfig, codec codec.Codec) *httpClient {
return &httpClient{
codec: codec.New(nil),
}
}
func (self *httpClient) Close() {
// do nothing
}
func (self *httpClient) Send(req interface{}) error {
return nil
}
func (self *httpClient) Recv() (interface{}, error) {
return nil, nil
}
\ No newline at end of file
......@@ -90,7 +90,7 @@ func newStoppableHandler(h http.Handler, stop chan struct{}) http.Handler {
case <-stop:
w.Header().Set("Content-Type", "application/json")
err := fmt.Errorf("RPC service stopped")
response := shared.NewRpcResponse(-1, jsonrpcver, nil, err)
response := shared.NewRpcResponse(-1, api.JsonRpcVersion, nil, err)
httpSend(w, response)
default:
h.ServeHTTP(w, r)
......@@ -110,14 +110,14 @@ func httpSend(writer io.Writer, v interface{}) (n int, err error) {
return writer.Write(payload)
}
func gethHttpHandler(codec codec.Codec, api api.EthereumApi) http.Handler {
func gethHttpHandler(codec codec.Codec, a api.EthereumApi) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
// Limit request size to resist DoS
if req.ContentLength > maxHttpSizeReqLength {
err := fmt.Errorf("Request too large")
response := shared.NewRpcErrorResponse(-1, jsonrpcver, -32700, err)
response := shared.NewRpcErrorResponse(-1, api.JsonRpcVersion, -32700, err)
httpSend(w, &response)
return
}
......@@ -126,7 +126,7 @@ func gethHttpHandler(codec codec.Codec, api api.EthereumApi) http.Handler {
payload, err := ioutil.ReadAll(req.Body)
if err != nil {
err := fmt.Errorf("Could not read request body")
response := shared.NewRpcErrorResponse(-1, jsonrpcver, -32700, err)
response := shared.NewRpcErrorResponse(-1, api.JsonRpcVersion, -32700, err)
httpSend(w, &response)
return
}
......@@ -134,7 +134,7 @@ func gethHttpHandler(codec codec.Codec, api api.EthereumApi) http.Handler {
c := codec.New(nil)
var rpcReq shared.Request
if err = c.Decode(payload, &rpcReq); err == nil {
reply, err := api.Execute(&rpcReq)
reply, err := a.Execute(&rpcReq)
res := shared.NewRpcResponse(rpcReq.Id, rpcReq.Jsonrpc, reply, err)
httpSend(w, &res)
return
......@@ -146,7 +146,7 @@ func gethHttpHandler(codec codec.Codec, api api.EthereumApi) http.Handler {
resCount := 0
for i, rpcReq := range reqBatch {
reply, err := api.Execute(&rpcReq)
reply, err := a.Execute(&rpcReq)
if rpcReq.Id != nil { // this leaves nil entries in the response batch for later removal
resBatch[i] = shared.NewRpcResponse(rpcReq.Id, rpcReq.Jsonrpc, reply, err)
resCount += 1
......@@ -161,7 +161,7 @@ func gethHttpHandler(codec codec.Codec, api api.EthereumApi) http.Handler {
// invalid request
err = fmt.Errorf("Could not decode request")
res := shared.NewRpcErrorResponse(-1, jsonrpcver, -32600, err)
res := shared.NewRpcErrorResponse(-1, api.JsonRpcVersion, -32600, err)
httpSend(w, res)
})
}
package comms
import (
"github.com/ethereum/go-ethereum/rpc/api"
"github.com/ethereum/go-ethereum/rpc/shared"
"fmt"
"github.com/ethereum/go-ethereum/rpc/codec"
"github.com/ethereum/go-ethereum/xeth"
"github.com/ethereum/go-ethereum/eth"
)
type InProcClient struct {
api api.EthereumApi
codec codec.Codec
lastId interface{}
lastJsonrpc string
lastErr error
lastRes interface{}
}
// Create a new in process client
func NewInProcClient(codec codec.Codec) *InProcClient {
return &InProcClient{
codec: codec,
}
}
func (self *InProcClient) Close() {
// do nothing
}
// Need to setup api support
func (self *InProcClient) Initialize(xeth *xeth.XEth, eth *eth.Ethereum) {
if apis, err := api.ParseApiString(api.AllApis, self.codec, xeth, eth); err == nil {
self.api = api.Merge(apis...)
}
}
func (self *InProcClient) Send(req interface{}) error {
if r, ok := req.(*shared.Request); ok {
self.lastId = r.Id
self.lastJsonrpc = r.Jsonrpc
self.lastRes, self.lastErr = self.api.Execute(r)
return self.lastErr
}
return fmt.Errorf("Invalid request (%T)", req)
}
func (self *InProcClient) Recv() (interface{}, error) {
return self.lastRes, self.lastErr
//return *shared.NewRpcResponse(self.lastId, self.lastJsonrpc, self.lastRes, self.lastErr), nil
}
......@@ -10,19 +10,19 @@ type IpcConfig struct {
}
type ipcClient struct {
c codec.ApiCoder
codec codec.ApiCoder
}
func (self *ipcClient) Close() {
self.c.Close()
self.codec.Close()
}
func (self *ipcClient) Send(req interface{}) error {
return self.c.WriteResponse(req)
return self.codec.WriteResponse(req)
}
func (self *ipcClient) Recv() (interface{}, error) {
return self.c.ReadResponse()
return self.codec.ReadResponse()
}
// Create a new IPC client, UNIX domain socket on posix, named pipe on Windows
......
package rpc
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/xeth"
"github.com/rs/cors"
)
var rpclistener *stoppableTCPListener
const (
jsonrpcver = "2.0"
maxSizeReqLength = 1024 * 1024 // 1MB
)
func Start(pipe *xeth.XEth, config RpcConfig) error {
if rpclistener != nil {
if fmt.Sprintf("%s:%d", config.ListenAddress, config.ListenPort) != rpclistener.Addr().String() {
return fmt.Errorf("RPC service already running on %s ", rpclistener.Addr().String())
}
return nil // RPC service already running on given host/port
}
l, err := newStoppableTCPListener(fmt.Sprintf("%s:%d", config.ListenAddress, config.ListenPort))
if err != nil {
glog.V(logger.Error).Infof("Can't listen on %s:%d: %v", config.ListenAddress, config.ListenPort, err)
return err
}
rpclistener = l
var handler http.Handler
if len(config.CorsDomain) > 0 {
var opts cors.Options
opts.AllowedMethods = []string{"POST"}
opts.AllowedOrigins = strings.Split(config.CorsDomain, " ")
c := cors.New(opts)
handler = newStoppableHandler(c.Handler(JSONRPC(pipe)), l.stop)
} else {
handler = newStoppableHandler(JSONRPC(pipe), l.stop)
}
go http.Serve(l, handler)
return nil
}
func Stop() error {
if rpclistener != nil {
rpclistener.Stop()
rpclistener = nil
}
return nil
}
// JSONRPC returns a handler that implements the Ethereum JSON-RPC API.
func JSONRPC(pipe *xeth.XEth) http.Handler {
api := NewEthereumApi(pipe)
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
// Limit request size to resist DoS
if req.ContentLength > maxSizeReqLength {
jsonerr := &RpcErrorObject{-32700, "Request too large"}
send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr})
return
}
// Read request body
defer req.Body.Close()
body, err := ioutil.ReadAll(req.Body)
if err != nil {
jsonerr := &RpcErrorObject{-32700, "Could not read request body"}
send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr})
}
// Try to parse the request as a single
var reqSingle RpcRequest
if err := json.Unmarshal(body, &reqSingle); err == nil {
response := RpcResponse(api, &reqSingle)
if reqSingle.Id != nil {
send(w, &response)
}
return
}
// Try to parse the request to batch
var reqBatch []RpcRequest
if err := json.Unmarshal(body, &reqBatch); err == nil {
// Build response batch
resBatch := make([]*interface{}, len(reqBatch))
resCount := 0
for i, request := range reqBatch {
response := RpcResponse(api, &request)
// this leaves nil entries in the response batch for later removal
if request.Id != nil {
resBatch[i] = response
resCount = resCount + 1
}
}
// make response omitting nil entries
respBatchComp := make([]*interface{}, resCount)
for _, v := range resBatch {
if v != nil {
respBatchComp[len(respBatchComp)-resCount] = v
resCount = resCount - 1
}
}
send(w, respBatchComp)
return
}
// Not a batch or single request, error
jsonerr := &RpcErrorObject{-32600, "Could not decode request"}
send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr})
})
}
func RpcResponse(api *EthereumApi, request *RpcRequest) *interface{} {
var reply, response interface{}
reserr := api.GetRequestReply(request, &reply)
switch reserr.(type) {
case nil:
response = &RpcSuccessResponse{Jsonrpc: jsonrpcver, Id: request.Id, Result: reply}
case *NotImplementedError, *NotAvailableError:
jsonerr := &RpcErrorObject{-32601, reserr.Error()}
response = &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: request.Id, Error: jsonerr}
case *DecodeParamError, *InsufficientParamsError, *ValidationError, *InvalidTypeError:
jsonerr := &RpcErrorObject{-32602, reserr.Error()}
response = &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: request.Id, Error: jsonerr}
default:
jsonerr := &RpcErrorObject{-32603, reserr.Error()}
response = &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: request.Id, Error: jsonerr}
}
glog.V(logger.Detail).Infof("Generated response: %T %s", response, response)
return &response
}
func send(writer io.Writer, v interface{}) (n int, err error) {
var payload []byte
payload, err = json.MarshalIndent(v, "", "\t")
if err != nil {
glog.V(logger.Error).Infoln("Error marshalling JSON", err)
return 0, err
}
glog.V(logger.Detail).Infof("Sending payload: %s", payload)
return writer.Write(payload)
}
package rpc
import (
"encoding/json"
"fmt"
"reflect"
"github.com/ethereum/go-ethereum/jsre"
"github.com/ethereum/go-ethereum/rpc/codec"
"github.com/ethereum/go-ethereum/rpc/api"
"github.com/ethereum/go-ethereum/rpc/comms"
"github.com/ethereum/go-ethereum/rpc/shared"
"github.com/robertkrimen/otto"
"encoding/json"
"fmt"
)
type Jeth struct {
ethApi *EthereumApi
re *jsre.JSRE
ipcpath string
ethApi api.EthereumApi
re *jsre.JSRE
client comms.EthereumClient
}
func NewJeth(ethApi *EthereumApi, re *jsre.JSRE, ipcpath string) *Jeth {
return &Jeth{ethApi, re, ipcpath}
func NewJeth(ethApi api.EthereumApi, re *jsre.JSRE, client comms.EthereumClient) *Jeth {
return &Jeth{ethApi, re, client}
}
func (self *Jeth) err(call otto.FunctionCall, code int, msg string, id interface{}) (response otto.Value) {
rpcerr := &RpcErrorObject{code, msg}
call.Otto.Set("ret_jsonrpc", jsonrpcver)
rpcerr := &shared.ErrorObject{code, msg}
call.Otto.Set("ret_jsonrpc", api.JsonRpcVersion)
call.Otto.Set("ret_id", id)
call.Otto.Set("ret_error", rpcerr)
response, _ = call.Otto.Run(`
......@@ -34,6 +31,7 @@ func (self *Jeth) err(call otto.FunctionCall, code int, msg string, id interface
return
}
func (self *Jeth) Send(call otto.FunctionCall) (response otto.Value) {
reqif, err := call.Argument(0).Export()
if err != nil {
......@@ -41,11 +39,11 @@ func (self *Jeth) Send(call otto.FunctionCall) (response otto.Value) {
}
jsonreq, err := json.Marshal(reqif)
var reqs []RpcRequest
var reqs []shared.Request
batch := true
err = json.Unmarshal(jsonreq, &reqs)
if err != nil {
reqs = make([]RpcRequest, 1)
reqs = make([]shared.Request, 1)
err = json.Unmarshal(jsonreq, &reqs[0])
batch = false
}
......@@ -55,12 +53,18 @@ func (self *Jeth) Send(call otto.FunctionCall) (response otto.Value) {
for i, req := range reqs {
var respif interface{}
err = self.ethApi.GetRequestReply(&req, &respif)
err := self.client.Send(&req)//self.ethApi.Execute(&req)
if err != nil {
fmt.Println("Error request:", err)
return self.err(call, -32603, err.Error(), req.Id)
}
respif, err = self.client.Recv()
if err != nil {
fmt.Println("Error response:", err)
return self.err(call, -32603, err.Error(), req.Id)
}
call.Otto.Set("ret_jsonrpc", jsonrpcver)
call.Otto.Set("ret_jsonrpc", api.JsonRpcVersion)
call.Otto.Set("ret_id", req.Id)
res, _ := json.Marshal(respif)
......@@ -88,6 +92,7 @@ func (self *Jeth) Send(call otto.FunctionCall) (response otto.Value) {
return
}
/*
func (self *Jeth) SendIpc(call otto.FunctionCall) (response otto.Value) {
reqif, err := call.Argument(0).Export()
if err != nil {
......@@ -102,11 +107,11 @@ func (self *Jeth) SendIpc(call otto.FunctionCall) (response otto.Value) {
defer client.Close()
jsonreq, err := json.Marshal(reqif)
var reqs []RpcRequest
var reqs []shared.Request
batch := true
err = json.Unmarshal(jsonreq, &reqs)
if err != nil {
reqs = make([]RpcRequest, 1)
reqs = make([]shared.Request, 1)
err = json.Unmarshal(jsonreq, &reqs[0])
batch = false
}
......@@ -169,3 +174,4 @@ func (self *Jeth) SendIpc(call otto.FunctionCall) (response otto.Value) {
return
}
*/
This diff is collapsed.
package rpc
import (
"encoding/json"
"fmt"
"math/big"
"regexp"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
)
const (
reHash = `"0x[0-9a-f]{64}"` // 32 bytes
reHashOpt = `"(0x[0-9a-f]{64})"|null` // 32 bytes or null
reAddress = `"0x[0-9a-f]{40}"` // 20 bytes
reAddressOpt = `"0x[0-9a-f]{40}"|null` // 20 bytes or null
reNum = `"0x([1-9a-f][0-9a-f]{0,15})|0"` // must not have left-padded zeros
reNumNonZero = `"0x([1-9a-f][0-9a-f]{0,15})"` // non-zero required must not have left-padded zeros
reNumOpt = `"0x([1-9a-f][0-9a-f]{0,15})|0"|null` // must not have left-padded zeros or null
reData = `"0x[0-9a-f]*"` // can be "empty"
// reListHash = `[("\w":"0x[0-9a-f]{64}",?)*]`
// reListObj = `[("\w":(".+"|null),?)*]`
)
func TestNewBlockRes(t *testing.T) {
tests := map[string]string{
"number": reNum,
"hash": reHash,
"parentHash": reHash,
"nonce": reData,
"sha3Uncles": reHash,
"logsBloom": reData,
"transactionsRoot": reHash,
"stateRoot": reHash,
"miner": reAddress,
"difficulty": `"0x1"`,
"totalDifficulty": reNum,
"size": reNumNonZero,
"extraData": reData,
"gasLimit": reNum,
// "minGasPrice": "0x",
"gasUsed": reNum,
"timestamp": reNum,
// "transactions": reListHash,
// "uncles": reListHash,
}
block := makeBlock()
v := NewBlockRes(block, false)
j, _ := json.Marshal(v)
for k, re := range tests {
match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j))
if !match {
t.Error(fmt.Sprintf("%s output json does not match format %s. Got %s", k, re, j))
}
}
}
func TestNewBlockResTxFull(t *testing.T) {
tests := map[string]string{
"number": reNum,
"hash": reHash,
"parentHash": reHash,
"nonce": reData,
"sha3Uncles": reHash,
"logsBloom": reData,
"transactionsRoot": reHash,
"stateRoot": reHash,
"miner": reAddress,
"difficulty": `"0x1"`,
"totalDifficulty": reNum,
"size": reNumNonZero,
"extraData": reData,
"gasLimit": reNum,
// "minGasPrice": "0x",
"gasUsed": reNum,
"timestamp": reNum,
// "transactions": reListHash,
// "uncles": reListHash,
}
block := makeBlock()
v := NewBlockRes(block, true)
j, _ := json.Marshal(v)
for k, re := range tests {
match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j))
if !match {
t.Error(fmt.Sprintf("%s output json does not match format %s. Got %s", k, re, j))
}
}
}
func TestBlockNil(t *testing.T) {
var block *types.Block
block = nil
u := NewBlockRes(block, false)
j, _ := json.Marshal(u)
if string(j) != "null" {
t.Errorf("Expected null but got %v", string(j))
}
}
func TestNewTransactionRes(t *testing.T) {
to := common.HexToAddress("0x02")
amount := big.NewInt(1)
gasAmount := big.NewInt(1)
gasPrice := big.NewInt(1)
data := []byte{1, 2, 3}
tx := types.NewTransactionMessage(to, amount, gasAmount, gasPrice, data)
tests := map[string]string{
"hash": reHash,
"nonce": reNum,
"blockHash": reHashOpt,
"blockNum": reNumOpt,
"transactionIndex": reNumOpt,
"from": reAddress,
"to": reAddressOpt,
"value": reNum,
"gas": reNum,
"gasPrice": reNum,
"input": reData,
}
v := NewTransactionRes(tx)
v.BlockHash = newHexData(common.HexToHash("0x030201"))
v.BlockNumber = newHexNum(5)
v.TxIndex = newHexNum(0)
j, _ := json.Marshal(v)
for k, re := range tests {
match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j))
if !match {
t.Error(fmt.Sprintf("`%s` output json does not match format %s. Source %s", k, re, j))
}
}
}
func TestTransactionNil(t *testing.T) {
var tx *types.Transaction
tx = nil
u := NewTransactionRes(tx)
j, _ := json.Marshal(u)
if string(j) != "null" {
t.Errorf("Expected null but got %v", string(j))
}
}
func TestNewUncleRes(t *testing.T) {
header := makeHeader()
u := NewUncleRes(header)
tests := map[string]string{
"number": reNum,
"hash": reHash,
"parentHash": reHash,
"nonce": reData,
"sha3Uncles": reHash,
"receiptHash": reHash,
"transactionsRoot": reHash,
"stateRoot": reHash,
"miner": reAddress,
"difficulty": reNum,
"extraData": reData,
"gasLimit": reNum,
"gasUsed": reNum,
"timestamp": reNum,
}
j, _ := json.Marshal(u)
for k, re := range tests {
match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j))
if !match {
t.Error(fmt.Sprintf("`%s` output json does not match format %s. Source %s", k, re, j))
}
}
}
func TestUncleNil(t *testing.T) {
var header *types.Header
header = nil
u := NewUncleRes(header)
j, _ := json.Marshal(u)
if string(j) != "null" {
t.Errorf("Expected null but got %v", string(j))
}
}
func TestNewLogRes(t *testing.T) {
log := makeStateLog(0)
tests := map[string]string{
"address": reAddress,
// "topics": "[.*]"
"data": reData,
"blockNumber": reNum,
// "hash": reHash,
// "logIndex": reNum,
// "blockHash": reHash,
// "transactionHash": reHash,
"transactionIndex": reNum,
}
v := NewLogRes(log)
j, _ := json.Marshal(v)
for k, re := range tests {
match, _ := regexp.MatchString(fmt.Sprintf(`{.*"%s":%s.*}`, k, re), string(j))
if !match {
t.Error(fmt.Sprintf("`%s` output json does not match format %s. Got %s", k, re, j))
}
}
}
func TestNewLogsRes(t *testing.T) {
logs := make([]*state.Log, 3)
logs[0] = makeStateLog(1)
logs[1] = makeStateLog(2)
logs[2] = makeStateLog(3)
tests := map[string]string{}
v := NewLogsRes(logs)
j, _ := json.Marshal(v)
for k, re := range tests {
match, _ := regexp.MatchString(fmt.Sprintf(`[{.*"%s":%s.*}]`, k, re), string(j))
if !match {
t.Error(fmt.Sprintf("%s output json does not match format %s. Got %s", k, re, j))
}
}
}
func makeStateLog(num int) *state.Log {
address := common.HexToAddress("0x0")
data := []byte{1, 2, 3}
number := uint64(num)
topics := make([]common.Hash, 3)
topics = append(topics, common.HexToHash("0x00"))
topics = append(topics, common.HexToHash("0x10"))
topics = append(topics, common.HexToHash("0x20"))
log := state.NewLog(address, topics, data, number)
return log
}
func makeHeader() *types.Header {
header := &types.Header{
ParentHash: common.StringToHash("0x00"),
UncleHash: common.StringToHash("0x00"),
Coinbase: common.StringToAddress("0x00"),
Root: common.StringToHash("0x00"),
TxHash: common.StringToHash("0x00"),
ReceiptHash: common.StringToHash("0x00"),
// Bloom:
Difficulty: big.NewInt(88888888),
Number: big.NewInt(16),
GasLimit: big.NewInt(70000),
GasUsed: big.NewInt(25000),
Time: 124356789,
Extra: nil,
MixDigest: common.StringToHash("0x00"),
Nonce: [8]byte{0, 1, 2, 3, 4, 5, 6, 7},
}
return header
}
func makeBlock() *types.Block {
parentHash := common.HexToHash("0x01")
coinbase := common.HexToAddress("0x01")
root := common.HexToHash("0x01")
difficulty := common.Big1
nonce := uint64(1)
block := types.NewBlock(parentHash, coinbase, root, difficulty, nonce, nil)
txto := common.HexToAddress("0x02")
txamount := big.NewInt(1)
txgasAmount := big.NewInt(1)
txgasPrice := big.NewInt(1)
txdata := []byte{1, 2, 3}
tx := types.NewTransactionMessage(txto, txamount, txgasAmount, txgasPrice, txdata)
txs := make([]*types.Transaction, 1)
txs[0] = tx
block.SetTransactions(txs)
uncles := make([]*types.Header, 1)
uncles[0] = makeHeader()
block.SetUncles(uncles)
return block
}
/*
This file is part of go-ethereum
go-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
go-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
package rpc
import (
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"math/big"
"strings"
"errors"
"net"
"net/http"
"time"
"io"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
type hexdata struct {
data []byte
isNil bool
}
func (d *hexdata) String() string {
return "0x" + common.Bytes2Hex(d.data)
}
func (d *hexdata) MarshalJSON() ([]byte, error) {
if d.isNil {
return json.Marshal(nil)
}
return json.Marshal(d.String())
}
func newHexData(input interface{}) *hexdata {
d := new(hexdata)
if input == nil {
d.isNil = true
return d
}
switch input := input.(type) {
case []byte:
d.data = input
case common.Hash:
d.data = input.Bytes()
case *common.Hash:
if input == nil {
d.isNil = true
} else {
d.data = input.Bytes()
}
case common.Address:
d.data = input.Bytes()
case *common.Address:
if input == nil {
d.isNil = true
} else {
d.data = input.Bytes()
}
case types.Bloom:
d.data = input.Bytes()
case *types.Bloom:
if input == nil {
d.isNil = true
} else {
d.data = input.Bytes()
}
case *big.Int:
if input == nil {
d.isNil = true
} else {
d.data = input.Bytes()
}
case int64:
d.data = big.NewInt(input).Bytes()
case uint64:
buff := make([]byte, 8)
binary.BigEndian.PutUint64(buff, input)
d.data = buff
case int:
d.data = big.NewInt(int64(input)).Bytes()
case uint:
d.data = big.NewInt(int64(input)).Bytes()
case int8:
d.data = big.NewInt(int64(input)).Bytes()
case uint8:
d.data = big.NewInt(int64(input)).Bytes()
case int16:
d.data = big.NewInt(int64(input)).Bytes()
case uint16:
buff := make([]byte, 2)
binary.BigEndian.PutUint16(buff, input)
d.data = buff
case int32:
d.data = big.NewInt(int64(input)).Bytes()
case uint32:
buff := make([]byte, 4)
binary.BigEndian.PutUint32(buff, input)
d.data = buff
case string: // hexstring
// aaargh ffs TODO: avoid back-and-forth hex encodings where unneeded
bytes, err := hex.DecodeString(strings.TrimPrefix(input, "0x"))
if err != nil {
d.isNil = true
} else {
d.data = bytes
}
default:
d.isNil = true
}
return d
}
type hexnum struct {
data []byte
isNil bool
}
func (d *hexnum) String() string {
// Get hex string from bytes
out := common.Bytes2Hex(d.data)
// Trim leading 0s
out = strings.TrimLeft(out, "0")
// Output "0x0" when value is 0
if len(out) == 0 {
out = "0"
}
return "0x" + out
}
func (d *hexnum) MarshalJSON() ([]byte, error) {
if d.isNil {
return json.Marshal(nil)
}
return json.Marshal(d.String())
}
func newHexNum(input interface{}) *hexnum {
d := new(hexnum)
d.data = newHexData(input).data
return d
}
type RpcConfig struct {
ListenAddress string
ListenPort uint
CorsDomain string
}
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 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,
}
}
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 RpcRequest struct {
Id interface{} `json:"id"`
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params json.RawMessage `json:"params"`
}
type RpcSuccessResponse struct {
Id interface{} `json:"id"`
Jsonrpc string `json:"jsonrpc"`
Result interface{} `json:"result"`
}
type RpcErrorResponse struct {
Id interface{} `json:"id"`
Jsonrpc string `json:"jsonrpc"`
Error *RpcErrorObject `json:"error"`
}
type RpcErrorObject struct {
Code int `json:"code"`
Message string `json:"message"`
// Data interface{} `json:"data"`
}
type listenerHasStoppedError struct {
msg string
}
func (self listenerHasStoppedError) Error() string {
return self.msg
}
var listenerStoppedError = listenerHasStoppedError{"Listener stopped"}
// When https://github.com/golang/go/issues/4674 is fixed this could be replaced
type stoppableTCPListener struct {
*net.TCPListener
stop chan struct{} // closed when the listener must stop
}
// Wraps the default handler and checks if the RPC service was stopped. In that case it returns an
// error indicating that the service was stopped. This will only happen for connections which are
// kept open (HTTP keep-alive) when the RPC service was shutdown.
func newStoppableHandler(h http.Handler, stop chan struct{}) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
select {
case <-stop:
w.Header().Set("Content-Type", "application/json")
jsonerr := &RpcErrorObject{-32603, "RPC service stopped"}
send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr})
default:
h.ServeHTTP(w, r)
}
})
}
// Stop the listener and all accepted and still active connections.
func (self *stoppableTCPListener) Stop() {
close(self.stop)
}
func newStoppableTCPListener(addr string) (*stoppableTCPListener, error) {
wl, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
if tcpl, ok := wl.(*net.TCPListener); ok {
stop := make(chan struct{})
l := &stoppableTCPListener{tcpl, stop}
return l, nil
}
return nil, errors.New("Unable to create TCP listener for RPC service")
}
func (self *stoppableTCPListener) Accept() (net.Conn, error) {
for {
self.SetDeadline(time.Now().Add(time.Duration(1 * time.Second)))
c, err := self.TCPListener.AcceptTCP()
select {
case <-self.stop:
if c != nil { // accept timeout
c.Close()
}
self.TCPListener.Close()
return nil, listenerStoppedError
default:
}
if err != nil {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() && netErr.Temporary() {
continue // regular timeout
}
}
return &closableConnection{c, self.stop}, err
}
}
type closableConnection struct {
*net.TCPConn
closed chan struct{}
}
func (self *closableConnection) Read(b []byte) (n int, err error) {
select {
case <-self.closed:
self.TCPConn.Close()
return 0, io.EOF
default:
return self.TCPConn.Read(b)
}
}
This diff is collapsed.
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