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(l1): support snap capability message GetStorageRanges #1025

Merged
merged 90 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 82 commits
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
02fe744
Corrupted History Fix
fmoletta Oct 22, 2024
a2a2aac
Fix slim encoding for AccountState
fmoletta Oct 22, 2024
aef7441
Fix slim encoding for AccountState
fmoletta Oct 22, 2024
13d9bc3
Commit progress
fmoletta Oct 22, 2024
0ca3589
Commit progress
fmoletta Oct 22, 2024
66b6f90
Add snap as supported cap
fmoletta Oct 22, 2024
41dcaff
Push missing changes
fmoletta Oct 23, 2024
a526c10
Initial peer listen loop
ElFantasma Oct 23, 2024
562bc49
Deprecate `RLPEncodeSlim` trait and use `AccountStateSlim` instead
fmoletta Oct 23, 2024
3893b05
Fix logic
fmoletta Oct 23, 2024
dbe110f
Remove debug print
fmoletta Oct 23, 2024
8b8eacb
Limit response bytes
fmoletta Oct 23, 2024
4ee1790
Add test
fmoletta Oct 23, 2024
d87b1ca
Add more tests
fmoletta Oct 23, 2024
89621bf
Cleanup test code + Fix logic + add test
fmoletta Oct 23, 2024
3a14cdd
Add test
fmoletta Oct 23, 2024
c354321
Add test
fmoletta Oct 23, 2024
7abd4f7
Add test
fmoletta Oct 23, 2024
7cee2b7
Add test
fmoletta Oct 23, 2024
579309f
Also fetch limit proof
fmoletta Oct 23, 2024
9fccb07
Trim test state 408 -> 100
fmoletta Oct 24, 2024
38a7d5b
Simplify logic
fmoletta Oct 24, 2024
574299b
Merge branch 'main' into 840-rlpx-listen-loop
ElFantasma Oct 24, 2024
704da4c
Encode accounts while building range so we do not encode twice
fmoletta Oct 24, 2024
b55294b
Merge branch 'main' of github.com:lambdaclass/lambda_ethereum_rust in…
fmoletta Oct 24, 2024
6d5cc45
Clippy
fmoletta Oct 24, 2024
9514037
Add allow tag
fmoletta Oct 24, 2024
a5b3934
Remove comment
fmoletta Oct 24, 2024
68dcef6
Fix typo
fmoletta Oct 24, 2024
f23077e
Sending eth Status message first
ElFantasma Oct 24, 2024
122778d
Small fixes
ElFantasma Oct 25, 2024
7a4f9cf
Merge branch 'main' into 840-rlpx-listen-loop
ElFantasma Oct 25, 2024
5ae41fe
Small fixes
ElFantasma Oct 25, 2024
66b0bd8
Added TODO comments for pending tasks
ElFantasma Oct 25, 2024
6a5f580
Refactored code to separate encoding/decoding from backend logic
ElFantasma Oct 25, 2024
6c04bb6
Merge branch '840-rlpx-listen-loop' of github.com:lambdaclass/lambda_…
fmoletta Oct 25, 2024
05c9a5e
Replaced hardcoded capabilities strings with enums
ElFantasma Oct 25, 2024
f011aa6
Merge branch 'main' into 840-rlpx-listen-loop
ElFantasma Oct 25, 2024
8ea2644
Add snap messages to liste loop
fmoletta Oct 25, 2024
42049ab
Made Status test to pass
ElFantasma Oct 28, 2024
34e0d9c
Merge branch 'main' into 840-rlpx-listen-loop
ElFantasma Oct 28, 2024
940b6f3
Fixed format
ElFantasma Oct 28, 2024
8047f41
Removed required Debug format on error printing
ElFantasma Oct 28, 2024
f474617
Trying previous commit from hive to see if CI works
ElFantasma Oct 28, 2024
1d5223b
Fix `AccountRange message decoding
fmoletta Oct 28, 2024
80329c7
Fix
fmoletta Oct 28, 2024
297a37b
fix
fmoletta Oct 28, 2024
f25fb1a
Fix: build proof for last account not limit hash
fmoletta Oct 28, 2024
0fdf562
Using apply_fork_choice to set last block number and make blocks cann…
ElFantasma Oct 28, 2024
c989418
Merge branch 'main' into 840-rlpx-listen-loop
ElFantasma Oct 28, 2024
12010b3
Fmt + Return error instead of pancking if invalid root is used
fmoletta Oct 28, 2024
ed0186f
Calling apply_fork_choice only once for the whole chain
ElFantasma Oct 28, 2024
42f2af3
Revert change
fmoletta Oct 28, 2024
61ad329
Return empty proof if root is missing
fmoletta Oct 28, 2024
13c44cf
Calling apply_fork_choice only once for the whole chain
ElFantasma Oct 28, 2024
ecb682f
Clippy fix
ElFantasma Oct 28, 2024
f8bd1dc
use trait method
fmoletta Oct 28, 2024
b890f60
Merge remote-tracking branch 'origin/840-rlpx-listen-loop' into trie_…
fmoletta Oct 28, 2024
31aa8ac
Update hive revision
fmoletta Oct 28, 2024
34d8d84
Reorganize module
fmoletta Oct 28, 2024
9b85c69
Merge branch 'main' into 840-rlpx-listen-loop
ElFantasma Oct 29, 2024
013a977
Update hive revision
fmoletta Oct 29, 2024
66f759a
Add hive snap worflow
fmoletta Oct 29, 2024
67cb57a
Merge branch 'main' into 840-rlpx-listen-loop
ElFantasma Oct 29, 2024
b1ea882
Reverting hive version
ElFantasma Oct 29, 2024
58da873
Merge branch 'main' of github.com:lambdaclass/lambda_ethereum_rust in…
fmoletta Oct 29, 2024
fe2fb77
Removed unnecessary info! messages
ElFantasma Oct 29, 2024
50ca190
Merge branch '840-rlpx-listen-loop' into trie_iter
fmoletta Oct 29, 2024
7062b1b
Add messages + inner structs
fmoletta Oct 29, 2024
d609cae
progress
fmoletta Oct 29, 2024
c69690d
Bump hive version
fmoletta Oct 29, 2024
65f8c77
Merge branch 'main' of github.com:lambdaclass/lambda_ethereum_rust in…
fmoletta Oct 29, 2024
b7c0211
Fix bug in response bytes length calculation
fmoletta Oct 29, 2024
74173d2
Restore Makefile
fmoletta Oct 29, 2024
777732a
Identify storage tries by hashed account address
fmoletta Oct 29, 2024
0e3e276
Merge branch 'trie_iter' into snap-storage-range
fmoletta Oct 29, 2024
abb28cf
Impl logic
fmoletta Oct 29, 2024
fea174d
Connect to main loop
fmoletta Oct 30, 2024
dc6752c
Compute proofs
fmoletta Oct 30, 2024
03d9928
Clippy
fmoletta Oct 30, 2024
c791a89
Chamge type
fmoletta Oct 30, 2024
9fd88d3
Add test
fmoletta Oct 30, 2024
3b7d70e
Fix
fmoletta Oct 31, 2024
384eff7
Merge branch 'trie_iter' into snap-storage-range
fmoletta Oct 31, 2024
d02e8f2
Merge branch 'main' of github.com:lambdaclass/lambda_ethereum_rust in…
fmoletta Oct 31, 2024
0575a44
Merge branch 'snap-storage-range' of github.com:lambdaclass/lambda_et…
fmoletta Oct 31, 2024
aa29e5f
Fix lingering conflicts
fmoletta Oct 31, 2024
200d014
Merge branch 'main' of github.com:lambdaclass/lambda_ethereum_rust in…
fmoletta Nov 4, 2024
b270580
Update workflow
fmoletta Nov 4, 2024
09f688b
Clippy
fmoletta Nov 5, 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
3 changes: 3 additions & 0 deletions .github/workflows/hive.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ jobs:
- simulation: discv4
name: "Devp2p discv4 tests"
run_command: make run-hive SIMULATION=devp2p TEST_PATTERN="discv4"
- simulation: snap
name: "Devp2p snap tests"
run_command: make run-hive SIMULATION=devp2p TEST_PATTERN="/AccountRange|StorageRanges"
- simulation: engine
name: "Engine tests"
run_command: make run-hive SIMULATION=ethereum/engine TEST_PATTERN="/Blob Transactions On Block 1, Cancun Genesis|Blob Transactions On Block 1, Shanghai Genesis|Blob Transaction Ordering, Single Account, Single Blob|Blob Transaction Ordering, Single Account, Dual Blob|Blob Transaction Ordering, Multiple Accounts|Replace Blob Transactions|Parallel Blob Transactions|ForkchoiceUpdatedV3 Modifies Payload ID on Different Beacon Root|NewPayloadV3 After Cancun|NewPayloadV3 Versioned Hashes|ForkchoiceUpdated Version on Payload Request"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ stop-localnet-silent:
@kurtosis enclave stop lambdanet >/dev/null 2>&1 || true
@kurtosis enclave rm lambdanet --force >/dev/null 2>&1 || true

HIVE_REVISION := ccf28e5c3e940b2bc4b4f387317ee6a46f5d15c8
HIVE_REVISION := 421852ec25e4e608fe5460656f4bf0637649619e
# Shallow clones can't specify a single revision, but at least we avoid working
# the whole history by making it shallow since a given date (one day before our
# target revision).
Expand Down
42 changes: 0 additions & 42 deletions crates/common/rlp/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ pub trait RLPEncode {
}
}

pub trait RLPEncodeSlim {
fn encode(&self, buf: &mut dyn BufMut);

fn length(&self) -> usize {
let mut buf = Vec::new();
self.encode(&mut buf);
buf.len()
}
}

impl RLPEncode for bool {
#[inline(always)]
fn encode(&self, buf: &mut dyn BufMut) {
Expand Down Expand Up @@ -378,38 +368,6 @@ impl RLPEncode for ethereum_types::H256 {
}
}

impl RLPEncodeSlim for ethereum_types::H256 {
fn encode(&self, buf: &mut dyn BufMut) {
self.as_bytes().encode(buf)
}
}

impl<T: RLPEncodeSlim> RLPEncodeSlim for Vec<T> {
fn encode(&self, buf: &mut dyn BufMut) {
if self.is_empty() {
buf.put_u8(0xc0);
} else {
let mut total_len = 0;
for item in self {
total_len += item.length();
}
encode_length(total_len, buf);
for item in self {
item.encode(buf);
}
}
}
}

impl<S: RLPEncodeSlim, T: RLPEncodeSlim> RLPEncodeSlim for (S, T) {
fn encode(&self, buf: &mut dyn BufMut) {
let total_len = self.0.length() + self.1.length();
encode_length(total_len, buf);
self.0.encode(buf);
self.1.encode(buf);
}
}

impl RLPEncode for ethereum_types::H264 {
fn encode(&self, buf: &mut dyn BufMut) {
self.as_bytes().encode(buf)
Expand Down
9 changes: 0 additions & 9 deletions crates/common/rlp/structs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::encode::RLPEncodeSlim;

use super::{
decode::{decode_rlp_item, get_item_with_prefix, RLPDecode},
encode::{encode_length, RLPEncode},
Expand Down Expand Up @@ -185,13 +183,6 @@ impl<'a> Encoder<'a> {
self
}

/// Stores a field to be encoded, but in slim format
/// https://github.com/ethereum/devp2p/blob/master/caps/snap.md#data-format
pub fn encode_slim_field<T: RLPEncodeSlim>(mut self, value: &T) -> Self {
<T as RLPEncodeSlim>::encode(value, &mut self.temp_buf);
self
}

/// If `Some`, stores a field to be encoded, else does nothing.
pub fn encode_optional_field<T: RLPEncode>(mut self, opt_value: &Option<T>) -> Self {
if let Some(value) = opt_value {
Expand Down
27 changes: 2 additions & 25 deletions crates/common/types/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use ethereum_types::{H256, U256};
use sha3::{Digest as _, Keccak256};

use ethereum_rust_rlp::{
constants::{RLP_EMPTY_LIST, RLP_NULL},
constants::RLP_NULL,
decode::RLPDecode,
encode::{RLPEncode, RLPEncodeSlim},
encode::RLPEncode,
error::RLPDecodeError,
structs::{Decoder, Encoder},
};
Expand Down Expand Up @@ -100,17 +100,6 @@ impl RLPEncode for AccountInfo {
}
}

impl RLPEncodeSlim for AccountInfo {
fn encode(&self, buf: &mut dyn bytes::BufMut) {
// TODO: check if it's okay to use RLP_EMPTY_LIST
Encoder::new(buf)
.encode_field(&RLP_EMPTY_LIST)
.encode_field(&self.balance)
.encode_field(&self.nonce)
.finish();
}
}

impl RLPDecode for AccountInfo {
fn decode_unfinished(rlp: &[u8]) -> Result<(AccountInfo, &[u8]), RLPDecodeError> {
let decoder = Decoder::new(rlp)?;
Expand All @@ -137,18 +126,6 @@ impl RLPEncode for AccountState {
}
}

impl RLPEncodeSlim for AccountState {
fn encode(&self, buf: &mut dyn bytes::BufMut) {
// TODO: check if it's okay to use RLP_EMPTY_LIST
Encoder::new(buf)
.encode_field(&self.nonce)
.encode_field(&self.balance)
.encode_field(&RLP_EMPTY_LIST)
.encode_field(&self.code_hash)
.finish();
}
}

impl RLPDecode for AccountState {
fn decode_unfinished(rlp: &[u8]) -> Result<(AccountState, &[u8]), RLPDecodeError> {
let decoder = Decoder::new(rlp)?;
Expand Down
1 change: 1 addition & 0 deletions crates/networking/p2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tokio.workspace = true
bytes.workspace = true
hex.workspace = true
thiserror.workspace = true
lazy_static.workspace = true

k256 = { version = "0.13.3", features = ["ecdh"] }
sha3 = "0.10.8"
Expand Down
1 change: 1 addition & 0 deletions crates/networking/p2p/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub mod bootnode;
pub(crate) mod discv4;
pub(crate) mod kademlia;
pub mod rlpx;
pub(crate) mod snap;
pub mod types;

const MAX_DISC_PACKET_SIZE: usize = 1280;
Expand Down
16 changes: 13 additions & 3 deletions crates/networking/p2p/rlpx/connection.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
rlpx::{eth::backend, handshake::encode_ack_message, message::Message, p2p, utils::id2pubkey},
snap::{process_account_range_request, process_storage_ranges_request},
MAX_DISC_PACKET_SIZE,
};

Expand All @@ -25,9 +26,8 @@ use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
use tracing::{error, info};
const CAP_P2P: (Capability, u8) = (Capability::P2p, 5);
const CAP_ETH: (Capability, u8) = (Capability::Eth, 68);
//const CAP_SNAP: (Capability, u8) = (Capability::Snap, 1);
const SUPPORTED_CAPABILITIES: [(Capability, u8); 2] = [CAP_P2P, CAP_ETH];
// pub const SUPPORTED_CAPABILITIES: [(&str, u8); 3] = [CAP_P2P, CAP_ETH, CAP_SNAP)];
const CAP_SNAP: (Capability, u8) = (Capability::Snap, 1);
const SUPPORTED_CAPABILITIES: [(Capability, u8); 3] = [CAP_P2P, CAP_ETH, CAP_SNAP];

pub(crate) type Aes256Ctr64BE = ctr::Ctr64BE<aes::Aes256>;

Expand Down Expand Up @@ -145,6 +145,16 @@ impl<S: AsyncWrite + AsyncRead + std::marker::Unpin> RLPxConnection<S> {
Message::Ping(_) => info!("Received Ping"),
Message::Pong(_) => info!("Received Pong"),
Message::Status(_) => info!("Received Status"),
Message::GetAccountRange(req) => {
let response =
process_account_range_request(req, self.storage.clone())?;
self.send(Message::AccountRange(response)).await
}
Message::GetStorageRanges(req) => {
let response =
process_storage_ranges_request(req, self.storage.clone())?;
self.send(Message::StorageRanges(response)).await
}
// TODO: Add new message types and handlers as they are implemented
message => return Err(RLPxError::UnexpectedMessage(message)),
};
Expand Down
3 changes: 3 additions & 0 deletions crates/networking/p2p/rlpx/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::rlpx::message::Message;
use ethereum_rust_storage::error::StoreError;
use thiserror::Error;

// TODO improve errors
Expand All @@ -10,4 +11,6 @@ pub(crate) enum RLPxError {
InvalidState(String),
#[error("Unexpected message: {0}")]
UnexpectedMessage(Message),
#[error(transparent)]
Store(#[from] StoreError),
}
1 change: 0 additions & 1 deletion crates/networking/p2p/rlpx/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ pub(crate) async fn write<S: AsyncWrite + std::marker::Unpin>(
};
state.egress_mac.update(frame_mac_seed);
let frame_mac = state.egress_mac.clone().finalize();

// Send frame-mac
stream.write_all(&frame_mac[..16]).await.unwrap();
}
Expand Down
34 changes: 34 additions & 0 deletions crates/networking/p2p/rlpx/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use std::fmt::Display;

use super::eth::status::StatusMessage;
use super::p2p::{DisconnectMessage, HelloMessage, PingMessage, PongMessage};
use super::snap::{AccountRange, GetAccountRange, GetStorageRanges, StorageRanges};

use ethereum_rust_rlp::encode::RLPEncode;

pub trait RLPxMessage: Sized {
fn encode(&self, buf: &mut dyn BufMut) -> Result<(), RLPEncodeError>;
Expand All @@ -17,6 +20,11 @@ pub(crate) enum Message {
Ping(PingMessage),
Pong(PongMessage),
Status(StatusMessage),
// snap capability
GetAccountRange(GetAccountRange),
AccountRange(AccountRange),
GetStorageRanges(GetStorageRanges),
StorageRanges(StorageRanges),
}

impl Message {
Expand All @@ -27,6 +35,12 @@ impl Message {
0x02 => Ok(Message::Ping(PingMessage::decode(msg_data)?)),
0x03 => Ok(Message::Pong(PongMessage::decode(msg_data)?)),
0x10 => Ok(Message::Status(StatusMessage::decode(msg_data)?)),
0x21 => Ok(Message::GetAccountRange(GetAccountRange::decode(msg_data)?)),
0x22 => Ok(Message::AccountRange(AccountRange::decode(msg_data)?)),
0x23 => Ok(Message::GetStorageRanges(GetStorageRanges::decode(
msg_data,
)?)),
0x24 => Ok(Message::StorageRanges(StorageRanges::decode(msg_data)?)),
_ => Err(RLPDecodeError::MalformedData),
}
}
Expand All @@ -38,6 +52,22 @@ impl Message {
Message::Ping(msg) => msg.encode(buf),
Message::Pong(msg) => msg.encode(buf),
Message::Status(msg) => msg.encode(buf),
Message::GetAccountRange(msg) => {
0x21_u8.encode(buf);
msg.encode(buf)
}
Message::AccountRange(msg) => {
0x22_u8.encode(buf);
msg.encode(buf)
}
Message::GetStorageRanges(msg) => {
0x23_u8.encode(buf);
msg.encode(buf)
}
Message::StorageRanges(msg) => {
0x24_u8.encode(buf);
msg.encode(buf)
}
}
}
}
Expand All @@ -50,6 +80,10 @@ impl Display for Message {
Message::Ping(_) => "p2p:Ping".fmt(f),
Message::Pong(_) => "p2p:Pong".fmt(f),
Message::Status(_) => "eth:Status".fmt(f),
Message::GetAccountRange(_) => "snap::GetAccountRange".fmt(f),
Message::AccountRange(_) => "snap::AccountRange".fmt(f),
Message::GetStorageRanges(_) => "snap::GetStorageRanges".fmt(f),
Message::StorageRanges(_) => "snap::StorageRanges".fmt(f),
}
}
}
Loading
Loading