Skip to content

Commit

Permalink
v0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
remollemo committed Dec 5, 2023
1 parent eedee83 commit 1cb7308
Show file tree
Hide file tree
Showing 28 changed files with 708 additions and 319 deletions.
1 change: 1 addition & 0 deletions scripts/build-cairo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pushd $(dirname $0)/..
set -e
mkdir -p cairo_contracts

scripts/starknet-compile.py src --contract-path src::update_712_vars_eic::Update712VarsEIC cairo_contracts/Update712VarsEIC.sierra
scripts/starknet-compile.py src --contract-path src::roles_init_eic::RolesExternalInitializer cairo_contracts/RolesExternalInitializer.sierra
scripts/starknet-compile.py src --contract-path src::legacy_bridge_eic::LegacyBridgeUpgradeEIC cairo_contracts/LegacyBridgeUpgradeEIC.sierra
scripts/starknet-compile.py src --contract-path src::token_bridge::TokenBridge cairo_contracts/TokenBridge.sierra
Expand Down
14 changes: 11 additions & 3 deletions src/cairo/legacy_bridge_eic.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ mod LegacyBridgeUpgradeEIC {
use starknet::{
ContractAddress, get_caller_address, EthAddress, EthAddressIntoFelt252, EthAddressSerde
};
use super::super::erc20_interface::{IERC20Dispatcher, IERC20DispatcherTrait};
use super::super::access_control_interface::{
IAccessControl, RoleId, RoleAdminChanged, RoleGranted
};
use super::super::replaceability_interface::IEICInitializable;
use super::super::roles_interface::{
APP_GOVERNOR, APP_ROLE_ADMIN, GOVERNANCE_ADMIN, OPERATOR, TOKEN_ADMIN, UPGRADE_GOVERNOR
APP_GOVERNOR, APP_ROLE_ADMIN, GOVERNANCE_ADMIN, OPERATOR, SECURITY_ADMIN, SECURITY_AGENT,
TOKEN_ADMIN, UPGRADE_GOVERNOR
};

#[storage]
Expand All @@ -35,9 +37,7 @@ mod LegacyBridgeUpgradeEIC {
#[event]
enum Event {
// --- Access Control ---
#[event]
RoleGranted: RoleGranted,
#[event]
RoleAdminChanged: RoleAdminChanged,
}

Expand Down Expand Up @@ -71,6 +71,10 @@ mod LegacyBridgeUpgradeEIC {
self._set_role_admin(role: OPERATOR, admin_role: APP_ROLE_ADMIN);
self._set_role_admin(role: TOKEN_ADMIN, admin_role: APP_ROLE_ADMIN);
self._set_role_admin(role: UPGRADE_GOVERNOR, admin_role: GOVERNANCE_ADMIN);

self._grant_role(role: SECURITY_ADMIN, account: provisional_governance_admin);
self._set_role_admin(role: SECURITY_ADMIN, admin_role: SECURITY_ADMIN);
self._set_role_admin(role: SECURITY_AGENT, admin_role: SECURITY_ADMIN);
}

fn _grant_role(ref self: ContractState, role: RoleId, account: ContractAddress) {
Expand Down Expand Up @@ -105,6 +109,10 @@ mod LegacyBridgeUpgradeEIC {
assert(l2_token.is_non_zero(), 'ZERO_L2_TOKEN');

assert(legacy_l2_token == l2_token, 'TOKEN_ADDRESS_MISMATCH');

// Implicitly assert that the L2 token supports snake case (i.e. already upgraded.)
IERC20Dispatcher { contract_address: l2_token }.total_supply();

assert(self.l1_l2_token_map.read(l1_token).is_zero(), 'L2_BRIDGE_ALREADY_INITIALIZED');
assert(self.l2_l1_token_map.read(l2_token).is_zero(), 'L2_BRIDGE_ALREADY_INITIALIZED');

Expand Down
109 changes: 90 additions & 19 deletions src/cairo/legacy_eic_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,29 @@ mod legacy_eic_test {
use traits::TryInto;
use option::OptionTrait;
use serde::Serde;
use src::token_bridge_interface::{ITokenBridgeDispatcher, ITokenBridgeDispatcherTrait};

use starknet::class_hash::{ClassHash, class_hash_const};
use starknet::{ContractAddress, EthAddress, EthAddressZeroable, syscalls::deploy_syscall};
use src::legacy_bridge_tester::LegacyBridgeTester;
use openzeppelin::token::erc20::presets::erc20votes::ERC20VotesPreset::{
DAPP_NAME, DAPP_VERSION
};

use src::legacy_bridge_eic::LegacyBridgeUpgradeEIC;
use src::legacy_bridge_tester::LegacyBridgeTester;
use src::replaceability_interface::{
EICData, ImplementationData, IReplaceable, IReplaceableDispatcher,
IReplaceableDispatcherTrait
};
use src::roles_init_eic::RolesExternalInitializer;
use src::roles_interface::{IRolesDispatcher, IRolesDispatcherTrait};
use src::token_bridge::TokenBridge;
use src::test_utils::test_utils::{
caller, get_roles, get_token_bridge, set_contract_address_as_caller, get_replaceable,
DEFAULT_UPGRADE_DELAY
simple_deploy_l2_token, DEFAULT_UPGRADE_DELAY
};
use src::replaceability_interface::{
EICData, ImplementationData, IReplaceable, IReplaceableDispatcher,
IReplaceableDispatcherTrait
use src::token_bridge_interface::{ITokenBridgeDispatcher, ITokenBridgeDispatcherTrait};
use src::token_bridge::TokenBridge;
use src::update_712_vars_eic::Update712VarsEIC;
use src::update712_eic_tester::{
Update712EICTester, ITester, ITesterDispatcher, ITesterDispatcherTrait
};

fn L1_TOKEN_ADDRESS() -> EthAddress {
Expand All @@ -38,6 +45,10 @@ mod legacy_eic_test {
starknet::contract_address_const::<2073>()
}

fn get_712_tester(contract_address: ContractAddress) -> ITesterDispatcher {
ITesterDispatcher { contract_address }
}

fn deploy_legacy_tester(l2_token: ContractAddress) -> ContractAddress {
let mut calldata = ArrayTrait::new();
l2_token.serialize(ref calldata);
Expand All @@ -53,6 +64,17 @@ mod legacy_eic_test {
tester
}

fn deploy_eip_712_tester() -> ContractAddress {
let mut calldata = array![];
// Set the caller address for all the functions calls (except the constructor).
set_contract_address_as_caller();

// Deploy the contract.
let (tester, _) = deploy_syscall(update_712_eic_tester_hash(), 0, calldata.span(), false)
.unwrap();
tester
}

#[test]
#[available_gas(30000000)]
fn test_deploy_tester() {
Expand All @@ -68,12 +90,31 @@ mod legacy_eic_test {
LegacyBridgeTester::TEST_CLASS_HASH.try_into().unwrap()
}

fn update_712_eic_tester_hash() -> ClassHash {
Update712EICTester::TEST_CLASS_HASH.try_into().unwrap()
}

fn update_712_eic_hash() -> ClassHash {
Update712VarsEIC::TEST_CLASS_HASH.try_into().unwrap()
}

fn roles_init_eic_hash() -> ClassHash {
RolesExternalInitializer::TEST_CLASS_HASH.try_into().unwrap()
}

fn custom_712_eic_implementation_data(impl_hash: ClassHash) -> ImplementationData {
generic_eic_implementation_data(:impl_hash, eic_hash: update_712_eic_hash())
}

fn custom_roles_eic_implementation_data(impl_hash: ClassHash) -> ImplementationData {
generic_eic_implementation_data(:impl_hash, eic_hash: roles_init_eic_hash())
}

fn generic_eic_implementation_data(
impl_hash: ClassHash, eic_hash: ClassHash
) -> ImplementationData {
let mut calldata = array![];
let eic_data = EICData {
eic_hash: RolesExternalInitializer::TEST_CLASS_HASH.try_into().unwrap(),
eic_init_data: calldata.span()
};
let eic_data = EICData { eic_hash, eic_init_data: calldata.span() };

ImplementationData { impl_hash, eic_data: Option::Some(eic_data), final: false }
}
Expand Down Expand Up @@ -140,19 +181,20 @@ mod legacy_eic_test {
#[test]
#[available_gas(30000000)]
fn test_happy_path() {
let tester_address = deploy_legacy_tester(L2_TOKEN_ADDRESS());
let l2_token = simple_deploy_l2_token();
let tester_address = deploy_legacy_tester(l2_token);
let impl_data = token_bridge_w_eic_implementation_data(
l1_token: L1_TOKEN_ADDRESS(), l2_token: L2_TOKEN_ADDRESS(),
l1_token: L1_TOKEN_ADDRESS(), :l2_token,
);
add_impl_and_replace_to(
replaceable_address: tester_address, implementation_data: impl_data,
);

let token_bridge = get_token_bridge(tester_address);
let l1_token = token_bridge.get_l1_token(L2_TOKEN_ADDRESS());
let l2_token = token_bridge.get_l2_token(L1_TOKEN_ADDRESS());
let l1_token = token_bridge.get_l1_token(l2_token);
let l2_token_actual = token_bridge.get_l2_token(L1_TOKEN_ADDRESS());
assert(L1_TOKEN_ADDRESS() == l1_token, 'L1_ZEROED');
assert(L2_TOKEN_ADDRESS() == l2_token, 'L2_ZEROED');
assert(l2_token == l2_token_actual, 'L2_ZEROED');
}

#[test]
Expand All @@ -166,6 +208,8 @@ mod legacy_eic_test {
// Tester 1 roles are not initialzied, and gov admin not set.
let roles1 = get_roles(tester1);
assert(!roles1.is_governance_admin(caller()), 'Roles should not be initialized');
assert(!roles1.is_upgrade_governor(caller()), 'Roles should not be initialized');
assert(!roles1.is_security_admin(caller()), 'Roles should not be initialized');

// Tester 2 is upgraded to the token_bridge with the roles EIC.
let tester2 = deploy_legacy_tester(ContractAddressZeroable::zero());
Expand All @@ -175,6 +219,32 @@ mod legacy_eic_test {
// Tester 2 roles are initialized and gov admin assigned.
let roles = get_roles(tester2);
assert(roles.is_governance_admin(caller()), 'Roles should be initialized');
assert(roles.is_upgrade_governor(caller()), 'Roles should be initialized');
assert(roles.is_security_admin(caller()), 'Roles should be initialized');
}

#[test]
#[available_gas(30000000)]
fn test_update_eip712_vars_eic() {
// Test update_eip712_eic - i.e. that the eic populate vars correctly.

// Deploy tester & check that vars are empty.
let tester_address = deploy_eip_712_tester();
let tester = get_712_tester(tester_address);
assert(tester.get_dapp_name() == '', 'dapp_name not empty');
assert(tester.get_dapp_version() == '', 'dapp_version not empty');

// Perform an update with the update_712_eic.
let implementation_data = custom_712_eic_implementation_data(update_712_eic_tester_hash());
add_impl_and_replace_to(replaceable_address: tester_address, :implementation_data);

// Check that variables are now with correct values.
assert(tester.get_dapp_name() == DAPP_NAME, 'Bad dapp_name');
assert(tester.get_dapp_version() == DAPP_VERSION, 'Bad dapp_version');

// Sanity - assert that the values are what we really expect.
assert(DAPP_NAME == 'TOKEN_DELEGATION', 'Broken test');
assert(DAPP_VERSION == '1.0.0', 'Broken test');
}

#[test]
Expand Down Expand Up @@ -252,9 +322,10 @@ mod legacy_eic_test {
#[available_gas(30000000)]
fn test_upgrade_an_upgraded() {
// Test failing to upgrade twice.
let tester_address = deploy_legacy_tester(L2_TOKEN_ADDRESS());
let l2_token = simple_deploy_l2_token();
let tester_address = deploy_legacy_tester(l2_token);
let impl_data = token_bridge_w_eic_implementation_data(
l1_token: L1_TOKEN_ADDRESS(), l2_token: L2_TOKEN_ADDRESS(),
l1_token: L1_TOKEN_ADDRESS(), :l2_token,
);

// Upgrade first time. All goes well.
Expand Down
2 changes: 2 additions & 0 deletions src/cairo/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod token_test_setup_interface;
mod token_bridge;
mod legacy_bridge_eic;
mod roles_init_eic;
mod update_712_vars_eic;
mod err_msg;

// Tests.
Expand All @@ -26,3 +27,4 @@ mod stub_msg_receiver;
mod replaceability_test;
mod legacy_bridge_tester;
mod legacy_eic_test;
mod update712_eic_tester;
9 changes: 6 additions & 3 deletions src/cairo/roles_init_eic.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ mod RolesExternalInitializer {
};
use super::super::replaceability_interface::IEICInitializable;
use super::super::roles_interface::{
APP_GOVERNOR, APP_ROLE_ADMIN, GOVERNANCE_ADMIN, OPERATOR, TOKEN_ADMIN, UPGRADE_GOVERNOR
APP_GOVERNOR, APP_ROLE_ADMIN, GOVERNANCE_ADMIN, OPERATOR, SECURITY_ADMIN, SECURITY_AGENT,
TOKEN_ADMIN, UPGRADE_GOVERNOR
};

#[storage]
Expand All @@ -29,9 +30,7 @@ mod RolesExternalInitializer {
#[event]
enum Event {
// --- Access Control ---
#[event]
RoleGranted: RoleGranted,
#[event]
RoleAdminChanged: RoleAdminChanged,
}

Expand All @@ -57,6 +56,10 @@ mod RolesExternalInitializer {
self._set_role_admin(role: OPERATOR, admin_role: APP_ROLE_ADMIN);
self._set_role_admin(role: TOKEN_ADMIN, admin_role: APP_ROLE_ADMIN);
self._set_role_admin(role: UPGRADE_GOVERNOR, admin_role: GOVERNANCE_ADMIN);

self._grant_role(role: SECURITY_ADMIN, account: provisional_governance_admin);
self._set_role_admin(role: SECURITY_ADMIN, admin_role: SECURITY_ADMIN);
self._set_role_admin(role: SECURITY_AGENT, admin_role: SECURITY_ADMIN);
}

fn _grant_role(ref self: ContractState, role: RoleId, account: ContractAddress) {
Expand Down
2 changes: 1 addition & 1 deletion src/cairo/test_utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ mod test_utils {

// Deploy token contract.
let mut token_bridge_state = TokenBridge::contract_state_for_testing();
TokenBridge::handle_token_enrollment(
TokenBridge::handle_token_deployment(
ref token_bridge_state,
from_address: l1_bridge_address.into(),
:l1_token,
Expand Down
Loading

0 comments on commit 1cb7308

Please sign in to comment.