deamon: fixing bug and move to jsonrpc 2.0
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
This commit is contained in:
parent
f91663099d
commit
cf5c829b79
3
go.mod
3
go.mod
|
@ -1,6 +1,6 @@
|
||||||
module github.com/vincenzopalazzo/lnprototest-v2
|
module github.com/vincenzopalazzo/lnprototest-v2
|
||||||
|
|
||||||
go 1.23
|
go 1.22
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/akamensky/argparse v1.4.0
|
github.com/akamensky/argparse v1.4.0
|
||||||
|
@ -37,6 +37,7 @@ require (
|
||||||
github.com/lightningnetwork/lnd/tlv v1.0.3 // indirect
|
github.com/lightningnetwork/lnd/tlv v1.0.3 // indirect
|
||||||
github.com/lightningnetwork/lnd/tor v1.0.1 // indirect
|
github.com/lightningnetwork/lnd/tor v1.0.1 // indirect
|
||||||
github.com/miekg/dns v1.1.43 // indirect
|
github.com/miekg/dns v1.1.43 // indirect
|
||||||
|
github.com/sourcegraph/jsonrpc2 v0.2.0 // indirect
|
||||||
github.com/stretchr/testify v1.8.4 // indirect
|
github.com/stretchr/testify v1.8.4 // indirect
|
||||||
golang.org/x/crypto v0.21.0 // indirect
|
golang.org/x/crypto v0.21.0 // indirect
|
||||||
golang.org/x/net v0.21.0 // indirect
|
golang.org/x/net v0.21.0 // indirect
|
||||||
|
|
3
go.sum
3
go.sum
|
@ -111,6 +111,7 @@ github.com/google/btree v1.0.1/go.mod h1:xXMiIv4Fb/0kKde4SpL7qlzvu5cMJDRkFDxJfI9
|
||||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 h1:+9834+KizmvFV7pXQGSXQTsaWhq2GjuNUt0aUU0YBYw=
|
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 h1:+9834+KizmvFV7pXQGSXQTsaWhq2GjuNUt0aUU0YBYw=
|
||||||
|
@ -224,6 +225,8 @@ github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM
|
||||||
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||||
github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js=
|
github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js=
|
||||||
github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE1GqG0=
|
github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE1GqG0=
|
||||||
|
github.com/sourcegraph/jsonrpc2 v0.2.0 h1:KjN/dC4fP6aN9030MZCJs9WQbTOjWHhrtKVpzzSrr/U=
|
||||||
|
github.com/sourcegraph/jsonrpc2 v0.2.0/go.mod h1:ZafdZgk/axhT1cvZAPOhw+95nz2I/Ra5qMlU4gTRwIo=
|
||||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
|
|
@ -55,7 +55,8 @@ func (self *ProtoTestServer) Connect(nodeId string, port uint32, network wire.Bi
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
hostname := fmt.Sprintf("127.0.01:%d", &port)
|
hostname := fmt.Sprintf("127.0.0.1:%d", port)
|
||||||
|
fmt.Printf("\n*******%s******\n", hostname)
|
||||||
addr, err := net.ResolveTCPAddr("tcp", hostname)
|
addr, err := net.ResolveTCPAddr("tcp", hostname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -2,31 +2,43 @@ package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/wire"
|
"github.com/btcsuite/btcd/wire"
|
||||||
|
"github.com/sourcegraph/jsonrpc2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ConnectRPC struct {
|
type ConnectRPC struct {
|
||||||
NodeId string
|
NodeId string
|
||||||
port uint64
|
Port uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_ *ConnectRPC) Call(request *ConnectRPC, response *ConnectRPC) error {
|
func ConnectCall(request *json.RawMessage, response *json.RawMessage) error {
|
||||||
if err := lnprototestServer.Connect(request.NodeId, uint32(request.port), wire.SimNet); err != nil {
|
var connect ConnectRPC
|
||||||
|
fmt.Println(string(*request))
|
||||||
|
if err := json.Unmarshal(*request, &connect); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if err := lnprototestServer.Connect(connect.NodeId, connect.Port, wire.SimNet); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
response.NodeId = request.NodeId
|
*response = *request
|
||||||
response.port = request.port
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type SendRPC struct {
|
type SendRPC struct {
|
||||||
msg string
|
Msg string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_ *SendRPC) Call(request *SendRPC, response *SendRPC) error {
|
func SendCall(request *json.RawMessage, response *json.RawMessage) error {
|
||||||
buff, err := hex.DecodeString(request.msg)
|
var sendCall SendRPC
|
||||||
|
if err := json.Unmarshal(*request, &sendCall); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
buff, err := hex.DecodeString(sendCall.Msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -40,6 +52,49 @@ func (_ *SendRPC) Call(request *SendRPC, response *SendRPC) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
response.msg = hex.EncodeToString(buffResp.Bytes())
|
if buffResp == nil {
|
||||||
return nil
|
return fmt.Errorf("empty answer from the node")
|
||||||
|
}
|
||||||
|
|
||||||
|
sendCall.Msg = hex.EncodeToString(buffResp.Bytes())
|
||||||
|
*response, err = json.Marshal(sendCall)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
type RPCHandler struct{}
|
||||||
|
|
||||||
|
// Handle implements the jsonrpc2.Handler interface.
|
||||||
|
func (h *RPCHandler) Handle(ctx context.Context, c *jsonrpc2.Conn, r *jsonrpc2.Request) {
|
||||||
|
switch r.Method {
|
||||||
|
case "connect":
|
||||||
|
var response json.RawMessage
|
||||||
|
if err := ConnectCall(r.Params, &response); err != nil {
|
||||||
|
if err := c.ReplyWithError(ctx, r.ID, &jsonrpc2.Error{
|
||||||
|
Code: -1,
|
||||||
|
Message: fmt.Sprintf("%s", err),
|
||||||
|
Data: nil,
|
||||||
|
}); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := c.Reply(ctx, r.ID, response); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case "send":
|
||||||
|
var response json.RawMessage
|
||||||
|
if err := SendCall(r.Params, &response); err != nil {
|
||||||
|
if err := c.ReplyWithError(ctx, r.ID, &jsonrpc2.Error{
|
||||||
|
Code: -1,
|
||||||
|
Message: fmt.Sprintf("%s", err),
|
||||||
|
Data: nil,
|
||||||
|
}); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
err := &jsonrpc2.Error{Code: jsonrpc2.CodeMethodNotFound, Message: "Method not found"}
|
||||||
|
if err := c.ReplyWithError(ctx, r.ID, err); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/rpc"
|
|
||||||
"net/rpc/jsonrpc"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/sourcegraph/jsonrpc2"
|
||||||
|
|
||||||
"github.com/vincenzopalazzo/lnprototest-v2/lnprototest"
|
"github.com/vincenzopalazzo/lnprototest-v2/lnprototest"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -15,30 +16,17 @@ var lnprototestServer *lnprototest.ProtoTestServer = nil
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
dataDir string
|
dataDir string
|
||||||
|
context context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func Make(datadir string) (*Server, error) {
|
func Make(datadir string) (*Server, error) {
|
||||||
return &Server{
|
return &Server{
|
||||||
dataDir: datadir,
|
dataDir: datadir,
|
||||||
|
context: context.Background(),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Server) RegisterRPCs() error {
|
|
||||||
if err := rpc.Register(new(ConnectRPC)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := rpc.Register(new(SendRPC)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *Server) Listen() error {
|
func (self *Server) Listen() error {
|
||||||
if err := self.RegisterRPCs(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
unixPath := fmt.Sprintf("%s/lnprototest.sock", self.dataDir)
|
unixPath := fmt.Sprintf("%s/lnprototest.sock", self.dataDir)
|
||||||
if _, err := os.Stat(unixPath); !errors.Is(err, os.ErrNotExist) {
|
if _, err := os.Stat(unixPath); !errors.Is(err, os.ErrNotExist) {
|
||||||
os.Remove(unixPath)
|
os.Remove(unixPath)
|
||||||
|
@ -62,6 +50,7 @@ func (self *Server) Listen() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
go jsonrpc.ServeConn(conn)
|
|
||||||
|
_ = jsonrpc2.NewConn(self.context, jsonrpc2.NewPlainObjectStream(conn), &RPCHandler{})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue