Skip to content

Commit a899dea

Browse files
Lagrang3rustyrussell
authored andcommitted
use amount_msat_mul_div operation to compute fees
Changelog-None Signed-off-by: Lagrang3 <[email protected]>
1 parent b379500 commit a899dea

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

common/amount.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -581,11 +581,9 @@ bool amount_msat_fee(struct amount_msat *fee,
581581
* - fee_base_msat + ( amount_to_forward * fee_proportional_millionths / 1000000 )
582582
*/
583583
fee_base.millisatoshis = fee_base_msat;
584-
585-
if (mul_overflows_u64(amt.millisatoshis, fee_proportional_millionths))
584+
if (!amount_msat_mul_div(&fee_prop, amt, fee_proportional_millionths,
585+
1000000))
586586
return false;
587-
fee_prop.millisatoshis = amt.millisatoshis * fee_proportional_millionths
588-
/ 1000000;
589587

590588
return amount_msat_add(fee, fee_base, fee_prop);
591589
}

common/test/run-amount.c

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,23 @@ static void test_amount_sub_fee(struct amount_msat msat,
8585
assert(amount_msat_greater(in2, in));
8686
}
8787

88+
static void test_amount_fee(struct amount_msat msat, u32 base, u32 prop,
89+
struct amount_msat expected_msat)
90+
{
91+
struct amount_msat fee;
92+
assert(amount_msat_fee(&fee, msat, base, prop));
93+
assert(amount_msat_eq(fee, expected_msat));
94+
}
95+
96+
static void test_amount_fee_str(const char *msat_str, u32 base, u32 prop,
97+
u64 expected)
98+
{
99+
struct amount_msat msat;
100+
struct amount_msat expected_msat = amount_msat(expected);
101+
assert(parse_amount_msat(&msat, msat_str, strlen(msat_str)));
102+
return test_amount_fee(msat, base, prop, expected_msat);
103+
}
104+
88105
static void test_amount_with_fee(void)
89106
{
90107
for (int basebits = 0; basebits < 33; basebits++) {
@@ -93,13 +110,57 @@ static void test_amount_with_fee(void)
93110
for (int propbits = 0; propbits < 20; propbits++) {
94111
u32 prop = (1ULL << propbits);
95112

96-
for (int amtbits1 = 0; amtbits1 < 42; amtbits1++) {
97-
for (int amtbits2 = 0; amtbits2 < 42; amtbits2++) {
113+
for (int amtbits1 = 0; amtbits1 < 63; amtbits1++) {
114+
for (int amtbits2 = 0; amtbits2 < 63; amtbits2++) {
98115
test_amount_sub_fee(amount_msat((1ULL << amtbits1) | (1ULL << amtbits2)), base, prop);
99116
}
100117
}
101118
}
102119
}
120+
121+
for (int basebits = 0; basebits < 33; basebits++) {
122+
u32 base = (1ULL << basebits);
123+
for (int propbits = 0; propbits < 20; propbits++) {
124+
u32 prop = (1ULL << propbits);
125+
test_amount_fee(amount_msat(0), base, prop,
126+
amount_msat(base));
127+
}
128+
}
129+
for (int basebits = 0; basebits < 33; basebits++) {
130+
u32 base = (1ULL << basebits);
131+
for (int amtbits = 0; amtbits < 63; amtbits++)
132+
test_amount_fee(amount_msat(1ULL << amtbits), base, 0,
133+
amount_msat(base));
134+
}
135+
for (int amtbits = 0; amtbits < 63; amtbits++)
136+
test_amount_fee(amount_msat(1ULL << amtbits), 0, 0,
137+
amount_msat(0));
138+
139+
test_amount_fee_str("1msat", 1, 1, 1);
140+
test_amount_fee_str("1msat", 1, 500000, 1);
141+
test_amount_fee_str("1msat", 1, 1000000, 2);
142+
143+
test_amount_fee_str("1msat", 1234567890, 1, 1234567890ULL);
144+
test_amount_fee_str("1msat", 1234567890, 500000, 1234567890ULL);
145+
test_amount_fee_str("1msat", 1234567890, 1000000, 1234567891ULL);
146+
147+
test_amount_fee_str("1btc", 1, 1, 100001ULL);
148+
test_amount_fee_str("1btc", 1, 500000, 50000000001ULL);
149+
test_amount_fee_str("1btc", 1, 1000000, 100000000001ULL);
150+
151+
test_amount_fee_str("1btc", 1234567890, 1, 1234667890ULL);
152+
test_amount_fee_str("1btc", 1234567890, 500000, 51234567890ULL);
153+
test_amount_fee_str("1btc", 1234567890, 1000000, 101234567890ULL);
154+
155+
test_amount_fee_str("21000000btc", 1, 1, 2100000000001ULL);
156+
test_amount_fee_str("21000000btc", 1, 500000, 1050000000000000001ULL);
157+
test_amount_fee_str("21000000btc", 1, 1000000, 2100000000000000001ULL);
158+
159+
test_amount_fee_str("21000000btc", 1234567890, 1, 2101234567890ULL);
160+
test_amount_fee_str("21000000btc", 1234567890, 500000,
161+
1050000001234567890ULL);
162+
test_amount_fee_str("21000000btc", 1234567890, 1000000,
163+
2100000001234567890ULL);
103164
}
104165

105166
#define FAIL_MSAT(msatp, str) \

0 commit comments

Comments
 (0)