lsps0: add integration test

This commit is contained in:
Jesse de Wit
2023-08-11 14:20:45 +02:00
parent 26edc7683c
commit 06dfa7c334
6 changed files with 170 additions and 17 deletions

View File

@@ -129,3 +129,35 @@ jobs:
CLIENT_REF: ${{ env.CLIENT_REF }} CLIENT_REF: ${{ env.CLIENT_REF }}
GO_VERSION: ${{ env.GO_VERSION }} GO_VERSION: ${{ env.GO_VERSION }}
CLN_VERSION: ${{ env.CLN_VERSION }} CLN_VERSION: ${{ env.CLN_VERSION }}
run-lsps2-test:
runs-on: ubuntu-22.04
needs:
- setup-itest
- setup-bitcoin-core
- setup-cln
- build-lspd
name: test ${{ matrix.implementation }} ${{ matrix.test }}
strategy:
max-parallel: 6
matrix:
test: [
testLsps0GetProtocolVersions
]
implementation: [
CLN
]
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Run and Process Test State
uses: ./.github/actions/test-lspd
with:
TESTRE: "TestLspd/${{ matrix.implementation }}-lspd:_${{ matrix.test }}"
artifact-name: TestLspd-${{ matrix.implementation }}-lspd_${{ matrix.test }}
bitcoin-version: ${{ env.BITCOIN_VERSION }}
LSP_REF: ${{ env.LSP_REF }}
CLIENT_REF: ${{ env.CLIENT_REF }}
GO_VERSION: ${{ env.GO_VERSION }}
CLN_VERSION: ${{ env.CLN_VERSION }}

View File

@@ -21,6 +21,7 @@ type BreezClient interface {
Stop() error Stop() error
SetHtlcAcceptor(totalMsat uint64) SetHtlcAcceptor(totalMsat uint64)
ResetHtlcAcceptor() ResetHtlcAcceptor()
ReceiveCustomMessage() *lntest.CustomMsgRequest
} }
type generateInvoicesRequest struct { type generateInvoicesRequest struct {

View File

@@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"context" "context"
"encoding/binary"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io" "io"
@@ -54,16 +55,18 @@ python %s
` `
type clnBreezClient struct { type clnBreezClient struct {
name string name string
scriptDir string scriptDir string
pluginFilePath string pluginFilePath string
htlcAcceptorAddress string pluginAddress string
htlcAcceptor func(*proto.HtlcAccepted) *proto.HtlcResolution htlcAcceptor func(*proto.HtlcAccepted) *proto.HtlcResolution
htlcAcceptorCancel context.CancelFunc htlcAcceptorCancel context.CancelFunc
harness *lntest.TestHarness customMsgCancel context.CancelFunc
isInitialized bool customMsgQueue chan *lntest.CustomMsgRequest
node *lntest.ClnNode harness *lntest.TestHarness
mtx sync.Mutex isInitialized bool
node *lntest.ClnNode
mtx sync.Mutex
} }
func newClnBreezClient(h *lntest.TestHarness, m *lntest.Miner, name string) BreezClient { func newClnBreezClient(h *lntest.TestHarness, m *lntest.Miner, name string) BreezClient {
@@ -93,12 +96,12 @@ func newClnBreezClient(h *lntest.TestHarness, m *lntest.Miner, name string) Bree
) )
return &clnBreezClient{ return &clnBreezClient{
name: name, name: name,
harness: h, harness: h,
node: node, node: node,
scriptDir: scriptDir, scriptDir: scriptDir,
pluginFilePath: pluginFilePath, pluginFilePath: pluginFilePath,
htlcAcceptorAddress: htlcAcceptorAddress, pluginAddress: htlcAcceptorAddress,
} }
} }
@@ -125,6 +128,8 @@ func (c *clnBreezClient) Start() {
c.node.Start() c.node.Start()
c.startHtlcAcceptor() c.startHtlcAcceptor()
c.customMsgQueue = make(chan *lntest.CustomMsgRequest, 100)
c.startCustomMsgListener()
} }
func (c *clnBreezClient) ResetHtlcAcceptor() { func (c *clnBreezClient) ResetHtlcAcceptor() {
@@ -206,6 +211,67 @@ func (c *clnBreezClient) SetHtlcAcceptor(totalMsat uint64) {
} }
} }
func (c *clnBreezClient) startCustomMsgListener() {
ctx, cancel := context.WithCancel(c.harness.Ctx)
c.customMsgCancel = cancel
go func() {
for {
if ctx.Err() != nil {
return
}
select {
case <-ctx.Done():
return
case <-time.After(time.Second):
}
conn, err := grpc.DialContext(
ctx,
c.pluginAddress,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: time.Duration(10) * time.Second,
Timeout: time.Duration(10) * time.Second,
}),
)
if err != nil {
log.Printf("%s: Dial htlc acceptor error: %v", c.name, err)
continue
}
client := proto.NewClnPluginClient(conn)
listener, err := client.CustomMsgStream(ctx, &proto.CustomMessageRequest{})
if err != nil {
log.Printf("%s: client.CustomMsgStream() error: %v", c.name, err)
break
}
for {
msg, err := listener.Recv()
if err != nil {
log.Printf("%s: listener.Recv() error: %v", c.name, err)
break
}
payload, err := hex.DecodeString(msg.Payload)
lntest.CheckError(c.harness.T, err)
c.customMsgQueue <- &lntest.CustomMsgRequest{
PeerId: msg.PeerId,
Type: uint32(binary.BigEndian.Uint16(payload)),
Data: payload[2:],
}
}
}
}()
}
func (c *clnBreezClient) ReceiveCustomMessage() *lntest.CustomMsgRequest {
msg := <-c.customMsgQueue
return msg
}
func (c *clnBreezClient) startHtlcAcceptor() { func (c *clnBreezClient) startHtlcAcceptor() {
ctx, cancel := context.WithCancel(c.harness.Ctx) ctx, cancel := context.WithCancel(c.harness.Ctx)
c.htlcAcceptorCancel = cancel c.htlcAcceptorCancel = cancel
@@ -224,7 +290,7 @@ func (c *clnBreezClient) startHtlcAcceptor() {
conn, err := grpc.DialContext( conn, err := grpc.DialContext(
ctx, ctx,
c.htlcAcceptorAddress, c.pluginAddress,
grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithKeepaliveParams(keepalive.ClientParameters{ grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: time.Duration(10) * time.Second, Time: time.Duration(10) * time.Second,

View File

@@ -86,6 +86,11 @@ func (c *lndBreezClient) SetHtlcAcceptor(totalMsat uint64) {
// No need for a htlc acceptor in the LND breez client // No need for a htlc acceptor in the LND breez client
} }
func (c *lndBreezClient) ReceiveCustomMessage() *lntest.CustomMsgRequest {
// TODO: Not implemented.
return nil
}
func (c *lndBreezClient) startChannelAcceptor(ctx context.Context) error { func (c *lndBreezClient) startChannelAcceptor(ctx context.Context) error {
client, err := c.node.LightningClient().ChannelAcceptor(ctx) client, err := c.node.LightningClient().ChannelAcceptor(ctx)
if err != nil { if err != nil {

View File

@@ -174,4 +174,8 @@ var allTestCases = []*testCase{
name: "testOfflineNotificationZeroConfChannel", name: "testOfflineNotificationZeroConfChannel",
test: testOfflineNotificationZeroConfChannel, test: testOfflineNotificationZeroConfChannel,
}, },
{
name: "testLsps0GetProtocolVersions",
test: testLsps0GetProtocolVersions,
},
} }

View File

@@ -0,0 +1,45 @@
package itest
import (
"encoding/hex"
"encoding/json"
"github.com/breez/lntest"
"github.com/breez/lspd/lsps0"
"github.com/stretchr/testify/assert"
)
func testLsps0GetProtocolVersions(p *testParams) {
p.BreezClient().Node().ConnectPeer(p.Lsp().LightningNode())
rawMsg := `{
"method": "lsps0.list_protocols",
"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]interface{})
err := json.Unmarshal(resp.Data[:], &content)
lntest.CheckError(p.t, err)
assert.Equal(p.t, "2.0", content["jsonrpc"])
assert.Equal(p.t, "example#3cad6a54d302edba4c9ade2f7ffac098", content["id"])
content2 := make(map[string]json.RawMessage)
err = json.Unmarshal(resp.Data[:], &content2)
lntest.CheckError(p.t, err)
result := make(map[string][]int)
err = json.Unmarshal(content2["result"], &result)
lntest.CheckError(p.t, err)
assert.Equal(p.t, []int{2}, result["protocols"])
}