alloy-network 2.0.1

Ethereum blockchain RPC behavior abstraction
Documentation
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
mod builder;
mod either;

pub mod error;

use alloy_consensus::{
    Sealed, Signed, TxEip1559, TxEip2930, TxEip4844Variant, TxEip7702, TxEnvelope, TxLegacy,
};
use alloy_eips::{eip7702::SignedAuthorization, Typed2718};
use alloy_primitives::{Bytes, ChainId, TxKind, B256, U256};
pub use either::{AnyTxEnvelope, AnyTypedTransaction};
use std::error::Error;

mod unknowns;
pub use unknowns::{AnyTxType, UnknownTxEnvelope, UnknownTypedTransaction};

pub use alloy_consensus_any::{AnyHeader, AnyReceiptEnvelope};

use crate::{any::error::AnyConversionError, Network};
use alloy_consensus::{
    error::ValueError,
    transaction::{Either, Recovered},
};
use alloy_network_primitives::{BlockResponse, TransactionResponse};
pub use alloy_rpc_types_any::{AnyRpcHeader, AnyTransactionReceipt};
use alloy_rpc_types_eth::{AccessList, Block, BlockTransactions, Transaction, TransactionRequest};
use alloy_serde::WithOtherFields;
use derive_more::From;
use serde::{Deserialize, Serialize};
use std::ops::{Deref, DerefMut};

/// Types for a catch-all network.
///
/// `AnyNetwork`'s associated types allow for many different types of
/// transactions, using catch-all fields. This [`Network`] should be used
/// only when the application needs to support multiple networks via the same
/// codepaths without knowing the networks at compile time.
///
/// ## Rough Edges
///
/// Supporting arbitrary unknown types is hard, and users of this network
/// should be aware of the following:
///
/// - The implementation of [`Decodable2718`] for [`AnyTxEnvelope`] will not work for non-Ethereum
///   transaction types. It will successfully decode an Ethereum [`TxEnvelope`], but will decode
///   only the type for any unknown transaction type. It will also leave the buffer unconsumed,
///   which will cause further deserialization to produce erroneous results.
/// - The implementation of [`Encodable2718`] for [`AnyTxEnvelope`] will panic for non-Ethereum
///   transaction types. Unknown transaction types cannot be re-encoded through [`AnyNetwork`]; use
///   a custom transaction type and network implementation instead.
/// - The [`TransactionRequest`] will build ONLY Ethereum types. It will error when attempting to
///   build any unknown type.
/// - The [`Network::TransactionResponse`] may deserialize unknown metadata fields into the inner
///   [`AnyTxEnvelope`], rather than into the outer [`WithOtherFields`].
///
/// [`Decodable2718`]: alloy_eips::eip2718::Decodable2718
/// [`Encodable2718`]: alloy_eips::eip2718::Encodable2718
/// [`TxEnvelope`]: alloy_consensus::TxEnvelope
#[derive(Clone, Copy, Debug)]
pub struct AnyNetwork {
    _private: (),
}

impl Network for AnyNetwork {
    type TxType = AnyTxType;

    type TxEnvelope = AnyTxEnvelope;

    type UnsignedTx = AnyTypedTransaction;

    type ReceiptEnvelope = AnyReceiptEnvelope;

    type Header = AnyHeader;

    type TransactionRequest = WithOtherFields<TransactionRequest>;

    type TransactionResponse = AnyRpcTransaction;

    type ReceiptResponse = AnyTransactionReceipt;

    type HeaderResponse = AnyRpcHeader;

    type BlockResponse = AnyRpcBlock;
}

/// A wrapper for [`AnyRpcBlock`] that allows for handling unknown block types.
///
/// This type wraps:
///  - rpc transaction
///  - additional fields
#[derive(Clone, Debug, From, PartialEq, Eq, Deserialize, Serialize)]
pub struct AnyRpcBlock(pub WithOtherFields<Block<AnyRpcTransaction, AnyRpcHeader>>);

impl AnyRpcBlock {
    /// Create a new [`AnyRpcBlock`].
    pub const fn new(inner: WithOtherFields<Block<AnyRpcTransaction, AnyRpcHeader>>) -> Self {
        Self(inner)
    }

    /// Consumes the type and returns the wrapped rpc block.
    pub fn into_inner(self) -> Block<AnyRpcTransaction, AnyRpcHeader> {
        self.0.into_inner()
    }

    /// Attempts to convert the inner RPC [`Block`] into a consensus block.
    ///
    /// Returns an [`AnyConversionError`] if any of the conversions fail.
    pub fn try_into_consensus<T, H>(
        self,
    ) -> Result<alloy_consensus::Block<T, H>, AnyConversionError>
    where
        T: TryFrom<AnyRpcTransaction, Error: Error + Send + Sync + 'static>,
        H: TryFrom<AnyHeader, Error: Error + Send + Sync + 'static>,
    {
        self.into_inner()
            .map_header(|h| h.into_consensus())
            .try_convert_header()
            .map_err(AnyConversionError::new)?
            .try_convert_transactions()
            .map_err(AnyConversionError::new)
            .map(Block::into_consensus_block)
    }

    /// Attempts to convert the inner RPC [`Block`] into a sealed consensus block.
    ///
    /// Uses the block hash from the RPC header to seal the block.
    ///
    /// Returns an [`AnyConversionError`] if any of the conversions fail.
    pub fn try_into_sealed<T, H>(
        self,
    ) -> Result<Sealed<alloy_consensus::Block<T, H>>, AnyConversionError>
    where
        T: TryFrom<AnyRpcTransaction, Error: Error + Send + Sync + 'static>,
        H: TryFrom<AnyHeader, Error: Error + Send + Sync + 'static>,
    {
        let block_hash = self.header.hash;
        let block = self.try_into_consensus()?;
        Ok(Sealed::new_unchecked(block, block_hash))
    }

    /// Tries to convert inner transactions into a vector of [`AnyRpcTransaction`].
    ///
    /// Returns an error if the block contains only transaction hashes or if it is an uncle block.
    pub fn try_into_transactions(
        self,
    ) -> Result<Vec<AnyRpcTransaction>, ValueError<BlockTransactions<AnyRpcTransaction>>> {
        self.0.inner.try_into_transactions()
    }

    /// Consumes the type and returns an iterator over the transactions in this block
    pub fn into_transactions_iter(self) -> impl Iterator<Item = AnyRpcTransaction> {
        self.into_inner().transactions.into_transactions()
    }
}

impl BlockResponse for AnyRpcBlock {
    type Header = AnyRpcHeader;
    type Transaction = AnyRpcTransaction;

    fn header(&self) -> &Self::Header {
        &self.0.inner.header
    }

    fn transactions(&self) -> &BlockTransactions<Self::Transaction> {
        &self.0.inner.transactions
    }

    fn transactions_mut(&mut self) -> &mut BlockTransactions<Self::Transaction> {
        &mut self.0.inner.transactions
    }

    fn other_fields(&self) -> Option<&alloy_serde::OtherFields> {
        self.0.other_fields()
    }
}

impl AsRef<WithOtherFields<Block<AnyRpcTransaction, AnyRpcHeader>>> for AnyRpcBlock {
    fn as_ref(&self) -> &WithOtherFields<Block<AnyRpcTransaction, AnyRpcHeader>> {
        &self.0
    }
}

impl Deref for AnyRpcBlock {
    type Target = WithOtherFields<Block<AnyRpcTransaction, AnyRpcHeader>>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for AnyRpcBlock {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl From<Block> for AnyRpcBlock {
    fn from(value: Block) -> Self {
        let block = value.map_header(|h| h.map(|h| h.into())).map_transactions(|tx| {
            AnyRpcTransaction::new(WithOtherFields::new(tx.map(AnyTxEnvelope::Ethereum)))
        });

        Self(WithOtherFields::new(block))
    }
}

impl From<AnyRpcBlock> for Block<AnyRpcTransaction, AnyRpcHeader> {
    fn from(value: AnyRpcBlock) -> Self {
        value.into_inner()
    }
}
impl From<AnyRpcBlock> for WithOtherFields<Block<AnyRpcTransaction, AnyRpcHeader>> {
    fn from(value: AnyRpcBlock) -> Self {
        value.0
    }
}

impl<T, H> TryFrom<AnyRpcBlock> for alloy_consensus::Block<T, H>
where
    T: TryFrom<AnyRpcTransaction, Error: Error + Send + Sync + 'static>,
    H: TryFrom<AnyHeader, Error: Error + Send + Sync + 'static>,
{
    type Error = AnyConversionError;

    fn try_from(value: AnyRpcBlock) -> Result<Self, Self::Error> {
        value.try_into_consensus()
    }
}

/// A wrapper for [`AnyRpcTransaction`] that allows for handling unknown transaction types.
#[derive(Clone, Debug, From, PartialEq, Eq, Deserialize, Serialize)]
pub struct AnyRpcTransaction(pub WithOtherFields<Transaction<AnyTxEnvelope>>);

impl AnyRpcTransaction {
    /// Create a new [`AnyRpcTransaction`].
    pub const fn new(inner: WithOtherFields<Transaction<AnyTxEnvelope>>) -> Self {
        Self(inner)
    }

    /// Split the transaction into its parts.
    pub fn into_parts(self) -> (Transaction<AnyTxEnvelope>, alloy_serde::OtherFields) {
        let WithOtherFields { inner, other } = self.0;
        (inner, other)
    }

    /// Consumes the outer layer for this transaction and returns the inner transaction.
    pub fn into_inner(self) -> Transaction<AnyTxEnvelope> {
        self.0.into_inner()
    }

    /// Returns the inner transaction [`TxEnvelope`] if inner tx type if
    /// [`AnyTxEnvelope::Ethereum`].
    pub fn as_envelope(&self) -> Option<&TxEnvelope> {
        self.inner.inner.as_envelope()
    }

    /// Returns the inner Ethereum transaction envelope, if it is an Ethereum transaction.
    /// If the transaction is not an Ethereum transaction, it is returned as an error.
    pub fn try_into_envelope(self) -> Result<TxEnvelope, ValueError<AnyTxEnvelope>> {
        self.0.inner.inner.into_inner().try_into_envelope()
    }

    /// Returns the [`TxLegacy`] variant if the transaction is a legacy transaction.
    pub fn as_legacy(&self) -> Option<&Signed<TxLegacy>> {
        self.0.inner().inner.as_legacy()
    }

    /// Returns the [`TxEip2930`] variant if the transaction is an EIP-2930 transaction.
    pub fn as_eip2930(&self) -> Option<&Signed<TxEip2930>> {
        self.0.inner().inner.as_eip2930()
    }

    /// Returns the [`TxEip1559`] variant if the transaction is an EIP-1559 transaction.
    pub fn as_eip1559(&self) -> Option<&Signed<TxEip1559>> {
        self.0.inner().inner.as_eip1559()
    }

    /// Returns the [`TxEip4844Variant`] variant if the transaction is an EIP-4844 transaction.
    pub fn as_eip4844(&self) -> Option<&Signed<TxEip4844Variant>> {
        self.0.inner().inner.as_eip4844()
    }

    /// Returns the [`TxEip7702`] variant if the transaction is an EIP-7702 transaction.
    pub fn as_eip7702(&self) -> Option<&Signed<TxEip7702>> {
        self.0.inner().inner.as_eip7702()
    }

    /// Returns true if the transaction is a legacy transaction.
    #[inline]
    pub fn is_legacy(&self) -> bool {
        self.0.inner().inner.is_legacy()
    }

    /// Returns true if the transaction is an EIP-2930 transaction.
    #[inline]
    pub fn is_eip2930(&self) -> bool {
        self.0.inner().inner.is_eip2930()
    }

    /// Returns true if the transaction is an EIP-1559 transaction.
    #[inline]
    pub fn is_eip1559(&self) -> bool {
        self.0.inner().inner.is_eip1559()
    }

    /// Returns true if the transaction is an EIP-4844 transaction.
    #[inline]
    pub fn is_eip4844(&self) -> bool {
        self.0.inner().inner.is_eip4844()
    }

    /// Returns true if the transaction is an EIP-7702 transaction.
    #[inline]
    pub fn is_eip7702(&self) -> bool {
        self.0.inner().inner.is_eip7702()
    }

    /// Attempts to convert the [`AnyRpcTransaction`] into `Either::Right` if this is an unknown
    /// variant.
    ///
    /// Returns `Either::Left` with the ethereum `TxEnvelope` if this is the
    /// [`AnyTxEnvelope::Ethereum`] variant and [`Either::Right`] with the converted variant.
    pub fn try_into_either<T>(self) -> Result<Either<TxEnvelope, T>, T::Error>
    where
        T: TryFrom<Self>,
    {
        if self.0.inner.inner.inner().is_ethereum() {
            Ok(Either::Left(self.0.inner.inner.into_inner().try_into_envelope().unwrap()))
        } else {
            T::try_from(self).map(Either::Right)
        }
    }

    /// Attempts to convert the [`UnknownTxEnvelope`] into `Either::Right` if this is an unknown
    /// variant.
    ///
    /// Returns `Either::Left` with the ethereum `TxEnvelope` if this is the
    /// [`AnyTxEnvelope::Ethereum`] variant and [`Either::Right`] with the converted variant.
    pub fn try_unknown_into_either<T>(self) -> Result<Either<TxEnvelope, T>, T::Error>
    where
        T: TryFrom<UnknownTxEnvelope>,
    {
        self.0.inner.inner.into_inner().try_into_either()
    }

    /// Applies the given closure to the inner transaction type.
    ///
    /// [`alloy_serde::OtherFields`] are stripped away while mapping.
    /// Applies the given closure to the inner transaction type.
    pub fn map<Tx>(self, f: impl FnOnce(AnyTxEnvelope) -> Tx) -> Transaction<Tx> {
        self.into_inner().map(f)
    }

    /// Applies the given fallible closure to the inner transactions.
    ///
    /// [`alloy_serde::OtherFields`] are stripped away while mapping.
    pub fn try_map<Tx, E>(
        self,
        f: impl FnOnce(AnyTxEnvelope) -> Result<Tx, E>,
    ) -> Result<Transaction<Tx>, E> {
        self.into_inner().try_map(f)
    }

    /// Converts the transaction type to the given alternative that is `From<T>`.
    ///
    /// [`alloy_serde::OtherFields`] are stripped away while mapping.
    pub fn convert<U>(self) -> Transaction<U>
    where
        U: From<AnyTxEnvelope>,
    {
        self.into_inner().map(U::from)
    }

    /// Converts the transaction to the given alternative that is `TryFrom<T>`
    ///
    /// Returns the transaction with the new transaction type if all conversions were successful.
    ///
    /// [`alloy_serde::OtherFields`] are stripped away while mapping.
    pub fn try_convert<U>(self) -> Result<Transaction<U>, U::Error>
    where
        U: TryFrom<AnyTxEnvelope>,
    {
        self.into_inner().try_map(U::try_from)
    }
}

impl AsRef<AnyTxEnvelope> for AnyRpcTransaction {
    fn as_ref(&self) -> &AnyTxEnvelope {
        &self.0.inner.inner
    }
}

impl Deref for AnyRpcTransaction {
    type Target = WithOtherFields<Transaction<AnyTxEnvelope>>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for AnyRpcTransaction {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl From<Transaction<TxEnvelope>> for AnyRpcTransaction {
    fn from(tx: Transaction<TxEnvelope>) -> Self {
        let tx = tx.map(AnyTxEnvelope::Ethereum);
        Self(WithOtherFields::new(tx))
    }
}

impl From<AnyRpcTransaction> for AnyTxEnvelope {
    fn from(tx: AnyRpcTransaction) -> Self {
        tx.0.inner.into_inner()
    }
}

impl From<AnyRpcTransaction> for Transaction<AnyTxEnvelope> {
    fn from(tx: AnyRpcTransaction) -> Self {
        tx.0.inner
    }
}

impl From<AnyRpcTransaction> for WithOtherFields<Transaction<AnyTxEnvelope>> {
    fn from(tx: AnyRpcTransaction) -> Self {
        tx.0
    }
}

impl From<AnyRpcTransaction> for Recovered<AnyTxEnvelope> {
    fn from(tx: AnyRpcTransaction) -> Self {
        tx.0.inner.inner
    }
}

impl From<AnyRpcTransaction> for WithOtherFields<TransactionRequest> {
    fn from(tx: AnyRpcTransaction) -> Self {
        let req: TransactionRequest = tx.0.inner.into_recovered().into();
        Self::new(req)
    }
}

impl TryFrom<AnyRpcTransaction> for TxEnvelope {
    type Error = ValueError<AnyTxEnvelope>;

    fn try_from(value: AnyRpcTransaction) -> Result<Self, Self::Error> {
        value.try_into_envelope()
    }
}

impl alloy_consensus::Transaction for AnyRpcTransaction {
    fn chain_id(&self) -> Option<ChainId> {
        self.inner.chain_id()
    }

    fn nonce(&self) -> u64 {
        self.inner.nonce()
    }

    fn gas_limit(&self) -> u64 {
        self.inner.gas_limit()
    }

    fn gas_price(&self) -> Option<u128> {
        alloy_consensus::Transaction::gas_price(&self.0.inner)
    }

    fn max_fee_per_gas(&self) -> u128 {
        alloy_consensus::Transaction::max_fee_per_gas(&self.inner)
    }

    fn max_priority_fee_per_gas(&self) -> Option<u128> {
        self.inner.max_priority_fee_per_gas()
    }

    fn max_fee_per_blob_gas(&self) -> Option<u128> {
        self.inner.max_fee_per_blob_gas()
    }

    fn priority_fee_or_price(&self) -> u128 {
        self.inner.priority_fee_or_price()
    }

    fn effective_gas_price(&self, base_fee: Option<u64>) -> u128 {
        self.inner.effective_gas_price(base_fee)
    }

    fn is_dynamic_fee(&self) -> bool {
        self.inner.is_dynamic_fee()
    }

    fn kind(&self) -> TxKind {
        self.inner.kind()
    }

    fn is_create(&self) -> bool {
        self.inner.is_create()
    }

    fn value(&self) -> U256 {
        self.inner.value()
    }

    fn input(&self) -> &Bytes {
        self.inner.input()
    }

    fn access_list(&self) -> Option<&AccessList> {
        self.inner.access_list()
    }

    fn blob_versioned_hashes(&self) -> Option<&[B256]> {
        self.inner.blob_versioned_hashes()
    }

    fn authorization_list(&self) -> Option<&[SignedAuthorization]> {
        self.inner.authorization_list()
    }
}

impl TransactionResponse for AnyRpcTransaction {
    fn tx_hash(&self) -> alloy_primitives::TxHash {
        self.inner.tx_hash()
    }

    fn block_hash(&self) -> Option<alloy_primitives::BlockHash> {
        self.0.inner.block_hash
    }

    fn block_number(&self) -> Option<u64> {
        self.inner.block_number
    }

    fn transaction_index(&self) -> Option<u64> {
        self.inner.transaction_index
    }

    fn from(&self) -> alloy_primitives::Address {
        self.inner.from()
    }

    fn gas_price(&self) -> Option<u128> {
        self.inner.effective_gas_price
    }
}

impl Typed2718 for AnyRpcTransaction {
    fn ty(&self) -> u8 {
        self.inner.ty()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloy_primitives::B64;

    #[test]
    fn convert_any_block() {
        let block = AnyRpcBlock::new(
            Block::new(
                AnyRpcHeader::from_sealed(
                    AnyHeader {
                        nonce: Some(B64::ZERO),
                        mix_hash: Some(B256::ZERO),
                        ..Default::default()
                    }
                    .seal(B256::ZERO),
                ),
                BlockTransactions::Full(vec![]),
            )
            .into(),
        );

        let _block: alloy_consensus::Block<TxEnvelope, alloy_consensus::Header> =
            block.try_into().unwrap();
    }
}