From eb243dfb48a2a2961ac5fb2d3a003e21c9c8c724 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 24 Dec 2023 22:00:39 +0800 Subject: [PATCH] Split runes more evenly (#2897) --- src/index/updater/rune_updater.rs | 11 ++++++++--- src/runes.rs | 26 +++++++++++++------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/index/updater/rune_updater.rs b/src/index/updater/rune_updater.rs index c7c37f6e5d..5c22fe91ee 100644 --- a/src/index/updater/rune_updater.rs +++ b/src/index/updater/rune_updater.rs @@ -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 diff --git a/src/runes.rs b/src/runes.rs index b7224fbccf..4ce5ea9f89 100644 --- a/src/runes.rs +++ b/src/runes.rs @@ -2770,7 +2770,7 @@ mod tests { RuneEntry { etching: txid, rune: Rune(RUNE), - supply: (u128::max_value() / 4) * 4, + supply: u128::max_value(), timestamp: 2, ..Default::default() }, @@ -2778,15 +2778,15 @@ mod tests { [ ( 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 }, @@ -2843,7 +2843,7 @@ mod tests { RuneEntry { etching: txid, rune: Rune(RUNE), - supply: 1000 + ((u128::max_value() - 1000) / 4) * 4, + supply: u128::max_value(), timestamp: 2, ..Default::default() }, @@ -2851,15 +2851,15 @@ mod tests { [ ( 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 }, @@ -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 }, @@ -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)], ), ], );