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
/*
* Copyright (C) 2020  Aravinth Manivannan <realaravinth@batsense.net>
*
* This program 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 of the
* License, or (at your option) any later version.
*
* This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.
*/
//! Client wrapper for p2p communication

use actix_web::client::Client as awc;
use serde::{Deserialize, Serialize};

use crate::asset::{Asset, ReplaceLedger, Stake};
use crate::block::Block;
use crate::config::Config;
use crate::discovery::AddPeer;
use crate::payload::{Peer, Tx, ValidateTx};
use crate::utils::*;
//use crate::logs::SellAsset;

// NOTE these URLs are subject to change
// if tests are failing, come check the URLs
// here
const PEER_ENROLL: &str = "/peers/enroll";
const PEER_DISCOVER_ALL: &str = "/peers/all";
const GET_ALL_ASSETS: &str = "/assets/all";
const SELL_ASSET: &str = "/assets/sell";
const GET_STAKE: &str = "/stake";
const SET_ATTACK: &str = "/attack";
const GET_CHAIN: &str = "/chain/all";
const SEND_VALIDATOR_TX: &str = "/block/validate";

/// Client wrapper for p2p communication
#[derive(Clone, Default)]
pub struct Client {
    pub client: awc,
}

/// Get stake using client
#[derive(Clone, Deserialize, Serialize)]
pub struct GetStake {
    pub block_id: usize,
    pub peer_id: String,
}

impl Client {
    /// enrolls peer with the auditor enode
    pub async fn peer_enroll(&self, config: &Config) {
        let peer = Peer {
            id: config.peer_id.clone(),
            ip: config.public_ip.clone(),
        };
        let addr = Client::make_uri(&config.auditor_node, PEER_ENROLL);
        self.client
            .post(addr)
            .header("content-type", "application/json")
            .send_json(&peer)
            .await
            .unwrap();
    }

    /// set attack
    pub async fn set_attack(&self, config: &Config) {
        for peer in ["attacker", "victim"].iter() {
            config.info(&format!("Setting {} peer in mode", &peer));
            let attack_peer = get_peer(&config, &format!("{}.batsense.net", peer)).await;
            let addr = Client::make_uri(&attack_peer.ip, SET_ATTACK);
            self.client.post(addr).send().await.unwrap();
        }
    }

    /// get stake for a block
    pub async fn get_stake(&self, peer: GetStake, config: &Config) -> Stake {
        use crate::payload::GetStake as PayloadGetStake;

        let payload = PayloadGetStake {
            block_id: peer.block_id,
        };

        let peer_addr = get_peer(&config, &peer.peer_id).await;
        let addr = Client::make_uri(&peer_addr.ip, GET_STAKE);
        loop {
            if let Ok(mut val) = self
                .client
                .post(&addr)
                .header("content-type", "application/json")
                .send_json(&payload)
                .await
            {
                if let Ok(stake) = val.json().await {
                    return stake;
                }
            }
        }
    }

    fn make_uri(address: &str, path: &str) -> String {
        format!("http://{}{}", address, path)
    }

    /// gets list of peers from auditor, should be called periodically
    pub async fn peer_discovery(&self, config: &Config) {
        // gets peers from Auditor and replaces peers
        // in local Network
        let addr = Client::make_uri(&config.auditor_node, PEER_DISCOVER_ALL);
        loop {
            if let Ok(mut val) = self.client.get(&addr).send().await {
                config.debug("Peer discovery request success");
                let peers: Result<Vec<Peer>, _> = val.json().await;
                if let Ok(val) = peers {
                    for peer in val.iter() {
                        config.network_addr.send(AddPeer(peer.to_owned())).await;
                    }
                    break;
                }
            }
        }
    }

    /// gets asset ledger from auditor node, should be called periodically
    pub async fn get_all_assets(&self, config: &Config) {
        // gets assets from Auditor and replaces assets
        // in local AssetsLedger
        let addr = Client::make_uri(&config.auditor_node, GET_ALL_ASSETS);
        loop {
            if let Ok(mut val) = self.client.get(&addr).send().await {
                config.debug("Asset request success");
                let peers: Result<Vec<Asset>, _> = val.json().await;
                if let Ok(val) = peers {
                    config.debug("Asset deserialization success");
                    config.asset_addr.send(ReplaceLedger(val)).await;
                    break;
                }
            }
        }
    }

    /// send Tx request to validator
    pub async fn send_tx_to_validator(&self, validator: &Peer, payload: &ValidateTx) {
        let addr = Client::make_uri(&validator.ip, SEND_VALIDATOR_TX);

        loop {
            if let Ok(_) = self
                .client
                .post(&addr)
                .header("content-type", "application/json")
                .send_json(&payload)
                .await
            {
                break;
            }
        }
    }

    /// send Tx request to validator
    pub async fn sell_asset(&self, config: &Config, seller_id: &str, payload: &Tx) {
        let seller = get_peer(&config, seller_id).await;
        let addr = Client::make_uri(&seller.ip, SELL_ASSET);

        loop {
            if let Ok(_) = self
                .client
                .post(&addr)
                .header("content-type", "application/json")
                .send_json(&payload)
                .await
            {
                break;
            }
        }
    }

    /// Get chain dump
    pub async fn get_chain(&self, config: &Config, peer_id: &str) -> Vec<Block> {
        let peer = get_peer(&config, peer_id).await;
        let addr = Client::make_uri(&peer.ip, GET_CHAIN);

        loop {
            if let Ok(mut val) = self.client.get(&addr).send().await {
                config.debug("Chain dump request success");
                let blocks: Result<Vec<Block>, _> = val.json().await;
                if let Ok(blocks) = blocks {
                    return blocks;
                }
            }
        }
    }

    /// gets list of peers from auditor, should be called periodically
    pub async fn peer_dump(&self, config: &Config) -> Vec<Peer> {
        // gets peers from Auditor and replaces peers
        // in local Network
        let addr = Client::make_uri(&config.auditor_node, PEER_DISCOVER_ALL);
        loop {
            if let Ok(mut val) = self.client.get(&addr).send().await {
                config.debug("Peer discovery request success");
                let peers: Result<Vec<Peer>, _> = val.json().await;
                if let Ok(val) = peers {
                    return val;
                }
            }
        }
    }

    /// gets asset ledger from auditor node, should be called periodically
    pub async fn get_peer_assets(&self, config: &Config, peer: &Peer) {
        // gets assets from Auditor and replaces assets
        // in local AssetsLedger

        let addr = Client::make_uri(&peer.ip, GET_ALL_ASSETS);
        loop {
            if let Ok(mut val) = self.client.get(&addr).send().await {
                config.debug("Asset request success");
                let peers: Result<Vec<Asset>, _> = val.json().await;
                if let Ok(val) = peers {
                    config.debug("Asset deserialization success");
                    config.asset_addr.send(ReplaceLedger(val)).await;
                    break;
                }
            }
        }
    }
}