|
| 1 | +//! JSON patching utilities for Ethereum blocks and receipts. |
| 2 | +//! |
| 3 | +//! Some cached blocks are missing the transaction `type` field because |
| 4 | +//! graph-node's rust-web3 fork didn't capture it. Alloy requires this field for |
| 5 | +//! deserialization. These utilities patch the JSON to add `type: "0x0"` (legacy |
| 6 | +//! transaction) where missing. |
| 7 | +//! |
| 8 | +//! Also used by `PatchingHttp` for chains that don't support EIP-2718 typed transactions. |
| 9 | +
|
| 10 | +use graph::prelude::serde_json::Value; |
| 11 | + |
| 12 | +pub(crate) fn patch_type_field(obj: &mut Value) -> bool { |
| 13 | + if let Value::Object(map) = obj { |
| 14 | + if !map.contains_key("type") { |
| 15 | + map.insert("type".to_string(), Value::String("0x0".to_string())); |
| 16 | + return true; |
| 17 | + } |
| 18 | + } |
| 19 | + false |
| 20 | +} |
| 21 | + |
| 22 | +pub(crate) fn patch_block_transactions(block: &mut Value) -> bool { |
| 23 | + let Some(txs) = block.get_mut("transactions").and_then(|t| t.as_array_mut()) else { |
| 24 | + return false; |
| 25 | + }; |
| 26 | + let mut patched = false; |
| 27 | + for tx in txs { |
| 28 | + patched |= patch_type_field(tx); |
| 29 | + } |
| 30 | + patched |
| 31 | +} |
| 32 | + |
| 33 | +pub(crate) fn patch_receipts(result: &mut Value) -> bool { |
| 34 | + match result { |
| 35 | + Value::Object(_) => patch_type_field(result), |
| 36 | + Value::Array(arr) => { |
| 37 | + let mut patched = false; |
| 38 | + for r in arr { |
| 39 | + patched |= patch_type_field(r); |
| 40 | + } |
| 41 | + patched |
| 42 | + } |
| 43 | + _ => false, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +#[cfg(test)] |
| 48 | +mod tests { |
| 49 | + use super::*; |
| 50 | + use graph::prelude::serde_json::json; |
| 51 | + |
| 52 | + #[test] |
| 53 | + fn patch_type_field_adds_missing_type() { |
| 54 | + let mut obj = json!({"status": "0x1", "gasUsed": "0x5208"}); |
| 55 | + assert!(patch_type_field(&mut obj)); |
| 56 | + assert_eq!(obj["type"], "0x0"); |
| 57 | + } |
| 58 | + |
| 59 | + #[test] |
| 60 | + fn patch_type_field_preserves_existing_type() { |
| 61 | + let mut obj = json!({"status": "0x1", "type": "0x2"}); |
| 62 | + assert!(!patch_type_field(&mut obj)); |
| 63 | + assert_eq!(obj["type"], "0x2"); |
| 64 | + } |
| 65 | + |
| 66 | + #[test] |
| 67 | + fn patch_type_field_handles_non_object() { |
| 68 | + let mut val = json!("not an object"); |
| 69 | + assert!(!patch_type_field(&mut val)); |
| 70 | + } |
| 71 | + |
| 72 | + #[test] |
| 73 | + fn patch_block_transactions_patches_all() { |
| 74 | + let mut block = json!({ |
| 75 | + "hash": "0x123", |
| 76 | + "transactions": [ |
| 77 | + {"hash": "0xabc", "nonce": "0x1"}, |
| 78 | + {"hash": "0xdef", "nonce": "0x2", "type": "0x2"}, |
| 79 | + {"hash": "0xghi", "nonce": "0x3"} |
| 80 | + ] |
| 81 | + }); |
| 82 | + assert!(patch_block_transactions(&mut block)); |
| 83 | + assert_eq!(block["transactions"][0]["type"], "0x0"); |
| 84 | + assert_eq!(block["transactions"][1]["type"], "0x2"); |
| 85 | + assert_eq!(block["transactions"][2]["type"], "0x0"); |
| 86 | + } |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn patch_block_transactions_handles_empty() { |
| 90 | + let mut block = json!({"hash": "0x123", "transactions": []}); |
| 91 | + assert!(!patch_block_transactions(&mut block)); |
| 92 | + } |
| 93 | + |
| 94 | + #[test] |
| 95 | + fn patch_block_transactions_handles_missing_field() { |
| 96 | + let mut block = json!({"hash": "0x123"}); |
| 97 | + assert!(!patch_block_transactions(&mut block)); |
| 98 | + } |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn patch_receipts_single() { |
| 102 | + let mut receipt = json!({"status": "0x1"}); |
| 103 | + assert!(patch_receipts(&mut receipt)); |
| 104 | + assert_eq!(receipt["type"], "0x0"); |
| 105 | + } |
| 106 | + |
| 107 | + #[test] |
| 108 | + fn patch_receipts_array() { |
| 109 | + let mut receipts = json!([ |
| 110 | + {"status": "0x1"}, |
| 111 | + {"status": "0x1", "type": "0x2"} |
| 112 | + ]); |
| 113 | + assert!(patch_receipts(&mut receipts)); |
| 114 | + assert_eq!(receipts[0]["type"], "0x0"); |
| 115 | + assert_eq!(receipts[1]["type"], "0x2"); |
| 116 | + } |
| 117 | + |
| 118 | + #[test] |
| 119 | + fn patch_receipts_handles_null() { |
| 120 | + let mut val = Value::Null; |
| 121 | + assert!(!patch_receipts(&mut val)); |
| 122 | + } |
| 123 | +} |
0 commit comments