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

Commit

Permalink
H256 as storage keys for commitments (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wizdave97 authored Jul 31, 2023
1 parent 252250d commit 3c4a9f3
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 38 deletions.
10 changes: 0 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ jobs:
with:
version: '3.9.1'

- name: Rust cache
uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true

- name: Build
run: |
cargo +nightly-2022-10-28 check --workspace --all-targets --all-features --verbose --locked
Expand Down Expand Up @@ -68,10 +63,5 @@ jobs:
with:
components: rustfmt

- name: Rust cache
uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true

- name: Check format
run: cargo +nightly fmt --all --check
2 changes: 1 addition & 1 deletion pallet-ismp/evm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ fn on_accept_callback() {

let request_commitment = hash_request::<Host<Test>>(&Request::Post(post.clone()));
RequestCommitments::<Test>::insert(
request_commitment.0.to_vec(),
request_commitment,
LeafIndexQuery { source_chain: post.source, dest_chain: post.dest, nonce: 0 },
);

Expand Down
2 changes: 1 addition & 1 deletion pallet-ismp/primitives/state-machine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ where
match req {
Request::Post(post) => {
let request = Request::Post(post);
let commitment = hash_request::<Host<T>>(&request).0.to_vec();
let commitment = hash_request::<Host<T>>(&request);
keys.push(pallet_ismp::RequestReceipts::<T>::hashed_key_for(commitment));
}
Request::Get(_) => continue,
Expand Down
12 changes: 6 additions & 6 deletions pallet-ismp/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub mod benchmarks {
handle(RawOrigin::Signed(caller), vec![Message::Request(msg)]);

let commitment = hash_request::<Host<T>>(&Request::Post(post));
assert!(RequestReceipts::<T>::get(commitment.0.to_vec()).is_some());
assert!(RequestReceipts::<T>::get(commitment).is_some());
}

#[benchmark]
Expand All @@ -144,7 +144,7 @@ pub mod benchmarks {

let commitment = hash_request::<Host<T>>(&request);
RequestCommitments::<T>::insert(
commitment.0.to_vec(),
commitment,
LeafIndexQuery { source_chain: post.source, dest_chain: post.dest, nonce: post.nonce },
);

Expand All @@ -160,7 +160,7 @@ pub mod benchmarks {
#[extrinsic_call]
handle(RawOrigin::Signed(caller), vec![Message::Response(msg)]);

assert!(ResponseReceipts::<T>::get(request_commitment.0.to_vec()).is_some());
assert!(ResponseReceipts::<T>::get(request_commitment).is_some());
}

#[benchmark]
Expand All @@ -182,7 +182,7 @@ pub mod benchmarks {

let commitment = hash_request::<Host<T>>(&request);
RequestCommitments::<T>::insert(
commitment.0.to_vec(),
commitment,
LeafIndexQuery { source_chain: post.source, dest_chain: post.dest, nonce: post.nonce },
);

Expand All @@ -195,7 +195,7 @@ pub mod benchmarks {
#[extrinsic_call]
handle(RawOrigin::Signed(caller), vec![Message::Timeout(msg)]);

assert!(RequestCommitments::<T>::get(commitment.0.to_vec()).is_none());
assert!(RequestCommitments::<T>::get(commitment).is_none());
}

#[benchmark]
Expand Down Expand Up @@ -274,7 +274,7 @@ pub mod benchmarks {
};
let request_commitment = hash_request::<Host<T>>(&Request::Post(post.clone()));
RequestCommitments::<T>::insert(
request_commitment.0.to_vec(),
request_commitment,
LeafIndexQuery { source_chain: post.source, dest_chain: post.dest, nonce: 0 },
);

Expand Down
12 changes: 6 additions & 6 deletions pallet-ismp/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ where
{
/// Dispatch an outgoing request
pub fn dispatch_request(request: Request) -> Result<(), IsmpError> {
let commitment = hash_request::<Host<T>>(&request).0.to_vec();
let commitment = hash_request::<Host<T>>(&request);

if RequestCommitments::<T>::contains_key(commitment.clone()) {
if RequestCommitments::<T>::contains_key(commitment) {
Err(IsmpError::ImplementationSpecific("Duplicate request".to_string()))?
}

Expand All @@ -45,15 +45,15 @@ where

/// Dispatch an outgoing response
pub fn dispatch_response(response: Response) -> Result<(), IsmpError> {
let commitment = hash_request::<Host<T>>(&response.request()).0.to_vec();
let commitment = hash_request::<Host<T>>(&response.request());

if !RequestCommitments::<T>::contains_key(commitment.clone()) {
if !RequestCommitments::<T>::contains_key(commitment) {
Err(IsmpError::ImplementationSpecific("Unknown request for response".to_string()))?
}

let commitment = hash_response::<Host<T>>(&response).0.to_vec();
let commitment = hash_response::<Host<T>>(&response);

if ResponseCommitments::<T>::contains_key(commitment.clone()) {
if ResponseCommitments::<T>::contains_key(commitment) {
Err(IsmpError::ImplementationSpecific("Duplicate response".to_string()))?
}

Expand Down
12 changes: 6 additions & 6 deletions pallet-ismp/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ where
}

fn request_commitment(&self, commitment: H256) -> Result<(), Error> {
let _ = RequestCommitments::<T>::get(commitment.0.to_vec()).ok_or_else(|| {
let _ = RequestCommitments::<T>::get(commitment).ok_or_else(|| {
Error::ImplementationSpecific("Request commitment not found".to_string())
})?;

Expand All @@ -94,7 +94,7 @@ where
fn request_receipt(&self, req: &Request) -> Option<()> {
let commitment = hash_request::<Self>(req);

let _ = RequestReceipts::<T>::get(commitment.0.to_vec())
let _ = RequestReceipts::<T>::get(commitment)
.ok_or_else(|| Error::RequestCommitmentNotFound {
nonce: req.nonce(),
source: req.source_chain(),
Expand Down Expand Up @@ -141,13 +141,13 @@ where
fn delete_request_commitment(&self, req: &Request) -> Result<(), Error> {
let hash = hash_request::<Self>(req);
// We can't delete actual leaves in the mmr so this serves as a replacement for that
RequestCommitments::<T>::remove(hash.0.to_vec());
RequestCommitments::<T>::remove(hash);
Ok(())
}

fn store_request_receipt(&self, req: &Request) -> Result<(), Error> {
let hash = hash_request::<Self>(req);
RequestReceipts::<T>::insert(hash.0.to_vec(), Receipt::Ok);
RequestReceipts::<T>::insert(hash, Receipt::Ok);
Ok(())
}

Expand Down Expand Up @@ -195,7 +195,7 @@ where
fn response_receipt(&self, res: &Request) -> Option<()> {
let commitment = hash_request::<Self>(res);

let _ = ResponseReceipts::<T>::get(commitment.0.to_vec())
let _ = ResponseReceipts::<T>::get(commitment)
.ok_or_else(|| Error::ImplementationSpecific("Response receipt not found".to_string()))
.ok()?;

Expand All @@ -209,7 +209,7 @@ where

fn store_response_receipt(&self, req: &Request) -> Result<(), Error> {
let hash = hash_request::<Self>(req);
ResponseReceipts::<T>::insert(hash.0.to_vec(), Receipt::Ok);
ResponseReceipts::<T>::insert(hash, Receipt::Ok);
Ok(())
}

Expand Down
11 changes: 4 additions & 7 deletions pallet-ismp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,28 +227,25 @@ pub mod pallet {
#[pallet::storage]
#[pallet::getter(fn request_commitments)]
pub type RequestCommitments<T: Config> =
StorageMap<_, Blake2_128Concat, Vec<u8>, LeafIndexQuery, OptionQuery>;
StorageMap<_, Identity, H256, LeafIndexQuery, OptionQuery>;

/// Commitments for outgoing responses
/// The key is the response commitment
#[pallet::storage]
#[pallet::getter(fn response_commitments)]
pub type ResponseCommitments<T: Config> =
StorageMap<_, Blake2_128Concat, Vec<u8>, Receipt, OptionQuery>;
pub type ResponseCommitments<T: Config> = StorageMap<_, Identity, H256, Receipt, OptionQuery>;

/// Receipts for incoming requests
/// The key is the request commitment
#[pallet::storage]
#[pallet::getter(fn request_receipts)]
pub type RequestReceipts<T: Config> =
StorageMap<_, Blake2_128Concat, Vec<u8>, Receipt, OptionQuery>;
pub type RequestReceipts<T: Config> = StorageMap<_, Identity, H256, Receipt, OptionQuery>;

/// Receipts for incoming responses
/// The key is the request commitment
#[pallet::storage]
#[pallet::getter(fn response_receipts)]
pub type ResponseReceipts<T: Config> =
StorageMap<_, Blake2_128Concat, Vec<u8>, Receipt, OptionQuery>;
pub type ResponseReceipts<T: Config> = StorageMap<_, Identity, H256, Receipt, OptionQuery>;

/// Consensus update results still in challenge period
/// Set contains a tuple of previous height and latest height
Expand Down
2 changes: 1 addition & 1 deletion pallet-ismp/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ fn dispatcher_should_write_receipts_for_outgoing_requests_and_responses() {

let request_commitment = hash_request::<Host<Test>>(&Request::Post(post.clone()));
RequestCommitments::<Test>::insert(
request_commitment.0.to_vec(),
request_commitment,
LeafIndexQuery { source_chain: post.source, dest_chain: post.dest, nonce: 0 },
);
write_outgoing_commitments(&host, &dispatcher).unwrap();
Expand Down

0 comments on commit 3c4a9f3

Please sign in to comment.