From f0a369fa547fc2ea7ca08f0d4baa9b5ae0acb5be Mon Sep 17 00:00:00 2001 From: dragosrebegea Date: Mon, 7 Oct 2024 11:39:44 +0300 Subject: [PATCH 1/2] submission timestamp checks improvement --- contracts/price-aggregator/src/lib.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/contracts/price-aggregator/src/lib.rs b/contracts/price-aggregator/src/lib.rs index 05698745..493deb25 100644 --- a/contracts/price-aggregator/src/lib.rs +++ b/contracts/price-aggregator/src/lib.rs @@ -168,8 +168,6 @@ pub trait PriceAggregator: let current_timestamp = self.blockchain().get_block_timestamp(); let mut is_first_submission = false; let mut first_submission_timestamp = if submissions.is_empty() { - self.require_valid_first_submission(submission_timestamp, current_timestamp); - first_sub_time_mapper.set(current_timestamp); is_first_submission = true; @@ -180,8 +178,6 @@ pub trait PriceAggregator: // round was not completed in time, so it's discarded if current_timestamp > first_submission_timestamp + MAX_ROUND_DURATION_SECONDS { - self.require_valid_first_submission(submission_timestamp, current_timestamp); - submissions.clear(); first_sub_time_mapper.set(current_timestamp); last_sub_time_mapper.set(current_timestamp); @@ -194,7 +190,7 @@ pub trait PriceAggregator: let caller = self.blockchain().get_caller(); let has_caller_already_submitted = submissions.contains_key(&caller); let accepted = !has_caller_already_submitted - && (is_first_submission || submission_timestamp >= first_submission_timestamp); + && (is_first_submission || current_timestamp >= first_submission_timestamp); if accepted { submissions.insert(caller.clone(), price.clone()); last_sub_time_mapper.set(current_timestamp); @@ -224,7 +220,11 @@ pub trait PriceAggregator: }); } - fn require_valid_first_submission(&self, submission_timestamp: u64, current_timestamp: u64) { + fn require_valid_submission_timestamp(&self, submission_timestamp: u64, current_timestamp: u64) { + require!( + submission_timestamp <= current_timestamp, + "Timestamp is from the future" + ); require!( current_timestamp - submission_timestamp <= FIRST_SUBMISSION_TIMESTAMP_MAX_DIFF_SECONDS, "First submission too old" @@ -244,10 +244,7 @@ pub trait PriceAggregator: .into_iter() .map(|submission| submission.into_tuple()) { - require!( - submission_timestamp <= current_timestamp, - "Timestamp is from the future" - ); + self.require_valid_submission_timestamp(submission_timestamp, current_timestamp); self.check_decimals(&from, &to, decimals); From 9c18d7cb9efeef881d741a74fabc2ddc15dd11e7 Mon Sep 17 00:00:00 2001 From: dragosrebegea Date: Mon, 7 Oct 2024 11:51:36 +0300 Subject: [PATCH 2/2] fixes --- contracts/price-aggregator/src/lib.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/contracts/price-aggregator/src/lib.rs b/contracts/price-aggregator/src/lib.rs index 493deb25..1b98f359 100644 --- a/contracts/price-aggregator/src/lib.rs +++ b/contracts/price-aggregator/src/lib.rs @@ -130,22 +130,17 @@ pub trait PriceAggregator: self.require_not_paused(); self.require_is_oracle(); - let current_timestamp = self.blockchain().get_block_timestamp(); - require!( - submission_timestamp <= current_timestamp, - "Timestamp is from the future" - ); + self.require_valid_submission_timestamp(submission_timestamp); self.check_decimals(&from, &to, decimals); - self.submit_unchecked(from, to, submission_timestamp, price, decimals); + self.submit_unchecked(from, to, price, decimals); } fn submit_unchecked( &self, from: ManagedBuffer, to: ManagedBuffer, - submission_timestamp: u64, price: BigUint, decimals: u8, ) { @@ -206,7 +201,7 @@ pub trait PriceAggregator: self.emit_discard_submission_event( &token_pair, round_id, - submission_timestamp, + current_timestamp, first_submission_timestamp, has_caller_already_submitted, ); @@ -220,7 +215,8 @@ pub trait PriceAggregator: }); } - fn require_valid_submission_timestamp(&self, submission_timestamp: u64, current_timestamp: u64) { + fn require_valid_submission_timestamp(&self, submission_timestamp: u64) { + let current_timestamp = self.blockchain().get_block_timestamp(); require!( submission_timestamp <= current_timestamp, "Timestamp is from the future" @@ -239,16 +235,15 @@ pub trait PriceAggregator: self.require_not_paused(); self.require_is_oracle(); - let current_timestamp = self.blockchain().get_block_timestamp(); for (from, to, submission_timestamp, price, decimals) in submissions .into_iter() .map(|submission| submission.into_tuple()) { - self.require_valid_submission_timestamp(submission_timestamp, current_timestamp); + self.require_valid_submission_timestamp(submission_timestamp); self.check_decimals(&from, &to, decimals); - self.submit_unchecked(from, to, submission_timestamp, price, decimals); + self.submit_unchecked(from, to, price, decimals); } }