Skip to content
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

Merged
merged 7 commits into from
Dec 2, 2024

Conversation

etherhood
Copy link
Contributor

@etherhood etherhood commented Oct 24, 2024

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

@etherhood etherhood force-pushed the feat/alloy-bytes-conversion branch from 1daad07 to 71175fc Compare October 24, 2024 14:59
@KolbyML KolbyML requested a review from morph-dev October 24, 2024 20:02
@KolbyML KolbyML changed the title Feat: alloy bytes conversion feat: alloy bytes conversion Oct 24, 2024
Copy link
Collaborator

@morph-dev morph-dev left a 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");
Copy link
Collaborator

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");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here

@@ -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());
Copy link
Collaborator

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(),
Copy link
Collaborator

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())

@@ -699,7 +699,7 @@ impl<
.connect_inbound_stream(cid)
.await
{
Ok(data) => data,
Ok(data) => data.into(),
Copy link
Collaborator

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)

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()));
Copy link
Collaborator

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(),
Copy link
Collaborator

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(
Copy link
Collaborator

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())?;
Copy link
Collaborator

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

@@ -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;
Copy link
Collaborator

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>.

@etherhood etherhood requested a review from morph-dev December 1, 2024 08:20
@etherhood
Copy link
Contributor Author

A lot of good work, but also a lot of small comments.

Addressed all the comments, ready for review

Copy link
Collaborator

@morph-dev morph-dev left a 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...

@@ -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(
Copy link
Collaborator

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(
Copy link
Collaborator

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.

Ok(response)
let response = self
.discv5
.talk_req(enr, protocol, request.to_vec())
Copy link
Collaborator

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()).

@@ -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;
Copy link
Collaborator

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

.send_talk_req(
destination,
protocol,
Message::from(request).as_ssz_bytes().to_vec(),
Copy link
Collaborator

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()

@@ -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())
Copy link
Collaborator

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

@etherhood etherhood force-pushed the feat/alloy-bytes-conversion branch from a6679cf to 376742e Compare December 2, 2024 17:23
@morph-dev
Copy link
Collaborator

Looks good. Thanks!

@morph-dev morph-dev merged commit d877539 into ethereum:master Dec 2, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants