-
Notifications
You must be signed in to change notification settings - Fork 866
/
sidecar.rs
508 lines (436 loc) · 18.9 KB
/
sidecar.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#![cfg(feature = "c-kzg")]
#![cfg_attr(docsrs, doc(cfg(feature = "c-kzg")))]
use crate::{
keccak256, Signature, Transaction, TransactionSigned, TxEip4844, TxHash, EIP4844_TX_TYPE_ID,
};
use crate::kzg::{
self, Blob, Bytes48, KzgSettings, BYTES_PER_BLOB, BYTES_PER_COMMITMENT, BYTES_PER_PROOF,
};
use alloy_rlp::{Decodable, Encodable, Error as RlpError, Header};
use bytes::BufMut;
use serde::{Deserialize, Serialize};
#[cfg(any(test, feature = "arbitrary"))]
use proptest::{
arbitrary::{any as proptest_any, ParamsFor},
collection::vec as proptest_vec,
strategy::{BoxedStrategy, Strategy},
};
#[cfg(any(test, feature = "arbitrary"))]
use crate::{
constants::eip4844::{FIELD_ELEMENTS_PER_BLOB, MAINNET_KZG_TRUSTED_SETUP},
kzg::{KzgCommitment, KzgProof, BYTES_PER_FIELD_ELEMENT},
};
/// An error that can occur when validating a [BlobTransaction].
#[derive(Debug, thiserror::Error)]
pub enum BlobTransactionValidationError {
/// Proof validation failed.
#[error("invalid KZG proof")]
InvalidProof,
/// An error returned by [`kzg`].
#[error("KZG error: {0:?}")]
KZGError(#[from] kzg::Error),
/// The inner transaction is not a blob transaction.
#[error("unable to verify proof for non blob transaction: {0}")]
NotBlobTransaction(u8),
}
/// A response to `GetPooledTransactions` that includes blob data, their commitments, and their
/// corresponding proofs.
///
/// This is defined in [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844#networking) as an element
/// of a `PooledTransactions` response.
#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct BlobTransaction {
/// The transaction hash.
pub hash: TxHash,
/// The transaction payload.
pub transaction: TxEip4844,
/// The transaction signature.
pub signature: Signature,
/// The transaction's blob sidecar.
pub sidecar: BlobTransactionSidecar,
}
impl BlobTransaction {
/// Constructs a new [BlobTransaction] from a [TransactionSigned] and a
/// [BlobTransactionSidecar].
///
/// Returns an error if the signed transaction is not [TxEip4844]
pub fn try_from_signed(
tx: TransactionSigned,
sidecar: BlobTransactionSidecar,
) -> Result<Self, (TransactionSigned, BlobTransactionSidecar)> {
let TransactionSigned { transaction, signature, hash } = tx;
match transaction {
Transaction::Eip4844(transaction) => Ok(Self { hash, transaction, signature, sidecar }),
transaction => {
let tx = TransactionSigned { transaction, signature, hash };
Err((tx, sidecar))
}
}
}
/// Verifies that the transaction's blob data, commitments, and proofs are all valid.
///
/// See also [TxEip4844::validate_blob]
pub fn validate(
&self,
proof_settings: &KzgSettings,
) -> Result<(), BlobTransactionValidationError> {
self.transaction.validate_blob(&self.sidecar, proof_settings)
}
/// Splits the [BlobTransaction] into its [TransactionSigned] and [BlobTransactionSidecar]
/// components.
pub fn into_parts(self) -> (TransactionSigned, BlobTransactionSidecar) {
let transaction = TransactionSigned {
transaction: Transaction::Eip4844(self.transaction),
hash: self.hash,
signature: self.signature,
};
(transaction, self.sidecar)
}
/// Encodes the [BlobTransaction] fields as RLP, with a tx type. If `with_header` is `false`,
/// the following will be encoded:
/// `tx_type (0x03) || rlp([transaction_payload_body, blobs, commitments, proofs])`
///
/// If `with_header` is `true`, the following will be encoded:
/// `rlp(tx_type (0x03) || rlp([transaction_payload_body, blobs, commitments, proofs]))`
///
/// NOTE: The header will be a byte string header, not a list header.
pub(crate) fn encode_with_type_inner(&self, out: &mut dyn bytes::BufMut, with_header: bool) {
// Calculate the length of:
// `tx_type || rlp([transaction_payload_body, blobs, commitments, proofs])`
//
// to construct and encode the string header
if with_header {
Header {
list: false,
// add one for the tx type
payload_length: 1 + self.payload_len(),
}
.encode(out);
}
out.put_u8(EIP4844_TX_TYPE_ID);
// Now we encode the inner blob transaction:
self.encode_inner(out);
}
/// Encodes the [BlobTransaction] fields as RLP, with the following format:
/// `rlp([transaction_payload_body, blobs, commitments, proofs])`
///
/// where `transaction_payload_body` is a list:
/// `[chain_id, nonce, max_priority_fee_per_gas, ..., y_parity, r, s]`
///
/// Note: this should be used only when implementing other RLP encoding methods, and does not
/// represent the full RLP encoding of the blob transaction.
pub(crate) fn encode_inner(&self, out: &mut dyn bytes::BufMut) {
// First we construct both required list headers.
//
// The `transaction_payload_body` length is the length of the fields, plus the length of
// its list header.
let tx_header = Header {
list: true,
payload_length: self.transaction.fields_len() + self.signature.payload_len(),
};
let tx_length = tx_header.length() + tx_header.payload_length;
// The payload length is the length of the `tranascation_payload_body` list, plus the
// length of the blobs, commitments, and proofs.
let payload_length = tx_length + self.sidecar.fields_len();
// First we use the payload len to construct the first list header
let blob_tx_header = Header { list: true, payload_length };
// Encode the blob tx header first
blob_tx_header.encode(out);
// Encode the inner tx list header, then its fields
tx_header.encode(out);
self.transaction.encode_fields(out);
// Encode the signature
self.signature.encode(out);
// Encode the blobs, commitments, and proofs
self.sidecar.encode_inner(out);
}
/// Ouputs the length of the RLP encoding of the blob transaction, including the tx type byte,
/// optionally including the length of a wrapping string header. If `with_header` is `false`,
/// the length of the following will be calculated:
/// `tx_type (0x03) || rlp([transaction_payload_body, blobs, commitments, proofs])`
///
/// If `with_header` is `true`, the length of the following will be calculated:
/// `rlp(tx_type (0x03) || rlp([transaction_payload_body, blobs, commitments, proofs]))`
pub(crate) fn payload_len_with_type(&self, with_header: bool) -> usize {
if with_header {
// Construct a header and use that to calculate the total length
let wrapped_header = Header {
list: false,
// add one for the tx type byte
payload_length: 1 + self.payload_len(),
};
// The total length is now the length of the header plus the length of the payload
// (which includes the tx type byte)
wrapped_header.length() + wrapped_header.payload_length
} else {
// Just add the length of the tx type to the payload length
1 + self.payload_len()
}
}
/// Outputs the length of the RLP encoding of the blob transaction with the following format:
/// `rlp([transaction_payload_body, blobs, commitments, proofs])`
///
/// where `transaction_payload_body` is a list:
/// `[chain_id, nonce, max_priority_fee_per_gas, ..., y_parity, r, s]`
///
/// Note: this should be used only when implementing other RLP encoding length methods, and
/// does not represent the full RLP encoding of the blob transaction.
pub(crate) fn payload_len(&self) -> usize {
// The `transaction_payload_body` length is the length of the fields, plus the length of
// its list header.
let tx_header = Header {
list: true,
payload_length: self.transaction.fields_len() + self.signature.payload_len(),
};
let tx_length = tx_header.length() + tx_header.payload_length;
// The payload length is the length of the `tranascation_payload_body` list, plus the
// length of the blobs, commitments, and proofs.
let payload_length = tx_length + self.sidecar.fields_len();
// We use the calculated payload len to construct the first list header, which encompasses
// everything in the tx - the length of the second, inner list header is part of
// payload_length
let blob_tx_header = Header { list: true, payload_length };
// The final length is the length of:
// * the outer blob tx header +
// * the inner tx header +
// * the inner tx fields +
// * the signature fields +
// * the sidecar fields
blob_tx_header.length() + blob_tx_header.payload_length
}
/// Decodes a [BlobTransaction] from RLP. This expects the encoding to be:
/// `rlp([transaction_payload_body, blobs, commitments, proofs])`
///
/// where `transaction_payload_body` is a list:
/// `[chain_id, nonce, max_priority_fee_per_gas, ..., y_parity, r, s]`
///
/// Note: this should be used only when implementing other RLP decoding methods, and does not
/// represent the full RLP decoding of the `PooledTransactionsElement` type.
pub(crate) fn decode_inner(data: &mut &[u8]) -> alloy_rlp::Result<Self> {
// decode the _first_ list header for the rest of the transaction
let outer_header = Header::decode(data)?;
if !outer_header.list {
return Err(RlpError::Custom("PooledTransactions blob tx must be encoded as a list"))
}
let outer_remaining_len = data.len();
// Now we need to decode the inner 4844 transaction and its signature:
//
// `[chain_id, nonce, max_priority_fee_per_gas, ..., y_parity, r, s]`
let inner_header = Header::decode(data)?;
if !inner_header.list {
return Err(RlpError::Custom(
"PooledTransactions inner blob tx must be encoded as a list",
))
}
let inner_remaining_len = data.len();
// inner transaction
let transaction = TxEip4844::decode_inner(data)?;
// signature
let signature = Signature::decode(data)?;
// the inner header only decodes the transaction and signature, so we check the length here
let inner_consumed = inner_remaining_len - data.len();
if inner_consumed != inner_header.payload_length {
return Err(RlpError::UnexpectedLength)
}
// All that's left are the blobs, commitments, and proofs
let sidecar = BlobTransactionSidecar::decode_inner(data)?;
// # Calculating the hash
//
// The full encoding of the `PooledTransaction` response is:
// `tx_type (0x03) || rlp([tx_payload_body, blobs, commitments, proofs])`
//
// The transaction hash however, is:
// `keccak256(tx_type (0x03) || rlp(tx_payload_body))`
//
// Note that this is `tx_payload_body`, not `[tx_payload_body]`, which would be
// `[[chain_id, nonce, max_priority_fee_per_gas, ...]]`, i.e. a list within a list.
//
// Because the pooled transaction encoding is different than the hash encoding for
// EIP-4844 transactions, we do not use the original buffer to calculate the hash.
//
// Instead, we use `encode_with_signature`, which RLP encodes the transaction with a
// signature for hashing without a header. We then hash the result.
let mut buf = Vec::new();
transaction.encode_with_signature(&signature, &mut buf, false);
let hash = keccak256(&buf);
// the outer header is for the entire transaction, so we check the length here
let outer_consumed = outer_remaining_len - data.len();
if outer_consumed != outer_header.payload_length {
return Err(RlpError::UnexpectedLength)
}
Ok(Self { transaction, hash, signature, sidecar })
}
}
/// This represents a set of blobs, and its corresponding commitments and proofs.
#[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
#[repr(C)]
pub struct BlobTransactionSidecar {
/// The blob data.
pub blobs: Vec<Blob>,
/// The blob commitments.
pub commitments: Vec<Bytes48>,
/// The blob proofs.
pub proofs: Vec<Bytes48>,
}
impl BlobTransactionSidecar {
/// Creates a new [BlobTransactionSidecar] using the given blobs, commitments, and proofs.
pub fn new(blobs: Vec<Blob>, commitments: Vec<Bytes48>, proofs: Vec<Bytes48>) -> Self {
Self { blobs, commitments, proofs }
}
/// Encodes the inner [BlobTransactionSidecar] fields as RLP bytes, without a RLP header.
///
/// This encodes the fields in the following order:
/// - `blobs`
/// - `commitments`
/// - `proofs`
#[inline]
pub(crate) fn encode_inner(&self, out: &mut dyn bytes::BufMut) {
BlobTransactionSidecarRlp::wrap_ref(self).encode(out);
}
/// Outputs the RLP length of the [BlobTransactionSidecar] fields, without a RLP header.
pub fn fields_len(&self) -> usize {
BlobTransactionSidecarRlp::wrap_ref(self).fields_len()
}
/// Decodes the inner [BlobTransactionSidecar] fields from RLP bytes, without a RLP header.
///
/// This decodes the fields in the following order:
/// - `blobs`
/// - `commitments`
/// - `proofs`
#[inline]
pub(crate) fn decode_inner(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Ok(BlobTransactionSidecarRlp::decode(buf)?.unwrap())
}
/// Calculates a size heuristic for the in-memory size of the [BlobTransactionSidecar].
#[inline]
pub fn size(&self) -> usize {
self.blobs.len() * BYTES_PER_BLOB + // blobs
self.commitments.len() * BYTES_PER_COMMITMENT + // commitments
self.proofs.len() * BYTES_PER_PROOF // proofs
}
}
impl From<reth_rpc_types::BlobTransactionSidecar> for BlobTransactionSidecar {
fn from(value: reth_rpc_types::BlobTransactionSidecar) -> Self {
// SAFETY: Same repr and size
unsafe { std::mem::transmute(value) }
}
}
impl From<BlobTransactionSidecar> for reth_rpc_types::BlobTransactionSidecar {
fn from(value: BlobTransactionSidecar) -> Self {
// SAFETY: Same repr and size
unsafe { std::mem::transmute(value) }
}
}
impl Encodable for BlobTransactionSidecar {
/// Encodes the inner [BlobTransactionSidecar] fields as RLP bytes, without a RLP header.
fn encode(&self, out: &mut dyn BufMut) {
self.encode_inner(out)
}
fn length(&self) -> usize {
self.fields_len()
}
}
impl Decodable for BlobTransactionSidecar {
/// Decodes the inner [BlobTransactionSidecar] fields from RLP bytes, without a RLP header.
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Self::decode_inner(buf)
}
}
// Wrapper for c-kzg rlp
#[repr(C)]
struct BlobTransactionSidecarRlp {
blobs: Vec<[u8; c_kzg::BYTES_PER_BLOB]>,
commitments: Vec<[u8; 48]>,
proofs: Vec<[u8; 48]>,
}
const _: [(); std::mem::size_of::<BlobTransactionSidecar>()] =
[(); std::mem::size_of::<BlobTransactionSidecarRlp>()];
const _: [(); std::mem::size_of::<BlobTransactionSidecar>()] =
[(); std::mem::size_of::<reth_rpc_types::BlobTransactionSidecar>()];
impl BlobTransactionSidecarRlp {
fn wrap_ref(other: &BlobTransactionSidecar) -> &Self {
// SAFETY: Same repr and size
unsafe { &*(other as *const BlobTransactionSidecar).cast::<Self>() }
}
fn unwrap(self) -> BlobTransactionSidecar {
// SAFETY: Same repr and size
unsafe { std::mem::transmute(self) }
}
fn encode(&self, out: &mut dyn bytes::BufMut) {
// Encode the blobs, commitments, and proofs
self.blobs.encode(out);
self.commitments.encode(out);
self.proofs.encode(out);
}
fn fields_len(&self) -> usize {
self.blobs.length() + self.commitments.length() + self.proofs.length()
}
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Ok(Self {
blobs: Decodable::decode(buf)?,
commitments: Decodable::decode(buf)?,
proofs: Decodable::decode(buf)?,
})
}
}
#[cfg(any(test, feature = "arbitrary"))]
impl<'a> arbitrary::Arbitrary<'a> for BlobTransactionSidecar {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let mut arr = [0u8; BYTES_PER_BLOB];
let blobs: Vec<Blob> = (0..u.int_in_range(1..=16)?)
.map(|_| {
arr = arbitrary::Arbitrary::arbitrary(u).unwrap();
// Ensure that each blob is cacnonical by ensuring each field element contained in
// the blob is < BLS_MODULUS
for i in 0..(FIELD_ELEMENTS_PER_BLOB as usize) {
arr[i * BYTES_PER_FIELD_ELEMENT] = 0;
}
Blob::from(arr)
})
.collect();
Ok(generate_blob_sidecar(blobs))
}
}
#[cfg(any(test, feature = "arbitrary"))]
impl proptest::arbitrary::Arbitrary for BlobTransactionSidecar {
type Parameters = ParamsFor<String>;
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
proptest_vec(proptest_vec(proptest_any::<u8>(), BYTES_PER_BLOB), 1..=5)
.prop_map(move |blobs| {
let blobs = blobs
.into_iter()
.map(|mut blob| {
let mut arr = [0u8; BYTES_PER_BLOB];
// Ensure that each blob is cacnonical by ensuring each field element
// contained in the blob is < BLS_MODULUS
for i in 0..(FIELD_ELEMENTS_PER_BLOB as usize) {
blob[i * BYTES_PER_FIELD_ELEMENT] = 0;
}
arr.copy_from_slice(blob.as_slice());
arr.into()
})
.collect();
generate_blob_sidecar(blobs)
})
.boxed()
}
type Strategy = BoxedStrategy<BlobTransactionSidecar>;
}
#[cfg(any(test, feature = "arbitrary"))]
fn generate_blob_sidecar(blobs: Vec<Blob>) -> BlobTransactionSidecar {
let kzg_settings = MAINNET_KZG_TRUSTED_SETUP.clone();
let commitments: Vec<Bytes48> = blobs
.iter()
.map(|blob| KzgCommitment::blob_to_kzg_commitment(&blob.clone(), &kzg_settings).unwrap())
.map(|commitment| commitment.to_bytes())
.collect();
let proofs: Vec<Bytes48> = blobs
.iter()
.zip(commitments.iter())
.map(|(blob, commitment)| {
KzgProof::compute_blob_kzg_proof(blob, commitment, &kzg_settings).unwrap()
})
.map(|proof| proof.to_bytes())
.collect();
BlobTransactionSidecar { blobs, commitments, proofs }
}