Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce DeltaForks structure for enhanced fork configuration #2162

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions z2/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use tokio::fs;
/// For now, it just generates secret keys (which should be different each run, or we will become dependent on their values)
use zilliqa::{
api,
cfg::{max_rpc_response_size_default, state_cache_size_default, ApiServer},
cfg::{max_rpc_response_size_default, state_cache_size_default, ApiServer, DeltaForks},
crypto::{SecretKey, TransactionPublicKey},
};
use zilliqa::{
Expand All @@ -28,7 +28,7 @@ use zilliqa::{
eth_chain_id_default, failed_request_sleep_duration_default, local_address_default,
max_blocks_in_flight_default, scilla_address_default, scilla_ext_libs_path_default,
scilla_stdlib_dir_default, state_rpc_limit_default, total_native_token_supply_default,
Amount, ConsensusConfig, ContractUpgradesBlockHeights, Forks, GenesisDeposit,
Amount, ConsensusConfig, ContractUpgradesBlockHeights, GenesisDeposit,
},
transaction::EvmGas,
};
Expand Down Expand Up @@ -537,7 +537,7 @@ impl Setup {
total_native_token_supply: total_native_token_supply_default(),
scilla_call_gas_exempt_addrs: vec![],
contract_upgrade_block_heights: ContractUpgradesBlockHeights::default(),
forks: Forks::default(),
forks: DeltaForks::default(),
},
block_request_limit: block_request_limit_default(),
max_blocks_in_flight: max_blocks_in_flight_default(),
Expand Down
160 changes: 151 additions & 9 deletions zilliqa/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ pub struct ConsensusConfig {
/// Forks in block execution logic. Each entry describes the difference in logic and the block height at which that
/// difference applies.
#[serde(default)]
pub forks: Forks,
pub forks: DeltaForks,
}

impl Default for ConsensusConfig {
Expand Down Expand Up @@ -397,6 +397,50 @@ impl Default for ConsensusConfig {
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DeltaForks(Vec<DeltaFork>);
impl Default for DeltaForks {
fn default() -> Self {
DeltaForks(vec![DeltaFork {
at_height: 0,
failed_scilla_call_from_gas_exempt_caller_causes_revert: Some(true),
call_mode_1_sets_caller_to_parent_caller: Some(true),
scilla_messages_can_call_evm_contracts: Some(true),
scilla_contract_creation_increments_account_balance: Some(true),
}])
}
}

impl From<DeltaForks> for Forks {
fn from(delta_forks: DeltaForks) -> Self {
let mut forks: Vec<Fork> = vec![];
for delta in delta_forks.0 {
if let Some(last_fork) = forks.last() {
let new_fork = last_fork.apply_delta_fork(&delta);
forks.push(new_fork);
} else {
let base_fork = Fork {
at_height: delta.at_height,
failed_scilla_call_from_gas_exempt_caller_causes_revert: delta
.failed_scilla_call_from_gas_exempt_caller_causes_revert
.unwrap_or(true),
call_mode_1_sets_caller_to_parent_caller: delta
.call_mode_1_sets_caller_to_parent_caller
.unwrap_or(true),
scilla_messages_can_call_evm_contracts: delta
.scilla_messages_can_call_evm_contracts
.unwrap_or(true),
scilla_contract_creation_increments_account_balance: delta
.scilla_contract_creation_increments_account_balance
.unwrap_or(true),
};
forks.push(base_fork);
}
}
Forks(forks)
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(try_from = "Vec<Fork>", into = "Vec<Fork>")]
pub struct Forks(Vec<Fork>);
Expand Down Expand Up @@ -455,11 +499,20 @@ impl Forks {

#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct Fork {
pub at_height: u64,
pub failed_scilla_call_from_gas_exempt_caller_causes_revert: bool,
pub call_mode_1_sets_caller_to_parent_caller: bool,
pub scilla_messages_can_call_evm_contracts: bool,
pub scilla_contract_creation_increments_account_balance: bool,
}

#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct DeltaFork {
pub at_height: u64,
/// If true, if a caller who is in the `scilla_call_gas_exempt_addrs` list makes a call to the `scilla_call`
/// precompile and the inner Scilla call fails, the entire transaction will revert. If false, the normal EVM
/// semantics apply where the caller can decide how to act based on the success of the inner call.
pub failed_scilla_call_from_gas_exempt_caller_causes_revert: bool,
pub failed_scilla_call_from_gas_exempt_caller_causes_revert: Option<bool>,
/// If true, if a call is made to the `scilla_call` precompile with `call_mode` / `keep_origin` set to `1`, the
/// `_sender` of the inner Scilla call will be set to the caller of the current call-stack. If false, the `_sender`
/// will be set to the original transaction signer.
Expand All @@ -469,16 +522,35 @@ pub struct Fork {
///
/// When this flag is true, `D` will see the `_sender` as `B`. When this flag is false, `D` will see the `_sender`
/// as `A`.
pub call_mode_1_sets_caller_to_parent_caller: bool,
pub call_mode_1_sets_caller_to_parent_caller: Option<bool>,
/// If true, when a Scilla message is sent to an EVM contract, the EVM contract will be treated as if it was an
/// 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,
pub scilla_messages_can_call_evm_contracts: Option<bool>,
/// If true, when a contract is deployed, if the contract address is already funded,
/// the contract balance will be sum of the existing balance and the amount sent in the deployment transaction.
/// If false, the contract balance will be the amount sent in the deployment transaction.
pub scilla_contract_creation_increments_account_balance: Option<bool>,
}

impl Fork {
pub fn apply_delta_fork(&self, delta: &DeltaFork) -> Fork {
Fork {
at_height: delta.at_height,
failed_scilla_call_from_gas_exempt_caller_causes_revert: delta
.failed_scilla_call_from_gas_exempt_caller_causes_revert
.unwrap_or(self.failed_scilla_call_from_gas_exempt_caller_causes_revert),
call_mode_1_sets_caller_to_parent_caller: delta
.call_mode_1_sets_caller_to_parent_caller
.unwrap_or(self.call_mode_1_sets_caller_to_parent_caller),
scilla_messages_can_call_evm_contracts: delta
.scilla_messages_can_call_evm_contracts
.unwrap_or(self.scilla_messages_can_call_evm_contracts),
scilla_contract_creation_increments_account_balance: delta
.scilla_contract_creation_increments_account_balance
.unwrap_or(self.scilla_contract_creation_increments_account_balance),
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -560,3 +632,73 @@ impl ContractUpgradesBlockHeights {
)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_single_delta_fork() {
let delta_forks = DeltaForks(vec![DeltaFork {
at_height: 0,
failed_scilla_call_from_gas_exempt_caller_causes_revert: Some(true),
call_mode_1_sets_caller_to_parent_caller: Some(false),
scilla_messages_can_call_evm_contracts: Some(true),
scilla_contract_creation_increments_account_balance: Some(false),
}]);

let result = Forks::from(delta_forks);
assert_eq!(result.0.len(), 1);
assert_eq!(result.0[0].at_height, 0);
assert!(!result.0[0].call_mode_1_sets_caller_to_parent_caller);
assert!(!result.0[0].scilla_contract_creation_increments_account_balance,);
}

#[test]
fn test_multiple_delta_forks() {
let delta_forks = DeltaForks(vec![
DeltaFork {
at_height: 0,
failed_scilla_call_from_gas_exempt_caller_causes_revert: Some(true),
call_mode_1_sets_caller_to_parent_caller: Some(true),
scilla_messages_can_call_evm_contracts: Some(true),
scilla_contract_creation_increments_account_balance: Some(true),
},
DeltaFork {
at_height: 100,
failed_scilla_call_from_gas_exempt_caller_causes_revert: None,
call_mode_1_sets_caller_to_parent_caller: Some(false),
scilla_messages_can_call_evm_contracts: None,
scilla_contract_creation_increments_account_balance: Some(false),
},
DeltaFork {
at_height: 200,
failed_scilla_call_from_gas_exempt_caller_causes_revert: Some(false),
call_mode_1_sets_caller_to_parent_caller: None,
scilla_messages_can_call_evm_contracts: Some(false),
scilla_contract_creation_increments_account_balance: None,
},
]);

let result = Forks::from(delta_forks);
assert_eq!(result.0.len(), 3);

assert_eq!(result.0[0].at_height, 0);
assert!(result.0[0].failed_scilla_call_from_gas_exempt_caller_causes_revert);
assert!(result.0[0].call_mode_1_sets_caller_to_parent_caller);
assert!(result.0[0].scilla_messages_can_call_evm_contracts);
assert!(result.0[0].scilla_contract_creation_increments_account_balance,);

assert_eq!(result.0[1].at_height, 100);
assert!(result.0[1].failed_scilla_call_from_gas_exempt_caller_causes_revert,);
assert!(!result.0[1].call_mode_1_sets_caller_to_parent_caller);
assert!(result.0[1].scilla_messages_can_call_evm_contracts);
assert!(!result.0[1].scilla_contract_creation_increments_account_balance);

assert_eq!(result.0[2].at_height, 200);
assert!(!result.0[2].failed_scilla_call_from_gas_exempt_caller_causes_revert);
assert!(!result.0[2].call_mode_1_sets_caller_to_parent_caller);
assert!(!result.0[2].scilla_messages_can_call_evm_contracts);
assert!(!result.0[2].scilla_contract_creation_increments_account_balance,);
}
}
2 changes: 1 addition & 1 deletion zilliqa/src/contracts/deposit_v1.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.20;
pragma solidity ^0.8.24;

import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import {Deque} from "./utils/deque.sol";
Expand Down
2 changes: 1 addition & 1 deletion zilliqa/src/contracts/deposit_v2.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.20;
pragma solidity ^0.8.24;

import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import {Deque, Withdrawal} from "./utils/deque.sol";
Expand Down
2 changes: 1 addition & 1 deletion zilliqa/src/contracts/deposit_v3.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.20;
pragma solidity ^0.8.24;

import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import {Deque, Withdrawal} from "./utils/deque.sol";
Expand Down
2 changes: 1 addition & 1 deletion zilliqa/src/contracts/deposit_v4.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.20;
pragma solidity ^0.8.24;

import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import {Deque, Withdrawal} from "./utils/deque.sol";
Expand Down
2 changes: 1 addition & 1 deletion zilliqa/src/contracts/intershard_bridge.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.4;
pragma solidity ^0.8.24;

contract IntershardBridge {
event Relayed(
Expand Down
2 changes: 1 addition & 1 deletion zilliqa/src/contracts/shard.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.4;
pragma solidity ^0.8.24;

contract Shard {
event ValidatorAdded(address validator);
Expand Down
2 changes: 1 addition & 1 deletion zilliqa/src/contracts/shard_registry.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.4;
pragma solidity ^0.8.24;

import {Shard} from "./shard.sol";

Expand Down
2 changes: 1 addition & 1 deletion zilliqa/src/contracts/utils/deque.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.20;
pragma solidity ^0.8.24;

struct Withdrawal {
uint256 startedAt;
Expand Down
2 changes: 1 addition & 1 deletion zilliqa/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl State {
gas_price: *consensus_config.gas_price,
scilla_call_gas_exempt_addrs: consensus_config.scilla_call_gas_exempt_addrs.clone(),
chain_id: ChainId::new(config.eth_chain_id),
forks: consensus_config.forks.clone(),
forks: consensus_config.forks.clone().into(),
block_store,
}
}
Expand Down
2 changes: 1 addition & 1 deletion zilliqa/tests/it/contracts/CallMe.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT
pragma solidity ^0.8.20;
pragma solidity ^0.8.24;

contract CallMe {
constructor() {}
Expand Down
2 changes: 1 addition & 1 deletion zilliqa/tests/it/contracts/CallingContract.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT
// Source: https://solidity-by-example.org/calling-contract/
pragma solidity ^0.8.20;
pragma solidity ^0.8.24;

contract Callee {
uint256 public x;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT
pragma solidity ^0.8.20;
pragma solidity ^0.8.24;

contract Creator {
event Created(CreateMe createMe);
Expand Down
2 changes: 1 addition & 1 deletion zilliqa/tests/it/contracts/EmitEvents.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT
pragma solidity ^0.8.20;
pragma solidity ^0.8.24;

contract EmitEvents {
// Two random events from common tokens.
Expand Down
2 changes: 1 addition & 1 deletion zilliqa/tests/it/contracts/LinkableShard.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
pragma solidity ^0.8.24;

import "../../../src/contracts/shard.sol";
import "../../../src/contracts/shard_registry.sol";
Expand Down
2 changes: 1 addition & 1 deletion zilliqa/tests/it/contracts/RevertMe.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
pragma solidity ^0.8.24;

contract RevertMe {
int256 public value = 0;
Expand Down
2 changes: 1 addition & 1 deletion zilliqa/tests/it/contracts/ScillaInterop.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
pragma solidity ^0.8.24;

// Source: https://github.com/Zilliqa/zilliqa-developer/blob/main/contracts/experimental/ERC20ProxyForZRC2/contracts/ScillaConnector.sol
library ScillaConnector {
Expand Down
2 changes: 1 addition & 1 deletion zilliqa/tests/it/contracts/SetGetContractValue.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
pragma solidity ^0.8.24;

contract SetGetContractValue {
uint256 private value = 99;
Expand Down
2 changes: 1 addition & 1 deletion zilliqa/tests/it/contracts/Storage.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
pragma solidity ^0.8.24;
contract Storage {
uint pos0;
mapping(address => uint) public pos1;
Expand Down
6 changes: 3 additions & 3 deletions zilliqa/tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ use zilliqa::{
max_blocks_in_flight_default, max_rpc_response_size_default, scilla_address_default,
scilla_ext_libs_path_default, scilla_stdlib_dir_default, state_cache_size_default,
state_rpc_limit_default, total_native_token_supply_default, Amount, ApiServer, Checkpoint,
ConsensusConfig, ContractUpgradesBlockHeights, Forks, GenesisDeposit, NodeConfig,
ConsensusConfig, ContractUpgradesBlockHeights, DeltaForks, GenesisDeposit, NodeConfig,
},
crypto::{SecretKey, TransactionPublicKey},
db,
Expand Down Expand Up @@ -351,7 +351,7 @@ impl Network {
Address::new(get_contract_address(secret_key_to_address(&genesis_key).0, 2).0),
],
contract_upgrade_block_heights,
forks: Forks::default(),
forks: DeltaForks::default(),
},
api_servers: vec![ApiServer {
port: 4201,
Expand Down Expand Up @@ -489,7 +489,7 @@ impl Network {
get_contract_address(secret_key_to_address(&self.genesis_key).0, 2).0,
)],
contract_upgrade_block_heights,
forks: Forks::default(),
forks: DeltaForks::default(),
},
block_request_limit: block_request_limit_default(),
max_blocks_in_flight: max_blocks_in_flight_default(),
Expand Down
Loading