Skip to content

Commit

Permalink
handle argument gas cost increasing (#1)
Browse files Browse the repository at this point in the history
Co-authored-by: Marco <[email protected]>
  • Loading branch information
marco-sundsk and Marco authored Sep 3, 2024
1 parent b70296a commit 00807fa
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 9 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion sputnikdao-factory2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sputnikdao-factory2"
version = "0.2.0"
version = "0.2.1"
authors = ["Illia Polosukhin <[email protected]>"]
edition = "2018"
publish = false
Expand Down
Binary file modified sputnikdao-factory2/res/sputnikdao_factory2.wasm
Binary file not shown.
Binary file not shown.
7 changes: 6 additions & 1 deletion sputnikdao-factory2/src/factory_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const ON_CREATE_CALL_GAS: Gas = Gas(10_000_000_000_000);
/// Leftover gas after creating promise and calling update.
const GAS_UPDATE_LEFTOVER: Gas = Gas(10_000_000_000_000);

/// Since Nightshade V2, the send_not_sir of action_function_call_per_byte increase to this value, please refer to:
/// https://github.com/near/nearcore/blob/0c2374993fc74b57faf2bcdf5c7c73a37e82b75a/core/parameters/res/runtime_configs/parameters.snap#L52
pub const GAS_FUNCTION_CALL_PER_BYTE: u64 = 47_683_715;

const NO_DEPOSIT: Balance = 0;

/// Factory manager that allows to store/load contracts by hash directly in the storage.
Expand Down Expand Up @@ -68,6 +72,7 @@ impl FactoryManager {
assert!(env::storage_has_key(&code_hash), "Contract doesn't exist");
// Load the hash from storage.
let code = env::storage_read(&code_hash).expect("ERR_NO_HASH");
let wasm_argument_gas = Gas(code.len() as u64 * GAS_FUNCTION_CALL_PER_BYTE);
// Create a promise toward given account.
let promise_id = env::promise_batch_create(&account_id);
// Call `update` method, which should also handle migrations.
Expand All @@ -76,7 +81,7 @@ impl FactoryManager {
method_name,
&code,
NO_DEPOSIT,
env::prepaid_gas() - env::used_gas() - GAS_UPDATE_LEFTOVER,
env::prepaid_gas() - env::used_gas() - GAS_UPDATE_LEFTOVER - wasm_argument_gas,
);
env::promise_return(promise_id);
}
Expand Down
2 changes: 1 addition & 1 deletion sputnikdao2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sputnikdao2"
version = "2.0.0"
version = "2.3.1"
authors = ["Sputnik Devs <[email protected]>"]
edition = "2018"
publish = false
Expand Down
Binary file modified sputnikdao2/res/sputnikdao2.wasm
Binary file not shown.
Binary file added sputnikdao2/res/sputnikdao2_v_3_0.wasm
Binary file not shown.
12 changes: 8 additions & 4 deletions sputnikdao2/src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ const UPDATE_GAS_LEFTOVER: Gas = Gas(10_000_000_000_000);
const FACTORY_UPDATE_GAS_LEFTOVER: Gas = Gas(15_000_000_000_000);
const NO_DEPOSIT: Balance = 0;

pub const GAS_FOR_UPGRADE_SELF_DEPLOY: Gas = Gas(15_000_000_000_000);
pub const GAS_FOR_UPGRADE_REMOTE_DEPLOY: Gas = Gas(15_000_000_000_000);
pub const GAS_FOR_UPGRADE_SELF_PROMISE_CREATION: Gas = Gas(15_000_000_000_000);
pub const GAS_FOR_UPGRADE_REMOTE_PROMISE_CREATION: Gas = Gas(15_000_000_000_000);
/// Since Nightshade V2, the send_not_sir of action_function_call_per_byte increase to this value, please refer to:
/// https://github.com/near/nearcore/blob/0c2374993fc74b57faf2bcdf5c7c73a37e82b75a/core/parameters/res/runtime_configs/parameters.snap#L52
pub const GAS_FUNCTION_CALL_PER_BYTE: u64 = 47_683_715;

/// Info about factory that deployed this contract and if auto-update is allowed.
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize)]
Expand Down Expand Up @@ -123,18 +126,19 @@ pub(crate) fn upgrade_self(hash: &[u8]) {
"migrate",
&[],
NO_DEPOSIT,
env::prepaid_gas() - env::used_gas() - GAS_FOR_UPGRADE_SELF_DEPLOY,
env::prepaid_gas() - env::used_gas() - GAS_FOR_UPGRADE_SELF_PROMISE_CREATION,
);
}

pub(crate) fn upgrade_remote(receiver_id: &AccountId, method_name: &str, hash: &[u8]) {
let input = env::storage_read(hash).expect("ERR_NO_HASH");
let promise_id = env::promise_batch_create(receiver_id);
let wasm_argument_gas = Gas(input.len() as u64 * GAS_FUNCTION_CALL_PER_BYTE);
env::promise_batch_action_function_call(
promise_id,
method_name,
&input,
NO_DEPOSIT,
env::prepaid_gas() - env::used_gas() - GAS_FOR_UPGRADE_REMOTE_DEPLOY,
env::prepaid_gas() - env::used_gas() - GAS_FOR_UPGRADE_REMOTE_PROMISE_CREATION - wasm_argument_gas,
);
}

0 comments on commit 00807fa

Please sign in to comment.