summaryrefslogtreecommitdiff
path: root/btc-wire/src/loops/analysis.rs
blob: 57b7bd1e75a44881027a46ca30673be5795b68ed (plain)
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
/*
  This file is part of TALER
  Copyright (C) 2022 Taler Systems SA

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU Affero General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  TALER is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License along with
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
*/
use std::sync::atomic::Ordering;

use btc_wire::rpc::{AutoRpcCommon, ChainTipsStatus};
use common::{
    log::log::{error, warn},
    postgres::fallible_iterator::FallibleIterator,
    reconnect::AutoReconnectDb,
};

use crate::WireState;

use super::LoopResult;

/// Analyse blockchain behavior and adapt confirmations in real time
pub fn analysis(mut rpc: AutoRpcCommon, mut db: AutoReconnectDb, state: &WireState) {
    // The biggest fork ever seen
    let mut max_seen = 0;
    loop {
        let rpc = rpc.client();
        let db = db.client();
        let result: LoopResult<()> = (|| {
            // Register as listener
            db.batch_execute("LISTEN new_block")?;
            loop {
                // Get biggest known valid fork
                let fork = rpc
                    .get_chain_tips()?
                    .into_iter()
                    .filter_map(|t| {
                        (t.status == ChainTipsStatus::ValidFork).then(|| t.length)
                    })
                    .max()
                    .unwrap_or(0) as u16;
                // The first time we see a fork that big
                if fork > max_seen {
                    max_seen = fork;
                    let current_conf = state.confirmation.load(Ordering::SeqCst);
                    // If new fork is bigger than the current confirmation
                    if fork > current_conf {
                        // Max two time the configuration
                        let new_conf = fork.min(state.max_confirmation);
                        state.confirmation.store(new_conf, Ordering::SeqCst);
                        warn!(
                            "analysis: found dangerous fork of {} blocks, adapt confirmation to {} blocks capped at {}, you should update taler.conf",
                            fork, new_conf, state.max_confirmation 
                        );
                    }
                }

                // TODO smarter analysis: suspicious transaction value, limit wire bitcoin throughput

                // Wait for next notification
                db.notifications().blocking_iter().next()?;
            }
        })();
        if let Err(e) = result {
            error!("analysis: {}", e);
        }
    }
}