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

feat: block type enum #480

Merged
merged 11 commits into from
Sep 7, 2023
Prev Previous commit
Next Next commit
refactored middleware.rs
  • Loading branch information
Autoparallel committed Sep 5, 2023
commit 257bb34c67393edbbeef114999c4fe7897311006
110 changes: 39 additions & 71 deletions arbiter-core/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,18 @@ pub struct RevmMiddleware {
/// [GitHub](https://github.com/primitivefinance/arbiter/).
#[derive(Error, Debug)]
pub enum RevmMiddlewareError {
/// An error occurred while attempting to interact with the environment.
#[error("an error came from the environment! due to: {0}")]
Environment(#[from] crate::environment::EnvironmentError),

/// An error occurred while attempting to send a transaction.
#[error("failed to send transaction! due to: {0}")]
Send(String),

/// There was an issue receiving an [`ExecutionResult`], possibly from
/// another service or module.
#[error("failed to receive `ExecutionResult`! due to: {0}")]
Receive(String),
Receive(#[from] crossbeam_channel::RecvError),

/// There was a failure trying to obtain a lock on the [`EventBroadcaster`],
/// possibly due to concurrency issues.
Expand Down Expand Up @@ -302,52 +306,33 @@ impl Middleware for RevmMiddleware {
))
.map_err(|e| RevmMiddlewareError::Send(e.to_string()))?;

let revm_result = self
.provider()
.as_ref()
.result_receiver
.recv()
.map_err(|e| RevmMiddlewareError::Receive(e.to_string()))?;

match revm_result.outcome {
TransactionOutcome::Success(execution_result) => {
let Success {
_reason: _,
_gas_used: _,
_gas_refunded: _,
logs,
output,
} = unpack_execution_result(execution_result)?;

match output {
Output::Create(_, address) => {
let address = address.ok_or(RevmMiddlewareError::MissingData(
"Address missing in transaction!".to_string(),
))?;
let mut pending_tx =
PendingTransaction::new(ethers::types::H256::zero(), self.provider());
pending_tx.state =
PendingTxState::RevmDeployOutput(recast_address(address));
return Ok(pending_tx);
}
Output::Call(_) => {
let mut pending_tx =
PendingTransaction::new(ethers::types::H256::zero(), self.provider());
let (execution_result, block_number) =
self.provider().as_ref().result_receiver.recv()??;

pending_tx.state =
PendingTxState::RevmTransactOutput(logs, revm_result.block_number);
return Ok(pending_tx);
}
}
let Success {
_reason: _,
_gas_used: _,
_gas_refunded: _,
logs,
output,
} = unpack_execution_result(execution_result)?;

match output {
Output::Create(_, address) => {
let address = address.ok_or(RevmMiddlewareError::MissingData(
"Address missing in transaction!".to_string(),
))?;
let mut pending_tx =
PendingTransaction::new(ethers::types::H256::zero(), self.provider());
pending_tx.state = PendingTxState::RevmDeployOutput(recast_address(address));
return Ok(pending_tx);
}
TransactionOutcome::Error(err) => {
return Err(RevmMiddlewareError::Receive(
format!(
"Error recieving response from the environement with environment error: {}",
err
)
.to_string(),
));
Output::Call(_) => {
let mut pending_tx =
PendingTransaction::new(ethers::types::H256::zero(), self.provider());

pending_tx.state = PendingTxState::RevmTransactOutput(logs, block_number);
return Ok(pending_tx);
}
}
}
Expand Down Expand Up @@ -410,33 +395,16 @@ impl Middleware for RevmMiddleware {
self.provider().as_ref().result_sender.clone(),
))
.map_err(|e| RevmMiddlewareError::Send(e.to_string()))?;
let revm_result = self
.provider()
.as_ref()
.result_receiver
.recv()
.map_err(|e| RevmMiddlewareError::Receive(e.to_string()))?;

match revm_result.outcome {
TransactionOutcome::Success(execution_result) => {
let output = unpack_execution_result(execution_result)?.output;
match output {
Output::Create(bytes, ..) => {
return Ok(Bytes::from(bytes.to_vec()));
}
Output::Call(bytes) => {
return Ok(Bytes::from(bytes.to_vec()));
}
}
let (execution_result, _block_number) =
self.provider().as_ref().result_receiver.recv()??;

let output = unpack_execution_result(execution_result)?.output;
match output {
Output::Create(bytes, ..) => {
return Ok(Bytes::from(bytes.to_vec()));
}
TransactionOutcome::Error(err) => {
return Err(RevmMiddlewareError::Receive(
format!(
"Error recieving response from the environement with environment error: {}",
err
)
.to_string(),
));
Output::Call(bytes) => {
return Ok(Bytes::from(bytes.to_vec()));
}
}
}
Expand Down