Fix jsonrpc client after splitting apis
This commit is contained in:
parent
eda03095b0
commit
f0e807dabb
@ -7,9 +7,26 @@ import (
|
|||||||
"github.com/filecoin-project/go-lotus/lib/jsonrpc"
|
"github.com/filecoin-project/go-lotus/lib/jsonrpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewRPC creates a new http jsonrpc client.
|
// NewFullNodeRPC creates a new http jsonrpc client.
|
||||||
func NewRPC(addr string, requestHeader http.Header) (api.FullNode, error) {
|
func NewFullNodeRPC(addr string, requestHeader http.Header) (api.FullNode, error) {
|
||||||
var res api.FullNodeStruct
|
var res api.FullNodeStruct
|
||||||
_, err := jsonrpc.NewClient(addr, "Filecoin", &res.Internal, requestHeader)
|
_, err := jsonrpc.NewMergeClient(addr, "Filecoin",
|
||||||
|
[]interface{}{
|
||||||
|
&res.CommonStruct.Internal,
|
||||||
|
&res.Internal,
|
||||||
|
}, requestHeader)
|
||||||
|
|
||||||
return &res, err
|
return &res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewStorageMinerRPC creates a new http jsonrpc client for storage miner
|
||||||
|
func NewStorageMinerRPC(addr string, requestHeader http.Header) (api.StorageMiner, error) {
|
||||||
|
var res api.StorageMinerStruct
|
||||||
|
_, err := jsonrpc.NewMergeClient(addr, "Filecoin",
|
||||||
|
[]interface{}{
|
||||||
|
&res.CommonStruct.Internal,
|
||||||
|
&res.Internal,
|
||||||
|
}, requestHeader)
|
||||||
|
|
||||||
|
return &res, err
|
||||||
|
}
|
@ -48,7 +48,7 @@ func GetAPI(ctx *cli.Context) (api.FullNode, error) {
|
|||||||
headers.Add("Authorization", "Bearer "+string(token))
|
headers.Add("Authorization", "Bearer "+string(token))
|
||||||
}
|
}
|
||||||
|
|
||||||
return client.NewRPC("ws://"+addr+"/rpc/v0", headers)
|
return client.NewFullNodeRPC("ws://"+addr+"/rpc/v0", headers)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReqContext returns context for cli execution. Calling it for the first time
|
// ReqContext returns context for cli execution. Calling it for the first time
|
||||||
|
@ -66,7 +66,7 @@ var runCmd = &cli.Command{
|
|||||||
log.Infof("Remote version %s", v)
|
log.Infof("Remote version %s", v)
|
||||||
|
|
||||||
rpcServer := jsonrpc.NewServer()
|
rpcServer := jsonrpc.NewServer()
|
||||||
//rpcServer.Register("Filecoin", minerapi)
|
rpcServer.Register("Filecoin", minerapi)
|
||||||
http.Handle("/rpc/v0", rpcServer)
|
http.Handle("/rpc/v0", rpcServer)
|
||||||
return http.ListenAndServe("127.0.0.1:"+cctx.String("api"), http.DefaultServeMux)
|
return http.ListenAndServe("127.0.0.1:"+cctx.String("api"), http.DefaultServeMux)
|
||||||
},
|
},
|
||||||
|
@ -59,17 +59,12 @@ type ClientCloser func()
|
|||||||
// Returned value closes the client connection
|
// Returned value closes the client connection
|
||||||
// TODO: Example
|
// TODO: Example
|
||||||
func NewClient(addr string, namespace string, handler interface{}, requestHeader http.Header) (ClientCloser, error) {
|
func NewClient(addr string, namespace string, handler interface{}, requestHeader http.Header) (ClientCloser, error) {
|
||||||
htyp := reflect.TypeOf(handler)
|
return NewMergeClient(addr, namespace, []interface{}{handler}, requestHeader)
|
||||||
if htyp.Kind() != reflect.Ptr {
|
}
|
||||||
return nil, xerrors.New("expected handler to be a pointer")
|
|
||||||
}
|
|
||||||
typ := htyp.Elem()
|
|
||||||
if typ.Kind() != reflect.Struct {
|
|
||||||
return nil, xerrors.New("handler should be a struct")
|
|
||||||
}
|
|
||||||
|
|
||||||
val := reflect.ValueOf(handler)
|
|
||||||
|
|
||||||
|
// NewMergeClient is like NewClient, but allows to specify multiple structs
|
||||||
|
// to be filled in the same namespace, using one connection
|
||||||
|
func NewMergeClient(addr string, namespace string, outs []interface{}, requestHeader http.Header) (ClientCloser, error) {
|
||||||
var idCtr int64
|
var idCtr int64
|
||||||
|
|
||||||
conn, _, err := websocket.DefaultDialer.Dial(addr, requestHeader)
|
conn, _, err := websocket.DefaultDialer.Dial(addr, requestHeader)
|
||||||
@ -88,159 +83,172 @@ func NewClient(addr string, namespace string, handler interface{}, requestHeader
|
|||||||
stop: stop,
|
stop: stop,
|
||||||
}).handleWsConn(context.TODO())
|
}).handleWsConn(context.TODO())
|
||||||
|
|
||||||
for i := 0; i < typ.NumField(); i++ {
|
for _, handler := range outs {
|
||||||
f := typ.Field(i)
|
htyp := reflect.TypeOf(handler)
|
||||||
ftyp := f.Type
|
if htyp.Kind() != reflect.Ptr {
|
||||||
if ftyp.Kind() != reflect.Func {
|
return nil, xerrors.New("expected handler to be a pointer")
|
||||||
return nil, xerrors.New("handler field not a func")
|
}
|
||||||
|
typ := htyp.Elem()
|
||||||
|
if typ.Kind() != reflect.Struct {
|
||||||
|
return nil, xerrors.New("handler should be a struct")
|
||||||
}
|
}
|
||||||
|
|
||||||
valOut, errOut, nout := processFuncOut(ftyp)
|
val := reflect.ValueOf(handler)
|
||||||
|
|
||||||
processResponse := func(resp clientResponse, rval reflect.Value) []reflect.Value {
|
for i := 0; i < typ.NumField(); i++ {
|
||||||
out := make([]reflect.Value, nout)
|
f := typ.Field(i)
|
||||||
|
ftyp := f.Type
|
||||||
if valOut != -1 {
|
if ftyp.Kind() != reflect.Func {
|
||||||
out[valOut] = rval
|
return nil, xerrors.New("handler field not a func")
|
||||||
}
|
}
|
||||||
if errOut != -1 {
|
|
||||||
out[errOut] = reflect.New(errorType).Elem()
|
valOut, errOut, nout := processFuncOut(ftyp)
|
||||||
if resp.Error != nil {
|
|
||||||
out[errOut].Set(reflect.ValueOf(resp.Error))
|
processResponse := func(resp clientResponse, rval reflect.Value) []reflect.Value {
|
||||||
|
out := make([]reflect.Value, nout)
|
||||||
|
|
||||||
|
if valOut != -1 {
|
||||||
|
out[valOut] = rval
|
||||||
}
|
}
|
||||||
}
|
if errOut != -1 {
|
||||||
|
out[errOut] = reflect.New(errorType).Elem()
|
||||||
return out
|
if resp.Error != nil {
|
||||||
}
|
out[errOut].Set(reflect.ValueOf(resp.Error))
|
||||||
|
}
|
||||||
processError := func(err error) []reflect.Value {
|
|
||||||
out := make([]reflect.Value, nout)
|
|
||||||
|
|
||||||
if valOut != -1 {
|
|
||||||
out[valOut] = reflect.New(ftyp.Out(valOut)).Elem()
|
|
||||||
}
|
|
||||||
if errOut != -1 {
|
|
||||||
out[errOut] = reflect.New(errorType).Elem()
|
|
||||||
out[errOut].Set(reflect.ValueOf(&ErrClient{err}))
|
|
||||||
}
|
|
||||||
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
hasCtx := 0
|
|
||||||
if ftyp.NumIn() > 0 && ftyp.In(0) == contextType {
|
|
||||||
hasCtx = 1
|
|
||||||
}
|
|
||||||
retCh := valOut != -1 && ftyp.Out(valOut).Kind() == reflect.Chan
|
|
||||||
|
|
||||||
fn := reflect.MakeFunc(ftyp, func(args []reflect.Value) (results []reflect.Value) {
|
|
||||||
id := atomic.AddInt64(&idCtr, 1)
|
|
||||||
params := make([]param, len(args)-hasCtx)
|
|
||||||
for i, arg := range args[hasCtx:] {
|
|
||||||
params[i] = param{
|
|
||||||
v: arg,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
var ctx context.Context
|
processError := func(err error) []reflect.Value {
|
||||||
if hasCtx == 1 {
|
out := make([]reflect.Value, nout)
|
||||||
ctx = args[0].Interface().(context.Context)
|
|
||||||
|
if valOut != -1 {
|
||||||
|
out[valOut] = reflect.New(ftyp.Out(valOut)).Elem()
|
||||||
|
}
|
||||||
|
if errOut != -1 {
|
||||||
|
out[errOut] = reflect.New(errorType).Elem()
|
||||||
|
out[errOut].Set(reflect.ValueOf(&ErrClient{err}))
|
||||||
|
}
|
||||||
|
|
||||||
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
var retVal reflect.Value
|
hasCtx := 0
|
||||||
|
if ftyp.NumIn() > 0 && ftyp.In(0) == contextType {
|
||||||
|
hasCtx = 1
|
||||||
|
}
|
||||||
|
retCh := valOut != -1 && ftyp.Out(valOut).Kind() == reflect.Chan
|
||||||
|
|
||||||
// if the function returns a channel, we need to provide a sink for the
|
fn := reflect.MakeFunc(ftyp, func(args []reflect.Value) (results []reflect.Value) {
|
||||||
// messages
|
id := atomic.AddInt64(&idCtr, 1)
|
||||||
var chCtor func() (context.Context, func([]byte, bool))
|
params := make([]param, len(args)-hasCtx)
|
||||||
|
for i, arg := range args[hasCtx:] {
|
||||||
|
params[i] = param{
|
||||||
|
v: arg,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if retCh {
|
var ctx context.Context
|
||||||
retVal = reflect.Zero(ftyp.Out(valOut))
|
if hasCtx == 1 {
|
||||||
|
ctx = args[0].Interface().(context.Context)
|
||||||
|
}
|
||||||
|
|
||||||
chCtor = func() (context.Context, func([]byte, bool)) {
|
var retVal reflect.Value
|
||||||
// unpack chan type to make sure it's reflect.BothDir
|
|
||||||
ctyp := reflect.ChanOf(reflect.BothDir, ftyp.Out(valOut).Elem())
|
|
||||||
ch := reflect.MakeChan(ctyp, 0) // todo: buffer?
|
|
||||||
retVal = ch.Convert(ftyp.Out(valOut))
|
|
||||||
|
|
||||||
return ctx, func(result []byte, ok bool) {
|
// if the function returns a channel, we need to provide a sink for the
|
||||||
if !ok {
|
// messages
|
||||||
// remote channel closed, close ours too
|
var chCtor func() (context.Context, func([]byte, bool))
|
||||||
ch.Close()
|
|
||||||
return
|
if retCh {
|
||||||
|
retVal = reflect.Zero(ftyp.Out(valOut))
|
||||||
|
|
||||||
|
chCtor = func() (context.Context, func([]byte, bool)) {
|
||||||
|
// unpack chan type to make sure it's reflect.BothDir
|
||||||
|
ctyp := reflect.ChanOf(reflect.BothDir, ftyp.Out(valOut).Elem())
|
||||||
|
ch := reflect.MakeChan(ctyp, 0) // todo: buffer?
|
||||||
|
retVal = ch.Convert(ftyp.Out(valOut))
|
||||||
|
|
||||||
|
return ctx, func(result []byte, ok bool) {
|
||||||
|
if !ok {
|
||||||
|
// remote channel closed, close ours too
|
||||||
|
ch.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val := reflect.New(ftyp.Out(valOut).Elem())
|
||||||
|
if err := json.Unmarshal(result, val.Interface()); err != nil {
|
||||||
|
log.Errorf("error unmarshaling chan response: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ch.Send(val.Elem()) // todo: select on ctx is probably a good idea
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val := reflect.New(ftyp.Out(valOut).Elem())
|
req := request{
|
||||||
if err := json.Unmarshal(result, val.Interface()); err != nil {
|
Jsonrpc: "2.0",
|
||||||
log.Errorf("error unmarshaling chan response: %s", err)
|
ID: &id,
|
||||||
return
|
Method: namespace + "." + f.Name,
|
||||||
|
Params: params,
|
||||||
|
}
|
||||||
|
|
||||||
|
rchan := make(chan clientResponse, 1)
|
||||||
|
requests <- clientRequest{
|
||||||
|
req: req,
|
||||||
|
ready: rchan,
|
||||||
|
|
||||||
|
retCh: chCtor,
|
||||||
|
}
|
||||||
|
var ctxDone <-chan struct{}
|
||||||
|
var resp clientResponse
|
||||||
|
|
||||||
|
if ctx != nil {
|
||||||
|
ctxDone = ctx.Done()
|
||||||
|
}
|
||||||
|
|
||||||
|
// wait for response, handle context cancellation
|
||||||
|
loop:
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case resp = <-rchan:
|
||||||
|
break loop
|
||||||
|
case <-ctxDone: // send cancel request
|
||||||
|
ctxDone = nil
|
||||||
|
|
||||||
|
requests <- clientRequest{
|
||||||
|
req: request{
|
||||||
|
Jsonrpc: "2.0",
|
||||||
|
Method: wsCancel,
|
||||||
|
Params: []param{{v: reflect.ValueOf(id)}},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
ch.Send(val.Elem()) // todo: select on ctx is probably a good idea
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
req := request{
|
|
||||||
Jsonrpc: "2.0",
|
|
||||||
ID: &id,
|
|
||||||
Method: namespace + "." + f.Name,
|
|
||||||
Params: params,
|
|
||||||
}
|
|
||||||
|
|
||||||
rchan := make(chan clientResponse, 1)
|
|
||||||
requests <- clientRequest{
|
|
||||||
req: req,
|
|
||||||
ready: rchan,
|
|
||||||
|
|
||||||
retCh: chCtor,
|
|
||||||
}
|
|
||||||
var ctxDone <-chan struct{}
|
|
||||||
var resp clientResponse
|
|
||||||
|
|
||||||
if ctx != nil {
|
|
||||||
ctxDone = ctx.Done()
|
|
||||||
}
|
|
||||||
|
|
||||||
// wait for response, handle context cancellation
|
|
||||||
loop:
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case resp = <-rchan:
|
|
||||||
break loop
|
|
||||||
case <-ctxDone: // send cancel request
|
|
||||||
ctxDone = nil
|
|
||||||
|
|
||||||
requests <- clientRequest{
|
|
||||||
req: request{
|
|
||||||
Jsonrpc: "2.0",
|
|
||||||
Method: wsCancel,
|
|
||||||
Params: []param{{v: reflect.ValueOf(id)}},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if valOut != -1 && !retCh {
|
|
||||||
retVal = reflect.New(ftyp.Out(valOut))
|
|
||||||
|
|
||||||
if resp.Result != nil {
|
|
||||||
log.Debugw("rpc result", "type", ftyp.Out(valOut))
|
|
||||||
if err := json.Unmarshal(resp.Result, retVal.Interface()); err != nil {
|
|
||||||
return processError(xerrors.Errorf("unmarshaling result: %w", err))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
retVal = retVal.Elem()
|
if valOut != -1 && !retCh {
|
||||||
}
|
retVal = reflect.New(ftyp.Out(valOut))
|
||||||
|
|
||||||
if resp.ID != *req.ID {
|
if resp.Result != nil {
|
||||||
return processError(errors.New("request and response id didn't match"))
|
log.Debugw("rpc result", "type", ftyp.Out(valOut))
|
||||||
}
|
if err := json.Unmarshal(resp.Result, retVal.Interface()); err != nil {
|
||||||
|
return processError(xerrors.Errorf("unmarshaling result: %w", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return processResponse(resp, retVal)
|
retVal = retVal.Elem()
|
||||||
})
|
}
|
||||||
|
|
||||||
val.Elem().Field(i).Set(fn)
|
if resp.ID != *req.ID {
|
||||||
|
return processError(errors.New("request and response id didn't match"))
|
||||||
|
}
|
||||||
|
|
||||||
|
return processResponse(resp, retVal)
|
||||||
|
})
|
||||||
|
|
||||||
|
val.Elem().Field(i).Set(fn)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return func() {
|
return func() {
|
||||||
|
50
node/api.go
50
node/api.go
@ -24,7 +24,7 @@ import (
|
|||||||
|
|
||||||
var log = logging.Logger("node")
|
var log = logging.Logger("node")
|
||||||
|
|
||||||
type API struct {
|
type FullNodeAPI struct {
|
||||||
client.LocalStorage
|
client.LocalStorage
|
||||||
|
|
||||||
Host host.Host
|
Host host.Host
|
||||||
@ -39,7 +39,7 @@ type jwtPayload struct {
|
|||||||
Allow []string
|
Allow []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) AuthVerify(ctx context.Context, token string) ([]string, error) {
|
func (a *FullNodeAPI) AuthVerify(ctx context.Context, token string) ([]string, error) {
|
||||||
var payload jwtPayload
|
var payload jwtPayload
|
||||||
if _, err := jwt.Verify([]byte(token), (*jwt.HMACSHA)(a.APISecret), &payload); err != nil {
|
if _, err := jwt.Verify([]byte(token), (*jwt.HMACSHA)(a.APISecret), &payload); err != nil {
|
||||||
return nil, xerrors.Errorf("JWT Verification failed: %w", err)
|
return nil, xerrors.Errorf("JWT Verification failed: %w", err)
|
||||||
@ -48,7 +48,7 @@ func (a *API) AuthVerify(ctx context.Context, token string) ([]string, error) {
|
|||||||
return payload.Allow, nil
|
return payload.Allow, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) AuthNew(ctx context.Context, perms []string) ([]byte, error) {
|
func (a *FullNodeAPI) AuthNew(ctx context.Context, perms []string) ([]byte, error) {
|
||||||
p := jwtPayload{
|
p := jwtPayload{
|
||||||
Allow: perms, // TODO: consider checking validity
|
Allow: perms, // TODO: consider checking validity
|
||||||
}
|
}
|
||||||
@ -56,7 +56,7 @@ func (a *API) AuthNew(ctx context.Context, perms []string) ([]byte, error) {
|
|||||||
return jwt.Sign(&p, (*jwt.HMACSHA)(a.APISecret))
|
return jwt.Sign(&p, (*jwt.HMACSHA)(a.APISecret))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) ChainSubmitBlock(ctx context.Context, blk *chain.BlockMsg) error {
|
func (a *FullNodeAPI) ChainSubmitBlock(ctx context.Context, blk *chain.BlockMsg) error {
|
||||||
if err := a.Chain.AddBlock(blk.Header); err != nil {
|
if err := a.Chain.AddBlock(blk.Header); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -70,24 +70,24 @@ func (a *API) ChainSubmitBlock(ctx context.Context, blk *chain.BlockMsg) error {
|
|||||||
return a.PubSub.Publish("/fil/blocks", b)
|
return a.PubSub.Publish("/fil/blocks", b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) ChainHead(context.Context) (*chain.TipSet, error) {
|
func (a *FullNodeAPI) ChainHead(context.Context) (*chain.TipSet, error) {
|
||||||
return a.Chain.GetHeaviestTipSet(), nil
|
return a.Chain.GetHeaviestTipSet(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) ChainGetRandomness(ctx context.Context, pts *chain.TipSet) ([]byte, error) {
|
func (a *FullNodeAPI) ChainGetRandomness(ctx context.Context, pts *chain.TipSet) ([]byte, error) {
|
||||||
// TODO: this needs to look back in the chain for the right random beacon value
|
// TODO: this needs to look back in the chain for the right random beacon value
|
||||||
return []byte("foo bar random"), nil
|
return []byte("foo bar random"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) ChainWaitMsg(ctx context.Context, msg cid.Cid) (*api.MsgWait, error) {
|
func (a *FullNodeAPI) ChainWaitMsg(ctx context.Context, msg cid.Cid) (*api.MsgWait, error) {
|
||||||
panic("TODO")
|
panic("TODO")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) ChainGetBlock(ctx context.Context, msg cid.Cid) (*chain.BlockHeader, error) {
|
func (a *FullNodeAPI) ChainGetBlock(ctx context.Context, msg cid.Cid) (*chain.BlockHeader, error) {
|
||||||
return a.Chain.GetBlock(msg)
|
return a.Chain.GetBlock(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) ChainGetBlockMessages(ctx context.Context, msg cid.Cid) ([]*chain.SignedMessage, error) {
|
func (a *FullNodeAPI) ChainGetBlockMessages(ctx context.Context, msg cid.Cid) ([]*chain.SignedMessage, error) {
|
||||||
b, err := a.Chain.GetBlock(msg)
|
b, err := a.Chain.GetBlock(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -96,23 +96,23 @@ func (a *API) ChainGetBlockMessages(ctx context.Context, msg cid.Cid) ([]*chain.
|
|||||||
return a.Chain.MessagesForBlock(b)
|
return a.Chain.MessagesForBlock(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) ID(context.Context) (peer.ID, error) {
|
func (a *FullNodeAPI) ID(context.Context) (peer.ID, error) {
|
||||||
return a.Host.ID(), nil
|
return a.Host.ID(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) Version(context.Context) (api.Version, error) {
|
func (a *FullNodeAPI) Version(context.Context) (api.Version, error) {
|
||||||
return api.Version{
|
return api.Version{
|
||||||
Version: build.Version,
|
Version: build.Version,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) MpoolPending(ctx context.Context, ts *chain.TipSet) ([]*chain.SignedMessage, error) {
|
func (a *FullNodeAPI) MpoolPending(ctx context.Context, ts *chain.TipSet) ([]*chain.SignedMessage, error) {
|
||||||
// TODO: need to make sure we don't return messages that were already included in the referenced chain
|
// TODO: need to make sure we don't return messages that were already included in the referenced chain
|
||||||
// also need to accept ts == nil just fine, assume nil == chain.Head()
|
// also need to accept ts == nil just fine, assume nil == chain.Head()
|
||||||
return a.Mpool.Pending(), nil
|
return a.Mpool.Pending(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) MpoolPush(ctx context.Context, smsg *chain.SignedMessage) error {
|
func (a *FullNodeAPI) MpoolPush(ctx context.Context, smsg *chain.SignedMessage) error {
|
||||||
msgb, err := smsg.Serialize()
|
msgb, err := smsg.Serialize()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -121,11 +121,11 @@ func (a *API) MpoolPush(ctx context.Context, smsg *chain.SignedMessage) error {
|
|||||||
return a.PubSub.Publish("/fil/messages", msgb)
|
return a.PubSub.Publish("/fil/messages", msgb)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) MpoolGetNonce(ctx context.Context, addr address.Address) (uint64, error) {
|
func (a *FullNodeAPI) MpoolGetNonce(ctx context.Context, addr address.Address) (uint64, error) {
|
||||||
return a.Mpool.GetNonce(addr)
|
return a.Mpool.GetNonce(addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) MinerStart(ctx context.Context, addr address.Address) error {
|
func (a *FullNodeAPI) MinerStart(ctx context.Context, addr address.Address) error {
|
||||||
// hrm...
|
// hrm...
|
||||||
m := miner.NewMiner(a, addr)
|
m := miner.NewMiner(a, addr)
|
||||||
|
|
||||||
@ -134,7 +134,7 @@ func (a *API) MinerStart(ctx context.Context, addr address.Address) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) MinerCreateBlock(ctx context.Context, addr address.Address, parents *chain.TipSet, tickets []chain.Ticket, proof chain.ElectionProof, msgs []*chain.SignedMessage) (*chain.BlockMsg, error) {
|
func (a *FullNodeAPI) MinerCreateBlock(ctx context.Context, addr address.Address, parents *chain.TipSet, tickets []chain.Ticket, proof chain.ElectionProof, msgs []*chain.SignedMessage) (*chain.BlockMsg, error) {
|
||||||
fblk, err := chain.MinerCreateBlock(a.Chain, addr, parents, tickets, proof, msgs)
|
fblk, err := chain.MinerCreateBlock(a.Chain, addr, parents, tickets, proof, msgs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -149,7 +149,7 @@ func (a *API) MinerCreateBlock(ctx context.Context, addr address.Address, parent
|
|||||||
return &out, nil
|
return &out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) NetPeers(context.Context) ([]peer.AddrInfo, error) {
|
func (a *FullNodeAPI) NetPeers(context.Context) ([]peer.AddrInfo, error) {
|
||||||
conns := a.Host.Network().Conns()
|
conns := a.Host.Network().Conns()
|
||||||
out := make([]peer.AddrInfo, len(conns))
|
out := make([]peer.AddrInfo, len(conns))
|
||||||
|
|
||||||
@ -165,23 +165,23 @@ func (a *API) NetPeers(context.Context) ([]peer.AddrInfo, error) {
|
|||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) WalletNew(ctx context.Context, typ string) (address.Address, error) {
|
func (a *FullNodeAPI) WalletNew(ctx context.Context, typ string) (address.Address, error) {
|
||||||
return a.Wallet.GenerateKey(typ)
|
return a.Wallet.GenerateKey(typ)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) WalletList(ctx context.Context) ([]address.Address, error) {
|
func (a *FullNodeAPI) WalletList(ctx context.Context) ([]address.Address, error) {
|
||||||
return a.Wallet.ListAddrs()
|
return a.Wallet.ListAddrs()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) WalletBalance(ctx context.Context, addr address.Address) (types.BigInt, error) {
|
func (a *FullNodeAPI) WalletBalance(ctx context.Context, addr address.Address) (types.BigInt, error) {
|
||||||
return a.Chain.GetBalance(addr)
|
return a.Chain.GetBalance(addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) WalletSign(ctx context.Context, k address.Address, msg []byte) (*chain.Signature, error) {
|
func (a *FullNodeAPI) WalletSign(ctx context.Context, k address.Address, msg []byte) (*chain.Signature, error) {
|
||||||
return a.Wallet.Sign(k, msg)
|
return a.Wallet.Sign(k, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) WalletDefaultAddress(ctx context.Context) (address.Address, error) {
|
func (a *FullNodeAPI) WalletDefaultAddress(ctx context.Context) (address.Address, error) {
|
||||||
addrs, err := a.Wallet.ListAddrs()
|
addrs, err := a.Wallet.ListAddrs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return address.Undef, err
|
return address.Undef, err
|
||||||
@ -191,15 +191,15 @@ func (a *API) WalletDefaultAddress(ctx context.Context) (address.Address, error)
|
|||||||
return addrs[0], nil
|
return addrs[0], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) NetConnect(ctx context.Context, p peer.AddrInfo) error {
|
func (a *FullNodeAPI) NetConnect(ctx context.Context, p peer.AddrInfo) error {
|
||||||
return a.Host.Connect(ctx, p)
|
return a.Host.Connect(ctx, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) NetAddrsListen(context.Context) (peer.AddrInfo, error) {
|
func (a *FullNodeAPI) NetAddrsListen(context.Context) (peer.AddrInfo, error) {
|
||||||
return peer.AddrInfo{
|
return peer.AddrInfo{
|
||||||
ID: a.Host.ID(),
|
ID: a.Host.ID(),
|
||||||
Addrs: a.Host.Addrs(),
|
Addrs: a.Host.Addrs(),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ api.FullNode = &API{}
|
var _ api.FullNode = &FullNodeAPI{}
|
||||||
|
@ -270,7 +270,7 @@ func Repo(r repo.Repo) Option {
|
|||||||
|
|
||||||
func FullAPI(out *api.FullNode) Option {
|
func FullAPI(out *api.FullNode) Option {
|
||||||
return func(s *Settings) error {
|
return func(s *Settings) error {
|
||||||
resAPI := &API{}
|
resAPI := &FullNodeAPI{}
|
||||||
s.invokes[ExtractApiKey] = fx.Extract(resAPI)
|
s.invokes[ExtractApiKey] = fx.Extract(resAPI)
|
||||||
*out = resAPI
|
*out = resAPI
|
||||||
return nil
|
return nil
|
||||||
|
@ -58,7 +58,7 @@ func rpcBuilder(t *testing.T, n int) []api.FullNode {
|
|||||||
testServ := httptest.NewServer(rpcServer) // todo: close
|
testServ := httptest.NewServer(rpcServer) // todo: close
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
out[i], err = client.NewRPC("ws://"+testServ.Listener.Addr().String(), nil)
|
out[i], err = client.NewFullNodeRPC("ws://"+testServ.Listener.Addr().String(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user