From 0c5b7eb34a24928e95d6057ed8ae839c4cdac929 Mon Sep 17 00:00:00 2001 From: Giacomo Date: Thu, 3 Oct 2024 10:13:36 +0200 Subject: [PATCH] refactor(contracts): adapt trait to check and pass internal-external behaviour --- packages/contracts/contracts/src/core/Excubia.sol | 9 ++++++++- .../contracts/src/extensions/FreeForAllExcubia.sol | 4 +++- .../contracts/contracts/test/FreeForAllExcubia.t.sol | 6 +++++- .../test/wrappers/FreeForAllExcubiaTestWrapper.sol | 4 ++++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/contracts/contracts/src/core/Excubia.sol b/packages/contracts/contracts/src/core/Excubia.sol index e7a5edb..be02b59 100644 --- a/packages/contracts/contracts/src/core/Excubia.sol +++ b/packages/contracts/contracts/src/core/Excubia.sol @@ -22,7 +22,9 @@ abstract contract Excubia is IExcubia, Ownable(msg.sender) { } /// @inheritdoc IExcubia - function trait() external pure virtual returns (string memory) {} + function trait() external pure virtual returns (string memory) { + return _trait(); + } /// @inheritdoc IExcubia function setGate(address _gate) public virtual onlyOwner { @@ -44,6 +46,11 @@ abstract contract Excubia is IExcubia, Ownable(msg.sender) { _check(passerby, data); } + /// @notice Internal function to define the trait of the Excubia contract. + /// @dev maintain consistency across _pass & _check definitions. + /// @return The specific trait of the Excubia contract (e.g., SemaphoreExcubia has trait `Semaphore`). + function _trait() internal pure virtual returns (string memory) {} + /// @notice Internal function to enforce the custom gate passing logic. /// @dev Calls the `_check` internal logic and emits the relative event if successful. /// @param passerby The address of the entity attempting to pass the gate. diff --git a/packages/contracts/contracts/src/extensions/FreeForAllExcubia.sol b/packages/contracts/contracts/src/extensions/FreeForAllExcubia.sol index faed571..078e3b3 100644 --- a/packages/contracts/contracts/src/extensions/FreeForAllExcubia.sol +++ b/packages/contracts/contracts/src/extensions/FreeForAllExcubia.sol @@ -15,7 +15,9 @@ contract FreeForAllExcubia is Excubia { mapping(address => bool) public passedPassersby; /// @notice The trait of the Excubia contract. - function trait() external pure override returns (string memory) { + function _trait() internal pure override returns (string memory) { + super._trait(); + return "FreeForAll"; } diff --git a/packages/contracts/contracts/test/FreeForAllExcubia.t.sol b/packages/contracts/contracts/test/FreeForAllExcubia.t.sol index f7d48f2..4aca228 100644 --- a/packages/contracts/contracts/test/FreeForAllExcubia.t.sol +++ b/packages/contracts/contracts/test/FreeForAllExcubia.t.sol @@ -34,6 +34,10 @@ contract FreeForAllExcubiaTest is Test { vm.stopPrank(); } + function testInternalTrait() public view { + freeForAllExcubiaTW.exposed_trait(); + } + function testInternalCheck() public view { freeForAllExcubiaTW.exposed_check(passerby, ""); } @@ -222,7 +226,7 @@ contract FreeForAllExcubiaTest is Test { uint256 gasAfter = gasleft(); uint256 gasUsed = gasBefore - gasAfter; - assert(gasUsed < 30000); + assert(gasUsed < 70000); } function test_GatePassesSelf() public { diff --git a/packages/contracts/contracts/test/wrappers/FreeForAllExcubiaTestWrapper.sol b/packages/contracts/contracts/test/wrappers/FreeForAllExcubiaTestWrapper.sol index f3fea55..5f26210 100644 --- a/packages/contracts/contracts/test/wrappers/FreeForAllExcubiaTestWrapper.sol +++ b/packages/contracts/contracts/test/wrappers/FreeForAllExcubiaTestWrapper.sol @@ -4,6 +4,10 @@ pragma solidity ^0.8.27; import {FreeForAllExcubia} from "../../src/extensions/FreeForAllExcubia.sol"; contract FreeForAllExcubiaTestWrapper is FreeForAllExcubia { + function exposed_trait() public pure { + _trait(); + } + function exposed_check(address passerby, bytes calldata data) public view { _check(passerby, data); }