Skip to content

Commit

Permalink
Add traits command
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Jan 10, 2022
1 parent a7ab5df commit d1c45fc
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ tempfile = "3.2.0"

[[test]]
name = "integration"
path = "tests/lib.rs"
path = "tests/integration.rs"
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,24 @@ fn transfer(transaction: Transaction) {
}
```

The `find` command, unfinished, gives the current outpoint containing a given
satoshi as of a given height:
The `find` command, as of yet unfinished, gives the current outpoint containing
a given satoshi as of a given height:

```
$ sat-tracker find --blocksdir ~/.bicoin/blocks 0 0
4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:0
```


## Traits

Satoshis have traits, based on their number.

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

```
$ sat-tracker traits 0
zero
genesis
even
```
12 changes: 12 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ mod traits;

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

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

let halvings = height / 210000;

if halvings < 64 {
subsidy >> halvings
} else {
0
}
}

fn main() -> Result {
Arguments::from_args().run()
}
12 changes: 0 additions & 12 deletions src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,3 @@ pub(crate) fn run(height: u64) -> Result {

Ok(())
}

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

let halvings = height / 210000;

if halvings < 64 {
subsidy >> halvings
} else {
0
}
}
13 changes: 12 additions & 1 deletion src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@ use super::*;

pub(crate) fn run(n: u64) -> Result {
if n == 0 {
println!("divine");
println!("zero");
}

if n < subsidy(0) {
println!("genesis");
}

if n % 2 == 0 {
println!("even");
} else {
println!("odd");
}

Ok(())
}
22 changes: 20 additions & 2 deletions tests/lib.rs → tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use {
},
executable_path::executable_path,
std::{
collections::BTreeSet,
error::Error,
fs::{self, File},
io::{self, Seek, SeekFrom, Write},
Expand All @@ -26,6 +27,7 @@ type Result<T = ()> = std::result::Result<T, Box<dyn Error>>;
struct Test {
args: Vec<String>,
expected_stdout: String,
ignore_stdout: bool,
tempdir: TempDir,
}

Expand All @@ -34,6 +36,7 @@ impl Test {
Ok(Self {
args: Vec::new(),
expected_stdout: String::new(),
ignore_stdout: false,
tempdir: TempDir::new()?,
})
}
Expand All @@ -56,7 +59,18 @@ impl Test {
}
}

fn ignore_stdout(self) -> Self {
Self {
ignore_stdout: true,
..self
}
}

fn run(self) -> Result {
self.run_with_stdout().map(|_| ())
}

fn run_with_stdout(self) -> Result<String> {
let blocksdir = self.tempdir.path().join("blocks");
fs::create_dir(&blocksdir)?;
populate_blockfile(File::create(blocksdir.join("blk00000.dat"))?, 1)?;
Expand All @@ -72,9 +86,13 @@ impl Test {
panic!("Test failed: {}\n{}", output.status, stderr);
}

assert_eq!(str::from_utf8(&output.stdout)?, self.expected_stdout);
let stdout = str::from_utf8(&output.stdout)?;

if !self.ignore_stdout {
assert_eq!(stdout, self.expected_stdout);
}

Ok(())
Ok(stdout.to_owned())
}
}

Expand Down
43 changes: 39 additions & 4 deletions tests/traits.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,44 @@
use super::*;

fn traits(n: u64) -> Result<BTreeSet<String>> {
Ok(
Test::new()?
.args(&["traits", &n.to_string()])
.ignore_stdout()
.run_with_stdout()?
.split_whitespace()
.map(str::to_owned)
.collect(),
)
}

#[test]
fn zero() -> Result {
Test::new()?
.args(&["traits", "0"])
.expected_stdout("divine\n")
.run()
assert!(traits(0)?.contains("zero"));
assert!(!traits(1)?.contains("zero"));
Ok(())
}

#[test]
fn genesis() -> Result {
assert!(traits(0)?.contains("genesis"));
assert!(traits(50 * COIN_VALUE - 1)?.contains("genesis"));
assert!(!traits(50 * COIN_VALUE)?.contains("genesis"));
Ok(())
}

#[test]
fn even() -> Result {
assert!(traits(0)?.contains("even"));
assert!(!traits(1)?.contains("even"));
assert!(traits(2)?.contains("even"));
Ok(())
}

#[test]
fn odd() -> Result {
assert!(!traits(0)?.contains("odd"));
assert!(traits(1)?.contains("odd"));
assert!(!traits(2)?.contains("odd"));
Ok(())
}

0 comments on commit d1c45fc

Please sign in to comment.