-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathverify.rs
73 lines (66 loc) · 2.08 KB
/
verify.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use bitcoin::consensus::{deserialize, serialize};
use bitcoin::{Amount, ScriptBuf, Transaction};
use bitcoinconsensus::height_to_flags;
use blocks_iterator::{BlockExtra, Config};
use clap::Parser;
use env_logger::Env;
use log::{debug, error, info};
use rayon::iter::{ParallelBridge, ParallelIterator};
use std::error::Error;
use std::sync::Arc;
#[derive(Debug)]
struct VerifyData {
script_pubkey: ScriptBuf,
index: usize,
amount: Amount,
spending: Arc<Vec<u8>>,
flags: u32,
}
fn pre_processing(block_extra: BlockExtra) -> Vec<VerifyData> {
let mut vec = vec![];
for tx in block_extra.block().txdata.iter().skip(1) {
let tx_bytes = serialize(tx);
let arc_tx_bytes = Arc::new(tx_bytes);
for (i, input) in tx.input.iter().enumerate() {
let prevout = block_extra
.outpoint_values()
.get(&input.previous_output)
.unwrap();
let data = VerifyData {
script_pubkey: prevout.script_pubkey.clone(),
index: i,
amount: prevout.value,
spending: arc_tx_bytes.clone(),
flags: height_to_flags(block_extra.height()),
};
vec.push(data);
}
}
debug!("end preprocessing, #elements: {}", vec.len());
vec
}
fn main() -> Result<(), Box<dyn Error>> {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
info!("start");
let config = Config::parse();
let errors: Vec<_> = blocks_iterator::iter(config)
.flat_map(pre_processing)
.par_bridge()
.filter_map(|d| {
match d
.script_pubkey
.verify_with_flags(d.index, d.amount, &d.spending, d.flags)
{
Err(e) => Some((d, e)),
_ => None,
}
})
.collect();
for (data, e) in errors {
error!("{:?}", e);
error!("{:?}", data);
let tx: Transaction = deserialize(&data.spending).unwrap();
error!("tx: {}", tx.compute_txid());
}
Ok(())
}