From ae61cc7d93be735dc22d41fe4c7a5fbff98a156b Mon Sep 17 00:00:00 2001 From: David Salami <31099392+Wizdave97@users.noreply.github.com> Date: Mon, 7 Aug 2023 19:13:03 +0100 Subject: [PATCH] Ismp updates (#81) --- Cargo.lock | 4 ++-- pallet-ismp/src/host.rs | 28 +++++++++++++++++++++++++++- pallet-ismp/src/lib.rs | 7 +++++++ pallet-ismp/src/mocks/ismp.rs | 11 +++++++---- 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d7df07e..e41adf9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3386,7 +3386,7 @@ dependencies = [ [[package]] name = "ismp" version = "0.1.0" -source = "git+https://github.com/polytope-labs/ismp-rs?branch=main#59d83711eec3f567c5a5984d032fca2abbf2d559" +source = "git+https://github.com/polytope-labs/ismp-rs?branch=main#317cb2ffc16d4d5b0b50dfee282728bf70fc0641" dependencies = [ "derive_more", "parity-scale-codec", @@ -3639,7 +3639,7 @@ dependencies = [ [[package]] name = "ismp-testsuite" version = "0.1.0" -source = "git+https://github.com/polytope-labs/ismp-rs?branch=main#59d83711eec3f567c5a5984d032fca2abbf2d559" +source = "git+https://github.com/polytope-labs/ismp-rs?branch=main#317cb2ffc16d4d5b0b50dfee282728bf70fc0641" dependencies = [ "ismp", "parity-scale-codec", diff --git a/pallet-ismp/src/host.rs b/pallet-ismp/src/host.rs index 00c5a62..1f836b2 100644 --- a/pallet-ismp/src/host.rs +++ b/pallet-ismp/src/host.rs @@ -18,7 +18,7 @@ use crate::{ dispatcher::Receipt, primitives::ConsensusClientProvider, AllowedProxies, ChallengePeriod, Config, ConsensusClientUpdateTime, ConsensusStateClient, ConsensusStates, FrozenConsensusClients, FrozenHeights, LatestStateMachineHeight, Nonce, RequestCommitments, - RequestReceipts, ResponseReceipts, StateCommitments, UnbondingPeriod, + RequestReceipts, ResponseReceipts, StateCommitments, StateMachineUpdateTime, UnbondingPeriod, }; use alloc::{format, string::ToString}; use core::time::Duration; @@ -71,6 +71,20 @@ impl IsmpHost for Host { }) } + fn state_machine_update_time( + &self, + state_machine_height: StateMachineHeight, + ) -> Result { + StateMachineUpdateTime::::get(state_machine_height) + .map(|timestamp| Duration::from_secs(timestamp)) + .ok_or_else(|| { + Error::ImplementationSpecific(format!( + "Update time not found for {:?}", + state_machine_height + )) + }) + } + fn consensus_state(&self, id: ConsensusClientId) -> Result, Error> { ConsensusStates::::get(id) .ok_or_else(|| Error::ConsensusStateNotFound { consensus_state_id: id }) @@ -116,6 +130,18 @@ impl IsmpHost for Host { Ok(()) } + fn store_state_machine_update_time( + &self, + state_machine_height: StateMachineHeight, + timestamp: Duration, + ) -> Result<(), Error> { + StateMachineUpdateTime::::insert( + state_machine_height, + timestamp.as_secs().saturated_into::(), + ); + Ok(()) + } + fn store_state_machine_commitment( &self, height: StateMachineHeight, diff --git a/pallet-ismp/src/lib.rs b/pallet-ismp/src/lib.rs index 7d27707..2f307e0 100644 --- a/pallet-ismp/src/lib.rs +++ b/pallet-ismp/src/lib.rs @@ -221,6 +221,13 @@ pub mod pallet { pub type ConsensusClientUpdateTime = StorageMap<_, Twox64Concat, ConsensusClientId, u64, OptionQuery>; + /// Holds the timestamp at which a state machine height was updated. + /// Used in ensuring that the configured challenge period elapses. + #[pallet::storage] + #[pallet::getter(fn state_machine_update_time)] + pub type StateMachineUpdateTime = + StorageMap<_, Twox64Concat, StateMachineHeight, u64, OptionQuery>; + /// Commitments for outgoing requests /// The key is the request commitment #[pallet::storage] diff --git a/pallet-ismp/src/mocks/ismp.rs b/pallet-ismp/src/mocks/ismp.rs index 890684e..81adcdd 100644 --- a/pallet-ismp/src/mocks/ismp.rs +++ b/pallet-ismp/src/mocks/ismp.rs @@ -136,13 +136,16 @@ where }, ) .unwrap(); - - set_timestamp::(1000_000_000); - StateMachineHeight { + let height = StateMachineHeight { id: StateMachineId { state_id: StateMachine::Ethereum(Ethereum::ExecutionLayer), consensus_state_id: MOCK_CONSENSUS_STATE_ID, }, height: 3, - } + }; + host.store_state_machine_update_time(height, core::time::Duration::from_millis(1000_000)) + .unwrap(); + + set_timestamp::(1000_000_000); + height }