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

Make find command follow subsequent transactions #60

Merged
merged 26 commits into from
Jan 28, 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
20 changes: 10 additions & 10 deletions Cargo.lock

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

37 changes: 12 additions & 25 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,33 @@ use super::*;

mod epochs;
mod find;
mod list;
mod name;
mod range;
mod supply;
mod traits;

#[derive(StructOpt)]
pub(crate) enum Command {
Epochs,
Find {
#[structopt(long)]
blocksdir: Option<PathBuf>,
ordinal: Ordinal,
height: u64,
},
Name {
name: String,
},
Range {
#[structopt(long)]
name: bool,
height: Height,
},
Find(find::Find),
Name(name::Name),
List(list::List),
Range(range::Range),
Supply,
Traits {
ordinal: Ordinal,
},
Traits(traits::Traits),
}

impl Command {
pub(crate) fn run(self) -> Result<()> {
match self {
Self::Epochs => epochs::run(),
Self::Find {
blocksdir,
ordinal,
height,
} => find::run(blocksdir.as_deref(), ordinal, height),
Self::Name { name } => name::run(&name),
Self::Range { height, name } => range::run(height, name),
Self::Find(find) => find.run(),
Self::Name(name) => name.run(),
Self::List(list) => list.run(),
Self::Range(range) => range.run(),
Self::Supply => supply::run(),
Self::Traits { ordinal } => traits::run(ordinal),
Self::Traits(traits) => traits.run(),
}
}
}
55 changes: 41 additions & 14 deletions src/command/find.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,49 @@
use super::*;

pub(crate) fn run(blocksdir: Option<&Path>, ordinal: Ordinal, at_height: u64) -> Result<()> {
let index = Index::new(blocksdir)?;
#[derive(StructOpt)]
pub(crate) struct Find {
#[structopt(long)]
blocksdir: Option<PathBuf>,
#[structopt(long)]
as_of_height: u64,
#[structopt(long)]
slot: bool,
ordinal: Ordinal,
}

impl Find {
pub(crate) fn run(self) -> Result<()> {
let index = Index::new(self.blocksdir.as_deref())?;

let height = ordinal.height().n();
assert!(height < 100);
assert!(height == at_height);
let creation_height = self.ordinal.height().n();
let block = index.block(creation_height)?.unwrap();

let block = index.block(height)?;
let offset = self.ordinal.subsidy_position();
let mut satpoint = SatPoint::from_transaction_and_offset(&block.txdata[0], offset);
let mut slot = (creation_height, 0, satpoint.outpoint.vout, offset);

let mut offset = ordinal.subsidy_position();
for (index, output) in block.txdata[0].output.iter().enumerate() {
if output.value > offset {
println!("{}:{index}:{offset}", block.txdata[0].txid());
break;
for height in (creation_height + 1)..(self.as_of_height + 1) {
match index.block(height)? {
Some(block) => {
for (txindex, transaction) in block.txdata.iter().enumerate() {
for input in &transaction.input {
if input.previous_output == satpoint.outpoint {
satpoint = SatPoint::from_transaction_and_offset(transaction, satpoint.offset);
slot = (height, txindex, satpoint.outpoint.vout, satpoint.offset);
}
}
}
}
None => break,
}
}
offset -= output.value;
}

Ok(())
if self.slot {
println!("{}.{}.{}.{}", slot.0, slot.1, slot.2, slot.3);
} else {
println!("{satpoint}");
}

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

#[derive(StructOpt)]
pub(crate) struct List {
#[structopt(long)]
blocksdir: Option<PathBuf>,
outpoint: OutPoint,
}

impl List {
pub(crate) fn run(self) -> Result<()> {
let index = Index::new(self.blocksdir.as_deref())?;
let ranges = index.list(self.outpoint)?;

for (start, end) in ranges {
println!("[{start},{end})");
}

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

#[derive(StructOpt)]
pub(crate) struct Name {
name: String,
}

impl Name {
pub(crate) fn run(self) -> Result {
for c in self.name.chars() {
if !('a'..='z').contains(&c) {
return Err("Invalid name".into());
}
}

let mut min = 0;
let mut max = 2099999997690000;
let mut guess = max / 2;

loop {
log::info!("min max guess: {} {} {}", min, max, guess);

let name = Ordinal::new(guess).name();

match name
.len()
.cmp(&self.name.len())
.then(name.deref().cmp(&self.name))
.reverse()
{
Ordering::Less => min = guess + 1,
Ordering::Equal => break,
Ordering::Greater => max = guess,
}

if max - min == 0 {
return Err("Name out of range".into());
}

guess = min + (max - min) / 2;
}

println!("{}", guess);

Ok(())
}
}
53 changes: 31 additions & 22 deletions src/command/range.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
use super::*;

pub(crate) fn run(height: Height, name_range: bool) -> Result {
let mut start = 0;
#[derive(StructOpt)]
pub(crate) struct Range {
#[structopt(long)]
name: bool,
height: Height,
}

impl Range {
pub(crate) fn run(self) -> Result {
let mut start = 0;

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

if subsidy == 0 {
break;
if subsidy == 0 {
break;
}

start += subsidy;
}

start += subsidy;
}
let end = start + self.height.subsidy();

let end = start + height.subsidy();

if name_range {
let (start, end) = match (Ordinal::new_checked(start), Ordinal::new_checked(end)) {
(Some(start), Some(end)) => (start.name(), end.name()),
(Some(start), None) => (start.name(), start.name()),
(None, None) => (Ordinal::LAST.name(), Ordinal::LAST.name()),
(None, Some(_)) => unreachable!(),
};
println!("[{},{})", start, end);
} else {
println!("[{},{})", start, end);
}
if self.name {
let (start, end) = match (Ordinal::new_checked(start), Ordinal::new_checked(end)) {
(Some(start), Some(end)) => (start.name(), end.name()),
(Some(start), None) => (start.name(), start.name()),
(None, None) => (Ordinal::LAST.name(), Ordinal::LAST.name()),
(None, Some(_)) => unreachable!(),
};
println!("[{},{})", start, end);
} else {
println!("[{},{})", start, end);
}

Ok(())
Ok(())
}
}
Loading