Skip to content

strip xdr from proposal hash - #661

Merged
ozgunozerk merged 1 commit into
mainfrom
strip-xdr
Mar 25, 2026
Merged

ozgunozerk merged 1 commit into
mainfrom
strip-xdr

Conversation

@ozgunozerk

@ozgunozerk ozgunozerk commented Mar 25, 2026 •

Copy link
Copy Markdown
Collaborator

Partially addresses #641

PR Checklist

  • Tests
  • Documentation

Summary by CodeRabbit

  • Bug Fixes
    • Updated proposal description hash computation to use raw string bytes instead of encoded format, simplifying hash reproduction for off-chain clients and external integrations.

@ozgunozerk
ozgunozerk requested a review from brozorec March 25, 2026 10:04
@ozgunozerk ozgunozerk self-assigned this Mar 25, 2026
@coderabbitai

coderabbitai Bot commented Mar 25, 2026 •

Copy link
Copy Markdown
Contributor

Walkthrough

The 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

Cohort / File(s) Summary
Documentation Updates
packages/governance/src/governor/mod.rs
Updated get_proposal_id() documentation to explicitly define description_hash as keccak256(description.to_bytes()) instead of generic "hash of proposal description," clarifying the input is raw UTF-8 bytes without XDR encoding.
Implementation Changes
packages/governance/src/governor/storage.rs
Modified propose() to compute description_hash via keccak256(description.to_bytes()) instead of keccak256(description.clone().to_xdr(e)). Updated hash_proposal() documentation to reflect that off-chain clients can reproduce the hash by hashing raw string bytes directly.
Test Updates
examples/fungible-governor/token/src/test.rs, packages/governance/src/governor/test.rs
Removed xdr::ToXdr imports and updated test hash computations to use description.to_bytes() for keccak256 hashing instead of XDR-encoded representation across multiple test scenarios.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • Governance base contract #563: Related PR that changes Governor proposal ID and description_hash computation to use keccak256(description.to_bytes()) instead of XDR-encoded hashing across the same governance module files.

Suggested reviewers

  • brozorec

Poem

🐰 No more XDR tangles in our hash,
Just raw bytes, clean and bold dash,
Keccak256 now speaks the truth so clear,
Off-chain verification without fear! 🌿✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'strip xdr from proposal hash' is concise and directly reflects the main change: removing XDR encoding from the proposal hash computation and using raw bytes instead.
Description check ✅ Passed The description includes a reference to the related issue (#641) and confirms both required checklist items (Tests and Documentation) are complete, meeting the template requirements.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch strip-xdr

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov

codecov Bot commented Mar 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.16%. Comparing base (ff17d24) to head (7788b02).
⚠️ Report is 2 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 | 🟠 Major

This 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/cancel still 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 local description_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

📥 Commits

Reviewing files that changed from the base of the PR and between ff17d24 and 7788b02.

📒 Files selected for processing (4)
  • examples/fungible-governor/token/src/test.rs
  • packages/governance/src/governor/mod.rs
  • packages/governance/src/governor/storage.rs
  • packages/governance/src/governor/test.rs

@ozgunozerk
ozgunozerk merged commit 96a2152 into main Mar 25, 2026
6 checks passed
@ozgunozerk
ozgunozerk deleted the strip-xdr branch March 25, 2026 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants