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

Remove unsafe from impl Compact for ClientVersion #11307

Closed
wants to merge 7 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions crates/storage/db-models/src/client_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,48 @@
use reth_codecs::{add_arbitrary_tests, Compact};
use serde::{Deserialize, Serialize};

/// Wrapper around `String`
#[derive(Clone, Eq, PartialEq, Debug, Default, Serialize, Deserialize)]
pub struct CompactString(String);

/// Client version that accessed the database.
#[derive(Clone, Eq, PartialEq, Debug, Default, Serialize, Deserialize)]
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
#[add_arbitrary_tests(compact)]
pub struct ClientVersion {
/// Client version
pub version: String,
pub version: CompactString,
/// The git commit sha
pub git_sha: String,
pub git_sha: CompactString,
/// Build timestamp
pub build_timestamp: String,
pub build_timestamp: CompactString,
}

impl ClientVersion {
/// Returns `true` if no version fields are set.
pub fn is_empty(&self) -> bool {
self.version.is_empty() && self.git_sha.is_empty() && self.build_timestamp.is_empty()
self.version.0.is_empty() && self.git_sha.0.is_empty() && self.build_timestamp.0.is_empty()
}
}

impl From<String> for CompactString {
fn from(s: String) -> Self {
CompactString(s)
}
}

impl Compact for CompactString {
Copy link
Collaborator

Choose a reason for hiding this comment

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

you can move this to the reth-codecs crate, where the trait is defined, then you don't need the wrapper

fn to_compact<B>(&self, buf: &mut B) -> usize
where
B: bytes::BufMut + AsMut<[u8]>,
{
self.0.as_bytes().to_compact(buf)
}

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8]) {
let (vec, buf) = Vec::<u8>::from_compact(buf, len);
let string = String::from_utf8(vec).unwrap(); // Safe conversion
(CompactString(string), buf)
}
}

Expand All @@ -28,20 +53,16 @@ impl Compact for ClientVersion {
where
B: bytes::BufMut + AsMut<[u8]>,
{
self.version.as_bytes().to_compact(buf);
self.git_sha.as_bytes().to_compact(buf);
self.build_timestamp.as_bytes().to_compact(buf)
self.version.to_compact(buf);
self.git_sha.to_compact(buf);
self.build_timestamp.to_compact(buf)
}

fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8]) {
let (version, buf) = Vec::<u8>::from_compact(buf, len);
let (git_sha, buf) = Vec::<u8>::from_compact(buf, len);
let (build_timestamp, buf) = Vec::<u8>::from_compact(buf, len);
let client_version = Self {
version: unsafe { String::from_utf8_unchecked(version) },
git_sha: unsafe { String::from_utf8_unchecked(git_sha) },
build_timestamp: unsafe { String::from_utf8_unchecked(build_timestamp) },
};
let (version, buf) = CompactString::from_compact(buf, len);
let (git_sha, buf) = CompactString::from_compact(buf, len);
let (build_timestamp, buf) = CompactString::from_compact(buf, len);
let client_version = Self { version, git_sha, build_timestamp };
(client_version, buf)
}
}
Loading