aboutsummaryrefslogtreecommitdiffstats
path: root/rpc
diff options
context:
space:
mode:
Diffstat (limited to 'rpc')
-rw-r--r--rpc/api.go37
-rw-r--r--rpc/args.go1
-rw-r--r--rpc/http.go52
-rw-r--r--rpc/http/server.go124
4 files changed, 62 insertions, 152 deletions
diff --git a/rpc/api.go b/rpc/api.go
index 70a8cf9b4..d6854bbab 100644
--- a/rpc/api.go
+++ b/rpc/api.go
@@ -10,6 +10,7 @@ package rpc
import (
"math/big"
+ "path"
"strings"
"sync"
"time"
@@ -53,8 +54,8 @@ type EthereumApi struct {
defaultBlockAge int64
}
-func NewEthereumApi(eth *xeth.XEth) *EthereumApi {
- db, _ := ethdb.NewLDBDatabase("dapps")
+func NewEthereumApi(eth *xeth.XEth, dataDir string) *EthereumApi {
+ db, _ := ethdb.NewLDBDatabase(path.Join(dataDir, "dapps"))
api := &EthereumApi{
eth: eth,
mux: eth.Backend().EventMux(),
@@ -250,44 +251,24 @@ func (p *EthereumApi) GetBlock(args *GetBlockArgs, reply *interface{}) error {
}
func (p *EthereumApi) Transact(args *NewTxArgs, reply *interface{}) error {
- if len(args.Gas) == 0 {
+ // TODO: align default values to have the same type, e.g. not depend on
+ // ethutil.Value conversions later on
+ if ethutil.Big(args.Gas).Cmp(big.NewInt(0)) == 0 {
args.Gas = defaultGas.String()
}
- if len(args.GasPrice) == 0 {
+ if ethutil.Big(args.GasPrice).Cmp(big.NewInt(0)) == 0 {
args.GasPrice = defaultGasPrice.String()
}
- // TODO if no_private_key then
- //if _, exists := p.register[args.From]; exists {
- // p.register[args.From] = append(p.register[args.From], args)
- //} else {
- /*
- account := accounts.Get(fromHex(args.From))
- if account != nil {
- if account.Unlocked() {
- if !unlockAccount(account) {
- return
- }
- }
-
- result, _ := account.Transact(fromHex(args.To), fromHex(args.Value), fromHex(args.Gas), fromHex(args.GasPrice), fromHex(args.Data))
- if len(result) > 0 {
- *reply = toHex(result)
- }
- } else if _, exists := p.register[args.From]; exists {
- p.register[ags.From] = append(p.register[args.From], args)
- }
- */
- result, _ := p.xeth().Transact( /* TODO specify account */ args.To, args.Value, args.Gas, args.GasPrice, args.Data)
+ result, _ := p.xeth().Transact(args.From, args.To, args.Value, args.Gas, args.GasPrice, args.Data)
*reply = result
- //}
return nil
}
func (p *EthereumApi) Call(args *NewTxArgs, reply *interface{}) error {
- result, err := p.xeth().Call( /* TODO specify account */ args.To, args.Value, args.Gas, args.GasPrice, args.Data)
+ result, err := p.xeth().Call(args.From, args.To, args.Value, args.Gas, args.GasPrice, args.Data)
if err != nil {
return err
}
diff --git a/rpc/args.go b/rpc/args.go
index ea8489585..ec3359a4a 100644
--- a/rpc/args.go
+++ b/rpc/args.go
@@ -24,6 +24,7 @@ func (obj *GetBlockArgs) UnmarshalJSON(b []byte) (err error) {
type NewTxArgs struct {
From string `json:"from"`
+ Pass string `json:"pass"`
To string `json:"to"`
Value string `json:"value"`
Gas string `json:"gas"`
diff --git a/rpc/http.go b/rpc/http.go
new file mode 100644
index 000000000..857cf3221
--- /dev/null
+++ b/rpc/http.go
@@ -0,0 +1,52 @@
+package rpc
+
+import (
+ "net/http"
+
+ "github.com/ethereum/go-ethereum/logger"
+ "github.com/ethereum/go-ethereum/xeth"
+)
+
+var rpchttplogger = logger.NewLogger("RPC-HTTP")
+
+const (
+ jsonrpcver = "2.0"
+ maxSizeReqLength = 1024 * 1024 // 1MB
+)
+
+// JSONRPC returns a handler that implements the Ethereum JSON-RPC API.
+func JSONRPC(pipe *xeth.XEth, dataDir string) http.Handler {
+ var json JsonWrapper
+ api := NewEthereumApi(pipe, dataDir)
+
+ return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
+ w.Header().Set("Access-Control-Allow-Origin", "*")
+
+ rpchttplogger.DebugDetailln("Handling request")
+
+ if req.ContentLength > maxSizeReqLength {
+ jsonerr := &RpcErrorObject{-32700, "Error: Request too large"}
+ json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr})
+ return
+ }
+
+ reqParsed, reqerr := json.ParseRequestBody(req)
+ if reqerr != nil {
+ jsonerr := &RpcErrorObject{-32700, "Error: Could not parse request"}
+ json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr})
+ return
+ }
+
+ var response interface{}
+ reserr := api.GetRequestReply(&reqParsed, &response)
+ if reserr != nil {
+ rpchttplogger.Warnln(reserr)
+ jsonerr := &RpcErrorObject{-32603, reserr.Error()}
+ json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Error: jsonerr})
+ return
+ }
+
+ rpchttplogger.DebugDetailf("Generated response: %T %s", response, response)
+ json.Send(w, &RpcSuccessResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Result: response})
+ })
+}
diff --git a/rpc/http/server.go b/rpc/http/server.go
deleted file mode 100644
index d8f0157f1..000000000
--- a/rpc/http/server.go
+++ /dev/null
@@ -1,124 +0,0 @@
-/*
- 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 rpchttp
-
-import (
- "fmt"
- "net"
- "net/http"
-
- "github.com/ethereum/go-ethereum/logger"
- "github.com/ethereum/go-ethereum/rpc"
- "github.com/ethereum/go-ethereum/xeth"
-)
-
-var rpchttplogger = logger.NewLogger("RPC-HTTP")
-var JSON rpc.JsonWrapper
-
-const maxSizeReqLength = 1024 * 1024 // 1MB
-
-func NewRpcHttpServer(pipe *xeth.XEth, address string, port int) (*RpcHttpServer, error) {
- sport := fmt.Sprintf("%s:%d", address, port)
- l, err := net.Listen("tcp", sport)
- if err != nil {
- return nil, err
- }
-
- return &RpcHttpServer{
- listener: l,
- quit: make(chan bool),
- pipe: pipe,
- port: port,
- addr: address,
- }, nil
-}
-
-type RpcHttpServer struct {
- quit chan bool
- listener net.Listener
- pipe *xeth.XEth
- port int
- addr string
-}
-
-func (s *RpcHttpServer) exitHandler() {
-out:
- for {
- select {
- case <-s.quit:
- s.listener.Close()
- break out
- }
- }
-
- rpchttplogger.Infoln("Shutdown RPC-HTTP server")
-}
-
-func (s *RpcHttpServer) Stop() {
- close(s.quit)
-}
-
-func (s *RpcHttpServer) Start() {
- rpchttplogger.Infof("Starting RPC-HTTP server on %s:%d", s.addr, s.port)
- go s.exitHandler()
-
- api := rpc.NewEthereumApi(s.pipe)
- h := s.apiHandler(api)
- http.Handle("/", h)
-
- err := http.Serve(s.listener, nil)
- // FIX Complains on shutdown due to listner already being closed
- if err != nil {
- rpchttplogger.Errorln("Error on RPC-HTTP interface:", err)
- }
-}
-
-func (s *RpcHttpServer) apiHandler(api *rpc.EthereumApi) http.Handler {
- var jsonrpcver string = "2.0"
- fn := func(w http.ResponseWriter, req *http.Request) {
- w.Header().Set("Access-Control-Allow-Origin", "*")
-
- rpchttplogger.DebugDetailln("Handling request")
-
- if req.ContentLength > maxSizeReqLength {
- jsonerr := &rpc.RpcErrorObject{-32700, "Error: Request too large"}
- JSON.Send(w, &rpc.RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr})
- return
- }
-
- reqParsed, reqerr := JSON.ParseRequestBody(req)
- if reqerr != nil {
- jsonerr := &rpc.RpcErrorObject{-32700, "Error: Could not parse request"}
- JSON.Send(w, &rpc.RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr})
- return
- }
-
- var response interface{}
- reserr := api.GetRequestReply(&reqParsed, &response)
- if reserr != nil {
- rpchttplogger.Warnln(reserr)
- jsonerr := &rpc.RpcErrorObject{-32603, reserr.Error()}
- JSON.Send(w, &rpc.RpcErrorResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Error: jsonerr})
- return
- }
-
- rpchttplogger.DebugDetailf("Generated response: %T %s", response, response)
- JSON.Send(w, &rpc.RpcSuccessResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Result: response})
- }
-
- return http.HandlerFunc(fn)
-}