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

Track atom locations #2

Merged
merged 3 commits into from
Dec 12, 2021
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ license = "CC0-1.0"
edition = "2021"

[dependencies]
bitcoin = "0.27.1"
bitcoincore-rpc = "0.14.0"
dirs = "4.0.0"
72 changes: 69 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use bitcoincore_rpc::{Auth, Client, RpcApi};
use {
bitcoin::blockdata::transaction::OutPoint,
bitcoincore_rpc::{Auth, Client, RpcApi},
std::collections::BTreeMap,
};

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

Expand All @@ -19,8 +23,70 @@ fn main() -> Result<()> {

let client = Client::new("http://localhost:8332", Auth::CookieFile(cookiefile))?;

let best_block_hash = client.get_best_block_hash().unwrap();
println!("best block hash: {}", best_block_hash);
let tip_height = client
.get_block_header_info(&client.get_best_block_hash()?)?
.height as u64;

eprintln!("Scanning for atoms up to height {}…", tip_height);

let mut atoms: BTreeMap<OutPoint, u64> = BTreeMap::new();

for height in 0..tip_height {
eprintln!("Scanning for atoms in block {}…", height);
let hash = client.get_block_hash(height)?;

let block = client.get_block(&hash)?;

for (i, transaction) in block.txdata.iter().enumerate() {
let txid = transaction.txid();
if i == 0 {
atoms.insert(OutPoint { txid, vout: 0 }, height);
} else {
let mut transferred = transaction
.input
.iter()
.map(|txin| atoms.remove_entry(&txin.previous_output))
.flatten()
.collect::<Vec<(OutPoint, u64)>>();

if transferred.is_empty() {
continue;
}

eprintln!(
"Transferring {} atoms: {:?}",
transferred.len(),
transferred,
);

let total = transaction
.output
.iter()
.map(|txout| txout.value)
.sum::<u64>();

let mut pending = 0;
for (vout, output) in transaction.output.iter().enumerate() {
let vout = vout as u32;

pending += output.value * transferred.len() as u64;

while pending >= total {
let (old_outpoint, atom) = transferred.remove(0);
let new_outpoint = OutPoint { vout, txid };
eprintln!(
"Transferring atom {} from {} to {}",
atom, old_outpoint, new_outpoint
);
atoms.insert(new_outpoint, atom);
pending -= total;
}
}

assert!(transferred.is_empty());
}
}
}

Ok(())
}