Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix tests update comments
  • Loading branch information
ascjones committed Jan 31, 2024
commit 76c4fc14a5981b294630bde88da9d5612490e1bb
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ async fn migration_works<Client: E2EBackend>(mut client: Client) -> E2EResult<()
.submit()
.await
.expect("instantiate failed");
let mut call = contract.call::<Incrementer>();
let mut call_builder = contract.call_builder::<Incrementer>();

let get = call.get();
let get_res = client.call(&ink_e2e::alice(), &get).dry_run().await;
let get = call_builder.get();
let get_res = client.call(&ink_e2e::alice(), &get).dry_run().await?;
assert_eq!(get_res.return_value(), 0);

let inc = call.inc();
let inc = call_builder.inc();
let _inc_result = client
.call(&ink_e2e::alice(), &inc)
.submit()
.await
.expect("`inc` failed");

let get = call.get();
let get_res = client.call(&ink_e2e::alice(), &get).dry_run().await;
let get = call_builder.get();
let get_res = client.call(&ink_e2e::alice(), &get).dry_run().await?;
let pre_migration_value = get_res.return_value();
assert_eq!(pre_migration_value, 1);

Expand All @@ -50,7 +50,7 @@ async fn migration_works<Client: E2EBackend>(mut client: Client) -> E2EResult<()
// When

// Set the code hash to the migration contract
let set_code = call.set_code(migration_code_hash);
let set_code = call_builder.set_code(migration_code_hash);
let _set_code_result = client
.call(&ink_e2e::alice(), &set_code)
.submit()
Expand All @@ -61,7 +61,7 @@ async fn migration_works<Client: E2EBackend>(mut client: Client) -> E2EResult<()
// of the updated contract.
const NEW_INC_BY: u8 = 4;
let migrate = contract
.call::<migration::incrementer::Incrementer>()
.call_builder::<migration::incrementer::Incrementer>()
.migrate(NEW_INC_BY, new_code_hash);

let _migration_result = client
Expand All @@ -72,7 +72,7 @@ async fn migration_works<Client: E2EBackend>(mut client: Client) -> E2EResult<()

// Then
let inc = contract
.call::<updated_incrementer::incrementer::Incrementer>()
.call_builder::<updated_incrementer::incrementer::Incrementer>()
.inc();

let _inc_result = client
Expand All @@ -81,8 +81,8 @@ async fn migration_works<Client: E2EBackend>(mut client: Client) -> E2EResult<()
.await
.expect("`inc` failed");

let get = call.get();
let get_res = client.call(&ink_e2e::alice(), &get).dry_run().await;
let get = call_builder.get();
let get_res = client.call(&ink_e2e::alice(), &get).dry_run().await?;

// Remember, we updated our incrementer contract to increment by `4`.
assert_eq!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,22 @@ pub mod incrementer {
/// # Note
///
/// This function necessarily accepts a `&self` instead of a `&mut self` because
/// we are modifying storage directly for the migration. Using `&mut self`
/// would overwrite our migration changes with the contents of the
/// original `Incrementer`.
/// we are modifying storage directly for the migration.
///
/// The `self` in `&mut self` is the original `Incrementer` storage struct, and
/// would be implicitly written to storage following the function execution,
/// overwriting the migrated storage.
#[ink(message)]
pub fn migrate(&self, inc_by: u8, code_hash: Hash) {
let incrementer_new = IncrementerNew {
count: self.count as u64,
inc_by,
};
const STORAGE_KEY: u32 = 0x00000000;

// overwrite the original storage struct with the migrated storage struct,
// which has a layout compatible with the new contract code.
const STORAGE_KEY: u32 =
<Incrementer as ink::storage::traits::StorageKey>::KEY;
ink::env::set_contract_storage(&STORAGE_KEY, &incrementer_new);

ink::env::set_code_hash::<<Self as ink::env::ContractEnv>::Env>(&code_hash)
Expand Down