Skip to content

Commit

Permalink
Number satoshis in ascending order (ordinals#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Jan 17, 2022
1 parent e7627e9 commit c3af054
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 71 deletions.
46 changes: 27 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@ Satoshi serial numbers can be used as an addressing scheme for NFTs.

## Numbering

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
Satoshis are numbered in ascending order, starting at 0 in the genesis block,
and ending with 2099999997689999, 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 affect the serial numbers of satoshis in subsequent blocks.
by one satoshi, does not reduce 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
2099999997689999 2099994997689999
[0,5000000000)
```

See [src/range.rs](src/range.rs) for the numbering algorithm.
Expand Down Expand Up @@ -69,33 +70,40 @@ $ sat-tracker find --blocksdir ~/.bicoin/blocks 0 0

Satoshis have traits, based on their number.

NB: Traits should be considered *UNSTABLE*. In particular, the satoshis with
short names will not be available for quite some time, which might be desirable
to fix, and would require an overhaul of the name trait.

The `traits` command prints out the traits of a given satoshi:

```
$ sat-tracker traits 0
zero
genesis
even
square
cube
luck: 0/1
population: 0
name: nvtdijuwxlo
character: '\u{0}'
shiny
block: 0
```

## Names

The name trait is of particular interest, as it can serve as the basis for a
decentralized naming system.
Each satoshi is assigned a name, consisting of lowercase ASCII characters.
Satoshi 0 has name `nvtdijuwxlo`, and names get shorter as the satoshi number
gets larger. This is to ensure that short names aren't locked in the genesis
block output which is unspendable, and other outputs, which are unlikely to
ever be spent.

The `name` command finds the satoshi with the given name:

```
$ sat-tracker name nvtdijuwxlp
$ sat-tracker name nvtdijuwxlo
0
$ sat-tracker name hello
2099999993937872
$ sat-tracker name ''
2099999997689999
```

## Open Questions

- Satoshis are numbered in descending order. Numbering them in ascending order
would be more natural, but it has the unfortunate consequence that satoshis
with short, and thus desirable serial numbers, are stuck in the genesis block
coinbase output, which is unspendable. Using descending order does have the
nice benefit that they serve as legible count-down to the final satoshi to be
mined. However, I'm not totally convinced that this tradeoff is worth the
potential confusion.
4 changes: 3 additions & 1 deletion src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub enum Arguments {
name: String,
},
Range {
#[structopt(long)]
name: bool,
height: u64,
},
Supply,
Expand All @@ -29,7 +31,7 @@ impl Arguments {
height,
} => crate::find::run(blocksdir, n, height),
Self::Name { name } => crate::name::run(&name),
Self::Range { height } => crate::range::run(height),
Self::Range { height, name } => crate::range::run(height, name),
Self::Supply => crate::supply::run(),
Self::Traits { n } => crate::traits::run(n),
}
Expand Down
18 changes: 10 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,17 @@ fn subsidy(height: u64) -> u64 {
}
}

fn name(mut n: u64) -> String {
fn name(n: u64) -> String {
let mut x = SUPPLY - n - 1;
let mut name = String::new();
while n > 0 {
while x > 0 {
name.push(
"abcdefghijklmnopqrstuvwxyz"
.chars()
.nth(((n - 1) % 26) as usize)
.nth(((x - 1) % 26) as usize)
.unwrap(),
);
n = (n - 1) / 26;
x = (x - 1) / 26;
}
name.chars().rev().collect()
}
Expand Down Expand Up @@ -85,10 +86,11 @@ mod tests {

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

#[test]
Expand Down
7 changes: 6 additions & 1 deletion src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ pub(crate) fn run(needle: &str) -> Result {

let name = name(guess);

match name.len().cmp(&needle.len()).then(name.deref().cmp(needle)) {
match name
.len()
.cmp(&needle.len())
.then(name.deref().cmp(needle))
.reverse()
{
Ordering::Less => min = guess + 1,
Ordering::Equal => break,
Ordering::Greater => max = guess,
Expand Down
12 changes: 8 additions & 4 deletions src/range.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
use super::*;

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

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

start -= subsidy(i) as i64;
start += subsidy(i);
}

println!("{} {}", start, start as i64 - subsidy(height) as i64);
if name_range {
println!("[{},{})", name(start), name(start + subsidy(height)));
} else {
println!("[{},{})", start, start + subsidy(height));
}

Ok(())
}
5 changes: 3 additions & 2 deletions src/supply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ pub(crate) fn run() -> Result {
}

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

Ok(())
}
10 changes: 5 additions & 5 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,25 @@ pub(crate) fn run(n: u64) -> Result {
}

let mut block = 0;
let mut remaining = SUPPLY;
let mut mined = 0;
loop {
if n == remaining - 1 {
if n == mined {
println!("shiny");
}

let subsidy = subsidy(block);

remaining -= subsidy;
mined += subsidy;

if remaining <= n {
if mined > n {
println!("block: {}", block);
break;
}

block += 1;
}

if n == 1476369997690000 {
if n == 623624999999999 {
println!("illusive");
} else if block == 124724 {
println!("cursed");
Expand Down
8 changes: 4 additions & 4 deletions tests/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@ use super::*;
fn empty() -> Result {
Test::new()?
.args(&["name", ""])
.expected_stdout("0\n")
.expected_stdout("2099999997689999\n")
.run()
}

#[test]
fn a() -> Result {
Test::new()?
.args(&["name", "a"])
.expected_stdout("1\n")
.expected_stdout("2099999997689998\n")
.run()
}

#[test]
fn b() -> Result {
Test::new()?
.args(&["name", "b"])
.expected_stdout("2\n")
.expected_stdout("2099999997689997\n")
.run()
}

#[test]
fn end_of_range() -> Result {
Test::new()?
.args(&["name", "nvtdijuwxlo"])
.expected_stdout("2099999997689999\n")
.expected_stdout("0\n")
.run()
}

Expand Down
16 changes: 12 additions & 4 deletions tests/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,38 @@ use super::*;
fn genesis() -> Result {
Test::new()?
.args(&["range", "0"])
.expected_stdout("2099999997689999 2099994997689999\n")
.expected_stdout("[0,5000000000)\n")
.run()
}

#[test]
fn second_block() -> Result {
Test::new()?
.args(&["range", "1"])
.expected_stdout("2099994997689999 2099989997689999\n")
.expected_stdout("[5000000000,10000000000)\n")
.run()
}

#[test]
fn last_block_with_subsidy() -> Result {
Test::new()?
.args(&["range", "6929999"])
.expected_stdout("0 -1\n")
.expected_stdout("[2099999997689999,2099999997690000)\n")
.run()
}

#[test]
fn first_block_without_subsidy() -> Result {
Test::new()?
.args(&["range", "6930000"])
.expected_stdout("-1 -1\n")
.expected_stdout("[2099999997690000,2099999997690000)\n")
.run()
}

#[test]
fn genesis_names() -> Result {
Test::new()?
.args(&["range", "--name", "0"])
.expected_stdout("[nvtdijuwxlo,nvtcsezkbtg)\n")
.run()
}
5 changes: 3 additions & 2 deletions tests/supply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ fn genesis() -> Result {
.expected_stdout(
&"
supply: 2099999997690000
first: 2099999997689999
last subsidy block: 6929999
first: 0
last: 2099999997689999
last mined in block: 6929999
"
.unindent(),
)
Expand Down
44 changes: 23 additions & 21 deletions tests/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,24 @@ fn divine() -> Result {

#[test]
fn name() -> Result {
assert!(traits(0)?.contains("name: "));
assert!(traits(1)?.contains("name: a"));
assert!(traits(26)?.contains("name: z"));
assert!(traits(27)?.contains("name: aa"));
assert!(traits(2099999997689999)?.contains("name: "));
assert!(traits(2099999997689999 - 1)?.contains("name: a"));
assert!(traits(2099999997689999 - 26)?.contains("name: z"));
assert!(traits(2099999997689999 - 27)?.contains("name: aa"));
assert!(traits(0)?.contains("name: nvtdijuwxlo"));
assert!(traits(1)?.contains("name: nvtdijuwxln"));
assert!(traits(26)?.contains("name: nvtdijuwxko"));
assert!(traits(27)?.contains("name: nvtdijuwxkn"));
Ok(())
}

#[test]
fn block() -> Result {
assert!(traits(2099999997689999)?.contains("block: 0"));
assert!(traits(2099999997689998)?.contains("block: 0"));
assert!(traits(2099999997689999 - 50 * 100_000_000)?.contains("block: 1"));
assert!(traits(0)?.contains("block: 6929999"));
assert!(traits(1)?.contains("block: 6929998"));
assert!(traits(0)?.contains("block: 0"));
assert!(traits(1)?.contains("block: 0"));
assert!(traits(50 * 100_000_000)?.contains("block: 1"));
assert!(traits(2099999997689999)?.contains("block: 6929999"));
assert!(traits(2099999997689998)?.contains("block: 6929998"));
Ok(())
}

Expand All @@ -92,11 +96,11 @@ fn lucky() -> Result {
#[test]
fn shiny() -> Result {
assert!(traits(0)?.contains("shiny"));
assert!(traits(1)?.contains("shiny"));
assert!(!traits(1)?.contains("shiny"));
assert!(traits(2099999997689999)?.contains("shiny"));
assert!(!traits(2099999997689998)?.contains("shiny"));
assert!(traits(2099999997689999 - 50 * 100_000_000)?.contains("shiny"));
assert!(!traits(2099999997689999 - 50 * 100_000_000 - 1)?.contains("shiny"));
assert!(traits(2099999997689998)?.contains("shiny"));
assert!(traits(50 * 100_000_000)?.contains("shiny"));
assert!(!traits(50 * 100_000_000 + 1)?.contains("shiny"));
Ok(())
}

Expand Down Expand Up @@ -145,18 +149,16 @@ fn character() -> Result {
#[test]
fn cursed() -> Result {
assert!(!traits(0)?.contains("cursed"));
assert!(!traits(1476379997690000)?.contains("cursed"));
assert!(traits(1476379997689999)?.contains("cursed"));
assert!(traits(1476379997689999 - 50 * 100_000_000 + 1)?.contains("cursed"));
assert!(!traits(1476379997689999 - 50 * 100_000_000)?.contains("cursed"));
assert!(!traits(1476374997689999 - 50 * 100_000_000 + 1)?.contains("cursed"));
assert!(!traits(623624999999999)?.contains("cursed"));
assert!(traits(623624999999999 - 1)?.contains("cursed"));
assert!(!traits(623624999999999 + 1)?.contains("cursed"));
Ok(())
}

#[test]
fn illusive() -> Result {
assert!(!traits(1476374997689999 - 50 * 100_000_000 + 2)?.contains("illusive"));
assert!(traits(1476374997689999 - 50 * 100_000_000 + 1)?.contains("illusive"));
assert!(!traits(1476374997689999 - 50 * 100_000_000 - 1)?.contains("illusive"));
assert!(!traits(1476374997689999 - 1)?.contains("illusive"));
assert!(traits(623624999999999)?.contains("illusive"));
assert!(!traits(623624999999999 + 1)?.contains("illusive"));
Ok(())
}

0 comments on commit c3af054

Please sign in to comment.