Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions path/to/contracts/events/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Import necessary dependencies
use std::error::Error;

// Define the Error enum
#[derive(Debug)]
pub enum Error {
EventIdOverflow,
}

// Implement the Error trait for the Error enum
impl std::error::Error for Error {
fn description(&self) -> &str {
match self {
Error::EventIdOverflow => "Event ID overflow",
}
}
}
19 changes: 19 additions & 0 deletions path/to/contracts/events/src/idempotency.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Import necessary dependencies
use std::ops::Add;
use std::result::Result;

// Define the Error enum
#[derive(Debug)]
pub enum Error {
EventIdOverflow,
}

// Modify the next_event_id function to use checked_add and handle overflows
pub fn next_event_id(current: u64, increment: u64) -> Result<u64, Error> {
current.checked_add(increment).ok_or(Error::EventIdOverflow)
}

// Update the stored_next_event_id function to use checked_add and handle overflows
pub fn stored_next_event_id(current: u64, increment: u64) -> Result<u64, Error> {
current.checked_add(increment).ok_or(Error::EventIdOverflow)
}
26 changes: 26 additions & 0 deletions path/to/contracts/events/src/tests/idempotency.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Import necessary dependencies
use super::*;

// Write unit tests to verify that overflows are properly handled and revert with a typed error
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_next_event_id_overflow() {
let current = u64::MAX;
let increment = 1;
let result = next_event_id(current, increment);
assert!(result.is_err());
assert_eq!(result.unwrap_err(), Error::EventIdOverflow);
}

#[test]
fn test_stored_next_event_id_overflow() {
let current = u64::MAX;
let increment = 1;
let result = stored_next_event_id(current, increment);
assert!(result.is_err());
assert_eq!(result.unwrap_err(), Error::EventIdOverflow);
}
}
14 changes: 14 additions & 0 deletions path/to/contracts/profile/src/earnings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Import necessary dependencies
use std::ops::Add;
use std::result::Result;

// Define the Error enum
#[derive(Debug)]
pub enum Error {
EarningsOverflow,
}

// Modify the register function to use checked_add and handle overflows
pub fn register(current: i128, amount: i128) -> Result<i128, Error> {
current.checked_add(amount).ok_or(Error::EarningsOverflow)
}
17 changes: 17 additions & 0 deletions path/to/contracts/profile/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Import necessary dependencies
use std::error::Error;

// Define the Error enum
#[derive(Debug)]
pub enum Error {
EarningsOverflow,
}

// Implement the Error trait for the Error enum
impl std::error::Error for Error {
fn description(&self) -> &str {
match self {
Error::EarningsOverflow => "Earnings overflow",
}
}
}
17 changes: 17 additions & 0 deletions path/to/contracts/profile/src/tests/earnings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Import necessary dependencies
use super::*;

// Write unit tests to verify that overflows are properly handled and revert with a typed error
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_register_overflow() {
let current = i128::MAX;
let amount = 1;
let result = register(current, amount);
assert!(result.is_err());
assert_eq!(result.unwrap_err(), Error::EarningsOverflow);
}
}