Skip to content

Commit

Permalink
lsps2: implement lsps2.get_versions
Browse files Browse the repository at this point in the history
  • Loading branch information
JssDWt committed Aug 14, 2023
1 parent a6f2e33 commit 4254ba3
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .github/workflows/integration_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ jobs:
max-parallel: 6
matrix:
test: [
testLsps0GetProtocolVersions
testLsps0GetProtocolVersions,
testLsps2GetVersions
]
implementation: [
CLN
Expand Down
4 changes: 4 additions & 0 deletions itest/lspd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,8 @@ var allTestCases = []*testCase{
name: "testLsps0GetProtocolVersions",
test: testLsps0GetProtocolVersions,
},
{
name: "testLsps2GetVersions",
test: testLsps2GetVersions,
},
}
40 changes: 40 additions & 0 deletions itest/lsps2_get_versions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package itest

import (
"encoding/hex"
"encoding/json"
"log"

"github.com/breez/lntest"
"github.com/breez/lspd/lsps0"
"github.com/stretchr/testify/assert"
)

func testLsps2GetVersions(p *testParams) {
p.BreezClient().Node().ConnectPeer(p.Lsp().LightningNode())

rawMsg := `{
"method": "lsps2.get_versions",
"jsonrpc": "2.0",
"id": "example#3cad6a54d302edba4c9ade2f7ffac098",
"params": {}
}`
p.BreezClient().Node().SendCustomMessage(&lntest.CustomMsgRequest{
PeerId: hex.EncodeToString(p.Lsp().NodeId()),
Type: lsps0.Lsps0MessageType,
Data: []byte(rawMsg),
})

resp := p.BreezClient().ReceiveCustomMessage()
assert.Equal(p.t, uint32(37913), resp.Type)

content := make(map[string]json.RawMessage)
err := json.Unmarshal(resp.Data[:], &content)
lntest.CheckError(p.t, err)

log.Print(string(resp.Data))
result := make(map[string][]int)
err = json.Unmarshal(content["result"], &result)
lntest.CheckError(p.t, err)
assert.Equal(p.t, []int{1}, result["versions"])
}
54 changes: 54 additions & 0 deletions lsps2/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package lsps2

import (
"context"

"github.com/breez/lspd/lsps0"
)

type GetVersionsRequest struct {
}

type GetVersionsResponse struct {
Versions []int32 `json:"versions"`
}

type Lsps2Server interface {
GetVersions(ctx context.Context, request *GetVersionsRequest) (*GetVersionsResponse, error)
}
type server struct{}

func NewLsps2Server() Lsps2Server {
return &server{}
}

func (s *server) GetVersions(
ctx context.Context,
request *GetVersionsRequest,
) (*GetVersionsResponse, error) {
return &GetVersionsResponse{
Versions: []int32{1},
}, nil
}

func RegisterLsps2Server(s lsps0.ServiceRegistrar, l Lsps2Server) {
s.RegisterService(
&lsps0.ServiceDesc{
ServiceName: "lsps2",
HandlerType: (*Lsps2Server)(nil),
Methods: []lsps0.MethodDesc{
{
MethodName: "lsps2.get_versions",
Handler: func(srv interface{}, ctx context.Context, dec func(interface{}) error) (interface{}, error) {
in := new(GetVersionsRequest)
if err := dec(in); err != nil {
return nil, err
}
return srv.(Lsps2Server).GetVersions(ctx, in)
},
},
},
},
l,
)
}
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/breez/lspd/interceptor"
"github.com/breez/lspd/lnd"
"github.com/breez/lspd/lsps0"
"github.com/breez/lspd/lsps2"
"github.com/breez/lspd/mempool"
"github.com/breez/lspd/notifications"
"github.com/breez/lspd/postgresql"
Expand Down Expand Up @@ -116,7 +117,9 @@ func main() {
go msgClient.Start()
msgServer := lsps0.NewServer()
protocolServer := lsps0.NewProtocolServer([]uint32{2})
lsps2Server := lsps2.NewLsps2Server()
lsps0.RegisterProtocolServer(msgServer, protocolServer)
lsps2.RegisterLsps2Server(msgServer, lsps2Server)
msgClient.WaitStarted()
defer msgClient.Stop()
go msgServer.Serve(msgClient)
Expand Down

0 comments on commit 4254ba3

Please sign in to comment.