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

Implement open etchings #2548

Merged
merged 21 commits into from
Oct 30, 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
68 changes: 52 additions & 16 deletions src/index/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,37 @@ impl Entry for BlockHash {
pub(crate) struct RuneEntry {
pub(crate) burned: u128,
pub(crate) divisibility: u8,
pub(crate) end: Option<u64>,
pub(crate) etching: Txid,
pub(crate) limit: Option<u128>,
pub(crate) number: u64,
pub(crate) rune: Rune,
pub(crate) supply: u128,
pub(crate) symbol: Option<char>,
pub(crate) timestamp: u32,
}

pub(super) type RuneEntryValue = (u128, u8, (u128, u128), u64, u128, u128, u32, u32);
pub(super) type RuneEntryValue = (
u128, // burned
u8, // divisibility
u64, // end
(u128, u128), // etching
u128, // limit
u64, // number
u128, // rune
u128, // supply
u32, // symbol
u32, // timestamp
);

impl Default for RuneEntry {
fn default() -> Self {
Self {
burned: 0,
divisibility: 0,
end: None,
etching: Txid::all_zeros(),
limit: None,
number: 0,
rune: Rune(0),
supply: 0,
Expand All @@ -55,11 +70,12 @@ impl Entry for RuneEntry {
type Value = RuneEntryValue;

fn load(
(burned, divisibility, etching, number, rune, supply, symbol, timestamp): RuneEntryValue,
(burned, divisibility, end, etching, limit, number, rune, supply, symbol, timestamp): RuneEntryValue,
) -> Self {
Self {
burned,
divisibility,
end: (end != u64::max_value()).then_some(end),
etching: {
let low = etching.0.to_le_bytes();
let high = etching.1.to_le_bytes();
Expand All @@ -70,6 +86,7 @@ impl Entry for RuneEntry {
high[14], high[15],
])
},
limit: (limit != u128::max_value()).then_some(limit),
number,
rune: Rune(rune),
supply,
Expand All @@ -82,6 +99,7 @@ impl Entry for RuneEntry {
(
self.burned,
self.divisibility,
self.end.unwrap_or(u64::max_value()),
{
let bytes = self.etching.to_byte_array();
(
Expand All @@ -95,13 +113,11 @@ impl Entry for RuneEntry {
]),
)
},
self.limit.unwrap_or(u128::max_value()),
self.number,
self.rune.0,
self.supply,
match self.symbol {
Some(symbol) => symbol.into(),
None => u32::max_value(),
},
self.symbol.map(u32::from).unwrap_or(u32::max_value()),
self.timestamp,
)
}
Expand Down Expand Up @@ -132,7 +148,15 @@ pub(crate) struct InscriptionEntry {
pub(crate) timestamp: u32,
}

pub(crate) type InscriptionEntryValue = (u64, u64, i64, ParentValue, u64, u64, u32);
pub(crate) type InscriptionEntryValue = (
u64, // fee
u64, // height
i64, // inscription number
ParentValue, // parent
u64, // sat
u64, // sequence number
u32, // timestamp
);

impl Entry for InscriptionEntry {
type Value = InscriptionEntryValue;
Expand Down Expand Up @@ -422,14 +446,16 @@ mod tests {
let rune_entry = RuneEntry {
burned: 1,
divisibility: 2,
end: Some(3),
etching: Txid::from_byte_array([
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,
0x1E, 0x1F,
]),
number: 3,
rune: Rune(4),
supply: 5,
limit: Some(4),
number: 5,
rune: Rune(6),
supply: 7,
symbol: Some('a'),
timestamp: 6,
};
Expand All @@ -439,13 +465,15 @@ mod tests {
(
1,
2,
3,
(
0x0F0E0D0C0B0A09080706050403020100,
0x1F1E1D1C1B1A19181716151413121110
),
3,
4,
5,
6,
7,
u32::from('a'),
6,
)
Expand All @@ -455,13 +483,15 @@ mod tests {
RuneEntry::load((
1,
2,
3,
(
0x0F0E0D0C0B0A09080706050403020100,
0x1F1E1D1C1B1A19181716151413121110
),
3,
4,
5,
6,
7,
u32::from('a'),
6,
)),
Expand All @@ -470,6 +500,8 @@ mod tests {

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

Expand All @@ -478,13 +510,15 @@ mod tests {
(
1,
2,
u64::max_value(),
(
0x0F0E0D0C0B0A09080706050403020100,
0x1F1E1D1C1B1A19181716151413121110
),
3,
4,
u128::max_value(),
5,
6,
7,
u32::max_value(),
6,
)
Expand All @@ -494,13 +528,15 @@ mod tests {
RuneEntry::load((
1,
2,
u64::max_value(),
(
0x0F0E0D0C0B0A09080706050403020100,
0x1F1E1D1C1B1A19181716151413121110
),
3,
4,
u128::max_value(),
5,
6,
7,
u32::max_value(),
6,
)),
Expand Down
36 changes: 36 additions & 0 deletions src/index/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,40 @@ impl Context {
Context::builder().arg("--index-sats").build(),
]
}

#[track_caller]
pub(crate) fn assert_runes(
&self,
mut runes: impl AsMut<[(RuneId, RuneEntry)]>,
mut balances: impl AsMut<[(OutPoint, Vec<(RuneId, u128)>)]>,
) {
let runes = runes.as_mut();
runes.sort_by_key(|(id, _)| *id);

let balances = balances.as_mut();
balances.sort_by_key(|(outpoint, _)| *outpoint);

for (_, balances) in balances.iter_mut() {
balances.sort_by_key(|(id, _)| *id);
}

assert_eq!(runes, self.index.runes().unwrap());

assert_eq!(balances, self.index.get_rune_balances());

let mut outstanding: HashMap<RuneId, u128> = HashMap::new();

for (_, balances) in balances {
for (id, balance) in balances {
*outstanding.entry(*id).or_default() += *balance;
}
}

for (id, entry) in runes {
assert_eq!(
outstanding.get(id).copied().unwrap_or_default(),
entry.supply - entry.burned
);
}
}
}
Loading