Skip to content

task: add collision rejection to market id generation#568

Merged
greatest0fallt1me merged 2 commits into
Predictify-org:masterfrom
michaelvic123:task/market-id-collision-guard
May 27, 2026
Merged

task: add collision rejection to market id generation#568
greatest0fallt1me merged 2 commits into
Predictify-org:masterfrom
michaelvic123:task/market-id-collision-guard

Conversation

@michaelvic123

@michaelvic123 michaelvic123 commented May 27, 2026

Copy link
Copy Markdown
Contributor

Closes #549


Problem

build_market_id hashed only ledger_sequence and global_nonce. The admin address appeared only in the counter suffix, not in the hash input. Two admins calling generate_market_id with the same sequence and nonce could produce the same 8-char hex prefix and collide if their counters also matched.

Additionally, admin.clone().to_xdr(env) requires use soroban_sdk::xdr::ToXdr to be in scope — without it the code does not compile.

Changes

File What changed
market_id_generator.rs Added use soroban_sdk::xdr::ToXdr import
market_id_generator.rs build_market_id now takes admin: &Address and appends admin.to_xdr(env) bytes to the SHA-256 input after sequence+nonce
market_id_generator.rs generate_market_id passes admin through to build_market_id
market_id_generator.rs Module doc updated: entropy table now lists admin address as third input
market_id_generator.rs Two new tests added (see below)

New tests

  • test_same_ledger_same_nonce_different_admins_produce_different_ids — resets the global nonce to 0 between two calls to isolate the admin-address contribution; proves the fix works in the worst case
  • test_forced_registry_collision_triggers_retry — pre-populates storage with the first candidate ID, then verifies the generator skips it and returns a distinct, format-valid ID

Acceptance criteria

  • Generation never overwrites an existing registry entry (retry loop unchanged)
  • Collisions trigger retry then InvalidState error after MAX_RETRIES
  • Generated IDs always pass validate_market_id_format
  • Admin address is now part of the hash seed, not just the suffix

Fold admin address into the SHA-256 seed so two admins calling
generate_market_id with the same ledger sequence and global nonce
produce different IDs. Previously the admin was only in the counter
suffix, not the hash input.

Changes:
- build_market_id: append admin.to_xdr() bytes after sequence+nonce
  before hashing; update doc comment with new layout table
- generate_market_id: pass admin ref through to build_market_id
- Module doc: update entropy table to list admin address as third input

Tests added:
- test_same_ledger_same_nonce_different_admins_produce_different_ids:
  resets the global nonce between two calls to prove the admin address
  alone differentiates the hash
- test_forced_registry_collision_triggers_retry: pre-populates storage
  with the first candidate ID and verifies the generator skips it and
  returns a distinct, format-valid ID
admin.clone().to_xdr(env) requires the ToXdr trait to be in scope.
Without the import the call fails to compile with 'method not found'.
Pattern matches audit_trail.rs which uses the same trait inline.
@drips-wave

drips-wave Bot commented May 27, 2026

Copy link
Copy Markdown

@michaelvic123 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@greatest0fallt1me greatest0fallt1me merged commit dc701db into Predictify-org:master May 27, 2026
1 check failed
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.

Strengthen MarketIdGenerator entropy source and add registry-backed collision rejection

2 participants