Skip to content

Commit

Permalink
test: integration tests for premium
Browse files Browse the repository at this point in the history
This commit is to modify and add integration tests
for the addition of premium.
  • Loading branch information
YusukeShimizu committed Dec 24, 2023
1 parent 1ec38e5 commit 4c591cc
Show file tree
Hide file tree
Showing 13 changed files with 1,676 additions and 1,053 deletions.
15 changes: 9 additions & 6 deletions peerswaprpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (p *PeerswapServer) SwapOut(ctx context.Context, request *SwapOutRequest) (
return nil, fmt.Errorf("peer is not connected")
}

swapOut, err := p.swaps.SwapOut(peerId, request.Asset, shortId.String(), pk, request.SwapAmount)
swapOut, err := p.swaps.SwapOut(peerId, request.Asset, shortId.String(), pk, request.SwapAmount, request.GetPremiumLimit())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -268,7 +268,7 @@ func (p *PeerswapServer) SwapIn(ctx context.Context, request *SwapInRequest) (*S
return nil, fmt.Errorf("peer is not connected")
}

swapIn, err := p.swaps.SwapIn(peerId, request.Asset, shortId.String(), pk, request.SwapAmount)
swapIn, err := p.swaps.SwapIn(peerId, request.Asset, shortId.String(), pk, request.SwapAmount, request.GetPremiumLimit())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -376,10 +376,12 @@ func (p *PeerswapServer) ListPeers(ctx context.Context, request *ListPeersReques
}

peerSwapPeers = append(peerSwapPeers, &PeerSwapPeer{
NodeId: v.PubKey,
SwapsAllowed: poll.PeerAllowed,
SupportedAssets: poll.Assets,
Channels: getPeerSwapChannels(v.PubKey, channelRes.Channels),
NodeId: v.PubKey,
SwapsAllowed: poll.PeerAllowed,
SwapInPremiumRatePpm: poll.SwapInPremiumRatePPM,
SwapOutPremiumRatePpm: poll.SwapOutPremiumRatePPM,
SupportedAssets: poll.Assets,
Channels: getPeerSwapChannels(v.PubKey, channelRes.Channels),
AsSender: &SwapStats{
SwapsOut: SenderSwapsOut,
SwapsIn: SenderSwapsIn,
Expand Down Expand Up @@ -571,6 +573,7 @@ func PrettyprintFromServiceSwap(swap *swap.SwapStateMachine) *PrettyPrintSwap {
ClaimTxId: swap.Data.ClaimTxId,
CancelMessage: swap.Data.GetCancelMessage(),
LndChanId: lnd_chan_id,
PremiumAmount: swap.Data.GetPremium(),
}
}

Expand Down
42 changes: 24 additions & 18 deletions policy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@ func Test_Reload(t *testing.T) {
policy, err := create(strings.NewReader(conf))
assert.NoError(t, err)
assert.EqualValues(t, &Policy{
ReserveOnchainMsat: defaultReserveOnchainMsat,
PeerAllowlist: []string{peer1, peer2},
SuspiciousPeerList: defaultSuspiciousPeerList,
AcceptAllPeers: accept,
MinSwapAmountMsat: defaultMinSwapAmountMsat,
AllowNewSwaps: defaultAllowNewSwaps,
ReserveOnchainMsat: defaultReserveOnchainMsat,
PeerAllowlist: []string{peer1, peer2},
SuspiciousPeerList: defaultSuspiciousPeerList,
AcceptAllPeers: accept,
MinSwapAmountMsat: defaultMinSwapAmountMsat,
AllowNewSwaps: defaultAllowNewSwaps,
SwapInPremiumRatePPM: defaultSwapInPremiumRatePPM,
SwapOutPremiumRatePPM: defaultSwapOutPremiumRatePPM,
}, policy)

newPeer := "new_peer"
Expand All @@ -98,12 +100,14 @@ func Test_Reload(t *testing.T) {
err = policy.reload(strings.NewReader(newConf))
assert.NoError(t, err)
assert.EqualValues(t, &Policy{
ReserveOnchainMsat: defaultReserveOnchainMsat,
PeerAllowlist: []string{newPeer},
SuspiciousPeerList: []string{newPeer},
AcceptAllPeers: defaultAcceptAllPeers,
MinSwapAmountMsat: defaultMinSwapAmountMsat,
AllowNewSwaps: defaultAllowNewSwaps,
ReserveOnchainMsat: defaultReserveOnchainMsat,
PeerAllowlist: []string{newPeer},
SuspiciousPeerList: []string{newPeer},
AcceptAllPeers: defaultAcceptAllPeers,
MinSwapAmountMsat: defaultMinSwapAmountMsat,
AllowNewSwaps: defaultAllowNewSwaps,
SwapInPremiumRatePPM: defaultSwapInPremiumRatePPM,
SwapOutPremiumRatePPM: defaultSwapOutPremiumRatePPM,
}, policy)
}

Expand All @@ -121,12 +125,14 @@ func Test_Reload_NoOverrideOnError(t *testing.T) {
policy, err := create(strings.NewReader(conf))
assert.NoError(t, err)
assert.EqualValues(t, &Policy{
ReserveOnchainMsat: defaultReserveOnchainMsat,
PeerAllowlist: []string{peer1, peer2},
SuspiciousPeerList: defaultSuspiciousPeerList,
AcceptAllPeers: accept,
MinSwapAmountMsat: defaultMinSwapAmountMsat,
AllowNewSwaps: defaultAllowNewSwaps,
ReserveOnchainMsat: defaultReserveOnchainMsat,
PeerAllowlist: []string{peer1, peer2},
SuspiciousPeerList: defaultSuspiciousPeerList,
AcceptAllPeers: accept,
MinSwapAmountMsat: defaultMinSwapAmountMsat,
AllowNewSwaps: defaultAllowNewSwaps,
SwapInPremiumRatePPM: defaultSwapInPremiumRatePPM,
SwapOutPremiumRatePPM: defaultSwapOutPremiumRatePPM,
}, policy)

// copy policy
Expand Down
2 changes: 1 addition & 1 deletion swap/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func TestMessageFromUnexpectedPeer(t *testing.T) {
if err != nil {
t.Fatal(err)
}
aliceSwap, err := aliceSwapService.SwapOut(peer, "btc", channelId, initiator, amount, 0)
aliceSwap, err := aliceSwapService.SwapOut(peer, "btc", channelId, initiator, amount, 1000)
if err != nil {
t.Fatalf(" error swapping oput %v: ", err)
}
Expand Down
6 changes: 4 additions & 2 deletions swap/swap_in_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func Test_SwapInReceiverValid(t *testing.T) {
SwapId: swapId,
Network: "mainnet",
ProtocolVersion: PEERSWAP_PROTOCOL_VERSION,
PremiumLimit: 10000,
})
if err != nil {
t.Fatal(err)
Expand All @@ -35,7 +36,7 @@ func Test_SwapInReceiverValid(t *testing.T) {

_, err = swap.SendEvent(Event_OnTxOpenedMessage, &OpeningTxBroadcastedMessage{
SwapId: swap.SwapId,
Payreq: "invoice",
Payreq: "swapin",
TxId: getRandom32ByteHexString(),
ScriptOut: 0,
BlindingKey: "",
Expand Down Expand Up @@ -105,6 +106,7 @@ func Test_SwapInReceiverCancel2(t *testing.T) {
SwapId: swapId,
Network: "mainnet",
ProtocolVersion: PEERSWAP_PROTOCOL_VERSION,
PremiumLimit: 10000,
})
if err != nil {
t.Fatal(err)
Expand All @@ -115,7 +117,7 @@ func Test_SwapInReceiverCancel2(t *testing.T) {

_, err = swap.SendEvent(Event_OnTxOpenedMessage, &OpeningTxBroadcastedMessage{
SwapId: swap.SwapId,
Payreq: "invoice",
Payreq: "swapin",
TxId: getRandom32ByteHexString(),
ScriptOut: 0,
BlindingKey: "",
Expand Down
15 changes: 11 additions & 4 deletions swap/swap_out_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func Test_ValidSwap(t *testing.T) {
Pubkey: takerpubkeyhash,
Network: "mainnet",
ProtocolVersion: PEERSWAP_PROTOCOL_VERSION,
PremiumLimit: 10000,
})
if err != nil {
t.Fatal(err)
Expand All @@ -64,8 +65,9 @@ func Test_ValidSwap(t *testing.T) {
assert.NotEqual(t, "", swapFSM.Data.SwapOutRequest.Pubkey)

_, err = swapFSM.SendEvent(Event_OnFeeInvoiceReceived, &SwapOutAgreementMessage{
Payreq: FeeInvoice,
Pubkey: peer,
Payreq: FeeInvoice,
Pubkey: peer,
Premium: 1000,
})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -166,6 +168,7 @@ func Test_AbortCsvClaim(t *testing.T) {
Pubkey: takerpubkeyhash,
Network: "mainnet",
ProtocolVersion: PEERSWAP_PROTOCOL_VERSION,
PremiumLimit: 10000,
})
if err != nil {
t.Fatal(err)
Expand All @@ -175,8 +178,9 @@ func Test_AbortCsvClaim(t *testing.T) {
assert.NotEqual(t, "", swapFSM.Data.SwapOutRequest.Pubkey)

_, err = swapFSM.SendEvent(Event_OnFeeInvoiceReceived, &SwapOutAgreementMessage{
Payreq: FeeInvoice,
Pubkey: peer,
Payreq: FeeInvoice,
Pubkey: peer,
Premium: 1000,
})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -321,6 +325,9 @@ func (d *dummyLightningClient) DecodePayreq(payreq string) (string, uint64, int6
if payreq == "fee" {
return "foo", 100 * 1000, 10, nil
}
if payreq == "swapin" {
return "foo", 100000 * 1000, 10, nil
}
return "foo", (100000 + 1000) * 1000, 10, nil
}

Expand Down
Loading

0 comments on commit 4c591cc

Please sign in to comment.