Skip to content

Commit

Permalink
feat: add index attestations by portal by subject (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xEillo authored Nov 8, 2023
1 parent d59b60a commit 55a26b3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
32 changes: 25 additions & 7 deletions contracts/src/stdlib/IndexerModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ contract IndexerModule is AbstractModule {
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;

/**
Expand Down Expand Up @@ -76,7 +77,7 @@ contract IndexerModule is AbstractModule {
* @param subject The subject to retrieve attestation IDs for.
* @return An array of attestation IDs.
*/
function getAttestationIdsBySubject(bytes memory subject) public view returns (bytes32[] memory) {
function getAttestationIdsBySubject(bytes memory subject) external view returns (bytes32[] memory) {
return attestationIdsBySubject[subject];
}

Expand All @@ -89,7 +90,7 @@ contract IndexerModule is AbstractModule {
function getAttestationIdsBySubjectBySchema(
bytes memory subject,
bytes32 schemaId
) public view returns (bytes32[] memory) {
) external view returns (bytes32[] memory) {
return attestationIdsBySubjectBySchema[subject][schemaId];
}

Expand All @@ -98,7 +99,7 @@ contract IndexerModule is AbstractModule {
* @param attester The attester to retrieve attestation IDs for.
* @return An array of attestation IDs.
*/
function getAttestationIdsByAttester(address attester) public view returns (bytes32[] memory) {
function getAttestationIdsByAttester(address attester) external view returns (bytes32[] memory) {
return attestationIdsByAttester[attester];
}

Expand All @@ -107,7 +108,7 @@ contract IndexerModule is AbstractModule {
* @param schema The schema to retrieve attestation IDs for.
* @return An array of attestation IDs.
*/
function getAttestationIdsBySchema(bytes32 schema) public view returns (bytes32[] memory) {
function getAttestationIdsBySchema(bytes32 schema) external view returns (bytes32[] memory) {
return attestationIdsBySchema[schema];
}

Expand All @@ -116,16 +117,29 @@ contract IndexerModule is AbstractModule {
* @param portal The portal to retrieve attestation IDs for.
* @return An array of attestation IDs.
*/
function getAttestationIdsByPortal(address portal) public view returns (bytes32[] memory) {
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 memory 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) public view returns (bool) {
function getIndexedAttestationStatus(bytes32 attestationId) external view returns (bool) {
return indexedAttestations[attestationId];
}

Expand All @@ -134,11 +148,15 @@ contract IndexerModule is AbstractModule {
* @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);
Expand All @@ -149,7 +167,7 @@ contract IndexerModule is AbstractModule {
* @param attestationPayload The payload of the attestation.
* @return The built attestation.
*/
function _buildAttestation(AttestationPayload memory attestationPayload) public view returns (Attestation memory) {
function _buildAttestation(AttestationPayload memory attestationPayload) internal view returns (Attestation memory) {
AttestationRegistry attestationRegistry = AttestationRegistry(router.getAttestationRegistry());
return
Attestation(
Expand Down
15 changes: 15 additions & 0 deletions contracts/test/stdlib/IndexerModule.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ contract IndexerModuleTest is Test {
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 {
bytes32[] memory attestationIds = indexerModule.getAttestationIdsBySubject(payload1.subject);
assertEq(attestationIds[0], bytes32(abi.encode(1)));
Expand Down Expand Up @@ -113,6 +123,11 @@ contract IndexerModuleTest is Test {
assertEq(attestationIds[0], bytes32(abi.encode(1)));
}

function test_getAttestationByPortalBySubject() public {
bytes32[] memory attestationIds = indexerModule.getAttestationIdsByPortalBySubject(portalOwner, payload1.subject);
assertEq(attestationIds[0], bytes32(abi.encode(1)));
}

function test_getIndexedAttestationStatus() public {
bool status = indexerModule.getIndexedAttestationStatus(bytes32(abi.encode(1)));
assertEq(status, true);
Expand Down

0 comments on commit 55a26b3

Please sign in to comment.