-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
179 lines (149 loc) · 4.52 KB
/
mod.rs
File metadata and controls
179 lines (149 loc) · 4.52 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use bitcoincore_rpc::{Auth, Client, RpcApi};
use serde::{Deserialize, Serialize};
use std::time::Duration;
use tokio::sync::broadcast;
use tokio::time;
use tracing::{debug, error, info, warn};
use crate::common::{Event, CoinbaseOut, Sv2Error, Result};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BitcoinRpcConfig {
pub rpc_url: String,
pub rpc_user: String,
pub rpc_password: String,
pub poll_interval: u64,
pub min_fee_rate: f64,
}
pub struct BitcoinNode {
cfg: BitcoinRpcConfig,
rpc: Option<Client>,
bus: broadcast::Sender<Event>,
outputs: Vec<CoinbaseOut>,
last_height: u64,
tpl_seq: u64,
}
impl BitcoinNode {
pub fn new(
cfg: BitcoinRpcConfig,
bus: broadcast::Sender<Event>,
outputs: Vec<CoinbaseOut>,
) -> Self {
Self {
cfg,
rpc: None,
bus,
outputs,
last_height: 0,
tpl_seq: 0,
}
}
pub async fn run(mut self) -> Result<()> {
info!("Starting Bitcoin RPC handler");
if let Err(e) = self.connect() {
error!("RPC connect failed: {}", e);
let _ = self.bus.send(Event::NodeDown);
return Err(e);
}
let _ = self.bus.send(Event::NodeUp);
info!("Connected to {}", self.cfg.rpc_url);
let mut ticker = time::interval(Duration::from_secs(self.cfg.poll_interval));
loop {
tokio::select! {
_ = ticker.tick() => {
if let Err(e) = self.poll_template().await {
error!("Template poll error: {}", e);
let _ = self.bus.send(Event::TemplateErr(e.to_string()));
}
}
}
}
}
fn connect(&mut self) -> Result<()> {
let auth = Auth::UserPass(
self.cfg.rpc_user.clone(),
self.cfg.rpc_password.clone(),
);
let client = Client::new(&self.cfg.rpc_url, auth)?;
let chain = client.get_blockchain_info()?;
info!("Chain: {}, height: {}", chain.chain, chain.blocks);
self.last_height = chain.blocks;
self.rpc = Some(client);
Ok(())
}
async fn poll_template(&mut self) -> Result<()> {
let client = self.rpc.as_ref().ok_or_else(|| {
Sv2Error::PoolConnection("RPC not ready".into())
})?;
let chain = client.get_blockchain_info()?;
let h = chain.blocks;
if h > self.last_height {
info!("New block at height {}", h);
self.last_height = h;
}
let tpl = match self.fetch_template() {
Ok(t) => t,
Err(e) => {
warn!("Template fetch failed: {}", e);
return Err(e);
}
};
debug!("Template: height={}, txs={}", tpl.height, tpl.txs.len());
let fees: u64 = tpl.txs.iter().filter_map(|tx| tx.fee).sum();
let _ = self.bus.send(Event::NewTemplate {
height: tpl.height,
txs: tpl.txs.len(),
fees,
});
self.tpl_seq += 1;
let tpl_id = self.tpl_seq;
let raw_txs: Vec<Vec<u8>> = tpl
.txs
.iter()
.filter_map(|tx| hex::decode(&tx.data).ok())
.collect();
let _ = self.bus.send(Event::DeclareJob {
tpl_id,
outputs: self.outputs.clone(),
txs: raw_txs,
});
Ok(())
}
fn fetch_template(&self) -> Result<Template> {
let client = self.rpc.as_ref().ok_or_else(|| {
Sv2Error::PoolConnection("RPC not ready".into())
})?;
let raw: serde_json::Value = client.call(
"getblocktemplate",
&[serde_json::json!({ "rules": ["segwit"] })],
)?;
let tpl: Template = serde_json::from_value(raw)
.map_err(|e| Sv2Error::Serialization(e.to_string()))?;
Ok(tpl)
}
}
#[derive(Debug, Deserialize)]
struct Template {
version: u32,
#[serde(rename = "previousblockhash")]
prev_hash: String,
#[serde(rename = "transactions")]
txs: Vec<TxEntry>,
#[serde(rename = "coinbasevalue")]
coinbase_val: u64,
target: String,
#[serde(rename = "mintime")]
min_time: u64,
#[serde(rename = "curtime")]
cur_time: u64,
bits: String,
height: u64,
}
#[derive(Debug, Deserialize)]
struct TxEntry {
data: String,
txid: String,
hash: String,
fee: Option<u64>,
#[serde(default)]
depends: Vec<usize>,
weight: u64,
}