Skip to content

Commit

Permalink
feat: add option to adjust contract balance on deployment if already …
Browse files Browse the repository at this point in the history
…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 <[email protected]>

* Update configs

* update configs.

---------

Co-authored-by: James Hinshelwood <[email protected]>
  • Loading branch information
saeed-zil and JamesHinshelwood authored Jan 16, 2025
1 parent 1e97af8 commit 6ecb6af
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
5 changes: 4 additions & 1 deletion z2/resources/chain-specs/zq2-protomainnet.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
]
6 changes: 3 additions & 3 deletions z2/resources/chain-specs/zq2-prototestnet.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
]
10 changes: 5 additions & 5 deletions z2/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,16 @@ impl Chain {
pub fn get_forks(&self) -> Option<Vec<Value>> {
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,
}
Expand Down
6 changes: 6 additions & 0 deletions zilliqa/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)]
Expand Down
8 changes: 8 additions & 0 deletions zilliqa/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ impl State {
current_block,
inspector,
&self.scilla_ext_libs_path,
self.forks.get(current_block.number),
)
} else {
scilla_call(
Expand Down Expand Up @@ -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>,
Expand All @@ -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"));
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 6ecb6af

Please sign in to comment.