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

Use descending numbering scheme #33

Merged
merged 2 commits into from
Jan 14, 2022
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
42 changes: 42 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ autotests = false
bitcoin = "0.27.1"
env_logger = "0.9.0"
executable-path = "1.0.0"
integer-cbrt = "0.1.2"
integer-sqrt = "0.1.5"
log = "0.4.14"
redb = "0.0.3"
structopt = "0.3.25"
tempfile = "3.2.0"
unindent = "0.1.7"

[[test]]
name = "integration"
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@ Satoshi serial numbers can be used as an addressing scheme for NFTs.

## Numbering

Satoshis are numbered in the order in which they are mined.
Satoshis are numbered in descending order, starting at 2099999997689999 in the
genesis block. Satoshi 0 will be mined in block 6929999, the last block with a
subsidy.

Satoshi numbers only depend on how many satoshis could have been created in
previous blocks, not how many were *actually* created.

In particular, this means that block 124724, which underpaid the block subsidy
by one, does not reduce the serial numbers of satoshis in subsequent blocks by
one.
by one, does not affect the serial numbers of satoshis in subsequent blocks.

The `range` command gives the half-open range of satoshis mined in the block at
a given height:

```
$ sat-tracker range 0
0 50000000000
2099999997689999 2099994997689999
```

See [src/range.rs](src/range.rs) for the numbering algorithm.
Expand Down Expand Up @@ -86,5 +87,5 @@ The `name` command finds the satoshi with the given name:

```
$ sat-tracker name nvtdijuwxlp
2099999997690000
2099999997689999
```
2 changes: 2 additions & 0 deletions src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub enum Arguments {
Range {
height: u64,
},
Supply,
Traits {
n: u64,
},
Expand All @@ -29,6 +30,7 @@ impl Arguments {
} => crate::find::run(&blocksdir, n, height),
Self::Name { name } => crate::name::run(&name),
Self::Range { height } => crate::range::run(height),
Self::Supply => crate::supply::run(),
Self::Traits { n } => crate::traits::run(n),
}
}
Expand Down
70 changes: 65 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use {
consensus::Decodable,
Block, Network,
},
integer_cbrt::IntegerCubeRoot,
integer_sqrt::IntegerSquareRoot,
redb::{
Database, MultimapTable, ReadOnlyMultimapTable, ReadOnlyTable, ReadableMultimapTable,
ReadableTable, Table,
Expand All @@ -23,10 +25,22 @@ mod arguments;
mod find;
mod name;
mod range;
mod supply;
mod traits;

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

fn main() {
env_logger::init();

if let Err(error) = Arguments::from_args().run() {
eprintln!("error: {}", error);
process::exit(1);
}
}

const SUPPLY: u64 = 2099999997690000;

fn subsidy(height: u64) -> u64 {
let subsidy = 50 * COIN_VALUE;

Expand All @@ -53,11 +67,57 @@ fn name(mut n: u64) -> String {
name.chars().rev().collect()
}

fn main() {
env_logger::init();
fn pop(mut n: u64) -> u64 {
let mut pop = 0;
while n > 0 {
pop += n & 1;
n >>= 1;
}
pop
}

if let Err(error) = Arguments::from_args().run() {
eprintln!("error: {}", error);
process::exit(1);
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn subsidies() {
assert_eq!(subsidy(0), 5000000000);
assert_eq!(subsidy(1), 5000000000);
assert_eq!(subsidy(210000 - 1), 5000000000);
assert_eq!(subsidy(210000), 2500000000);
assert_eq!(subsidy(210000 + 1), 2500000000);
}

#[test]
fn names() {
assert_eq!(name(0), "");
assert_eq!(name(1), "a");
assert_eq!(name(26), "z");
assert_eq!(name(27), "aa");
}

#[test]
fn supply() {
let mut mined = 0;

for height in 0.. {
let subsidy = subsidy(height);

if subsidy == 0 {
break;
}

mined += subsidy;
}

assert_eq!(SUPPLY, mined);
}

#[test]
fn pops() {
assert_eq!(pop(0), 0);
assert_eq!(pop(1), 1);
assert_eq!(pop(u64::max_value()), 64);
}
}
10 changes: 7 additions & 3 deletions src/range.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use super::*;

pub(crate) fn run(height: u64) -> Result {
let mut mined = 0;
let mut start = SUPPLY as i64 - 1;

for i in 0..height {
mined += subsidy(i);
if subsidy(i) == 0 {
break;
}

start -= subsidy(i) as i64;
}

println!("{} {}", mined, mined + subsidy(height));
println!("{} {}", start, start as i64 - subsidy(height) as i64);

Ok(())
}
18 changes: 18 additions & 0 deletions src/supply.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use super::*;

pub(crate) fn run() -> Result {
let mut last = 0;

loop {
if subsidy(last + 1) == 0 {
break;
}
last += 1;
}

println!("supply: {}", SUPPLY);
println!("first: {}", SUPPLY - 1);
println!("last subsidy block: {}", last);

Ok(())
}
49 changes: 7 additions & 42 deletions src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,5 @@
use super::*;

fn isqrt(x: u64) -> u64 {
let mut a = 1;
let mut b = ((x >> 5) + 8).min(65536);
loop {
let m = (a + b) >> 1;
if m * m > x {
b = m - 1;
} else {
a = m + 1;
}

if b < a {
break;
}
}
a - 1
}

fn icbrt(mut x: u64) -> u64 {
let mut y = 0;
let mut s = 30;
while s >= 0 {
y *= 2;
let b = 3 * y * (y + 1) + 1;
let bs = b << s;
if x >= bs && b == (bs >> s) {
x -= b;
y += 1
}
s -= 3;
}
y
}

pub(crate) fn run(n: u64) -> Result {
if n < subsidy(0) {
println!("genesis");
Expand All @@ -45,11 +11,13 @@ pub(crate) fn run(n: u64) -> Result {
println!("odd");
}

if isqrt(n) * isqrt(n) == n {
let isqrt = n.integer_sqrt();
if isqrt * isqrt == n {
println!("square");
}

if icbrt(n) * icbrt(n) * icbrt(n) == n {
let icbrt = n.integer_cbrt();
if icbrt * icbrt * icbrt == n {
println!("cube");
}

Expand All @@ -71,15 +39,12 @@ pub(crate) fn run(n: u64) -> Result {

println!(
"luck:{}/{}",
digits.iter().filter(|c| **c == '8').count(),
digits.iter().filter(|c| **c == '8').count() as i64
- digits.iter().filter(|c| **c == '4').count() as i64,
digits.len()
);

println!(
"population:{}",
(n.wrapping_mul(0x0002000400080010) & 0x1111111111111111).wrapping_mul(0x1111111111111111)
>> 60
);
println!("population:{}", pop(n),);

println!("name:{}", name(n));

Expand Down
2 changes: 2 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ use {
str,
},
tempfile::TempDir,
unindent::Unindent,
};

mod find;
mod name;
mod range;
mod supply;
mod traits;

type Result<T = ()> = std::result::Result<T, Box<dyn Error>>;
Expand Down
Loading