Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup custom error types #434

Merged
merged 4 commits into from
Aug 17, 2023
Merged
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
2 changes: 1 addition & 1 deletion arbiter-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "arbiter-core"
version = "0.3.0"
version = "0.3.1"
edition = "2021"

# Dependencies for the release build
Expand Down
91 changes: 31 additions & 60 deletions arbiter-core/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,47 +178,31 @@
/// throws an error in execution. To be clear, this is not a contract
/// revert or halt, this is likely an error in `revm`. Please report
/// this type of error.
#[error("execution error! the source error is: {cause:?}")]
Execution {
/// The internal cause for the [`EnvironmentError::Execution`]
/// arising from the [`EVM`].
cause: EVMError<Infallible>,
},
#[error("execution error! the source error is: {0:?}")]
Execution(EVMError<Infallible>),

/// [`EnvironmentError::Pause`] is thrown when the [`Environment`]
/// fails to pause. This should likely never occur, but if it does,
/// please report this error!
#[error("error pausing! the source error is: {cause:?}")]
Pause {
/// Internal cause for [`EnvironmentError::Pause`] parsed as a
/// string.
cause: String,
},
#[error("error pausing! the source error is: {0}")]
Pause(String),

/// [`EnvironmentError::Communication`] is thrown when a channel for
/// receiving or broadcasting fails in some way. This error could happen
/// due to a channel being closed accidentally. If this is thrown, a
/// restart of the simulation and an investigation into what caused a
/// dropped channel is necessary.
#[error("error communicating! the source error is: {cause:?}")]
Communication {
/// Internal cause for [`EnvironmentError::Communication`] parsed
/// as a string.
cause: String,
},
#[error("error communicating! the source error is: {0}")]
Communication(String),

/// [`EnvironmentError::Conversion`] is thrown when a type fails to
/// convert into another (typically a type used in `revm` versus a type used
/// in [`ethers-rs`](https://github.com/gakonst/ethers-rs)).
/// This error should be rare (if not impossible).
/// Furthermore, after a switch to [`alloy`](https://github.com/alloy-rs)
/// this will be (hopefully) unnecessary!
#[error("conversion error! the source error is: {cause:?}")]
Conversion {
/// Internal cause for [`EnvironmentError::Conversion`] parsed as a
/// string.
cause: String,
},
#[error("conversion error! the source error is: {0}")]
Conversion(String),
}

impl Environment {
Expand Down Expand Up @@ -292,37 +276,32 @@
// Await for the condvar alert to change the state
State::Paused => {
let (lock, cvar) = &*pausevar;
let mut guard = lock.lock().map_err(|e| EnvironmentError::Pause {
cause: format!("{:?}", e),
})?;
let mut guard = lock
.lock()
.map_err(|e| EnvironmentError::Pause(format!("{:?}", e)))?;

// this logic here ensures we catch any edge case last transactions and send
// the appropriate error so that we dont hang in
// limbo forever
while let Ok((_, _, sender)) = tx_receiver.try_recv() {
let error_outcome =
TransactionOutcome::Error(EnvironmentError::Pause {
cause: "Environment is paused".into(),
});
let error_outcome = TransactionOutcome::Error(EnvironmentError::Pause(
"Environment is paused".into(),
));

Check warning on line 289 in arbiter-core/src/environment.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-core/src/environment.rs#L287-L289

Added lines #L287 - L289 were not covered by tests
let revm_result = RevmResult {
outcome: error_outcome,
block_number: convert_uint_to_u64(evm.env.block.number).map_err(
|e| EnvironmentError::Conversion {
cause: format!("{:?}", e),
},
|e| EnvironmentError::Conversion(format!("{:?}", e)),

Check warning on line 293 in arbiter-core/src/environment.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-core/src/environment.rs#L293

Added line #L293 was not covered by tests
)?,
};
sender.send(revm_result).map_err(|e| {
EnvironmentError::Communication {
cause: format!("{:?}", e),
}
})?;
sender
.send(revm_result)
.map_err(|e| EnvironmentError::Communication(format!("{:?}", e)))?;

Check warning on line 298 in arbiter-core/src/environment.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-core/src/environment.rs#L296-L298

Added lines #L296 - L298 were not covered by tests
}

while state.load(std::sync::atomic::Ordering::SeqCst) == State::Paused {
guard = cvar.wait(guard).map_err(|e| EnvironmentError::Pause {
cause: format!("{:?}", e),
})?;
guard = cvar
.wait(guard)
.map_err(|e| EnvironmentError::Pause(format!("{:?}", e)))?;
}
}

Expand Down Expand Up @@ -360,26 +339,22 @@
std::sync::atomic::Ordering::SeqCst,
);
error!("Pausing the environment labeled {} due to an execution error: {:#?}", label, e);
return Err(EnvironmentError::Execution { cause: e });
return Err(EnvironmentError::Execution(e));

Check warning on line 342 in arbiter-core/src/environment.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-core/src/environment.rs#L342

Added line #L342 was not covered by tests
}
};
let event_broadcaster = event_broadcaster.lock().map_err(|e| {
EnvironmentError::Communication {
cause: format!("{:?}", e),
}
EnvironmentError::Communication(format!("{:?}", e))

Check warning on line 346 in arbiter-core/src/environment.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-core/src/environment.rs#L346

Added line #L346 was not covered by tests
})?;
event_broadcaster.broadcast(execution_result.logs())?;
let revm_result = RevmResult {
outcome: TransactionOutcome::Success(execution_result),
block_number: convert_uint_to_u64(evm.env.block.number)
.map_err(|e| EnvironmentError::Conversion {
cause: format!("{:?}", e),
.map_err(|e| {
EnvironmentError::Conversion(format!("{:?}", e))

Check warning on line 353 in arbiter-core/src/environment.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-core/src/environment.rs#L353

Added line #L353 was not covered by tests
})?,
};
sender.send(revm_result).map_err(|e| {
EnvironmentError::Communication {
cause: format!("{:?}", e),
}
EnvironmentError::Communication(format!("{:?}", e))

Check warning on line 357 in arbiter-core/src/environment.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-core/src/environment.rs#L357

Added line #L357 was not covered by tests
})?;
counter += 1;
} else {
Expand All @@ -395,20 +370,18 @@
std::sync::atomic::Ordering::SeqCst,
);
error!("Pausing the environment labeled {} due to an execution error: {:#?}", label, e);
return Err(EnvironmentError::Execution { cause: e });
return Err(EnvironmentError::Execution(e));

Check warning on line 373 in arbiter-core/src/environment.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-core/src/environment.rs#L373

Added line #L373 was not covered by tests
}
};
let result_and_block = RevmResult {
outcome: TransactionOutcome::Success(result),
block_number: convert_uint_to_u64(evm.env.block.number)
.map_err(|e| EnvironmentError::Conversion {
cause: format!("{:?}", e),
.map_err(|e| {
EnvironmentError::Conversion(format!("{:?}", e))

Check warning on line 380 in arbiter-core/src/environment.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-core/src/environment.rs#L380

Added line #L380 was not covered by tests
})?,
};
sender.send(result_and_block).map_err(|e| {
EnvironmentError::Communication {
cause: format!("{:?}", e),
}
EnvironmentError::Communication(format!("{:?}", e))

Check warning on line 384 in arbiter-core/src/environment.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-core/src/environment.rs#L384

Added line #L384 was not covered by tests
})?;
}
}
Expand Down Expand Up @@ -531,9 +504,7 @@
for sender in &self.0 {
sender
.send(logs.clone())
.map_err(|e| EnvironmentError::Communication {
cause: format!("{:?}", e),
})?;
.map_err(|e| EnvironmentError::Communication(format!("{:?}", e)))?;
}
Ok(())
}
Expand Down
109 changes: 46 additions & 63 deletions arbiter-core/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,49 +38,31 @@
#[derive(Error, Debug)]
pub enum ManagerError {
/// Indicates that an [`Environment`] with the given label already exists.
#[error("environment labeled {label} already exists!")]
EnvironmentAlreadyExists {
/// The `label` that was attempted to be used again.
label: String,
},
#[error("environment labeled {0} already exists!")]
EnvironmentAlreadyExists(String),

/// Indicates that no [`Environment`] exists with the provided label.
#[error("environment labeled {label} does not exist!")]
EnvironmentDoesNotExist {
/// The `label` that could not be found in the [`Manager`]'s collection.
label: String,
},
#[error("environment labeled {0} does not exist!")]
EnvironmentDoesNotExist(String),

/// Indicates that the [`Environment`] with the given label is currently
/// running.
#[error("environment labeled {label} is already running!")]
EnvironmentAlreadyRunning {
/// The `label` of the [`Environment`] that is already running.
label: String,
},
#[error("environment labeled {0} is already running!")]
EnvironmentAlreadyRunning(String),

/// Indicates that the [`Environment`] with the given label is not running.
#[error("environment labeled {label} is already stopped!")]
EnvironmentNotRunning {
/// The `label` of the [`Environment`] that is not currently running.
label: String,
},
#[error("environment labeled {0} is already stopped!")]
EnvironmentNotRunning(String),

/// Indicates that the [`Environment`] with the given label has been stopped
/// and cannot be restarted or paused.
#[error("environment labeled {label} has been stopped and cannot be restarted or paused!")]
EnvironmentStopped {
/// The `label` of the [`Environment`] that is already stopped.
label: String,
},
#[error("environment labeled {0} has been stopped and cannot be restarted or paused!")]
EnvironmentStopped(String),

/// Indicates that the [`Environment`] with the given label is currently
/// paused.
#[error("environment labeled {label} is already paused!")]
EnvironmentAlreadyPaused {
/// The `label` of the [`Environment`] that is already paused.
label: String,
},
#[error("environment labeled {0} is already paused!")]
EnvironmentAlreadyPaused(String),

/// Indicates that the [`Environment`]'s thread handle could not be found.
#[error("no handle available to join the environment")]
Expand All @@ -90,6 +72,7 @@
#[error("joining on the environment thread resulted in a panic")]
ThreadPanic,
}

impl Default for Manager {
fn default() -> Self {
Self::new()
Expand Down Expand Up @@ -144,9 +127,9 @@
.get(&environment_label.clone().into())
.is_some()
{
return Err(ManagerError::EnvironmentAlreadyExists {
label: environment_label.into(),
});
return Err(ManagerError::EnvironmentAlreadyExists(
environment_label.into(),
));

Check warning on line 132 in arbiter-core/src/manager.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-core/src/manager.rs#L130-L132

Added lines #L130 - L132 were not covered by tests
}
self.environments.insert(
environment_label.clone().into(),
Expand Down Expand Up @@ -210,17 +193,17 @@
info!("Restarted environment labeled {}", environment_label.into());
Ok(())
}
State::Running => Err(ManagerError::EnvironmentAlreadyRunning {
label: environment_label.into(),
}),
State::Stopped => Err(ManagerError::EnvironmentStopped {
label: environment_label.into(),
}),
State::Running => Err(ManagerError::EnvironmentAlreadyRunning(
environment_label.into(),
)),

Check warning on line 198 in arbiter-core/src/manager.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-core/src/manager.rs#L196-L198

Added lines #L196 - L198 were not covered by tests
State::Stopped => {
Err(ManagerError::EnvironmentStopped(environment_label.into()))

Check warning on line 200 in arbiter-core/src/manager.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-core/src/manager.rs#L200

Added line #L200 was not covered by tests
}
}
}
None => Err(ManagerError::EnvironmentDoesNotExist {
label: environment_label.into(),
}),
None => Err(ManagerError::EnvironmentDoesNotExist(
environment_label.into(),
)),

Check warning on line 206 in arbiter-core/src/manager.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-core/src/manager.rs#L204-L206

Added lines #L204 - L206 were not covered by tests
}
}

Expand Down Expand Up @@ -267,27 +250,27 @@
match self.environments.get_mut(&environment_label.clone().into()) {
Some(environment) => {
match environment.state.load(std::sync::atomic::Ordering::SeqCst) {
State::Initialization => Err(ManagerError::EnvironmentNotRunning {
label: environment_label.into(),
}),
State::Initialization => Err(ManagerError::EnvironmentNotRunning(
environment_label.into(),
)),

Check warning on line 255 in arbiter-core/src/manager.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-core/src/manager.rs#L253-L255

Added lines #L253 - L255 were not covered by tests
State::Running => {
environment
.state
.store(State::Paused, std::sync::atomic::Ordering::SeqCst);
info!("Paused environment labeled {}", environment_label.into());
Ok(())
}
State::Paused => Err(ManagerError::EnvironmentAlreadyPaused {
label: environment_label.into(),
}),
State::Stopped => Err(ManagerError::EnvironmentStopped {
label: environment_label.into(),
}),
State::Paused => Err(ManagerError::EnvironmentAlreadyPaused(
environment_label.into(),
)),

Check warning on line 265 in arbiter-core/src/manager.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-core/src/manager.rs#L263-L265

Added lines #L263 - L265 were not covered by tests
State::Stopped => {
Err(ManagerError::EnvironmentStopped(environment_label.into()))

Check warning on line 267 in arbiter-core/src/manager.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-core/src/manager.rs#L267

Added line #L267 was not covered by tests
}
}
}
None => Err(ManagerError::EnvironmentDoesNotExist {
label: environment_label.into(),
}),
None => Err(ManagerError::EnvironmentDoesNotExist(
environment_label.into(),
)),

Check warning on line 273 in arbiter-core/src/manager.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-core/src/manager.rs#L271-L273

Added lines #L271 - L273 were not covered by tests
}
}

Expand Down Expand Up @@ -334,9 +317,9 @@
match self.environments.get_mut(&environment_label.clone().into()) {
Some(environment) => {
match environment.state.load(std::sync::atomic::Ordering::SeqCst) {
State::Initialization => Err(ManagerError::EnvironmentNotRunning {
label: environment_label.into(),
}),
State::Initialization => Err(ManagerError::EnvironmentNotRunning(
environment_label.into(),
)),

Check warning on line 322 in arbiter-core/src/manager.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-core/src/manager.rs#L320-L322

Added lines #L320 - L322 were not covered by tests
State::Running => {
environment
.state
Expand Down Expand Up @@ -373,14 +356,14 @@
);
Ok(())
}
State::Stopped => Err(ManagerError::EnvironmentStopped {
label: environment_label.into(),
}),
State::Stopped => {
Err(ManagerError::EnvironmentStopped(environment_label.into()))

Check warning on line 360 in arbiter-core/src/manager.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-core/src/manager.rs#L360

Added line #L360 was not covered by tests
}
}
}
None => Err(ManagerError::EnvironmentDoesNotExist {
label: environment_label.into(),
}),
None => Err(ManagerError::EnvironmentDoesNotExist(
environment_label.into(),
)),

Check warning on line 366 in arbiter-core/src/manager.rs

View check run for this annotation

Codecov / codecov/patch

arbiter-core/src/manager.rs#L364-L366

Added lines #L364 - L366 were not covered by tests
}
}
}
Expand Down
Loading
Loading