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
#![crate_name = "vls_frontend"]
#![forbid(unsafe_code)]
#![allow(bare_trait_objects)]
#![allow(ellipsis_inclusive_range_patterns)]

use std::sync::Arc;

use async_trait::async_trait;

use bitcoin::secp256k1::PublicKey;
use bitcoin::{BlockHash, BlockHeader, Network, OutPoint, Txid};
use lightning_signer::bitcoin;
use lightning_signer::node::SignedHeartbeat;
use lightning_signer::txoo::proof::UnspentProof;

mod chain_follower;
pub mod frontend;
pub mod heartbeat;

pub use self::frontend::Frontend;
pub use self::heartbeat::HeartbeatMonitor;

/// Provides ChainTracks for nodes in a signer
#[async_trait]
pub trait ChainTrackDirectory: Sync + Send {
    /// Return a ChainTrack for a specific node
    fn tracker(&self, node_id: &PublicKey) -> Arc<dyn ChainTrack>;

    /// Return ChainTracks for all nodes
    async fn trackers(&self) -> Vec<Arc<dyn ChainTrack>>;
}

/// ChainTracker interface
#[async_trait]
pub trait ChainTrack: Sync + Send {
    /// Identity string for the log
    fn log_prefix(&self) -> String;

    /// Full identity
    async fn id(&self) -> Vec<u8>;

    /// The heartbeat public key
    async fn heartbeat_pubkey(&self) -> PublicKey;

    /// Returns the network
    fn network(&self) -> Network;

    /// Return the block height and hash of specified node's chaintracker tip
    async fn tip_info(&self) -> (u32, BlockHash);

    /// Returns all Txid and OutPoints to watch for in future blocks
    async fn forward_watches(&self) -> (Vec<Txid>, Vec<OutPoint>);

    /// Returns all Txid and OutPoint watches used for prior blocks (used when removing)
    async fn reverse_watches(&self) -> (Vec<Txid>, Vec<OutPoint>);

    /// Add a block to the tracker
    async fn add_block(&self, header: BlockHeader, proof: UnspentProof);

    /// Remove block at tip due to reorg
    async fn remove_block(&self, proof: UnspentProof);

    /// Produce a signed heartbeat for the signer node
    async fn beat(&self) -> SignedHeartbeat;
}