Make the Inbound invariant clearer

This commit is contained in:
teor 2021-01-22 23:02:30 +10:00
parent 2a25b9ee72
commit 4d1a2fd02e
1 changed files with 14 additions and 11 deletions

View File

@ -53,7 +53,9 @@ pub type SetupData = (Outbound, Arc<Mutex<AddressBook>>);
/// responding to block gossip by attempting to download and validate advertised /// responding to block gossip by attempting to download and validate advertised
/// blocks. /// blocks.
pub struct Inbound { pub struct Inbound {
// invariant: address_book and downloads are Some if network_setup or verifier are None // invariants:
// * Before setup: address_book and downloads are None, and the *_setup members are Some
// * After setup: address_book and downloads are Some, and the *_setup members are None
// //
// why not use an enum for the inbound state? because it would mean // why not use an enum for the inbound state? because it would mean
// match-wrapping the body of Service::call rather than just expect()ing // match-wrapping the body of Service::call rather than just expect()ing
@ -70,10 +72,10 @@ pub struct Inbound {
/// after the network is set up. /// after the network is set up.
/// ///
/// `None` after the network is set up and `downloads` is created. /// `None` after the network is set up and `downloads` is created.
verifier: Option<Verifier>, verifier_setup: Option<Verifier>,
// Services // Services and Data Stores
/// A service that maintains a list of peer addresses. /// A shared list of peer addresses.
/// ///
/// `None` until the network is set up. /// `None` until the network is set up.
address_book: Option<Arc<Mutex<zn::AddressBook>>>, address_book: Option<Arc<Mutex<zn::AddressBook>>>,
@ -95,7 +97,7 @@ impl Inbound {
) -> Self { ) -> Self {
Self { Self {
network_setup: Some(network_setup), network_setup: Some(network_setup),
verifier: Some(verifier), verifier_setup: Some(verifier),
address_book: None, address_book: None,
downloads: None, downloads: None,
state, state,
@ -119,17 +121,18 @@ impl Service<zn::Request> for Inbound {
use oneshot::error::TryRecvError; use oneshot::error::TryRecvError;
match rx.try_recv() { match rx.try_recv() {
Ok((outbound, address_book)) => { Ok((outbound, address_book)) => {
let verifier = self
.verifier_setup
.take()
.expect("unexpected missing verifier during inbound network setup");
self.address_book = Some(address_book); self.address_book = Some(address_book);
self.downloads = Some(Box::pin(Downloads::new( self.downloads = Some(Box::pin(Downloads::new(
Timeout::new(outbound, BLOCK_DOWNLOAD_TIMEOUT), Timeout::new(outbound, BLOCK_DOWNLOAD_TIMEOUT),
Timeout::new( Timeout::new(verifier, BLOCK_VERIFY_TIMEOUT),
self.verifier
.take()
.expect("verifier is Some when network_setup is Some"),
BLOCK_VERIFY_TIMEOUT,
),
self.state.clone(), self.state.clone(),
))); )));
self.network_setup = None; self.network_setup = None;
} }
Err(TryRecvError::Empty) => { Err(TryRecvError::Empty) => {