-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(upgrader): add generalized atomic upgrade and migration capabili…
…ties
- Loading branch information
Showing
10 changed files
with
151 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,4 +23,5 @@ pub enum ContractError { | |
EmptyMessages = 14, | ||
// Executable | ||
NotApproved = 15, | ||
MigrationAlreadyCompleted = 16, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "upgrader" | ||
version = "0.1.0" | ||
edition = { workspace = true } | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[dependencies] | ||
soroban-sdk = { workspace = true } | ||
axelar-gateway = { workspace = true } | ||
|
||
[dev-dependencies] | ||
soroban-sdk = { workspace = true, features = ["testutils"] } | ||
|
||
[features] | ||
|
||
[lints] | ||
workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use crate::error::ContractError; | ||
use soroban_sdk::{contract, contractimpl, Address, BytesN, Env, String, Symbol, Val}; | ||
|
||
#[contract] | ||
pub struct Upgrader; | ||
|
||
#[contractimpl] | ||
impl Upgrader { | ||
pub fn __constructor(_env: Env) {} | ||
pub fn upgrade(env: Env, contract_address: Address, new_version: String, new_wasm_hash: BytesN<32>, migration_data: soroban_sdk::Vec<Val>) -> Result<(), ContractError> { | ||
assert_new_version_is_different(&env, &contract_address, &new_version)?; | ||
|
||
env.invoke_contract::<()>(&contract_address, &upgrade_fn(&env), soroban_sdk::vec![&env, new_wasm_hash.into()]); | ||
env.invoke_contract::<()>(&contract_address, &migrate_fn(&env), migration_data); | ||
|
||
assert_new_version_matches_expected(&env, &contract_address, &new_version) | ||
} | ||
} | ||
|
||
fn migrate_fn(env: &Env) -> Symbol { | ||
Symbol::new(&env, "migrate") | ||
} | ||
|
||
fn upgrade_fn(env: &Env) -> Symbol { | ||
Symbol::new(&env, "upgrade") | ||
} | ||
|
||
fn version_fn(env: &Env) -> Symbol { | ||
Symbol::new(&env, "version") | ||
} | ||
|
||
fn assert_new_version_is_different(env: &Env, contract_address: &Address, new_version: &String) -> Result<(), ContractError> { | ||
let cur_version: String = env.invoke_contract(&contract_address, &version_fn(&env), soroban_sdk::vec![&env]); | ||
|
||
if cur_version != *new_version { | ||
Ok(()) | ||
} else { | ||
Err(ContractError::SameVersion) | ||
} | ||
} | ||
|
||
fn assert_new_version_matches_expected(env: &Env, contract_address: &Address, new_version: &String) -> Result<(), ContractError> { | ||
let cur_version: String = env.invoke_contract(&contract_address, &version_fn(&env), soroban_sdk::vec![&env]); | ||
|
||
if cur_version == *new_version { | ||
Ok(()) | ||
} else { | ||
Err(ContractError::VersionMismatch) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::contract::{Upgrader, UpgraderClient}; | ||
use axelar_gateway::AxelarGateway; | ||
use soroban_sdk::{BytesN, Env, String}; | ||
|
||
const _WASM: &[u8] = include_bytes!("testdata/axelar_gateway.wasm"); | ||
#[test] | ||
fn test() { | ||
let env = Env::default(); | ||
|
||
let contract_id = env.register(Upgrader, ()); | ||
let gateway_id = env.register(AxelarGateway, ()); | ||
let client = UpgraderClient::new(&env, &contract_id); | ||
client.upgrade(&gateway_id, &String::from_str(&env, "1.0"), &BytesN::from_array(&env, &[0; 32]), &soroban_sdk::vec![&env]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use soroban_sdk::contracterror; | ||
|
||
#[contracterror] | ||
#[repr(u32)] | ||
pub enum ContractError { | ||
SameVersion = 1, | ||
VersionMismatch = 2, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#![no_std] | ||
|
||
mod contract; | ||
mod error; |
Binary file not shown.