Skip to content

Commit

Permalink
feat(HatsGatekeepers): add zero-address check to setMaciInstance()
Browse files Browse the repository at this point in the history
Prevents MACI address from being set to the zero address
  • Loading branch information
spengrah committed Mar 11, 2024
1 parent 6da9d55 commit 88bb16c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@ import { IHats } from "../../interfaces/IHats.sol";
abstract contract HatsGatekeeperBase is SignUpGatekeeper, Ownable {
/*//////////////////////////////////////////////////////////////
CUSTOM ERRORS
//////////////////////////////////////////////////////////////*/
//////////////////////////////////////////////////////////////*/

error OnlyMACI();
error NotWearingCriterionHat();
error AlreadyRegistered();
error ZeroAddress();

/*//////////////////////////////////////////////////////////////
PUBLIC CONSTANTS
//////////////////////////////////////////////////////////////*/
//////////////////////////////////////////////////////////////*/

/// @notice The Hats Protocol contract address
IHats public immutable hats;

/*//////////////////////////////////////////////////////////////
PUBLIC STATE VARIABLES
//////////////////////////////////////////////////////////////*/
//////////////////////////////////////////////////////////////*/

/// @notice the reference to the MACI contract
address public maci;
Expand All @@ -35,7 +36,7 @@ abstract contract HatsGatekeeperBase is SignUpGatekeeper, Ownable {

/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
//////////////////////////////////////////////////////////////*/

/// @notice Deploy an instance of HatsGatekeeper
/// @param _hats The Hats Protocol contract
Expand All @@ -45,11 +46,12 @@ abstract contract HatsGatekeeperBase is SignUpGatekeeper, Ownable {

/*//////////////////////////////////////////////////////////////
OWNER FUNCTIONS
//////////////////////////////////////////////////////////////*/
//////////////////////////////////////////////////////////////*/

/// @notice Allows to set the MACI contract
/// @param _maci The MACI contract interface to be stored
function setMaciInstance(address _maci) public override onlyOwner {
if (_maci == address(0)) revert ZeroAddress();
maci = _maci;
}
}
19 changes: 17 additions & 2 deletions contracts/tests/HatsGatekeeper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe("HatsProtocol Gatekeeper", () => {
const hatsContractOP = "0x3bc1A0Ad72417f2d411118085256fC53CBdDd137";

const user = new Keypair();
const oneAddress = "0x0000000000000000000000000000000000000001";

let topHat: bigint;
let hatId: bigint;
Expand Down Expand Up @@ -138,6 +139,13 @@ describe("HatsProtocol Gatekeeper", () => {
expect(await hatsGatekeeperSingle.maci()).to.eq(maciAddress);
});

it("should fail to set MACI instance to the zero address", async () => {
await expect(hatsGatekeeperSingle.setMaciInstance(ZeroAddress)).to.be.revertedWithCustomError(
hatsGatekeeperSingle,
"ZeroAddress",
);
});

it("should fail to set MACI instance when the caller is not the owner", async () => {
await expect(hatsGatekeeperSingle.connect(voter).setMaciInstance(signerAddress)).to.be.revertedWith(
"Ownable: caller is not the owner",
Expand All @@ -147,7 +155,7 @@ describe("HatsProtocol Gatekeeper", () => {

describe("register", () => {
it("should not allow to call from a non-registered MACI contract", async () => {
await hatsGatekeeperSingle.setMaciInstance(ZeroAddress);
await hatsGatekeeperSingle.setMaciInstance(oneAddress);
await expect(
maciContract
.connect(signer)
Expand Down Expand Up @@ -233,6 +241,13 @@ describe("HatsProtocol Gatekeeper", () => {
expect(await hatsGatekeeperMultiple.maci()).to.eq(maciAddress);
});

it("should fail to set MACI instance to the zero address", async () => {
await expect(hatsGatekeeperMultiple.setMaciInstance(ZeroAddress)).to.be.revertedWithCustomError(
hatsGatekeeperMultiple,
"ZeroAddress",
);
});

it("should fail to set MACI instance when the caller is not the owner", async () => {
await expect(hatsGatekeeperMultiple.connect(voter).setMaciInstance(signerAddress)).to.be.revertedWith(
"Ownable: caller is not the owner",
Expand All @@ -242,7 +257,7 @@ describe("HatsProtocol Gatekeeper", () => {

describe("register", () => {
it("should not allow to call from a non-registered MACI contract", async () => {
await hatsGatekeeperMultiple.connect(signer).setMaciInstance(ZeroAddress);
await hatsGatekeeperMultiple.connect(signer).setMaciInstance(oneAddress);
await expect(
maciContract
.connect(signer)
Expand Down

0 comments on commit 88bb16c

Please sign in to comment.