-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(wasm-dpp): add extra methods for state transitions #2401
base: v1.8-dev
Are you sure you want to change the base?
Conversation
Warning Rate limit exceeded@pshenmic has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 8 minutes and 25 seconds before requesting another review. ⌛ 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. 📒 Files selected for processing (3)
WalkthroughThis pull request introduces a comprehensive set of enhancements across multiple Rust and WebAssembly files, focusing on expanding the functionality of various state transition structs. The changes primarily involve adding new methods for managing user fee increases, identity contract nonces, and signature-related operations across different transition types in the Dash Platform Protocol (DPP) implementation. Additionally, new variants for the Changes
Possibly related PRs
Suggested Labels
Suggested Reviewers
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (2)
packages/wasm-dpp/src/identity/state_transition/identity_credit_withdrawal_transition/transition.rs (1)
149-157
: Consider renaming methods for consistency.The getter and setter methods have inconsistent naming:
- Getter:
get_identity_nonce
- Setter:
set_identity_contract_nonce
Consider using consistent naming:
- pub fn get_identity_nonce(&self) -> u64 { + pub fn get_identity_contract_nonce(&self) -> u64 {packages/wasm-dpp/src/identity/state_transition/identity_topup_transition/identity_topup_transition.rs (1)
118-126
: Consider adding documentation for the user fee methodsWhile the implementation is correct and consistent with other transition types, consider adding documentation to explain:
- The purpose of user fee increases
- Valid range for the fee increase value
- Impact on transaction processing
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (14)
packages/rs-dpp/src/state_transition/state_transitions/document/documents_batch_transition/document_transition/action_type.rs
(1 hunks)packages/wasm-dpp/src/data_contract/state_transition/data_contract_create_transition/mod.rs
(1 hunks)packages/wasm-dpp/src/data_contract/state_transition/data_contract_update_transition/mod.rs
(1 hunks)packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_create_transition.rs
(2 hunks)packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_delete_transition.rs
(1 hunks)packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_replace_transition.rs
(1 hunks)packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs
(3 hunks)packages/wasm-dpp/src/document/state_transition/document_batch_transition/mod.rs
(1 hunks)packages/wasm-dpp/src/identity/state_transition/identity_create_transition/identity_create_transition.rs
(1 hunks)packages/wasm-dpp/src/identity/state_transition/identity_credit_transfer_transition/transition.rs
(2 hunks)packages/wasm-dpp/src/identity/state_transition/identity_credit_withdrawal_transition/transition.rs
(2 hunks)packages/wasm-dpp/src/identity/state_transition/identity_topup_transition/identity_topup_transition.rs
(1 hunks)packages/wasm-dpp/src/identity/state_transition/identity_update_transition/identity_update_transition.rs
(2 hunks)packages/wasm-dpp/src/voting/state_transition/masternode_vote_transition/mod.rs
(8 hunks)
👮 Files not reviewed due to content moderation or server errors (5)
- packages/wasm-dpp/src/data_contract/state_transition/data_contract_create_transition/mod.rs
- packages/wasm-dpp/src/data_contract/state_transition/data_contract_update_transition/mod.rs
- packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_replace_transition.rs
- packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_create_transition.rs
- packages/wasm-dpp/src/identity/state_transition/identity_create_transition/identity_create_transition.rs
🔇 Additional comments (16)
packages/rs-dpp/src/state_transition/state_transitions/document/documents_batch_transition/document_transition/action_type.rs (1)
42-43
: LGTM! New action types properly implemented.
The new Purchase
and UpdatePrice
variants are correctly added to the TryFrom
implementation, maintaining consistency with the existing pattern.
packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_delete_transition.rs (2)
76-79
: LGTM! Identity contract nonce getter properly implemented.
The getter method correctly exposes the nonce value from the base transition.
81-88
: LGTM! Identity contract nonce setter properly implemented.
The setter method correctly handles the base transition modification with proper cloning.
packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/mod.rs (1)
106-130
: LGTM! Prefunded voting balance handling properly implemented.
The implementation correctly handles the prefunded voting balance, including proper null handling and JS object creation.
packages/wasm-dpp/src/identity/state_transition/identity_credit_transfer_transition/transition.rs (3)
102-110
: LGTM! User fee increase methods are well implemented.
The getter and setter for user fee increase are properly implemented with correct type conversion.
112-120
: LGTM! Identity nonce methods are properly implemented.
The getter and setter for identity nonce maintain consistency with the inner implementation.
341-344
: LGTM! Signature public key ID getter is correctly implemented.
The method properly exposes the inner signature public key ID.
packages/wasm-dpp/src/voting/state_transition/masternode_vote_transition/mod.rs (2)
73-81
: LGTM! User fee methods are well-implemented.
The implementation follows the established pattern across other transition types and handles types appropriately.
83-86
: LGTM! Identity nonce getter is correctly implemented.
The method properly exposes the underlying nonce value, aligning with the PR objectives.
packages/wasm-dpp/src/identity/state_transition/identity_credit_withdrawal_transition/transition.rs (2)
139-147
: LGTM! User fee methods are consistently implemented.
The implementation aligns with other transition types and handles types appropriately.
423-426
: LGTM! Signature public key ID getter is properly implemented.
The method follows the established pattern and correctly exposes the underlying ID.
packages/wasm-dpp/src/identity/state_transition/identity_update_transition/identity_update_transition.rs (3)
172-180
: LGTM: Well-implemented user fee methods.
The implementation is consistent with other transition types and follows Rust conventions.
182-190
: LGTM: Properly implemented nonce management methods.
The implementation correctly handles the identity contract nonce with appropriate type conversions.
435-438
: LGTM: Signature public key ID getter implementation.
The method correctly retrieves the signature public key ID.
packages/wasm-dpp/src/document/state_transition/document_batch_transition/mod.rs (1)
101-109
: LGTM! Clean implementation of user fee management methods.
The implementation follows best practices with:
- Appropriate type safety using u16 for fee values
- Consistent naming conventions
- Clean integration with the existing WASM bindings architecture
packages/wasm-dpp/src/identity/state_transition/identity_topup_transition/identity_topup_transition.rs (1)
Line range hint 85-126
: Verify consistent implementation across all transition types
The implementation of user fee and signature methods looks consistent across the reviewed files. Let's verify this pattern is followed in all transition types.
✅ Verification successful
Implementation is consistent across all transition types
The verification results show that both user fee increase and signature methods are consistently implemented across all state transition types:
-
User fee increase methods (
get_user_fee_increase
andset_user_fee_increase
) are uniformly implemented in:- Identity transitions (Create, Update, Topup, Credit Transfer, Credit Withdrawal)
- Document transitions (Batch)
- Data Contract transitions (Create, Update)
- Masternode Vote transitions
-
Signature methods (
get_signature
andget_signature_public_key_id
) follow the same pattern across:- Identity transitions
- Document transitions
- Data Contract transitions
- Masternode Vote transitions
All implementations maintain consistent return types and parameter types, with proper WASM bindings.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Verify consistent implementation of user fee methods across all transition types
# Search for user fee increase methods
echo "Checking user fee increase methods implementation..."
rg -A 2 "fn (get|set)_user_fee_increase" packages/wasm-dpp/src/
# Search for signature-related methods
echo "Checking signature-related methods implementation..."
rg -A 2 "fn get_signature(_public_key_id)?" packages/wasm-dpp/src/
Length of output: 14190
#[wasm_bindgen(js_name=getData)] | ||
pub fn get_data(&self) -> JsValue { | ||
match &self.0 { | ||
DocumentTransition::Create(document_create_transition) => { | ||
let json_value = document_create_transition.data().to_json_value().unwrap(); | ||
json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible()).unwrap() | ||
} | ||
DocumentTransition::Replace(document_replace_transition) => { | ||
let json_value = document_replace_transition.data().to_json_value().unwrap(); | ||
json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible()).unwrap() | ||
} | ||
DocumentTransition::Delete(document_delete_transition) => JsValue::null(), | ||
DocumentTransition::Transfer(document_transfer_transition) => JsValue::null(), | ||
DocumentTransition::UpdatePrice(document_update_price_transition) => JsValue::null(), | ||
DocumentTransition::Purchase(document_purchase_transition) => JsValue::null() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add error handling for JSON conversion.
While the implementation is generally good, the unwrap calls on JSON conversion could lead to runtime panics.
Consider handling potential conversion errors:
pub fn get_data(&self) -> JsValue {
match &self.0 {
DocumentTransition::Create(document_create_transition) => {
- let json_value = document_create_transition.data().to_json_value().unwrap();
- json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible()).unwrap()
+ document_create_transition.data()
+ .to_json_value()
+ .and_then(|json_value|
+ json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible())
+ )
+ .unwrap_or(JsValue::null())
}
DocumentTransition::Replace(document_replace_transition) => {
- let json_value = document_replace_transition.data().to_json_value().unwrap();
- json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible()).unwrap()
+ document_replace_transition.data()
+ .to_json_value()
+ .and_then(|json_value|
+ json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible())
+ )
+ .unwrap_or(JsValue::null())
}
📝 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(js_name=getData)] | |
pub fn get_data(&self) -> JsValue { | |
match &self.0 { | |
DocumentTransition::Create(document_create_transition) => { | |
let json_value = document_create_transition.data().to_json_value().unwrap(); | |
json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible()).unwrap() | |
} | |
DocumentTransition::Replace(document_replace_transition) => { | |
let json_value = document_replace_transition.data().to_json_value().unwrap(); | |
json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible()).unwrap() | |
} | |
DocumentTransition::Delete(document_delete_transition) => JsValue::null(), | |
DocumentTransition::Transfer(document_transfer_transition) => JsValue::null(), | |
DocumentTransition::UpdatePrice(document_update_price_transition) => JsValue::null(), | |
DocumentTransition::Purchase(document_purchase_transition) => JsValue::null() | |
} | |
} | |
#[wasm_bindgen(js_name=getData)] | |
pub fn get_data(&self) -> JsValue { | |
match &self.0 { | |
DocumentTransition::Create(document_create_transition) => { | |
document_create_transition.data() | |
.to_json_value() | |
.and_then(|json_value| | |
json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible()) | |
) | |
.unwrap_or(JsValue::null()) | |
} | |
DocumentTransition::Replace(document_replace_transition) => { | |
document_replace_transition.data() | |
.to_json_value() | |
.and_then(|json_value| | |
json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible()) | |
) | |
.unwrap_or(JsValue::null()) | |
} | |
DocumentTransition::Delete(document_delete_transition) => JsValue::null(), | |
DocumentTransition::Transfer(document_transfer_transition) => JsValue::null(), | |
DocumentTransition::UpdatePrice(document_update_price_transition) => JsValue::null(), | |
DocumentTransition::Purchase(document_purchase_transition) => JsValue::null() | |
} | |
} |
&"documentTypeName".into(), | ||
&contested_document_resource_vote_poll | ||
.document_type_name | ||
.clone() | ||
.into(), | ||
) | ||
.unwrap(); | ||
Reflect::set( | ||
&js_object, | ||
&"indexName".into(), | ||
&contested_document_resource_vote_poll | ||
.index_name | ||
.clone() | ||
.into(), | ||
) | ||
.unwrap(); | ||
|
||
let config = bincode::config::standard() | ||
.with_big_endian() | ||
.with_no_limit(); | ||
|
||
let serialized_index_values = contested_document_resource_vote_poll | ||
.index_values | ||
.iter() | ||
.map(|value| { | ||
JsValue::from(Buffer::from_bytes_owned( | ||
bincode::encode_to_vec(value, config) | ||
.expect("expected to encode value in path"), | ||
)) | ||
}); | ||
|
||
let js_array = Array::from_iter(serialized_index_values); | ||
|
||
Reflect::set(&js_object, &"indexValues".into(), &js_array.into()).unwrap(); | ||
|
||
Some(js_object) | ||
Vote::ResourceVote(vote) => { | ||
let js_object = Object::new(); | ||
|
||
Reflect::set( | ||
&js_object, | ||
&"choice".into(), | ||
&vote | ||
.resource_vote_choice() | ||
.clone() | ||
.to_string() | ||
.into(), | ||
).unwrap(); | ||
|
||
match vote.vote_poll() { | ||
VotePoll::ContestedDocumentResourceVotePoll( | ||
contested_document_resource_vote_poll, | ||
) => { | ||
let contract_id = IdentifierWrapper::from( | ||
contested_document_resource_vote_poll.contract_id.clone(), | ||
); | ||
|
||
Reflect::set(&js_object, &"contractId".into(), &contract_id.into()).unwrap(); | ||
Reflect::set( | ||
&js_object, | ||
&"documentTypeName".into(), | ||
&contested_document_resource_vote_poll | ||
.document_type_name | ||
.clone() | ||
.into(), | ||
) | ||
.unwrap(); | ||
Reflect::set( | ||
&js_object, | ||
&"indexName".into(), | ||
&contested_document_resource_vote_poll | ||
.index_name | ||
.clone() | ||
.into(), | ||
) | ||
.unwrap(); | ||
|
||
let config = bincode::config::standard() | ||
.with_big_endian() | ||
.with_no_limit(); | ||
|
||
let serialized_index_values = contested_document_resource_vote_poll | ||
.index_values | ||
.iter() | ||
.map(|value| { | ||
JsValue::from(Buffer::from_bytes_owned( | ||
bincode::encode_to_vec(value, config) | ||
.expect("expected to encode value in path"), | ||
)) | ||
}); | ||
|
||
let js_array = Array::from_iter(serialized_index_values); | ||
|
||
Reflect::set(&js_object, &"indexValues".into(), &js_array.into()).unwrap(); | ||
|
||
Some(js_object) | ||
} | ||
} | ||
}, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider adding error handling for bincode operations.
The implementation of contested_document_resource_vote_poll
could be improved:
- The
expect
call on line 292 could panic. Consider proper error handling:
- bincode::encode_to_vec(value, config)
- .expect("expected to encode value in path"),
+ bincode::encode_to_vec(value, config)
+ .map_err(|e| JsError::new(&e.to_string()))?,
- The
unwrap
calls onReflect::set
should handle potential errors:
- ).unwrap();
+ ).map_err(|e| JsError::new("Failed to set choice"))?;
Committable suggestion skipped: line range outside the PR's diff.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/wasm-dpp/src/voting/state_transition/masternode_vote_transition/mod.rs (1)
228-231
: Consider validating user fee boundaries.
Setting the user fee increase directly from user input may require logical checks (e.g., non-negative values or max limits). If your business rules allow any u16, this is fine; otherwise, consider validating.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/wasm-dpp/src/voting/state_transition/masternode_vote_transition/mod.rs
(4 hunks)
🔇 Additional comments (6)
packages/wasm-dpp/src/voting/state_transition/masternode_vote_transition/mod.rs (6)
136-136
: Check error propagation strategy.
The use of '.with_js_error()?' is acceptable; consider whether additional context is needed (e.g., logging or descriptive error messages) for more precise debugging. Otherwise, this usage looks correct.
223-226
: Confirm safe integer range for user fee increase.
Casting to u16 may lead to data truncation if the underlying type has a larger range. Verify that the internal logic never exceeds u16.
233-236
: No implementation issues found here.
The method cleanly returns the nonce, matching the intended usage.
241-303
: Avoid panics in bincode encoding and Reflect calls.
This block uses '.expect(...)' (line 292) and '.unwrap()' multiple times. If encoding or JavaScript reflection fails, it may cause runtime panics. Here’s a recommended approach, mirroring previous feedback:
Use '.map_err(|e| JsError::new(&e.to_string()))?' instead of '.expect(...)' and safely handle Reflect errors:
- .expect("expected to encode value in path")
+ .map_err(|e| JsError::new(&e.to_string()))?
- ).unwrap();
+ ).map_err(|e| JsError::new("Failed to set choice"))?;
322-322
: No issues identified with error return.
The error message is explicit enough.
381-381
: No further comments — end of class.
Closing brace appears to match all opened blocks.
Issue being fixed or feature implemented
WASM bindings were missing some methods for data in the state transitions, such like identityContractNonce, userFeeIncrease, signaturePublicKeyId, prefundingVotingBalance, vote choice and such, that we use to show on the Platform Explorer (specific transaction page). We did that in the our own fork for now (which is already live in production), and with this PR I'm merging changes in the main platform dev branch.
What was done?
How Has This Been Tested?
In the integration with Platform Explorer
Breaking Changes
No
Checklist:
For repository code-owners and collaborators only
Summary by CodeRabbit
New Features
Purchase
andUpdatePrice
for document transitions.Bug Fixes
Documentation