From 6ecb6af3611dd5ea142103ba65aac5b3069cb666 Mon Sep 17 00:00:00 2001 From: Saeed Dadkhah Date: Thu, 16 Jan 2025 16:20:36 +0330 Subject: [PATCH] feat: add option to adjust contract balance on deployment if already funded (#2155) * feat: add option to adjust contract balance on deployment if already funded * fix clippy * Update zilliqa/src/cfg.rs Co-authored-by: James Hinshelwood * Update configs * update configs. --------- Co-authored-by: James Hinshelwood --- z2/resources/chain-specs/zq2-protomainnet.toml | 5 ++++- z2/resources/chain-specs/zq2-prototestnet.toml | 6 +++--- z2/src/chain.rs | 10 +++++----- zilliqa/src/cfg.rs | 6 ++++++ zilliqa/src/exec.rs | 8 ++++++++ 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/z2/resources/chain-specs/zq2-protomainnet.toml b/z2/resources/chain-specs/zq2-protomainnet.toml index 7a0079ca4..d79a709e4 100644 --- a/z2/resources/chain-specs/zq2-protomainnet.toml +++ b/z2/resources/chain-specs/zq2-protomainnet.toml @@ -19,4 +19,7 @@ consensus.scilla_call_gas_exempt_addrs = ["0x95347b860Bd49818AFAccCA8403C55C23e7 consensus.contract_upgrade_block_heights = { deposit_v3 = 5342400 } api_servers = [{ port = 4201, enabled_apis = [{ apis = ["blockNumber"], namespace = "eth" }] }, { enabled_apis = ["admin", "erigon", "eth", "net", "ots", "trace", "txpool", "web3", "zilliqa"], port = 4202 }] -consensus.forks = [{ at_height = 0, call_mode_1_sets_caller_to_parent_caller = false, failed_scilla_call_from_gas_exempt_caller_causes_revert = false }, { at_height = 5342400, call_mode_1_sets_caller_to_parent_caller = true, failed_scilla_call_from_gas_exempt_caller_causes_revert = true }] +consensus.forks = [ + { at_height = 0, call_mode_1_sets_caller_to_parent_caller = false, failed_scilla_call_from_gas_exempt_caller_causes_revert = false, scilla_messages_can_call_evm_contracts = false, scilla_contract_creation_increments_account_balance = false }, + { at_height = 5342400, call_mode_1_sets_caller_to_parent_caller = true, failed_scilla_call_from_gas_exempt_caller_causes_revert = true, scilla_messages_can_call_evm_contracts = false, scilla_contract_creation_increments_account_balance = false } +] diff --git a/z2/resources/chain-specs/zq2-prototestnet.toml b/z2/resources/chain-specs/zq2-prototestnet.toml index 8be3d56e1..a2609b649 100644 --- a/z2/resources/chain-specs/zq2-prototestnet.toml +++ b/z2/resources/chain-specs/zq2-prototestnet.toml @@ -20,7 +20,7 @@ consensus.contract_upgrade_block_heights = { deposit_v3 = 8406000 } api_servers = [{ port = 4201, enabled_apis = [{ apis = ["blockNumber"], namespace = "eth" }] }, { enabled_apis = ["admin", "erigon", "eth", "net", "ots", "trace", "txpool", "web3", "zilliqa"], port = 4202 }] consensus.forks = [ - { at_height = 0, call_mode_1_sets_caller_to_parent_caller = false, failed_scilla_call_from_gas_exempt_caller_causes_revert = false, scilla_messages_can_call_evm_contracts = false }, - { at_height = 8404000, call_mode_1_sets_caller_to_parent_caller = true, failed_scilla_call_from_gas_exempt_caller_causes_revert = true, scilla_messages_can_call_evm_contracts = false }, - { at_height = 10200000, call_mode_1_sets_caller_to_parent_caller = true, failed_scilla_call_from_gas_exempt_caller_causes_revert = true, scilla_messages_can_call_evm_contracts = true } + { at_height = 0, call_mode_1_sets_caller_to_parent_caller = false, failed_scilla_call_from_gas_exempt_caller_causes_revert = false, scilla_messages_can_call_evm_contracts = false, scilla_contract_creation_increments_account_balance = false }, + { at_height = 8404000, call_mode_1_sets_caller_to_parent_caller = true, failed_scilla_call_from_gas_exempt_caller_causes_revert = true, scilla_messages_can_call_evm_contracts = false, scilla_contract_creation_increments_account_balance = false }, + { at_height = 10200000, call_mode_1_sets_caller_to_parent_caller = true, failed_scilla_call_from_gas_exempt_caller_causes_revert = true, scilla_messages_can_call_evm_contracts = true, scilla_contract_creation_increments_account_balance = false } ] diff --git a/z2/src/chain.rs b/z2/src/chain.rs index fbe1be055..413cfcf7c 100644 --- a/z2/src/chain.rs +++ b/z2/src/chain.rs @@ -182,16 +182,16 @@ impl Chain { pub fn get_forks(&self) -> Option> { match self { Chain::Zq2ProtoTestnet => Some(vec![ - json!({ "at_height": 0, "failed_scilla_call_from_gas_exempt_caller_causes_revert": false, "call_mode_1_sets_caller_to_parent_caller": false, "scilla_messages_can_call_evm_contracts": false }), + json!({ "at_height": 0, "failed_scilla_call_from_gas_exempt_caller_causes_revert": false, "call_mode_1_sets_caller_to_parent_caller": false, "scilla_messages_can_call_evm_contracts": false, "scilla_contract_creation_increments_account_balance": false }), // estimated: 2024-12-18T14:57:53Z - json!({ "at_height": 8404000, "failed_scilla_call_from_gas_exempt_caller_causes_revert": true, "call_mode_1_sets_caller_to_parent_caller": true, "scilla_messages_can_call_evm_contracts": false }), + json!({ "at_height": 8404000, "failed_scilla_call_from_gas_exempt_caller_causes_revert": true, "call_mode_1_sets_caller_to_parent_caller": true, "scilla_messages_can_call_evm_contracts": false, "scilla_contract_creation_increments_account_balance": false }), // estimated: 2025-01-15T09:10:37Z - json!({ "at_height": 10200000, "failed_scilla_call_from_gas_exempt_caller_causes_revert": true, "call_mode_1_sets_caller_to_parent_caller": true, "scilla_messages_can_call_evm_contracts": true }), + json!({ "at_height": 10200000, "failed_scilla_call_from_gas_exempt_caller_causes_revert": true, "call_mode_1_sets_caller_to_parent_caller": true, "scilla_messages_can_call_evm_contracts": true, "scilla_contract_creation_increments_account_balance": false }), ]), Chain::Zq2ProtoMainnet => Some(vec![ - json!({ "at_height": 0, "failed_scilla_call_from_gas_exempt_caller_causes_revert": false, "call_mode_1_sets_caller_to_parent_caller": false, "scilla_messages_can_call_evm_contracts": false }), + json!({ "at_height": 0, "failed_scilla_call_from_gas_exempt_caller_causes_revert": false, "call_mode_1_sets_caller_to_parent_caller": false, "scilla_messages_can_call_evm_contracts": false, "scilla_contract_creation_increments_account_balance": false }), // estimated: 2024-12-20T23:33:12Z - json!({ "at_height": 5342400, "failed_scilla_call_from_gas_exempt_caller_causes_revert": true, "call_mode_1_sets_caller_to_parent_caller": true, "scilla_messages_can_call_evm_contracts": false }), + json!({ "at_height": 5342400, "failed_scilla_call_from_gas_exempt_caller_causes_revert": true, "call_mode_1_sets_caller_to_parent_caller": true, "scilla_messages_can_call_evm_contracts": false, "scilla_contract_creation_increments_account_balance": false }), ]), _ => None, } diff --git a/zilliqa/src/cfg.rs b/zilliqa/src/cfg.rs index c751d99ec..4e6f9f5d8 100644 --- a/zilliqa/src/cfg.rs +++ b/zilliqa/src/cfg.rs @@ -432,6 +432,7 @@ impl Default for Forks { failed_scilla_call_from_gas_exempt_caller_causes_revert: true, call_mode_1_sets_caller_to_parent_caller: true, scilla_messages_can_call_evm_contracts: true, + scilla_contract_creation_increments_account_balance: true, }] .try_into() .unwrap() @@ -473,6 +474,11 @@ pub struct Fork { /// EOA (i.e. any ZIL passed will be transferred to the contract and execution will continue). If false, sending a /// Scilla message to an EVM contract will cause the Scilla transaction to fail. pub scilla_messages_can_call_evm_contracts: bool, + /// If true, if a Scilla contract is deployed to an address with a non-zero balance, the contract balance will be equal + /// to the account's existing balance plus the amount sent in the deployment transaction. If false, the contract + /// balance will be equal to the amount sent in the deployment transaction. The account's existing balance is wiped + /// out (meaning the total supply of the network is not preserved). + pub scilla_contract_creation_increments_account_balance: bool, } #[derive(Debug, Clone, Serialize, Deserialize)] diff --git a/zilliqa/src/exec.rs b/zilliqa/src/exec.rs index 93655b64a..7ba7c4e47 100644 --- a/zilliqa/src/exec.rs +++ b/zilliqa/src/exec.rs @@ -659,6 +659,7 @@ impl State { current_block, inspector, &self.scilla_ext_libs_path, + self.forks.get(current_block.number), ) } else { scilla_call( @@ -1518,6 +1519,7 @@ pub fn store_external_libraries( Ok((ext_libs_dir_in_zq2, ext_libs_dir_in_scilla)) } +#[allow(clippy::too_many_arguments)] fn scilla_create( mut state: PendingState, scilla: MutexGuard<'_, Scilla>, @@ -1526,6 +1528,7 @@ fn scilla_create( current_block: BlockHeader, mut inspector: impl ScillaInspector, scilla_ext_libs_path: &ScillaExtLibsPath, + fork: Fork, ) -> Result<(ScillaResult, PendingState)> { if txn.data.is_empty() { return Err(anyhow!("contract creation without init data")); @@ -1628,6 +1631,11 @@ fn scilla_create( let transitions = contract_info.transitions; let account = state.load_account(contract_address)?; + if fork.scilla_contract_creation_increments_account_balance { + account.account.balance += txn.amount.get(); + } else { + account.account.balance = txn.amount.get(); + } account.account.balance = txn.amount.get(); account.account.code = Code::Scilla { code: txn.code.clone(),