Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show rune balances on /output page #2527

Merged
merged 9 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
270 changes: 173 additions & 97 deletions fuzz/Cargo.lock

Large diffs are not rendered by default.

47 changes: 46 additions & 1 deletion src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,8 +674,53 @@ impl Index {
}
}

pub(crate) fn get_rune_balances_for_outpoint(
&self,
outpoint: OutPoint,
) -> Result<Vec<(Rune, Pile)>> {
if !self.has_rune_index()? {
return Ok(Vec::new());
}

let rtx = &self.database.begin_read()?;

let outpoint_to_balances = rtx.open_table(OUTPOINT_TO_RUNE_BALANCES)?;

let id_to_rune_entries = rtx.open_table(RUNE_ID_TO_RUNE_ENTRY)?;

let Some(balances) = outpoint_to_balances.get(&outpoint.store())? else {
return Ok(Vec::new());
};

let balances_buffer = balances.value();

let mut balances = Vec::new();
let mut i = 0;
while i < balances_buffer.len() {
let (id, length) = runes::varint::decode(&balances_buffer[i..]).unwrap();
i += length;
let (amount, length) = runes::varint::decode(&balances_buffer[i..]).unwrap();
i += length;

let id = RuneId::try_from(id).unwrap();

let entry = RuneEntry::load(id_to_rune_entries.get(id.store())?.unwrap().value());

balances.push((
entry.rune,
Pile {
amount,
divisibility: entry.divisibility,
symbol: entry.symbol,
},
));
}

Ok(balances)
}

#[cfg(test)]
pub(crate) fn rune_balances(&self) -> Vec<(OutPoint, Vec<(RuneId, u128)>)> {
pub(crate) fn get_rune_balances(&self) -> Vec<(OutPoint, Vec<(RuneId, u128)>)> {
let mut result = Vec::new();

for entry in self
Expand Down
58 changes: 48 additions & 10 deletions src/index/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ pub(crate) struct RuneEntry {
pub(crate) burned: u128,
pub(crate) divisibility: u8,
pub(crate) etching: Txid,
pub(crate) rarity: Rarity,
pub(crate) rune: Rune,
pub(crate) supply: u128,
pub(crate) symbol: Option<char>,
}

pub(super) type RuneEntryValue = (u128, u8, (u128, u128), u8, u128, u128);
pub(super) type RuneEntryValue = (u128, u8, (u128, u128), u128, u128, u32);

impl Entry for RuneEntry {
type Value = RuneEntryValue;

fn load((burned, divisibility, etching, rarity, rune, supply): RuneEntryValue) -> Self {
fn load((burned, divisibility, etching, rune, supply, symbol): RuneEntryValue) -> Self {
Self {
burned,
divisibility,
Expand All @@ -51,9 +51,9 @@ impl Entry for RuneEntry {
high[14], high[15],
])
},
rarity: Rarity::try_from(rarity).unwrap(),
rune: Rune(rune),
supply,
symbol: char::from_u32(symbol),
}
}

Expand All @@ -74,9 +74,12 @@ impl Entry for RuneEntry {
]),
)
},
self.rarity.into(),
self.rune.0,
self.supply,
match self.symbol {
Some(symbol) => symbol.into(),
None => u32::max_value(),
},
)
}
}
Expand Down Expand Up @@ -401,9 +404,44 @@ mod tests {
0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
0x1E, 0x1F,
]),
rarity: Rarity::Epic,
rune: Rune(4),
supply: 5,
rune: Rune(3),
supply: 4,
symbol: Some('a'),
};

assert_eq!(
rune_entry.store(),
(
1,
2,
(
0x0F0E0D0C0B0A09080706050403020100,
0x1F1E1D1C1B1A19181716151413121110
),
3,
4,
u32::from('a'),
)
);

assert_eq!(
RuneEntry::load((
1,
2,
(
0x0F0E0D0C0B0A09080706050403020100,
0x1F1E1D1C1B1A19181716151413121110
),
3,
4,
u32::from('a'),
)),
rune_entry
);

let rune_entry = RuneEntry {
symbol: None,
..rune_entry
};

assert_eq!(
Expand All @@ -417,7 +455,7 @@ mod tests {
),
3,
4,
5,
u32::max_value(),
)
);

Expand All @@ -431,7 +469,7 @@ mod tests {
),
3,
4,
5,
u32::max_value(),
)),
rune_entry
);
Expand Down
14 changes: 4 additions & 10 deletions src/index/updater/rune_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ struct Allocation {
divisibility: u8,
id: u128,
rune: Rune,
symbol: Option<char>,
}

pub(super) struct RuneUpdater<'a, 'db, 'tx> {
count: usize,
height: u64,
id_to_entry: &'a mut Table<'db, 'tx, RuneIdValue, RuneEntryValue>,
minimum: Rune,
outpoint_to_balances: &'a mut Table<'db, 'tx, &'static OutPointValue, &'static [u8]>,
rune_to_id: &'a mut Table<'db, 'tx, u128, RuneIdValue>,
rarity: Rarity,
}

impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
Expand All @@ -28,8 +27,6 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
rune_to_id: &'a mut Table<'db, 'tx, u128, RuneIdValue>,
) -> Self {
Self {
count: 0,
rarity: Height(height).starting_sat().rarity(),
height,
id_to_entry,
minimum: Rune::minimum_at_height(Height(height)),
Expand Down Expand Up @@ -84,6 +81,7 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
divisibility: etching.divisibility,
id: u128::from(self.height) << 16 | u128::from(index),
rune: etching.rune,
symbol: etching.symbol,
}),
Err(_) => None,
}
Expand Down Expand Up @@ -132,6 +130,7 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
divisibility,
id,
rune,
symbol,
}) = allocation
{
// Calculate the allocated supply
Expand All @@ -147,18 +146,13 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
burned: 0,
divisibility,
etching: txid,
rarity: if self.count == 0 {
self.rarity
} else {
Rarity::Common
},
rune,
supply,
symbol,
}
.store(),
)?;
}
self.count += 1;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use {
options::Options,
outgoing::Outgoing,
representation::Representation,
runes::{Pile, RuneId},
runes::{Pile, Rune, RuneId},
subcommand::{Subcommand, SubcommandResult},
tally::Tally,
},
Expand Down
Loading