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(p2p): P2P Crate #27

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ categories = ["cryptography", "cryptography::cryptocurrencies"]
[workspace]
members = [
"bin/hera",
"crates/p2p",
"crates/kona-providers",
"crates/ser",
]
Expand Down
16 changes: 16 additions & 0 deletions crates/p2p/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "p2p"
description = "Consensus P2P Stack for L2"
version = "0.0.0"
edition.workspace = true
authors.workspace = true
license.workspace = true
keywords.workspace = true
repository.workspace = true
categories.workspace = true
rust-version.workspace = true

[dependencies]
alloy.workspace = true

kona-primitives = { git = "https://github.com/ethereum-optimism/kona", default-features = true }
10 changes: 10 additions & 0 deletions crates/p2p/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//! Consensus P2P Stack

#![doc(issue_tracker_base_url = "https://github.com/paradigmxyz/op-rs/issues/")]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![cfg_attr(not(test), no_std)]

extern crate alloc;

pub mod types;
58 changes: 58 additions & 0 deletions crates/p2p/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//! Types for the P2P network.

use alloc::vec::Vec;
use alloy::primitives::{keccak256, Signature, B256};
use kona_primitives::L2ExecutionPayload;

/// An envelope around the execution payload for L2.
#[derive(Debug)]
pub struct ExecutionPayloadEnvelope {
/// The execution payload.
pub payload: L2ExecutionPayload,
/// A signature for the payload.
pub signature: Signature,
/// The hash of the payload.
pub hash: PayloadHash,
/// The parent beacon block root.
pub parent_beacon_block_root: Option<B256>,
}

/// Represents the Keccak256 hash of the block
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PayloadHash(B256);

impl From<&[u8]> for PayloadHash {
/// Returns the Keccak256 hash of a sequence of bytes
fn from(value: &[u8]) -> Self {
Self(keccak256(value))
}
}

impl PayloadHash {
/// The expected message that should be signed by the unsafe block signer.
pub fn signature_message(&self, chain_id: u64) -> B256 {
let domain = B256::ZERO;
let chain_id = B256::left_padding_from(&chain_id.to_be_bytes()[..]);
let payload_hash = self.0;

let data: Vec<u8> =
[domain.as_slice(), chain_id.as_slice(), payload_hash.as_slice()].concat();

keccak256(data)
}
}

#[cfg(test)]
mod tests {
use super::*;
use alloy::primitives::b256;

#[test]
fn test_signature_message() {
let inner = b256!("9999999999999999999999999999999999999999999999999999999999999999");
let hash = PayloadHash::from(inner.as_slice());
let chain_id = 10;
let expected = b256!("44a0e2b1aba1aae1771eddae1dcd2ad18a8cdac8891517153f03253e49d3f206");
assert_eq!(hash.signature_message(chain_id), expected);
}
}