|
| 1 | +//! Cross-check: assert that the pure-Rust encoding agrees with the committed |
| 2 | +//! test vectors in scripts/key_vectors.json (issue #1168). |
| 3 | +//! |
| 4 | +//! This test does NOT use soroban_sdk — it reimplements the same |
| 5 | +//! length-prefix + SHA-256 scheme in native std Rust so it can be run with |
| 6 | +//! `cargo test -p gmx-keys` without a Soroban environment. Any divergence |
| 7 | +//! between this implementation and libs/keys/src/lib.rs is itself a bug and |
| 8 | +//! should be fixed in lib.rs first, then regenerate the JSON with: |
| 9 | +//! |
| 10 | +//! python3 scripts/compute_key.py <key_type> [args] > /dev/null # sanity |
| 11 | +//! # re-run the generation script and commit the updated JSON |
| 12 | +
|
| 13 | +use sha2::{Digest, Sha256}; |
| 14 | +use std::path::PathBuf; |
| 15 | + |
| 16 | +// ── encoding helpers (mirror libs/keys/src/lib.rs) ────────────────────────── |
| 17 | + |
| 18 | +fn push_str(buf: &mut Vec<u8>, s: &str) { |
| 19 | + let b = s.as_bytes(); |
| 20 | + let len = b.len() as u16; |
| 21 | + buf.extend_from_slice(&len.to_be_bytes()); |
| 22 | + buf.extend_from_slice(b); |
| 23 | +} |
| 24 | + |
| 25 | +fn push_addr(buf: &mut Vec<u8>, addr: &str) { |
| 26 | + let b = addr.as_bytes(); |
| 27 | + let len = b.len() as u16; |
| 28 | + buf.extend_from_slice(&len.to_be_bytes()); |
| 29 | + buf.extend_from_slice(b); |
| 30 | +} |
| 31 | + |
| 32 | +fn push_bool(buf: &mut Vec<u8>, v: bool) { |
| 33 | + buf.push(if v { 1 } else { 0 }); |
| 34 | +} |
| 35 | + |
| 36 | +fn push_raw32(buf: &mut Vec<u8>, hex: &str) { |
| 37 | + let raw = hex::decode(hex).expect("PNL_HEX must be valid hex"); |
| 38 | + assert_eq!(raw.len(), 32, "pnl_type must be 32 bytes"); |
| 39 | + buf.extend_from_slice(&raw); |
| 40 | +} |
| 41 | + |
| 42 | +fn sha256_hex(buf: &[u8]) -> String { |
| 43 | + let digest = Sha256::digest(buf); |
| 44 | + hex::encode(digest) |
| 45 | +} |
| 46 | + |
| 47 | +// ── key constructors ───────────────────────────────────────────────────────── |
| 48 | + |
| 49 | +fn role_key(role: &str) -> String { |
| 50 | + let mut b = Vec::new(); |
| 51 | + push_str(&mut b, role); |
| 52 | + sha256_hex(&b) |
| 53 | +} |
| 54 | + |
| 55 | +fn market_type_default() -> String { |
| 56 | + sha256_hex(b"DEFAULT") |
| 57 | +} |
| 58 | + |
| 59 | +fn max_pnl_factor_for_traders() -> String { |
| 60 | + let mut b = Vec::new(); |
| 61 | + push_str(&mut b, "MAX_PNL_FACTOR_FOR_TRADERS"); |
| 62 | + sha256_hex(&b) |
| 63 | +} |
| 64 | + |
| 65 | +fn max_pnl_factor_for_deposits() -> String { |
| 66 | + let mut b = Vec::new(); |
| 67 | + push_str(&mut b, "MAX_PNL_FACTOR_FOR_DEPOSITS"); |
| 68 | + sha256_hex(&b) |
| 69 | +} |
| 70 | + |
| 71 | +fn max_pnl_factor_for_withdrawals() -> String { |
| 72 | + let mut b = Vec::new(); |
| 73 | + push_str(&mut b, "MAX_PNL_FACTOR_FOR_WITHDRAWALS"); |
| 74 | + sha256_hex(&b) |
| 75 | +} |
| 76 | + |
| 77 | +fn max_pool_amount_key(market: &str, token: &str) -> String { |
| 78 | + let mut b = Vec::new(); |
| 79 | + push_str(&mut b, "MAX_POOL_AMOUNT"); |
| 80 | + push_addr(&mut b, market); |
| 81 | + push_addr(&mut b, token); |
| 82 | + sha256_hex(&b) |
| 83 | +} |
| 84 | + |
| 85 | +fn min_collateral_factor_key(market: &str) -> String { |
| 86 | + let mut b = Vec::new(); |
| 87 | + push_str(&mut b, "MIN_COLLATERAL_FACTOR"); |
| 88 | + push_addr(&mut b, market); |
| 89 | + sha256_hex(&b) |
| 90 | +} |
| 91 | + |
| 92 | +fn max_leverage_key(market: &str) -> String { |
| 93 | + let mut b = Vec::new(); |
| 94 | + push_str(&mut b, "MAX_LEVERAGE"); |
| 95 | + push_addr(&mut b, market); |
| 96 | + sha256_hex(&b) |
| 97 | +} |
| 98 | + |
| 99 | +fn max_open_interest_key(market: &str, is_long: bool) -> String { |
| 100 | + let mut b = Vec::new(); |
| 101 | + push_str(&mut b, "MAX_OPEN_INTEREST"); |
| 102 | + push_addr(&mut b, market); |
| 103 | + push_bool(&mut b, is_long); |
| 104 | + sha256_hex(&b) |
| 105 | +} |
| 106 | + |
| 107 | +fn borrowing_factor_key(market: &str, is_long: bool) -> String { |
| 108 | + let mut b = Vec::new(); |
| 109 | + push_str(&mut b, "BORROWING_FACTOR"); |
| 110 | + push_addr(&mut b, market); |
| 111 | + push_bool(&mut b, is_long); |
| 112 | + sha256_hex(&b) |
| 113 | +} |
| 114 | + |
| 115 | +fn borrowing_exponent_factor_key(market: &str, is_long: bool) -> String { |
| 116 | + let mut b = Vec::new(); |
| 117 | + push_str(&mut b, "BORROWING_EXPONENT_FACTOR"); |
| 118 | + push_addr(&mut b, market); |
| 119 | + push_bool(&mut b, is_long); |
| 120 | + sha256_hex(&b) |
| 121 | +} |
| 122 | + |
| 123 | +fn funding_factor_key(market: &str) -> String { |
| 124 | + let mut b = Vec::new(); |
| 125 | + push_str(&mut b, "FUNDING_FACTOR"); |
| 126 | + push_addr(&mut b, market); |
| 127 | + sha256_hex(&b) |
| 128 | +} |
| 129 | + |
| 130 | +fn funding_exponent_factor_key(market: &str) -> String { |
| 131 | + let mut b = Vec::new(); |
| 132 | + push_str(&mut b, "FUNDING_EXPONENT_FACTOR"); |
| 133 | + push_addr(&mut b, market); |
| 134 | + sha256_hex(&b) |
| 135 | +} |
| 136 | + |
| 137 | +fn funding_increase_factor_per_second_key(market: &str) -> String { |
| 138 | + let mut b = Vec::new(); |
| 139 | + push_str(&mut b, "FUNDING_INCREASE_FACTOR_PER_SECOND"); |
| 140 | + push_addr(&mut b, market); |
| 141 | + sha256_hex(&b) |
| 142 | +} |
| 143 | + |
| 144 | +fn funding_decrease_factor_per_second_key(market: &str) -> String { |
| 145 | + let mut b = Vec::new(); |
| 146 | + push_str(&mut b, "FUNDING_DECREASE_FACTOR_PER_SECOND"); |
| 147 | + push_addr(&mut b, market); |
| 148 | + sha256_hex(&b) |
| 149 | +} |
| 150 | + |
| 151 | +fn min_funding_factor_per_second_key(market: &str) -> String { |
| 152 | + let mut b = Vec::new(); |
| 153 | + push_str(&mut b, "MIN_FUNDING_FACTOR_PER_SECOND"); |
| 154 | + push_addr(&mut b, market); |
| 155 | + sha256_hex(&b) |
| 156 | +} |
| 157 | + |
| 158 | +fn max_funding_factor_per_second_key(market: &str) -> String { |
| 159 | + let mut b = Vec::new(); |
| 160 | + push_str(&mut b, "MAX_FUNDING_FACTOR_PER_SECOND"); |
| 161 | + push_addr(&mut b, market); |
| 162 | + sha256_hex(&b) |
| 163 | +} |
| 164 | + |
| 165 | +fn swap_impact_exponent_factor_key(market: &str) -> String { |
| 166 | + let mut b = Vec::new(); |
| 167 | + push_str(&mut b, "SWAP_IMPACT_EXPONENT_FACTOR"); |
| 168 | + push_addr(&mut b, market); |
| 169 | + sha256_hex(&b) |
| 170 | +} |
| 171 | + |
| 172 | +fn position_impact_exponent_factor_key(market: &str) -> String { |
| 173 | + let mut b = Vec::new(); |
| 174 | + push_str(&mut b, "POSITION_IMPACT_EXPONENT_FACTOR"); |
| 175 | + push_addr(&mut b, market); |
| 176 | + sha256_hex(&b) |
| 177 | +} |
| 178 | + |
| 179 | +fn min_market_tokens_for_first_deposit_key(market: &str) -> String { |
| 180 | + let mut b = Vec::new(); |
| 181 | + push_str(&mut b, "MIN_MARKET_TOKENS_FOR_FIRST_DEPOSIT"); |
| 182 | + push_addr(&mut b, market); |
| 183 | + sha256_hex(&b) |
| 184 | +} |
| 185 | + |
| 186 | +fn stable_price_key(token: &str) -> String { |
| 187 | + let mut b = Vec::new(); |
| 188 | + push_str(&mut b, "STABLE_PRICE"); |
| 189 | + push_addr(&mut b, token); |
| 190 | + sha256_hex(&b) |
| 191 | +} |
| 192 | + |
| 193 | +fn max_pnl_factor_for_adl_key(market: &str, is_long: bool) -> String { |
| 194 | + let mut b = Vec::new(); |
| 195 | + push_str(&mut b, "MAX_PNL_FACTOR_FOR_ADL"); |
| 196 | + push_addr(&mut b, market); |
| 197 | + push_bool(&mut b, is_long); |
| 198 | + sha256_hex(&b) |
| 199 | +} |
| 200 | + |
| 201 | +fn max_pnl_factor_key(pnl_hex: &str, market: &str, is_long: bool) -> String { |
| 202 | + let mut b = Vec::new(); |
| 203 | + push_str(&mut b, "MAX_PNL_FACTOR"); |
| 204 | + push_raw32(&mut b, pnl_hex); |
| 205 | + push_addr(&mut b, market); |
| 206 | + push_bool(&mut b, is_long); |
| 207 | + sha256_hex(&b) |
| 208 | +} |
| 209 | + |
| 210 | +fn position_fee_factor_key(market: &str, for_positive: bool) -> String { |
| 211 | + let mut b = Vec::new(); |
| 212 | + push_str(&mut b, "POSITION_FEE_FACTOR"); |
| 213 | + push_addr(&mut b, market); |
| 214 | + push_bool(&mut b, for_positive); |
| 215 | + sha256_hex(&b) |
| 216 | +} |
| 217 | + |
| 218 | +fn swap_fee_factor_key(market: &str, for_positive: bool) -> String { |
| 219 | + let mut b = Vec::new(); |
| 220 | + push_str(&mut b, "SWAP_FEE_FACTOR"); |
| 221 | + push_addr(&mut b, market); |
| 222 | + push_bool(&mut b, for_positive); |
| 223 | + sha256_hex(&b) |
| 224 | +} |
| 225 | + |
| 226 | +fn swap_impact_factor_key(market: &str, is_positive: bool) -> String { |
| 227 | + let mut b = Vec::new(); |
| 228 | + push_str(&mut b, "SWAP_IMPACT_FACTOR"); |
| 229 | + push_addr(&mut b, market); |
| 230 | + push_bool(&mut b, is_positive); |
| 231 | + sha256_hex(&b) |
| 232 | +} |
| 233 | + |
| 234 | +fn position_impact_factor_key(market: &str, is_positive: bool) -> String { |
| 235 | + let mut b = Vec::new(); |
| 236 | + push_str(&mut b, "POSITION_IMPACT_FACTOR"); |
| 237 | + push_addr(&mut b, market); |
| 238 | + push_bool(&mut b, is_positive); |
| 239 | + sha256_hex(&b) |
| 240 | +} |
| 241 | + |
| 242 | +fn keeper_public_key_key(index: u32) -> String { |
| 243 | + // prefix = sha256(push_str("KEEPER_PUBLIC_KEY")) |
| 244 | + let mut prefix_buf = Vec::new(); |
| 245 | + push_str(&mut prefix_buf, "KEEPER_PUBLIC_KEY"); |
| 246 | + let prefix = Sha256::digest(&prefix_buf); |
| 247 | + let mut b = prefix.to_vec(); |
| 248 | + b.extend_from_slice(&index.to_be_bytes()); |
| 249 | + sha256_hex(&b) |
| 250 | +} |
| 251 | + |
| 252 | +// ── hex helper (avoids adding the hex crate as a named dep) ────────────────── |
| 253 | + |
| 254 | +mod hex { |
| 255 | + pub fn decode(s: &str) -> Result<Vec<u8>, ()> { |
| 256 | + if s.len() % 2 != 0 { |
| 257 | + return Err(()); |
| 258 | + } |
| 259 | + (0..s.len() / 2) |
| 260 | + .map(|i| u8::from_str_radix(&s[i * 2..i * 2 + 2], 16).map_err(|_| ())) |
| 261 | + .collect() |
| 262 | + } |
| 263 | + |
| 264 | + pub fn encode(bytes: impl AsRef<[u8]>) -> String { |
| 265 | + bytes.as_ref().iter().map(|b| format!("{:02x}", b)).collect() |
| 266 | + } |
| 267 | +} |
| 268 | + |
| 269 | +// ── test ────────────────────────────────────────────────────────────────────── |
| 270 | + |
| 271 | +fn load_vectors() -> (serde_json::Value, serde_json::Value) { |
| 272 | + // Walk up from the test binary's manifest dir to find the workspace root. |
| 273 | + let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); |
| 274 | + let json_path = manifest_dir |
| 275 | + .parent() // libs/ |
| 276 | + .unwrap() |
| 277 | + .parent() // workspace root |
| 278 | + .unwrap() |
| 279 | + .join("scripts/key_vectors.json"); |
| 280 | + let content = std::fs::read_to_string(&json_path) |
| 281 | + .unwrap_or_else(|e| panic!("cannot read {}: {}", json_path.display(), e)); |
| 282 | + let root: serde_json::Value = serde_json::from_str(&content).expect("invalid JSON"); |
| 283 | + let meta = root["_meta"].clone(); |
| 284 | + let vectors = root["vectors"].clone(); |
| 285 | + (meta, vectors) |
| 286 | +} |
| 287 | + |
| 288 | +#[test] |
| 289 | +fn rust_encoding_matches_committed_vectors() { |
| 290 | + let (meta, vectors) = load_vectors(); |
| 291 | + |
| 292 | + let market = meta["MARKET"].as_str().unwrap(); |
| 293 | + let token_a = meta["TOKEN_A"].as_str().unwrap(); |
| 294 | + let token_b = meta["TOKEN_B"].as_str().unwrap(); |
| 295 | + let pnl_hex = meta["PNL_HEX"].as_str().unwrap(); |
| 296 | + |
| 297 | + macro_rules! check { |
| 298 | + ($key:expr, $got:expr) => {{ |
| 299 | + let expected = vectors[$key].as_str().unwrap_or_else(|| { |
| 300 | + panic!("vector '{}' missing from key_vectors.json", $key) |
| 301 | + }); |
| 302 | + assert_eq!( |
| 303 | + $got, expected, |
| 304 | + "Rust/Python divergence for key '{}'\n Rust {}\n JSON {}", |
| 305 | + $key, $got, expected |
| 306 | + ); |
| 307 | + }}; |
| 308 | + } |
| 309 | + |
| 310 | + // role keys |
| 311 | + for role in ["ROLE_ADMIN","CONTROLLER","MARKET_KEEPER","ORDER_KEEPER", |
| 312 | + "LIQUIDATION_KEEPER","ADL_KEEPER","FEE_KEEPER"] { |
| 313 | + check!(format!("role:{}", role), role_key(role)); |
| 314 | + } |
| 315 | + |
| 316 | + // constant keys |
| 317 | + check!("max_pnl_factor_for_traders", max_pnl_factor_for_traders()); |
| 318 | + check!("max_pnl_factor_for_deposits", max_pnl_factor_for_deposits()); |
| 319 | + check!("max_pnl_factor_for_withdrawals",max_pnl_factor_for_withdrawals()); |
| 320 | + check!("market_type_default", market_type_default()); |
| 321 | + |
| 322 | + // address keys |
| 323 | + check!("max_pool_amount:market,token_a", max_pool_amount_key(market, token_a)); |
| 324 | + check!("max_pool_amount:market,token_b", max_pool_amount_key(market, token_b)); |
| 325 | + check!("min_collateral_factor:market", min_collateral_factor_key(market)); |
| 326 | + check!("max_leverage:market", max_leverage_key(market)); |
| 327 | + check!("funding_factor:market", funding_factor_key(market)); |
| 328 | + check!("funding_exponent_factor:market", funding_exponent_factor_key(market)); |
| 329 | + check!("funding_increase_factor_per_second:market", funding_increase_factor_per_second_key(market)); |
| 330 | + check!("funding_decrease_factor_per_second:market", funding_decrease_factor_per_second_key(market)); |
| 331 | + check!("min_funding_factor_per_second:market", min_funding_factor_per_second_key(market)); |
| 332 | + check!("max_funding_factor_per_second:market", max_funding_factor_per_second_key(market)); |
| 333 | + check!("swap_impact_exponent_factor:market", swap_impact_exponent_factor_key(market)); |
| 334 | + check!("position_impact_exponent_factor:market", position_impact_exponent_factor_key(market)); |
| 335 | + check!("min_market_tokens_for_first_deposit:market", min_market_tokens_for_first_deposit_key(market)); |
| 336 | + check!("stable_price:token_a", stable_price_key(token_a)); |
| 337 | + |
| 338 | + // bool keys |
| 339 | + check!("max_open_interest:market,long", max_open_interest_key(market, true)); |
| 340 | + check!("max_open_interest:market,short", max_open_interest_key(market, false)); |
| 341 | + check!("borrowing_factor:market,long", borrowing_factor_key(market, true)); |
| 342 | + check!("borrowing_factor:market,short", borrowing_factor_key(market, false)); |
| 343 | + check!("borrowing_exponent_factor:market,long", borrowing_exponent_factor_key(market, true)); |
| 344 | + check!("borrowing_exponent_factor:market,short", borrowing_exponent_factor_key(market, false)); |
| 345 | + check!("max_pnl_factor_for_adl:market,long", max_pnl_factor_for_adl_key(market, true)); |
| 346 | + check!("max_pnl_factor_for_adl:market,short", max_pnl_factor_for_adl_key(market, false)); |
| 347 | + check!("max_pnl_factor:long", max_pnl_factor_key(pnl_hex, market, true)); |
| 348 | + check!("max_pnl_factor:short", max_pnl_factor_key(pnl_hex, market, false)); |
| 349 | + check!("position_fee_factor:market,pos", position_fee_factor_key(market, true)); |
| 350 | + check!("position_fee_factor:market,neg", position_fee_factor_key(market, false)); |
| 351 | + check!("swap_fee_factor:market,pos", swap_fee_factor_key(market, true)); |
| 352 | + check!("swap_fee_factor:market,neg", swap_fee_factor_key(market, false)); |
| 353 | + check!("swap_impact_factor:market,pos", swap_impact_factor_key(market, true)); |
| 354 | + check!("swap_impact_factor:market,neg", swap_impact_factor_key(market, false)); |
| 355 | + check!("position_impact_factor:market,pos", position_impact_factor_key(market, true)); |
| 356 | + check!("position_impact_factor:market,neg", position_impact_factor_key(market, false)); |
| 357 | + |
| 358 | + // keeper key |
| 359 | + check!("keeper_public_key:0", keeper_public_key_key(0)); |
| 360 | + check!("keeper_public_key:1", keeper_public_key_key(1)); |
| 361 | +} |
0 commit comments