Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
9676d53
implement rlp for numeric types
juanbono Jun 10, 2024
fb1afcc
Merge branch 'main' of github.com:lambdaclass/ethereum_rust into impl…
juanbono Jun 10, 2024
b223f15
add impl for remaining unsigned integers
juanbono Jun 10, 2024
9e34a11
add bytes enconding
juanbono Jun 10, 2024
e88a9b1
add str encoding
juanbono Jun 10, 2024
a075d8a
implement rlp for lists
juanbono Jun 10, 2024
b64bdb0
implement rlp for IPs
juanbono Jun 10, 2024
0f31bdb
implement rlp for ethereum types
juanbono Jun 10, 2024
01f2660
add test and more impls for eth types
juanbono Jun 10, 2024
dc8556e
add rlp decode module
juanbono Jun 10, 2024
890162f
add basic error type
juanbono Jun 10, 2024
0b3744d
pull from main
juanbono Jun 10, 2024
0593c92
fix typo
juanbono Jun 10, 2024
9f685b5
add decode for more types
juanbono Jun 24, 2024
6729a07
pull from main
juanbono Jun 24, 2024
f5a4510
add constants
juanbono Jun 25, 2024
8676f2f
add decoding for fixed size slices
juanbono Jun 26, 2024
e7753ba
add decode for strings and bytes
juanbono Jun 26, 2024
4d32bec
add ethereum_types
juanbono Jun 26, 2024
e404adc
add decoding for ip types
juanbono Jun 26, 2024
cac0cf3
add decoding for u32, u64
juanbono Jun 26, 2024
0859dbf
add decoding for u128
juanbono Jun 26, 2024
3048e59
add tests
juanbono Jun 26, 2024
0c58d5a
feat: add udp `recv_from` call
MegaRedHand Jun 26, 2024
81f798d
feat: add discv4 message encoding
MegaRedHand Jun 26, 2024
8174bf0
chore: rename NodeRecord -> Endpoint
MegaRedHand Jun 27, 2024
4eec6bd
docs: document a bit
MegaRedHand Jun 27, 2024
892436b
chore: add lint target to justfile
MegaRedHand Jun 27, 2024
7b94b7c
perf: optimize `to_hex` (thanks clippy)
MegaRedHand Jun 27, 2024
dc5f7a1
Merge branch 'main' into discv4-setup
MegaRedHand Jun 27, 2024
a7beed7
implement rlp decoding for lists
juanbono Jun 27, 2024
22dac5b
pull from main
juanbono Jun 27, 2024
c831c41
clippy
juanbono Jun 27, 2024
6d7d208
link to docs
juanbono Jun 27, 2024
7162e89
implement rlp decoding for tuples up to size=3
juanbono Jun 27, 2024
847f489
Merge branch 'main' of github.com:lambdaclass/ethereum_rust into impl…
juanbono Jun 27, 2024
78824f5
add u256 decoding
juanbono Jun 27, 2024
22354c2
feat: add PongMessage struct
ricomateo Jun 28, 2024
5a2fed8
merge implement_rlp_decoding branch
ricomateo Jun 28, 2024
dce1fb5
feat: add PongMessage decoding
ricomateo Jun 28, 2024
80dd70f
fix: typo on RLP decoding for 4-tuple
ricomateo Jun 28, 2024
7e26506
add match to decode_with_header function
ricomateo Jun 28, 2024
84b838d
fix: linter
ricomateo Jun 28, 2024
7303d18
merge main
ricomateo Jun 28, 2024
15bf1fc
merge discv4-setup branch
ricomateo Jun 28, 2024
14c76ab
Merge branch 'main' into discv4-setup
MegaRedHand Jun 28, 2024
7118d24
Merge branch 'main' into discv4-setup
MegaRedHand Jun 28, 2024
d138505
fix: restrict decode_bytes to strings
MegaRedHand Jun 28, 2024
17d9e53
feat: add struct Decoder helper
MegaRedHand Jun 28, 2024
9a1fe16
pull from main
juanbono Jun 29, 2024
261a56f
fix: was reading the remaining buffer instead of the payload
MegaRedHand Jun 29, 2024
c6018b5
docs: add documentation
MegaRedHand Jun 29, 2024
e328afc
add message decoding in discover_peers
ricomateo Jul 1, 2024
76fb130
docs: add example `impl RLPDecode` as doctest
MegaRedHand Jul 1, 2024
3db5f64
merge struct-decoder
ricomateo Jul 1, 2024
5cf76be
refactor: pong decoding
ricomateo Jul 1, 2024
a939e44
Merge branch 'main' into discv4-setup
MegaRedHand Jul 1, 2024
1c96af4
fix: use Decoder struct
ricomateo Jul 1, 2024
5848272
chore: add #[must_use] to Encoder and Decoder
MegaRedHand Jul 1, 2024
dcaa89b
fix: remove unnecessary trait
ricomateo Jul 1, 2024
da90c8a
refactor: use Encoder in PingMessage's RLPEncode
MegaRedHand Jul 1, 2024
256351a
merge main
ricomateo Jul 1, 2024
a5217b0
refactor: use Encoder on Endpoint's RLPEncode
MegaRedHand Jul 1, 2024
259dd70
docs: add documentation
MegaRedHand Jul 1, 2024
dc8d44b
fix: change RLPDecode Endpoint trait to use Encoder
ricomateo Jul 1, 2024
5a3408c
feat: add RLPDecode for PongMessage
ricomateo Jul 1, 2024
b25c465
small fix
ricomateo Jul 1, 2024
d00d801
merge main
ricomateo Jul 1, 2024
b9b3e8a
feat: add RLPDecode for PingMessage
ricomateo Jul 1, 2024
58ae847
merge discv4-setup
ricomateo Jul 1, 2024
5f7445a
feat: add decode_optional_field method to Decoder
ricomateo Jul 1, 2024
1784be4
fix: support decoding with optional fields for PingMessage
ricomateo Jul 1, 2024
1c48449
fix: linter
ricomateo Jul 1, 2024
b948803
merge main
ricomateo Jul 2, 2024
ee83710
fix: remove unnecessary RLPDecode impl
ricomateo Jul 2, 2024
5ce7475
merge main
ricomateo Jul 2, 2024
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
2 changes: 1 addition & 1 deletion crates/core/src/rlp/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ impl<T1: RLPDecode, T2: RLPDecode, T3: RLPDecode> RLPDecode for (T1, T2, T3) {
/// - A boolean indicating if the item is a list or not.
/// - The payload of the item, without its prefix.
/// - The remaining bytes after the item.
pub(crate) fn decode_rlp_item(data: &[u8]) -> Result<(bool, &[u8], &[u8]), RLPDecodeError> {
pub fn decode_rlp_item(data: &[u8]) -> Result<(bool, &[u8], &[u8]), RLPDecodeError> {
if data.is_empty() {
return Err(RLPDecodeError::InvalidLength);
}
Expand Down
105 changes: 95 additions & 10 deletions crates/net/src/discv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ impl Message {
let packet_type = encoded_msg[packet_index];
let msg = &encoded_msg[packet_index + 1..];
match packet_type {
0x01 => {
let ping = PingMessage::decode(msg)?;
Ok(Message::Ping(ping))
}
0x02 => {
let pong = PongMessage::decode(msg)?;
Ok(Message::Pong(pong))
Expand All @@ -82,6 +86,16 @@ pub(crate) struct Endpoint {
pub tcp_port: u16,
}

impl RLPEncode for Endpoint {
fn encode(&self, buf: &mut dyn BufMut) {
structs::Encoder::new(buf)
.encode_field(&self.ip)
.encode_field(&self.udp_port)
.encode_field(&self.tcp_port)
.finish();
}
}

impl RLPDecode for Endpoint {
fn decode_unfinished(rlp: &[u8]) -> Result<(Self, &[u8]), RLPDecodeError> {
let decoder = Decoder::new(rlp)?;
Expand All @@ -98,16 +112,6 @@ impl RLPDecode for Endpoint {
}
}

impl RLPEncode for Endpoint {
fn encode(&self, buf: &mut dyn BufMut) {
structs::Encoder::new(buf)
.encode_field(&self.ip)
.encode_field(&self.udp_port)
.encode_field(&self.tcp_port)
.finish();
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct PingMessage {
/// The Ping message version. Should be set to 4, but mustn't be enforced.
Expand Down Expand Up @@ -156,6 +160,28 @@ impl RLPEncode for PingMessage {
}
}

impl RLPDecode for PingMessage {
fn decode_unfinished(rlp: &[u8]) -> Result<(Self, &[u8]), RLPDecodeError> {
let decoder = Decoder::new(rlp)?;
let (version, decoder): (u8, Decoder) = decoder.decode_field("version")?;
let (from, decoder) = decoder.decode_field("from")?;
let (to, decoder) = decoder.decode_field("to")?;
let (expiration, decoder) = decoder.decode_field("expiration")?;
let (enr_seq, decoder) = decoder.decode_optional_field();

let ping = PingMessage {
version,
from,
to,
expiration,
enr_seq,
};

let remaining = decoder.finish()?;
Ok((ping, remaining))
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct PongMessage {
/// The endpoint of the receiver.
Expand Down Expand Up @@ -319,6 +345,65 @@ mod tests {
.collect()
}

#[test]
fn test_decode_ping_message() {
let expiration: u64 = 17195043770;

let from = Endpoint {
ip: IpAddr::from_str("1.2.3.4").unwrap(),
udp_port: 1613,
tcp_port: 6363,
};
let to = Endpoint {
ip: IpAddr::from_str("255.255.2.5").unwrap(),
udp_port: 3063,
tcp_port: 0,
};

let msg = Message::Ping(PingMessage::new(from, to, expiration));

let key_bytes =
H256::from_str("577d8278cc7748fad214b5378669b420f8221afb45ce930b7f22da49cbc545f3")
.unwrap();
let signer = SigningKey::from_slice(key_bytes.as_bytes()).unwrap();

let mut buf = Vec::new();

msg.encode_with_header(&mut buf, signer.clone());
let result = Message::decode_with_header(&buf).expect("Failed decoding PingMessage");
assert_eq!(result, msg);
}

#[test]
fn test_decode_ping_message_with_enr_seq() {
let expiration: u64 = 17195043770;

let from = Endpoint {
ip: IpAddr::from_str("1.2.3.4").unwrap(),
udp_port: 1613,
tcp_port: 6363,
};
let to = Endpoint {
ip: IpAddr::from_str("255.255.2.5").unwrap(),
udp_port: 3063,
tcp_port: 0,
};

let enr_seq = 1704896740573;
let msg = Message::Ping(PingMessage::new(from, to, expiration).with_enr_seq(enr_seq));

let key_bytes =
H256::from_str("577d8278cc7748fad214b5378669b420f8221afb45ce930b7f22da49cbc545f3")
.unwrap();
let signer = SigningKey::from_slice(key_bytes.as_bytes()).unwrap();

let mut buf = Vec::new();

msg.encode_with_header(&mut buf, signer.clone());
let result = Message::decode_with_header(&buf).expect("Failed decoding PingMessage");
assert_eq!(result, msg);
}

#[test]
fn test_decode_endpoint() {
let endpoint = Endpoint {
Expand Down