Skip to content

Commit

Permalink
fix Transaction::length
Browse files Browse the repository at this point in the history
  • Loading branch information
Wollac committed Aug 11, 2023
1 parent ce267b6 commit 744b671
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/src/block_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ where

// Add receipt and tx to tries
let trie_key = tx_no.to_rlp();
dbg!(tx_no, &tx);
tx_trie
.insert_rlp(&trie_key, tx)
.context("failed to insert transaction")?;
Expand Down
54 changes: 51 additions & 3 deletions primitives/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,12 @@ impl Encodable for Transaction {
/// Returns the length of the encoding of the transaction in bytes.
#[inline]
fn length(&self) -> usize {
let mut payload_length = self.essence.payload_length() + self.signature.payload_length();
let payload_length = self.essence.payload_length() + self.signature.payload_length();
let mut length = payload_length + alloy_rlp::length_of_length(payload_length);
if self.tx_type() != 0 {
payload_length += 1;
length += 1;
}
payload_length + alloy_rlp::length_of_length(payload_length)
length
}
}

Expand Down Expand Up @@ -557,4 +558,51 @@ mod tests {
recovered.to_string()
);
}

#[test]
fn rlp() {
// Tx: 0x275631a3549307b2e8c93b18dfcc0fe8aedf0276bb650c28eaa0a8a011d18867
let tx = json!({
"Eip1559": {
"chain_id": 1,
"nonce": 267,
"max_priority_fee_per_gas": "0x05f5e100",
"max_fee_per_gas": "0x0cb2bf61c2",
"gas_limit": "0x0278be",
"to": { "Call": "0x00005ea00ac477b1030ce78506496e8c2de24bf5" },
"value": "0x01351609ff758000",
"data": "0x161ac21f0000000000000000000000007de6f03b8b50b835f706e51a40b3224465802ddc0000000000000000000000000000a26b00c1f0df003000390027140000faa71900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003360c6ebe",
"access_list": []
}
});
let essence: TxEssence = serde_json::from_value(tx).unwrap();

let encoded = alloy_rlp::encode(&essence);
assert_eq!(encoded.len(), essence.length());
assert_eq!(
essence.payload_length() + alloy_rlp::length_of_length(essence.payload_length()),
encoded.len()
);

let signature: TxSignature = serde_json::from_value(json!({
"v": 0,
"r": "0x5fc1441d3469a16715c862240794ef76656c284930e08820b79fd703a98b380a",
"s": "0x37488b0ceef613dc68116ed44b8e63769dbcf039222e25acc1cb9e85e777ade2"
}))
.unwrap();

let encoded = alloy_rlp::encode(&signature);
assert_eq!(encoded.len(), signature.length());
assert_eq!(
signature.payload_length() + alloy_rlp::length_of_length(signature.payload_length()),
encoded.len()
);

let transaction = Transaction { essence, signature };
dbg!(transaction.hash());

let encoded = alloy_rlp::encode(&transaction);
dbg!(Bytes::from(encoded.clone()));
assert_eq!(encoded.len(), transaction.length());
}
}

0 comments on commit 744b671

Please sign in to comment.