diff options
author | obscuren <geffobscura@gmail.com> | 2015-03-11 08:08:42 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-03-11 08:08:42 +0800 |
commit | 7e0ccc9de53e788ddc1879248bceb33a9ccdbae0 (patch) | |
tree | 816ff2e06aaa78721a22eee7f2c8493fbf313fb4 /rpc | |
parent | ce595b9266d658a5eae626d78aec7f47c04222ca (diff) | |
parent | eba4f389a6c494bc3e15b3bbc6516b51a5e61236 (diff) | |
download | go-tangerine-7e0ccc9de53e788ddc1879248bceb33a9ccdbae0.tar go-tangerine-7e0ccc9de53e788ddc1879248bceb33a9ccdbae0.tar.gz go-tangerine-7e0ccc9de53e788ddc1879248bceb33a9ccdbae0.tar.bz2 go-tangerine-7e0ccc9de53e788ddc1879248bceb33a9ccdbae0.tar.lz go-tangerine-7e0ccc9de53e788ddc1879248bceb33a9ccdbae0.tar.xz go-tangerine-7e0ccc9de53e788ddc1879248bceb33a9ccdbae0.tar.zst go-tangerine-7e0ccc9de53e788ddc1879248bceb33a9ccdbae0.zip |
Merge branch 'develop' into rpcfrontier
Conflicts:
rpc/api.go
rpc/args.go
Diffstat (limited to 'rpc')
-rw-r--r-- | rpc/api.go | 34 | ||||
-rw-r--r-- | rpc/http.go | 52 | ||||
-rw-r--r-- | rpc/http/server.go | 124 |
3 files changed, 71 insertions, 139 deletions
diff --git a/rpc/api.go b/rpc/api.go index 38f02a473..04492f27c 100644 --- a/rpc/api.go +++ b/rpc/api.go @@ -2,7 +2,9 @@ package rpc import ( "encoding/json" + "fmt" "math/big" + "path" "strings" "sync" "time" @@ -46,8 +48,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(), @@ -232,15 +234,7 @@ func (self *EthereumApi) AllLogs(args *FilterOptions, reply *interface{}) error return nil } -func (p *EthereumApi) Transact(args *NewTxArgs, reply *interface{}) error { - if args.Gas == ethutil.Big0 { - args.Gas = defaultGas - } - - if args.GasPrice == ethutil.Big0 { - args.GasPrice = defaultGasPrice - } - +func (p *EthereumApi) Transact(args *NewTxArgs, reply *interface{}) (err error) { // TODO if no_private_key then //if _, exists := p.register[args.From]; exists { // p.register[args.From] = append(p.register[args.From], args) @@ -262,18 +256,28 @@ func (p *EthereumApi) Transact(args *NewTxArgs, reply *interface{}) error { p.register[ags.From] = append(p.register[args.From], args) } */ - result, err := p.xeth().Transact( /* TODO specify account */ args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data) + // TODO: align default values to have the same type, e.g. not depend on + // ethutil.Value conversions later on + fmt.Println("gas", args.Gas) + if args.Gas.Cmp(big.NewInt(0)) == 0 { + args.Gas = defaultGas + } + + if args.GasPrice.Cmp(big.NewInt(0)) == 0 { + args.GasPrice = defaultGasPrice + } + + *reply, err = p.xeth().Transact(args.From, args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data) if err != nil { + fmt.Println("err:", err) return err } - *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.String(), args.Gas.String(), args.GasPrice.String(), args.Data) + result, err := p.xeth().Call(args.From, args.To, args.Value.String(), args.Gas.String(), args.GasPrice.String(), args.Data) if err != nil { return err } 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) -} |