Skip to content

Fix Version Marker Pattern test examples to use proper contract context#2300

Closed
Copilot wants to merge 2 commits intomigrate-contract-storagefrom
copilot/sub-pr-2299
Closed

Fix Version Marker Pattern test examples to use proper contract context#2300
Copilot wants to merge 2 commits intomigrate-contract-storagefrom
copilot/sub-pr-2299

Conversation

Copy link
Contributor

Copilot AI commented Mar 6, 2026

The test examples in the Version Marker Pattern section accessed env.storage() directly from Env::default() without a contract context, which panics at runtime. All storage reads/writes and helper calls must occur inside env.as_contract(...).

Changes

  • Each test now registers a contract via env.register(Contract, ()) to obtain a contract_id
  • All storage and helper calls wrapped in env.as_contract(&contract_id, || { ... }) — including legacy v1 state injection, read_data, write_data, and version key reads
  • Prose updated to explain why env.register + env.as_contract are required
#[test]
fn test_reads_v1_entry_as_v2() {
    let env = Env::default();
    let contract_id = env.register(Contract, ());
    let id: u32 = 42;

    env.as_contract(&contract_id, || {
        let v1_data = DataV1 { a: 10, b: 20 };
        env.storage().persistent().set(&DataKey::Data(id), &v1_data);
    });

    let result = env.as_contract(&contract_id, || read_data(&env, id));

    assert_eq!(result.a, 10);
    assert_eq!(result.b, 20);
    assert_eq!(result.c, None);
}

Pattern now matches the Versioned Enum Pattern tests already present in the same guide.


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Co-authored-by: carstenjacobsen <4585552+carstenjacobsen@users.noreply.github.com>
Copilot AI changed the title [WIP] Address feedback on how-to guide for storage data migration Fix Version Marker Pattern test examples to use proper contract context Mar 6, 2026
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.

2 participants