Skip to content

Commit

Permalink
Supervisor follow-up (#327)
Browse files Browse the repository at this point in the history
* Document the supervisor

* Rename LightStore::latest to LightStore::highest

* Rename AtHeight::Latest to AtHeight::Highest
  • Loading branch information
romac committed Jun 16, 2020
1 parent ca22a6d commit 1df3ac3
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 29 deletions.
2 changes: 1 addition & 1 deletion light-client/examples/light_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn make_instance(

light_store.insert(trusted_state, VerifiedStatus::Verified);
} else {
if light_store.latest(VerifiedStatus::Verified).is_none() {
if light_store.highest(VerifiedStatus::Verified).is_none() {
println!("[ error ] no trusted state in database, please specify a trusted header");
std::process::exit(1);
}
Expand Down
8 changes: 4 additions & 4 deletions light-client/src/components/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ use crate::{

pub enum AtHeight {
At(Height),
Latest,
Highest,
}

impl From<Height> for AtHeight {
fn from(height: Height) -> Self {
if height == 0 {
Self::Latest
Self::Highest
} else {
Self::At(height)
}
Expand Down Expand Up @@ -110,7 +110,7 @@ impl ProdIo {
let res = block_on(
async {
match height {
AtHeight::Latest => rpc_client.latest_commit().await,
AtHeight::Highest => rpc_client.latest_commit().await,
AtHeight::At(height) => rpc_client.commit(height).await,
}
},
Expand All @@ -131,7 +131,7 @@ impl ProdIo {
height: AtHeight,
) -> Result<TMValidatorSet, IoError> {
let height = match height {
AtHeight::Latest => bail!(IoError::InvalidHeight(
AtHeight::Highest => bail!(IoError::InvalidHeight(
"given height must be greater than 0".to_string()
)),
AtHeight::At(height) => height,
Expand Down
18 changes: 9 additions & 9 deletions light-client/src/components/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub trait Scheduler: Send {
///
/// ## Postcondition
/// - The resulting height must be valid according to `valid_schedule`. [LCV-SCHEDULE-POST.1]
#[pre(light_store.latest(VerifiedStatus::Verified).is_some())]
#[pre(light_store.highest(VerifiedStatus::Verified).is_some())]
#[post(valid_schedule(ret, target_height, current_height, light_store))]
fn schedule(
&self,
Expand Down Expand Up @@ -51,23 +51,23 @@ where
///
/// ## Postcondition
/// - The resulting height must be valid according to `valid_schedule`. [LCV-SCHEDULE-POST.1]
#[pre(light_store.latest(VerifiedStatus::Verified).is_some())]
#[pre(light_store.highest(VerifiedStatus::Verified).is_some())]
#[post(valid_schedule(ret, target_height, current_height, light_store))]
pub fn basic_bisecting_schedule(
light_store: &dyn LightStore,
current_height: Height,
target_height: Height,
) -> Height {
let latest_trusted_height = light_store
.latest(VerifiedStatus::Verified)
let trusted_height = light_store
.highest(VerifiedStatus::Verified)
.map(|lb| lb.height())
.unwrap();

if latest_trusted_height == current_height && latest_trusted_height < target_height {
if trusted_height == current_height && trusted_height < target_height {
target_height
} else if latest_trusted_height < current_height && latest_trusted_height < target_height {
midpoint(latest_trusted_height, current_height)
} else if latest_trusted_height == target_height {
} else if trusted_height < current_height && trusted_height < target_height {
midpoint(trusted_height, current_height)
} else if trusted_height == target_height {
target_height
} else {
midpoint(current_height, target_height)
Expand Down Expand Up @@ -103,7 +103,7 @@ pub fn valid_schedule(
light_store: &dyn LightStore,
) -> bool {
let latest_trusted_height = light_store
.latest(VerifiedStatus::Verified)
.highest(VerifiedStatus::Verified)
.map(|lb| lb.height())
.unwrap();

Expand Down
4 changes: 2 additions & 2 deletions light-client/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub enum ErrorKind {
#[error("no witnesses")]
NoWitnesses,

#[error("no witness left")]
NoWitnessLeft,
#[error("no valid peer left")]
NoValidPeerLeft,

#[error("fork detected peers={0:?}")]
ForkDetected(Vec<PeerId>),
Expand Down
10 changes: 5 additions & 5 deletions light-client/src/light_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ impl LightClient {
}
}

/// Attempt to update the light client to the latest block of the primary node.
/// Attempt to update the light client to the highest block of the primary node.
///
/// Note: This functin delegates the actual work to `verify_to_target`.
pub fn verify_to_highest(&mut self, state: &mut State) -> Result<LightBlock, Error> {
let target_block = match self.io.fetch_light_block(self.peer, AtHeight::Latest) {
let target_block = match self.io.fetch_light_block(self.peer, AtHeight::Highest) {
Ok(last_block) => last_block,
Err(io_error) => bail!(ErrorKind::Io(io_error)),
};
Expand Down Expand Up @@ -165,10 +165,10 @@ impl LightClient {
let mut current_height = target_height;

loop {
// Get the latest trusted state
// Get the highest trusted state
let trusted_state = state
.light_store
.latest(VerifiedStatus::Verified)
.highest(VerifiedStatus::Verified)
.ok_or_else(|| ErrorKind::NoInitialTrustedState)?;

// Check invariant [LCV-INV-TP.1]
Expand Down Expand Up @@ -214,7 +214,7 @@ impl LightClient {
Verdict::NotEnoughTrust(_) => {
// The current block cannot be trusted because of missing overlap in the validator sets.
// Add the block to the light store with `unverified` status.
// This will engage bisection in an attempt to raise the height of the latest
// This will engage bisection in an attempt to raise the height of the highest
// trusted state until there is enough overlap.
state
.light_store
Expand Down
2 changes: 1 addition & 1 deletion light-client/src/peer_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl PeerList {
}
}

bail!(ErrorKind::NoWitnessLeft)
bail!(ErrorKind::NoValidPeerLeft)
}
}

Expand Down
2 changes: 1 addition & 1 deletion light-client/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub trait LightStore: std::fmt::Debug + Send {
/// Remove the light block with the given height and status, if any.
fn remove(&mut self, height: Height, status: VerifiedStatus);
/// Get the highest light block with the given status.
fn latest(&self, status: VerifiedStatus) -> Option<LightBlock>;
fn highest(&self, status: VerifiedStatus) -> Option<LightBlock>;
/// Get an iterator of all light blocks with the given status.
fn all(&self, status: VerifiedStatus) -> Box<dyn Iterator<Item = LightBlock>>;
}
2 changes: 1 addition & 1 deletion light-client/src/store/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl LightStore for MemoryStore {
self.insert(light_block, status);
}

fn latest(&self, status: VerifiedStatus) -> Option<LightBlock> {
fn highest(&self, status: VerifiedStatus) -> Option<LightBlock> {
self.store
.iter()
.rev()
Expand Down
2 changes: 1 addition & 1 deletion light-client/src/store/sled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl LightStore for SledStore {
self.db(status).remove(&self.db, &height).ok();
}

fn latest(&self, status: VerifiedStatus) -> Option<LightBlock> {
fn highest(&self, status: VerifiedStatus) -> Option<LightBlock> {
self.db(status).iter(&self.db).next_back()
}

Expand Down
Loading

0 comments on commit 1df3ac3

Please sign in to comment.