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

Commit

Permalink
state machine height update time
Browse files Browse the repository at this point in the history
  • Loading branch information
Wizdave97 committed Aug 7, 2023
1 parent 59d8371 commit eab698f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
5 changes: 3 additions & 2 deletions ismp-testsuite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn check_challenge_period<H: IsmpHost>(host: &H) -> Result<(), &'static str>
let challenge_period = host.challenge_period(mock_consensus_state_id()).unwrap();
let previous_update_time = host.timestamp() - (challenge_period / 2);
host.store_consensus_update_time(mock_consensus_state_id(), previous_update_time).unwrap();

host.store_state_machine_update_time(intermediate_state.height, previous_update_time).unwrap();
let res = handle_incoming_message::<H>(host, consensus_message);
assert!(matches!(res, Err(ismp::error::Error::ChallengePeriodNotElapsed { .. })));

Expand Down Expand Up @@ -147,7 +147,7 @@ pub fn frozen_check<H: IsmpHost>(host: &H) -> Result<(), &'static str> {
let challenge_period = host.challenge_period(mock_consensus_state_id()).unwrap();
let previous_update_time = host.timestamp() - (challenge_period * 2);
host.store_consensus_update_time(mock_consensus_state_id(), previous_update_time).unwrap();

host.store_state_machine_update_time(intermediate_state.height, previous_update_time).unwrap();
let frozen_height = StateMachineHeight {
id: intermediate_state.height.id,
height: intermediate_state.height.height - 1,
Expand Down Expand Up @@ -205,6 +205,7 @@ pub fn timeout_post_processing_check<H: IsmpHost>(
let challenge_period = host.challenge_period(mock_consensus_state_id()).unwrap();
let previous_update_time = host.timestamp() - (challenge_period * 2);
host.store_consensus_update_time(mock_consensus_state_id(), previous_update_time).unwrap();
host.store_state_machine_update_time(intermediate_state.height, previous_update_time).unwrap();
let dispatch_post = DispatchPost {
dest: StateMachine::Kusama(2000),
from: vec![0u8; 32],
Expand Down
19 changes: 19 additions & 0 deletions ismp-testsuite/src/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ impl IsmpHost for Host {
.ok_or_else(|| Error::ImplementationSpecific("Consensus update time not found".into()))
}

fn state_machine_update_time(
&self,
state_machine_height: StateMachineHeight,
) -> Result<Duration, Error> {
self.consensus_update_time
.borrow()
.get(&state_machine_height.id.consensus_state_id)
.copied()
.ok_or_else(|| Error::ImplementationSpecific("Consensus update time not found".into()))
}

fn consensus_client_id(
&self,
consensus_state_id: ConsensusStateId,
Expand Down Expand Up @@ -224,6 +235,14 @@ impl IsmpHost for Host {
Ok(())
}

fn store_state_machine_update_time(
&self,
_state_machine_height: StateMachineHeight,
_timestamp: Duration,
) -> Result<(), Error> {
Ok(())
}

fn store_state_machine_commitment(
&self,
height: StateMachineHeight,
Expand Down
4 changes: 2 additions & 2 deletions ismp/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn verify_delay_passed<H>(host: &H, proof_height: &StateMachineHeight) -> Result
where
H: IsmpHost,
{
let update_time = host.consensus_update_time(proof_height.id.consensus_state_id)?;
let update_time = host.state_machine_update_time(*proof_height)?;
let delay_period = host.challenge_period(proof_height.id.consensus_state_id).ok_or(
Error::ChallengePeriodNotConfigured {
consensus_state_id: proof_height.id.consensus_state_id,
Expand Down Expand Up @@ -123,7 +123,7 @@ where
return Err(Error::ChallengePeriodNotElapsed {
consensus_state_id: proof_height.id.consensus_state_id,
current_time: host.timestamp(),
update_time: host.consensus_update_time(proof_height.id.consensus_state_id)?,
update_time: host.state_machine_update_time(proof_height)?,
})
}

Expand Down
4 changes: 3 additions & 1 deletion ismp/src/handlers/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ where
}

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

if let Some(latest_height) = commitment_heights.last() {
Expand Down Expand Up @@ -125,10 +126,11 @@ where
host.store_challenge_period(message.consensus_state_id, message.challenge_period)?;
host.store_consensus_state_id(message.consensus_state_id, message.consensus_client_id)?;

// Store all intermedite state machine commitments
// Store all intermediate state machine commitments
for (id, state_commitment) in message.state_machine_commitments {
let height = StateMachineHeight { id, height: state_commitment.height };
host.store_state_machine_commitment(height, state_commitment.commitment)?;
host.store_state_machine_update_time(height, host.timestamp())?;
host.store_latest_commitment_height(height)?;
}

Expand Down
13 changes: 13 additions & 0 deletions ismp/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ pub trait IsmpHost: Keccak256 {
consensus_state_id: ConsensusStateId,
) -> Result<Duration, Error>;

/// Should return the host timestamp when this state machine height was committed
fn state_machine_update_time(
&self,
state_machine_height: StateMachineHeight,
) -> Result<Duration, Error>;

/// Should return the registered consensus client id for this consensus state id
fn consensus_client_id(
&self,
Expand Down Expand Up @@ -116,6 +122,13 @@ pub trait IsmpHost: Keccak256 {
timestamp: Duration,
) -> Result<(), Error>;

/// Store the timestamp when the state machine height was committed
fn store_state_machine_update_time(
&self,
state_machine_height: StateMachineHeight,
timestamp: Duration,
) -> Result<(), Error>;

/// Store the timestamp when the state machine was updated
fn store_state_machine_commitment(
&self,
Expand Down

0 comments on commit eab698f

Please sign in to comment.