Trivela contracts keep state at stable contract IDs and evolve logic through Soroban Wasm
upgrades.
To make storage evolution explicit and reviewable, each contract now exposes:
schema_version() -> u32migrate(admin, target_version) -> Result<u32, Error>
Both campaign and rewards contracts currently use schema version 1.
- Schema is explicit
initializepersistsschema_v = 1. - Admin-gated migration
migraterequires admin auth and validates supported target versions. - Idempotent current migration
Callingmigrate(..., 1)is safe and returns1. - Forward-safe failure mode
Unsupported versions returnUnsupportedMigrationinstead of mutating state.
This makes migration behavior deterministic for CI and rollout scripts.
The campaign contract previously stored per-participant registration records in instance storage
under (PARTICIPANT, participant). Soroban instance storage shares a single ~64KB envelope with the
contract code, so a viral campaign would silently brick somewhere around 1.8k participants (each
Address is ~35 bytes in XDR).
As of this release, participant records live in persistent storage:
// register()
env.storage().persistent().set(&key, &true);
env.storage().persistent().extend_ttl(&key, threshold, extend_to);
// is_participant()
env.storage().persistent().get(&key)
// do_deregister()
env.storage().persistent().remove(&key);PARTICIPANT_COUNT (the aggregate) stays in instance storage — it's a single counter, not per-user,
and pulling it out doesn't help.
A campaign contract deployed under schema v1 (instance-storage participants) is not
auto-migrated by migrate(admin, 2) — moving keys across storage tiers requires reading from one
tier and writing to the other, which we can only do safely if the registry of participants is known.
The recommended procedure for a deployer:
- Drain the campaign (set inactive via
set_active(admin, n, false)). - Read every registered address via your indexer / event log (
registerevents are public). - Deploy the new Wasm and
upgrade --new_wasm_hash <HASH>. - Call
register(...)for each historical participant — the new call writes to persistent storage. Idempotency is guaranteed because the contract returnsfalsefor already-registered participants. - Re-activate via
set_active(admin, n+1, true).
For new campaigns deployed after this release, no migration is needed — the first register call
already targets persistent.
Two constants in contracts/campaign/src/lib.rs control how aggressively each registration
refreshes its key's TTL:
const PARTICIPANT_TTL_THRESHOLD: u32 = 100;
const PARTICIPANT_TTL_EXTEND_TO: u32 = 500;These are deliberately modest for the initial rollout. Production deployers running long campaigns should tune them in a follow-up contract release — moving them out into per-campaign admin-settable storage is appropriate once traffic patterns are known.
- Build and upload new Wasm:
stellar contract build
stellar contract install --wasm <updated_contract.wasm> --source <admin> --network testnet- Upgrade contract code (admin path):
stellar contract invoke --id <CONTRACT_ID> --source <admin> --network testnet -- \
upgrade --new_wasm_hash <WASM_HASH>- Run migration hook:
stellar contract invoke --id <CONTRACT_ID> --source <admin> --network testnet -- \
migrate --target_version <VERSION>- Verify:
stellar contract invoke --id <CONTRACT_ID> --source <admin> --network testnet -- \
schema_version- Never repurpose a key with incompatible value type.
- Introduce new keys for new fields; preserve old keys until migrated.
- Keep
migratepure/idempotent per target version. - Add test coverage for:
- initial schema version
- successful migrate at current version
- unsupported version rejection
- unauthorized caller rejection
Who can trigger an upgrade. campaign::upgrade(admin, nonce, new_wasm_hash) requires the
contract admin's authorization and the admin's current nonce (require_admin_with_nonce), so a
signed upgrade cannot be replayed. No other account can call it. The campaign contract applies the
new Wasm immediately: there is no on-chain delay between the call and the new code running. Any
time-lock today is operational (an announced window and a multisig admin), not enforced by the
contract.
Verifying the new Wasm before an upgrade.
- Build the release Wasm from the tagged commit and compute its hash:
sha256sum target/wasm32-unknown-unknown/release/<contract>.wasm. - Upload it (
stellar contract upload) and confirm the returned hash equals the one you computed. Reviewers should reproduce the build independently and compare hashes. - Pass exactly that hash as
new_wasm_hashtoupgrade. - After the call, read
schema_version()and runmigrate(admin, target_version)only if the storage layout changed.
See also the mainnet checklist and formal verification.
Today, direct admin upgrades are enough for a small contributor project. As Trivela grows, we can move to a stricter deployer pattern so contract IDs remain stable while upgrade authority is easier to rotate and audit.
- Deployer/Proxy admin contract: route upgrades through a dedicated deployer contract (or a governance-controlled admin account) instead of a long-lived individual key.
- Two-step rollout: install new Wasm, run
upgrade, then runmigratewith explicit target versions and smoke checks between each step. - Migration ledgering: persist migration checkpoints/events so off-chain monitors can verify exactly which schema version is live.
- Rollback readiness: keep previous Wasm hashes and migration runbooks ready to redeploy quickly if a release introduces regressions.