-
Notifications
You must be signed in to change notification settings - Fork 131
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
feat: alloy bytes conversion #1552
feat: alloy bytes conversion #1552
Conversation
1daad07
to
71175fc
Compare
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.
A lot of good work, but also a lot of small comments.
@@ -703,13 +703,13 @@ mod test { | |||
|
|||
#[test] | |||
fn message_encoding_find_content() { | |||
let content_key = hex_decode("0x706f7274616c").unwrap(); | |||
let content_key = Bytes::from("0x706f7274616c"); |
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.
This should have been: Bytes::from_hex("0x706f7274616c").unwrap()
And you should revert expected_encoded
to original value (line 712).
@@ -733,13 +733,14 @@ mod test { | |||
|
|||
#[test] | |||
fn message_encoding_content_content() { | |||
let content_val = hex_decode("0x7468652063616b652069732061206c6965").unwrap(); | |||
let content_val = Bytes::from("0x7468652063616b652069732061206c6965"); |
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.
Also here
portalnet/src/discovery.rs
Outdated
@@ -460,7 +464,7 @@ impl AsyncUdpSocket<UtpEnr> for Discv5UdpSocket { | |||
async fn send_to(&mut self, buf: &[u8], target: &UtpEnr) -> io::Result<usize> { | |||
let discv5 = Arc::clone(&self.discv5); | |||
let target = target.0.clone(); | |||
let data = buf.to_vec(); | |||
let data = Bytes::from(buf.to_vec()); |
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 be: Bytes::copy_from_slice(buf)
@@ -657,7 +658,9 @@ mod tests { | |||
let peer_node_id = k.preimage(); | |||
query.on_success( | |||
peer_node_id, | |||
FindContentQueryResponse::Content(found_content.clone()), | |||
FindContentQueryResponse::Content( | |||
found_content.clone().into(), |
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.
Type of found_content
should be changed to RawContentValue. Meaning, at line 583, it should be:
let found_content = RawContentValue::from([0xef]);
Then here, you would have:
FindContentQueryResponse::Content(found_content.clone())
portalnet/src/overlay/service.rs
Outdated
@@ -699,7 +699,7 @@ impl< | |||
.connect_inbound_stream(cid) | |||
.await | |||
{ | |||
Ok(data) => data, | |||
Ok(data) => data.into(), |
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.
nit: I think we can/should be more explicit here:
Ok(data) => RawContentValue::from(data)
trin-storage/src/lib.rs
Outdated
let content_id = key.content_id(); | ||
let value: &[u8] = value.as_ref(); | ||
self.store.insert(content_id.to_vec(), value.to_vec()); | ||
self.store | ||
.insert(content_id.to_vec(), Bytes::from(value.to_vec())); |
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.
This should be: Bytes::copy_from_slice(value)
@@ -265,7 +265,7 @@ impl<TContentKey: OverlayContentKey> IdIndexedV1Store<TContentKey> { | |||
named_params! { | |||
":content_id": content_id, | |||
":content_key": content_key, | |||
":content_value": content_value, | |||
":content_value": content_value.to_vec(), |
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.
Instead of to_vec()
we should use as_ref()
.
@@ -510,7 +510,11 @@ impl<TContentKey: OverlayContentKey> IdIndexedV1Store<TContentKey> { | |||
self.usage_stats.entry_count -= to_delete; | |||
self.usage_stats.total_entry_size_bytes -= deleted_content_size; | |||
self.usage_stats.report_metrics(&self.metrics); | |||
deleted_content.extend(deleted_content_values); | |||
deleted_content.extend( |
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.
instead of doing it here, conversion should be done while reading data from db. Somewhere around line 481.
For example, something like this:
let key_bytes: Vec<u8> = row.get("content_key")?;
let value_bytes: Vec<u8> = row.get("content_value")?;
let value = RawContentValue::from(value_bytes);
let size: u64 = row.get("content_size")?;
TContentKey::try_from_bytes(key_bytes)
.map(|key| (key, value, size))
.map_err(|e| {
rusqlite::Error::FromSqlConversionFailure(0, Type::Blob, e.into())
})
@@ -811,12 +815,15 @@ mod tests { | |||
assert!(!store.has_content(&id)?); | |||
let usage_stats = store.usage_stats(); | |||
|
|||
store.insert(&key, value.clone())?; | |||
store.insert(&key, value.clone().into())?; |
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.
Instead of calling value.into()
here and in many places below, we should refactor generate_key_value
and generate_key_value_with_content_size
so they return RawContentValue
portalnet/src/discovery.rs
Outdated
@@ -36,7 +37,7 @@ const TALKREQ_CHANNEL_BUFFER: usize = 100; | |||
/// ENR key for portal network client version. | |||
pub const ENR_PORTAL_CLIENT_KEY: &str = "c"; | |||
|
|||
pub type ProtocolRequest = Vec<u8>; | |||
pub type ProtocolRequest = Bytes; |
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.
After looking more into how this is used, I think it's better if this is not changed to Bytes
and left as Vec<u8>
.
Addressed all the comments, ready for review |
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.
Looks good! Few minor comments.
After you address remaining comments, can you rebase/merge to head. This diverged quite some time ago...
trin-state/src/storage.rs
Outdated
@@ -280,10 +278,10 @@ pub mod test { | |||
for value in value.as_sequence().unwrap() { | |||
result.push(ContentData { | |||
key: StateContentKey::deserialize(value.get("content_key").unwrap())?, | |||
store_value: hex_decode( | |||
store_value: RawContentValue::from_hex( |
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.
This whole section can be:
result.push(ContentData {
key: StateContentKey::deserialize(&value["content_key"])?,
store_value: RawContentValue::deserialize(&value["content_value_offer"])?,
lookup_value: RawContentValue::deserialize(&value["content_value_retrieval"])?,
});
) -> (IdentityContentKey, RawContentValue) { | ||
let (key, value) = | ||
generate_key_value_with_content_size(config, distance, CONTENT_DEFAULT_SIZE_BYTES); | ||
(key, RawContentValue::copy_from_slice(value.as_ref())) | ||
} | ||
|
||
fn generate_key_value_with_content_size( |
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.
The generate_key_value_with_content_size
should return (IdentityContentKey, RawContentValue)
.
This will simplify generate_key_value
, and we can remove several value.into()
below.
portalnet/src/discovery.rs
Outdated
Ok(response) | ||
let response = self | ||
.discv5 | ||
.talk_req(enr, protocol, request.to_vec()) |
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.
With reverted changes, we can just use request
(no need for request.to_vec()
).
portalnet/src/overlay/service.rs
Outdated
@@ -1042,7 +1044,7 @@ impl< | |||
// over the uTP stream. | |||
let utp = Arc::clone(&self.utp_controller); | |||
tokio::spawn(async move { | |||
utp.accept_outbound_stream(cid, content).await; | |||
utp.accept_outbound_stream(cid, content.as_ref()).await; |
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.
nit: this should be &content
portalnet/src/overlay/service.rs
Outdated
.send_talk_req( | ||
destination, | ||
protocol, | ||
Message::from(request).as_ssz_bytes().to_vec(), |
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.
No need for .to_vec()
, meaning it should be: Message::from(request).as_ssz_bytes()
portalnet/src/overlay/service.rs
Outdated
@@ -1595,7 +1600,7 @@ impl< | |||
} | |||
}; | |||
let result = utp_controller | |||
.connect_outbound_stream(cid, content_payload.to_vec()) | |||
.connect_outbound_stream(cid, content_payload.as_ref()) |
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.
This should be: &content_payload
a6679cf
to
376742e
Compare
Looks good. Thanks! |
What was wrong?
Replace Vec with alloy::Bytes or bytes::Bytes based on usage
#1404
How was it fixed?
To-Do
Replace intermediary usage of Vec in some places