Repository navigation
strip xdr from proposal hash - #661
Conversation
WalkthroughThe changes update how proposal description hashes are computed in the Governor governance contract. The implementation switches from hashing XDR-encoded representation to hashing raw UTF-8 bytes of the description string using keccak256. Documentation is updated to clarify this behavior, and tests are adjusted to match the new hash computation. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #661 +/- ##
=======================================
Coverage 96.16% 96.16%
=======================================
Files 59 59
Lines 6134 6134
=======================================
Hits 5899 5899
Misses 235 235 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/governance/src/governor/storage.rs (1)
494-499:⚠️ Potential issue | 🟠 MajorThis breaks proposal ID stability across upgrades.
Line 494 changes the description component from the legacy XDR bytes to raw string bytes, so the same logical proposal now hashes to a different
proposal_id. Since proposals are stored under that ID, an in-place upgrade leaves pre-upgrade proposals under the old key, and the duplicate check on Lines 498-499 stops detecting them. Existing proposals then require the legacy hash flow to execute/cancel, and identical proposals can be recreated under a second ID.If upgrade compatibility matters, keep a legacy lookup/check path here or treat this as an explicit breaking migration / fresh deployment only.
🛠️ Minimal guard for legacy duplicate detection
let description_hash = e.crypto().keccak256(&description.to_bytes()).to_bytes(); let proposal_id = hash_proposal(e, &targets, &functions, &args, &description_hash); + let legacy_description_hash = e.crypto().keccak256(&description.clone().to_xdr(e)).to_bytes(); + let legacy_proposal_id = + hash_proposal(e, &targets, &functions, &args, &legacy_description_hash); // Check proposal doesn't already exist - if e.storage().persistent().has(&GovernorStorageKey::Proposal(proposal_id.clone())) { + if e.storage().persistent().has(&GovernorStorageKey::Proposal(proposal_id.clone())) + || e.storage().persistent().has(&GovernorStorageKey::Proposal(legacy_proposal_id)) + { panic_with_error!(e, GovernorError::ProposalAlreadyExists); }This only protects duplicate detection;
execute/cancelstill need an explicit legacy-compatibility story.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/governance/src/governor/storage.rs` around lines 494 - 499, The duplicate-check now uses description raw bytes which breaks upgrade compatibility; compute the legacy description hash using the pre-upgrade encoding (the same XDR encoding used previously, e.g. the legacy to-xdr bytes function) and derive a legacy_proposal_id via hash_proposal just like the new proposal_id, then change the existence check to look up e.storage().persistent().has(&GovernorStorageKey::Proposal(proposal_id.clone())) || e.storage().persistent().has(&GovernorStorageKey::Proposal(legacy_proposal_id.clone())) and panic_with_error!(e, GovernorError::ProposalAlreadyExists) if either is present; keep the new description_hash and proposal_id for new storage writes but include this legacy lookup (reference symbols: description_hash, hash_proposal, proposal_id, legacy_proposal_id, GovernorStorageKey::Proposal, e.storage().persistent().has, GovernorError::ProposalAlreadyExists).
🧹 Nitpick comments (1)
packages/governance/src/governor/test.rs (1)
974-975: Consider a localdescription_hash()helper.This raw-byte keccak expression is now repeated throughout the module. Centralizing it would keep future hash-format changes localized and make the tests easier to skim.
♻️ Possible cleanup
+fn description_hash(e: &Env, description: &String) -> BytesN<32> { + e.crypto().keccak256(&description.to_bytes()).to_bytes() +} + #[test] fn hash_proposal_is_deterministic() { let (e, _) = setup_env(); let (targets, functions, args, description) = simple_proposal(&e); - let desc_hash = e.crypto().keccak256(&description.to_bytes()).to_bytes(); + let desc_hash = description_hash(&e, &description);Apply the same helper at the other updated call sites.
Also applies to: 989-990, 1002-1002
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/governance/src/governor/test.rs` around lines 974 - 975, Introduce a local helper function description_hash(description: &SomeType) -> [u8; N] (name it description_hash) that encapsulates the repeated keccak computation (currently written as e.crypto().keccak256(&description.to_bytes()).to_bytes()); replace the three raw occurrences (the one shown and the other two at the reported sites) with calls to description_hash(&description) so any future hash-format change is centralized; ensure the helper uses the same e.crypto() context or takes the crypto/engine reference if needed and update imports/signature accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@packages/governance/src/governor/storage.rs`:
- Around line 494-499: The duplicate-check now uses description raw bytes which
breaks upgrade compatibility; compute the legacy description hash using the
pre-upgrade encoding (the same XDR encoding used previously, e.g. the legacy
to-xdr bytes function) and derive a legacy_proposal_id via hash_proposal just
like the new proposal_id, then change the existence check to look up
e.storage().persistent().has(&GovernorStorageKey::Proposal(proposal_id.clone()))
||
e.storage().persistent().has(&GovernorStorageKey::Proposal(legacy_proposal_id.clone()))
and panic_with_error!(e, GovernorError::ProposalAlreadyExists) if either is
present; keep the new description_hash and proposal_id for new storage writes
but include this legacy lookup (reference symbols: description_hash,
hash_proposal, proposal_id, legacy_proposal_id, GovernorStorageKey::Proposal,
e.storage().persistent().has, GovernorError::ProposalAlreadyExists).
---
Nitpick comments:
In `@packages/governance/src/governor/test.rs`:
- Around line 974-975: Introduce a local helper function
description_hash(description: &SomeType) -> [u8; N] (name it description_hash)
that encapsulates the repeated keccak computation (currently written as
e.crypto().keccak256(&description.to_bytes()).to_bytes()); replace the three raw
occurrences (the one shown and the other two at the reported sites) with calls
to description_hash(&description) so any future hash-format change is
centralized; ensure the helper uses the same e.crypto() context or takes the
crypto/engine reference if needed and update imports/signature accordingly.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: b1a2ec99-1045-449c-a8c6-02d353eab1e6
📒 Files selected for processing (4)
examples/fungible-governor/token/src/test.rspackages/governance/src/governor/mod.rspackages/governance/src/governor/storage.rspackages/governance/src/governor/test.rs
Partially addresses #641
PR Checklist
Summary by CodeRabbit