Skip to content

Commit

Permalink
fix(p2p_proto/receipt): L1 handler message hashes are 256 bit hashes
Browse files Browse the repository at this point in the history
This change introduces a `Hash256` type to describe fields which
represent 256 bit hash values (like the output of Keccak256).
  • Loading branch information
kkovaacs committed Sep 13, 2024
1 parent 19aebe9 commit 03d9788
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
6 changes: 3 additions & 3 deletions crates/p2p/src/client/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::io::Read;

use anyhow::Context;
use p2p_proto::class::{Cairo0Class, Cairo1Class, Cairo1EntryPoints, SierraEntryPoint};
use p2p_proto::common::{Address, Hash};
use p2p_proto::common::{Address, Hash, Hash256};
use p2p_proto::receipt::execution_resources::BuiltinCounter;
use p2p_proto::receipt::{
DeclareTransactionReceipt,
Expand Down Expand Up @@ -364,9 +364,9 @@ impl ToDto<p2p_proto::receipt::Receipt> for (&TransactionVariant, Receipt) {
TransactionVariant::InvokeV0(_)
| TransactionVariant::InvokeV1(_)
| TransactionVariant::InvokeV3(_) => Invoke(InvokeTransactionReceipt { common }),
TransactionVariant::L1Handler(_) => L1Handler(L1HandlerTransactionReceipt {
TransactionVariant::L1Handler(tx) => L1Handler(L1HandlerTransactionReceipt {
common,
msg_hash: Hash(Felt::ZERO), // TODO what is this
msg_hash: Hash256(tx.calculate_message_hash()),
}),
}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/p2p_proto/proto/common.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ message Felt252 {
bytes elements = 1;
}

// A hash value represented as a Felt252
message Hash {
bytes elements = 1;
}

// A 256 bit hash value (like Keccak256)
message Hash256 {
bytes elements = 1;
}

message Hashes {
repeated Hash items = 1;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/p2p_proto/proto/receipt.proto
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ message Receipt {

message L1Handler {
Common common = 1;
starknet.common.Hash msg_hash = 2;
starknet.common.Hash256 msg_hash = 2;
}

message Declare {
Expand Down
34 changes: 34 additions & 0 deletions crates/p2p_proto/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@ use std::num::NonZeroU64;
use fake::Dummy;
use libp2p_identity::PeerId;
use pathfinder_crypto::Felt;
use primitive_types::H256;
use rand::Rng;

use crate::{proto, ToProtobuf, TryFromProtobuf};

#[derive(Debug, Copy, Clone, PartialEq, Eq, Dummy, std::hash::Hash, Default)]
pub struct Hash(pub Felt);

#[derive(Debug, Copy, Clone, PartialEq, Eq, std::hash::Hash, Default)]
pub struct Hash256(pub primitive_types::H256);

impl<T> Dummy<T> for Hash256 {
fn dummy_with_rng<R: Rng + ?Sized>(_: &T, rng: &mut R) -> Self {
Self(H256::random_using(rng))
}
}

#[derive(Debug, Clone, PartialEq, Eq, ToProtobuf, TryFromProtobuf, Dummy)]
#[protobuf(name = "crate::proto::common::Hashes")]
pub struct Hashes {
Expand Down Expand Up @@ -135,6 +145,30 @@ impl TryFromProtobuf<proto::common::Hash> for Hash {
}
}

impl ToProtobuf<proto::common::Hash256> for Hash256 {
fn to_protobuf(self) -> proto::common::Hash256 {
proto::common::Hash256 {
elements: self.0.as_fixed_bytes().into(),
}
}
}

impl TryFromProtobuf<proto::common::Hash256> for Hash256 {
fn try_from_protobuf(
input: proto::common::Hash256,
field_name: &'static str,
) -> Result<Self, std::io::Error> {
if input.elements.len() != H256::len_bytes() {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!("Invalid field element {field_name}: expected to be 32 bytes long"),
));
}
let hash = H256::from_slice(&input.elements);
Ok(Hash256(hash))
}
}

impl ToProtobuf<proto::common::Address> for Address {
fn to_protobuf(self) -> proto::common::Address {
proto::common::Address {
Expand Down
4 changes: 2 additions & 2 deletions crates/p2p_proto/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use fake::Dummy;
use pathfinder_crypto::Felt;
use primitive_types::H160;

use crate::common::Hash;
use crate::common::Hash256;
use crate::{proto, proto_field, ToProtobuf, TryFromProtobuf};

#[derive(Debug, Clone, PartialEq, Eq, ToProtobuf, TryFromProtobuf, Dummy)]
Expand Down Expand Up @@ -76,7 +76,7 @@ pub struct InvokeTransactionReceipt {
#[protobuf(name = "crate::proto::receipt::receipt::L1Handler")]
pub struct L1HandlerTransactionReceipt {
pub common: ReceiptCommon,
pub msg_hash: Hash,
pub msg_hash: Hash256,
}

#[derive(Debug, Clone, PartialEq, Eq, ToProtobuf, TryFromProtobuf, Dummy)]
Expand Down

0 comments on commit 03d9788

Please sign in to comment.