-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitcoin_conf_compat.rs
More file actions
220 lines (206 loc) · 7.88 KB
/
Copy pathbitcoin_conf_compat.rs
File metadata and controls
220 lines (206 loc) · 7.88 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use std::path::Path;
use anyhow::{Context as _, Result};
use crate::config::{Auth, Config, ConfigLayer};
use bitcoin_rs_primitives::Network;
/// Applies a Bitcoin Core `bitcoin.conf` file to `config`.
pub fn apply_file(config: &mut Config, path: &Path) -> Result<()> {
let text = std::fs::read_to_string(path)
.with_context(|| format!("failed to read bitcoin.conf {}", path.display()))?;
let layer = parse_for_network(&text, config.network);
layer.apply_to(config);
Ok(())
}
fn parse_for_network(text: &str, network: Network) -> ConfigLayer {
let mut global = ConfigLayer::default();
let mut selected = ConfigLayer::default();
let mut current_section_selected = None;
for raw_line in text.lines() {
let line = strip_inline_comment(raw_line).trim();
if line.is_empty() {
continue;
}
if let Some(section) = parse_section(line) {
current_section_selected = Some(section_matches_network(section, network));
continue;
}
let Some((raw_key, raw_value)) = line.split_once('=') else {
continue;
};
let key = raw_key.trim().trim_start_matches('-');
let value = raw_value.trim();
match current_section_selected {
None => apply_key(&mut global, key, value),
Some(true) => apply_key(&mut selected, key, value),
Some(false) => {}
}
}
global.apply_from(&selected);
global
}
fn apply_key(layer: &mut ConfigLayer, key: &str, value: &str) {
match key {
"prune" => {
if let Ok(prune_target_mb) = value.parse() {
layer.prune_target_mb = Some(prune_target_mb);
}
}
"rpcuser" => layer.rpc_user = Some(value.to_owned()),
"rpcpassword" => layer.rpc_password = Some(value.to_owned()),
"rpccookiefile" => layer.rpc_cookie = Some(value.into()),
"listen" if parse_core_bool(value).is_some_and(|listen| !listen) => {
layer.p2p_listen = Some(Vec::new());
}
"txindex" => layer.txindex = parse_core_bool(value),
"blockfilterindex" => layer.blockfilterindex = parse_core_bool(value),
"dbcache" => {
if let Ok(dbcache_mb) = value.parse() {
layer.dbcache_mb = Some(dbcache_mb);
}
}
"zmqpubhashblock" => push_endpoint(&mut layer.zmqpubhashblock, value),
"zmqpubhashtx" => push_endpoint(&mut layer.zmqpubhashtx, value),
"zmqpubrawblock" => push_endpoint(&mut layer.zmqpubrawblock, value),
"zmqpubrawtx" => push_endpoint(&mut layer.zmqpubrawtx, value),
"zmqpubsequence" => push_endpoint(&mut layer.zmqpubsequence, value),
"zmqpubhashblockhwm" => layer.zmqpubhashblockhwm = value.parse().ok(),
"zmqpubhashtxhwm" => layer.zmqpubhashtxhwm = value.parse().ok(),
"zmqpubrawblockhwm" => layer.zmqpubrawblockhwm = value.parse().ok(),
"zmqpubrawtxhwm" => layer.zmqpubrawtxhwm = value.parse().ok(),
"zmqpubsequencehwm" => layer.zmqpubsequencehwm = value.parse().ok(),
_ => {}
}
if layer.rpc_user.is_some() || layer.rpc_password.is_some() {
let user = layer
.rpc_user
.clone()
.unwrap_or_else(|| "bitcoin-rs".to_owned());
let password = layer.rpc_password.clone().unwrap_or_default();
layer.rpc_auth = Some(Auth::basic(user, password));
}
}
fn parse_core_bool(value: &str) -> Option<bool> {
match value.trim().to_ascii_lowercase().as_str() {
"1" | "true" | "yes" | "on" => Some(true),
"0" | "false" | "no" | "off" => Some(false),
_ => None,
}
}
fn push_endpoint(slot: &mut Option<Vec<String>>, value: &str) {
slot.get_or_insert_with(Vec::new).push(value.to_owned());
}
fn parse_section(line: &str) -> Option<&str> {
line.strip_prefix('[')?.strip_suffix(']').map(str::trim)
}
fn section_matches_network(section: &str, network: Network) -> bool {
match section.trim().to_ascii_lowercase().as_str() {
"main" | "mainnet" => network == Network::Mainnet,
"test" | "testnet" | "testnet3" => network == Network::Testnet3,
"testnet4" => network == Network::Testnet4,
"signet" => network == Network::Signet,
"regtest" => network == Network::Regtest,
_ => false,
}
}
fn strip_inline_comment(line: &str) -> &str {
let hash = line.find('#');
let semicolon = line.find(';');
match (hash, semicolon) {
(Some(left), Some(right)) => &line[..left.min(right)],
(Some(index), None) | (None, Some(index)) => &line[..index],
(None, None) => line,
}
}
trait ConfigLayerMerge {
fn apply_from(&mut self, other: &Self);
}
impl ConfigLayerMerge for ConfigLayer {
fn apply_from(&mut self, other: &Self) {
if other.network.is_some() {
self.network = other.network;
}
if other.data_dir.is_some() {
self.data_dir.clone_from(&other.data_dir);
}
if other.storage_backend.is_some() {
self.storage_backend.clone_from(&other.storage_backend);
}
if other.rpc_bind.is_some() {
self.rpc_bind = other.rpc_bind;
}
if other.rpc_auth.is_some() {
self.rpc_auth.clone_from(&other.rpc_auth);
}
if other.rpc_user.is_some() {
self.rpc_user.clone_from(&other.rpc_user);
}
if other.rpc_password.is_some() {
self.rpc_password.clone_from(&other.rpc_password);
}
if other.rpc_cookie.is_some() {
self.rpc_cookie.clone_from(&other.rpc_cookie);
}
if other.electrum_bind.is_some() {
self.electrum_bind = other.electrum_bind;
}
if other.electrum_tls_cert.is_some() {
self.electrum_tls_cert.clone_from(&other.electrum_tls_cert);
}
if other.p2p_listen.is_some() {
self.p2p_listen.clone_from(&other.p2p_listen);
}
if other.dns_seeds_enabled.is_some() {
self.dns_seeds_enabled = other.dns_seeds_enabled;
}
if other.prune_target_mb.is_some() {
self.prune_target_mb = other.prune_target_mb;
}
if other.utreexo_mode.is_some() {
self.utreexo_mode = other.utreexo_mode;
}
if other.txindex.is_some() {
self.txindex = other.txindex;
}
if other.blockfilterindex.is_some() {
self.blockfilterindex = other.blockfilterindex;
}
if other.dbcache_mb.is_some() {
self.dbcache_mb = other.dbcache_mb;
}
if other.log_level.is_some() {
self.log_level.clone_from(&other.log_level);
}
if other.metrics_bind.is_some() {
self.metrics_bind = other.metrics_bind;
}
if other.zmqpubhashblock.is_some() {
self.zmqpubhashblock.clone_from(&other.zmqpubhashblock);
}
if other.zmqpubhashtx.is_some() {
self.zmqpubhashtx.clone_from(&other.zmqpubhashtx);
}
if other.zmqpubrawblock.is_some() {
self.zmqpubrawblock.clone_from(&other.zmqpubrawblock);
}
if other.zmqpubrawtx.is_some() {
self.zmqpubrawtx.clone_from(&other.zmqpubrawtx);
}
if other.zmqpubsequence.is_some() {
self.zmqpubsequence.clone_from(&other.zmqpubsequence);
}
if other.zmqpubhashblockhwm.is_some() {
self.zmqpubhashblockhwm = other.zmqpubhashblockhwm;
}
if other.zmqpubhashtxhwm.is_some() {
self.zmqpubhashtxhwm = other.zmqpubhashtxhwm;
}
if other.zmqpubrawblockhwm.is_some() {
self.zmqpubrawblockhwm = other.zmqpubrawblockhwm;
}
if other.zmqpubrawtxhwm.is_some() {
self.zmqpubrawtxhwm = other.zmqpubrawtxhwm;
}
if other.zmqpubsequencehwm.is_some() {
self.zmqpubsequencehwm = other.zmqpubsequencehwm;
}
}
}