Skip to content

Commit

Permalink
Merge pull request ElementsProject#242 from YusukeShimizu/faster-testing
Browse files Browse the repository at this point in the history
test: the amount of the fee invoice is taken into account when testing
  • Loading branch information
wtogami committed Sep 13, 2023
2 parents ef21c40 + 9c2afaf commit bae0405
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
8 changes: 6 additions & 2 deletions test/testcases.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ func coopClaimTest(t *testing.T, params *testParams) {
//
// Move local balance from taker to maker so that the taker does not
// have enough balance to pay the invoice and cancels the swap coop.
moveAmt := (params.origTakerBalance - params.swapAmt) + 100
feeInvoiceAmt, err := params.makerNode.GetFeeInvoiceAmtSat()
require.NoError(err)

moveAmt := (params.origTakerBalance - feeInvoiceAmt - params.swapAmt) + 1
inv, err := params.makerNode.AddInvoice(moveAmt, "shift balance", "")
require.NoError(err)

Expand All @@ -56,8 +59,9 @@ func coopClaimTest(t *testing.T, params *testParams) {
err = testframework.WaitFor(func() bool {
setTakerFunds, err = params.takerNode.GetChannelBalanceSat(params.scid)
require.NoError(err)
return params.origTakerBalance-moveAmt-10 < setTakerFunds && setTakerFunds < params.origTakerBalance-moveAmt+10
return setTakerFunds == params.swapAmt-1
}, testframework.TIMEOUT)
require.NoError(err)

//
// STEP 3: Confirm opening tx
Expand Down
15 changes: 15 additions & 0 deletions testframework/clightning.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"regexp"
"strings"
"time"

"github.com/elementsproject/glightning/glightning"
Expand Down Expand Up @@ -504,3 +505,17 @@ func (n *CLightningNode) GetMemoFromPayreq(bolt11 string) (string, error) {

return r.Description, nil
}

func (n *CLightningNode) GetFeeInvoiceAmtSat() (sat uint64, err error) {
var feeInvoiceAmt uint64
r, err := n.Rpc.ListInvoices()
if err != nil {
return 0, err
}
for _, i := range r {
if strings.Contains(i.Description, "fee") {
feeInvoiceAmt += i.AmountMilliSatoshi.MSat() / 1000
}
}
return feeInvoiceAmt, nil
}
1 change: 1 addition & 0 deletions testframework/lightning.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type LightningNode interface {
// invoices.
GetLatestInvoice() (payreq string, err error)
GetMemoFromPayreq(payreq string) (memo string, err error)
GetFeeInvoiceAmtSat() (sat uint64, err error)

Run(waitForReady, swaitForBitcoinSynced bool) error
Stop() error
Expand Down
16 changes: 16 additions & 0 deletions testframework/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math"
"os"
"path/filepath"
"strings"

"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnwire"
Expand Down Expand Up @@ -512,3 +513,18 @@ func ScidFromLndChanId(id uint64) string {
lndScid := lnwire.NewShortChanIDFromInt(id)
return fmt.Sprintf("%dx%dx%d", lndScid.BlockHeight, lndScid.TxIndex, lndScid.TxPosition)
}

func (n *LndNode) GetFeeInvoiceAmtSat() (sat uint64, err error) {
var feeInvoiceAmt uint64
r, err := n.Rpc.ListInvoices(context.Background(), &lnrpc.ListInvoiceRequest{})
if err != nil {
return 0, err
}

for _, i := range r.Invoices {
if strings.Contains(i.GetMemo(), "fee") {
feeInvoiceAmt += uint64(i.GetValue())
}
}
return feeInvoiceAmt, nil
}

0 comments on commit bae0405

Please sign in to comment.