|
1 |
| -use futures::future::FutureExt; |
2 |
| -use primitives::validator::{Validator, ValidatorFuture}; |
3 |
| -use primitives::Channel; |
| 1 | +use std::error::Error; |
4 | 2 |
|
5 |
| -#[derive(Clone)] |
6 |
| -pub struct Follower {} |
| 3 | +use primitives::adapter::Adapter; |
| 4 | +use primitives::validator::{MessageTypes, NewState}; |
| 5 | +use primitives::BalancesMap; |
7 | 6 |
|
8 |
| -impl Validator for Follower { |
9 |
| - fn tick(&self, _channel: Channel) -> ValidatorFuture<()> { |
10 |
| - futures::future::ok(()).boxed() |
| 7 | +use crate::heartbeat::heartbeat; |
| 8 | +use crate::sentry_interface::SentryApi; |
| 9 | +use crate::{get_state_root_hash, producer}; |
| 10 | + |
| 11 | +pub async fn tick<A: Adapter + 'static>(iface: &SentryApi<A>) -> Result<(), Box<dyn Error>> { |
| 12 | + let from = iface.channel.spec.validators.leader().id.clone(); |
| 13 | + let new_msg_response = await!(iface.get_latest_msg(from, "NewState".to_string()))?; |
| 14 | + let new_msg = new_msg_response |
| 15 | + .msg |
| 16 | + .get(0) |
| 17 | + .and_then(|message_types| match message_types { |
| 18 | + MessageTypes::NewState(new_state) => Some(new_state.clone()), |
| 19 | + _ => None, |
| 20 | + }); |
| 21 | + let our_latest_msg_response = |
| 22 | + await!(iface.get_our_latest_msg("ApproveState+RejectState".to_string()))?; |
| 23 | + let our_latest_msg_state_root = our_latest_msg_response |
| 24 | + .msg |
| 25 | + .get(0) |
| 26 | + .and_then(|message_types| match message_types { |
| 27 | + MessageTypes::ApproveState(approve_state) => Some(approve_state.state_root.clone()), |
| 28 | + MessageTypes::RejectState(reject_state) => Some(reject_state.state_root.clone()), |
| 29 | + _ => None, |
| 30 | + }); |
| 31 | + |
| 32 | + let latest_is_responded_to = match (&new_msg, &our_latest_msg_state_root) { |
| 33 | + (Some(new_msg), Some(state_root)) => &new_msg.state_root == state_root, |
| 34 | + (_, _) => false, |
| 35 | + }; |
| 36 | + |
| 37 | + let (balances, _) = await!(producer::tick(&iface))?; |
| 38 | + |
| 39 | + match (new_msg, latest_is_responded_to) { |
| 40 | + (Some(new_state), false) => on_new_state(&iface, &balances, &new_state)?, |
| 41 | + (_, _) => {} |
11 | 42 | }
|
| 43 | + |
| 44 | + // TODO: Pass the heartbeat time from the Configuration |
| 45 | + await!(heartbeat(&iface, balances, 250)).map(|_| ()) |
| 46 | +} |
| 47 | + |
| 48 | +fn on_new_state<A: Adapter + 'static>( |
| 49 | + _iface: &SentryApi<A>, |
| 50 | + _balances: &BalancesMap, |
| 51 | + _new_state: &NewState, |
| 52 | +) -> Result<(), Box<dyn Error>> { |
| 53 | + unimplemented!(); |
12 | 54 | }
|
0 commit comments