Repository navigation
fix(sdk): outdated platfrom version in JS SDK - #3046
Conversation
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📝 WalkthroughWalkthroughThe PlatformVersionWasm API is refactored from an enum-based design to a struct-based approach with a single Changes
Estimated Code Review Effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@packages/wasm-dpp2/src/version.rs`:
- Around line 45-51: Change the wasm constructor signature for
PlatformVersionWasm::new to accept a JsValue and validate it with try_to_u32
(reusing the same checks as TryFrom<&JsValue>) before proceeding: call
try_to_u32 on the incoming JsValue, map any conversion error to
WasmDppError::invalid_argument, then call PlatformVersion::get with the
resulting u32 and preserve the existing PlatformVersion::get error mapping;
update references to the constructor accordingly so the PlatformVersionWasm {
version } is created only after the validated u32 is obtained.
🧹 Nitpick comments (1)
packages/wasm-dpp2/src/version.rs (1)
91-110: Consider rejecting string inputs to match the new PlatformVersionLike contract.
PlatformVersionLikeno longer includes strings, buttry_to_u32still accepts numeric strings. If you want runtime behavior to match the TS declaration, explicitly reject strings beforetry_to_u32.♻️ Suggested guard
if value.is_undefined() || value.is_null() { return Ok(Self::default()); } // Detect PlatformVersionWasm instance via to_wasm if let Ok(wasm_ref) = value.to_wasm::<PlatformVersionWasm>("PlatformVersion") { return Ok(wasm_ref.clone()); } + + if value.as_string().is_some() { + return Err(WasmDppError::invalid_argument( + "'platformVersion' must be a number", + )); + } // Otherwise treat as raw number let version = try_to_u32(value, "platformVersion")?;
| #[wasm_bindgen(constructor)] | ||
| pub fn new(version: u32) -> Result<PlatformVersionWasm, WasmDppError> { | ||
| PlatformVersion::get(version).map_err(|_| { | ||
| WasmDppError::invalid_argument(format!("unknown platform version: {}", version)) | ||
| })?; | ||
| Ok(PlatformVersionWasm { version }) | ||
| } |
There was a problem hiding this comment.
🧩 Analysis chain
🌐 Web query:
wasm-bindgen JavaScript number conversion u32 truncation fractional
💡 Result:
In wasm-bindgen, passing a JavaScript Number into a Rust u32 parameter works like this: (rustwasm.github.io)
- If the JS value is
NaN,Infinity, or-Infinity→ Rust receives0. - Otherwise, the JS number is truncated toward zero (fractional part dropped, like
Math.trunc). - Then it’s converted to
u32; if it’s out of range, it wraps modulo 2³² (same behavior asnew Uint32Array([value])[0]).
Examples (JS → Rust u32):
1.999→1-1.999→4294967295(becauseMath.trunc(-1.999) === -1, then wrap)4294967296→0
Citations:
🏁 Script executed:
# Search for try_to_u32 function in the codebase
rg "try_to_u32" --type rust -B2 -A5Repository: dashpay/platform
Length of output: 20722
🏁 Script executed:
# Search for TryFrom implementation with JsValue for PlatformVersionWasm
rg "TryFrom.*JsValue" --type rust -B3 -A10Repository: dashpay/platform
Length of output: 50374
🏁 Script executed:
# Check the actual file to understand current implementation and related code
cat -n packages/wasm-dpp2/src/version.rs | head -80Repository: dashpay/platform
Length of output: 3013
🏁 Script executed:
# Search for other constructors in wasm files to see if there's a pattern
rg "#\[wasm_bindgen\(constructor\)\]" --type rust -B2 -A8 -m 10Repository: dashpay/platform
Length of output: 50373
Use JsValue and try_to_u32 in the constructor for consistent input validation.
The constructor currently accepts a bare u32, which allows wasm-bindgen to silently truncate fractional numbers (e.g., 1.999 → 1) and convert NaN/Infinity to 0 without validation. This bypasses the NaN/Infinity/fractional checks that try_to_u32 enforces and that the TryFrom<&JsValue> implementation already applies. Change the constructor to accept JsValue and validate via try_to_u32 to keep behavior consistent with the trait implementation.
🔧 Suggested fix
- pub fn new(version: u32) -> Result<PlatformVersionWasm, WasmDppError> {
- PlatformVersion::get(version).map_err(|_| {
- WasmDppError::invalid_argument(format!("unknown platform version: {}", version))
- })?;
- Ok(PlatformVersionWasm { version })
- }
+ pub fn new(version: JsValue) -> Result<PlatformVersionWasm, WasmDppError> {
+ let version = try_to_u32(&version, "platformVersion")?;
+ PlatformVersion::get(version).map_err(|_| {
+ WasmDppError::invalid_argument(format!("unknown platform version: {}", version))
+ })?;
+ Ok(PlatformVersionWasm { version })
+ }📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| #[wasm_bindgen(constructor)] | |
| pub fn new(version: u32) -> Result<PlatformVersionWasm, WasmDppError> { | |
| PlatformVersion::get(version).map_err(|_| { | |
| WasmDppError::invalid_argument(format!("unknown platform version: {}", version)) | |
| })?; | |
| Ok(PlatformVersionWasm { version }) | |
| } | |
| #[wasm_bindgen(constructor)] | |
| pub fn new(version: JsValue) -> Result<PlatformVersionWasm, WasmDppError> { | |
| let version = try_to_u32(&version, "platformVersion")?; | |
| PlatformVersion::get(version).map_err(|_| { | |
| WasmDppError::invalid_argument(format!("unknown platform version: {}", version)) | |
| })?; | |
| Ok(PlatformVersionWasm { version }) | |
| } |
🤖 Prompt for AI Agents
In `@packages/wasm-dpp2/src/version.rs` around lines 45 - 51, Change the wasm
constructor signature for PlatformVersionWasm::new to accept a JsValue and
validate it with try_to_u32 (reusing the same checks as TryFrom<&JsValue>)
before proceeding: call try_to_u32 on the incoming JsValue, map any conversion
error to WasmDppError::invalid_argument, then call PlatformVersion::get with the
resulting u32 and preserve the existing PlatformVersion::get error mapping;
update references to the constructor accordingly so the PlatformVersionWasm {
version } is created only after the validated u32 is obtained.
Issue being fixed or feature implemented
PlatformVersionWasmwas a manually-maintained enum (V1–V10) that required 6 separate code changes every time a new platform version was added in Rust. It was already out of sync — Rust hadV1–V12while WASM only hadV1–V10.What was done?
Replaced
PlatformVersionWasmenum with au32wrapper struct that automatically adapts to any number of platform versions without code changes:u32— constructor validates viaPlatformVersion::get(), so only valid versions are acceptedlatest(),first(),current()delegate toPlatformVersionstatics instead of hardcoding valuesPlatformVersion::desired()(current protocol version) instead of hardcoded1impl_wasm_type_info!macro andto_wasm()pattern consistent with other WASM typestry_to_u32()for numeric input (handles NaN, Infinity, fractional values)parse_version_string()helper, string handling inTryFrom, unusedFrom<PlatformVersionWasm> for StringPlatformVersionLikesimplified toPlatformVersion | number(removedstring)new PlatformVersion(1)instead ofPlatformVersion.PLATFORM_V1, explicit version passed where comparing against v1 fixturesHow Has This Been Tested?
cargo fmt -p wasm-dpp2 -- --check— cleancargo clippy -p wasm-dpp2 --target wasm32-unknown-unknown— cleancargo check -p wasm-dpp2 --target wasm32-unknown-unknown— compilescargo check -p wasm-sdk --target wasm32-unknown-unknown— compilesyarn workspace dash build— compilesbuild.sh+bundle.cjs) — successyarn workspace @dashevo/wasm-dpp2 test— 718 passing, 0 failingBreaking Changes
PlatformVersion.PLATFORM_V1,PlatformVersion.PLATFORM_V2, etc. no longer exist. Usenew PlatformVersion(1),new PlatformVersion(2), etc.PlatformVersionLikeno longer acceptsstring— onlyPlatformVersion | numbercurrent(desired) instead of1Checklist:
For repository code-owners and collaborators only
Summary by CodeRabbit
Refactor
Tests