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
use std::sync::Arc;
use creep::Context;
use futures::channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
use parking_lot::RwLock;
use crate::error::ConsensusError;
use crate::state::process::State;
use crate::types::{Address, Node, OverlordMsg};
use crate::DurationConfig;
use crate::{smr::SMR, timer::Timer};
use crate::{Codec, Consensus, ConsensusResult, Crypto, Wal};
type Pile<T> = RwLock<Option<T>>;
pub struct Overlord<T: Codec, F: Consensus<T>, C: Crypto, W: Wal> {
sender: Pile<UnboundedSender<(Context, OverlordMsg<T>)>>,
state_rx: Pile<UnboundedReceiver<(Context, OverlordMsg<T>)>>,
address: Pile<Address>,
consensus: Pile<Arc<F>>,
crypto: Pile<Arc<C>>,
wal: Pile<Arc<W>>,
}
impl<T, F, C, W> Overlord<T, F, C, W>
where
T: Codec + Send + Sync + 'static,
F: Consensus<T> + 'static,
C: Crypto + Send + Sync + 'static,
W: Wal + 'static,
{
pub fn new(address: Address, consensus: Arc<F>, crypto: Arc<C>, wal: Arc<W>) -> Self {
let (tx, rx) = unbounded();
Overlord {
sender: RwLock::new(Some(tx)),
state_rx: RwLock::new(Some(rx)),
address: RwLock::new(Some(address)),
consensus: RwLock::new(Some(consensus)),
crypto: RwLock::new(Some(crypto)),
wal: RwLock::new(Some(wal)),
}
}
pub fn get_handler(&self) -> OverlordHandler<T> {
let sender = self.sender.write();
assert!(sender.is_some());
let tx = sender.clone().unwrap();
OverlordHandler::new(tx)
}
pub async fn run(
&self,
interval: u64,
authority_list: Vec<Node>,
timer_config: Option<DurationConfig>,
) -> ConsensusResult<()> {
let (mut smr_provider, evt_1, evt_2) = SMR::new();
let smr_handler = smr_provider.take_smr();
let timer = Timer::new(evt_2, smr_handler.clone(), interval, timer_config);
let (rx, mut state, resp) = {
let mut state_rx = self.state_rx.write();
let mut address = self.address.write();
let mut consensus = self.consensus.write();
let mut crypto = self.crypto.write();
let mut wal = self.wal.write();
let tmp_rx = state_rx.take().unwrap();
let (tmp_state, tmp_resp) = State::new(
smr_handler,
address.take().unwrap(),
interval,
authority_list,
consensus.take().unwrap(),
crypto.take().unwrap(),
wal.take().unwrap(),
);
assert!(address.is_none());
assert!(consensus.is_none());
assert!(crypto.is_none());
assert!(state_rx.is_none());
assert!(wal.is_none());
(tmp_rx, tmp_state, tmp_resp)
};
log::info!("Overlord start running");
smr_provider.run();
timer.run();
state.run(rx, evt_1, resp).await;
Ok(())
}
}
#[derive(Clone, Debug)]
pub struct OverlordHandler<T: Codec>(UnboundedSender<(Context, OverlordMsg<T>)>);
impl<T: Codec> OverlordHandler<T> {
fn new(tx: UnboundedSender<(Context, OverlordMsg<T>)>) -> Self {
OverlordHandler(tx)
}
pub fn send_msg(&self, ctx: Context, msg: OverlordMsg<T>) -> ConsensusResult<()> {
if self.0.is_closed() {
panic!("[OverlordHandler]: channel closed");
} else {
self.0
.unbounded_send((ctx, msg))
.map_err(|e| ConsensusError::Other(format!("Send message error {:?}", e)))
}
}
}