Skip to content

Commit 2d57c25

Browse files
bitromortacellemouton
authored andcommitted
firewall: redefine obfuscation interval
1 parent d3cab70 commit 2d57c25

File tree

2 files changed

+42
-45
lines changed

2 files changed

+42
-45
lines changed

firewall/privacy_mapper.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -537,16 +537,9 @@ func handleListChannelsResponse(db firewalldb.PrivacyMapDB,
537537
if localBalance > c.Capacity {
538538
localBalance = c.Capacity
539539
}
540-
if initiator {
541-
localBalance -= c.CommitFee
542-
}
543540

544541
// We adapt the remote balance accordingly.
545-
remoteBalance := c.Capacity - localBalance -
546-
c.CommitFee
547-
if !initiator {
548-
remoteBalance -= c.CommitFee
549-
}
542+
remoteBalance := c.Capacity - localBalance
550543

551544
// We hide the total sats sent and received.
552545
satsReceived, err := hideAmount(
@@ -748,8 +741,8 @@ func hideAmount(randIntn func(n int) (int, error), relativeVariation float64,
748741
// between 0 and 1.
749742
fuzzInterval := uint64(float64(amount) * relativeVariation)
750743

751-
amountMin := int(amount - fuzzInterval/2)
752-
amountMax := int(amount + fuzzInterval/2)
744+
amountMin := int(amount - fuzzInterval)
745+
amountMax := int(amount + fuzzInterval)
753746

754747
randAmount, err := randBetween(randIntn, amountMin, amountMax)
755748
if err != nil {
@@ -781,8 +774,8 @@ func hideTimestamp(randIntn func(n int) (int, error),
781774
}
782775

783776
// We vary symmetrically around the provided timestamp.
784-
timeMin := timestamp.Add(-absoluteVariation / 2)
785-
timeMax := timestamp.Add(absoluteVariation / 2)
777+
timeMin := timestamp.Add(-absoluteVariation)
778+
timeMax := timestamp.Add(absoluteVariation)
786779

787780
timeNs, err := randBetween(
788781
randIntn, int(timeMin.UnixNano()), int(timeMax.UnixNano()),

firewall/privacy_mapper_test.go

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -76,26 +76,26 @@ func TestPrivacyMapper(t *testing.T) {
7676
expectedReplacement: &lnrpc.ForwardingHistoryResponse{
7777
ForwardingEvents: []*lnrpc.ForwardingEvent{
7878
{
79-
AmtIn: 1_950,
80-
AmtInMsat: 1_950_200,
81-
AmtOut: 975,
82-
AmtOutMsat: 975_100,
83-
Fee: 975,
84-
FeeMsat: 975_100,
85-
Timestamp: 700,
86-
TimestampNs: 700_000_000_100,
79+
AmtIn: 1_900,
80+
AmtInMsat: 1_900_200,
81+
AmtOut: 950,
82+
AmtOutMsat: 950_100,
83+
Fee: 950,
84+
FeeMsat: 950_100,
85+
Timestamp: 400,
86+
TimestampNs: 400_000_000_100,
8787
ChanIdIn: 5178778334600911958,
8888
ChanIdOut: 3446430762436373227,
8989
},
9090
{
91-
AmtIn: 2_925,
92-
AmtInMsat: 2_925_200,
93-
AmtOut: 1_950,
94-
AmtOutMsat: 1_950_100,
95-
Fee: 975,
96-
FeeMsat: 975_100,
97-
Timestamp: 700,
98-
TimestampNs: 700_000_000_100,
91+
AmtIn: 2_850,
92+
AmtInMsat: 2_850_200,
93+
AmtOut: 1_900,
94+
AmtOutMsat: 1_900_100,
95+
Fee: 950,
96+
FeeMsat: 950_100,
97+
Timestamp: 400,
98+
TimestampNs: 400_000_000_100,
9999
ChanIdIn: 8672172843977902018,
100100
ChanIdOut: 1378354177616075123,
101101
},
@@ -167,11 +167,11 @@ func TestPrivacyMapper(t *testing.T) {
167167
Channels: []*lnrpc.Channel{
168168
{
169169
Capacity: 1_000_000,
170-
RemoteBalance: 513_375,
171-
LocalBalance: 485_625,
170+
RemoteBalance: 525_850,
171+
LocalBalance: 474_150,
172172
CommitFee: 1_000,
173-
TotalSatoshisSent: 487_600,
174-
TotalSatoshisReceived: 438_850,
173+
TotalSatoshisSent: 475_100,
174+
TotalSatoshisReceived: 427_600,
175175
RemotePubkey: "c8134495",
176176
Initiator: true,
177177
ChanId: 5178778334600911958,
@@ -362,16 +362,16 @@ func TestPrivacyMapper(t *testing.T) {
362362
// would also be dependend on the fee variation).
363363
amtOutMsat := msg.ForwardingEvents[0].AmtOutMsat
364364
amtInterval := uint64(amountVariation * float64(amtOutMsat))
365-
minAmt := amtOutMsat - amtInterval/2
366-
maxAmt := amtOutMsat + amtInterval/2
365+
minAmt := amtOutMsat - amtInterval
366+
maxAmt := amtOutMsat + amtInterval
367367

368368
// We keep track of the timestamp. We test only the timestamp in
369369
// seconds as there can be numerical inaccuracies with the
370370
// nanosecond one.
371371
timestamp := msg.ForwardingEvents[0].Timestamp
372372
timestampInterval := uint64(timeVariation) / 1e9
373-
minTime := timestamp - timestampInterval/2
374-
maxTime := timestamp + timestampInterval/2
373+
minTime := timestamp - timestampInterval
374+
maxTime := timestamp + timestampInterval
375375

376376
// We need a certain number of samples to have statistical
377377
// accuracy.
@@ -537,7 +537,9 @@ func TestRandBetween(t *testing.T) {
537537
func TestHideAmount(t *testing.T) {
538538
testAmount := uint64(10_000)
539539
relativeVariation := 0.05
540-
fuzzInterval := int(float64(testAmount) * relativeVariation)
540+
absoluteVariation := int(float64(testAmount) * relativeVariation)
541+
lowerBound := testAmount - uint64(absoluteVariation)
542+
upperBound := testAmount + uint64(absoluteVariation)
541543

542544
tests := []struct {
543545
name string
@@ -559,21 +561,21 @@ func TestHideAmount(t *testing.T) {
559561
name: "min value",
560562
randIntFn: func(int) (int, error) { return 0, nil },
561563
amount: testAmount,
562-
expected: 9750,
564+
expected: lowerBound,
563565
},
564566
{
565567
name: "max value",
566568
randIntFn: func(int) (int, error) {
567-
return fuzzInterval, nil
569+
return int(upperBound - lowerBound), nil
568570
},
569571
amount: testAmount,
570-
expected: 10250,
572+
expected: upperBound,
571573
},
572574
{
573575
name: "some fuzz",
574576
randIntFn: func(int) (int, error) { return 123, nil },
575577
amount: testAmount,
576-
expected: 9750 + 123,
578+
expected: lowerBound + 123,
577579
},
578580
}
579581

@@ -608,6 +610,8 @@ func TestHideAmount(t *testing.T) {
608610
func TestHideTimestamp(t *testing.T) {
609611
timestamp := time.Unix(1_000_000, 0)
610612
absoluteVariation := time.Duration(10) * time.Minute
613+
lowerBound := timestamp.Add(-absoluteVariation)
614+
upperBound := timestamp.Add(absoluteVariation)
611615

612616
tests := []struct {
613617
name string
@@ -623,21 +627,21 @@ func TestHideTimestamp(t *testing.T) {
623627
name: "min value",
624628
randIntFn: func(int) (int, error) { return 0, nil },
625629
timestamp: timestamp,
626-
expected: time.Unix(999_700, 0),
630+
expected: lowerBound,
627631
},
628632
{
629633
name: "max value",
630634
randIntFn: func(int) (int, error) {
631-
return int(absoluteVariation), nil
635+
return int(upperBound.Sub(lowerBound)), nil
632636
},
633637
timestamp: timestamp,
634-
expected: time.Unix(1_000_300, 0),
638+
expected: upperBound,
635639
},
636640
{
637641
name: "some fuzz",
638642
randIntFn: func(int) (int, error) { return 123, nil },
639643
timestamp: timestamp,
640-
expected: time.Unix(999_700, 123),
644+
expected: lowerBound.Add(time.Duration(123)),
641645
},
642646
}
643647

0 commit comments

Comments
 (0)