Skip to content

Commit

Permalink
Correct, not cyclical encoded_len computation (#1982)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm authored Aug 23, 2024
1 parent 6d5596f commit d26b99e
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions pallets/liquidity-pools/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ impl TryFrom<Message> for NonBatchMessage {

impl MaxEncodedLen for NonBatchMessage {
fn max_encoded_len() -> usize {
// This message uses a non-batch message version to obtain the encoded
// len to avoid an infinite recursion: message -> batch -> message -> batch ...
Message::<()>::max_encoded_len()
// This message uses a non-recursive message version to obtain the encoded
// len to avoid an infinite recursion of messages
//
// Note: A Batch can NOT contain Forwarded messages
Message::<(), ()>::max_encoded_len()
}
}

Expand Down Expand Up @@ -206,7 +208,7 @@ impl TryFrom<Message> for NonForwardMessage {
fn try_from(message: Message) -> Result<Self, DispatchError> {
match message {
Message::Forwarded { .. } => Err(DispatchError::Other(
"A submessage can not be a forwarded one",
"The inner forwarded message can not be a forwarded one",
)),
_ => Ok(Self(message.into())),
}
Expand All @@ -221,10 +223,11 @@ impl From<NonForwardMessage> for Message {

impl MaxEncodedLen for NonForwardMessage {
fn max_encoded_len() -> usize {
// This message uses a non-forwarded message version to obtain the encoded
// len to avoid an infinite recursion: message -> forward -> message -> forward
// ...
Message::<()>::max_encoded_len()
// This message uses a non-recursive message version to obtain the encoded
// len to avoid an infinite recursion of messages
//
// Note: A Batch CAN be inside of a Forwarded message
Message::<BatchMessages, ()>::max_encoded_len()
}
}

Expand Down Expand Up @@ -667,11 +670,7 @@ impl LpMessageForwarded for Message {
Ok(Self::Forwarded {
source_domain: source_domain.into(),
forwarding_contract,
message: message.try_into().map_err(|_| {
DispatchError::Other(
"Failed to convert LpMessage {message:?} into NonForwardMessage",
)
})?,
message: message.try_into()?,
})
}
}
Expand Down Expand Up @@ -731,6 +730,11 @@ mod tests {
const POOL_ID: PoolId = 12378532;
const TOKEN_ID: u128 = 246803579;

#[test]
fn ensure_non_recursive_max_encoded_len_computation() {
Message::<BatchMessages, NonForwardMessage>::max_encoded_len();
}

#[test]
fn invalid() {
let msg: Message<BatchMessages> = Message::Invalid;
Expand Down

0 comments on commit d26b99e

Please sign in to comment.