Skip to content

Conversation

@arepala-uml
Copy link
Contributor

@arepala-uml arepala-uml commented Nov 30, 2025

  1. Used CIP-129 specifications to create the bech32 (string) representations of the Voter type in the ledger/common.
  2. Added constants to build the CIP-129 headers and encode bech32 safely.
  3. Added TestVoterType in gov_test.go with coverage for the new string encodings.

Closes #994


Summary by cubic

Add a String() method for Voter that outputs CIP-129 bech32 identifiers for all voter types. This makes voter credentials human-readable and consistent with Cardano standards.

  • New Features
    • Implemented Voter.String() for cc hot (key/script), drep (key/script), and staking pool (via PoolId).
    • Added bech32 encoding helper that builds the CIP-129 header using existing voter/credential constants.
    • Added tests covering expected encodings and panic on unknown voter type.

Written for commit 232ddab. Summary will update automatically on new commits.

Summary by CodeRabbit

  • New Features

    • Adds CIP-129 bech32 voter encoding so voter identifiers are emitted in standardized bech32 form.
    • Voter objects now produce readable string identifiers for Constitutional Committee, DRep, and Staking Pool voter types.
  • Tests

    • Added tests verifying exact string/bech32 outputs for supported voter types and correct handling of unsupported types.

✏️ Tip: You can customize this high-level summary in your review settings.

…cations to generate bech32 encodings for each voter type

Signed-off-by: Akhil Repala <[email protected]>
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 30, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

📝 Walkthrough

Walkthrough

This change adds a String() method to the Voter type that produces Bech32 identifiers, preferring CIP-129 encoding. New helper(s) implement CIP-129 voter encoding paths for Constitutional Committee hot key/script, DRep key/script, and staking pool voters (pools use PoolId(v.Hash)). New imports (fmt and btcutil/bech32) support encoding. Existing ToPlutusData() remains unchanged. Tests were added (TestVoterString) to verify expected string outputs for several voter variants and that unknown voter types panic.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Areas requiring extra attention:

  • Bech32/CIP-129 encoding: verify bit/word conversion, prefix/version construction, and edge cases.
  • Panic usage: confirm panics on encoding/conversion failures align with project error-handling conventions.
  • Tests: validate hardcoded expected Bech32 strings match the CIP-129/CIP-105 specifications and provided hashes.
  • Staking pool handling: ensure PoolId(v.Hash) integration and resulting string format are correct.

Pre-merge checks and finishing touches

✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding a String() method to the Voter type.
Linked Issues check ✅ Passed The PR implements a String() method for Voter using CIP-129 bech32 encoding across all voter variants, directly addressing issue #994 requirements.
Out of Scope Changes check ✅ Passed All changes are focused on implementing the Voter.String() method and its supporting test; no unrelated modifications detected.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch voter_string

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3c9dbff and 232ddab.

📒 Files selected for processing (1)
  • ledger/common/gov.go (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
ledger/common/gov.go (3)
ledger/common/credentials.go (2)
  • CredentialTypeAddrKeyHash (27-27)
  • CredentialTypeScriptHash (28-28)
ledger/common/common.go (1)
  • PoolId (466-466)
ledger/compat.go (1)
  • PoolId (77-77)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Analyze (go)
🔇 Additional comments (3)
ledger/common/gov.go (3)

17-24: Imports correctly reflect new encoding behavior

fmt and bech32 are both used in the new encoding helper and there are no unused or layering issues with these additions.


43-65: CIP-129 header construction and Bech32 encoding look correct

The helper cleanly encapsulates CIP-129 encoding: it packs keyType into the high nibble and uses (credentialType + 2) for the low nibble, which, given CredentialTypeAddrKeyHash = 0 and CredentialTypeScriptHash = 1, yields the expected 0x2/0x3 tags. The data layout ([header || hash]), followed by 8→5 bit conversion and Bech32 encoding, is straightforward, and panicking on errors here is reasonable since all inputs are internal, fixed-size values and any failure would indicate a programming/configuration bug rather than bad user input.

If you haven’t already, please double‑check the current CIP‑129 text to confirm that key‑type and credential‑tag values (including the 0x2/0x3 low‑nibble mapping) still match these constants.


67-104: Voter.String mapping to CIP‑129 and PoolId is consistent and complete

The String() switch cleanly covers all existing voter types:

  • CC hot key/script both use the cc_hot HRP with key type 0 and low nibble derived from credential type.
  • DRep key/script both use the drep HRP with key type 2 and the same credential‑nibble scheme.
  • Staking pool voters delegate to PoolId(v.Hash).String(), reusing the established pool Bech32 representation.

The default panic for unknown v.Type matches the PR description/tests and gives an immediate signal if new voter types are added without updating the string encoding.


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.

❤️ Share

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

@arepala-uml arepala-uml marked this pull request as ready for review November 30, 2025 21:26
@arepala-uml arepala-uml requested a review from a team as a code owner November 30, 2025 21:26
Copy link
Contributor

@coderabbitai coderabbitai bot left a 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 (2)
ledger/common/gov_test.go (1)

171-231: Voter.String test coverage is solid; optional extension for non-zero CIP-129 hashes

The table-driven tests nicely cover all VoterType variants and the panic on unknown types, with clear vectors for zero hashes and a realistic pool ID via sequentialHash. If you want to harden this further, consider adding at least one non-zero hash case for a CIP-129 voter (e.g., DRep key/script) to ensure the header+payload encoding behaves as expected beyond the all-zero pattern, but this is purely optional given the current coverage.

ledger/common/gov.go (1)

43-74: CIP-129 header construction and Bech32 usage look correct; consider minor hardening

The header composition (keyType << 4) | (credentialType & 0x0f) and payload layout [header | hash...] match the intended “high nibble = key type, low nibble = credential type” scheme, and the Bech32 path (8→5 bit conversion with padding, then bech32.Encode) is appropriate for these identifiers. Two small, optional hardening tweaks you might consider:

  • Mask keyType as well, e.g. byte(((keyType & 0x0f) << 4) | (credentialType & 0x0f)), to guard against future changes accidentally using values > 0x0f.
  • Have encodeVoterBech32 return (string, error) and let callers decide how to handle unexpected encoding failures rather than panicking, if you anticipate this ever being called on untrusted/externally-sourced values. Right now the panics are effectively “should never happen” paths, which is also reasonable.
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1808850 and 2d53bca.

📒 Files selected for processing (2)
  • ledger/common/gov.go (2 hunks)
  • ledger/common/gov_test.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
ledger/common/gov_test.go (1)
ledger/common/gov.go (6)
  • Voter (37-41)
  • VoterTypeConstitutionalCommitteeHotKeyHash (30-30)
  • VoterTypeConstitutionalCommitteeHotScriptHash (31-31)
  • VoterTypeDRepKeyHash (32-32)
  • VoterTypeDRepScriptHash (33-33)
  • VoterTypeStakingPoolKeyHash (34-34)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: cubic · AI code reviewer
🔇 Additional comments (1)
ledger/common/gov.go (1)

76-113: Voter.String switch cleanly maps voter types to CIP-129 / pool identifiers

The String() implementation cleanly separates concerns:

  • CC hot and DRep voters are encoded via CIP-129 using appropriate HRPs ("cc_hot", "drep") and key/script credential types.
  • Staking pool voters correctly delegate to PoolId(v.Hash).String(), reusing the established pool Bech32 format.
  • The default case panics on unknown voter types, which, together with the test that asserts this behavior, makes it a good “fail fast on impossible state” guard for newly added enum values.

This looks consistent with the tests and the intended standards usage.

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

No issues found across 2 files

cip129KeyTypeDRep uint8 = 2
cip129CredentialTypeKeyHash uint8 = 0x02
cip129CredentialTypeScriptHash uint8 = 0x03
)
Copy link
Contributor

Choose a reason for hiding this comment

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

These values are the same as the existing constants at the top of this file, and the existing ones should be used instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed these new constants and used existing constants only. please review it.

credentialType uint8,
hash []byte,
) string {
header := byte((keyType << 4) | (credentialType & 0x0f))
Copy link
Contributor

Choose a reason for hiding this comment

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

This could use a comment about what's actually happening here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a comment here for what we are doing here. please review it.

return encodeVoterBech32(prefix, data)
}

func encodeVoterBech32(prefix string, data []byte) string {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function is only ever called by the function above and could easily be merged into it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I merged this function into the encodeCip129Voter function and please review it.

@arepala-uml arepala-uml requested a review from agaffney December 7, 2025 19:05
Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 1 file (changes from recent commits).

Prompt for AI agents (all 1 issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="ledger/common/gov.go">

<violation number="1" location="ledger/common/gov.go:50">
P3: Minor typo: missing space after comma in comment (`1,we` should be `1, we`).</violation>
</file>

Reply to cubic to teach it or ask questions. Re-run a review with @cubic-dev-ai review this PR

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
Signed-off-by: Chris Gianelloni <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

String method for Voter

4 participants