Skip to content

Commit 92c7840

Browse files
Bartosz Lenartbartossh
andauthored
Add associated values in mod errors Error. (#28)
### Reference [RDS-1914](https://redstone-team.atlassian.net/browse/RDS-1914) Fix Error variant: - TimestampMustBeGreaterThanBefore - CurrentTimestampMustBeGreaterThanLatestUpdateTimestamp --------- Co-authored-by: bartossh <bartosz.lenart@redstone.com>
1 parent 40812cb commit 92c7840

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

crates/redstone/src/contract/verification.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub fn verify_write_timestamp(
7474
.add(min_time_between_updates)
7575
.is_same_or_after(time_now) =>
7676
{
77-
Err(Error::CurrentTimestampMustBeGreaterThanLatestUpdateTimestamp)
77+
Err(Error::CurrentTimestampMustBeGreaterThanLatestUpdateTimestamp(time_now, write_time))
7878
}
7979
_ => Ok(()),
8080
}
@@ -87,7 +87,10 @@ pub fn verify_package_timestamp(
8787
new_package_time: TimestampMillis,
8888
) -> Result<(), Error> {
8989
if new_package_time.is_same_or_before(last_package_time) {
90-
return Err(Error::TimestampMustBeGreaterThanBefore);
90+
return Err(Error::DataTimestampMustBeGreaterThanBefore(
91+
new_package_time,
92+
last_package_time,
93+
));
9194
}
9295

9396
Ok(())
@@ -189,7 +192,12 @@ mod tests {
189192

190193
assert_eq!(
191194
res,
192-
Err(Error::CurrentTimestampMustBeGreaterThanLatestUpdateTimestamp)
195+
Err(
196+
Error::CurrentTimestampMustBeGreaterThanLatestUpdateTimestamp(
197+
999.into(),
198+
900.into()
199+
)
200+
)
193201
);
194202
}
195203

@@ -204,7 +212,12 @@ mod tests {
204212

205213
assert_eq!(
206214
res,
207-
Err(Error::CurrentTimestampMustBeGreaterThanLatestUpdateTimestamp)
215+
Err(
216+
Error::CurrentTimestampMustBeGreaterThanLatestUpdateTimestamp(
217+
900.into(),
218+
900.into()
219+
)
220+
)
208221
);
209222
}
210223

@@ -217,10 +230,22 @@ mod tests {
217230
#[test]
218231
fn verify_package_timestamp_non_increase_is_err() {
219232
let res = verify_trusted_update(901.into(), Some(900.into()), 1.into(), 1.into());
220-
assert_eq!(res, Err(Error::TimestampMustBeGreaterThanBefore));
233+
assert_eq!(
234+
res,
235+
Err(Error::DataTimestampMustBeGreaterThanBefore(
236+
1.into(),
237+
1.into()
238+
))
239+
);
221240

222241
let res =
223242
verify_untrusted_update(901.into(), Some(900.into()), 1.into(), 1.into(), 1.into());
224-
assert_eq!(res, Err(Error::TimestampMustBeGreaterThanBefore));
243+
assert_eq!(
244+
res,
245+
Err(Error::DataTimestampMustBeGreaterThanBefore(
246+
1.into(),
247+
1.into()
248+
))
249+
);
225250
}
226251
}

crates/redstone/src/network/error.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,24 @@ pub enum Error {
108108
/// Includes FeedId that is reocuring.
109109
ConfigReocuringFeedId(FeedId),
110110

111-
/// Indicates that the provided timestamp is not greater than a previously written timestamp.
111+
/// Indicates that the provided data timestamp is not greater than a previously written package timestamp.
112112
///
113113
/// For the price adapter to accept a new price update, the associated timestamp must be
114114
/// strictly greater than the timestamp of the last update. This error is raised if a new
115115
/// timestamp does not meet this criterion, ensuring the chronological integrity of price data.
116-
TimestampMustBeGreaterThanBefore,
116+
///
117+
/// Includes the value of a current package timestamp and the timestamp of the previous package.
118+
DataTimestampMustBeGreaterThanBefore(TimestampMillis, TimestampMillis),
117119

118-
/// Indicates that the current timestamp is not greater than the timestamp of the last update.
120+
/// Indicates that the current update timestamp is not greater than the last update timestamp.
119121
///
120122
/// This error is raised to ensure that the data being written has a timestamp strictly greater
121123
/// than the most recent timestamp already stored in the system. It guarantees that new data
122124
/// is not outdated or stale compared to the existing records, thereby maintaining the chronological
123125
/// integrity and consistency of the updates.
124-
CurrentTimestampMustBeGreaterThanLatestUpdateTimestamp,
126+
///
127+
/// Includes the value of a current update timestamp and the last update timestamp.
128+
CurrentTimestampMustBeGreaterThanLatestUpdateTimestamp(TimestampMillis, TimestampMillis),
125129
}
126130

127131
impl From<CryptoError> for Error {
@@ -151,8 +155,8 @@ impl Error {
151155
Error::CryptographicError(error) => 700 + error.code(),
152156
Error::TimestampTooOld(data_package_index, _) => 1000 + *data_package_index as u16,
153157
Error::TimestampTooFuture(data_package_index, _) => 1050 + *data_package_index as u16,
154-
Error::TimestampMustBeGreaterThanBefore => 1101,
155-
Error::CurrentTimestampMustBeGreaterThanLatestUpdateTimestamp => 1102,
158+
Error::DataTimestampMustBeGreaterThanBefore(_, _) => 1101,
159+
Error::CurrentTimestampMustBeGreaterThanLatestUpdateTimestamp(_, _) => 1102,
156160
}
157161
}
158162
}
@@ -216,13 +220,16 @@ impl Display for Error {
216220
feed_id.as_hex_str()
217221
)
218222
}
219-
Error::TimestampMustBeGreaterThanBefore => {
220-
write!(f, "Timestamp must be greater than before")
223+
Error::DataTimestampMustBeGreaterThanBefore(current, before) => {
224+
write!(
225+
f,
226+
"Package timestamp: {current:?} must be greater than package timestamp before: {before:?}"
227+
)
221228
}
222-
Error::CurrentTimestampMustBeGreaterThanLatestUpdateTimestamp => {
229+
Error::CurrentTimestampMustBeGreaterThanLatestUpdateTimestamp(current, last) => {
223230
write!(
224231
f,
225-
"Current timestamp must be greater than latest update timestamp"
232+
"Current update timestamp: {current:?} must be greater than latest update timestamp: {last:?}"
226233
)
227234
}
228235
}

0 commit comments

Comments
 (0)