Skip to content

Commit

Permalink
Better debug logging (#7)
Browse files Browse the repository at this point in the history
* Update debug to be more readable in logs

* cleanup

* Address display

* ens display
  • Loading branch information
mattiekat authored Feb 10, 2023
1 parent 2b35ad9 commit fe5d882
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 10 deletions.
12 changes: 11 additions & 1 deletion ethers-core/src/types/address_or_bytes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::types::{Address, Bytes};
use std::fmt::Debug;

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, PartialEq, Eq)]
/// A type that can either be an `Address` or `Bytes`.
pub enum AddressOrBytes {
/// An address type
Expand All @@ -9,6 +10,15 @@ pub enum AddressOrBytes {
Bytes(Bytes),
}

impl Debug for AddressOrBytes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AddressOrBytes::Address(addr) => write!(f, "{addr:?}"),
AddressOrBytes::Bytes(bytes) => write!(f, "0x{}", hex::encode(bytes)),
}
}
}

impl From<Address> for AddressOrBytes {
fn from(s: Address) -> Self {
Self::Address(s)
Expand Down
13 changes: 11 additions & 2 deletions ethers-core/src/types/ens.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
use crate::types::Address;
use rlp::{Decodable, Encodable, RlpStream};
use serde::{ser::Error as SerializationError, Deserialize, Deserializer, Serialize, Serializer};
use std::{cmp::Ordering, convert::Infallible, str::FromStr};
use std::{cmp::Ordering, convert::Infallible, fmt::Debug, str::FromStr};

/// ENS name or Ethereum Address. Not RLP encoded/serialized if it's a name.
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, PartialEq, Eq)]
pub enum NameOrAddress {
/// An ENS Name (format does not get checked)
Name(String),
/// An Ethereum Address
Address(Address),
}

impl Debug for NameOrAddress {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
NameOrAddress::Name(name) => write!(f, "\"{name}\""),
NameOrAddress::Address(addr) => write!(f, "{addr:?}"),
}
}
}

// Only RLP encode the Address variant since it doesn't make sense to ever RLP encode
// an ENS name
impl Encodable for &NameOrAddress {
Expand Down
7 changes: 5 additions & 2 deletions ethers-middleware/src/nonce_manager.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use async_trait::async_trait;
use ethers_core::types::{transaction::eip2718::TypedTransaction, *};
use ethers_providers::{FromErr, Middleware, PendingTransaction};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::{
fmt::Debug,
sync::atomic::{AtomicBool, AtomicU64, Ordering},
};
use thiserror::Error;

#[derive(Debug)]
/// Middleware used for calculating nonces locally, useful for signing multiple
/// consecutive transactions without waiting for them to hit the mempool
pub struct NonceManagerMiddleware<M> {
inner: M,
initialized: AtomicBool,
nonce: AtomicU64,
address: Address,
inner: M,
}

impl<M> NonceManagerMiddleware<M>
Expand Down
2 changes: 1 addition & 1 deletion ethers-middleware/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ use thiserror::Error;
///
/// [`Signer`]: ethers_signers::Signer
pub struct SignerMiddleware<M, S> {
pub(crate) inner: M,
pub(crate) signer: S,
pub(crate) address: Address,
pub(crate) inner: M,
}

impl<M: Middleware, S: Signer> FromErr<M::Error> for SignerMiddlewareError<M, S> {
Expand Down
12 changes: 11 additions & 1 deletion ethers-providers/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl FromStr for NodeClient {
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct Provider<P> {
inner: P,
ens: Option<Address>,
Expand All @@ -110,6 +110,16 @@ impl FromErr<ProviderError> for ProviderError {
}
}

impl<P: Debug> Debug for Provider<P> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Provider {{ ens: {:?}, interval: {:?}, from: {:?}, inner: {:?} }}",
self.ens, self.interval, self.from, self.inner
)
}
}

#[derive(Debug, Error)]
/// An error thrown when making a call to the provider
pub enum ProviderError {
Expand Down
8 changes: 7 additions & 1 deletion ethers-providers/src/transports/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use async_trait::async_trait;
use reqwest::{header::HeaderValue, Client, Error as ReqwestError};
use serde::{de::DeserializeOwned, Serialize};
use std::{
fmt::Debug,
str::FromStr,
sync::atomic::{AtomicU64, Ordering},
};
Expand All @@ -28,13 +29,18 @@ use super::common::{Authorization, JsonRpcError, Request, Response};
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct Provider {
id: AtomicU64,
client: Client,
url: Url,
}

impl Debug for Provider {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Http {{ id: {:?}, url: {} }}", self.id, self.url)
}
}

#[derive(Error, Debug)]
/// Error thrown when sending an HTTP request
pub enum ClientError {
Expand Down
4 changes: 3 additions & 1 deletion ethers-providers/src/transports/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ mod ws;
pub use ws::{ClientError as WsClientError, Ws};

mod quorum;
pub use quorum::{JsonRpcClientWrapper, Quorum, QuorumError, QuorumProvider, WeightedProvider, WrappedParams};
pub use quorum::{
JsonRpcClientWrapper, Quorum, QuorumError, QuorumProvider, WeightedProvider, WrappedParams,
};

mod rw;
pub use rw::{RwClient, RwClientError};
Expand Down
2 changes: 1 addition & 1 deletion ethers-providers/src/transports/quorum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,8 @@ impl<'a, T> Future for QuorumRequest<'a, T> {
/// The configuration of a provider for the `QuorumProvider`
#[derive(Debug, Clone)]
pub struct WeightedProvider<T> {
inner: T,
weight: u64,
inner: T,
}

impl<T> WeightedProvider<T> {
Expand Down

0 comments on commit fe5d882

Please sign in to comment.