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 9e38296 commit a7ab5df
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 88 deletions.
4 changes: 4 additions & 0 deletions src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ pub enum Arguments {
Range {
height: u64,
},
Traits {
n: u64,
},
}

impl Arguments {
Expand All @@ -22,6 +25,7 @@ impl Arguments {
height,
} => crate::find::run(&blocksdir, n, height),
Self::Range { height } => crate::range::run(height),
Self::Traits { n } => crate::traits::run(n),
}
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use {
mod arguments;
mod find;
mod range;
mod traits;

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

Expand Down
8 changes: 8 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use super::*;

pub(crate) fn run(n: u64) -> Result {
if n == 0 {
println!("divine");
}
Ok(())
}
60 changes: 8 additions & 52 deletions tests/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,16 @@ use super::*;

#[test]
fn first_satoshi() -> Result {
let tmpdir = tempfile::tempdir()?;
populate_blockfile(File::create(tmpdir.path().join("blk00000.dat"))?, 0)?;
let output = Command::new(executable_path("sat-tracker"))
.args([
"find",
"--blocksdir",
tmpdir.path().to_str().unwrap(),
"0",
"0",
])
.output()?;

if !output.status.success() {
panic!(
"Command failed {}: {}",
output.status,
str::from_utf8(&output.stderr)?
);
}

assert_eq!(
str::from_utf8(&output.stdout)?,
"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:0\n"
);

Ok(())
Test::new()?
.args(&["find", "--blocksdir", "blocks", "0", "0"])
.expected_stdout("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:0\n")
.run()
}

#[test]
fn first_satoshi_of_second_block() -> Result {
let tmpdir = tempfile::tempdir()?;
populate_blockfile(File::create(tmpdir.path().join("blk00000.dat"))?, 1)?;
let output = Command::new(executable_path("sat-tracker"))
.args([
"find",
"--blocksdir",
tmpdir.path().to_str().unwrap(),
"5000000000",
"1",
])
.output()?;

if !output.status.success() {
panic!(
"Command failed {}: {}",
output.status,
str::from_utf8(&output.stderr)?
);
}

assert_eq!(
str::from_utf8(&output.stdout)?,
"e5fb252959bdc7727c80296dbc53e1583121503bb2e266a609ebc49cf2a74c1d:0\n",
);

Ok(())
Test::new()?
.args(&["find", "--blocksdir", "blocks", "5000000000", "1"])
.expected_stdout("e5fb252959bdc7727c80296dbc53e1583121503bb2e266a609ebc49cf2a74c1d:0\n")
.run()
}
61 changes: 59 additions & 2 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,74 @@ use {
executable_path::executable_path,
std::{
error::Error,
fs::File,
fs::{self, File},
io::{self, Seek, SeekFrom, Write},
process::Command,
str,
},
tempfile::TempDir,
};

mod find;
mod range;
mod traits;

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

struct Test {
args: Vec<String>,
expected_stdout: String,
tempdir: TempDir,
}

impl Test {
fn new() -> Result<Self> {
Ok(Self {
args: Vec::new(),
expected_stdout: String::new(),
tempdir: TempDir::new()?,
})
}

fn args(self, args: &[&str]) -> Self {
Self {
args: self
.args
.into_iter()
.chain(args.iter().cloned().map(str::to_owned))
.collect(),
..self
}
}

fn expected_stdout(self, expected_stdout: &str) -> Self {
Self {
expected_stdout: expected_stdout.to_owned(),
..self
}
}

fn run(self) -> Result {
let blocksdir = self.tempdir.path().join("blocks");
fs::create_dir(&blocksdir)?;
populate_blockfile(File::create(blocksdir.join("blk00000.dat"))?, 1)?;

let output = Command::new(executable_path("sat-tracker"))
.current_dir(&self.tempdir)
.args(self.args)
.output()?;

let stderr = str::from_utf8(&output.stderr)?;

if !output.status.success() {
panic!("Test failed: {}\n{}", output.status, stderr);
}

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

Ok(())
}
}

fn generate_transaction(height: usize) -> Transaction {
// Base
Expand Down
42 changes: 8 additions & 34 deletions tests/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,16 @@ use super::*;

#[test]
fn genesis() -> Result {
let tmpdir = tempfile::tempdir()?;
populate_blockfile(File::create(tmpdir.path().join("blk00000.dat"))?, 1)?;
let output = Command::new(executable_path("sat-tracker"))
.args(["range", "0"])
.output()?;

if !output.status.success() {
panic!(
"Command failed {}: {}",
output.status,
str::from_utf8(&output.stderr)?
);
}

assert_eq!(str::from_utf8(&output.stdout)?, "0 5000000000\n",);

Ok(())
Test::new()?
.args(&["range", "0"])
.expected_stdout("0 5000000000\n")
.run()
}

#[test]
fn second_block() -> Result {
let tmpdir = tempfile::tempdir()?;
populate_blockfile(File::create(tmpdir.path().join("blk00000.dat"))?, 1)?;
let output = Command::new(executable_path("sat-tracker"))
.args(["range", "1"])
.output()?;

if !output.status.success() {
panic!(
"Command failed {}: {}",
output.status,
str::from_utf8(&output.stderr)?
);
}

assert_eq!(str::from_utf8(&output.stdout)?, "5000000000 10000000000\n",);

Ok(())
Test::new()?
.args(&["range", "1"])
.expected_stdout("5000000000 10000000000\n")
.run()
}
9 changes: 9 additions & 0 deletions tests/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use super::*;

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

0 comments on commit a7ab5df

Please sign in to comment.