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

H02 L09 #10

Merged
merged 6 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/openzeppelin-contracts
Submodule openzeppelin-contracts added at 281550
6 changes: 4 additions & 2 deletions src/SphereXEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ contract SphereXEngine is Ownable, ISphereXEngine {
uint256 private constant PATTERN_START = 1;
uint256 private constant DEPTH_START = 1;
bytes32 private constant DEACTIVATED = bytes32(0);
uint64 private constant RULES_1_AND_2_TOGETHER = 3;

event TxStartedAtIrregularDepth();

Expand All @@ -47,14 +48,15 @@ contract SphereXEngine is Ownable, ISphereXEngine {
* Activate the guardian rules
* @param rules bytes8 representing the new rules to activate.
*/
function activateRules(bytes8 rules) external onlyOwner {
function configureRules(bytes8 rules) external onlyOwner {
require(RULES_1_AND_2_TOGETHER & uint64(rules) != RULES_1_AND_2_TOGETHER, "Illegal rules combination");
_engineRules = rules;
}

/**
* Deactivates the engine, the calls will return without being checked
*/
function deactivateRules() external onlyOwner {
function deactivateAllRules() external onlyOwner {
_engineRules = bytes8(uint64(0));
}

Expand Down
35 changes: 20 additions & 15 deletions test/SphereXEngine.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ contract SphereXEngineTest is Test, CFUtils {
// This will make forge call the function with 1 and 2 as inputs!
uint16 assumeVariable = uint8(uint16(uint64(rule)));
vm.assume(assumeVariable > 0 && assumeVariable < 3);
spherex_engine.activateRules(bytes8(rule));
spherex_engine.configureRules(bytes8(rule));

_;
}
Expand Down Expand Up @@ -142,6 +142,11 @@ contract SphereXEngineTest is Test, CFUtils {

// ============ Modifiers ============

function test_badRulesConfig() public {
vm.expectRevert("Illegal rules combination");
spherex_engine.configureRules(bytes8(uint64(3)));
}

function test_onlyOwner() public {
vm.expectRevert("Ownable: caller is not the owner");
// change caller to random address
Expand All @@ -151,37 +156,37 @@ contract SphereXEngineTest is Test, CFUtils {
}

function test_onlyApprovedSenders_sphereXValidateInternalPre() public {
spherex_engine.activateRules(CF);
spherex_engine.configureRules(CF);
vm.expectRevert("!SX:SENDERS");
vm.prank(random_address);
sendNumberToEngine(1);
}

function test_onlyApprovedSenders_sphereXValidatePre() public {
spherex_engine.activateRules(CF);
spherex_engine.configureRules(CF);
vm.expectRevert("!SX:SENDERS");
vm.prank(random_address);
spherex_engine.sphereXValidatePre(1, address(this), msg.data);
}

function test_onlyApprovedSenders_sphereXValidatePost() public {
spherex_engine.activateRules(CF);
spherex_engine.configureRules(CF);
vm.expectRevert("!SX:SENDERS");
vm.prank(random_address);
bytes32[] memory emptyArray = new bytes32[](0);
spherex_engine.sphereXValidatePost(1, 0, emptyArray, emptyArray);
}

function test_returnsIfNotActivated_sphereXValidateInternalPre() public {
spherex_engine.deactivateRules();
spherex_engine.deactivateAllRules();
sendNumberToEngine(1);
sendNumberToEngine(-1);

assertFlowStorageSlotsInInitialState();
}

function test_returnsIfNotActivated_sphereXValidatePrePost() public {
spherex_engine.deactivateRules();
spherex_engine.deactivateAllRules();
spherex_engine.sphereXValidatePre(1, address(this), msg.data);
bytes32[] memory emptyArray = new bytes32[](0);
spherex_engine.sphereXValidatePost(-1, 0, emptyArray, emptyArray);
Expand All @@ -190,31 +195,31 @@ contract SphereXEngineTest is Test, CFUtils {
}

function test_activateRule1_not_owner() public {
spherex_engine.activateRules(CF);
spherex_engine.configureRules(CF);

vm.expectRevert("Ownable: caller is not the owner");
vm.prank(random_address);
spherex_engine.activateRules(CF);
spherex_engine.configureRules(CF);

assertFlowStorageSlotsInInitialState();
}

function test_activateRule2_not_owner() public {
spherex_engine.activateRules(CF);
spherex_engine.configureRules(CF);

vm.expectRevert("Ownable: caller is not the owner");
vm.prank(random_address);
spherex_engine.activateRules(PREFIX_TX_FLOW);
spherex_engine.configureRules(PREFIX_TX_FLOW);

assertFlowStorageSlotsInInitialState();
}

function test_deactivateRules_not_owner() public {
spherex_engine.deactivateRules();
function test_deactivateAllRules_not_owner() public {
spherex_engine.deactivateAllRules();

vm.expectRevert("Ownable: caller is not the owner");
vm.prank(random_address);
spherex_engine.deactivateRules();
spherex_engine.deactivateAllRules();

assertFlowStorageSlotsInInitialState();
}
Expand Down Expand Up @@ -393,7 +398,7 @@ contract SphereXEngineTest is Test, CFUtils {
}

function test_activateRule1_after_Rule2() public activateRule(PREFIX_TX_FLOW) {
spherex_engine.activateRules(CF);
spherex_engine.configureRules(CF);
allowed_cf_storage = [int16(1), -1];
addAllowedPattern();

Expand All @@ -408,7 +413,7 @@ contract SphereXEngineTest is Test, CFUtils {
}

function test_activateRule2_after_Rule1() public activateRule(CF) {
spherex_engine.activateRules(PREFIX_TX_FLOW);
spherex_engine.configureRules(PREFIX_TX_FLOW);

// If we would have stayed in rule1 the test would have failed (see somment above the original test)
test_PrefixTFlow_same_origin_same_block_number();
Expand Down
4 changes: 2 additions & 2 deletions test/SphereXProtected.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ contract SphereXProtectedTest is Test, CFUtils {
CostumerContract public costumer_contract;

modifier activateRule2() {
spherex_engine.activateRules(PREFIX_TX_FLOW);
spherex_engine.configureRules(PREFIX_TX_FLOW);
allowed_cf_storage = [int16(1), -1, 11, 12, -12, -11];
addAllowedPattern();
allowed_cf_storage = [int16(1), -1];
Expand All @@ -38,7 +38,7 @@ contract SphereXProtectedTest is Test, CFUtils {

spherex_engine.addAllowedSender(allowed_senders);
spherex_engine.addAllowedPatterns(allowed_patterns);
spherex_engine.activateRules(bytes8(uint64(1)));
spherex_engine.configureRules(bytes8(uint64(1)));

costumer_contract.changeSphereXEngine(address(spherex_engine));
}
Expand Down
2 changes: 1 addition & 1 deletion test/SphereXProtectedUpgradeable.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ contract SphereXProtectedProxyTest is Test, SphereXProtectedTest {
allowed_senders.push(address(costumer_proxy_contract));
spherex_engine.addAllowedSender(allowed_senders);
spherex_engine.addAllowedPatterns(allowed_patterns);
spherex_engine.activateRules(CF);
spherex_engine.configureRules(CF);
p_costumerContract = CostumerContract(address(costumer_proxy_contract));
p_costumerContract.initialize(address(this));
p_costumerContract.changeSphereXEngine(address(spherex_engine));
Expand Down