Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
Change return type for verify_consensus (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wizdave97 authored Jul 17, 2023
1 parent 93271cb commit a5c6405
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
6 changes: 3 additions & 3 deletions ismp-testsuite/src/mocks.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use ismp::{
consensus::{
ConsensusClient, ConsensusClientId, ConsensusStateId, StateCommitment, StateMachineClient,
StateMachineHeight, StateMachineId,
StateMachineHeight, StateMachineId, VerifiedCommitments,
},
error::Error,
host::{IsmpHost, StateMachine},
messaging::{Proof, StateCommitmentHeight},
messaging::Proof,
module::IsmpModule,
router::{
DispatchRequest, Get, IsmpDispatcher, IsmpRouter, Post, PostResponse, Request,
Expand Down Expand Up @@ -39,7 +39,7 @@ impl ConsensusClient for MockClient {
_consensus_state_id: ConsensusStateId,
_trusted_consensus_state: Vec<u8>,
_proof: Vec<u8>,
) -> Result<(Vec<u8>, BTreeMap<StateMachine, StateCommitmentHeight>), Error> {
) -> Result<(Vec<u8>, VerifiedCommitments), Error> {
Ok(Default::default())
}

Expand Down
5 changes: 4 additions & 1 deletion ismp/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ pub struct StateMachineHeight {
pub height: u64,
}

/// A map of state machine to verified state commitments
pub type VerifiedCommitments = BTreeMap<StateMachine, Vec<StateCommitmentHeight>>;

/// We define the consensus client as a module that handles logic for consensus proof verification,
/// and State-Proof verification as well.
pub trait ConsensusClient {
Expand All @@ -99,7 +102,7 @@ pub trait ConsensusClient {
consensus_state_id: ConsensusStateId,
trusted_consensus_state: Vec<u8>,
proof: Vec<u8>,
) -> Result<(Vec<u8>, BTreeMap<StateMachine, StateCommitmentHeight>), Error>;
) -> Result<(Vec<u8>, VerifiedCommitments), Error>;

/// Given two distinct consensus proofs, verify that they're both valid and represent
/// conflicting views of the network. returns Ok(()) if they're both valid.
Expand Down
45 changes: 25 additions & 20 deletions ismp/src/handlers/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,36 @@ where
let timestamp = host.timestamp();
host.store_consensus_update_time(msg.consensus_state_id, timestamp)?;
let mut state_updates = BTreeSet::new();
for (id, commitment_height) in intermediate_states {
for (id, mut commitment_heights) in intermediate_states {
commitment_heights.sort_unstable_by(|a, b| a.height.cmp(&b.height));
let id = StateMachineId { state_id: id, consensus_state_id: msg.consensus_state_id };
let state_height = StateMachineHeight { id, height: commitment_height.height };
// If a state machine is frozen, we skip it
if host.is_state_machine_frozen(state_height).is_err() {
continue
}

let previous_latest_height = host.latest_commitment_height(id)?;

// Only allow heights greater than latest height
if previous_latest_height > commitment_height.height {
continue
for commitment_height in commitment_heights.iter() {
let state_height = StateMachineHeight { id, height: commitment_height.height };
// If a state machine is frozen, we skip it
if host.is_state_machine_frozen(state_height).is_err() {
continue
}

// Only allow heights greater than latest height
if previous_latest_height > commitment_height.height {
continue
}

// Skip duplicate states
if host.state_machine_commitment(state_height).is_ok() {
continue
}

host.store_state_machine_commitment(state_height, commitment_height.commitment)?;
}

// Skip duplicate states
if host.state_machine_commitment(state_height).is_ok() {
continue
if let Some(latest_height) = commitment_heights.last() {
let latest_height = StateMachineHeight { id, height: latest_height.height };
state_updates
.insert((StateMachineHeight { id, height: previous_latest_height }, latest_height));
host.store_latest_commitment_height(latest_height)?;
}

host.store_state_machine_commitment(state_height, commitment_height.commitment)?;

state_updates
.insert((StateMachineHeight { id, height: previous_latest_height }, state_height));
host.store_latest_commitment_height(state_height)?;
}

let result = ConsensusUpdateResult {
Expand Down

0 comments on commit a5c6405

Please sign in to comment.