Skip to content

Commit

Permalink
fix: encoding to have meaningful tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yum0e authored and leosayous21 committed Dec 6, 2022
1 parent bc834ab commit 4caedb3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 47 deletions.
88 changes: 42 additions & 46 deletions test/unit/attesters/hydra-s1/hydra-s1-accountbound-attester.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,12 @@ describe('Test HydraS1 Accountbound Attester contract', () => {
expect(buildAttestations[0].issuer).to.eql(hydraS1AccountboundAttester.address);
expect(buildAttestations[0].value.toNumber()).to.eql(sourceValue.toNumber());
expect(buildAttestations[0].timestamp).to.eql(group.properties.generationTimestamp);
expect(
utils.defaultAbiCoder.decode(['bytes', 'uint16'], buildAttestations[0].extraData)
).to.eql([BigNumber.from(inputs.publicInputs.nullifier)._hex, 0]);
expect(buildAttestations[0].extraData).to.eql(
encodeAccountBoundAttestationExtraData({
nullifier: inputs.publicInputs.nullifier,
burnCount: 0,
})
);
});
});

Expand Down Expand Up @@ -562,13 +565,6 @@ describe('Test HydraS1 Accountbound Attester contract', () => {
const generateAttestationsTransaction =
await hydraS1AccountboundAttester.generateAttestations(request, proof.toBytes());

const attestationsExtraData = await attestationsRegistry.getAttestationExtraData(
(
await hydraS1AccountboundAttester.AUTHORIZED_COLLECTION_ID_FIRST()
).add(group.properties.groupIndex),
request.destination
);

// 0 - Checks that the transaction emitted the event
await expect(generateAttestationsTransaction)
.to.emit(hydraS1AccountboundAttester, 'AttestationGenerated')
Expand All @@ -580,7 +576,10 @@ describe('Test HydraS1 Accountbound Attester contract', () => {
hydraS1AccountboundAttester.address,
sourceValue,
group.properties.generationTimestamp,
attestationsExtraData,
encodeAccountBoundAttestationExtraData({
nullifier: inputs.publicInputs.nullifier,
burnCount: 0,
}),
]);

// 1 - Checks that the provided nullifier was successfully recorded in the attester
Expand Down Expand Up @@ -621,12 +620,12 @@ describe('Test HydraS1 Accountbound Attester contract', () => {
destinationSigner.address
);

// expect(attestationExtraData).to.be.eql(
// encodeAccountBoundAttestationExtraData({
// nullifier: inputs.publicInputs.nullifier,
// burnCount: 0,
// })
// );
expect(attestationExtraData).to.be.eql(
encodeAccountBoundAttestationExtraData({
nullifier: inputs.publicInputs.nullifier,
burnCount: 0,
})
);

expect(
await hydraS1AccountboundAttester.getNullifierFromExtraData(
Expand Down Expand Up @@ -686,14 +685,6 @@ describe('Test HydraS1 Accountbound Attester contract', () => {
const generateAttestationsTransaction =
await hydraS1AccountboundAttester.generateAttestations(newRequest, newProof.toBytes());

// burnCount should be recorded in the attestationsRegistry
const attestationsExtraData = await attestationsRegistry.getAttestationExtraData(
(
await hydraS1AccountboundAttester.AUTHORIZED_COLLECTION_ID_FIRST()
).add(group.properties.groupIndex),
newRequest.destination
);

// 0 - Checks that the transaction emitted the event
await expect(generateAttestationsTransaction)
.to.emit(hydraS1AccountboundAttester, 'AttestationGenerated')
Expand All @@ -705,7 +696,10 @@ describe('Test HydraS1 Accountbound Attester contract', () => {
hydraS1AccountboundAttester.address,
sourceValue,
group.properties.generationTimestamp,
attestationsExtraData,
encodeAccountBoundAttestationExtraData({
nullifier: inputs.publicInputs.nullifier,
burnCount: 1, // burn count should be incremented
}),
]);
await expect(generateAttestationsTransaction)
.to.emit(hydraS1AccountboundAttester, 'NullifierDestinationUpdated')
Expand Down Expand Up @@ -733,10 +727,20 @@ describe('Test HydraS1 Accountbound Attester contract', () => {
)
).to.be.eql(1); // the burnCount should be incremented

expect(utils.defaultAbiCoder.decode(['bytes', 'uint16'], attestationsExtraData)).to.eql([
BigNumber.from(inputs.publicInputs.nullifier)._hex,
1,
]);
// burnCount should be recorded in the attestationsRegistry
expect(
await attestationsRegistry.getAttestationExtraData(
(
await hydraS1AccountboundAttester.AUTHORIZED_COLLECTION_ID_FIRST()
).add(group.properties.groupIndex),
destination2Signer.address
)
).to.equal(
encodeAccountBoundAttestationExtraData({
nullifier: inputs.publicInputs.nullifier,
burnCount: 1, // burnCount should be incremented
})
);
// 2 - Checks that the attester unrecorded & rerecorded the attestation in the registry
// 2.1 - Checks that the old destination has not anymore it's attestation
expect(
Expand Down Expand Up @@ -821,13 +825,6 @@ describe('Test HydraS1 Accountbound Attester contract', () => {
const generateAttestationsTransaction =
await hydraS1AccountboundAttester.generateAttestations(renewRequest, renewProof.toBytes());

const attestationsExtraData = await attestationsRegistry.getAttestationExtraData(
(
await hydraS1AccountboundAttester.AUTHORIZED_COLLECTION_ID_FIRST()
).add(group.properties.groupIndex),
renewRequest.destination
);

// 0 - Checks that the transaction emitted the event with the new timestamp
await expect(generateAttestationsTransaction)
.to.emit(hydraS1AccountboundAttester, 'AttestationGenerated')
Expand All @@ -839,7 +836,10 @@ describe('Test HydraS1 Accountbound Attester contract', () => {
hydraS1AccountboundAttester.address,
sourceValue,
renewGenerationTimestamp,
attestationsExtraData,
encodeAccountBoundAttestationExtraData({
nullifier: inputs.publicInputs.nullifier,
burnCount: 1,
}),
]);

// A renew should not have changed the nullifierData
Expand Down Expand Up @@ -912,13 +912,6 @@ describe('Test HydraS1 Accountbound Attester contract', () => {
const generateAttestationsTransaction =
await hydraS1AccountboundAttester.generateAttestations(renewRequest, renewProof.toBytes());

const attestationsExtraData = await attestationsRegistry.getAttestationExtraData(
(
await hydraS1AccountboundAttester.AUTHORIZED_COLLECTION_ID_FIRST()
).add(group.properties.groupIndex),
renewRequest.destination
);

// Checks that the transaction emitted the event with a claimedValue of zero
await expect(generateAttestationsTransaction)
.to.emit(hydraS1AccountboundAttester, 'AttestationGenerated')
Expand All @@ -930,7 +923,10 @@ describe('Test HydraS1 Accountbound Attester contract', () => {
hydraS1AccountboundAttester.address,
0, // claimedValue
group.properties.generationTimestamp,
attestationsExtraData,
encodeAccountBoundAttestationExtraData({
nullifier: inputs.publicInputs.nullifier,
burnCount: 1,
}),
]);

// Checks that the attestation in the registry has a value of zero
Expand Down
5 changes: 4 additions & 1 deletion test/utils/hydra-s1-accountbound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,8 @@ export const encodeAccountBoundAttestationExtraData = ({
nullifier: BigNumberish | BigInt;
burnCount: number;
}) => {
return ethers.utils.solidityPack(['uint256', 'uint16'], [nullifier, burnCount]);
return ethers.utils.defaultAbiCoder.encode(
['bytes', 'uint16'],
[ethers.utils.defaultAbiCoder.encode(['uint256'], [nullifier]), burnCount]
);
};

0 comments on commit 4caedb3

Please sign in to comment.