stratus/eth/primitives/
external_transaction.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
use alloy_consensus::transaction::Recovered;
use alloy_consensus::Signed;
use alloy_consensus::TxEnvelope;
use alloy_consensus::TxLegacy;
use alloy_primitives::Bytes;
use alloy_primitives::Signature;
use alloy_primitives::TxKind;
use anyhow::Context;
use anyhow::Result;
use ethereum_types::U256;
use fake::Dummy;
use fake::Fake;
use fake::Faker;

use crate::alias::AlloyTransaction;
use crate::eth::primitives::signature_component::SignatureComponent;
use crate::eth::primitives::Address;
use crate::eth::primitives::BlockNumber;
use crate::eth::primitives::Hash;
use crate::eth::primitives::Wei;

#[derive(Debug, Clone, PartialEq, derive_more::Deref, serde::Serialize)]
#[serde(transparent)]
pub struct ExternalTransaction(#[deref] pub AlloyTransaction);

impl<'de> serde::Deserialize<'de> for ExternalTransaction {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        use serde::de::Error;
        use serde_json::Value;

        let mut value = Value::deserialize(deserializer)?;

        if let Value::Object(ref mut map) = value {
            // If v is 0x0 or 0x1, this is a type 2 (EIP-1559) transaction
            if let Some(Value::String(v_value)) = map.get("v") {
                if (v_value == "0x0" || v_value == "0x1") && !map.contains_key("type") {
                    map.insert("type".to_string(), Value::String("0x2".to_string()));
                }
            }

            // Check if this is a type 2 transaction
            if let Some(Value::String(type_value)) = map.get("type") {
                if type_value == "0x2" {
                    // For EIP-1559 transactions, ensure max_fee_per_gas and max_priority_fee_per_gas are present
                    if !map.contains_key("maxFeePerGas") {
                        map.insert("maxFeePerGas".to_string(), Value::String("0x0".to_string()));
                    }
                    if !map.contains_key("maxPriorityFeePerGas") {
                        map.insert("maxPriorityFeePerGas".to_string(), Value::String("0x0".to_string()));
                    }
                    if !map.contains_key("accessList") {
                        map.insert("accessList".to_string(), Value::Array(Vec::new()));
                    }
                }
            }
            // Check if this is a type 1 transaction
            if let Some(Value::String(type_value)) = map.get("type") {
                if type_value == "0x1" {
                    // For EIP-2930 transactions, ensure accessList is present
                    if !map.contains_key("accessList") {
                        map.insert("accessList".to_string(), Value::Array(Vec::new()));
                    }
                }
            }
        }

        // Use the inner type's deserialization
        let transaction = AlloyTransaction::deserialize(value).map_err(D::Error::custom)?;

        Ok(ExternalTransaction(transaction))
    }
}

impl ExternalTransaction {
    /// Returns the block number where the transaction was mined.
    pub fn block_number(&self) -> Result<BlockNumber> {
        Ok(self.0.block_number.context("ExternalTransaction has no block_number")?.into())
    }

    /// Returns the transaction hash.
    pub fn hash(&self) -> Hash {
        Hash::from(*self.0.inner.tx_hash())
    }
}

impl Dummy<Faker> for ExternalTransaction {
    fn dummy_with_rng<R: rand_core::RngCore + ?Sized>(faker: &Faker, rng: &mut R) -> Self {
        let from: Address = faker.fake_with_rng(rng);
        let to: Address = faker.fake_with_rng(rng);

        let block_hash: Hash = faker.fake_with_rng(rng);

        let gas_price: Wei = Wei::from(rng.next_u64());
        let value: Wei = Wei::from(rng.next_u64());

        let tx = TxLegacy {
            chain_id: Some(1),
            nonce: rng.next_u64(),
            gas_price: gas_price.into(),
            gas_limit: rng.next_u64(),
            to: TxKind::Call(from.into()),
            value: value.into(),
            input: Bytes::default(),
        };

        let r = U256::from(rng.next_u64());
        let s = U256::from(rng.next_u64());
        let v = rng.next_u64() % 2 == 0;
        let signature = Signature::new(SignatureComponent(r).into(), SignatureComponent(s).into(), v);

        let hash: Hash = faker.fake_with_rng(rng);
        let inner_tx = TxEnvelope::Legacy(Signed::new_unchecked(tx, signature, hash.into()));

        let inner = alloy_rpc_types_eth::Transaction {
            inner: Recovered::new_unchecked(inner_tx, to.into()),
            block_hash: Some(block_hash.into()),
            block_number: Some(rng.next_u64()),
            transaction_index: Some(rng.next_u64()),
            effective_gas_price: Some(gas_price.as_u128()),
        };

        ExternalTransaction(inner)
    }
}

// -----------------------------------------------------------------------------
// Conversions: Other -> Self
// -----------------------------------------------------------------------------
impl From<AlloyTransaction> for ExternalTransaction {
    fn from(value: AlloyTransaction) -> Self {
        ExternalTransaction(value)
    }
}

// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;

    #[test]
    fn test_deserialize_type0_transaction() {
        let json = json!({
            "hash": "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
            "type": "0x0",
            "from": "0x1234567890123456789012345678901234567890",
            "to": "0x0987654321098765432109876543210987654321",
            "gas": "0x76c0",
            "gasPrice": "0x9184e72a000",
            "nonce": "0x1",
            "value": "0x9184e72a",
            "input": "0x",
            "chainId": "0x1",
            "r": "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
            "s": "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
            "v": "0x1b"
        });

        let tx: ExternalTransaction = serde_json::from_value(json).unwrap();

        assert!(matches!(tx.0.inner.inner(), TxEnvelope::Legacy(_)));
    }

    #[test]
    fn test_deserialize_type1_transaction_with_missing_access_list() {
        let json = json!({
            "hash": "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
            "type": "0x1",
            "from": "0x1234567890123456789012345678901234567890",
            "to": "0x0987654321098765432109876543210987654321",
            "gas": "0x76c0",
            "gasPrice": "0x9184e72a000",
            "nonce": "0x1",
            "value": "0x9184e72a",
            "input": "0x",
            "chainId": "0x1",
            "r": "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
            "s": "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
            "v": "0x0"
            // accessList is missing
        });

        let tx: ExternalTransaction = serde_json::from_value(json).unwrap();

        assert!(matches!(tx.0.inner.inner(), TxEnvelope::Eip2930(_)));
    }

    #[test]
    fn test_deserialize_type2_transaction_with_missing_fields() {
        let json = json!({
            "hash": "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
            "type": "0x2",
            "from": "0x1234567890123456789012345678901234567890",
            "to": "0x0987654321098765432109876543210987654321",
            "gas": "0x76c0",
            "nonce": "0x1",
            "value": "0x9184e72a",
            "input": "0x",
            "chainId": "0x1",
            "r": "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
            "s": "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
            "v": "0x1"
            // maxFeePerGas, maxPriorityFeePerGas, and accessList are missing
        });

        let tx: ExternalTransaction = serde_json::from_value(json).unwrap();

        assert!(matches!(tx.0.inner.inner(), TxEnvelope::Eip1559(_)));
    }

    #[test]
    fn test_deserialize_type2_inferred_from_v_value() {
        let json = json!({
            "hash": "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
            "from": "0x1234567890123456789012345678901234567890",
            "to": "0x0987654321098765432109876543210987654321",
            "gas": "0x76c0",
            "gasPrice": "0x9184e72a000",
            "nonce": "0x1",
            "value": "0x9184e72a",
            "input": "0x",
            "chainId": "0x1",
            "r": "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
            "s": "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
            "v": "0x0"
            // type field is missing, but v is 0x0 so it should be inferred as type 2
        });

        let tx: ExternalTransaction = serde_json::from_value(json).unwrap();

        assert!(matches!(tx.0.inner.inner(), TxEnvelope::Eip1559(_)));

        // Test with v = 0x1 as well
        let json = json!({
            "hash": "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
            "from": "0x1234567890123456789012345678901234567890",
            "to": "0x0987654321098765432109876543210987654321",
            "gas": "0x76c0",
            "gasPrice": "0x9184e72a000",
            "nonce": "0x1",
            "value": "0x9184e72a",
            "input": "0x",
            "chainId": "0x1",
            "r": "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
            "s": "0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
            "v": "0x1"
            // type field is missing, but v is 0x1 so it should be inferred as type 2
        });

        let tx: ExternalTransaction = serde_json::from_value(json).unwrap();

        assert!(matches!(tx.0.inner.inner(), TxEnvelope::Eip1559(_)));
    }
}