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

fix(resolver): fix recipient variable used in test #47

Closed
wants to merge 5 commits into from
Closed
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
21 changes: 17 additions & 4 deletions contracts/GitcoinResolver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@ contract GitcoinResolver is
function attest(
Attestation calldata attestation
) external payable whenNotPaused onlyAllowlisted returns (bool) {
if (scoreSchema == attestation.schema) {
_setScore(attestation);
}

return _attest(attestation);
}

Expand All @@ -137,6 +133,9 @@ contract GitcoinResolver is
userAttestations[attestation.recipient][attestation.schema] = attestation
.uid;

if (scoreSchema == attestation.schema) {
_setScore(attestation);
}
return true;
}

Expand Down Expand Up @@ -164,6 +163,14 @@ contract GitcoinResolver is
);
}

/**
* @dev Removes the score data from the state for the specified recipient.
* @param recipient The recipient of the score which needs to be removed.
*/
function _removeScore(address recipient) private {
delete scores[recipient];
}

/**
*
* @param user The ETH address of the recipient
Expand Down Expand Up @@ -228,8 +235,14 @@ contract GitcoinResolver is
return true;
}

/**
* @dev Processes an revocation request
* @param attestation The new attestation request.
* @return true indicating if the pre-revocation have been performed and the revocation process should continue
*/
function _revoke(Attestation calldata attestation) internal returns (bool) {
userAttestations[attestation.recipient][attestation.schema] = 0;
_removeScore(attestation.recipient);

return true;
}
Expand Down
143 changes: 129 additions & 14 deletions test/GitcoinResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { GitcoinAttester, GitcoinResolver } from "../typechain-types";
import { encodedData, getScoreAttestation } from "./helpers/mockAttestations";
import { SCHEMA_REGISTRY_ABI } from "./abi/SCHEMA_REGISTRY_ABI";
import { AttestationStruct } from "../typechain-types/contracts/GitcoinResolver";
import { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers";

export const schemaRegistryContractAddress =
process.env.SEPOLIA_SCHEMA_REGISTRY_ADDRESS ||
Expand Down Expand Up @@ -48,22 +49,30 @@ describe("GitcoinResolver", function () {
nonOwnerOrVerifier: any,
mockEas: any,
gitcoinResolver: GitcoinResolver,
gitcoinAttester: GitcoinAttester;
gitcoinAttester: GitcoinAttester,
recipients: HardhatEthersSigner[],
nextRecipientIndex: number = 0;

this.beforeEach(async function () {
// Make sure to use a different recipient for each test
recipient = recipients[nextRecipientIndex++];
});

before(async function () {
const [
ownerAccount,
otherAccount,
recipientAccount,
mockEasContractAccount,
nonOwnerOrVerifierAccount
_ownerAccount,
_iamAccount,
_recipientAccount,
_mockEasContractAccount,
_nonOwnerOrVerifierAccount,
...moreSigners
] = await ethers.getSigners();

owner = ownerAccount;
iamAccount = otherAccount;
recipient = recipientAccount;
mockEas = mockEasContractAccount;
nonOwnerOrVerifier = nonOwnerOrVerifierAccount;
owner = _ownerAccount;
iamAccount = _iamAccount;
mockEas = _mockEasContractAccount;
nonOwnerOrVerifier = _nonOwnerOrVerifierAccount;
recipients = moreSigners;

const GitcoinAttester = await ethers.getContractFactory(
"GitcoinAttester",
Expand Down Expand Up @@ -109,7 +118,7 @@ describe("GitcoinResolver", function () {
expirationTime: NO_EXPIRATION,
revocationTime: NO_EXPIRATION,
refUID: ZERO_BYTES32,
recipient: recipient.address,
recipient: _recipientAccount.address,
attester: this.gitcoinAttesterAddress,
revocable: true,
data: encodedData
Expand All @@ -121,7 +130,7 @@ describe("GitcoinResolver", function () {
await gitcoinResolver.connect(mockEas).attest(this.validAttestation);

const attestationUID = await gitcoinResolver.userAttestations(
recipient.address,
this.validAttestation.recipient,
this.testSchemaUID
);

Expand All @@ -137,7 +146,7 @@ describe("GitcoinResolver", function () {
);

const attestationUID = await gitcoinResolver.userAttestations(
recipient.address,
this.validAttestation.recipient,
this.testSchemaUID
);

Expand Down Expand Up @@ -258,6 +267,112 @@ describe("GitcoinResolver", function () {
expect(score[1]).to.equal("200500");
expect(score[2]).to.equal("700500");
});

it("should cache a score from an attestation that is part of a multiAttest call", async function () {
const attestation = getScoreAttestation(
{
schema: this.scoreSchemaId,
recipient: recipient.address,
attester: this.gitcoinAttesterAddress
},
{
score: "98765", // That is 12.34 (2 decimals)
scorer_id: 3,
score_decimals: 4
}
) as AttestationStruct;

await gitcoinResolver
.connect(mockEas)
.multiAttest([this.validAttestation, attestation], []);

const score = await gitcoinResolver.getCachedScore(recipient.address);

// Score should have been casted to a 4 digit value
expect(score[0]).to.equal("98765");
});

it("should remove a cached score if the attestation is revoked (attest and revoke calls)", async function () {
const attestation = getScoreAttestation(
{
schema: this.scoreSchemaId,
recipient: recipient.address,
attester: this.gitcoinAttesterAddress,
time: 1234,
expirationTime: 5678
},
{
score: "98765", // That is 98.7650
scorer_id: 3,
score_decimals: 4
}
) as AttestationStruct;

await gitcoinResolver.connect(mockEas).attest(attestation);

const scoreBeforeRevocation = await gitcoinResolver.getCachedScore(
recipient.address
);

// Score should have been casted to a 4 digit value
expect(scoreBeforeRevocation[0]).to.equal("98765");
expect(scoreBeforeRevocation[1]).to.equal("1234");
expect(scoreBeforeRevocation[2]).to.equal("5678");

await gitcoinResolver.connect(mockEas).revoke(attestation);

const scoreAfterRevocation = await gitcoinResolver.getCachedScore(
recipient.address
);

// Score should have been casted to a 4 digit value
expect(scoreAfterRevocation[0]).to.equal("0");
expect(scoreAfterRevocation[1]).to.equal("0");
expect(scoreAfterRevocation[2]).to.equal("0");
});

it("should remove a cached score if the attestation is revoked (multiAttest and multiRevoke calls)", async function () {
const attestation = getScoreAttestation(
{
schema: this.scoreSchemaId,
recipient: recipient.address,
attester: this.gitcoinAttesterAddress,
time: 1234,
expirationTime: 5678
},
{
score: "983855", // That is 98.3855
scorer_id: 3,
score_decimals: 4
}
) as AttestationStruct;

await gitcoinResolver
.connect(mockEas)
.multiAttest([attestation, this.validAttestation], []);

const scoreBeforeRevocation = await gitcoinResolver.getCachedScore(
recipient.address
);

// Score should have been casted to a 4 digit value
expect(scoreBeforeRevocation[0]).to.equal("983855");
expect(scoreBeforeRevocation[1]).to.equal("1234");
expect(scoreBeforeRevocation[2]).to.equal("5678");

await gitcoinResolver
.connect(mockEas)
.multiRevoke([attestation, this.validAttestation], []);

const scoreAfterRevocation = await gitcoinResolver.getCachedScore(
recipient.address
);

// Score should have been casted to a 4 digit value
expect(scoreAfterRevocation[0]).to.equal("0");
expect(scoreAfterRevocation[1]).to.equal("0");
expect(scoreAfterRevocation[2]).to.equal("0");
});
});

describe("Revocations", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/mockAttestations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const easEncodeScore = (score: Score) => {
};

export const getScoreAttestation = (
attestion: Pick<Attestation, "attester" | "recipient" | "schema">,
attestion: Pick<Attestation, "attester" | "recipient" | "schema"> & Partial<Attestation>,
score: Score
): Attestation => {
const scoreEncodedData = easEncodeScore(score);
Expand Down