itest+lntemp: move calculation functions into lntemp

Functions that can be useful to other tests are now moved into the
package `lntemp`.
This commit is contained in:
yyforyongyu committed 2023-02-23 21:56:09 +08:00
1 parent 4a9c3449a6
commit ee0790493c
12 files changed
+95 -96

No files matched your search

+10 -5
View File
@@ -58,10 +58,10 @@ func testChannelBalance(ht *lntemp.HarnessTest) {
// As this is a single funder channel, Alice's balance should be // As this is a single funder channel, Alice's balance should be
// exactly 0.5 BTC since now state transitions have taken place yet. // exactly 0.5 BTC since now state transitions have taken place yet.
checkChannelBalance(alice, amount-calcStaticFee(cType, 0), 0) checkChannelBalance(alice, amount-lntemp.CalcStaticFee(cType, 0), 0)
// Ensure Bob currently has no available balance within the channel. // Ensure Bob currently has no available balance within the channel.
checkChannelBalance(bob, 0, amount-calcStaticFee(cType, 0)) checkChannelBalance(bob, 0, amount-lntemp.CalcStaticFee(cType, 0))
// Finally close the channel between Alice and Bob, asserting that the // Finally close the channel between Alice and Bob, asserting that the
// channel has been properly closed on-chain. // channel has been properly closed on-chain.
@@ -129,11 +129,15 @@ func testChannelUnsettledBalance(ht *lntemp.HarnessTest) {
// Check alice's channel balance, which should have zero remote and zero // Check alice's channel balance, which should have zero remote and zero
// pending balance. // pending balance.
checkChannelBalance(alice, chanAmt-calcStaticFee(cType, 0), 0, 0, 0) checkChannelBalance(
alice, chanAmt-lntemp.CalcStaticFee(cType, 0), 0, 0, 0,
)
// Check carol's channel balance, which should have zero local and zero // Check carol's channel balance, which should have zero local and zero
// pending balance. // pending balance.
checkChannelBalance(carol, 0, chanAmt-calcStaticFee(cType, 0), 0, 0) checkChannelBalance(
carol, 0, chanAmt-lntemp.CalcStaticFee(cType, 0), 0, 0,
)
// Channel should be ready for payments. // Channel should be ready for payments.
const ( const (
@@ -195,7 +199,8 @@ func testChannelUnsettledBalance(ht *lntemp.HarnessTest) {
// Check alice's channel balance, which should have a remote unsettled // Check alice's channel balance, which should have a remote unsettled
// balance that equals to the amount of invoices * payAmt. The remote // balance that equals to the amount of invoices * payAmt. The remote
// balance remains zero. // balance remains zero.
aliceLocal := chanAmt - calcStaticFee(cType, 0) - numInvoices*payAmt fee := lntemp.CalcStaticFee(cType, 0)
aliceLocal := chanAmt - fee - numInvoices*payAmt
checkChannelBalance(alice, aliceLocal, 0, 0, numInvoices*payAmt) checkChannelBalance(alice, aliceLocal, 0, 0, numInvoices*payAmt)
// Check carol's channel balance, which should have a local unsettled // Check carol's channel balance, which should have a local unsettled
+1 -1
View File
@@ -141,7 +141,7 @@ func testUpdateChanStatus(ht *lntemp.HarnessTest) {
FeeRateMilliMsat: int64(chainreg.DefaultBitcoinFeeRate), FeeRateMilliMsat: int64(chainreg.DefaultBitcoinFeeRate),
TimeLockDelta: chainreg.DefaultBitcoinTimeLockDelta, TimeLockDelta: chainreg.DefaultBitcoinTimeLockDelta,
MinHtlc: 1000, // default value MinHtlc: 1000, // default value
MaxHtlcMsat: calculateMaxHtlc(chanAmt), MaxHtlcMsat: lntemp.CalculateMaxHtlc(chanAmt),
} }
// Manually disable the channel and ensure that a "Disabled = true" // Manually disable the channel and ensure that a "Disabled = true"
+2 -2
View File
@@ -25,7 +25,7 @@ func testUpdateChannelPolicy(ht *lntemp.HarnessTest) {
defaultTimeLockDelta = chainreg.DefaultBitcoinTimeLockDelta defaultTimeLockDelta = chainreg.DefaultBitcoinTimeLockDelta
defaultMinHtlc = 1000 defaultMinHtlc = 1000
) )
defaultMaxHtlc := calculateMaxHtlc(funding.MaxBtcFundingAmount) defaultMaxHtlc := lntemp.CalculateMaxHtlc(funding.MaxBtcFundingAmount)
chanAmt := funding.MaxBtcFundingAmount chanAmt := funding.MaxBtcFundingAmount
pushAmt := chanAmt / 2 pushAmt := chanAmt / 2
@@ -513,7 +513,7 @@ func testSendUpdateDisableChannel(ht *lntemp.HarnessTest) {
FeeRateMilliMsat: int64(chainreg.DefaultBitcoinFeeRate), FeeRateMilliMsat: int64(chainreg.DefaultBitcoinFeeRate),
TimeLockDelta: chainreg.DefaultBitcoinTimeLockDelta, TimeLockDelta: chainreg.DefaultBitcoinTimeLockDelta,
MinHtlc: 1000, // default value MinHtlc: 1000, // default value
MaxHtlcMsat: calculateMaxHtlc(chanAmt), MaxHtlcMsat: lntemp.CalculateMaxHtlc(chanAmt),
Disabled: true, Disabled: true,
} }
+3 -3
View File
@@ -193,7 +193,7 @@ func basicChannelFundingTest(ht *lntemp.HarnessTest,
// With the channel open, ensure that the amount specified above has // With the channel open, ensure that the amount specified above has
// properly been pushed to Bob. // properly been pushed to Bob.
aliceLocalBalance := chanAmt - pushAmt - calcStaticFee(cType, 0) aliceLocalBalance := chanAmt - pushAmt - lntemp.CalcStaticFee(cType, 0)
checkChannelBalance( checkChannelBalance(
alice, aliceChannelBalance, aliceLocalBalance, pushAmt, alice, aliceChannelBalance, aliceLocalBalance, pushAmt,
) )
@@ -289,7 +289,7 @@ func testUnconfirmedChannelFunding(ht *lntemp.HarnessTest) {
// Note that atm we haven't obtained the chanPoint yet, so we use the // Note that atm we haven't obtained the chanPoint yet, so we use the
// type directly. // type directly.
cType := lnrpc.CommitmentType_STATIC_REMOTE_KEY cType := lnrpc.CommitmentType_STATIC_REMOTE_KEY
carolLocalBalance := chanAmt - pushAmt - calcStaticFee(cType, 0) carolLocalBalance := chanAmt - pushAmt - lntemp.CalcStaticFee(cType, 0)
checkChannelBalance(carol, 0, 0, carolLocalBalance, pushAmt) checkChannelBalance(carol, 0, 0, carolLocalBalance, pushAmt)
// For Alice, her local/remote balances should be zero, and the // For Alice, her local/remote balances should be zero, and the
@@ -423,7 +423,7 @@ func runChannelFundingInputTypes(ht *lntemp.HarnessTest, alice,
// Note that atm we haven't obtained the chanPoint yet, so we // Note that atm we haven't obtained the chanPoint yet, so we
// use the type directly. // use the type directly.
cType := lnrpc.CommitmentType_STATIC_REMOTE_KEY cType := lnrpc.CommitmentType_STATIC_REMOTE_KEY
carolLocalBalance := chanAmt - calcStaticFee(cType, 0) carolLocalBalance := chanAmt - lntemp.CalcStaticFee(cType, 0)
checkChannelBalance(carol, 0, 0, carolLocalBalance, 0) checkChannelBalance(carol, 0, 0, carolLocalBalance, 0)
// For Alice, her local/remote balances should be zero, and the // For Alice, her local/remote balances should be zero, and the
@@ -55,7 +55,7 @@ func testHtlcErrorPropagation(ht *lntemp.HarnessTest) {
ht.AssertTopologyChannelOpen(alice, chanPointBob) ht.AssertTopologyChannelOpen(alice, chanPointBob)
cType := ht.GetChannelCommitType(alice, chanPointAlice) cType := ht.GetChannelCommitType(alice, chanPointAlice)
commitFee := calcStaticFee(cType, 0) commitFee := lntemp.CalcStaticFee(cType, 0)
assertBaseBalance := func() { assertBaseBalance := func() {
// Alice has opened a channel with Bob with zero push amount, // Alice has opened a channel with Bob with zero push amount,
+1 -1
View File
@@ -69,7 +69,7 @@ func testMultiHopPayments(ht *lntemp.HarnessTest) {
// Set the fee policies of the Alice -> Bob and the Dave -> Alice // Set the fee policies of the Alice -> Bob and the Dave -> Alice
// channel edges to relatively large non default values. This makes it // channel edges to relatively large non default values. This makes it
// possible to pick up more subtle fee calculation errors. // possible to pick up more subtle fee calculation errors.
maxHtlc := calculateMaxHtlc(chanAmt) maxHtlc := lntemp.CalculateMaxHtlc(chanAmt)
const aliceBaseFeeSat = 1 const aliceBaseFeeSat = 1
const aliceFeeRatePPM = 100000 const aliceFeeRatePPM = 100000
updateChannelPolicy( updateChannelPolicy(
+1 -1
View File
@@ -182,7 +182,7 @@ func testOpenChannelUpdateFeePolicy(ht *lntemp.HarnessTest) {
optionalFeeRate = 1337 optionalFeeRate = 1337
) )
defaultMaxHtlc := calculateMaxHtlc(funding.MaxBtcFundingAmount) defaultMaxHtlc := lntemp.CalculateMaxHtlc(funding.MaxBtcFundingAmount)
chanAmt := funding.MaxBtcFundingAmount chanAmt := funding.MaxBtcFundingAmount
pushAmt := chanAmt / 2 pushAmt := chanAmt / 2
+1 -1
View File
@@ -1203,7 +1203,7 @@ func testRouteFeeCutoff(ht *lntemp.HarnessTest) {
baseFee := int64(10000) baseFee := int64(10000)
feeRate := int64(5) feeRate := int64(5)
timeLockDelta := uint32(chainreg.DefaultBitcoinTimeLockDelta) timeLockDelta := uint32(chainreg.DefaultBitcoinTimeLockDelta)
maxHtlc := calculateMaxHtlc(chanAmt) maxHtlc := lntemp.CalculateMaxHtlc(chanAmt)
expectedPolicy := &lnrpc.RoutingPolicy{ expectedPolicy := &lnrpc.RoutingPolicy{
FeeBaseMsat: baseFee, FeeBaseMsat: baseFee,
+25
View File
@@ -4,6 +4,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"math"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
@@ -11,9 +12,12 @@ import (
"testing" "testing"
"time" "time"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/integration/rpctest" "github.com/btcsuite/btcd/integration/rpctest"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lntemp" "github.com/lightningnetwork/lnd/lntemp"
"github.com/lightningnetwork/lnd/lntemp/node" "github.com/lightningnetwork/lnd/lntemp/node"
"github.com/lightningnetwork/lnd/lntest/wait"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"google.golang.org/grpc/grpclog" "google.golang.org/grpc/grpclog"
) )
@@ -26,9 +30,25 @@ const (
// defaultRunTranche is the default index of the test cases tranche that // defaultRunTranche is the default index of the test cases tranche that
// we run. // we run.
defaultRunTranche uint = 0 defaultRunTranche uint = 0
defaultTimeout = wait.DefaultTimeout
itestLndBinary = "../lnd-itest"
// TODO(yy): remove the following defined constants and put them in the
// specific tests where they are used?
testFeeBase = 1e+6
anchorSize = 330
defaultCSV = node.DefaultCSV
noFeeLimitMsat = math.MaxInt64
AddrTypeWitnessPubkeyHash = lnrpc.AddressType_WITNESS_PUBKEY_HASH
AddrTypeNestedPubkeyHash = lnrpc.AddressType_NESTED_PUBKEY_HASH
AddrTypeTaprootPubkey = lnrpc.AddressType_TAPROOT_PUBKEY
) )
var ( var (
harnessNetParams = &chaincfg.RegressionNetParams
// testCasesSplitParts is the number of tranches the test cases should // testCasesSplitParts is the number of tranches the test cases should
// be split into. By default this is set to 1, so no splitting happens. // be split into. By default this is set to 1, so no splitting happens.
// If this value is increased, then the -runtranche flag must be // If this value is increased, then the -runtranche flag must be
@@ -50,6 +70,11 @@ var (
// dbBackendFlag specifies the backend to use. // dbBackendFlag specifies the backend to use.
dbBackendFlag = flag.String("dbbackend", "bbolt", "Database backend "+ dbBackendFlag = flag.String("dbbackend", "bbolt", "Database backend "+
"(bbolt, etcd, postgres)") "(bbolt, etcd, postgres)")
// lndExecutable is the full path to the lnd binary.
lndExecutable = flag.String(
"lndexec", itestLndBinary, "full path to lnd binary",
)
) )
// TestLightningNetworkDaemon performs a series of integration tests amongst a // TestLightningNetworkDaemon performs a series of integration tests amongst a
+4 -4
View File
@@ -541,7 +541,7 @@ func testPrivateUpdateAlias(ht *lntemp.HarnessTest,
FeeRateMilliMsat: testFeeBase * feeRate, FeeRateMilliMsat: testFeeBase * feeRate,
TimeLockDelta: timeLockDelta, TimeLockDelta: timeLockDelta,
MinHtlc: 1000, // default value MinHtlc: 1000, // default value
MaxHtlcMsat: calculateMaxHtlc(chanAmt), MaxHtlcMsat: lntemp.CalculateMaxHtlc(chanAmt),
} }
// Assert that Dave receives Carol's policy update. // Assert that Dave receives Carol's policy update.
@@ -567,7 +567,7 @@ func testPrivateUpdateAlias(ht *lntemp.HarnessTest,
FeeRateMilliMsat: testFeeBase * feeRate, FeeRateMilliMsat: testFeeBase * feeRate,
TimeLockDelta: timeLockDelta, TimeLockDelta: timeLockDelta,
MinHtlc: 1000, MinHtlc: 1000,
MaxHtlcMsat: calculateMaxHtlc(chanAmt), MaxHtlcMsat: lntemp.CalculateMaxHtlc(chanAmt),
} }
// Assert that Carol receives Dave's policy update. // Assert that Carol receives Dave's policy update.
@@ -654,7 +654,7 @@ func testPrivateUpdateAlias(ht *lntemp.HarnessTest,
FeeRateMilliMsat: testFeeBase * feeRate, FeeRateMilliMsat: testFeeBase * feeRate,
TimeLockDelta: timeLockDelta, TimeLockDelta: timeLockDelta,
MinHtlc: 1000, MinHtlc: 1000,
MaxHtlcMsat: calculateMaxHtlc(chanAmt), MaxHtlcMsat: lntemp.CalculateMaxHtlc(chanAmt),
} }
// Assert Dave receives Carol's policy update. // Assert Dave receives Carol's policy update.
@@ -743,7 +743,7 @@ func testPrivateUpdateAlias(ht *lntemp.HarnessTest,
FeeRateMilliMsat: testFeeBase * feeRate, FeeRateMilliMsat: testFeeBase * feeRate,
TimeLockDelta: timeLockDelta, TimeLockDelta: timeLockDelta,
MinHtlc: 1000, MinHtlc: 1000,
MaxHtlcMsat: calculateMaxHtlc(chanAmt), MaxHtlcMsat: lntemp.CalculateMaxHtlc(chanAmt),
} }
// Assert Dave and optionally Eve receives Carol's update. // Assert Dave and optionally Eve receives Carol's update.
-77
View File
@@ -1,77 +0,0 @@
package itest
import (
"flag"
"math"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lntemp"
"github.com/lightningnetwork/lnd/lntemp/node"
"github.com/lightningnetwork/lnd/lntest/wait"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/lightningnetwork/lnd/lnwire"
)
const (
testFeeBase = 1e+6
defaultCSV = node.DefaultCSV
defaultTimeout = wait.DefaultTimeout
itestLndBinary = "../../lnd-itest"
anchorSize = 330
noFeeLimitMsat = math.MaxInt64
AddrTypeWitnessPubkeyHash = lnrpc.AddressType_WITNESS_PUBKEY_HASH
AddrTypeNestedPubkeyHash = lnrpc.AddressType_NESTED_PUBKEY_HASH
AddrTypeTaprootPubkey = lnrpc.AddressType_TAPROOT_PUBKEY
)
var (
harnessNetParams = &chaincfg.RegressionNetParams
// lndExecutable is the full path to the lnd binary.
lndExecutable = flag.String(
"lndexec", itestLndBinary, "full path to lnd binary",
)
)
// calcStaticFee calculates appropriate fees for commitment transactions. This
// function provides a simple way to allow test balance assertions to take fee
// calculations into account.
func calcStaticFee(c lnrpc.CommitmentType, numHTLCs int) btcutil.Amount {
const htlcWeight = input.HTLCWeight
var (
feePerKw = chainfee.SatPerKWeight(
lntemp.DefaultFeeRateSatPerKw,
)
commitWeight = input.CommitWeight
anchors = btcutil.Amount(0)
defaultSatPerVByte = lnwallet.DefaultAnchorsCommitMaxFeeRateSatPerVByte
)
// The anchor commitment type is slightly heavier, and we must also add
// the value of the two anchors to the resulting fee the initiator
// pays. In addition the fee rate is capped at 10 sat/vbyte for anchor
// channels.
if lntemp.CommitTypeHasAnchors(c) {
feePerKw = chainfee.SatPerKVByte(
defaultSatPerVByte * 1000).FeePerKWeight()
commitWeight = input.AnchorCommitWeight
anchors = 2 * anchorSize
}
return feePerKw.FeeForWeight(int64(commitWeight+htlcWeight*numHTLCs)) +
anchors
}
// calculateMaxHtlc re-implements the RequiredRemoteChannelReserve of the
// funding manager's config, which corresponds to the maximum MaxHTLC value we
// allow users to set when updating a channel policy.
func calculateMaxHtlc(chanCap btcutil.Amount) uint64 {
reserve := lnwire.NewMSatFromSatoshis(chanCap / 100)
max := lnwire.NewMSatFromSatoshis(chanCap) - reserve
return uint64(max)
}
+46
View File
@@ -8,9 +8,14 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lntest/wait" "github.com/lightningnetwork/lnd/lntest/wait"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/lightningnetwork/lnd/lnwire"
) )
const ( const (
@@ -152,3 +157,44 @@ func NodeArgsForCommitType(commitType lnrpc.CommitmentType) []string {
return nil return nil
} }
// CalcStaticFee calculates appropriate fees for commitment transactions. This
// function provides a simple way to allow test balance assertions to take fee
// calculations into account.
func CalcStaticFee(c lnrpc.CommitmentType, numHTLCs int) btcutil.Amount {
const (
htlcWeight = input.HTLCWeight
anchorSize = 330
defaultSatPerVByte = lnwallet.DefaultAnchorsCommitMaxFeeRateSatPerVByte
)
var (
anchors = btcutil.Amount(0)
commitWeight = input.CommitWeight
feePerKw = chainfee.SatPerKWeight(DefaultFeeRateSatPerKw)
)
// The anchor commitment type is slightly heavier, and we must also add
// the value of the two anchors to the resulting fee the initiator
// pays. In addition the fee rate is capped at 10 sat/vbyte for anchor
// channels.
if CommitTypeHasAnchors(c) {
feePerKw = chainfee.SatPerKVByte(
defaultSatPerVByte * 1000,
).FeePerKWeight()
commitWeight = input.AnchorCommitWeight
anchors = 2 * anchorSize
}
return feePerKw.FeeForWeight(int64(commitWeight+htlcWeight*numHTLCs)) +
anchors
}
// CalculateMaxHtlc re-implements the RequiredRemoteChannelReserve of the
// funding manager's config, which corresponds to the maximum MaxHTLC value we
// allow users to set when updating a channel policy.
func CalculateMaxHtlc(chanCap btcutil.Amount) uint64 {
reserve := lnwire.NewMSatFromSatoshis(chanCap / 100)
max := lnwire.NewMSatFromSatoshis(chanCap) - reserve
return uint64(max)
}