Skip to content

Commit

Permalink
do not reference private methods, improve encode_for_signing docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected committed Jan 2, 2024
1 parent 272ed22 commit 016177e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 15 deletions.
6 changes: 6 additions & 0 deletions crates/primitives/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ impl TxEip1559 {
}

/// Encodes the legacy transaction in RLP for signing.
///
/// This encodes the transaction as:
/// `tx_type || rlp(chain_id, nonce, max_priority_fee_per_gas, max_fee_per_gas, gas_limit, to,
/// value, input, access_list)`
///
/// Note that there is no rlp header before the transaction type byte.
pub(crate) fn encode_for_signing(&self, out: &mut dyn bytes::BufMut) {
out.put_u8(self.tx_type() as u8);
Header { list: true, payload_length: self.fields_len() }.encode(out);
Expand Down
5 changes: 5 additions & 0 deletions crates/primitives/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ impl TxEip2930 {
}

/// Encodes the legacy transaction in RLP for signing.
///
/// This encodes the transaction as:
/// `tx_type || rlp(chain_id, nonce, gas_price, gas_limit, to, value, input, access_list)`
///
/// Note that there is no rlp header before the transaction type byte.
pub(crate) fn encode_for_signing(&self, out: &mut dyn bytes::BufMut) {
out.put_u8(self.tx_type() as u8);
Header { list: true, payload_length: self.fields_len() }.encode(out);
Expand Down
6 changes: 6 additions & 0 deletions crates/primitives/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,12 @@ impl TxEip4844 {
}

/// Encodes the legacy transaction in RLP for signing.
///
/// This encodes the transaction as:
/// `tx_type || rlp(chain_id, nonce, max_priority_fee_per_gas, max_fee_per_gas, gas_limit, to,
/// value, input, access_list, max_fee_per_blob_gas, blob_versioned_hashes)`
///
/// Note that there is no rlp header before the transaction type byte.
pub(crate) fn encode_for_signing(&self, out: &mut dyn bytes::BufMut) {
out.put_u8(self.tx_type() as u8);
Header { list: true, payload_length: self.fields_len() }.encode(out);
Expand Down
9 changes: 9 additions & 0 deletions crates/primitives/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ impl TxLegacy {

/// Encodes EIP-155 arguments into the desired buffer. Only encodes values for legacy
/// transactions.
///
/// If a `chain_id` is `Some`, this encodes the `chain_id`, followed by two zeroes, as defined
/// by [EIP-155](https://eips.ethereum.org/EIPS/eip-155).
pub(crate) fn encode_eip155_fields(&self, out: &mut dyn bytes::BufMut) {
// if this is a legacy transaction without a chain ID, it must be pre-EIP-155
// and does not need to encode the chain ID for the signature hash encoding
Expand All @@ -136,6 +139,12 @@ impl TxLegacy {
}

/// Encodes the legacy transaction in RLP for signing, including the EIP-155 fields if possible.
///
/// If a `chain_id` is `Some`, this encodes the transaction as:
/// `rlp(nonce, gas_price, gas_limit, to, value, input, chain_id, 0, 0)`
///
/// Otherwise, this encodes the transaction as:
/// `rlp(nonce, gas_price, gas_limit, to, value, input)`
pub(crate) fn encode_for_signing(&self, out: &mut dyn bytes::BufMut) {
Header { list: true, payload_length: self.fields_len() + self.eip155_fields_len() }
.encode(out);
Expand Down
6 changes: 0 additions & 6 deletions crates/primitives/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,12 +672,6 @@ impl Default for Transaction {
impl Encodable for Transaction {
/// This encodes the transaction _without_ the signature, and is only suitable for creating a
/// hash intended for signing.
///
/// See the following docs for how each transaction type is encoded:
/// - [TxLegacy](TxLegacy::encode_for_signing)
/// - [TxEip2930](TxEip2930::encode_for_signing)
/// - [TxEip1559](TxEip1559::encode_for_signing)
/// - [TxEip4844](TxEip4844::encode_for_signing)
fn encode(&self, out: &mut dyn bytes::BufMut) {
match self {
Transaction::Legacy(legacy_tx) => {
Expand Down
13 changes: 6 additions & 7 deletions crates/primitives/src/transaction/pooled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,13 @@ impl Encodable for PooledTransactionsElement {
/// For legacy transactions, this encodes the transaction as `rlp(tx-data)`.
///
/// For EIP-2718 transactions, this encodes the transaction as `rlp(tx_type || rlp(tx-data)))`.
///
/// The encoding of `tx-data` depends on the transaction type. Refer to these docs for more
/// information on the exact format:
/// - [Legacy](TxLegacy::encode_with_signature)
/// - [EIP-2930](TxEip2930::encode_with_signature)
/// - [EIP-1559](TxEip1559::encode_with_signature)
/// - [EIP-4844](BlobTransaction::encode_with_type_inner)
fn encode(&self, out: &mut dyn bytes::BufMut) {
// The encoding of `tx-data` depends on the transaction type. Refer to these docs for more
// information on the exact format:
// - Legacy: TxLegacy::encode_with_signature
// - EIP-2930: TxEip2930::encode_with_signature
// - EIP-1559: TxEip1559::encode_with_signature
// - EIP-4844: BlobTransaction::encode_with_type_inner
match self {
Self::Legacy { transaction, signature, .. } => {
transaction.encode_with_signature(signature, out)
Expand Down
2 changes: 0 additions & 2 deletions crates/primitives/src/transaction/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,6 @@ impl From<BlobTransactionSidecar> for reth_rpc_types::BlobTransactionSidecar {

impl Encodable for BlobTransactionSidecar {
/// Encodes the inner [BlobTransactionSidecar] fields as RLP bytes, without a RLP header.
///
/// Refer to [BlobTransactionSidecar::encode_inner] for more information on the exact format.
fn encode(&self, out: &mut dyn BufMut) {
self.encode_inner(out)
}
Expand Down

0 comments on commit 016177e

Please sign in to comment.