Skip to content

Commit

Permalink
fix get response (#48)
Browse files Browse the repository at this point in the history
* fix get response

* fix clippy
  • Loading branch information
seunlanlege authored May 26, 2023
1 parent 395e799 commit 5d8fb90
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
2 changes: 1 addition & 1 deletion ismp-testsuite/src/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl StateMachineClient for MockStateMachineClient {
_keys: Vec<Vec<u8>>,
_root: StateCommitment,
_proof: &Proof,
) -> Result<Vec<Option<Vec<u8>>>, Error> {
) -> Result<BTreeMap<Vec<u8>, Option<Vec<u8>>>, Error> {
Ok(Default::default())
}
}
Expand Down
2 changes: 1 addition & 1 deletion ismp/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,5 @@ pub trait StateMachineClient {
keys: Vec<Vec<u8>>,
root: StateCommitment,
proof: &Proof,
) -> Result<Vec<Option<Vec<u8>>>, Error>;
) -> Result<BTreeMap<Vec<u8>, Option<Vec<u8>>>, Error>;
}
11 changes: 5 additions & 6 deletions ismp/src/handlers/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
handlers::{validate_state_machine, MessageResult},
host::IsmpHost,
messaging::{sufficient_proof_height, ResponseMessage},
router::{RequestResponse, Response},
router::{GetResponse, RequestResponse, Response},
util::hash_request,
};
use alloc::{string::ToString, vec::Vec};
Expand Down Expand Up @@ -80,14 +80,13 @@ where
let keys = request.keys().ok_or_else(|| {
Error::ImplementationSpecific("Missing keys for get request".to_string())
})?;
let values =
state_machine.verify_state_proof(host, keys.clone(), state, &proof)?;
let values = state_machine.verify_state_proof(host, keys, state, &proof)?;

let router = host.ismp_router();
let res = router.handle_response(Response::Get {
let res = router.handle_response(Response::Get(GetResponse {
get: request.get_request()?,
values: keys.into_iter().zip(values.into_iter()).collect(),
});
values,
}));
Ok(res)
})
.collect::<Result<Vec<_>, _>>()?
Expand Down
2 changes: 1 addition & 1 deletion ismp/src/handlers/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ where

let values = state_machine.verify_state_proof(host, key, state, &timeout_proof)?;

if values.into_iter().any(|val| val.is_some()) {
if values.into_iter().any(|(_key, val)| val.is_some()) {
Err(Error::ImplementationSpecific("Some Requests not timed out".into()))?
}

Expand Down
30 changes: 19 additions & 11 deletions ismp/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
//! IsmpRouter definition
use crate::{error::Error, host::StateMachine, prelude::Vec};
use alloc::string::{String, ToString};
use alloc::{
collections::BTreeMap,
string::{String, ToString},
};
use codec::{Decode, Encode};
use core::time::Duration;

Expand Down Expand Up @@ -163,50 +166,55 @@ pub struct PostResponse {
pub response: Vec<u8>,
}

/// The response to a POST request
#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq, scale_info::TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Deserialize, serde::Serialize))]
pub struct GetResponse {
/// The Get request that triggered this response.
pub get: Get,
/// Values derived from the state proof
pub values: BTreeMap<Vec<u8>, Option<Vec<u8>>>,
}

/// The ISMP response
#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq, scale_info::TypeInfo)]
#[cfg_attr(feature = "std", derive(serde::Deserialize, serde::Serialize))]
pub enum Response {
/// The response to a POST request
Post(PostResponse),
/// The response to a GET request
Get {
/// The Get request that triggered this response.
get: Get,
/// Values derived from the state proof
values: Vec<(Vec<u8>, Option<Vec<u8>>)>,
},
Get(GetResponse),
}

impl Response {
/// Return the underlying request in the response
pub fn request(&self) -> Request {
match self {
Response::Post(res) => Request::Post(res.post.clone()),
Response::Get { get, .. } => Request::Get(get.clone()),
Response::Get(res) => Request::Get(res.get.clone()),
}
}

/// Get the source chain for this response
pub fn source_chain(&self) -> StateMachine {
match self {
Response::Get { get, .. } => get.dest_chain,
Response::Get(res) => res.get.dest_chain,
Response::Post(res) => res.post.dest_chain,
}
}

/// Get the destination chain for this response
pub fn dest_chain(&self) -> StateMachine {
match self {
Response::Get { get, .. } => get.source_chain,
Response::Get(res) => res.get.source_chain,
Response::Post(res) => res.post.source_chain,
}
}

/// Get the request nonce
pub fn nonce(&self) -> u64 {
match self {
Response::Get { get, .. } => get.nonce,
Response::Get(res) => res.get.nonce,
Response::Post(res) => res.post.nonce,
}
}
Expand Down

0 comments on commit 5d8fb90

Please sign in to comment.