-
Notifications
You must be signed in to change notification settings - Fork 136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Message Generator: fix serialization and add into_static for serde_sv2 primitives, broken unit tests, new unit test for NMJ message, #949
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,11 @@ use super::{ | |
}; | ||
use crate::primitives::{FixedSize, GetSize}; | ||
use alloc::vec::Vec; | ||
use serde::{ser, ser::SerializeTuple, Deserialize, Deserializer, Serialize}; | ||
use debug_assert; | ||
use serde::{ | ||
ser::{self, SerializeSeq, SerializeTuple}, | ||
Deserialize, Deserializer, Serialize, | ||
}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Sv2Option<'s, T: Serialize + TryFromBSlice<'s> + Clone> { | ||
|
@@ -65,13 +69,28 @@ impl<'s, T: Clone + Serialize + TryFromBSlice<'s>> Serialize for Sv2Option<'s, T | |
seq.end() | ||
} | ||
(None, Some(data)) => { | ||
let tuple = (data.len() as u8, &data[..]); | ||
let mut seq = serializer.serialize_tuple(2)?; | ||
seq.serialize_element(&tuple.0)?; | ||
seq.serialize_element(tuple.1)?; | ||
seq.end() | ||
if serializer.is_human_readable() { | ||
let data_ = data.clone(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. u can use masking and do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you might also be able to remove this |
||
let mut seq = serializer.serialize_seq(Some(data_.len()))?; | ||
for item in data_ { | ||
seq.serialize_element(&item)?; | ||
} | ||
seq.end() | ||
} else { | ||
let tuple = (data.len() as u8, &data[..]); | ||
let mut seq = serializer.serialize_tuple(2)?; | ||
seq.serialize_element(&tuple.0)?; | ||
seq.serialize_element(tuple.1)?; | ||
seq.end() | ||
} | ||
} | ||
_ => { | ||
debug_assert!( | ||
false, | ||
"sv2option can never have boh fields of the same type" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there an action expected from a user who hit this? regarding how to debug it? If is, its worth mentioning it IMO |
||
); | ||
panic!() | ||
} | ||
_ => panic!(), | ||
} | ||
} | ||
} | ||
|
@@ -219,15 +238,30 @@ impl<'a, T: Clone + FixedSize + Serialize + TryFromBSlice<'a>> GetSize for Sv2Op | |
} | ||
impl<'s> Sv2Option<'s, U256<'s>> { | ||
pub fn into_static(self) -> Sv2Option<'static, U256<'static>> { | ||
if let Some(inner) = self.data { | ||
let inner = inner.clone(); | ||
let data = inner.into_iter().map(|i| i.into_static()).collect(); | ||
Sv2Option { | ||
seq: None, | ||
data: Some(data), | ||
match (self.data, self.seq) { | ||
(None, Some(seq)) => { | ||
let data = seq.parse().unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be useful to use |
||
let data = data.into_iter().map(|i| i.into_static()).collect(); | ||
Sv2Option { | ||
seq: None, | ||
data: Some(data), | ||
} | ||
} | ||
(Some(inner), None) => { | ||
let inner = inner.clone(); | ||
let data = inner.into_iter().map(|i| i.into_static()).collect(); | ||
Sv2Option { | ||
seq: None, | ||
data: Some(data), | ||
} | ||
} | ||
_ => { | ||
debug_assert!( | ||
false, | ||
"sv2option can never have boh fields of the same type" | ||
); | ||
panic!() | ||
} | ||
} else { | ||
panic!() | ||
} | ||
} | ||
pub fn inner_as_ref(&self) -> &[&[u8]] { | ||
|
@@ -236,13 +270,28 @@ impl<'s> Sv2Option<'s, U256<'s>> { | |
} | ||
impl<'s> Sv2Option<'s, u32> { | ||
pub fn into_static(self) -> Sv2Option<'static, u32> { | ||
if let Some(inner) = self.data { | ||
Sv2Option { | ||
match (self.data, self.seq) { | ||
(None, Some(seq)) => { | ||
// this is an already valid seq should be safe to call the unwraps. | ||
// also this library shouldn't be used for priduction envs so is ok do thigs like this | ||
// one | ||
let data = seq.parse().unwrap(); | ||
Sv2Option { | ||
seq: None, | ||
data: Some(data), | ||
} | ||
} | ||
(Some(inner), None) => Sv2Option { | ||
seq: None, | ||
data: Some(inner), | ||
}, | ||
_ => { | ||
debug_assert!( | ||
false, | ||
"sv2option can never have boh fields of the same type" | ||
); | ||
panic!() | ||
} | ||
} else { | ||
panic!() | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add an error variant here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An
crate::error::Error::PrimitiveConversionError
should be the right solution, what do you think?BTW this part of code is used only in the message generator and not for production.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, we can leave as is then
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having second thoughts about this. As it is exposed through the public API, it might make more sense to handle errors properly here.
Also, I have a small question:
If
self.size
is bigger thanself.data.len
, is returning empty array withself.size
cap(the current behavior) makes sense or an error should be returned?