Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into server-integration-…
Browse files Browse the repository at this point in the history
…test
  • Loading branch information
casey committed Sep 30, 2022
2 parents ac1de01 + f3921c3 commit c6e114a
Show file tree
Hide file tree
Showing 15 changed files with 215 additions and 180 deletions.
154 changes: 75 additions & 79 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ members = [".", "test-bitcoincore-rpc"]
anyhow = { version = "1.0.56", features = ["backtrace"] }
axum = "0.5.6"
axum-server = "0.4.0"
bitcoin = "0.28.1"
bitcoincore-rpc = "0.15.0"
bitcoin = "0.29.1"
bitcoincore-rpc = "0.16.0"
boilerplate = { version = "0.2.0", features = ["axum"] }
chrono = "0.4.19"
clap = { version = "3.1.0", features = ["derive"] }
Expand Down
4 changes: 4 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ publish:
cd ../..
rm -rf tmp/release

list-outdated-dependencies:
cargo outdated -R
cd test-bitcoincore-rpc && cargo outdated -R

update-modern-normalize:
curl \
https://raw.githubusercontent.com/sindresorhus/modern-normalize/main/modern-normalize.css \
Expand Down
28 changes: 17 additions & 11 deletions src/degree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ impl From<Ordinal> for Degree {
fn from(ordinal: Ordinal) -> Self {
let height = ordinal.height().n();
Degree {
hour: height / (CYCLE_EPOCHS * Epoch::BLOCKS),
minute: height % Epoch::BLOCKS,
second: height % PERIOD_BLOCKS,
hour: height / (CYCLE_EPOCHS * SUBSIDY_HALVING_INTERVAL),
minute: height % SUBSIDY_HALVING_INTERVAL,
second: height % DIFFCHANGE_INTERVAL,
third: ordinal.third(),
}
}
Expand All @@ -51,15 +51,21 @@ mod tests {
case(0, 0, 0, 0, 0);
case(1, 0, 0, 0, 1);
case(5_000_000_000, 0, 1, 1, 0);
case(5_000_000_000 * 2016, 0, 2016, 0, 0);
case(5_000_000_000 * 210_000, 0, 0, 336, 0);
case(
5_000_000_000 * 210_000
+ 2_500_000_000 * 210_000
+ 1_250_000_000 * 210_000
+ 625_000_000 * 210_000
+ 312_500_000 * 210_000
+ 156_250_000 * 210_000,
5_000_000_000 * 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,
1,
0,
0,
Expand Down
25 changes: 9 additions & 16 deletions src/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ impl Epoch {
Ordinal(Ordinal::SUPPLY),
];
pub(crate) const FIRST_POST_SUBSIDY: Epoch = Self(33);
pub(crate) const BLOCKS: u64 = 210000;

pub(crate) fn subsidy(self) -> u64 {
if self < Self::FIRST_POST_SUBSIDY {
Expand All @@ -58,7 +57,7 @@ impl Epoch {
}

pub(crate) fn starting_height(self) -> Height {
Height(self.0 * Self::BLOCKS)
Height(self.0 * SUBSIDY_HALVING_INTERVAL)
}
}

Expand All @@ -79,7 +78,7 @@ impl From<Ordinal> for Epoch {

impl From<Height> for Epoch {
fn from(height: Height) -> Self {
Self(height.0 / Self::BLOCKS)
Self(height.0 / SUBSIDY_HALVING_INTERVAL)
}
}

Expand All @@ -92,11 +91,11 @@ mod tests {
assert_eq!(Epoch(0).starting_ordinal(), 0);
assert_eq!(
Epoch(1).starting_ordinal(),
Epoch(0).subsidy() * Epoch::BLOCKS
Epoch(0).subsidy() * SUBSIDY_HALVING_INTERVAL
);
assert_eq!(
Epoch(2).starting_ordinal(),
(Epoch(0).subsidy() + Epoch(1).subsidy()) * Epoch::BLOCKS
(Epoch(0).subsidy() + Epoch(1).subsidy()) * SUBSIDY_HALVING_INTERVAL
);
assert_eq!(Epoch(33).starting_ordinal(), Ordinal(Ordinal::SUPPLY));
assert_eq!(Epoch(34).starting_ordinal(), Ordinal(Ordinal::SUPPLY));
Expand All @@ -110,7 +109,7 @@ mod tests {

for epoch in 0..34 {
epoch_ordinals.push(ordinal);
ordinal += Epoch::BLOCKS * Epoch(epoch).subsidy();
ordinal += SUBSIDY_HALVING_INTERVAL * Epoch(epoch).subsidy();
}

assert_eq!(Epoch::STARTING_ORDINALS, epoch_ordinals);
Expand All @@ -125,24 +124,18 @@ mod tests {
assert_eq!(Epoch(33).subsidy(), 0);
}

#[test]
fn blocks() {
// c.f. https://github.com/bitcoin/bitcoin/blob/master/src/chainparams.cpp
assert_eq!(Epoch::BLOCKS, 210000);
}

#[test]
fn starting_height() {
assert_eq!(Epoch(0).starting_height(), 0);
assert_eq!(Epoch(1).starting_height(), Epoch::BLOCKS);
assert_eq!(Epoch(2).starting_height(), Epoch::BLOCKS * 2);
assert_eq!(Epoch(1).starting_height(), SUBSIDY_HALVING_INTERVAL);
assert_eq!(Epoch(2).starting_height(), SUBSIDY_HALVING_INTERVAL * 2);
}

#[test]
fn from_height() {
assert_eq!(Epoch::from(Height(0)), 0);
assert_eq!(Epoch::from(Height(Epoch::BLOCKS)), 1);
assert_eq!(Epoch::from(Height(Epoch::BLOCKS) + 1), 1);
assert_eq!(Epoch::from(Height(SUBSIDY_HALVING_INTERVAL)), 1);
assert_eq!(Epoch::from(Height(SUBSIDY_HALVING_INTERVAL) + 1), 1);
}

#[test]
Expand Down
27 changes: 15 additions & 12 deletions src/height.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Height {
}

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

Expand Down Expand Up @@ -84,23 +84,26 @@ mod tests {
fn subsidy() {
assert_eq!(Height(0).subsidy(), 5000000000);
assert_eq!(Height(1).subsidy(), 5000000000);
assert_eq!(Height(210000 - 1).subsidy(), 5000000000);
assert_eq!(Height(210000).subsidy(), 2500000000);
assert_eq!(Height(210000 + 1).subsidy(), 2500000000);
assert_eq!(Height(SUBSIDY_HALVING_INTERVAL - 1).subsidy(), 5000000000);
assert_eq!(Height(SUBSIDY_HALVING_INTERVAL).subsidy(), 2500000000);
assert_eq!(Height(SUBSIDY_HALVING_INTERVAL + 1).subsidy(), 2500000000);
}

#[test]
fn starting_ordinal() {
assert_eq!(Height(0).starting_ordinal(), 0);
assert_eq!(Height(1).starting_ordinal(), 5000000000);
assert_eq!(
Height(210000 - 1).starting_ordinal(),
(210000 - 1) * 5000000000
Height(SUBSIDY_HALVING_INTERVAL - 1).starting_ordinal(),
(SUBSIDY_HALVING_INTERVAL - 1) * 5000000000
);
assert_eq!(Height(210000).starting_ordinal(), 210000 * 5000000000);
assert_eq!(
Height(210000 + 1).starting_ordinal(),
210000 * 5000000000 + 2500000000
Height(SUBSIDY_HALVING_INTERVAL).starting_ordinal(),
SUBSIDY_HALVING_INTERVAL * 5000000000
);
assert_eq!(
Height(SUBSIDY_HALVING_INTERVAL + 1).starting_ordinal(),
SUBSIDY_HALVING_INTERVAL * 5000000000 + 2500000000
);
assert_eq!(
Height(u64::max_value()).starting_ordinal(),
Expand All @@ -112,8 +115,8 @@ mod tests {
fn period_offset() {
assert_eq!(Height(0).period_offset(), 0);
assert_eq!(Height(1).period_offset(), 1);
assert_eq!(Height(2015).period_offset(), 2015);
assert_eq!(Height(2016).period_offset(), 0);
assert_eq!(Height(2017).period_offset(), 1);
assert_eq!(Height(DIFFCHANGE_INTERVAL - 1).period_offset(), 2015);
assert_eq!(Height(DIFFCHANGE_INTERVAL).period_offset(), 0);
assert_eq!(Height(DIFFCHANGE_INTERVAL + 1).period_offset(), 1);
}
}
6 changes: 3 additions & 3 deletions src/index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
super::*,
bitcoin::consensus::encode::serialize,
bitcoin::consensus::encode::{deserialize, serialize},
bitcoin::BlockHeader,
bitcoincore_rpc::{Auth, Client, RpcApi},
rayon::iter::{IntoParallelRefIterator, ParallelIterator},
Expand Down Expand Up @@ -463,7 +463,7 @@ impl Index {
for chunk in value.chunks_exact(11) {
let (start, end) = Index::decode_ordinal_range(chunk.try_into().unwrap());
if start <= ordinal && ordinal < end {
let outpoint: OutPoint = Decodable::consensus_decode(key.as_slice())?;
let outpoint: OutPoint = deserialize(key.as_slice())?;
return Ok(Some(SatPoint {
outpoint,
offset: offset + ordinal - start,
Expand Down Expand Up @@ -505,7 +505,7 @@ impl Index {
.begin_read()?
.open_table(OUTPOINT_TO_TXID)?
.get(&outpoint_encoded.try_into().unwrap())?
.map(|txid| Txid::consensus_decode(txid.as_slice()))
.map(|txid| deserialize(txid.as_slice()))
.transpose()?
.map(List::Spent),
),
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ mod subcommand;

type Result<T = (), E = Error> = std::result::Result<T, E>;

const PERIOD_BLOCKS: u64 = 2016;
const DIFFCHANGE_INTERVAL: u64 = bitcoin::blockdata::constants::DIFFCHANGE_INTERVAL as u64;
const SUBSIDY_HALVING_INTERVAL: u64 =
bitcoin::blockdata::constants::SUBSIDY_HALVING_INTERVAL as u64;
const CYCLE_EPOCHS: u64 = 6;

static INTERRUPTS: AtomicU64 = AtomicU64::new(0);
Expand Down
Loading

0 comments on commit c6e114a

Please sign in to comment.