Skip to content

Commit

Permalink
refactor(upgrader): use contractstorage (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbayindirli authored Feb 7, 2025
1 parent a1ac57a commit 71a8cae
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions contracts/stellar-upgrader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ soroban-sdk = { workspace = true }
stellar-axelar-std = { workspace = true }

[dev-dependencies]
goldie = { workspace = true }
soroban-sdk = { workspace = true, features = ["testutils"] }

[features]
Expand Down
4 changes: 4 additions & 0 deletions contracts/stellar-upgrader/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#![no_std]

#[cfg(any(test, feature = "testutils"))]
extern crate std;

#[cfg(test)]
extern crate alloc;

Expand Down
5 changes: 3 additions & 2 deletions contracts/stellar-upgrader/src/tests/atomic_upgrades.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use soroban_sdk::testutils::Address as _;
use soroban_sdk::{Address, BytesN, Env, String};
use stellar_axelar_std::{assert_contract_err, mock_auth};

use super::utils::{DataKey, DummyContract, DummyContractClient};
use super::utils::{DummyContract, DummyContractClient};
use crate::error::ContractError;
use crate::tests::utils;
use crate::{Upgrader, UpgraderClient};

const WASM_AFTER_UPGRADE: &[u8] = include_bytes!("testdata/dummy.wasm");
Expand Down Expand Up @@ -42,7 +43,7 @@ fn upgrade_and_migrate_are_atomic() {

// ensure migration was successful
env.as_contract(&contract_address, || {
let data: String = env.storage().instance().get(&DataKey::Data).unwrap();
let data = utils::storage::data(&env);
assert_eq!(data, expected_data);
});
}
Expand Down
18 changes: 12 additions & 6 deletions contracts/stellar-upgrader/src/tests/utils/dummy_contract.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Dummy contract to test the [crate::Upgrader]
use soroban_sdk::{contract, contracterror, contractimpl, contracttype, Address, BytesN, Env};
use stellar_axelar_std::interfaces;
use stellar_axelar_std::interfaces::{OwnableInterface, UpgradableInterface};
use stellar_axelar_std::{contractstorage, interfaces};

#[contract]
pub struct DummyContract;
Expand Down Expand Up @@ -38,12 +38,18 @@ impl DummyContract {
}
}

#[contracttype]
pub enum DataKey {
Data,
}

#[contracterror]
pub enum ContractError {
SomeFailure = 1,
}

pub mod storage {
use super::*;

#[contractstorage]
pub enum DataKey {
#[instance]
#[value(soroban_sdk::String)]
Data,
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Base for the dummy.wasm file. This is the dummy contract after upgrade.
use soroban_sdk::{contract, contracterror, contractimpl, contracttype, Address, BytesN, Env};
use stellar_axelar_std::interfaces;
use stellar_axelar_std::interfaces::{OwnableInterface, UpgradableInterface};
use stellar_axelar_std::{contractstorage, interfaces};

#[contract]
pub struct DummyContract;
Expand Down Expand Up @@ -39,19 +39,24 @@ impl DummyContract {

pub fn migrate(env: Env, migration_data: soroban_sdk::String) -> Result<(), ContractError> {
Self::owner(&env).require_auth();
env.storage()
.instance()
.set(&DataKey::Data, &migration_data);
storage::set_data(&env, &migration_data);

Ok(())
}
}

#[contracttype]
pub enum DataKey {
Data,
}

#[contracterror]
pub enum ContractError {
SomeFailure = 1,
}

mod storage {
use super::*;

#[contractstorage]
pub enum DataKey {
#[instance]
#[value(soroban_sdk::String)]
Data,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub enum DataKey {

#[instance]
#[value(soroban_sdk::String)]
Data,
}

0 comments on commit 71a8cae

Please sign in to comment.