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(op-reth): Canyon receipts version #5526

Merged
merged 15 commits into from
Nov 27, 2023
2 changes: 2 additions & 0 deletions crates/interfaces/src/test_utils/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ pub fn random_receipt<R: Rng>(
},
#[cfg(feature = "optimism")]
deposit_nonce: None,
#[cfg(feature = "optimism")]
deposit_receipt_version: None,
}
}

Expand Down
6 changes: 6 additions & 0 deletions crates/net/eth-wire/src/types/receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ mod test {
logs: vec![],
#[cfg(feature = "optimism")]
deposit_nonce: None,
#[cfg(feature = "optimism")]
deposit_receipt_version: None,
},
bloom: Default::default(),
}]]);
Expand Down Expand Up @@ -121,6 +123,8 @@ mod test {
success: false,
#[cfg(feature = "optimism")]
deposit_nonce: None,
#[cfg(feature = "optimism")]
deposit_receipt_version: None,
},
bloom: hex!("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").into(),
},
Expand Down Expand Up @@ -158,6 +162,8 @@ mod test {
success: false,
#[cfg(feature = "optimism")]
deposit_nonce: None,
#[cfg(feature = "optimism")]
deposit_receipt_version: None,
},
bloom: hex!("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").into(),
},
Expand Down
2 changes: 2 additions & 0 deletions crates/payload/basic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,8 @@ where
logs: result.logs().into_iter().map(into_reth_log).collect(),
#[cfg(feature = "optimism")]
deposit_nonce: None,
#[cfg(feature = "optimism")]
deposit_receipt_version: None,
}));

// update add to total fees
Expand Down
9 changes: 9 additions & 0 deletions crates/payload/basic/src/optimism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ where
logs: result.logs().into_iter().map(into_reth_log).collect(),
#[cfg(feature = "optimism")]
deposit_nonce: depositor.map(|account| account.nonce),
// The deposit receipt version was introduced in Canyon to indicate an update to how
// receipt hashes should be computed when set. The state transition process
// ensures this is only set for post-Canyon deposit transactions.
#[cfg(feature = "optimism")]
deposit_receipt_version: chain_spec
.is_fork_active_at_timestamp(Hardfork::Canyon, attributes.timestamp)
.then_some(1),
}));

// append transaction to the list of executed transactions
Expand Down Expand Up @@ -204,6 +211,8 @@ where
logs: result.logs().into_iter().map(into_reth_log).collect(),
#[cfg(feature = "optimism")]
deposit_nonce: None,
#[cfg(feature = "optimism")]
deposit_receipt_version: None,
}));

// update add to total fees
Expand Down
12 changes: 12 additions & 0 deletions crates/primitives/src/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ mod tests {
logs: vec![],
#[cfg(feature = "optimism")]
deposit_nonce: Some(4012991u64),
#[cfg(feature = "optimism")]
deposit_receipt_version: None,
},
bloom: Bloom(hex!("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").into()),
},
Expand Down Expand Up @@ -321,6 +323,8 @@ mod tests {
],
#[cfg(feature = "optimism")]
deposit_nonce: None,
#[cfg(feature = "optimism")]
deposit_receipt_version: None,
},
bloom: Bloom(hex!("00001000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000800000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000040000000000004000000000080000000000000000000000000000000000000000000000000000008000000000000080020000000000000000000000000002000000000000000000000000000080000010000").into()),
},
Expand Down Expand Up @@ -363,6 +367,8 @@ mod tests {
],
#[cfg(feature = "optimism")]
deposit_nonce: None,
#[cfg(feature = "optimism")]
deposit_receipt_version: None,
},
bloom: Bloom(hex!("00000000000000000000200000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000002000000000020000000000000000000000000000000000000000000000000000000000000000020000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000040000000000004000000000080000000000000000000000000000000000000000000000000000008000000000000080020000000000000000000000000002000000000000000000000000000080000000000").into()),
},
Expand Down Expand Up @@ -435,6 +441,8 @@ mod tests {
],
#[cfg(feature = "optimism")]
deposit_nonce: None,
#[cfg(feature = "optimism")]
deposit_receipt_version: None,
},
bloom: Bloom(hex!("00200000000000000000000080000000000000000000000000040000100004000000000000000000000000100000000000000000000000000000100000000000000000000000000002000008000000200000000200000000020000000000000040000000000000000400000200000000000000000000000000000010000000000400000000010400000000000000000000000000002000c80000004080002000000000000000400200000000800000000000000000000000000000000000000000000002000000000000000000000000000000000100001000000000000000000000002000000000000000000000010000000000000000000000800000800000").into()),
},
Expand Down Expand Up @@ -477,6 +485,8 @@ mod tests {
],
#[cfg(feature = "optimism")]
deposit_nonce: None,
#[cfg(feature = "optimism")]
deposit_receipt_version: None,
},
bloom: Bloom(hex!("00000000000000000000000000000000400000000000000000000000000000000000004000000000000001000000000000000002000000000100000000000000000000000000000000000008000000000000000000000000000000000000000004000000020000000000000000000800000000000000000000000010200100200008000002000000000000000000800000000000000000000002000000000000000000000000000000080000000000000000000000004000000000000000000000000002000000000000000000000000000000000000200000000000000020002000000000000000002000000000000000000000000000000000000000000000").into()),
},
Expand All @@ -498,6 +508,8 @@ mod tests {
logs,
#[cfg(feature = "optimism")]
deposit_nonce: None,
#[cfg(feature = "optimism")]
deposit_receipt_version: None,
},
bloom,
};
Expand Down
97 changes: 88 additions & 9 deletions crates/primitives/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,17 @@ pub struct Receipt {
pub cumulative_gas_used: u64,
/// Log send from contracts.
pub logs: Vec<Log>,
/// Deposit nonce for Optimism deposited transactions
/// Deposit nonce for Optimism deposit transactions
#[cfg(feature = "optimism")]
pub deposit_nonce: Option<u64>,
/// Deposit receipt version for Optimism deposit transactions
///
///
/// The deposit receipt version was introduced in Canyon to indicate an update to how
/// receipt hashes should be computed when set. The state transition process
/// ensures this is only set for post-Canyon deposit transactions.
#[cfg(feature = "optimism")]
pub deposit_receipt_version: Option<u64>,
}

impl Receipt {
Expand Down Expand Up @@ -210,15 +218,29 @@ impl proptest::arbitrary::Arbitrary for Receipt {
success in any::<bool>(),
cumulative_gas_used in any::<u64>(),
logs in proptest::collection::vec(proptest::arbitrary::any::<Log>(), 0..=20),
_deposit_nonce in any::<Option<u64>>()) -> Receipt
_deposit_nonce in any::<Option<u64>>(),
_deposit_receipt_version in any::<Option<u64>>()) -> Receipt
{
// Only receipts for deposit transactions may contain a deposit nonce
#[cfg(feature = "optimism")]
let (deposit_nonce, deposit_receipt_version) = if tx_type == TxType::DEPOSIT {
// The deposit receipt version is only present if the deposit nonce is present
let deposit_receipt_version = _deposit_nonce.and(_deposit_receipt_version);
(_deposit_nonce, deposit_receipt_version)
} else {
(None, None)
};

Receipt { tx_type,
success,
cumulative_gas_used,
logs,
// Only receipts for deposit transactions may contain a deposit nonce
#[cfg(feature = "optimism")]
deposit_nonce: (tx_type == TxType::DEPOSIT).then_some(_deposit_nonce).flatten()
deposit_nonce,
// Only receipts for deposit transactions may contain a deposit nonce
#[cfg(feature = "optimism")]
deposit_receipt_version
}
}
};
Expand All @@ -238,8 +260,14 @@ impl<'a> arbitrary::Arbitrary<'a> for Receipt {

// Only receipts for deposit transactions may contain a deposit nonce
#[cfg(feature = "optimism")]
let deposit_nonce =
if tx_type == TxType::DEPOSIT { Option::<u64>::arbitrary(u)? } else { None };
let (deposit_nonce, deposit_receipt_version) = if tx_type == TxType::DEPOSIT {
let deposit_nonce = Option::<u64>::arbitrary(u)?;
let deposit_nonce_version =
deposit_nonce.map(|_| Option::<u64>::arbitrary(u)).transpose()?.flatten();
(deposit_nonce, deposit_nonce_version)
} else {
(None, None)
};

Ok(Self {
tx_type,
Expand All @@ -248,6 +276,8 @@ impl<'a> arbitrary::Arbitrary<'a> for Receipt {
logs,
#[cfg(feature = "optimism")]
deposit_nonce,
#[cfg(feature = "optimism")]
deposit_receipt_version,
})
}
}
Expand Down Expand Up @@ -275,12 +305,20 @@ impl ReceiptWithBloom {
let receipt = match tx_type {
#[cfg(feature = "optimism")]
TxType::DEPOSIT => {
let consumed = started_len - b.len();
let has_nonce = rlp_head.payload_length - consumed > 0;
let remaining = |b: &[u8]| rlp_head.payload_length - (started_len - b.len()) > 0;
let deposit_nonce =
if has_nonce { Some(alloy_rlp::Decodable::decode(b)?) } else { None };
remaining(b).then(|| alloy_rlp::Decodable::decode(b)).transpose()?;
let deposit_receipt_version =
remaining(b).then(|| alloy_rlp::Decodable::decode(b)).transpose()?;

Receipt { tx_type, success, cumulative_gas_used, logs, deposit_nonce }
Receipt {
tx_type,
success,
cumulative_gas_used,
logs,
deposit_nonce,
deposit_receipt_version,
}
}
_ => Receipt {
tx_type,
Expand All @@ -289,6 +327,8 @@ impl ReceiptWithBloom {
logs,
#[cfg(feature = "optimism")]
deposit_nonce: None,
#[cfg(feature = "optimism")]
deposit_receipt_version: None,
},
};

Expand Down Expand Up @@ -421,6 +461,9 @@ impl<'a> ReceiptWithBloomEncoder<'a> {
if let Some(deposit_nonce) = self.receipt.deposit_nonce {
rlp_head.payload_length += deposit_nonce.length();
}
if let Some(deposit_receipt_version) = self.receipt.deposit_receipt_version {
rlp_head.payload_length += deposit_receipt_version.length();
}
}

rlp_head
Expand All @@ -438,6 +481,9 @@ impl<'a> ReceiptWithBloomEncoder<'a> {
if let Some(deposit_nonce) = self.receipt.deposit_nonce {
deposit_nonce.encode(out)
}
if let Some(deposit_receipt_version) = self.receipt.deposit_receipt_version {
deposit_receipt_version.encode(out)
}
}
}

Expand Down Expand Up @@ -528,6 +574,8 @@ mod tests {
success: false,
#[cfg(feature = "optimism")]
deposit_nonce: None,
#[cfg(feature = "optimism")]
deposit_receipt_version: None,
},
bloom: [0; 256].into(),
};
Expand Down Expand Up @@ -560,6 +608,8 @@ mod tests {
success: false,
#[cfg(feature = "optimism")]
deposit_nonce: None,
#[cfg(feature = "optimism")]
deposit_receipt_version: None,
},
bloom: [0; 256].into(),
};
Expand All @@ -581,6 +631,7 @@ mod tests {
logs: vec![],
success: true,
deposit_nonce: Some(4012991),
deposit_receipt_version: None,
},
bloom: [0; 256].into(),
};
Expand All @@ -593,6 +644,32 @@ mod tests {
assert_eq!(buf.freeze(), &data[..]);
}

#[cfg(feature = "optimism")]
#[test]
fn decode_deposit_receipt_canyon_roundtrip() {
let data = hex!("7ef9010d0182b741b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0833d3bbf01");

// Deposit Receipt (post-regolith)
let expected = ReceiptWithBloom {
receipt: Receipt {
tx_type: TxType::DEPOSIT,
cumulative_gas_used: 46913,
logs: vec![],
success: true,
deposit_nonce: Some(4012991),
deposit_receipt_version: Some(1),
},
bloom: [0; 256].into(),
};

let receipt = ReceiptWithBloom::decode(&mut &data[..]).unwrap();
assert_eq!(receipt, expected);

let mut buf = BytesMut::default();
expected.encode_inner(&mut buf, false);
assert_eq!(buf.freeze(), &data[..]);
}

#[test]
fn gigantic_receipt() {
let receipt = Receipt {
Expand All @@ -617,6 +694,8 @@ mod tests {
],
#[cfg(feature = "optimism")]
deposit_nonce: None,
#[cfg(feature = "optimism")]
deposit_receipt_version: None,
};

let mut data = vec![];
Expand Down
8 changes: 8 additions & 0 deletions crates/revm/src/optimism/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ impl<'a> BlockExecutor for EVMProcessor<'a> {
logs: result.into_logs().into_iter().map(into_reth_log).collect(),
#[cfg(feature = "optimism")]
deposit_nonce: depositor.map(|account| account.nonce),
// The deposit receipt version was introduced in Canyon to indicate an update to how
// receipt hashes should be computed when set. The state transition process ensures
// this is only set for post-Canyon deposit transactions.
#[cfg(feature = "optimism")]
deposit_receipt_version: self
.chain_spec()
.is_fork_active_at_timestamp(Hardfork::Canyon, block.timestamp)
.then_some(1),
});
}

Expand Down
2 changes: 2 additions & 0 deletions crates/rpc/rpc/src/eth/api/pending_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ impl PendingBlockEnv {
logs: result.logs().into_iter().map(into_reth_log).collect(),
#[cfg(feature = "optimism")]
deposit_nonce: None,
#[cfg(feature = "optimism")]
deposit_receipt_version: None,
}));

// append transaction to the list of executed transactions
Expand Down
4 changes: 4 additions & 0 deletions crates/storage/provider/src/test_utils/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ fn block1(number: BlockNumber) -> (SealedBlockWithSenders, BundleStateWithReceip
}],
#[cfg(feature = "optimism")]
deposit_nonce: None,
#[cfg(feature = "optimism")]
deposit_receipt_version: None,
})]]),
number,
);
Expand Down Expand Up @@ -196,6 +198,8 @@ fn block2(
}],
#[cfg(feature = "optimism")]
deposit_nonce: None,
#[cfg(feature = "optimism")]
deposit_receipt_version: None,
})]]),
number,
);
Expand Down
Loading