Skip to content

Commit

Permalink
Merge pull request #806 from starius/client-last-hop
Browse files Browse the repository at this point in the history
client: fill LastHop in FetchSwaps
  • Loading branch information
starius authored Aug 8, 2024
2 parents 8c97c60 + ed3db3b commit 0c8d12a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ func (s *Client) FetchSwaps(ctx context.Context) ([]*SwapInfo, error) {
SwapStateData: swp.State(),
SwapHash: swp.Hash,
LastUpdate: swp.LastUpdateTime(),
LastHop: swp.Contract.LastHop,
}

htlc, err := utils.GetHtlc(
Expand Down
74 changes: 74 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/lightninglabs/loop/utils"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/routing/route"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -45,6 +46,23 @@ var (
defaultConfirmations = int32(loopdb.DefaultLoopOutHtlcConfirmations)
)

var htlcKeys = func() loopdb.HtlcKeys {
var senderKey, receiverKey [33]byte

// Generate keys.
_, senderPubKey := test.CreateKey(1)
copy(senderKey[:], senderPubKey.SerializeCompressed())
_, receiverPubKey := test.CreateKey(2)
copy(receiverKey[:], receiverPubKey.SerializeCompressed())

return loopdb.HtlcKeys{
SenderScriptKey: senderKey,
ReceiverScriptKey: receiverKey,
SenderInternalPubKey: senderKey,
ReceiverInternalPubKey: receiverKey,
}
}()

// TestLoopOutSuccess tests the loop out happy flow, using a custom htlc
// confirmation target.
func TestLoopOutSuccess(t *testing.T) {
Expand Down Expand Up @@ -437,3 +455,59 @@ func TestWrapGrpcError(t *testing.T) {
})
}
}

// TestFetchSwapsLastHop asserts that FetchSwaps loads LastHop for LoopIn's.
func TestFetchSwapsLastHop(t *testing.T) {
defer test.Guard(t)()

ctx := createClientTestContext(t, nil)

lastHop := route.Vertex{1, 2, 3}

// Create a loop in swap.
swapHash := lntypes.Hash{1, 1, 1}
swap := &loopdb.LoopInContract{
SwapContract: loopdb.SwapContract{
CltvExpiry: 111,
AmountRequested: 111,
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
HtlcKeys: htlcKeys,
},
LastHop: &lastHop,
}
err := ctx.store.CreateLoopIn(context.Background(), swapHash, swap)
require.NoError(t, err, "CreateLoopOut failed")

// Now read all the swaps from the store
swapInfos, err := ctx.swapClient.FetchSwaps(context.Background())
require.NoError(t, err, "FetchSwaps failed")

// Find the loop-in and compare with the expected value.
require.Len(t, swapInfos, 1)
loopInInfo := swapInfos[0]
wantLoopInInfo := &SwapInfo{
SwapHash: swapHash,
SwapContract: loopdb.SwapContract{
CltvExpiry: 111,
AmountRequested: 111,
ProtocolVersion: loopdb.ProtocolVersionMuSig2,
HtlcKeys: htlcKeys,
},

// Make sure LastHop is filled.
LastHop: &lastHop,
}

// Calculate HtlcAddressP2TR.
htlc, err := utils.GetHtlc(
swapHash, &wantLoopInInfo.SwapContract,
&chaincfg.TestNet3Params,
)
require.NoError(t, err)
wantLoopInInfo.HtlcAddressP2TR = htlc.Address

require.Equal(t, wantLoopInInfo, loopInInfo)

// Shutdown the client not to leak goroutines.
ctx.finish()
}
1 change: 1 addition & 0 deletions testcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func newSwapClient(t *testing.T, config *clientConfig) *Client {
sweeper: sweeper,
executor: executor,
resumeReady: make(chan struct{}),
abandonChans: make(map[lntypes.Hash]chan struct{}),
}
}

Expand Down

0 comments on commit 0c8d12a

Please sign in to comment.