Skip to content

Commit

Permalink
feat: make signature methods generic over EncodableSignature (#1138)
Browse files Browse the repository at this point in the history
  • Loading branch information
leruaa authored Aug 8, 2024
1 parent fd159f6 commit 25f0170
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 34 deletions.
12 changes: 5 additions & 7 deletions crates/consensus/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl TxEip1559 {
/// - `value`
/// - `data` (`input`)
/// - `access_list`
pub(crate) fn decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
pub fn decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Ok(Self {
chain_id: Decodable::decode(buf)?,
nonce: Decodable::decode(buf)?,
Expand Down Expand Up @@ -179,12 +179,10 @@ impl TxEip1559 {
/// Inner encoding function that is used for both rlp [`Encodable`] trait and for calculating
/// hash that for eip2718 does not require a rlp header.
#[doc(hidden)]
pub fn encode_with_signature(
&self,
signature: &Signature,
out: &mut dyn BufMut,
with_header: bool,
) {
pub fn encode_with_signature<S>(&self, signature: &S, out: &mut dyn BufMut, with_header: bool)
where
S: EncodableSignature,
{
let payload_length = self.fields_len() + signature.rlp_vrs_len();
if with_header {
Header {
Expand Down
12 changes: 5 additions & 7 deletions crates/consensus/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl TxEip2930 {
/// - `value`
/// - `data` (`input`)
/// - `access_list`
pub(crate) fn decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
pub fn decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Ok(Self {
chain_id: Decodable::decode(buf)?,
nonce: Decodable::decode(buf)?,
Expand Down Expand Up @@ -157,12 +157,10 @@ impl TxEip2930 {
/// Inner encoding function that is used for both rlp [`Encodable`] trait and for calculating
/// hash that for eip2718 does not require a rlp header
#[doc(hidden)]
pub fn encode_with_signature(
&self,
signature: &Signature,
out: &mut dyn BufMut,
with_header: bool,
) {
pub fn encode_with_signature<S>(&self, signature: &S, out: &mut dyn BufMut, with_header: bool)
where
S: EncodableSignature,
{
let payload_length = self.fields_len() + signature.rlp_vrs_len();
if with_header {
Header {
Expand Down
25 changes: 12 additions & 13 deletions crates/consensus/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,10 @@ impl TxEip4844Variant {
/// If `with_header` is `true`, the following will be encoded:
/// `rlp(tx_type (0x03) || rlp([transaction_payload_body, blobs, commitments, proofs]))`
#[doc(hidden)]
pub fn encode_with_signature(
&self,
signature: &Signature,
out: &mut dyn BufMut,
with_header: bool,
) {
pub fn encode_with_signature<S>(&self, signature: &S, out: &mut dyn BufMut, with_header: bool)
where
S: EncodableSignature,
{
let payload_length = match self {
Self::TxEip4844(tx) => tx.fields_len() + signature.rlp_vrs_len(),
Self::TxEip4844WithSidecar(tx) => {
Expand Down Expand Up @@ -519,12 +517,10 @@ impl TxEip4844 {
/// Inner encoding function that is used for both rlp [`Encodable`] trait and for calculating
/// hash that for eip2718 does not require a rlp header
#[doc(hidden)]
pub fn encode_with_signature(
&self,
signature: &Signature,
out: &mut dyn BufMut,
with_header: bool,
) {
pub fn encode_with_signature<S>(&self, signature: &S, out: &mut dyn BufMut, with_header: bool)
where
S: EncodableSignature,
{
let payload_length = self.fields_len() + signature.rlp_vrs_len();
if with_header {
Header {
Expand Down Expand Up @@ -778,7 +774,10 @@ impl TxEip4844WithSidecar {
///
/// where `tx_payload` is the RLP encoding of the [TxEip4844] transaction fields:
/// `rlp([chain_id, nonce, max_priority_fee_per_gas, ..., v, r, s])`
pub fn encode_with_signature_fields(&self, signature: &Signature, out: &mut dyn BufMut) {
pub fn encode_with_signature_fields<S>(&self, signature: &S, out: &mut dyn BufMut)
where
S: EncodableSignature,
{
let inner_payload_length = self.tx.fields_len() + signature.rlp_vrs_len();
let inner_header = Header { list: true, payload_length: inner_payload_length };

Expand Down
12 changes: 5 additions & 7 deletions crates/consensus/src/transaction/eip7702.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl TxEip7702 {
/// - `data` (`input`)
/// - `access_list`
/// - `authorization_list`
pub(crate) fn decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
pub fn decode_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Ok(Self {
chain_id: Decodable::decode(buf)?,
nonce: Decodable::decode(buf)?,
Expand Down Expand Up @@ -187,12 +187,10 @@ impl TxEip7702 {
/// Inner encoding function that is used for both rlp [`Encodable`] trait and for calculating
/// hash that for eip2718 does not require a rlp header.
#[doc(hidden)]
pub fn encode_with_signature(
&self,
signature: &Signature,
out: &mut dyn BufMut,
with_header: bool,
) {
pub fn encode_with_signature<S>(&self, signature: &S, out: &mut dyn BufMut, with_header: bool)
where
S: EncodableSignature,
{
let payload_length = self.fields_len() + signature.rlp_vrs_len();
if with_header {
Header {
Expand Down

0 comments on commit 25f0170

Please sign in to comment.