Skip to content

Commit

Permalink
Use sequence numbers as database keys
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Nov 13, 2023
1 parent 19db94a commit 0b32775
Show file tree
Hide file tree
Showing 34 changed files with 753 additions and 911 deletions.
12 changes: 12 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ benchmark-revision rev:
rsync -avz benchmark/checkout [email protected]:benchmark/checkout
ssh [email protected] 'cd benchmark && ./checkout {{rev}}'

benchmark-branch branch:
#/usr/bin/env bash
rm -f master.redb
rm -f {{branch}}.redb
git checkout master
cargo build --release
time ./target/release/ord --index master.redb index update
ll master.redb
git checkout {{branch}}
time ./target/release/ord --index {{branch}}.redb index update
ll {{branch}}.redb

build-snapshots:
#!/usr/bin/env bash
set -euxo pipefail
Expand Down
2 changes: 1 addition & 1 deletion src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Chain {
}
}

pub(crate) fn first_inscription_height(self) -> u64 {
pub(crate) fn first_inscription_height(self) -> u32 {
match self {
Self::Mainnet => 767430,
Self::Regtest => 0,
Expand Down
26 changes: 14 additions & 12 deletions src/degree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use super::*;

#[derive(PartialEq, Debug)]
pub(crate) struct Degree {
pub(crate) hour: u64,
pub(crate) minute: u64,
pub(crate) second: u64,
pub(crate) hour: u32,
pub(crate) minute: u32,
pub(crate) second: u32,
pub(crate) third: u64,
}

Expand Down Expand Up @@ -34,7 +34,7 @@ impl From<Sat> for Degree {
mod tests {
use super::*;

fn case(sat: u64, hour: u64, minute: u64, second: u64, third: u64) {
fn case(sat: u64, hour: u32, minute: u32, second: u32, third: u64) {
assert_eq!(
Degree::from(Sat(sat)),
Degree {
Expand All @@ -52,20 +52,22 @@ mod tests {
case(1, 0, 0, 0, 1);
case(5_000_000_000, 0, 1, 1, 0);
case(
5_000_000_000 * DIFFCHANGE_INTERVAL,
5_000_000_000 * u64::from(DIFFCHANGE_INTERVAL),
0,
DIFFCHANGE_INTERVAL,
0,
0,
);
case(5_000_000_000 * SUBSIDY_HALVING_INTERVAL, 0, 0, 336, 0);
case(
5_000_000_000 * SUBSIDY_HALVING_INTERVAL
+ 2_500_000_000 * SUBSIDY_HALVING_INTERVAL
+ 1_250_000_000 * SUBSIDY_HALVING_INTERVAL
+ 625_000_000 * SUBSIDY_HALVING_INTERVAL
+ 312_500_000 * SUBSIDY_HALVING_INTERVAL
+ 156_250_000 * SUBSIDY_HALVING_INTERVAL,
5_000_000_000 * u64::from(SUBSIDY_HALVING_INTERVAL),
0,
0,
336,
0,
);
case(
(5_000_000_000 + 2_500_000_000 + 1_250_000_000 + 625_000_000 + 312_500_000 + 156_250_000)
* u64::from(SUBSIDY_HALVING_INTERVAL),
1,
0,
0,
Expand Down
24 changes: 15 additions & 9 deletions src/epoch.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;

#[derive(Copy, Clone, Eq, PartialEq, Debug, Display, Serialize, PartialOrd)]
pub(crate) struct Epoch(pub(crate) u64);
pub(crate) struct Epoch(pub(crate) u32);

impl Epoch {
pub(crate) const STARTING_SATS: [Sat; 34] = [
Expand Down Expand Up @@ -61,8 +61,8 @@ impl Epoch {
}
}

impl PartialEq<u64> for Epoch {
fn eq(&self, other: &u64) -> bool {
impl PartialEq<u32> for Epoch {
fn eq(&self, other: &u32) -> bool {
self.0 == *other
}
}
Expand Down Expand Up @@ -156,11 +156,11 @@ mod tests {
assert_eq!(Epoch(0).starting_sat(), 0);
assert_eq!(
Epoch(1).starting_sat(),
Epoch(0).subsidy() * SUBSIDY_HALVING_INTERVAL
Epoch(0).subsidy() * u64::from(SUBSIDY_HALVING_INTERVAL)
);
assert_eq!(
Epoch(2).starting_sat(),
(Epoch(0).subsidy() + Epoch(1).subsidy()) * SUBSIDY_HALVING_INTERVAL
(Epoch(0).subsidy() + Epoch(1).subsidy()) * u64::from(SUBSIDY_HALVING_INTERVAL)
);
assert_eq!(Epoch(33).starting_sat(), Sat(Sat::SUPPLY));
assert_eq!(Epoch(34).starting_sat(), Sat(Sat::SUPPLY));
Expand All @@ -174,7 +174,7 @@ mod tests {

for epoch in 0..34 {
epoch_sats.push(sat);
sat += SUBSIDY_HALVING_INTERVAL * Epoch(epoch).subsidy();
sat += u64::from(SUBSIDY_HALVING_INTERVAL) * Epoch(epoch).subsidy();
}

assert_eq!(Epoch::STARTING_SATS.as_slice(), epoch_sats);
Expand Down Expand Up @@ -209,11 +209,17 @@ mod tests {
if epoch > 0 {
assert_eq!(
Epoch::from(Sat(starting_sat.n() - 1)),
Epoch(epoch as u64 - 1)
Epoch(u32::try_from(epoch).unwrap() - 1)
);
}
assert_eq!(Epoch::from(starting_sat), Epoch(epoch as u64));
assert_eq!(Epoch::from(starting_sat + 1), Epoch(epoch as u64));
assert_eq!(
Epoch::from(starting_sat),
Epoch(u32::try_from(epoch).unwrap())
);
assert_eq!(
Epoch::from(starting_sat + 1),
Epoch(u32::try_from(epoch).unwrap())
);
}
assert_eq!(Epoch::from(Sat(0)), 0);
assert_eq!(Epoch::from(Sat(1)), 0);
Expand Down
28 changes: 14 additions & 14 deletions src/height.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::*;

#[derive(Copy, Clone, Debug, Display, FromStr, Ord, Eq, Serialize, PartialEq, PartialOrd)]
pub(crate) struct Height(pub(crate) u64);
pub(crate) struct Height(pub(crate) u32);

impl Height {
pub(crate) fn n(self) -> u64 {
pub(crate) fn n(self) -> u32 {
self.0
}

Expand All @@ -16,32 +16,32 @@ impl Height {
let epoch = Epoch::from(self);
let epoch_starting_sat = epoch.starting_sat();
let epoch_starting_height = epoch.starting_height();
epoch_starting_sat + (self - epoch_starting_height.n()).n() * epoch.subsidy()
epoch_starting_sat + u64::from(self.n() - epoch_starting_height.n()) * epoch.subsidy()
}

pub(crate) fn period_offset(self) -> u64 {
pub(crate) fn period_offset(self) -> u32 {
self.0 % DIFFCHANGE_INTERVAL
}
}

impl Add<u64> for Height {
impl Add<u32> for Height {
type Output = Self;

fn add(self, other: u64) -> Height {
fn add(self, other: u32) -> Height {
Self(self.0 + other)
}
}

impl Sub<u64> for Height {
impl Sub<u32> for Height {
type Output = Self;

fn sub(self, other: u64) -> Height {
fn sub(self, other: u32) -> Height {
Self(self.0 - other)
}
}

impl PartialEq<u64> for Height {
fn eq(&self, other: &u64) -> bool {
impl PartialEq<u32> for Height {
fn eq(&self, other: &u32) -> bool {
self.0 == *other
}
}
Expand Down Expand Up @@ -95,18 +95,18 @@ mod tests {
assert_eq!(Height(1).starting_sat(), 5000000000);
assert_eq!(
Height(SUBSIDY_HALVING_INTERVAL - 1).starting_sat(),
(SUBSIDY_HALVING_INTERVAL - 1) * 5000000000
(u64::from(SUBSIDY_HALVING_INTERVAL) - 1) * 5000000000
);
assert_eq!(
Height(SUBSIDY_HALVING_INTERVAL).starting_sat(),
SUBSIDY_HALVING_INTERVAL * 5000000000
u64::from(SUBSIDY_HALVING_INTERVAL) * 5000000000
);
assert_eq!(
Height(SUBSIDY_HALVING_INTERVAL + 1).starting_sat(),
SUBSIDY_HALVING_INTERVAL * 5000000000 + 2500000000
u64::from(SUBSIDY_HALVING_INTERVAL) * 5000000000 + 2500000000
);
assert_eq!(
Height(u64::max_value()).starting_sat(),
Height(u32::max_value()).starting_sat(),
*Epoch::STARTING_SATS.last().unwrap()
);
}
Expand Down
Loading

0 comments on commit 0b32775

Please sign in to comment.