|
| 1 | +//! Cross-language ABI parity tests for app-level payloads dispatched |
| 2 | +//! between Rust pallets and Solidity contracts: |
| 3 | +//! |
| 4 | +//! - HFT `Message` (`pallets/hyper-fungible-token` ↔ `apps/HyperFungibleToken.sol`) |
| 5 | +//! - `BandwidthPurchaseMsg` (`apps/BandwidthManager.sol` → `pallets/bandwidth`) |
| 6 | +//! - `Tier[]` (`pallets/bandwidth` → `apps/BandwidthManager.sol` SetTiers) |
| 7 | +//! - `Withdrawal` (`pallets/bandwidth` → `apps/BandwidthManager.sol` Withdraw) |
| 8 | +//! |
| 9 | +//! Each test mirrors the production encode/decode methods on both |
| 10 | +//! sides — the Rust assertions are intentionally what the pallets do |
| 11 | +//! today, so any divergence from Solidity's `abi.encode(struct)` |
| 12 | +//! surfaces here. |
| 13 | +
|
| 14 | +use super::utils::*; |
| 15 | +use alloy_primitives::{Address, Bytes, U256}; |
| 16 | +use alloy_sol_types::{SolCall, SolValue}; |
| 17 | + |
| 18 | +alloy_sol_macro::sol! { |
| 19 | + // ── Mirror of production sol! types ───────────────────────────── |
| 20 | + |
| 21 | + /// Matches `Message` in `sdk/packages/core/contracts/apps/HyperFungibleToken.sol:82-91` |
| 22 | + /// and the alloy declaration in `pallets/hyper-fungible-token/src/types.rs`. |
| 23 | + struct HftMessage { |
| 24 | + bytes from; |
| 25 | + bytes to; |
| 26 | + uint256 amount; |
| 27 | + bytes data; |
| 28 | + } |
| 29 | + |
| 30 | + /// Matches `BandwidthPurchaseMsg` in `evm/src/apps/BandwidthManager.sol:32-41` |
| 31 | + /// and `BandwidthPurchaseMsgAbi` in `pallets/bandwidth/src/abi.rs:18-23`. |
| 32 | + struct BandwidthPurchaseMsg { |
| 33 | + bytes app; |
| 34 | + uint256 tier; |
| 35 | + uint256 months; |
| 36 | + bytes chain; |
| 37 | + } |
| 38 | + |
| 39 | + /// Matches `Tier` in `evm/src/apps/BandwidthManager.sol:44-49` |
| 40 | + /// and `TierAbi` in `pallets/bandwidth/src/abi.rs:27-30`. |
| 41 | + struct Tier { |
| 42 | + uint256 tier; |
| 43 | + uint256 price; |
| 44 | + } |
| 45 | + |
| 46 | + /// Matches `Withdrawal` in `evm/src/apps/BandwidthManager.sol:54-58` |
| 47 | + /// and `WithdrawalAbi` in `pallets/bandwidth/src/abi.rs:34-38`. |
| 48 | + struct Withdrawal { |
| 49 | + address token; |
| 50 | + address beneficiary; |
| 51 | + uint256 amount; |
| 52 | + } |
| 53 | + |
| 54 | + // ── AbiAppsCodec function selectors ───────────────────────────── |
| 55 | + |
| 56 | + function encodeHftMessage(HftMessage m) external pure returns (bytes); |
| 57 | + function decodeHftMessage(bytes data) external pure returns (HftMessage); |
| 58 | + |
| 59 | + function encodeBandwidthPurchase(BandwidthPurchaseMsg m) external pure returns (bytes); |
| 60 | + function decodeBandwidthPurchase(bytes data) external pure returns (BandwidthPurchaseMsg); |
| 61 | + |
| 62 | + function encodeTiers(Tier[] tiers) external pure returns (bytes); |
| 63 | + function decodeTiers(bytes data) external pure returns (Tier[]); |
| 64 | + |
| 65 | + function encodeWithdrawal(Withdrawal w) external pure returns (bytes); |
| 66 | + function decodeWithdrawal(bytes data) external pure returns (Withdrawal); |
| 67 | +} |
| 68 | + |
| 69 | +fn deploy_codec(env: &mut TestEnv) -> Address { |
| 70 | + let out_dir = env.evm_out_dir_public(); |
| 71 | + env.deploy_named(&out_dir, "AbiAppsCodec") |
| 72 | +} |
| 73 | + |
| 74 | +fn sample_hft_message() -> HftMessage { |
| 75 | + HftMessage { |
| 76 | + from: Bytes::from(hex::decode("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef").unwrap()), |
| 77 | + to: Bytes::from(hex::decode("cafebabecafebabecafebabecafebabecafebabe").unwrap()), |
| 78 | + amount: U256::from(1_000_000_000_000_000_000u128), |
| 79 | + data: Bytes::from(hex::decode("1234").unwrap()), |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +fn sample_bandwidth_purchase() -> BandwidthPurchaseMsg { |
| 84 | + BandwidthPurchaseMsg { |
| 85 | + app: Bytes::from(b"my-app".to_vec()), |
| 86 | + tier: U256::from(2u64), |
| 87 | + months: U256::from(3u64), |
| 88 | + chain: Bytes::from(b"EVM-8453".to_vec()), |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +fn sample_tiers() -> Vec<Tier> { |
| 93 | + vec![ |
| 94 | + Tier { tier: U256::from(0u64), price: U256::from(10_000_000_000_000_000u128) }, |
| 95 | + Tier { tier: U256::from(1u64), price: U256::from(50_000_000_000_000_000u128) }, |
| 96 | + Tier { tier: U256::from(2u64), price: U256::from(250_000_000_000_000_000u128) }, |
| 97 | + ] |
| 98 | +} |
| 99 | + |
| 100 | +fn sample_withdrawal() -> Withdrawal { |
| 101 | + Withdrawal { |
| 102 | + token: Address::repeat_byte(0xab), |
| 103 | + beneficiary: Address::repeat_byte(0xcd), |
| 104 | + amount: U256::from(123_456_789u64), |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/// Encoding parity for the HFT `Message`. Mirrors the production path |
| 109 | +/// in `pallets/hyper-fungible-token/src/lib.rs:296` and `module.rs:59,215`, |
| 110 | +/// which use `abi_encode` / `abi_decode` to match Solidity's |
| 111 | +/// `abi.encode(struct)` / `abi.decode(data, (Message))`. |
| 112 | +#[test] |
| 113 | +fn test_hft_message_encoding_parity() { |
| 114 | + let mut env = TestEnv::new(); |
| 115 | + let codec = deploy_codec(&mut env); |
| 116 | + let msg = sample_hft_message(); |
| 117 | + |
| 118 | + // Rust encodes — exactly as `pallets/hyper-fungible-token/src/lib.rs:296` does. |
| 119 | + let rust_encoded = HftMessage::abi_encode(&msg); |
| 120 | + |
| 121 | + // Solidity encodes — exactly as `HyperFungibleToken.sol:242` does. |
| 122 | + let result = env.call(codec, encodeHftMessageCall { m: msg.clone() }.abi_encode()); |
| 123 | + let sol_encoded = Bytes::abi_decode(&result).unwrap().to_vec(); |
| 124 | + |
| 125 | + assert_eq!(rust_encoded, sol_encoded, "HFT Message encoding mismatch"); |
| 126 | + |
| 127 | + // Solidity must be able to decode Rust-produced bytes. |
| 128 | + let _ = env.call( |
| 129 | + codec, |
| 130 | + decodeHftMessageCall { data: Bytes::from(rust_encoded.clone()) }.abi_encode(), |
| 131 | + ); |
| 132 | + |
| 133 | + // Rust must be able to decode Solidity-produced bytes — mirrors |
| 134 | + // `pallets/hyper-fungible-token/src/module.rs:59,215`. |
| 135 | + let decoded = HftMessage::abi_decode(&sol_encoded) |
| 136 | + .expect("Rust `abi_decode` failed on Solidity-encoded HFT Message"); |
| 137 | + assert_eq!(decoded.from, msg.from); |
| 138 | + assert_eq!(decoded.to, msg.to); |
| 139 | + assert_eq!(decoded.amount, msg.amount); |
| 140 | + assert_eq!(decoded.data, msg.data); |
| 141 | +} |
| 142 | + |
| 143 | +/// Encoding parity for `BandwidthPurchaseMsg`. Solidity encodes via |
| 144 | +/// `abi.encode(body)` (`BandwidthManager.sol:177`); Rust decodes via |
| 145 | +/// `abi_decode` (`pallets/bandwidth/src/abi.rs:64`). Mirror struct |
| 146 | +/// encoded with `abi_encode` to round-trip correctly. |
| 147 | +#[test] |
| 148 | +fn test_bandwidth_purchase_encoding_parity() { |
| 149 | + let mut env = TestEnv::new(); |
| 150 | + let codec = deploy_codec(&mut env); |
| 151 | + let msg = sample_bandwidth_purchase(); |
| 152 | + |
| 153 | + // Rust encodes — exactly as `pallets/bandwidth/src/abi.rs:90` does. |
| 154 | + let rust_encoded = BandwidthPurchaseMsg::abi_encode(&msg); |
| 155 | + |
| 156 | + // Solidity encodes — exactly as `BandwidthManager.sol:177` does. |
| 157 | + let result = |
| 158 | + env.call(codec, encodeBandwidthPurchaseCall { m: msg.clone() }.abi_encode()); |
| 159 | + let sol_encoded = Bytes::abi_decode(&result).unwrap().to_vec(); |
| 160 | + |
| 161 | + assert_eq!(rust_encoded, sol_encoded, "BandwidthPurchaseMsg encoding mismatch"); |
| 162 | + |
| 163 | + // Rust must be able to decode Solidity-produced bytes — mirrors |
| 164 | + // `pallets/bandwidth/src/abi.rs:64`. |
| 165 | + let decoded = BandwidthPurchaseMsg::abi_decode(&sol_encoded) |
| 166 | + .expect("Rust `abi_decode` failed on Solidity-encoded BandwidthPurchaseMsg"); |
| 167 | + assert_eq!(decoded.app, msg.app); |
| 168 | + assert_eq!(decoded.tier, msg.tier); |
| 169 | + assert_eq!(decoded.months, msg.months); |
| 170 | + assert_eq!(decoded.chain, msg.chain); |
| 171 | +} |
| 172 | + |
| 173 | +/// Encoding parity for the `Tier[]` governance payload. Rust uses |
| 174 | +/// `rows.abi_encode_params()` (`pallets/bandwidth/src/lib.rs:317`); |
| 175 | +/// Solidity uses `abi.decode(body[1:], (Tier[]))`. For a dynamic |
| 176 | +/// array (non-tuple SolType), `abi_encode_params` wraps in a 1-tuple |
| 177 | +/// and matches `abi.encode(arr)`, so this should pass. |
| 178 | +#[test] |
| 179 | +fn test_tiers_encoding_parity() { |
| 180 | + let mut env = TestEnv::new(); |
| 181 | + let codec = deploy_codec(&mut env); |
| 182 | + let tiers = sample_tiers(); |
| 183 | + |
| 184 | + // Rust encodes — exactly as `pallets/bandwidth/src/lib.rs:317` does. |
| 185 | + let rust_encoded = tiers.abi_encode_params(); |
| 186 | + |
| 187 | + // Solidity encodes — exactly as `abi.decode(body[1:], (Tier[]))` |
| 188 | + // expects on the receive side. |
| 189 | + let result = env.call(codec, encodeTiersCall { tiers: tiers.clone() }.abi_encode()); |
| 190 | + let sol_encoded = Bytes::abi_decode(&result).unwrap().to_vec(); |
| 191 | + |
| 192 | + assert_eq!(rust_encoded, sol_encoded, "Tier[] encoding mismatch"); |
| 193 | + |
| 194 | + // Solidity must be able to decode Rust-produced bytes. |
| 195 | + let result = env.call( |
| 196 | + codec, |
| 197 | + decodeTiersCall { data: Bytes::from(rust_encoded.clone()) }.abi_encode(), |
| 198 | + ); |
| 199 | + let decoded = <Vec<Tier> as SolValue>::abi_decode(&result).unwrap(); |
| 200 | + assert_eq!(decoded.len(), tiers.len()); |
| 201 | + for (a, b) in decoded.iter().zip(tiers.iter()) { |
| 202 | + assert_eq!(a.tier, b.tier); |
| 203 | + assert_eq!(a.price, b.price); |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +/// Encoding parity for the `Withdrawal` governance payload. Rust uses |
| 208 | +/// `payload.abi_encode_params()` (`pallets/bandwidth/src/lib.rs:346`); |
| 209 | +/// Solidity uses `abi.decode(body[1:], (Withdrawal))`. `Withdrawal` |
| 210 | +/// is all-static (`address`, `address`, `uint256`), so tuple-wrap vs |
| 211 | +/// fields-spread produce identical bytes — this should pass. |
| 212 | +#[test] |
| 213 | +fn test_withdrawal_encoding_parity() { |
| 214 | + let mut env = TestEnv::new(); |
| 215 | + let codec = deploy_codec(&mut env); |
| 216 | + let w = sample_withdrawal(); |
| 217 | + |
| 218 | + // Rust encodes — exactly as `pallets/bandwidth/src/lib.rs:346` does. |
| 219 | + let rust_encoded = Withdrawal::abi_encode_params(&w); |
| 220 | + |
| 221 | + // Solidity encodes — exactly as `abi.decode(body[1:], (Withdrawal))` |
| 222 | + // expects on the receive side. |
| 223 | + let result = env.call(codec, encodeWithdrawalCall { w: w.clone() }.abi_encode()); |
| 224 | + let sol_encoded = Bytes::abi_decode(&result).unwrap().to_vec(); |
| 225 | + |
| 226 | + assert_eq!(rust_encoded, sol_encoded, "Withdrawal encoding mismatch"); |
| 227 | + |
| 228 | + // Solidity must be able to decode Rust-produced bytes. |
| 229 | + let result = env.call( |
| 230 | + codec, |
| 231 | + decodeWithdrawalCall { data: Bytes::from(rust_encoded) }.abi_encode(), |
| 232 | + ); |
| 233 | + let decoded = Withdrawal::abi_decode(&result).unwrap(); |
| 234 | + assert_eq!(decoded.token, w.token); |
| 235 | + assert_eq!(decoded.beneficiary, w.beneficiary); |
| 236 | + assert_eq!(decoded.amount, w.amount); |
| 237 | +} |
0 commit comments