Skip to content

Commit

Permalink
Allocate remainder when splitting runes
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Dec 23, 2023
1 parent d856b05 commit 3b631c0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
11 changes: 8 additions & 3 deletions src/index/updater/rune_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,14 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
if amount == 0 {
// if amount is zero, divide balance between eligible outputs
let amount = *balance / destinations.len() as u128;

for output in destinations {
allocate(balance, amount, output);
let remainder = usize::try_from(*balance % destinations.len() as u128).unwrap();

for (i, output) in destinations.iter().enumerate() {
allocate(
balance,
if i < remainder { amount + 1 } else { amount },
*output,
);
}
} else {
// if amount is non-zero, distribute amount to eligible outputs
Expand Down
26 changes: 13 additions & 13 deletions src/runes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2770,23 +2770,23 @@ mod tests {
RuneEntry {
etching: txid,
rune: Rune(RUNE),
supply: (u128::max_value() / 4) * 4,
supply: u128::max_value(),
timestamp: 2,
..Default::default()
},
)],
[
(
OutPoint { txid, vout: 0 },
vec![(id, u128::max_value() / 4)],
vec![(id, u128::max_value() / 4 + 1)],
),
(
OutPoint { txid, vout: 1 },
vec![(id, u128::max_value() / 4)],
vec![(id, u128::max_value() / 4 + 1)],
),
(
OutPoint { txid, vout: 2 },
vec![(id, u128::max_value() / 4)],
vec![(id, u128::max_value() / 4 + 1)],
),
(
OutPoint { txid, vout: 3 },
Expand Down Expand Up @@ -2843,23 +2843,23 @@ mod tests {
RuneEntry {
etching: txid,
rune: Rune(RUNE),
supply: 1000 + ((u128::max_value() - 1000) / 4) * 4,
supply: u128::max_value(),
timestamp: 2,
..Default::default()
},
)],
[
(
OutPoint { txid, vout: 0 },
vec![(id, 1000 + (u128::max_value() - 1000) / 4)],
vec![(id, 1000 + (u128::max_value() - 1000) / 4 + 1)],
),
(
OutPoint { txid, vout: 1 },
vec![(id, (u128::max_value() - 1000) / 4)],
vec![(id, (u128::max_value() - 1000) / 4 + 1)],
),
(
OutPoint { txid, vout: 2 },
vec![(id, (u128::max_value() - 1000) / 4)],
vec![(id, (u128::max_value() - 1000) / 4 + 1)],
),
(
OutPoint { txid, vout: 3 },
Expand Down Expand Up @@ -2924,15 +2924,15 @@ mod tests {
[
(
OutPoint { txid, vout: 0 },
vec![(id, u128::max_value() / 4 + 3)],
vec![(id, u128::max_value() / 4 + 1)],
),
(
OutPoint { txid, vout: 1 },
vec![(id, u128::max_value() / 4)],
vec![(id, u128::max_value() / 4 + 1)],
),
(
OutPoint { txid, vout: 2 },
vec![(id, u128::max_value() / 4)],
vec![(id, u128::max_value() / 4 + 1)],
),
(
OutPoint { txid, vout: 3 },
Expand Down Expand Up @@ -3430,14 +3430,14 @@ mod tests {
txid: txid1,
vout: 0,
},
vec![(id, u128::max_value() / 2)],
vec![(id, u128::max_value() / 2 + 1)],
),
(
OutPoint {
txid: txid1,
vout: 1,
},
vec![(id, u128::max_value() / 2 + 1)],
vec![(id, u128::max_value() / 2)],
),
],
);
Expand Down

0 comments on commit 3b631c0

Please sign in to comment.