Skip to content

Commit

Permalink
fix: more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroknots committed Feb 21, 2024
1 parent 629c25f commit 7ca7186
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 22 deletions.
7 changes: 3 additions & 4 deletions src/core/TrustManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ abstract contract TrustManager is IRegistry {
address attester = $trustedAttesters.attester;

// smart account has no trusted attesters set
if (attester == ZERO_ADDRESS && threshold != 0) {
if (attester == ZERO_ADDRESS || threshold != 0) {
revert NoTrustedAttestersFound();
}
// smart account only has ONE trusted attester
Expand All @@ -119,13 +119,12 @@ abstract contract TrustManager is IRegistry {
else {
// loop though list and check if the attestation is valid
AttestationRecord storage $attestation = $getAttestation({ module: module, attester: attester });

Check warning on line 121 in src/core/TrustManager.sol

View check run for this annotation

Codecov / codecov/patch

src/core/TrustManager.sol#L121

Added line #L121 was not covered by tests
$attestation.enforceValid(moduleType);
if ($attestation.checkValid(moduleType)) threshold--;
for (uint256 i = 1; i < attesterCount; i++) {

Check warning on line 123 in src/core/TrustManager.sol

View check run for this annotation

Codecov / codecov/patch

src/core/TrustManager.sol#L123

Added line #L123 was not covered by tests
threshold--;
// get next attester from linked List
attester = $trustedAttesters.linkedAttesters[attester];
$attestation = $getAttestation({ module: module, attester: attester });

Check warning on line 126 in src/core/TrustManager.sol

View check run for this annotation

Codecov / codecov/patch

src/core/TrustManager.sol#L125-L126

Added lines #L125 - L126 were not covered by tests
$attestation.enforceValid(moduleType);
if ($attestation.checkValid(moduleType)) threshold--;
// if threshold reached, exit loop
if (threshold == 0) return;
}
Expand Down
14 changes: 8 additions & 6 deletions src/core/TrustManagerExternalAttesterList.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ abstract contract TrustManagerExternalAttesterList is IRegistry, TrustManager {
if (attestersLength < threshold || threshold == 0) threshold = attestersLength;

for (uint256 i; i < attestersLength; ++i) {
$getAttestation(module, attesters[i]).enforceValid(ZERO_MODULE_TYPE);
if (threshold != 0) --threshold;
if ($getAttestation(module, attesters[i]).checkValid(ZERO_MODULE_TYPE)) {
--threshold;
}
if (threshold == 0) return;
}
if (threshold == 0) return;
revert InsufficientAttestations();
}

Expand All @@ -52,10 +53,11 @@ abstract contract TrustManagerExternalAttesterList is IRegistry, TrustManager {
if (attestersLength < threshold || threshold == 0) threshold = attestersLength;

for (uint256 i; i < attestersLength; ++i) {
$getAttestation(module, attesters[i]).enforceValid(moduleType);
if (threshold != 0) --threshold;
if ($getAttestation(module, attesters[i]).checkValid(moduleType)) {
--threshold;
}
if (threshold == 0) return;
}
if (threshold == 0) return;
revert InsufficientAttestations();
}
}
49 changes: 49 additions & 0 deletions src/lib/TrustLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,53 @@ library TrustLib {
revert IRegistry.InvalidModuleType();

Check warning on line 65 in src/lib/TrustLib.sol

View check run for this annotation

Codecov / codecov/patch

src/lib/TrustLib.sol#L65

Added line #L65 was not covered by tests
}
}

function checkValid(AttestationRecord storage $attestation, ModuleType expectedType) internal view returns (bool) {
uint256 attestedAt;
uint256 expirationTime;
uint256 revocationTime;
PackedModuleTypes packedModuleType;

Check warning on line 73 in src/lib/TrustLib.sol

View check run for this annotation

Codecov / codecov/patch

src/lib/TrustLib.sol#L70-L73

Added lines #L70 - L73 were not covered by tests
/*
* Ensure only one SLOAD
* Assembly equiv to:
*
* uint256 attestedAt = record.time;
* uint256 expirationTime = record.expirationTime;
* uint256 revocationTime = record.revocationTime;
* PackedModuleTypes packedModuleType = record.moduleTypes;
*/

assembly {
let mask := 0xffffffffffff
let slot := sload($attestation.slot)
attestedAt := and(mask, slot)
slot := shr(48, slot)
expirationTime := and(mask, slot)
slot := shr(48, slot)
revocationTime := and(mask, slot)
slot := shr(48, slot)
packedModuleType := and(mask, slot)

Check warning on line 93 in src/lib/TrustLib.sol

View check run for this annotation

Codecov / codecov/patch

src/lib/TrustLib.sol#L87-L93

Added lines #L87 - L93 were not covered by tests
}

// check if any attestation was made
if (attestedAt == ZERO_TIMESTAMP) {
return false;

Check warning on line 98 in src/lib/TrustLib.sol

View check run for this annotation

Codecov / codecov/patch

src/lib/TrustLib.sol#L98

Added line #L98 was not covered by tests
}

// check if attestation has expired
if (expirationTime != ZERO_TIMESTAMP && block.timestamp > expirationTime) {
return false;

Check warning on line 103 in src/lib/TrustLib.sol

View check run for this annotation

Codecov / codecov/patch

src/lib/TrustLib.sol#L103

Added line #L103 was not covered by tests
}

// check if attestation has been revoked
if (revocationTime != ZERO_TIMESTAMP) {
return false;

Check warning on line 108 in src/lib/TrustLib.sol

View check run for this annotation

Codecov / codecov/patch

src/lib/TrustLib.sol#L108

Added line #L108 was not covered by tests
}
// if a expectedType is set, check if the attestation is for the correct module type
// if no expectedType is set, module type is not checked
if (expectedType != ZERO_MODULE_TYPE && !packedModuleType.isType(expectedType)) {
return false;

Check warning on line 113 in src/lib/TrustLib.sol

View check run for this annotation

Codecov / codecov/patch

src/lib/TrustLib.sol#L113

Added line #L113 was not covered by tests
}
return true;

Check warning on line 115 in src/lib/TrustLib.sol

View check run for this annotation

Codecov / codecov/patch

src/lib/TrustLib.sol#L115

Added line #L115 was not covered by tests
}
}
18 changes: 11 additions & 7 deletions test/Attestation.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -230,24 +230,28 @@ contract AttestationTest is BaseTest {
}

function test_WhenUsingValidECDSA() public whenAttestingWithSignature {
uint256 nonceBefore = registry.attesterNonce(attester1.addr);
_make_WhenUsingValidECDSA(attester1);
}

function _make_WhenUsingValidECDSA(Account memory attester) public whenAttestingWithSignature {
uint256 nonceBefore = registry.attesterNonce(attester.addr);
// It should recover.
uint32[] memory types = new uint32[](2);
types[0] = 1;
types[1] = 2;
AttestationRequest memory request = mockAttestation(address(module1), uint48(block.timestamp + 100), "", types);

bytes32 digest = registry.getDigest(request, attester1.addr);
bytes memory sig = ecdsaSign(attester1.key, digest);
registry.attest(defaultSchemaUID, attester1.addr, request, sig);
bytes32 digest = registry.getDigest(request, attester.addr);
bytes memory sig = ecdsaSign(attester.key, digest);
registry.attest(defaultSchemaUID, attester.addr, request, sig);

AttestationRecord memory record = registry.findAttestation(address(module1), attester1.addr);
uint256 nonceAfter = registry.attesterNonce(attester1.addr);
AttestationRecord memory record = registry.findAttestation(address(module1), attester.addr);
uint256 nonceAfter = registry.attesterNonce(attester.addr);

assertEq(record.time, block.timestamp);
assertEq(record.expirationTime, request.expirationTime);
assertEq(record.moduleAddr, request.moduleAddr);
assertEq(record.attester, attester1.addr);
assertEq(record.attester, attester.addr);
assertEq(nonceAfter, nonceBefore + 1);
assertEq(PackedModuleTypes.unwrap(record.moduleTypes), 2 ** 1 + 2 ** 2);
}
Expand Down
8 changes: 4 additions & 4 deletions test/TrustDelegation.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ contract TrustTest is AttestationTest {

function test_WhenNoAttestersSet() external whenQueryingRegisty {
// It should revert.
vm.expectRevert(abi.encodeWithSelector(IRegistry.AttestationNotFound.selector));
vm.expectRevert();
registry.check(address(module1), ModuleType.wrap(1));
vm.expectRevert(abi.encodeWithSelector(IRegistry.AttestationNotFound.selector));
vm.expectRevert();
registry.checkForAccount(makeAddr("foo"), address(module1), ModuleType.wrap(1));
vm.expectRevert(abi.encodeWithSelector(IRegistry.AttestationNotFound.selector));
vm.expectRevert();
registry.check(address(module1));
vm.expectRevert(abi.encodeWithSelector(IRegistry.AttestationNotFound.selector));
vm.expectRevert();
registry.checkForAccount(makeAddr("foo"), address(module1));
}

Expand Down
21 changes: 20 additions & 1 deletion test/TrustDelegationExternal.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "./Attestation.t.sol";
import "src/DataTypes.sol";
import { LibSort } from "solady/utils/LibSort.sol";


contract TrustTestExternal is AttestationTest {
using LibSort for address[];

Expand All @@ -19,7 +20,7 @@ contract TrustTestExternal is AttestationTest {

function test_WhenSupplyingExternal() external whenSettingAttester {
// It should set.
test_WhenUsingValidECDSA();
_make_WhenUsingValidECDSA(attester1);
address[] memory trustedAttesters = new address[](2);
trustedAttesters[0] = address(attester1.addr);
trustedAttesters[1] = address(attester2.addr);
Expand All @@ -29,6 +30,24 @@ contract TrustTestExternal is AttestationTest {
vm.expectRevert();
registry.check(address(module1), ModuleType.wrap(3), attester1.addr);
registry.checkN(address(module1), trustedAttesters, 1);
registry.checkN(address(module1), ModuleType.wrap(1), trustedAttesters, 1);
vm.expectRevert();
registry.checkN(address(module1), trustedAttesters, 2);
vm.expectRevert();
registry.checkN(address(module1), ModuleType.wrap(1), trustedAttesters, 2);
_make_WhenUsingValidECDSA(attester2);
registry.checkN(address(module1), trustedAttesters, 2);
registry.checkN(address(module1), trustedAttesters, 2);
// registry.checkN(address(module1), ModuleType.wrap(1), trustedAttesters, 2);

trustedAttesters = new address[](4);
Account memory attester3 = makeAccount("attester3");
Account memory attester4 = makeAccount("attester4");
trustedAttesters[0] = address(attester1.addr);
trustedAttesters[1] = address(attester3.addr);
trustedAttesters[2] = address(attester4.addr);
trustedAttesters[3] = address(attester2.addr);

registry.checkN(address(module1), trustedAttesters, 2);
}
}

0 comments on commit 7ca7186

Please sign in to comment.