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

feat(contracts): Fix Attestation ID generation in examples #797

Merged
merged 3 commits into from
Nov 14, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions contracts/src/AttestationRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,12 @@ contract AttestationRegistry is OwnableUpgradeable {
// Combine the chain prefix and the ID
return bytes32(abi.encode(chainPrefix + id));
}

/**
* @notice Get the next attestation ID including chain identifier
* @return The next attestation ID
*/
function getNextAttestationId() public view returns (bytes32) {
return generateAttestationId(attestationIdCounter + 1);
}
}
4 changes: 1 addition & 3 deletions contracts/src/examples/portals/EASPortal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ contract EASPortal is AbstractPortal {
if (!attestationRegistry.isRegistered(attestationRequest.data.refUID)) {
revert ReferenceAttestationNotRegistered();
}

uint32 attestationIdCounter = attestationRegistry.getAttestationIdCounter();
bytes32 attestationId = bytes32(abi.encode(attestationIdCounter));
bytes32 attestationId = attestationRegistry.getNextAttestationId();

AttestationPayload memory relationshipAttestationPayload = AttestationPayload(
_relationshipSchemaId,
Expand Down
3 changes: 1 addition & 2 deletions contracts/src/stdlib/IndexerModuleV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ contract IndexerModuleV2 is AbstractModuleV2 {
AttestationRegistry attestationRegistry = AttestationRegistry(router.getAttestationRegistry());
return
Attestation(
// TODO: Consider the chain prefix to generate the attestation ID
bytes32(abi.encode(attestationRegistry.getAttestationIdCounter() + 1)),
attestationRegistry.getNextAttestationId(),
attestationPayload.schemaId,
bytes32(0),
attester,
Expand Down
23 changes: 23 additions & 0 deletions contracts/test/AttestationRegistry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,29 @@ contract AttestationRegistryTest is Test {
assertEq(attestationId, 0x000300000000000000000000000000000000000000000000000000000000000a);
}

function test_getNextAttestationId(AttestationPayload memory attestationPayload) public {
vm.assume(attestationPayload.subject.length != 0);
vm.assume(attestationPayload.attestationData.length != 0);
SchemaRegistryMock schemaRegistryMock = SchemaRegistryMock(router.getSchemaRegistry());
attestationPayload.schemaId = schemaRegistryMock.getIdFromSchemaString("schemaString");
schemaRegistryMock.createSchema("name", "description", "context", "schemaString");
bytes32 nextAttestationId = attestationRegistry.getNextAttestationId();

bytes32 expectedNextAttestationId = bytes32(abi.encode(initialChainPrefix + 1));
assertEq(nextAttestationId, expectedNextAttestationId);

vm.startPrank(portal);

attestationRegistry.attest(attestationPayload, attester);

expectedNextAttestationId = bytes32(abi.encode(initialChainPrefix + 2));
nextAttestationId = attestationRegistry.getNextAttestationId();

assertEq(nextAttestationId, expectedNextAttestationId);

vm.stopPrank();
}

function _createAttestation(
AttestationPayload memory attestationPayload,
uint256 id
Expand Down
2 changes: 1 addition & 1 deletion contracts/test/examples/portals/EASPortal.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ contract EASPortalTest is Test {
emit AttestationRegistered();
easPortal.attest(attestationRequestWithoutRefUID);

bytes32 attestationIdWithoutRefUID = bytes32(abi.encode(1));
bytes32 attestationIdWithoutRefUID = attestationRegistryMock.getNextAttestationId();

// Create second EAS attestation request with RefUID
EASPortal.AttestationRequestData memory attestationRequestDataWithRefUID = EASPortal.AttestationRequestData(
Expand Down
9 changes: 9 additions & 0 deletions contracts/test/mocks/AttestationRegistryMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,13 @@ contract AttestationRegistryMock {
function getAttestation(bytes32 attestationId) public view returns (Attestation memory) {
return attestations[attestationId];
}

function generateAttestationId(uint256 id) internal view returns (bytes32) {
// This is a mock implementation, 1000 is considered as chain prefix
return bytes32(abi.encode(1000 + id));
}

function getNextAttestationId() public view returns (bytes32) {
return generateAttestationId(attestationIdCounter + 1);
}
}
5 changes: 3 additions & 2 deletions contracts/test/stdlib/IndexerModuleV2.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ contract IndexerModuleV2Test is Test {

function test_run() public {
address[] memory modules = new address[](0);
bytes32 expectedAttestationId = attestationRegistry.getNextAttestationId();
ValidPortalMock validPortal = new ValidPortalMock(modules, address(router));
vm.prank(address(validPortal));
vm.expectEmit({ emitter: address(indexerModule) });
emit AttestationIndexed(bytes32(abi.encode(3)));
emit AttestationIndexed(expectedAttestationId);
indexerModule.run(
payload3,
bytes(""),
Expand All @@ -76,7 +77,7 @@ contract IndexerModuleV2Test is Test {
address(validPortal),
OperationType.Attest
);
assertEq(indexerModule.getIndexedAttestationStatus(bytes32(abi.encode(3))), true);
assertEq(indexerModule.getIndexedAttestationStatus(expectedAttestationId), true);
}

function test_indexAttestation() public {
Expand Down
Loading