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
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* 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/>.
*/
use data_encoding::HEXUPPER;
use sha2::{Digest, Sha256};

use crate::asset::{Asset, AssetLedger, GetAssetInfo, Stake};
use crate::block::Block;
use crate::payload::Peer;
use crate::{Client, Config};

/// helper function for generating sha256 hashes
pub fn hasher(content: &str) -> String {
    let mut hasher = Sha256::new();
    hasher.update(&content);
    let hash = hasher.finalize();
    HEXUPPER.encode(&hash)
}

/// helper function for generating random strings
/// of length = `len`
pub fn get_rand_string(len: usize) -> String {
    use std::iter;

    use rand::distributions::Alphanumeric;
    use rand::{thread_rng, Rng};

    let mut rng = thread_rng();
    iter::repeat(())
        .map(|()| rng.sample(Alphanumeric))
        .map(char::from)
        .take(len)
        .collect()
}

/// helper function to get current timesamp
pub fn get_current_time() -> String {
    use chrono::prelude::*;
    Local::now().to_string()
}

///// helper function to get time as string since UNIX_EPOCH
//pub fn timesamp_to_string(timestamp: Timestamp) -> String {
//    unimplemented!()
//}

/// get stake from all peers in network and get the validator peer
pub async fn consensus(config: &Config, block_id: usize, client: &Client) -> Peer {
    use crate::client::GetStake as ClientGetStake;
    use crate::discovery::DumpPeer;
    let mut stake: Vec<(String, Stake)> = Vec::new();
    let peers = config.network_addr.send(DumpPeer).await.unwrap();

    for peer in peers.iter() {
        let client_payload = ClientGetStake {
            block_id,
            peer_id: peer.id.clone(),
        };
        config.debug(&format!("Requesting stake from peer {}", &peer.id));

        stake.push((
            peer.id.clone(),
            client.get_stake(client_payload, &config).await,
        ));
    }

    // now we have stake of all peers
    // time to calculate validator
    // asset ownership should be
    // verified before calculation
    from_stake_to_validator(&config, stake).await
}

/// get peer utility
pub async fn get_peer(config: &Config, peer_id: &str) -> Peer {
    use crate::discovery::GetPeer;

    config
        .network_addr
        .send(GetPeer(peer_id.into()))
        .await
        .unwrap()
        .unwrap()
}

/// get validator peer from stakes of all peers
async fn from_stake_to_validator(config: &Config, all_stakes: Vec<(String, Stake)>) -> Peer {
    let mut authenticated_stakes: Vec<Asset> = Vec::default();
    for (peer_id, stakes) in all_stakes.iter() {
        for stake in stakes.stake.iter() {
            if let Some(asset) = config
                .asset_addr
                .send(GetAssetInfo(stake.clone()))
                .await
                .unwrap()
            {
                if let Some(owner) = asset.get_owner() {
                    if owner == peer_id {
                        authenticated_stakes.push(asset);
                    }
                }
            }
        }
    }

    config.debug("Ownership verified");
    let mut stake_ledger = AssetLedger::new("stake_ledger");
    stake_ledger.assets = authenticated_stakes;
    let validator_peer_id = stake_ledger.choose_validator().unwrap();
    config.debug(&format!("Validator: {}", &validator_peer_id));

    get_peer(&config, &validator_peer_id).await
}

/// check ownsership utility
pub async fn check_ownership(config: &Config, owner: &str, asset_id: &str) -> bool {
    let asset_info = config
        .asset_addr
        .send(GetAssetInfo(asset_id.into()))
        .await
        .unwrap()
        .unwrap();
    config.debug(&format!("Owner: {:?}", asset_info.get_owner()));
    if let Some(asset_owner) = asset_info.get_owner() {
        if asset_owner == owner {
            config.debug("Ownership verified");
            true
        } else {
            false
        }
    } else {
        false
    }
}

/// get next block ID utility
pub async fn get_next_block_id(config: &Config) -> usize {
    use crate::chain::GetLastBlock;
    let current_block = config.chain_addr.send(GetLastBlock).await.unwrap();

    if current_block.get_serial_no().unwrap() == 0 {
        config.init_network_size + 1
    } else {
        current_block.get_serial_no().unwrap() + 1
    }
}

/// add block utility. Performs the following steps:
/// 1. Change asset ownership
/// 2. mutate validation assets and sold last transaction
/// 3. add block to chain
pub async fn add_block_runner(config: &Config, client: &Client, block: &Block) {
    use crate::asset::{ChangeAssetOwnerBuilder, SetLastTransationBuilder};
    use crate::chain::AddBlock;
    use crate::client::GetStake as ClientGetStake;

    let next_block_id = get_next_block_id(&config).await;

    // changing asset ownsership
    config.debug(&format!(
        "Chainging ownership of asset: {}",
        block.get_asset_id().unwrap()
    ));
    let change_ownsership_msg = ChangeAssetOwnerBuilder::default()
        .asset_id(block.get_asset_id().unwrap().into())
        .new_owner(block.get_rx().unwrap().into())
        .build()
        .unwrap();
    config.asset_addr.send(change_ownsership_msg).await.unwrap();

    // changing coinage of the asset transacted
    config.debug(&format!(
        "Chainging coinage of asset: {}",
        block.get_asset_id().unwrap()
    ));
    let change_tx_msg = SetLastTransationBuilder::default()
        .tx(next_block_id)
        .asset_id(block.get_asset_id().unwrap().into())
        .build()
        .unwrap();
    config.asset_addr.send(change_tx_msg).await.unwrap();

    // changing coinage of the assets staked by the validator
    let client_payload = ClientGetStake {
        peer_id: block.get_validator().unwrap().into(),
        block_id: next_block_id,
    };
    let validator_stakes = client.get_stake(client_payload, &config).await;
    for asset_id in validator_stakes.stake.iter() {
        config.debug(&format!("Chainging coinage of asset: {}", &asset_id));

        let change_tx_msg = SetLastTransationBuilder::default()
            .tx(next_block_id)
            .asset_id(asset_id.into())
            .build()
            .unwrap();
        config.asset_addr.send(change_tx_msg).await.unwrap();
    }

    // adding block to chain
    config.info(&format!("Adding block {} to chain", block.get_hash()));
    config
        .chain_addr
        .send(AddBlock(block.to_owned(), config.init_network_size))
        .await
        .unwrap()
        .unwrap();
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::config::Mode;
    use crate::helpers::*;

    #[actix_rt::test]
    async fn consensus_works() {
        let config = init_network(Mode::Normal).await;
        let client = Client::default();
        non_register_bootstrap(&config, &client).await;

        let validator = consensus(&config, 1, &client).await;
        assert_eq!(validator.id, "victim.batsense.net");
    }

    #[actix_rt::test]
    async fn get_next_block_id_works() {
        let config = init_network(Mode::Normal).await;
        assert_eq!(get_next_block_id(&config).await, 4)
    }
}