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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#![forbid(unsafe_code)]
#![deny(non_upper_case_globals)]
#![deny(non_camel_case_types)]
#![deny(non_snake_case)]
#![deny(unused_mut)]
#![deny(dead_code)]
#![deny(unused_imports)]
#![deny(missing_docs)]
#![deny(unused_must_use)]
use bitcoin::{Block, BlockHash, OutPoint, Transaction, TxOut, Txid};
use log::{info, Level};
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::sync::mpsc::{sync_channel, SyncSender};
use std::thread;
use std::thread::JoinHandle;
use std::time::Instant;
use structopt::StructOpt;
mod fee;
mod parse;
mod read;
mod reorder;
mod truncmap;
#[derive(StructOpt, Debug, Clone)]
pub struct Config {
#[structopt(short, long)]
pub blocks_dir: PathBuf,
#[structopt(short, long)]
pub network: bitcoin::Network,
#[structopt(short, long)]
pub skip_prevout: bool,
#[structopt(short, long, default_value = "6")]
pub max_reorg: u8,
}
#[derive(Debug)]
pub struct BlockExtra {
pub block: Block,
pub block_hash: BlockHash,
pub size: u32,
pub next: Vec<BlockHash>,
pub height: u32,
pub outpoint_values: HashMap<OutPoint, TxOut>,
pub tx_hashes: HashSet<Txid>,
}
impl BlockExtra {
pub fn average_fee(&self) -> Option<f64> {
Some(self.fee()? as f64 / self.block.txdata.len() as f64)
}
pub fn fee(&self) -> Option<u64> {
let mut total = 0u64;
for tx in self.block.txdata.iter() {
total += self.tx_fee(tx)?;
}
Some(total)
}
pub fn tx_fee(&self, tx: &Transaction) -> Option<u64> {
let output_total: u64 = tx.output.iter().map(|el| el.value).sum();
let mut input_total = 0u64;
for input in tx.input.iter() {
input_total += self.outpoint_values.get(&input.previous_output)?.value;
}
Some(input_total - output_total)
}
}
pub fn iterate(config: Config, channel: SyncSender<Option<BlockExtra>>) -> JoinHandle<()> {
thread::spawn(move || {
let now = Instant::now();
let (send_blobs, receive_blobs) = sync_channel(2);
let mut read = read::Read::new(config.blocks_dir.clone(), send_blobs);
let read_handle = thread::spawn(move || {
read.start();
});
let (send_blocks, receive_blocks) = sync_channel(200);
let mut parse = parse::Parse::new(config.network, receive_blobs, send_blocks);
let parse_handle = thread::spawn(move || {
parse.start();
});
let (send_ordered_blocks, receive_ordered_blocks) = sync_channel(200);
let send_ordered_blocks = if config.skip_prevout {
channel.clone()
} else {
send_ordered_blocks
};
let mut reorder = reorder::Reorder::new(
config.network,
config.max_reorg,
receive_blocks,
send_ordered_blocks,
);
let orderer_handle = thread::spawn(move || {
reorder.start();
});
if !config.skip_prevout {
let mut fee = fee::Fee::new(receive_ordered_blocks, channel);
let fee_handle = thread::spawn(move || {
fee.start();
});
fee_handle.join().unwrap();
}
read_handle.join().unwrap();
parse_handle.join().unwrap();
orderer_handle.join().unwrap();
info!("Total time elapsed: {}s", now.elapsed().as_secs());
})
}
pub fn periodic_log_level(i: u32) -> Level {
if i % 10_000 == 0 {
Level::Info
} else {
Level::Debug
}
}