summaryrefslogtreecommitdiff
path: root/btc-wire/src/loops/analysis.rs
blob: d2081d502b5aa7c798b4ec14f854698586b6c691 (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
/*
  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 btc_wire::rpc::{ChainTipsStatus, Rpc};
use common::log::log::warn;

use super::LoopResult;

/// Analyse blockchain behavior and return the new confirmation delay
pub fn analysis(rpc: &mut Rpc, current: u32, max: u32) -> LoopResult<u32> {
    // 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 u32;
    // If new fork is bigger than what current confirmation delay protect against
    if fork >= current {
        // Limit confirmation growth
        let new_conf = fork.saturating_add(1).min(max);
        warn!(
            "analysis: found dangerous fork of {} blocks, adapt confirmation to {} blocks capped at {}, you should update taler.conf",
            fork, new_conf, max
        );
        return Ok(new_conf);
    }

    // TODO smarter analysis: suspicious transaction value, limit wire bitcoin throughput
    Ok(current)
}