bitcoin-heuristics 0.1.0

Pattern detection heuristics for Bitcoin on-chain analytics — consolidations, distributions, CoinJoin, fee spikes, dormant supply reactivation and more
Documentation
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
use chrono::{DateTime, TimeZone, Utc};
use evidence_chain::{EvidenceCategory, EvidenceChain, EvidenceLink};

use crate::heuristics::{Heuristic, HeuristicStatus};
use crate::match_result::HeuristicMatch;
use crate::models::TxFeatures;

// ─── Bitcoin RPC Abstraction ─────────────────────────────────────────────────

/// UTXO information returned by Bitcoin RPC (`gettxout`).
#[derive(Debug, Clone)]
pub struct TxOutInfo {
    /// Number of confirmations of the UTXO.
    pub confirmations: u64,
    #[allow(dead_code)]
    pub block_time: Option<DateTime<Utc>>,
}

/// Bitcoin RPC client abstraction to allow testing with mocks.
///
/// Concrete implementations use `ureq` (synchronous HTTP, without tokio) to avoid
/// conflicts when executed within `tokio::task::spawn_blocking`.
pub trait BitcoinRpc: Send + Sync {
    /// Queries a specific UTXO. Returns `None` if the UTXO was spent.
    fn get_tx_out(&self, txid_hex: &str, vout: u32) -> anyhow::Result<Option<TxOutInfo>>;
}

// ─── Real Implementation with ureq ───────────────────────────────────────────

/// Synchronous JSON-RPC client for Bitcoin Core.
///
/// Uses `ureq` (pure HTTP without tokio) to be safe within `spawn_blocking`.
pub struct JsonRpcClient {
    url: String,
    user: Option<String>,
    pass: Option<String>,
    agent: ureq::Agent,
    max_retries: u8,
    cache: std::sync::Mutex<std::collections::HashMap<(String, u32), Option<u64>>>,
}

impl JsonRpcClient {
    pub fn new(url: &str, user: &str, pass: &str) -> Self {
        let agent = ureq::AgentBuilder::new()
            .timeout_connect(std::time::Duration::from_secs(5))
            .timeout_read(std::time::Duration::from_secs(10))
            .timeout_write(std::time::Duration::from_secs(10))
            .max_idle_connections(200)
            .max_idle_connections_per_host(200)
            .build();

        Self {
            url: url.trim().to_string(),
            user: if user.is_empty() {
                None
            } else {
                Some(user.to_string())
            },
            pass: if pass.is_empty() {
                None
            } else {
                Some(pass.to_string())
            },
            agent,
            max_retries: 2,
            cache: std::sync::Mutex::new(std::collections::HashMap::new()),
        }
    }

    fn call(&self, method: &str, params: serde_json::Value) -> anyhow::Result<serde_json::Value> {
        let body = serde_json::json!({
            "jsonrpc": "2.0",
            "id": "bitcoin-heuristics",
            "method": method,
            "params": params,
        });

        let mut attempt: u8 = 0;
        loop {
            let mut req = self
                .agent
                .post(&self.url)
                .set("Content-Type", "application/json");

            if let (Some(user), Some(pass)) = (&self.user, &self.pass) {
                let credentials = base64_encode(&format!("{user}:{pass}"));
                req = req.set("Authorization", &format!("Basic {credentials}"));
            }

            let resp = match req.send_string(&body.to_string()) {
                Ok(r) => r,
                Err(e) => {
                    if attempt < self.max_retries {
                        attempt += 1;
                        let backoff_ms = 200u64.saturating_mul(1u64 << (attempt - 1));
                        tracing::warn!(error = %e, attempt, "RPC request failed, retrying");
                        std::thread::sleep(std::time::Duration::from_millis(backoff_ms));
                        continue;
                    } else {
                        return Err(anyhow::anyhow!("RPC request failed after retries: {e}"));
                    }
                }
            };

            let resp_json: serde_json::Value = resp
                .into_json()
                .map_err(|e| anyhow::anyhow!("Failed to parse RPC response: {e}"))?;

            if let Some(error) = resp_json.get("error").filter(|e| !e.is_null()) {
                return Err(anyhow::anyhow!("RPC error: {error}"));
            }

            return Ok(resp_json["result"].clone());
        }
    }
}

fn base64_encode(input: &str) -> String {
    use std::fmt::Write;
    const TABLE: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    let bytes = input.as_bytes();
    let mut out = String::new();
    let mut i = 0;
    while i < bytes.len() {
        let b0 = bytes[i];
        let b1 = if i + 1 < bytes.len() { bytes[i + 1] } else { 0 };
        let b2 = if i + 2 < bytes.len() { bytes[i + 2] } else { 0 };
        let _ = write!(out, "{}", TABLE[(b0 >> 2) as usize] as char);
        let _ = write!(out, "{}", TABLE[((b0 & 3) << 4 | b1 >> 4) as usize] as char);
        let _ = write!(
            out,
            "{}",
            if i + 1 < bytes.len() {
                TABLE[((b1 & 0xF) << 2 | b2 >> 6) as usize] as char
            } else {
                '='
            }
        );
        let _ = write!(
            out,
            "{}",
            if i + 2 < bytes.len() {
                TABLE[(b2 & 0x3F) as usize] as char
            } else {
                '='
            }
        );
        i += 3;
    }
    out
}

impl BitcoinRpc for JsonRpcClient {
    fn get_tx_out(&self, txid_hex: &str, vout: u32) -> anyhow::Result<Option<TxOutInfo>> {
        let cache_key = (txid_hex.to_string(), vout);

        if let Ok(cache) = self.cache.lock() {
            if let Some(cached) = cache.get(&cache_key) {
                return Ok(cached.map(|confs| TxOutInfo {
                    confirmations: confs,
                    block_time: None,
                }));
            }
        }

        let result = self.call("gettxout", serde_json::json!([txid_hex, vout, false]))?;

        if result.is_null() {
            if let Ok(mut cache) = self.cache.lock() {
                cache.insert(cache_key, None);
            }
            return Ok(None);
        }

        let confirmations = result["confirmations"].as_u64().unwrap_or(0);

        let block_time = result["blockTime"]
            .as_i64()
            .map(|ts| Utc.timestamp_opt(ts, 0).single().unwrap_or_else(Utc::now));

        if let Ok(mut cache) = self.cache.lock() {
            cache.insert(cache_key, Some(confirmations));
        }

        Ok(Some(TxOutInfo {
            confirmations,
            block_time,
        }))
    }
}

// ─── Long-Term Supply Activation Heuristic ───────────────────────────────────

/// Detects capital activation from long-term supply.
///
/// Verifies if any input UTXO of the transaction remained inactive for
/// at least `min_activation_confirmations` blocks (~years).
///
/// Requires either a pre-populated `utxo_ages_blocks` map in `TxFeatures`
/// or a `BitcoinRpc` implementation for fallback RPC queries.
pub struct LongTermSupplyActivationHeuristic<R: BitcoinRpc> {
    pub rpc: R,
    /// Minimum UTXO age in blocks to be considered "long-term".
    /// Default: 157,680 (~3 years at 144 blocks/day).
    pub min_activation_confirmations: u64,
}

impl<R: BitcoinRpc> LongTermSupplyActivationHeuristic<R> {
    pub fn new(rpc: R, min_activation_confirmations: u64) -> Self {
        Self {
            rpc,
            min_activation_confirmations,
        }
    }
}

impl<R: BitcoinRpc> Heuristic for LongTermSupplyActivationHeuristic<R> {
    fn id(&self) -> &'static str {
        "long-term-supply-activation-v1"
    }

    fn version(&self) -> &'static str {
        "1.0.0"
    }

    fn status(&self) -> HeuristicStatus {
        HeuristicStatus::Active
    }

    fn evaluate(&self, f: &TxFeatures) -> Option<HeuristicMatch> {
        if f.is_coinbase || f.input_utxo_refs.is_empty() {
            return None;
        }

        let total_usd =
            (f.total_input_value as f64 * f.block_price_usd.unwrap_or(0.0)) / 100_000_000.0;
        if total_usd < 50_000.0 {
            return None;
        }

        let mut activation_refs: Vec<String> = Vec::new();
        let mut max_confirmations: u64 = 0;
        let mut sum_confirmations: u64 = 0;

        for (prev_txid, prev_vout) in &f.input_utxo_refs {
            let txid_hex: String = prev_txid.iter().rev().map(|b| format!("{b:02x}")).collect();

            let age_blocks: Option<u64> =
                if let Some(&age) = f.utxo_ages_blocks.get(&(prev_txid.clone(), *prev_vout)) {
                    if age >= 0 {
                        Some(age as u64)
                    } else {
                        None
                    }
                } else {
                    match self.rpc.get_tx_out(&txid_hex, *prev_vout) {
                        Ok(Some(info)) => Some(info.confirmations),
                        Ok(None) => None,
                        Err(e) => {
                            tracing::warn!(
                                error = %e,
                                txid = %txid_hex,
                                vout = prev_vout,
                                "Failed to query UTXO for activation check"
                            );
                            None
                        }
                    }
                };

            if let Some(age) = age_blocks {
                if age >= self.min_activation_confirmations {
                    activation_refs.push(format!("{}:{}", txid_hex, prev_vout));
                    max_confirmations = max_confirmations.max(age);
                    sum_confirmations += age;
                }
            }
        }

        if activation_refs.is_empty() {
            return None;
        }

        let activation_count = activation_refs.len();
        let total_input_count = f.input_utxo_refs.len().max(1);
        let activation_ratio = activation_count as f64 / total_input_count as f64;
        let avg_confirmations = sum_confirmations / activation_count as u64;

        if activation_ratio < 0.5 && avg_confirmations < 144_000 {
            return None;
        }

        let approx_years = max_confirmations / 52_560;
        let summary = format!(
            "Long-term supply activation: {activation_count} supply UTXO(s) \
             (≥{} blocks, ~{approx_years} year(s)) activated at block {}",
            self.min_activation_confirmations, f.block_height
        );

        Some(HeuristicMatch::new(
            self.id(),
            self.version(),
            "long_term_supply_activation",
            "long_term_supply_activation",
            self.trigger_scope(),
            summary,
            serde_json::json!({
                "activation_utxo_count": activation_count,
                "max_confirmations": max_confirmations,
                "avg_activation_confirmations": avg_confirmations,
                "activation_ratio": activation_ratio,
                "min_activation_confirmations": self.min_activation_confirmations,
                "input_count": f.input_count,
                "script_type_evolution": f.script_type_evolution,
                "total_usd_value": total_usd,
            }),
        ))
    }

    fn build_evidence(&self, f: &TxFeatures) -> Option<EvidenceChain> {
        if f.is_coinbase || f.input_utxo_refs.is_empty() {
            return None;
        }

        let total_usd =
            (f.total_input_value as f64 * f.block_price_usd.unwrap_or(0.0)) / 100_000_000.0;
        if total_usd < 50_000.0 {
            return None;
        }
        let txid_hex: String = f.txid.iter().rev().map(|b| format!("{b:02x}")).collect();
        let mut chain = EvidenceChain::new(self.id(), self.version());

        let (first_prev_txid, first_prev_vout) = &f.input_utxo_refs[0];
        let utxo_ref = format!(
            "{}:{}",
            first_prev_txid
                .iter()
                .rev()
                .map(|b| format!("{b:02x}"))
                .collect::<String>(),
            first_prev_vout
        );

        chain.add_link(
            EvidenceLink::new(
                EvidenceCategory::Temporal,
                format!(
                    "Input UTXO dormant for ≥{} blocks (~{} years) before activation",
                    self.min_activation_confirmations,
                    self.min_activation_confirmations / 52_560
                ),
                utxo_ref,
            )
            .with_metric(self.min_activation_confirmations as f64, "blocks")
            .with_threshold(self.min_activation_confirmations as f64, true),
        );

        chain.add_link(
            EvidenceLink::new(
                EvidenceCategory::Value,
                format!(
                    "Total input value {} sat (~${:.2}) reactivated in tx {}",
                    f.total_input_value,
                    total_usd,
                    &txid_hex[..8]
                ),
                txid_hex,
            )
            .with_metric(total_usd, "usd"),
        );

        chain.finalize();
        Some(chain)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Utc;

    struct MockRpc {
        dormant_confirmations: u64,
        fail: bool,
    }

    impl BitcoinRpc for MockRpc {
        fn get_tx_out(&self, _txid_hex: &str, _vout: u32) -> anyhow::Result<Option<TxOutInfo>> {
            if self.fail {
                return Err(anyhow::anyhow!("mock RPC failure"));
            }
            Ok(Some(TxOutInfo {
                confirmations: self.dormant_confirmations,
                block_time: None,
            }))
        }
    }

    struct MockRpcNeverDormant;
    impl BitcoinRpc for MockRpcNeverDormant {
        fn get_tx_out(&self, _: &str, _: u32) -> anyhow::Result<Option<TxOutInfo>> {
            Ok(Some(TxOutInfo {
                confirmations: 100,
                block_time: None,
            }))
        }
    }

    struct MockRpcSpent;
    impl BitcoinRpc for MockRpcSpent {
        fn get_tx_out(&self, _: &str, _: u32) -> anyhow::Result<Option<TxOutInfo>> {
            Ok(None)
        }
    }

    fn make_features(utxo_refs: Vec<(Vec<u8>, u32)>) -> TxFeatures {
        TxFeatures {
            txid: vec![0x06u8; 32],
            block_height: 840_000,
            block_timestamp: Utc::now(),
            input_count: utxo_refs.len() as i32,
            output_count: 2,
            is_coinbase: false,
            total_input_value: 10_000_000_000,
            total_output_value: 9_995_000_000,
            fee: 5_000_000,
            output_value_max: 9_994_000_000,
            block_price_usd: Some(100_000.0),
            fee_rate_sat_vb: Some(3.0),
            input_p2pkh_count: utxo_refs.len() as i32,
            output_p2pkh_count: 2,
            tx_vsize_vbytes: 300,
            tx_version: 1,
            input_utxo_refs: utxo_refs,
            output_values: vec![1_000_000, 3_995_000],
            ..Default::default()
        }
    }

    #[test]
    fn test_activation_detected() {
        let rpc = MockRpc {
            dormant_confirmations: 200_000,
            fail: false,
        };
        let h = LongTermSupplyActivationHeuristic::new(rpc, 157_680);
        let f = make_features(vec![(vec![0xaau8; 32], 0)]);
        let result = h.evaluate(&f);
        assert!(result.is_some(), "UTXO with 200k confirmations should fire");
        assert_eq!(result.unwrap().event_type, "long_term_supply_activation");
    }

    #[test]
    fn test_not_long_term_too_recent() {
        let rpc = MockRpcNeverDormant;
        let h = LongTermSupplyActivationHeuristic::new(rpc, 157_680);
        let f = make_features(vec![(vec![0xbbu8; 32], 0)]);
        assert!(h.evaluate(&f).is_none(), "Recent UTXO does not fire");
    }

    #[test]
    fn test_spent_utxo_skipped() {
        let rpc = MockRpcSpent;
        let h = LongTermSupplyActivationHeuristic::new(rpc, 157_680);
        let f = make_features(vec![(vec![0xccu8; 32], 0)]);
        assert!(h.evaluate(&f).is_none(), "Spent UTXO does not fire");
    }

    #[test]
    fn test_rpc_failure_skipped() {
        let rpc = MockRpc {
            dormant_confirmations: 0,
            fail: true,
        };
        let h = LongTermSupplyActivationHeuristic::new(rpc, 157_680);
        let f = make_features(vec![(vec![0xddu8; 32], 0)]);
        assert!(h.evaluate(&f).is_none(), "RPC failure should not fire");
    }

    #[test]
    fn test_coinbase_skipped() {
        let rpc = MockRpc {
            dormant_confirmations: 200_000,
            fail: false,
        };
        let h = LongTermSupplyActivationHeuristic::new(rpc, 157_680);
        let mut f = make_features(vec![(vec![0xeeu8; 32], 0)]);
        f.is_coinbase = true;
        assert!(h.evaluate(&f).is_none(), "coinbase ignored");
    }

    #[test]
    fn test_no_utxo_refs_skipped() {
        let rpc = MockRpc {
            dormant_confirmations: 200_000,
            fail: false,
        };
        let h = LongTermSupplyActivationHeuristic::new(rpc, 157_680);
        let f = make_features(vec![]);
        assert!(h.evaluate(&f).is_none(), "without UTXO refs does not fire");
    }

    #[test]
    fn test_activation_uses_utxo_ages_map() {
        struct PanicRpc;
        impl BitcoinRpc for PanicRpc {
            fn get_tx_out(&self, _: &str, _: u32) -> anyhow::Result<Option<TxOutInfo>> {
                panic!("RPC must not be called when utxo_ages_blocks map is populated")
            }
        }
        let h = LongTermSupplyActivationHeuristic::new(PanicRpc, 157_680);
        let mut f = make_features(vec![(vec![0xaau8; 32], 0)]);
        f.utxo_ages_blocks.insert((vec![0xaau8; 32], 0), 200_000);
        let result = h.evaluate(&f);
        assert!(result.is_some(), "should detect with map age without RPC");
        assert_eq!(result.unwrap().event_type, "long_term_supply_activation");
    }

    #[test]
    fn test_long_term_not_activated_in_map() {
        struct PanicRpc;
        impl BitcoinRpc for PanicRpc {
            fn get_tx_out(&self, _: &str, _: u32) -> anyhow::Result<Option<TxOutInfo>> {
                panic!("RPC must not be called when map is populated")
            }
        }
        let h = LongTermSupplyActivationHeuristic::new(PanicRpc, 157_680);
        let mut f = make_features(vec![(vec![0xbbu8; 32], 0)]);
        f.utxo_ages_blocks.insert((vec![0xbbu8; 32], 0), 1_000);
        assert!(h.evaluate(&f).is_none(), "age < threshold should not fire");
    }

    #[test]
    fn test_activation_fallback_to_rpc() {
        let rpc = MockRpc {
            dormant_confirmations: 200_000,
            fail: false,
        };
        let h = LongTermSupplyActivationHeuristic::new(rpc, 157_680);
        let f = make_features(vec![(vec![0xffu8; 32], 0)]);
        let result = h.evaluate(&f);
        assert!(result.is_some(), "fallback to RPC should detect activation");
    }

    #[test]
    fn test_base64_encode() {
        assert_eq!(base64_encode(""), "");
        assert_eq!(base64_encode("a"), "YQ==");
        assert_eq!(base64_encode("abc"), "YWJj");
        assert_eq!(base64_encode("user:pass"), "dXNlcjpwYXNz");
    }
}