-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplica-local.rs
65 lines (56 loc) · 2 KB
/
replica-local.rs
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
mod common;
use common::*;
use bafomet::bft::async_runtime as rt;
use bafomet::bft::collections::HashMap;
use bafomet::bft::communication::NodeId;
use bafomet::bft::crypto::signature::{KeyPair, PublicKey};
use bafomet::bft::threadpool;
use bafomet::bft::{init, InitConfig};
fn main() {
let conf = InitConfig {
async_threads: num_cpus::get(),
};
let _guard = unsafe { init(conf).unwrap() };
rt::block_on(async_main());
}
async fn async_main() {
let mut secret_keys: HashMap<NodeId, KeyPair> = sk_stream()
.take(4)
.enumerate()
.map(|(id, sk)| (NodeId::from(id), sk))
.collect();
let public_keys: HashMap<NodeId, PublicKey> = secret_keys
.iter()
.map(|(id, sk)| (*id, sk.public_key().into()))
.collect();
let pool = threadpool::Builder::new().num_threads(4).build();
for id in NodeId::targets(0..4) {
let addrs = map! {
// replicas
NodeId::from(0u32) => addr!("cop01" => "127.0.0.1:10001"),
NodeId::from(1u32) => addr!("cop02" => "127.0.0.1:10002"),
NodeId::from(2u32) => addr!("cop03" => "127.0.0.1:10003"),
NodeId::from(3u32) => addr!("cop04" => "127.0.0.1:10004"),
// clients
NodeId::from(1000u32) => addr!("cli1000" => "127.0.0.1:11000")
};
let sk = secret_keys.remove(&id).unwrap();
let fut = setup_replica(pool.clone(), id, sk, addrs, public_keys.clone());
rt::spawn(async move {
println!("Bootstrapping replica #{}", u32::from(id));
let mut replica = fut.await.unwrap();
println!("Running replica #{}", u32::from(id));
replica.run().await.unwrap();
});
}
drop((pool, secret_keys, public_keys));
// run forever
std::future::pending().await
}
fn sk_stream() -> impl Iterator<Item = KeyPair> {
std::iter::repeat_with(|| {
// only valid for ed25519!
let buf = [0; 32];
KeyPair::from_bytes(&buf[..]).unwrap()
})
}