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
use crate::blockchain::Blockchain;
use crate::consensus::SaitoMessage;
use crate::mempool::Mempool;
use crate::network::PEERS_DB_GLOBAL;
use crate::wallet::Wallet;
use std::convert::Infallible;
use std::sync::Arc;
use tokio::sync::{broadcast, RwLock};
use warp::{body, Filter, Reply};

use super::handlers::{get_block_handler, post_transaction_handler, ws_upgrade_handler};
use crate::peer::PeersDB;

/// websocket upgrade filter.
pub fn ws_upgrade_route_filter(
    wallet_lock: Arc<RwLock<Wallet>>,
    mempool_lock: Arc<RwLock<Mempool>>,
    blockchain_lock: Arc<RwLock<Blockchain>>,
    broadcast_channel_sender: broadcast::Sender<SaitoMessage>,
) -> impl Filter<Extract = (impl Reply,), Error = warp::Rejection> + Clone {
    warp::path("wsopen")
        .and(warp::ws())
        .and(with_peers_filter())
        .and(with_wallet(wallet_lock))
        .and(with_mempool(mempool_lock))
        .and(with_blockchain(blockchain_lock))
        .and(with_broadcast_channel_sender(broadcast_channel_sender))
        .and_then(ws_upgrade_handler)
}
/// get block filter.
/// TODO remove this? I believe we want ot use the socket for everything...
pub fn get_block_route_filter(
    blockchain_lock: Arc<RwLock<Blockchain>>,
) -> impl Filter<Extract = (impl Reply,), Error = warp::Rejection> + Clone {
    warp::path("block").and(
        warp::path::param()
            .and(with_blockchain(blockchain_lock))
            .and_then(get_block_handler),
    )
}

/// POST tx filter.
/// TODO remove this? I believe we want ot use the socket for everything...
pub fn post_transaction_route_filter(
    mempool_lock: Arc<RwLock<Mempool>>,
    blockchain_lock: Arc<RwLock<Blockchain>>,
) -> impl Filter<Extract = (impl Reply,), Error = warp::Rejection> + Clone {
    warp::post()
        .and(warp::path("sendtransaction"))
        .and(warp::path::end())
        .and(body::aggregate())
        .and(with_mempool(mempool_lock))
        .and(with_blockchain(blockchain_lock))
        .and_then(post_transaction_handler)
}

/// inject peers db lock
/// TODO Can this just be deleted? we should be able to just get the Peers DB from lazy_static global object PEERS_DB_GLOBAL
fn with_peers_filter() -> impl Filter<Extract = (Arc<RwLock<PeersDB>>,), Error = Infallible> + Clone
{
    warp::any().map(move || PEERS_DB_GLOBAL.clone())
}

/// inject wallet lock
fn with_wallet(
    wallet_lock: Arc<RwLock<Wallet>>,
) -> impl Filter<Extract = (Arc<RwLock<Wallet>>,), Error = Infallible> + Clone {
    warp::any().map(move || wallet_lock.clone())
}
/// inject peers db lock
fn with_mempool(
    mempool_lock: Arc<RwLock<Mempool>>,
) -> impl Filter<Extract = (Arc<RwLock<Mempool>>,), Error = Infallible> + Clone {
    warp::any().map(move || mempool_lock.clone())
}
/// inject blockchain lock
fn with_blockchain(
    blockchain_lock: Arc<RwLock<Blockchain>>,
) -> impl Filter<Extract = (Arc<RwLock<Blockchain>>,), Error = Infallible> + Clone {
    warp::any().map(move || blockchain_lock.clone())
}

/// inject blockchain lock
fn with_broadcast_channel_sender(
    broadcast_channel_sender: broadcast::Sender<SaitoMessage>,
) -> impl Filter<Extract = (broadcast::Sender<SaitoMessage>,), Error = Infallible> + Clone {
    warp::any().map(move || broadcast_channel_sender.clone())
}