Skip to content

Commit

Permalink
Fix exception in RequestContext
Browse files Browse the repository at this point in the history
Fixed possible exception in 'pub struct RequestContext' that could occur
when splitting the request and reply channel in 'pub fn split'
  • Loading branch information
hansieodendaal committed Sep 21, 2023
1 parent 24b4324 commit 36233a0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
6 changes: 3 additions & 3 deletions base_layer/mmr/src/sparse_merkle_tree/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ mod test {
// feature?)
assert!(proof.validate(&NodeKey::from([67u8; 32]), tree.hash()));
// Use an exclusion proof to try and give an invalid validation for a key that does exist
assert_eq!(proof.validate(&NodeKey::from([64u8; 32]), tree.hash()), false);
assert!(!proof.validate(&NodeKey::from([64u8; 32]), tree.hash()));

// A tree with branches
tree.upsert(NodeKey::from([96u8; 32]), ValueHash::from([1u8; 32]))
Expand All @@ -352,11 +352,11 @@ mod test {
// Does not validate against invalid root hash
assert!(!proof.validate(&NodeKey::from([99u8; 32]), &NodeHash::default()));
// Not all non-existent keys will validate. (bug or feature?)
assert_eq!(proof.validate(&NodeKey::from([65; 32]), tree.hash()), false);
assert!(!proof.validate(&NodeKey::from([65; 32]), tree.hash()));
// But any keys with prefix 011 (that is not 96) will validate
assert!(proof.validate(&NodeKey::from([0b0110_0011; 32]), tree.hash()));
assert!(proof.validate(&NodeKey::from([0b0111_0001; 32]), tree.hash()));
// The key 96 does exist..
assert_eq!(proof.validate(&NodeKey::from([0b0110_0000; 32]), tree.hash()), false);
assert!(!proof.validate(&NodeKey::from([0b0110_0000; 32]), tree.hash()));
}
}
2 changes: 1 addition & 1 deletion base_layer/service_framework/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
//! // At the same time receive the request and reply
//! async move {
//! let req_context = receiver.next().await.unwrap();
//! let msg = req_context.request().unwrap().clone();
//! let msg = req_context.request().clone();
//! req_context.reply(msg.to_uppercase());
//! }
//! );
Expand Down
16 changes: 5 additions & 11 deletions base_layer/service_framework/src/reply_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,35 +131,29 @@ impl<T> Future for TransportResponseFuture<T> {
/// request.
pub struct RequestContext<TReq, TResp> {
reply_tx: oneshot::Sender<TResp>,
request: Option<TReq>,
request: TReq,
}

impl<TReq, TResp> RequestContext<TReq, TResp> {
/// Create a new RequestContect
pub fn new(request: TReq, reply_tx: oneshot::Sender<TResp>) -> Self {
Self {
request: Some(request),
request,
reply_tx,
}
}

/// Return a reference to the request object. None is returned after take_request has
/// been called.
pub fn request(&self) -> Option<&TReq> {
self.request.as_ref()
}

/// Take ownership of the request object, if ownership has not already been taken,
/// otherwise None is returned.
pub fn take_request(&mut self) -> Option<TReq> {
self.request.take()
pub fn request(&self) -> &TReq {
&self.request
}

/// Consume this object and return it's parts. Namely, the request object and
/// the reply oneshot channel.
pub fn split(self) -> (TReq, oneshot::Sender<TResp>) {
(
self.request.expect("RequestContext must be initialized with a request"),
self.request,
self.reply_tx,
)
}
Expand Down

0 comments on commit 36233a0

Please sign in to comment.