Skip to content

Commit

Permalink
feat: provide amount of gas for state migration in upgrade (#937)
Browse files Browse the repository at this point in the history
The PR's motivation is to add the possibility of setting the amount of
gas for the state migration in the `upgrade` transaction. This allows us
to upgrade Aurora via a controller contract. With the previous amount of
gas for the state migration (100 TGas), we can't do it because 200 Tgas
is insufficient for the upgrade procedure via a controller. If we need
more than 50 TGas for the state migration, we can call the `upgrade`
transaction directly.
  • Loading branch information
aleksuss authored Jul 22, 2024
1 parent f9d30b4 commit dae6444
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
9 changes: 9 additions & 0 deletions engine-types/src/parameters/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,15 @@ pub struct RelayerKeyArgs {

pub type FullAccessKeyArgs = RelayerKeyArgs;

/// Parameters for upgrading the contract.
#[derive(Debug, Clone, Eq, PartialEq, BorshSerialize, BorshDeserialize)]
pub struct UpgradeParams {
/// Code for upgrading.
pub code: Vec<u8>,
/// Amount of gas for the state migration.
pub state_migration_gas: Option<u64>,
}

mod chain_id_deserialize {
use crate::types::{u256_to_arr, RawU256};
use primitive_types::U256;
Expand Down
43 changes: 25 additions & 18 deletions engine/src/contract_methods/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use aurora_engine_sdk::{
io::{StorageIntermediate, IO},
promise::PromiseHandler,
};
use aurora_engine_types::parameters::engine::FullAccessKeyArgs;
use aurora_engine_types::parameters::engine::{FullAccessKeyArgs, UpgradeParams};
use aurora_engine_types::types::{NearGas, ZERO_YOCTO};
use aurora_engine_types::{
borsh::BorshDeserialize,
Expand All @@ -38,7 +38,7 @@ use aurora_engine_types::{
NewCallArgs, PausePrecompilesCallArgs, RelayerKeyArgs, RelayerKeyManagerArgs,
SetOwnerArgs, SetUpgradeDelayBlocksArgs, StartHashchainArgs,
},
promise::{PromiseAction, PromiseBatchAction, PromiseCreateArgs},
promise::{PromiseAction, PromiseBatchAction},
},
storage::{self, KeyPrefix},
types::{Address, Yocto},
Expand All @@ -48,7 +48,7 @@ use function_name::named;

const CODE_KEY: &[u8; 4] = b"CODE";
const CODE_STAGE_KEY: &[u8; 10] = b"CODE_STAGE";
const GAS_FOR_STATE_MIGRATION: NearGas = NearGas::new(100_000_000_000_000);
const GAS_FOR_STATE_MIGRATION: NearGas = NearGas::new(50_000_000_000_000);

#[named]
pub fn new<I: IO + Copy, E: Env>(mut io: I, env: &E) -> Result<(), ContractError> {
Expand Down Expand Up @@ -189,23 +189,30 @@ pub fn upgrade<I: IO + Copy, E: Env, H: PromiseHandler>(
require_running(&state)?;
require_owner_only(&state, &env.predecessor_account_id())?;

let code = io.read_input().to_vec();
let current_account_id = env.current_account_id();
let batch = PromiseBatchAction {
target_account_id: current_account_id.clone(),
actions: vec![PromiseAction::DeployContract { code }],
};
let state_migration_callback = PromiseCreateArgs {
target_account_id: current_account_id,
method: "state_migration".to_string(),
args: vec![],
attached_balance: ZERO_YOCTO,
attached_gas: GAS_FOR_STATE_MIGRATION,
let input = io.read_input().to_vec();
let (code, state_migration_gas) = match UpgradeParams::try_from_slice(&input) {
Ok(args) => (
args.code,
args.state_migration_gas
.map_or(GAS_FOR_STATE_MIGRATION, NearGas::new),
),
Err(_) => (input, GAS_FOR_STATE_MIGRATION), // Backward compatibility
};
let promise_id = unsafe {
let base_id = handler.promise_create_batch(&batch);
handler.promise_attach_callback(base_id, &state_migration_callback)

let target_account_id = env.current_account_id();
let batch = PromiseBatchAction {
target_account_id,
actions: vec![
PromiseAction::DeployContract { code },
PromiseAction::FunctionCall {
name: "state_migration".to_string(),
args: vec![],
attached_yocto: ZERO_YOCTO,
gas: state_migration_gas,
},
],
};
let promise_id = unsafe { handler.promise_create_batch(&batch) };

handler.promise_return(promise_id);

Expand Down

0 comments on commit dae6444

Please sign in to comment.