Skip to content

Commit 003dd61

Browse files
f: validate before setting in storage
1 parent 19a4270 commit 003dd61

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

packages/horizon/contracts/payments/collectors/RecurringCollector.sol

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ contract RecurringCollector is EIP712, GraphDirectory, Authorizable, IRecurringC
8888
// check that the voucher is signed by the payer (or proxy)
8989
_requireAuthorizedRCASigner(signedRCA);
9090

91+
require(
92+
signedRCA.rca.dataService != address(0) &&
93+
signedRCA.rca.payer != address(0) &&
94+
signedRCA.rca.serviceProvider != address(0),
95+
RecurringCollectorAgreementAddressNotSet()
96+
);
97+
98+
_requireValidCollectionWindowParams(
99+
signedRCA.rca.endsAt,
100+
signedRCA.rca.minSecondsPerCollection,
101+
signedRCA.rca.maxSecondsPerCollection
102+
);
103+
91104
AgreementData storage agreement = _getAgreementStorage(signedRCA.rca.agreementId);
92105
// check that the agreement is not already accepted
93106
require(
@@ -106,7 +119,6 @@ contract RecurringCollector is EIP712, GraphDirectory, Authorizable, IRecurringC
106119
agreement.maxOngoingTokensPerSecond = signedRCA.rca.maxOngoingTokensPerSecond;
107120
agreement.minSecondsPerCollection = signedRCA.rca.minSecondsPerCollection;
108121
agreement.maxSecondsPerCollection = signedRCA.rca.maxSecondsPerCollection;
109-
_requireValidAgreement(agreement);
110122

111123
emit AgreementAccepted(
112124
agreement.dataService,
@@ -178,13 +190,18 @@ contract RecurringCollector is EIP712, GraphDirectory, Authorizable, IRecurringC
178190
// check that the voucher is signed by the payer (or proxy)
179191
_requireAuthorizedRCAUSigner(signedRCAU, agreement.payer);
180192

193+
_requireValidCollectionWindowParams(
194+
signedRCAU.rcau.endsAt,
195+
signedRCAU.rcau.minSecondsPerCollection,
196+
signedRCAU.rcau.maxSecondsPerCollection
197+
);
198+
181199
// update the agreement
182200
agreement.endsAt = signedRCAU.rcau.endsAt;
183201
agreement.maxInitialTokens = signedRCAU.rcau.maxInitialTokens;
184202
agreement.maxOngoingTokensPerSecond = signedRCAU.rcau.maxOngoingTokensPerSecond;
185203
agreement.minSecondsPerCollection = signedRCAU.rcau.minSecondsPerCollection;
186204
agreement.maxSecondsPerCollection = signedRCAU.rcau.maxSecondsPerCollection;
187-
_requireValidAgreement(agreement);
188205

189206
emit AgreementUpdated(
190207
agreement.dataService,
@@ -306,38 +323,31 @@ contract RecurringCollector is EIP712, GraphDirectory, Authorizable, IRecurringC
306323
return tokensToCollect;
307324
}
308325

309-
function _requireValidAgreement(AgreementData memory _agreement) private view {
310-
require(
311-
_agreement.dataService != address(0) &&
312-
_agreement.payer != address(0) &&
313-
_agreement.serviceProvider != address(0),
314-
RecurringCollectorAgreementAddressNotSet()
315-
);
316-
326+
function _requireValidCollectionWindowParams(
327+
uint64 _endsAt,
328+
uint32 _minSecondsPerCollection,
329+
uint32 _maxSecondsPerCollection
330+
) private view {
317331
// Agreement needs to end in the future
318-
require(
319-
_agreement.endsAt > block.timestamp,
320-
RecurringCollectorAgreementElapsedEndsAt(block.timestamp, _agreement.endsAt)
321-
);
332+
require(_endsAt > block.timestamp, RecurringCollectorAgreementElapsedEndsAt(block.timestamp, _endsAt));
322333

323334
// Collection window needs to be at least MIN_SECONDS_COLLECTION_WINDOW
324335
require(
325-
_agreement.maxSecondsPerCollection > _agreement.minSecondsPerCollection &&
326-
(_agreement.maxSecondsPerCollection - _agreement.minSecondsPerCollection >=
327-
MIN_SECONDS_COLLECTION_WINDOW),
336+
_maxSecondsPerCollection > _minSecondsPerCollection &&
337+
(_maxSecondsPerCollection - _minSecondsPerCollection >= MIN_SECONDS_COLLECTION_WINDOW),
328338
RecurringCollectorAgreementInvalidCollectionWindow(
329339
MIN_SECONDS_COLLECTION_WINDOW,
330-
_agreement.minSecondsPerCollection,
331-
_agreement.maxSecondsPerCollection
340+
_minSecondsPerCollection,
341+
_maxSecondsPerCollection
332342
)
333343
);
334344

335345
// Agreement needs to last at least one min collection window
336346
require(
337-
_agreement.endsAt - block.timestamp >= _agreement.minSecondsPerCollection + MIN_SECONDS_COLLECTION_WINDOW,
347+
_endsAt - block.timestamp >= _minSecondsPerCollection + MIN_SECONDS_COLLECTION_WINDOW,
338348
RecurringCollectorAgreementInvalidDuration(
339-
_agreement.minSecondsPerCollection + MIN_SECONDS_COLLECTION_WINDOW,
340-
_agreement.endsAt - block.timestamp
349+
_minSecondsPerCollection + MIN_SECONDS_COLLECTION_WINDOW,
350+
_endsAt - block.timestamp
341351
)
342352
);
343353
}

0 commit comments

Comments
 (0)