feat(miniprotocols): Allow graceful exit on chainsync and blockfetch (#83)
This commit is contained in:
parent
90ef5f3ab7
commit
90ad1e9217
2 changed files with 86 additions and 25 deletions
|
|
@ -1,5 +1,3 @@
|
||||||
use std::sync::mpsc::Receiver;
|
|
||||||
|
|
||||||
use crate::machines::{Agent, MachineOutput, Transition};
|
use crate::machines::{Agent, MachineOutput, Transition};
|
||||||
|
|
||||||
use crate::common::Point;
|
use crate::common::Point;
|
||||||
|
|
@ -206,20 +204,22 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct OnDemandClient<O>
|
pub struct OnDemandClient<I, O>
|
||||||
where
|
where
|
||||||
|
I: Iterator<Item = Point>,
|
||||||
O: Observer,
|
O: Observer,
|
||||||
{
|
{
|
||||||
pub state: State,
|
pub state: State,
|
||||||
pub requests: Receiver<Point>,
|
pub requests: I,
|
||||||
pub observer: O,
|
pub observer: O,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<O> OnDemandClient<O>
|
impl<I, O> OnDemandClient<I, O>
|
||||||
where
|
where
|
||||||
|
I: Iterator<Item = Point>,
|
||||||
O: Observer,
|
O: Observer,
|
||||||
{
|
{
|
||||||
pub fn initial(requests: Receiver<Point>, observer: O) -> Self {
|
pub fn initial(requests: I, observer: O) -> Self {
|
||||||
Self {
|
Self {
|
||||||
state: State::Idle,
|
state: State::Idle,
|
||||||
requests,
|
requests,
|
||||||
|
|
@ -227,8 +227,8 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wait_for_request_and_send(self, tx: &impl MachineOutput) -> Transition<Self> {
|
fn send_request_range(self, tx: &impl MachineOutput, point: Point) -> Transition<Self> {
|
||||||
let point = self.requests.recv()?;
|
log::debug!("requesting block {:?}", point);
|
||||||
|
|
||||||
let msg = Message::RequestRange {
|
let msg = Message::RequestRange {
|
||||||
range: (point.clone(), point),
|
range: (point.clone(), point),
|
||||||
|
|
@ -242,6 +242,24 @@ where
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn dropout(self) -> Transition<Self> {
|
||||||
|
log::debug!("dropping out, channel will remain open");
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
state: State::Done,
|
||||||
|
..self
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn wait_for_request_and_send(mut self, tx: &impl MachineOutput) -> Transition<Self> {
|
||||||
|
let point = self.requests.next();
|
||||||
|
|
||||||
|
match point {
|
||||||
|
Some(x) => self.send_request_range(tx, x),
|
||||||
|
None => self.dropout(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn on_block(self, body: Vec<u8>) -> Transition<Self> {
|
fn on_block(self, body: Vec<u8>) -> Transition<Self> {
|
||||||
log::debug!("received block body, size {}", body.len());
|
log::debug!("received block body, size {}", body.len());
|
||||||
|
|
||||||
|
|
@ -251,16 +269,15 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<O> Agent for OnDemandClient<O>
|
impl<I, O> Agent for OnDemandClient<I, O>
|
||||||
where
|
where
|
||||||
|
I: Iterator<Item = Point>,
|
||||||
O: Observer,
|
O: Observer,
|
||||||
{
|
{
|
||||||
type Message = Message;
|
type Message = Message;
|
||||||
|
|
||||||
// we're never done because we react to external work requests.
|
|
||||||
// TODO: see if we can inspect mpsc channel status and stop if disconnected
|
|
||||||
fn is_done(&self) -> bool {
|
fn is_done(&self) -> bool {
|
||||||
false
|
self.state == State::Done
|
||||||
}
|
}
|
||||||
|
|
||||||
fn has_agency(&self) -> bool {
|
fn has_agency(&self) -> bool {
|
||||||
|
|
|
||||||
|
|
@ -9,34 +9,44 @@ use crate::common::Point;
|
||||||
|
|
||||||
use super::{BlockContent, HeaderContent, Message, SkippedContent, State, Tip};
|
use super::{BlockContent, HeaderContent, Message, SkippedContent, State, Tip};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Continuation {
|
||||||
|
Proceed,
|
||||||
|
DropOut,
|
||||||
|
Done,
|
||||||
|
}
|
||||||
|
|
||||||
/// An observer of chain-sync events sent by the state-machine
|
/// An observer of chain-sync events sent by the state-machine
|
||||||
pub trait Observer<C> {
|
pub trait Observer<C> {
|
||||||
fn on_roll_forward(
|
fn on_roll_forward(
|
||||||
&mut self,
|
&mut self,
|
||||||
_content: C,
|
_content: C,
|
||||||
tip: &Tip,
|
tip: &Tip,
|
||||||
) -> Result<(), Box<dyn std::error::Error>> {
|
) -> Result<Continuation, Box<dyn std::error::Error>> {
|
||||||
log::debug!("asked to roll forward, tip at {:?}", tip);
|
log::debug!("asked to roll forward, tip at {:?}", tip);
|
||||||
|
|
||||||
Ok(())
|
Ok(Continuation::Proceed)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_intersect_found(
|
fn on_intersect_found(
|
||||||
&mut self,
|
&mut self,
|
||||||
point: &Point,
|
point: &Point,
|
||||||
tip: &Tip,
|
tip: &Tip,
|
||||||
) -> Result<(), Box<dyn std::error::Error>> {
|
) -> Result<Continuation, Box<dyn std::error::Error>> {
|
||||||
log::debug!("intersect was found {:?} (tip: {:?})", point, tip);
|
log::debug!("intersect was found {:?} (tip: {:?})", point, tip);
|
||||||
Ok(())
|
|
||||||
|
Ok(Continuation::Proceed)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_rollback(&mut self, point: &Point) -> Result<(), Box<dyn std::error::Error>> {
|
fn on_rollback(&mut self, point: &Point) -> Result<Continuation, Box<dyn std::error::Error>> {
|
||||||
log::debug!("asked to roll back {:?}", point);
|
log::debug!("asked to roll back {:?}", point);
|
||||||
Ok(())
|
|
||||||
|
Ok(Continuation::Proceed)
|
||||||
}
|
}
|
||||||
fn on_tip_reached(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
fn on_tip_reached(&mut self) -> Result<Continuation, Box<dyn std::error::Error>> {
|
||||||
log::debug!("tip was reached");
|
log::debug!("tip was reached");
|
||||||
Ok(())
|
|
||||||
|
Ok(Continuation::Proceed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -55,6 +65,8 @@ where
|
||||||
pub intersect: Option<Point>,
|
pub intersect: Option<Point>,
|
||||||
pub tip: Option<Tip>,
|
pub tip: Option<Tip>,
|
||||||
|
|
||||||
|
continuation: Continuation,
|
||||||
|
|
||||||
observer: O,
|
observer: O,
|
||||||
|
|
||||||
_phantom: PhantomData<C>,
|
_phantom: PhantomData<C>,
|
||||||
|
|
@ -71,6 +83,7 @@ where
|
||||||
intersect: None,
|
intersect: None,
|
||||||
tip: None,
|
tip: None,
|
||||||
known_points,
|
known_points,
|
||||||
|
continuation: Continuation::Proceed,
|
||||||
observer,
|
observer,
|
||||||
_phantom: PhantomData::default(),
|
_phantom: PhantomData::default(),
|
||||||
}
|
}
|
||||||
|
|
@ -107,15 +120,38 @@ where
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn send_done(self, tx: &impl MachineOutput) -> Transition<Self> {
|
||||||
|
log::debug!("notifying done");
|
||||||
|
|
||||||
|
let msg = Message::<C>::Done;
|
||||||
|
|
||||||
|
tx.send_msg(&msg)?;
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
state: State::Done,
|
||||||
|
..self
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn dropout(self) -> Transition<Self> {
|
||||||
|
log::debug!("dropping out, channel will keep open");
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
state: State::Done,
|
||||||
|
..self
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn on_intersect_found(mut self, point: Point, tip: Tip) -> Transition<Self> {
|
fn on_intersect_found(mut self, point: Point, tip: Tip) -> Transition<Self> {
|
||||||
log::debug!("intersect found: {:?} (tip: {:?})", point, tip);
|
log::debug!("intersect found: {:?} (tip: {:?})", point, tip);
|
||||||
|
|
||||||
self.observer.on_intersect_found(&point, &tip)?;
|
let continuation = self.observer.on_intersect_found(&point, &tip)?;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
tip: Some(tip),
|
tip: Some(tip),
|
||||||
intersect: Some(point),
|
intersect: Some(point),
|
||||||
state: State::Idle,
|
state: State::Idle,
|
||||||
|
continuation,
|
||||||
..self
|
..self
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -134,11 +170,12 @@ where
|
||||||
fn on_roll_forward(mut self, content: C, tip: Tip) -> Transition<Self> {
|
fn on_roll_forward(mut self, content: C, tip: Tip) -> Transition<Self> {
|
||||||
log::debug!("rolling forward");
|
log::debug!("rolling forward");
|
||||||
|
|
||||||
self.observer.on_roll_forward(content, &tip)?;
|
let continuation = self.observer.on_roll_forward(content, &tip)?;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
tip: Some(tip),
|
tip: Some(tip),
|
||||||
state: State::Idle,
|
state: State::Idle,
|
||||||
|
continuation,
|
||||||
..self
|
..self
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -146,13 +183,13 @@ where
|
||||||
fn on_roll_backward(mut self, point: Point, tip: Tip) -> Transition<Self> {
|
fn on_roll_backward(mut self, point: Point, tip: Tip) -> Transition<Self> {
|
||||||
log::debug!("rolling backward to point: {:?}", point);
|
log::debug!("rolling backward to point: {:?}", point);
|
||||||
|
|
||||||
log::debug!("reporting rollback to observer");
|
let continuation = self.observer.on_rollback(&point)?;
|
||||||
self.observer.on_rollback(&point)?;
|
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
tip: Some(tip),
|
tip: Some(tip),
|
||||||
intersect: Some(point),
|
intersect: Some(point),
|
||||||
state: State::Idle,
|
state: State::Idle,
|
||||||
|
continuation,
|
||||||
..self
|
..self
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -160,10 +197,11 @@ where
|
||||||
fn on_await_reply(mut self) -> Transition<Self> {
|
fn on_await_reply(mut self) -> Transition<Self> {
|
||||||
log::debug!("reached tip, await reply");
|
log::debug!("reached tip, await reply");
|
||||||
|
|
||||||
self.observer.on_tip_reached()?;
|
let continuation = self.observer.on_tip_reached()?;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
state: State::MustReply,
|
state: State::MustReply,
|
||||||
|
continuation,
|
||||||
..self
|
..self
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -192,6 +230,12 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_next(self, tx: &impl MachineOutput) -> Transition<Self> {
|
fn send_next(self, tx: &impl MachineOutput) -> Transition<Self> {
|
||||||
|
match self.continuation {
|
||||||
|
Continuation::Done => return self.send_done(tx),
|
||||||
|
Continuation::DropOut => return self.dropout(),
|
||||||
|
_ => (),
|
||||||
|
};
|
||||||
|
|
||||||
match self.state {
|
match self.state {
|
||||||
State::Idle => match self.intersect {
|
State::Idle => match self.intersect {
|
||||||
// keep going from pointer
|
// keep going from pointer
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue