Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions chanevents/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,31 @@ type channelEventSeq = iter.Seq2[*ChannelEvent, error]
// pair over the analysis window. It carries no derived rates or categories. The
// consumer derives velocity and uptime fraction from these and the window, and
// reconstructs any categorization (such as forwards observed without qualifying
// uptime) from EffectiveUptime and ForwardedAmount.
// uptime) from EffectiveUptime and ForwardedMsat.
type ForwardingAbility struct {
// EffectiveUptime is the time the pair held at least the liquidity floor
// of directional forwardable liquidity over the window.
EffectiveUptime time.Duration

// ForwardedAmount is the total successfully forwarded amount over the
// window, at satoshi granularity.
ForwardedAmount btcutil.Amount
// ForwardedMsat is the total successfully forwarded amount over the
// window, in millisatoshis.
ForwardedMsat lnwire.MilliSatoshi

// FeeMsat is the total fee earned on the pair's forwards over the
// window.
FeeMsat lnwire.MilliSatoshi

// Forwards is how many successful forwards the pair carried. It decides
// whether the pair forwarded at all, since both sums can be zero for
// one that did: sub-satoshi volume floors away and a zero-fee policy
// earns nothing.
// whether the pair forwarded at all, since the fee total can be zero
// for one that did under a zero-fee policy.
Forwards int64
}

// pairForwards accumulates what one direction of a peer pair carried over the
// analysis window.
type pairForwards struct {
// amount is the total forwarded amount, at satoshi granularity.
amount btcutil.Amount
// amountMsat is the total forwarded amount, in millisatoshis.
amountMsat lnwire.MilliSatoshi

// feeMsat is the total fee earned on those forwards.
feeMsat lnwire.MilliSatoshi
Expand Down Expand Up @@ -272,7 +271,7 @@ func (a *ForwardingAnalyzer) getForwardingData(ctx context.Context, startTime,
successfulForwards[pair] = totals
}

totals.amount += fwd.AmountMsatOut.ToSatoshis()
totals.amountMsat += fwd.AmountMsatOut
totals.feeMsat += fwd.FeeMsat
totals.forwards++
}
Expand Down Expand Up @@ -923,7 +922,7 @@ func makeAbility(totalUptime time.Duration,

return &ForwardingAbility{
EffectiveUptime: totalUptime,
ForwardedAmount: totals.amount,
ForwardedMsat: totals.amountMsat,
FeeMsat: totals.feeMsat,
Forwards: totals.forwards,
}
Expand Down
25 changes: 16 additions & 9 deletions chanevents/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,8 @@ func TestCalculateBothDirectionsUptime(t *testing.T) {
forwards: int64(len(tc.successAmts)),
}
for _, amt := range tc.successAmts {
forwards.amount += amt
forwards.amountMsat +=
lnwire.NewMSatFromSatoshis(amt)

// Two msat of fee per satoshi
// forwarded, so the fee total is
Expand Down Expand Up @@ -651,7 +652,7 @@ func TestCalculateBothDirectionsUptime(t *testing.T) {
require.NoError(t, err)
require.Equal(t, &ForwardingAbility{
EffectiveUptime: tc.expectedUptime,
ForwardedAmount: forwards.amount,
ForwardedMsat: forwards.amountMsat,
FeeMsat: forwards.feeMsat,
Forwards: forwards.forwards,
}, abilityAB)
Expand Down Expand Up @@ -716,15 +717,15 @@ func TestCalculateBothDirectionsUptimeAsymmetric(t *testing.T) {
liquidityFloor, statesA, statesB,
sumARemote, sumALocal, sumBRemote, sumBLocal,
mergeEventSlices(nil, nil),
pairForwards{amount: 100, feeMsat: 200, forwards: 2},
pairForwards{amount: 50, feeMsat: 100, forwards: 1},
pairForwards{amountMsat: 100_000, feeMsat: 200, forwards: 2},
pairForwards{amountMsat: 50_000, feeMsat: 100, forwards: 1},
)
require.NoError(t, err)

require.Equal(
t, &ForwardingAbility{
EffectiveUptime: 100 * time.Second,
ForwardedAmount: 100,
ForwardedMsat: 100_000,
FeeMsat: 200,
Forwards: 2,
}, abilityAB,
Expand All @@ -734,7 +735,7 @@ func TestCalculateBothDirectionsUptimeAsymmetric(t *testing.T) {
// Forwards landed but BA never crossed the floor: zero
// uptime, volume and fees still reported.
EffectiveUptime: 0,
ForwardedAmount: 50,
ForwardedMsat: 50_000,
FeeMsat: 100,
Forwards: 1,
}, abilityBA,
Expand Down Expand Up @@ -1041,14 +1042,17 @@ func TestEffectiveUptimeArgs(t *testing.T) {

// Case 1: liquidityFloor = 50k. Liquidity of 1M exceeds the floor for
// the whole 60s window, so effective uptime is the full minute and the
// forwarded volume is the 400k sat total, earning 400k msat.
// forwarded volume is the 400M msat total, earning 400k msat.
abilities, err := a.EffectiveUptime(ctx, startTime, endTime, 50_000)
require.NoError(t, err)

pair := PeerPair{PeerIn: validPubKey1, PeerOut: validPubKey2}
require.Contains(t, abilities, pair)
require.Equal(t, time.Minute, abilities[pair].EffectiveUptime)
require.Equal(t, btcutil.Amount(400_000), abilities[pair].ForwardedAmount)
require.Equal(
t, lnwire.MilliSatoshi(400_000_000),
abilities[pair].ForwardedMsat,
)
require.Equal(
t, lnwire.MilliSatoshi(400_000), abilities[pair].FeeMsat,
)
Expand All @@ -1062,7 +1066,10 @@ func TestEffectiveUptimeArgs(t *testing.T) {

require.Contains(t, abilities, pair)
require.Zero(t, abilities[pair].EffectiveUptime)
require.Equal(t, btcutil.Amount(400_000), abilities[pair].ForwardedAmount)
require.Equal(
t, lnwire.MilliSatoshi(400_000_000),
abilities[pair].ForwardedMsat,
)
require.Equal(
t, lnwire.MilliSatoshi(400_000), abilities[pair].FeeMsat,
)
Expand Down
21 changes: 12 additions & 9 deletions cmd/frcli/forwarding_ability.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ var forwardingAbilityCommand = cli.Command{
type pairView struct {
PeerIn string `json:"peer_in"`
PeerOut string `json:"peer_out"`
EffectiveUptimeS int64 `json:"effective_uptime_s"`
ForwardedSat int64 `json:"forwarded_sat"`
EffectiveUptimeS uint64 `json:"effective_uptime_s"`
ForwardedMsat int64 `json:"forwarded_msat"`
FeeMsat int64 `json:"fee_msat"`
Forwards int64 `json:"forwards"`
UptimeFraction float64 `json:"uptime_fraction"`
Expand Down Expand Up @@ -89,26 +89,29 @@ func queryForwardingAbility(ctx *cli.Context) error {
) / float64(windowSeconds)
}

// Volume over the uptime that carried it, in
// millisatoshis per second.
if ability.EffectiveUptimeS > 0 {
velocity = float64(ability.ForwardedSat) /
velocity = float64(ability.ForwardedMsat) /
float64(ability.EffectiveUptimeS)
}

// Fees over volume. A pair with only sub-satoshi
// forwards has no volume to divide by, so it keeps a
// zero rate rather than an infinite one.
// Fees over volume, both in millisatoshis, so the rate
// is the unitless fraction of volume kept as fees. A
// pair with no volume has nothing to divide by, so it
// keeps a zero rate rather than an infinite one.
var feeRate float64
if ability.ForwardedSat > 0 {
if ability.ForwardedMsat > 0 {
feeRate = float64(ability.FeeMsat) /
(float64(ability.ForwardedSat) * 1000)
float64(ability.ForwardedMsat)
}

views = append(
views, pairView{
PeerIn: inPeer,
PeerOut: outPeer,
EffectiveUptimeS: ability.EffectiveUptimeS,
ForwardedSat: ability.ForwardedSat,
ForwardedMsat: ability.ForwardedMsat,
FeeMsat: ability.FeeMsat,
Forwards: ability.Forwards,
UptimeFraction: uptimeFraction,
Expand Down
Loading
Loading