From aa53ed4c254b00d6558ac03dcbf12db1309d915f Mon Sep 17 00:00:00 2001 From: ffranr Date: Mon, 30 Sep 2024 17:33:32 +0100 Subject: [PATCH] rfq: remove unused function pricesWithinBounds --- rfq/negotiator.go | 35 ----------- rfq/negotiator_test.go | 136 ----------------------------------------- 2 files changed, 171 deletions(-) delete mode 100644 rfq/negotiator_test.go diff --git a/rfq/negotiator.go b/rfq/negotiator.go index f8635c3fa..74da7cfdc 100644 --- a/rfq/negotiator.go +++ b/rfq/negotiator.go @@ -2,7 +2,6 @@ package rfq import ( "fmt" - "math" "math/big" "sync" "time" @@ -13,7 +12,6 @@ import ( "github.com/lightninglabs/taproot-assets/rfqmath" "github.com/lightninglabs/taproot-assets/rfqmsg" "github.com/lightningnetwork/lnd/lnutils" - "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/routing/route" ) @@ -507,39 +505,6 @@ func expiryWithinBounds(expiryUnixTimestamp uint64, return diff >= int64(minExpiryLifetime) } -// priceWithinBounds returns true if the difference between the first price and -// the second price is within the given tolerance (in parts per million (PPM)). -// -// TODO(ffranr): Replace with FixedPoint[T].WithinTolerance. -func pricesWithinBounds(firstPrice lnwire.MilliSatoshi, - secondPrice lnwire.MilliSatoshi, tolerancePpm uint64) bool { - - // Handle the case where both prices are zero. - if firstPrice == 0 && secondPrice == 0 { - return true - } - - // Handle cases where either price is zero. - if firstPrice == 0 || secondPrice == 0 { - return false - } - - firstP := float64(firstPrice) - secondP := float64(secondPrice) - - // Calculate the absolute difference between both prices. - delta := math.Abs(firstP - secondP) - - // Normalize the delta by dividing by the greater of the two prices. - normalisedDelta := delta / math.Max(firstP, secondP) - - // Convert the fraction to parts per million (PPM). - deltaPpm := 1_000_000 * normalisedDelta - - // Compare the difference to the tolerance. - return deltaPpm <= float64(tolerancePpm) -} - // HandleIncomingBuyAccept handles an incoming buy accept message. This method // is called when a peer accepts a quote request from this node. The method // checks the price and expiry time of the quote accept message. Once validation diff --git a/rfq/negotiator_test.go b/rfq/negotiator_test.go deleted file mode 100644 index 20e2f1227..000000000 --- a/rfq/negotiator_test.go +++ /dev/null @@ -1,136 +0,0 @@ -package rfq - -import ( - "testing" - - "github.com/lightningnetwork/lnd/lnwire" - "github.com/stretchr/testify/require" -) - -// TestPricesWithinBounds tests the pricesWithinBounds function. -func TestPricesWithinBounds(t *testing.T) { - type testCase struct { - // firstPrice is the price to compare with secondPrice. - firstPrice lnwire.MilliSatoshi - - // secondPrice is the price to compare with firstPrice. - secondPrice lnwire.MilliSatoshi - - // tolerancePpm is the tolerance in parts per million (PPM) that - // the second price can deviate from the first price and still - // be considered within bounds. - tolerancePpm uint64 - - // withinBounds is the expected result of the bounds check. - withinBounds bool - } - - testCases := []testCase{ - { - // Case where secondPrice is 10% less than firstPrice, - // tolerance allows 11.11% (111111 PPM). Diff within - // bounds. - firstPrice: 100000, - secondPrice: 90000, - tolerancePpm: 111111, // 11.11% tolerance in PPM - withinBounds: true, - }, - { - // Case where firstPrice is 15% less than secondPrice, - // tolerance allows 17.65% (176470 PPM). Diff within - // bounds. - firstPrice: 85000, - secondPrice: 100000, - tolerancePpm: 176470, // 17.65% tolerance in PPM - withinBounds: true, - }, - { - // Case where secondPrice is 15% less than firstPrice, - // tolerance allows 10% (100000 PPM). Diff outside - // bounds. - firstPrice: 100000, - secondPrice: 85000, - tolerancePpm: 100000, // 10% tolerance in PPM - withinBounds: false, - }, - { - // Case where firstPrice and secondPrice are equal, - // tolerance is 0 PPM. Diff within bounds. - firstPrice: 100000, - secondPrice: 100000, - tolerancePpm: 0, // 0% tolerance in PPM - withinBounds: true, - }, - { - // Case where secondPrice is 1% more than firstPrice, - // tolerance allows 0.99% (9900 PPM). Diff outside - // bounds. - firstPrice: 100000, - secondPrice: 101000, - tolerancePpm: 9900, // 0.99% tolerance in PPM - withinBounds: false, - }, - { - // Case where secondPrice is 5% less than firstPrice, - // tolerance allows 5% (50000 PPM). Diff within bounds. - firstPrice: 100000, - secondPrice: 95000, - tolerancePpm: 50000, // 5% tolerance in PPM - withinBounds: true, - }, - { - // Case where secondPrice is 10% less than firstPrice, - // tolerance allows 9% (90000 PPM). Diff outside bounds. - firstPrice: 100000, - secondPrice: 90000, - tolerancePpm: 90000, // 9% tolerance in PPM - withinBounds: false, - }, - { - // Case where secondPrice is 9% less than firstPrice, - // tolerance allows 10% (100000 PPM). Diff within - // bounds. - firstPrice: 100000, - secondPrice: 91000, - tolerancePpm: 100000, // 10% tolerance in PPM - withinBounds: true, - }, - { - // Case where both prices are zero, should be within - // bounds. - firstPrice: 0, - secondPrice: 0, - tolerancePpm: 100000, // any tolerance in PPM - withinBounds: true, - }, - { - // Case where firstPrice is zero and secondPrice is - // non-zero, should not be within bounds. - firstPrice: 0, - secondPrice: 100000, - tolerancePpm: 100000, // any tolerance in PPM - withinBounds: false, - }, - { - // Case where secondPrice is zero and firstPrice is - // non-zero, should not be within bounds. - firstPrice: 100000, - secondPrice: 0, - tolerancePpm: 100000, // any tolerance in PPM - withinBounds: false, - }, - } - - // Run the test cases. - for idx, tc := range testCases { - result := pricesWithinBounds( - tc.firstPrice, tc.secondPrice, tc.tolerancePpm, - ) - - // Compare bounds check result with expected test case within - // bounds flag. - require.Equal( - t, tc.withinBounds, result, "Test case %d failed", idx, - ) - } -}