Skip to content

Commit

Permalink
Use a flag to indicate a mint (ordinals#3068)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Feb 2, 2024
1 parent 30a57b2 commit 84d5fbe
Show file tree
Hide file tree
Showing 18 changed files with 444 additions and 229 deletions.
4 changes: 2 additions & 2 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use {
},
};

pub use self::entry::RuneEntry;
pub use {self::entry::RuneEntry, entry::MintEntry};

pub(crate) mod entry;
mod fetcher;
Expand All @@ -41,7 +41,7 @@ mod updater;
#[cfg(test)]
pub(crate) mod testing;

const SCHEMA_VERSION: u64 = 16;
const SCHEMA_VERSION: u64 = 17;

macro_rules! define_table {
($name:ident, $key:ty, $value:ty) => {
Expand Down
84 changes: 48 additions & 36 deletions src/index/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ impl Entry for Header {
#[derive(Debug, PartialEq, Copy, Clone, Serialize, Deserialize)]
pub struct RuneEntry {
pub burned: u128,
pub deadline: Option<u32>,
pub divisibility: u8,
pub end: Option<u32>,
pub etching: Txid,
pub limit: Option<u128>,
pub mint: Option<MintEntry>,
pub mints: u64,
pub number: u64,
pub rune: Rune,
Expand All @@ -46,21 +44,30 @@ pub struct RuneEntry {
}

pub(super) type RuneEntryValue = (
u128, // burned
u128, // burned
u8, // divisibility
(u128, u128), // etching
Option<MintEntryValue>, // mint parameters
u64, // mints
u64, // number
u128, // rune
u32, // spacers
u128, // supply
Option<char>, // symbol
u32, // timestamp
);

#[derive(Debug, PartialEq, Copy, Clone, Serialize, Deserialize, Default)]
pub struct MintEntry {
pub deadline: Option<u32>,
pub end: Option<u32>,
pub limit: Option<u128>,
}

type MintEntryValue = (
Option<u32>, // deadline
u8, // divisibility
Option<u32>, // end
(u128, u128), // etching
Option<u128>, // limit
(
u64, // mints
u64, // number
),
u128, // rune
u32, // spacers
u128, // supply
Option<char>, // symbol
u32, // timestamp
);

impl RuneEntry {
Expand All @@ -76,11 +83,9 @@ impl Default for RuneEntry {
fn default() -> Self {
Self {
burned: 0,
deadline: None,
divisibility: 0,
end: None,
etching: Txid::all_zeros(),
limit: None,
mint: None,
mints: 0,
number: 0,
rune: Rune(0),
Expand All @@ -98,12 +103,11 @@ impl Entry for RuneEntry {
fn load(
(
burned,
deadline,
divisibility,
end,
etching,
limit,
(mints, number),
mint,
mints,
number,
rune,
spacers,
supply,
Expand All @@ -113,9 +117,7 @@ impl Entry for RuneEntry {
) -> Self {
Self {
burned,
deadline,
divisibility,
end,
etching: {
let low = etching.0.to_le_bytes();
let high = etching.1.to_le_bytes();
Expand All @@ -126,7 +128,11 @@ impl Entry for RuneEntry {
high[14], high[15],
])
},
limit,
mint: mint.map(|(deadline, end, limit)| MintEntry {
deadline,
end,
limit,
}),
mints,
number,
rune: Rune(rune),
Expand All @@ -140,9 +146,7 @@ impl Entry for RuneEntry {
fn store(self) -> Self::Value {
(
self.burned,
self.deadline,
self.divisibility,
self.end,
{
let bytes = self.etching.to_byte_array();
(
Expand All @@ -156,8 +160,15 @@ impl Entry for RuneEntry {
]),
)
},
self.limit,
(self.mints, self.number),
self.mint.map(
|MintEntry {
deadline,
end,
limit,
}| (deadline, end, limit),
),
self.mints,
self.number,
self.rune.0,
self.spacers,
self.supply,
Expand Down Expand Up @@ -434,15 +445,17 @@ mod tests {
fn rune_entry() {
let entry = RuneEntry {
burned: 1,
deadline: Some(2),
divisibility: 3,
end: Some(4),
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,
]),
limit: Some(5),
mint: Some(MintEntry {
deadline: Some(2),
end: Some(4),
limit: Some(5),
}),
mints: 11,
number: 6,
rune: Rune(7),
Expand All @@ -454,15 +467,14 @@ mod tests {

let value = (
1,
Some(2),
3,
Some(4),
(
0x0F0E0D0C0B0A09080706050403020100,
0x1F1E1D1C1B1A19181716151413121110,
),
Some(5),
(11, 6),
Some((Some(2), Some(4), Some(5))),
11,
6,
7,
8,
9,
Expand Down
6 changes: 3 additions & 3 deletions src/index/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ impl Context {
balances.sort_by_key(|(id, _)| *id);
}

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

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

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

Expand All @@ -133,7 +133,7 @@ impl Context {
}

for (id, entry) in runes {
assert_eq!(
pretty_assert_eq!(
outstanding.get(id).copied().unwrap_or_default(),
entry.supply - entry.burned
);
Expand Down
45 changes: 18 additions & 27 deletions src/index/updater/rune_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ fn claim(id: u128) -> Option<u128> {

struct Allocation {
balance: u128,
deadline: Option<u32>,
divisibility: u8,
end: Option<u32>,
id: u128,
limit: Option<u128>,
mint: Option<MintEntry>,
rune: Rune,
spacers: u32,
symbol: Option<char>,
Expand Down Expand Up @@ -112,35 +110,32 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
Rune::reserved(reserved_runes.into())
};

let (limit, term) = match (etching.limit, etching.term) {
(None, Some(term)) => (Some(runes::MAX_LIMIT), Some(term)),
(limit, term) => (limit, term),
};

// Construct an allocation, representing the new runes that may be
// allocated. Beware: Because it would require constructing a block
// with 2**16 + 1 transactions, there is no test that checks that
// an eching in a transaction with an out-of-bounds index is
// ignored.
match u16::try_from(index) {
Ok(index) => Some(Allocation {
balance: if let Some(limit) = limit {
if term == Some(0) {
balance: if let Some(mint) = etching.mint {
if mint.term == Some(0) {
0
} else {
limit
mint.limit.unwrap_or(runes::MAX_LIMIT)
}
} else {
u128::max_value()
},
deadline: etching.deadline,
divisibility: etching.divisibility,
end: term.map(|term| term + self.height),
id: u128::from(self.height) << 16 | u128::from(index),
limit,
rune,
spacers: etching.spacers,
symbol: etching.symbol,
mint: etching.mint.map(|mint| MintEntry {
deadline: mint.deadline,
end: mint.term.map(|term| term + self.height),
limit: mint.limit.map(|limit| limit.clamp(0, runes::MAX_LIMIT)),
}),
}),
Err(_) => None,
}
Expand All @@ -163,18 +158,18 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
if let Ok(key) = RuneId::try_from(id) {
if let Some(entry) = self.id_to_entry.get(&key.store())? {
let entry = RuneEntry::load(entry.value());
if let Some(limit) = entry.limit {
if let Some(end) = entry.end {
if let Some(mint) = entry.mint {
if let Some(end) = mint.end {
if self.height >= end {
continue;
}
}
if let Some(deadline) = entry.deadline {
if let Some(deadline) = mint.deadline {
if self.timestamp >= deadline {
continue;
}
}
mintable.insert(id, limit);
mintable.insert(id, mint.limit.unwrap_or(runes::MAX_LIMIT));
}
}
}
Expand Down Expand Up @@ -278,11 +273,9 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {

if let Some(Allocation {
balance,
deadline,
divisibility,
end,
id,
limit,
mint,
rune,
spacers,
symbol,
Expand All @@ -300,25 +293,23 @@ impl<'a, 'db, 'tx> RuneUpdater<'a, 'db, 'tx> {
id.store(),
RuneEntry {
burned: 0,
deadline: deadline.and_then(|deadline| (!burn).then_some(deadline)),
divisibility,
etching: txid,
mints: 0,
number,
mint: mint.and_then(|mint| (!burn).then_some(mint)),
rune,
spacers,
supply: if let Some(limit) = limit {
if end == Some(self.height) {
supply: if let Some(mint) = mint {
if mint.end == Some(self.height) {
0
} else {
limit
mint.limit.unwrap_or(runes::MAX_LIMIT)
}
} else {
u128::max_value()
} - balance,
end: end.and_then(|end| (!burn).then_some(end)),
symbol,
limit: limit.and_then(|limit| (!burn).then_some(limit)),
timestamp: self.timestamp,
}
.store(),
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ use {
pub use self::{
chain::Chain,
fee_rate::FeeRate,
index::{Index, RuneEntry},
index::{Index, MintEntry, RuneEntry},
inscriptions::{Envelope, Inscription, InscriptionId},
object::Object,
options::Options,
Expand Down
Loading

0 comments on commit 84d5fbe

Please sign in to comment.