|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.27; |
| 3 | + |
| 4 | +import { IRecurringCollector } from "@graphprotocol/interfaces/contracts/horizon/IRecurringCollector.sol"; |
| 5 | +import { |
| 6 | + SCOPE_SIGNED, |
| 7 | + SCOPE_ACTIVE, |
| 8 | + SCOPE_PENDING |
| 9 | +} from "@graphprotocol/interfaces/contracts/horizon/IAgreementCollector.sol"; |
| 10 | + |
| 11 | +import { RecurringCollectorSharedTest } from "./shared.t.sol"; |
| 12 | + |
| 13 | +contract RecurringCollectorCancelSignedOfferTest is RecurringCollectorSharedTest { |
| 14 | + /* |
| 15 | + * TESTS |
| 16 | + */ |
| 17 | + |
| 18 | + /* solhint-disable graph/func-name-mixedcase */ |
| 19 | + |
| 20 | + function test_CancelSigned_BlocksAccept(FuzzyTestAccept calldata fuzzyTestAccept) public { |
| 21 | + IRecurringCollector.RecurringCollectionAgreement memory rca = _recurringCollectorHelper.sensibleRCA( |
| 22 | + fuzzyTestAccept.rca |
| 23 | + ); |
| 24 | + uint256 signerKey = boundKey(fuzzyTestAccept.unboundedSignerKey); |
| 25 | + _recurringCollectorHelper.authorizeSignerWithChecks(rca.payer, signerKey); |
| 26 | + |
| 27 | + (, bytes memory signature) = _recurringCollectorHelper.generateSignedRCA(rca, signerKey); |
| 28 | + bytes32 rcaHash = _recurringCollector.hashRCA(rca); |
| 29 | + address signer = vm.addr(signerKey); |
| 30 | + bytes16 agreementId = _recurringCollector.generateAgreementId( |
| 31 | + rca.payer, |
| 32 | + rca.dataService, |
| 33 | + rca.serviceProvider, |
| 34 | + rca.deadline, |
| 35 | + rca.nonce |
| 36 | + ); |
| 37 | + |
| 38 | + vm.prank(signer); |
| 39 | + _recurringCollector.cancel(agreementId, rcaHash, SCOPE_SIGNED); |
| 40 | + |
| 41 | + // Accepting with the cancelled signature should revert |
| 42 | + vm.expectRevert( |
| 43 | + abi.encodeWithSelector(IRecurringCollector.RecurringCollectorOfferCancelled.selector, signer, rcaHash) |
| 44 | + ); |
| 45 | + vm.prank(rca.dataService); |
| 46 | + _recurringCollector.accept(rca, signature); |
| 47 | + } |
| 48 | + |
| 49 | + function test_CancelSigned_EmitsEvent(FuzzyTestAccept calldata fuzzyTestAccept) public { |
| 50 | + IRecurringCollector.RecurringCollectionAgreement memory rca = _recurringCollectorHelper.sensibleRCA( |
| 51 | + fuzzyTestAccept.rca |
| 52 | + ); |
| 53 | + uint256 signerKey = boundKey(fuzzyTestAccept.unboundedSignerKey); |
| 54 | + _recurringCollectorHelper.authorizeSignerWithChecks(rca.payer, signerKey); |
| 55 | + |
| 56 | + bytes32 rcaHash = _recurringCollector.hashRCA(rca); |
| 57 | + address signer = vm.addr(signerKey); |
| 58 | + bytes16 agreementId = _recurringCollector.generateAgreementId( |
| 59 | + rca.payer, |
| 60 | + rca.dataService, |
| 61 | + rca.serviceProvider, |
| 62 | + rca.deadline, |
| 63 | + rca.nonce |
| 64 | + ); |
| 65 | + |
| 66 | + vm.expectEmit(address(_recurringCollector)); |
| 67 | + emit IRecurringCollector.OfferCancelled(signer, agreementId, rcaHash); |
| 68 | + vm.prank(signer); |
| 69 | + _recurringCollector.cancel(agreementId, rcaHash, SCOPE_SIGNED); |
| 70 | + } |
| 71 | + |
| 72 | + function test_CancelSigned_BlocksUpdate(FuzzyTestUpdate calldata fuzzyTestUpdate) public { |
| 73 | + ( |
| 74 | + IRecurringCollector.RecurringCollectionAgreement memory rca, |
| 75 | + , |
| 76 | + uint256 signerKey, |
| 77 | + bytes16 agreementId |
| 78 | + ) = _sensibleAuthorizeAndAccept(fuzzyTestUpdate.fuzzyTestAccept); |
| 79 | + |
| 80 | + IRecurringCollector.RecurringCollectionAgreementUpdate memory rcau = _recurringCollectorHelper.sensibleRCAU( |
| 81 | + fuzzyTestUpdate.rcau |
| 82 | + ); |
| 83 | + rcau.agreementId = agreementId; |
| 84 | + |
| 85 | + ( |
| 86 | + IRecurringCollector.RecurringCollectionAgreementUpdate memory signedRcau, |
| 87 | + bytes memory rcauSig |
| 88 | + ) = _recurringCollectorHelper.generateSignedRCAUForAgreement(agreementId, rcau, signerKey); |
| 89 | + bytes32 rcauHash = _recurringCollector.hashRCAU(signedRcau); |
| 90 | + address signer = vm.addr(signerKey); |
| 91 | + |
| 92 | + vm.prank(signer); |
| 93 | + _recurringCollector.cancel(agreementId, rcauHash, SCOPE_SIGNED); |
| 94 | + |
| 95 | + // Updating with the cancelled signature should revert |
| 96 | + vm.expectRevert( |
| 97 | + abi.encodeWithSelector(IRecurringCollector.RecurringCollectorOfferCancelled.selector, signer, rcauHash) |
| 98 | + ); |
| 99 | + vm.prank(rca.dataService); |
| 100 | + _recurringCollector.update(signedRcau, rcauSig); |
| 101 | + } |
| 102 | + |
| 103 | + function test_CancelSigned_Idempotent(FuzzyTestAccept calldata fuzzyTestAccept) public { |
| 104 | + IRecurringCollector.RecurringCollectionAgreement memory rca = _recurringCollectorHelper.sensibleRCA( |
| 105 | + fuzzyTestAccept.rca |
| 106 | + ); |
| 107 | + uint256 signerKey = boundKey(fuzzyTestAccept.unboundedSignerKey); |
| 108 | + _recurringCollectorHelper.authorizeSignerWithChecks(rca.payer, signerKey); |
| 109 | + |
| 110 | + bytes32 rcaHash = _recurringCollector.hashRCA(rca); |
| 111 | + address signer = vm.addr(signerKey); |
| 112 | + bytes16 agreementId = _recurringCollector.generateAgreementId( |
| 113 | + rca.payer, |
| 114 | + rca.dataService, |
| 115 | + rca.serviceProvider, |
| 116 | + rca.deadline, |
| 117 | + rca.nonce |
| 118 | + ); |
| 119 | + |
| 120 | + vm.prank(signer); |
| 121 | + _recurringCollector.cancel(agreementId, rcaHash, SCOPE_SIGNED); |
| 122 | + |
| 123 | + // Second call succeeds silently — no revert, no event |
| 124 | + vm.recordLogs(); |
| 125 | + vm.prank(signer); |
| 126 | + _recurringCollector.cancel(agreementId, rcaHash, SCOPE_SIGNED); |
| 127 | + assertEq(vm.getRecordedLogs().length, 0); |
| 128 | + } |
| 129 | + |
| 130 | + function test_CancelSigned_DoesNotAffectDifferentSigner( |
| 131 | + FuzzyTestAccept calldata fuzzyTestAccept1, |
| 132 | + FuzzyTestAccept calldata fuzzyTestAccept2 |
| 133 | + ) public { |
| 134 | + IRecurringCollector.RecurringCollectionAgreement memory rca1 = _recurringCollectorHelper.sensibleRCA( |
| 135 | + fuzzyTestAccept1.rca |
| 136 | + ); |
| 137 | + uint256 signerKey1 = boundKey(fuzzyTestAccept1.unboundedSignerKey); |
| 138 | + |
| 139 | + IRecurringCollector.RecurringCollectionAgreement memory rca2 = _recurringCollectorHelper.sensibleRCA( |
| 140 | + fuzzyTestAccept2.rca |
| 141 | + ); |
| 142 | + uint256 signerKey2 = boundKey(fuzzyTestAccept2.unboundedSignerKey); |
| 143 | + |
| 144 | + vm.assume(rca1.payer != rca2.payer); |
| 145 | + vm.assume(vm.addr(signerKey1) != vm.addr(signerKey2)); |
| 146 | + |
| 147 | + _recurringCollectorHelper.authorizeSignerWithChecks(rca1.payer, signerKey1); |
| 148 | + _recurringCollectorHelper.authorizeSignerWithChecks(rca2.payer, signerKey2); |
| 149 | + |
| 150 | + bytes32 rcaHash = _recurringCollector.hashRCA(rca1); |
| 151 | + |
| 152 | + // Signer1 cancels — should not affect signer2 |
| 153 | + vm.prank(vm.addr(signerKey1)); |
| 154 | + _recurringCollector.cancel(bytes16(0), rcaHash, SCOPE_SIGNED); |
| 155 | + |
| 156 | + // Signer2's signatures for the same hash are unaffected |
| 157 | + // (signer-scoped, not hash-global) |
| 158 | + } |
| 159 | + |
| 160 | + function test_CancelSigned_SelfAuthenticating(FuzzyTestAccept calldata fuzzyTestAccept, address anyAddress) public { |
| 161 | + // Any address can call cancel with SCOPE_SIGNED — it only records for msg.sender |
| 162 | + IRecurringCollector.RecurringCollectionAgreement memory rca = _recurringCollectorHelper.sensibleRCA( |
| 163 | + fuzzyTestAccept.rca |
| 164 | + ); |
| 165 | + bytes32 rcaHash = _recurringCollector.hashRCA(rca); |
| 166 | + vm.assume(anyAddress != address(0)); |
| 167 | + vm.assume(anyAddress != _proxyAdmin); |
| 168 | + |
| 169 | + // Should not revert — self-authenticating, no _requirePayer |
| 170 | + vm.prank(anyAddress); |
| 171 | + _recurringCollector.cancel(bytes16(0), rcaHash, SCOPE_SIGNED); |
| 172 | + } |
| 173 | + |
| 174 | + function test_CancelSigned_CombinedWithActiveDoesNotRevert(FuzzyTestAccept calldata fuzzyTestAccept) public { |
| 175 | + IRecurringCollector.RecurringCollectionAgreement memory rca = _recurringCollectorHelper.sensibleRCA( |
| 176 | + fuzzyTestAccept.rca |
| 177 | + ); |
| 178 | + uint256 signerKey = boundKey(fuzzyTestAccept.unboundedSignerKey); |
| 179 | + _recurringCollectorHelper.authorizeSignerWithChecks(rca.payer, signerKey); |
| 180 | + |
| 181 | + bytes32 rcaHash = _recurringCollector.hashRCA(rca); |
| 182 | + address signer = vm.addr(signerKey); |
| 183 | + bytes16 agreementId = _recurringCollector.generateAgreementId( |
| 184 | + rca.payer, |
| 185 | + rca.dataService, |
| 186 | + rca.serviceProvider, |
| 187 | + rca.deadline, |
| 188 | + rca.nonce |
| 189 | + ); |
| 190 | + |
| 191 | + // SCOPE_SIGNED | SCOPE_ACTIVE with no accepted agreement — should not revert. |
| 192 | + // The signed recording succeeds; the active scope is skipped because nothing on-chain. |
| 193 | + vm.expectEmit(address(_recurringCollector)); |
| 194 | + emit IRecurringCollector.OfferCancelled(signer, agreementId, rcaHash); |
| 195 | + vm.prank(signer); |
| 196 | + _recurringCollector.cancel(agreementId, rcaHash, SCOPE_SIGNED | SCOPE_ACTIVE); |
| 197 | + } |
| 198 | + |
| 199 | + function test_CancelSigned_CombinedWithPendingDoesNotRevert(FuzzyTestAccept calldata fuzzyTestAccept) public { |
| 200 | + IRecurringCollector.RecurringCollectionAgreement memory rca = _recurringCollectorHelper.sensibleRCA( |
| 201 | + fuzzyTestAccept.rca |
| 202 | + ); |
| 203 | + uint256 signerKey = boundKey(fuzzyTestAccept.unboundedSignerKey); |
| 204 | + _recurringCollectorHelper.authorizeSignerWithChecks(rca.payer, signerKey); |
| 205 | + |
| 206 | + bytes32 rcaHash = _recurringCollector.hashRCA(rca); |
| 207 | + address signer = vm.addr(signerKey); |
| 208 | + bytes16 agreementId = _recurringCollector.generateAgreementId( |
| 209 | + rca.payer, |
| 210 | + rca.dataService, |
| 211 | + rca.serviceProvider, |
| 212 | + rca.deadline, |
| 213 | + rca.nonce |
| 214 | + ); |
| 215 | + |
| 216 | + // SCOPE_SIGNED | SCOPE_PENDING with no agreement — should not revert. |
| 217 | + vm.expectEmit(address(_recurringCollector)); |
| 218 | + emit IRecurringCollector.OfferCancelled(signer, agreementId, rcaHash); |
| 219 | + vm.prank(signer); |
| 220 | + _recurringCollector.cancel(agreementId, rcaHash, SCOPE_SIGNED | SCOPE_PENDING); |
| 221 | + } |
| 222 | + |
| 223 | + function test_CancelSigned_UndoWithZero(FuzzyTestAccept calldata fuzzyTestAccept) public { |
| 224 | + IRecurringCollector.RecurringCollectionAgreement memory rca = _recurringCollectorHelper.sensibleRCA( |
| 225 | + fuzzyTestAccept.rca |
| 226 | + ); |
| 227 | + uint256 signerKey = boundKey(fuzzyTestAccept.unboundedSignerKey); |
| 228 | + _recurringCollectorHelper.authorizeSignerWithChecks(rca.payer, signerKey); |
| 229 | + |
| 230 | + (, bytes memory signature) = _recurringCollectorHelper.generateSignedRCA(rca, signerKey); |
| 231 | + bytes32 rcaHash = _recurringCollector.hashRCA(rca); |
| 232 | + address signer = vm.addr(signerKey); |
| 233 | + bytes16 agreementId = _recurringCollector.generateAgreementId( |
| 234 | + rca.payer, |
| 235 | + rca.dataService, |
| 236 | + rca.serviceProvider, |
| 237 | + rca.deadline, |
| 238 | + rca.nonce |
| 239 | + ); |
| 240 | + |
| 241 | + // Cancel |
| 242 | + vm.prank(signer); |
| 243 | + _recurringCollector.cancel(agreementId, rcaHash, SCOPE_SIGNED); |
| 244 | + |
| 245 | + // Undo by calling with bytes16(0) |
| 246 | + vm.prank(signer); |
| 247 | + _recurringCollector.cancel(bytes16(0), rcaHash, SCOPE_SIGNED); |
| 248 | + |
| 249 | + // Accept should now succeed |
| 250 | + _setupValidProvision(rca.serviceProvider, rca.dataService); |
| 251 | + vm.prank(rca.dataService); |
| 252 | + _recurringCollector.accept(rca, signature); |
| 253 | + } |
| 254 | + |
| 255 | + /* solhint-enable graph/func-name-mixedcase */ |
| 256 | +} |
0 commit comments