From 7f57adb8820b329082f7f5126c6ed8af7b9569b7 Mon Sep 17 00:00:00 2001 From: Satyajeet Kolhapure <77279246+satyajeetkolhapure@users.noreply.github.com> Date: Tue, 26 Nov 2024 13:26:19 +0000 Subject: [PATCH] chore(contracts): The getAttester function should only be accessible internally (#827) --- contracts/src/abstracts/AbstractPortal.sol | 2 +- contracts/src/abstracts/AbstractPortalV2.sol | 2 +- contracts/src/interfaces/IPortal.sol | 6 - contracts/src/stdlib/IndexerModule.sol | 188 ------------------- contracts/test/stdlib/IndexerModule.t.sol | 135 ------------- 5 files changed, 2 insertions(+), 331 deletions(-) delete mode 100644 contracts/src/stdlib/IndexerModule.sol delete mode 100644 contracts/test/stdlib/IndexerModule.t.sol diff --git a/contracts/src/abstracts/AbstractPortal.sol b/contracts/src/abstracts/AbstractPortal.sol index 4c46ae1d..9e5f9022 100644 --- a/contracts/src/abstracts/AbstractPortal.sol +++ b/contracts/src/abstracts/AbstractPortal.sol @@ -270,7 +270,7 @@ abstract contract AbstractPortal is IPortal { * @notice Defines the address of the entity issuing attestations to the subject * @dev We strongly encourage a reflection when overriding this rule: who should be set as the attester? */ - function getAttester() public view virtual returns (address) { + function getAttester() internal view virtual returns (address) { return msg.sender; } diff --git a/contracts/src/abstracts/AbstractPortalV2.sol b/contracts/src/abstracts/AbstractPortalV2.sol index 614ff33b..5a73751a 100644 --- a/contracts/src/abstracts/AbstractPortalV2.sol +++ b/contracts/src/abstracts/AbstractPortalV2.sol @@ -202,7 +202,7 @@ abstract contract AbstractPortalV2 is IPortal { * @notice Defines the address of the entity issuing attestations to the subject * @dev We strongly encourage a reflection when overriding this rule: who should be set as the attester? */ - function getAttester() public view virtual returns (address) { + function getAttester() internal view virtual returns (address) { return msg.sender; } diff --git a/contracts/src/interfaces/IPortal.sol b/contracts/src/interfaces/IPortal.sol index cbd641d7..1968859c 100644 --- a/contracts/src/interfaces/IPortal.sol +++ b/contracts/src/interfaces/IPortal.sol @@ -16,10 +16,4 @@ interface IPortal is IERC165 { * @return The list of modules addresses linked to the Portal */ function getModules() external view returns (address[] memory); - - /** - * @notice Defines the address of the entity issuing attestations to the subject - * @dev We strongly encourage a reflection when implementing this method - */ - function getAttester() external view returns (address); } diff --git a/contracts/src/stdlib/IndexerModule.sol b/contracts/src/stdlib/IndexerModule.sol deleted file mode 100644 index a30b6f5c..00000000 --- a/contracts/src/stdlib/IndexerModule.sol +++ /dev/null @@ -1,188 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.21; - -import { AbstractModule } from "../abstracts/AbstractModule.sol"; -import { AbstractPortal } from "../abstracts/AbstractPortal.sol"; -import { AttestationPayload, Attestation } from "../types/Structs.sol"; -import { Router } from "../Router.sol"; -import { AttestationRegistry } from "../AttestationRegistry.sol"; - -/** - * @title IndexerModule - * @dev The contract is responsible for indexing attestations by various parameters. - * It allows for efficient retrieval of attestations based on these parameters. - */ -contract IndexerModule is AbstractModule { - Router public router; - - mapping(bytes subject => bytes32[] attestationIds) private attestationIdsBySubject; - mapping(bytes subject => mapping(bytes32 schemaId => bytes32[] attestationIds)) - private attestationIdsBySubjectBySchema; - mapping(address attester => bytes32[] attestationIds) private attestationIdsByAttester; - mapping(bytes32 schema => bytes32[] attestationIds) private attestationIdsBySchema; - mapping(address portal => bytes32[] attestationIds) private attestationIdsByPortal; - mapping(address portal => mapping(bytes subject => bytes32[] attestationIds)) private attestationIdsByPortalBySubject; - mapping(bytes32 attestationId => bool status) private indexedAttestations; - - /** - * @dev Emitted when an attestation is indexed. - */ - event AttestationIndexed(bytes32 attestationId); - - /** - * @dev Contract constructor sets the router. - * @param _router The address of the router contract. - */ - constructor(address _router) { - router = Router(_router); - } - - /** - * @dev The main method for the module, running the check. - * @param _attestationPayload The payload of the attestation. - */ - function run( - AttestationPayload memory _attestationPayload, - bytes memory /*_validationPayload*/, - address /*_txSender*/, - uint256 /*_value*/ - ) public override { - Attestation memory attestation = _buildAttestation(_attestationPayload); - _indexAttestation(attestation); - } - - /** - * @dev Indexes an attestation. - * @param attestationId The ID of the attestation to index. - */ - function indexAttestation(bytes32 attestationId) public { - AttestationRegistry attestationRegistry = AttestationRegistry(router.getAttestationRegistry()); - Attestation memory attestation = attestationRegistry.getAttestation(attestationId); - _indexAttestation(attestation); - } - - /** - * @dev Indexes multiple attestations. - * @param attestationIds An array of attestation IDs to index. - */ - function indexAttestations(bytes32[] calldata attestationIds) external { - uint256 length = attestationIds.length; - for (uint256 i = 0; i < length; i++) { - indexAttestation(attestationIds[i]); - } - } - - /** - * @dev Returns the attestation IDs for a given subject. - * @param subject The subject to retrieve attestation IDs for. - * @return An array of attestation IDs. - */ - function getAttestationIdsBySubject(bytes calldata subject) external view returns (bytes32[] memory) { - return attestationIdsBySubject[subject]; - } - - /** - * @dev Returns the attestation IDs for a given subject and schema. - * @param subject The subject to retrieve attestation IDs for. - * @param schemaId The schema ID to retrieve attestation IDs for. - * @return An array of attestation IDs. - */ - function getAttestationIdsBySubjectBySchema( - bytes calldata subject, - bytes32 schemaId - ) external view returns (bytes32[] memory) { - return attestationIdsBySubjectBySchema[subject][schemaId]; - } - - /** - * @dev Returns the attestation IDs for a given attester. - * @param attester The attester to retrieve attestation IDs for. - * @return An array of attestation IDs. - */ - function getAttestationIdsByAttester(address attester) external view returns (bytes32[] memory) { - return attestationIdsByAttester[attester]; - } - - /** - * @dev Returns the attestation IDs for a given schema. - * @param schema The schema to retrieve attestation IDs for. - * @return An array of attestation IDs. - */ - function getAttestationIdsBySchema(bytes32 schema) external view returns (bytes32[] memory) { - return attestationIdsBySchema[schema]; - } - - /** - * @dev Returns the attestation IDs for a given portal. - * @param portal The portal to retrieve attestation IDs for. - * @return An array of attestation IDs. - */ - function getAttestationIdsByPortal(address portal) external view returns (bytes32[] memory) { - return attestationIdsByPortal[portal]; - } - - /** - * @dev Returns the attestation IDs for a given portal and subject. - * @param portal The portal to retrieve attestation IDs for. - * @param subject The subject to retrieve attestation IDs for. - * @return An array of attestation IDs. - */ - function getAttestationIdsByPortalBySubject( - address portal, - bytes calldata subject - ) external view returns (bytes32[] memory) { - return attestationIdsByPortalBySubject[portal][subject]; - } - - /** - * @dev Returns the indexed status of an attestation. - * @param attestationId The ID of the attestation to check. - * @return The indexed status of the attestation. - */ - function getIndexedAttestationStatus(bytes32 attestationId) external view returns (bool) { - return indexedAttestations[attestationId]; - } - - /** - * @dev Indexes an attestation. - * @param attestation The attestation to index. - */ - function _indexAttestation(Attestation memory attestation) internal { - if (indexedAttestations[attestation.attestationId]) { - return; - } - attestationIdsBySubject[attestation.subject].push(attestation.attestationId); - attestationIdsBySubjectBySchema[attestation.subject][attestation.schemaId].push(attestation.attestationId); - attestationIdsByAttester[attestation.attester].push(attestation.attestationId); - attestationIdsBySchema[attestation.schemaId].push(attestation.attestationId); - attestationIdsByPortal[attestation.portal].push(attestation.attestationId); - attestationIdsByPortalBySubject[attestation.portal][attestation.subject].push(attestation.attestationId); - indexedAttestations[attestation.attestationId] = true; - - emit AttestationIndexed(attestation.attestationId); - } - - /** - * @dev Builds an attestation. - * @param attestationPayload The payload of the attestation. - * @return The built attestation. - */ - function _buildAttestation(AttestationPayload memory attestationPayload) internal view returns (Attestation memory) { - AttestationRegistry attestationRegistry = AttestationRegistry(router.getAttestationRegistry()); - return - Attestation( - bytes32(abi.encode(attestationRegistry.getAttestationIdCounter() + 1)), - attestationPayload.schemaId, - bytes32(0), - AbstractPortal(msg.sender).getAttester(), - msg.sender, - uint64(block.timestamp), - attestationPayload.expirationDate, - 0, - attestationRegistry.getVersionNumber(), - false, - attestationPayload.subject, - attestationPayload.attestationData - ); - } -} diff --git a/contracts/test/stdlib/IndexerModule.t.sol b/contracts/test/stdlib/IndexerModule.t.sol deleted file mode 100644 index 2a3c3dad..00000000 --- a/contracts/test/stdlib/IndexerModule.t.sol +++ /dev/null @@ -1,135 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.21; - -import { Test } from "forge-std/Test.sol"; -import { ECDSA } from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol"; - -import { IndexerModule } from "../../src/stdlib/IndexerModule.sol"; -import { AttestationPayload } from "../../src/types/Structs.sol"; -import { PortalRegistryMock } from "../mocks/PortalRegistryMock.sol"; -import { AttestationRegistryMock } from "../mocks/AttestationRegistryMock.sol"; -import { SchemaRegistryMock } from "../mocks/SchemaRegistryMock.sol"; -import { Router } from "../../src/Router.sol"; -import { ValidPortalMock } from "../mocks/ValidPortalMock.sol"; - -contract IndexerModuleTest is Test { - using ECDSA for bytes32; - - IndexerModule private indexerModule; - AttestationPayload private attestationPayload; - PortalRegistryMock private portalRegistry; - AttestationRegistryMock private attestationRegistry; - SchemaRegistryMock private schemaRegistry; - Router private router; - - address private user = makeAddr("user"); - address private portal = makeAddr("portal"); - address private portalOwner = makeAddr("portalOwner"); - - AttestationPayload private payload1; - AttestationPayload private payload2; - AttestationPayload private payload3; - - event AttestationIndexed(bytes32 attestationId); - - function setUp() public { - vm.startPrank(portalOwner); - // add portal to mock registry - portalRegistry = new PortalRegistryMock(); - portalRegistry.register(address(portal), "portal", "portal", false, "portal"); - - attestationRegistry = new AttestationRegistryMock(); - - router = new Router(); - router.initialize(); - router.updatePortalRegistry(address(portalRegistry)); - router.updateAttestationRegistry(address(attestationRegistry)); - router.updateSchemaRegistry(address(schemaRegistry)); - - payload1 = AttestationPayload(bytes32("1"), 0, bytes(""), bytes("")); - payload2 = AttestationPayload(bytes32("1"), 0, bytes(""), bytes("")); - payload3 = AttestationPayload(bytes32("2"), 0, bytes(""), bytes("")); - - attestationRegistry.attest(payload1, user); - attestationRegistry.attest(payload2, user); - - indexerModule = new IndexerModule(address(router)); - - indexerModule.indexAttestation(bytes32(abi.encode(1))); - - vm.stopPrank(); - } - - function test_run() public { - address[] memory modules = new address[](0); - ValidPortalMock validPortal = new ValidPortalMock(modules, address(router)); - vm.prank(address(validPortal)); - vm.expectEmit({ emitter: address(indexerModule) }); - emit AttestationIndexed(bytes32(abi.encode(3))); - indexerModule.run(payload3, bytes(""), address(0), 0); - assertEq(indexerModule.getIndexedAttestationStatus(bytes32(abi.encode(3))), true); - } - - function test_indexAttestation() public { - vm.expectEmit({ emitter: address(indexerModule) }); - emit AttestationIndexed(bytes32(abi.encode(2))); - indexerModule.indexAttestation(bytes32(abi.encode(2))); - } - - function test_indexAttestations() public { - bytes32[] memory attestationIds = new bytes32[](2); - attestationIds[0] = bytes32(abi.encode(1)); - attestationIds[1] = bytes32(abi.encode(2)); - indexerModule.indexAttestations(attestationIds); - assertEq(indexerModule.getIndexedAttestationStatus(attestationIds[0]), true); - assertEq(indexerModule.getIndexedAttestationStatus(attestationIds[1]), true); - } - - function test_indexAttestation_CannotBeIndexedTwice() public { - vm.expectEmit({ emitter: address(indexerModule) }); - emit AttestationIndexed(bytes32(abi.encode(2))); - indexerModule.indexAttestation(bytes32(abi.encode(2))); - indexerModule.indexAttestation(bytes32(abi.encode(2))); - - bytes32[] memory attestationIds = indexerModule.getAttestationIdsBySubject(payload1.subject); - assertEq(attestationIds.length, 2); - } - - function test_getAttestationIdsBySubject() public view { - bytes32[] memory attestationIds = indexerModule.getAttestationIdsBySubject(payload1.subject); - assertEq(attestationIds[0], bytes32(abi.encode(1))); - } - - function test_getAttestationIdsBySubjectBySchema() public view { - bytes32[] memory attestationIds = indexerModule.getAttestationIdsBySubjectBySchema( - payload1.subject, - payload1.schemaId - ); - assertEq(attestationIds[0], bytes32(abi.encode(1))); - } - - function test_getAttestationIdsByAttester() public view { - bytes32[] memory attestationIds = indexerModule.getAttestationIdsByAttester(user); - assertEq(attestationIds[0], bytes32(abi.encode(1))); - } - - function test_getAttestationIdsBySchema() public view { - bytes32[] memory attestationIds = indexerModule.getAttestationIdsBySchema(payload1.schemaId); - assertEq(attestationIds[0], bytes32(abi.encode(1))); - } - - function test_getAttestationIdsByPortal() public view { - bytes32[] memory attestationIds = indexerModule.getAttestationIdsByPortal(portalOwner); - assertEq(attestationIds[0], bytes32(abi.encode(1))); - } - - function test_getAttestationByPortalBySubject() public view { - bytes32[] memory attestationIds = indexerModule.getAttestationIdsByPortalBySubject(portalOwner, payload1.subject); - assertEq(attestationIds[0], bytes32(abi.encode(1))); - } - - function test_getIndexedAttestationStatus() public view { - bool status = indexerModule.getIndexedAttestationStatus(bytes32(abi.encode(1))); - assertEq(status, true); - } -}