Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
docs: add some docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Aug 23, 2021
1 parent 0cad44f commit 7851f11
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions ethers-middleware/src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ use async_trait::async_trait;
use std::fmt::Debug;
use thiserror::Error;

/// Basic trait to ensure that transactions about to be sent follow certain rules.
#[async_trait]
pub trait Policy: Sync + Send + Debug {
type Error: Sync + Send + Debug;

/// Evaluates the transactions.
///
/// Returns Ok with the `tx` or an Err otherwise.
async fn ensure_can_send(&self, tx: TypedTransaction) -> Result<TypedTransaction, Self::Error>;
}

Expand All @@ -27,17 +31,18 @@ impl Policy for AllowEverything {

/// A policy that rejects all transactions.
#[derive(Debug, Clone, Copy)]
pub struct RejectAll;
pub struct RejectEverything;

#[async_trait]
impl Policy for RejectAll {
impl Policy for RejectEverything {
type Error = ();

async fn ensure_can_send(&self, _: TypedTransaction) -> Result<TypedTransaction, Self::Error> {
Err(())
}
}

/// Middleware used to enforce certain policies for transactions.
#[derive(Clone, Debug)]
pub struct PolicyMiddleware<M, P> {
pub(crate) inner: M,
Expand All @@ -50,8 +55,19 @@ impl<M: Middleware, P: Policy> FromErr<M::Error> for PolicyMiddlewareError<M, P>
}
}

impl<M, P> PolicyMiddleware<M, P>
where
M: Middleware,
P: Policy,
{
/// Creates a new client from the provider and policy.
pub fn new(inner: M, policy: P) -> Self {
Self { inner, policy }
}
}

#[derive(Error, Debug)]
/// Error thrown when the client interacts with the blockchain
/// Error thrown when the client interacts with the policy middleware.
pub enum PolicyMiddlewareError<M: Middleware, P: Policy> {
/// Thrown when the internal policy errors
#[error("{0:?}")]
Expand All @@ -75,6 +91,8 @@ where
&self.inner
}

/// This ensures the tx complies with the registered policy.
/// If so then this simply delegates the transaction to the inner middleware
async fn send_transaction<T: Into<TypedTransaction> + Send + Sync>(
&self,
tx: T,
Expand Down

0 comments on commit 7851f11

Please sign in to comment.