Move json/cbor rpc to lib

This commit is contained in:
Łukasz Magiera 2019-07-08 13:02:02 +02:00
parent f06d874a8a
commit 4ef4721820
8 changed files with 36 additions and 33 deletions

View File

@ -2,12 +2,12 @@ package client
import (
"github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/jsonrpc"
"github.com/filecoin-project/go-lotus/lib"
)
// NewRPC creates a new http jsonrpc client.
func NewRPC(addr string) api.API {
var res api.Struct
jsonrpc.NewClient(addr, "Filecoin", &res.Internal)
lib.NewClient(addr, "Filecoin", &res.Internal)
return &res
}

View File

@ -4,7 +4,7 @@ import (
"bufio"
"context"
"fmt"
"github.com/filecoin-project/go-lotus/cborrpc"
"github.com/filecoin-project/go-lotus/lib"
"github.com/libp2p/go-libp2p-core/protocol"
"math/rand"
"sync"
@ -80,7 +80,7 @@ func (bss *BlockSyncService) HandleStream(s inet.Stream) {
log.Error("handling block sync request")
var req BlockSyncRequest
if err := cborrpc.ReadCborRPC(bufio.NewReader(s), &req); err != nil {
if err := lib.ReadCborRPC(bufio.NewReader(s), &req); err != nil {
log.Errorf("failed to read block sync request: %s", err)
return
}
@ -92,7 +92,7 @@ func (bss *BlockSyncService) HandleStream(s inet.Stream) {
return
}
if err := cborrpc.WriteCborRPC(s, resp); err != nil {
if err := lib.WriteCborRPC(s, resp); err != nil {
log.Error("failed to write back response for handle stream: ", err)
return
}
@ -326,12 +326,12 @@ func (bs *BlockSync) sendRequestToPeer(ctx context.Context, p peer.ID, req *Bloc
return nil, err
}
if err := cborrpc.WriteCborRPC(s, req); err != nil {
if err := lib.WriteCborRPC(s, req); err != nil {
return nil, err
}
var res BlockSyncResponse
if err := cborrpc.ReadCborRPC(bufio.NewReader(s), &res); err != nil {
if err := lib.ReadCborRPC(bufio.NewReader(s), &res); err != nil {
return nil, err
}

View File

@ -1,14 +1,14 @@
package daemon
import (
"github.com/filecoin-project/go-lotus/lib"
"net/http"
"github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/jsonrpc"
)
func serveRPC(api api.API) error {
rpcServer := jsonrpc.NewServer()
rpcServer := lib.NewServer()
rpcServer.Register("Filecoin", api)
http.Handle("/rpc/v0", rpcServer)
return http.ListenAndServe(":1234", http.DefaultServeMux)

View File

@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/filecoin-project/go-lotus/lib"
"net/http"
"reflect"
"sync/atomic"
@ -37,10 +38,10 @@ func (r *result) UnmarshalJSON(raw []byte) error {
}
type clientResponse struct {
Jsonrpc string `json:"jsonrpc"`
Result result `json:"result"`
ID int64 `json:"id"`
Error *respError `json:"error,omitempty"`
Jsonrpc string `json:"jsonrpc"`
Result result `json:"result"`
ID int64 `json:"id"`
Error *lib.respError `json:"error,omitempty"`
}
// ClientCloser is used to close Client from further use
@ -72,7 +73,7 @@ func NewClient(addr string, namespace string, handler interface{}) ClientCloser
panic("handler field not a func")
}
valOut, errOut, nout := processFuncOut(ftyp)
valOut, errOut, nout := lib.processFuncOut(ftyp)
processResponse := func(resp clientResponse, code int) []reflect.Value {
out := make([]reflect.Value, nout)
@ -111,14 +112,14 @@ func NewClient(addr string, namespace string, handler interface{}) ClientCloser
fn := reflect.MakeFunc(ftyp, func(args []reflect.Value) (results []reflect.Value) {
id := atomic.AddInt64(&idCtr, 1)
params := make([]param, len(args)-hasCtx)
params := make([]lib.param, len(args)-hasCtx)
for i, arg := range args[hasCtx:] {
params[i] = param{
params[i] = lib.param{
v: arg,
}
}
req := request{
req := lib.request{
Jsonrpc: "2.0",
ID: &id,
Method: namespace + "." + f.Name,

View File

@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/filecoin-project/go-lotus/lib"
"net/http"
"reflect"
)
@ -175,7 +176,7 @@ func (s *RPCServer) Register(namespace string, r interface{}) {
funcType := method.Func.Type()
hasCtx := 0
if funcType.NumIn() >= 2 && funcType.In(1) == contextType {
if funcType.NumIn() >= 2 && funcType.In(1) == lib.contextType {
hasCtx = 1
}
@ -212,7 +213,7 @@ func processFuncOut(funcType reflect.Type) (valOut int, errOut int, n int) {
switch n {
case 0:
case 1:
if funcType.Out(0) == errorType {
if funcType.Out(0) == lib.errorType {
errOut = 0
} else {
valOut = 0
@ -220,7 +221,7 @@ func processFuncOut(funcType reflect.Type) (valOut int, errOut int, n int) {
case 2:
valOut = 0
errOut = 1
if funcType.Out(1) != errorType {
if funcType.Out(1) != lib.errorType {
panic("expected error as second return value")
}
default:

View File

@ -3,6 +3,7 @@ package jsonrpc
import (
"context"
"errors"
"github.com/filecoin-project/go-lotus/lib"
"net/http/httptest"
"strconv"
"sync"
@ -56,7 +57,7 @@ func TestRPC(t *testing.T) {
serverHandler := &SimpleServerHandler{}
rpcServer := NewServer()
rpcServer := lib.NewServer()
rpcServer.Register("SimpleServerHandler", serverHandler)
// httptest stuff
@ -70,7 +71,7 @@ func TestRPC(t *testing.T) {
AddGet func(int) int
StringMatch func(t TestType, i2 int64) (out TestOut, err error)
}
closer := NewClient(testServ.URL, "SimpleServerHandler", &client)
closer := lib.NewClient(testServ.URL, "SimpleServerHandler", &client)
defer closer()
// Add(int) error
@ -130,7 +131,7 @@ func TestRPC(t *testing.T) {
var noret struct {
Add func(int)
}
closer = NewClient(testServ.URL, "SimpleServerHandler", &noret)
closer = lib.NewClient(testServ.URL, "SimpleServerHandler", &noret)
// this one should actually work
noret.Add(4)
@ -142,7 +143,7 @@ func TestRPC(t *testing.T) {
var noparam struct {
Add func()
}
closer = NewClient(testServ.URL, "SimpleServerHandler", &noparam)
closer = lib.NewClient(testServ.URL, "SimpleServerHandler", &noparam)
// shouldn't panic
noparam.Add()
@ -151,7 +152,7 @@ func TestRPC(t *testing.T) {
var erronly struct {
AddGet func() (int, error)
}
closer = NewClient(testServ.URL, "SimpleServerHandler", &erronly)
closer = lib.NewClient(testServ.URL, "SimpleServerHandler", &erronly)
_, err = erronly.AddGet()
if err == nil || err.Error() != "RPC error (-32602): wrong param count" {
@ -162,7 +163,7 @@ func TestRPC(t *testing.T) {
var wrongtype struct {
Add func(string) error
}
closer = NewClient(testServ.URL, "SimpleServerHandler", &wrongtype)
closer = lib.NewClient(testServ.URL, "SimpleServerHandler", &wrongtype)
err = wrongtype.Add("not an int")
if err == nil || err.Error() != "RPC error (-32700): json: cannot unmarshal string into Go value of type int" {
@ -173,7 +174,7 @@ func TestRPC(t *testing.T) {
var notfound struct {
NotThere func(string) error
}
closer = NewClient(testServ.URL, "SimpleServerHandler", &notfound)
closer = lib.NewClient(testServ.URL, "SimpleServerHandler", &notfound)
err = notfound.NotThere("hello?")
if err == nil || err.Error() != "RPC error (-32601): method 'SimpleServerHandler.NotThere' not found" {
@ -207,7 +208,7 @@ func TestCtx(t *testing.T) {
serverHandler := &CtxHandler{}
rpcServer := NewServer()
rpcServer := lib.NewServer()
rpcServer.Register("CtxHandler", serverHandler)
// httptest stuff
@ -219,7 +220,7 @@ func TestCtx(t *testing.T) {
var client struct {
Test func(ctx context.Context)
}
closer := NewClient(testServ.URL, "CtxHandler", &client)
closer := lib.NewClient(testServ.URL, "CtxHandler", &client)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
@ -239,7 +240,7 @@ func TestCtx(t *testing.T) {
var noCtxClient struct {
Test func()
}
closer = NewClient(testServ.URL, "CtxHandler", &noCtxClient)
closer = lib.NewClient(testServ.URL, "CtxHandler", &noCtxClient)
noCtxClient.Test()

View File

@ -4,8 +4,8 @@ import (
"context"
"fmt"
"github.com/filecoin-project/go-lotus/chain"
"github.com/filecoin-project/go-lotus/lib"
"github.com/filecoin-project/go-lotus/cborrpc"
"github.com/libp2p/go-libp2p-core/host"
"github.com/ipfs/go-cid"
@ -46,7 +46,7 @@ func (hs *Service) HandleStream(s inet.Stream) {
defer s.Close()
var hmsg Message
if err := cborrpc.ReadCborRPC(s, &hmsg); err != nil {
if err := lib.ReadCborRPC(s, &hmsg); err != nil {
log.Infow("failed to read hello message", "error", err)
return
}
@ -91,7 +91,7 @@ func (hs *Service) SayHello(ctx context.Context, pid peer.ID) error {
fmt.Println("SENDING HELLO MESSAGE: ", hts.Cids())
fmt.Println("hello message genesis: ", gen.Cid())
if err := cborrpc.WriteCborRPC(s, hmsg); err != nil {
if err := lib.WriteCborRPC(s, hmsg); err != nil {
return err
}