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

Flow #4023

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft

Flow #4023

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
4 changes: 4 additions & 0 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod decode;
pub mod env;
pub mod epochs;
pub mod find;
mod flow;
pub mod index;
pub mod list;
pub mod parse;
Expand All @@ -31,6 +32,8 @@ pub(crate) enum Subcommand {
Epochs,
#[command(about = "Find a satoshi's current location")]
Find(find::Find),
#[command(about = "Display PSBT sat flow")]
Flow(flow::Flow),
#[command(subcommand, about = "Index commands")]
Index(index::IndexSubcommand),
#[command(about = "List the satoshis in an output")]
Expand Down Expand Up @@ -67,6 +70,7 @@ impl Subcommand {
Self::Env(env) => env.run(),
Self::Epochs => epochs::run(),
Self::Find(find) => find.run(settings),
Self::Flow(flow) => flow.run(settings),
Self::Index(index) => index.run(settings),
Self::List(list) => list.run(settings),
Self::Parse(parse) => parse.run(),
Expand Down
113 changes: 113 additions & 0 deletions src/subcommand/flow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use {super::*, base64::Engine, bitcoin::psbt::Psbt};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Output {
inputs: Vec<Vec<Range>>,
outputs: Vec<Vec<Range>>,
fee: Vec<Range>,
}

#[derive(Debug, Parser)]
pub(crate) struct Flow {
#[arg(long)]
binary: bool,
psbt: PathBuf,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Range {
start: u64,
end: u64,
name: String,
}

impl From<(u64, u64)> for Range {
fn from((start, end): (u64, u64)) -> Self {
Self {
start,
end,
name: Sat(start).name(),
}
}
}

impl Flow {
pub(crate) fn run(self, settings: Settings) -> SubcommandResult {
let psbt = if self.binary {
fs::read(self.psbt).unwrap()
} else {
base64::engine::general_purpose::STANDARD
.decode(fs::read_to_string(self.psbt).unwrap().trim())
.unwrap()
};

let psbt = Psbt::deserialize(&psbt).unwrap();

let index = Index::open(&settings)?;

index.update()?;

let mut inputs = Vec::new();

for input in psbt.unsigned_tx.input {
inputs.push(index.list(input.previous_output)?.unwrap());
}

let mut fee: VecDeque<(u64, u64)> = inputs.iter().flatten().copied().collect();

let input_value = fee.iter().map(|(start, end)| end - start).sum::<u64>();

let output_value = psbt
.unsigned_tx
.output
.iter()
.map(|input| input.value)
.sum::<u64>();

ensure!(
input_value >= output_value,
"insufficient inputs to pay for outputs: {input_value} < {output_value}",
);

let mut outputs = Vec::new();

for output in psbt.unsigned_tx.output {
let mut ranges = Vec::new();

let mut deficit = output.value;

while deficit > 0 {
let (start, end) = fee
.pop_front()
.context("inputs insufficient to pay for outputs")?;

let size = end - start;

let (start, end) = if size <= deficit {
(start, end)
} else {
fee.push_front((start + deficit, end));
(start, start + deficit)
};

ranges.push((start, end));

deficit -= end - start;
}

outputs.push(ranges);
}

Ok(Some(Box::new(Output {
fee: fee.into_iter().map(|range| range.into()).collect(),
inputs: inputs
.into_iter()
.map(|ranges| ranges.into_iter().map(|range| range.into()).collect())
.collect(),
outputs: outputs
.into_iter()
.map(|ranges| ranges.into_iter().map(|range| range.into()).collect())
.collect(),
})))
}
}
Loading