Eliminate trivial constructors (#1847)

pull/1883/head
Mattie Conover 2 years ago committed by GitHub
parent b9dfbdb31b
commit 03d32f4009
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      rust/Cargo.lock
  2. 1
      rust/agents/relayer/Cargo.toml
  3. 2
      rust/agents/relayer/src/msg/gas_payment/mod.rs
  4. 9
      rust/agents/relayer/src/msg/gas_payment/policies/minimum.rs
  5. 10
      rust/agents/relayer/src/msg/gas_payment/policies/none.rs
  6. 7
      rust/agents/relayer/src/msg/gelato_submitter/sponsored_call_op.rs
  7. 21
      rust/agents/relayer/src/msg/metadata_builder.rs
  8. 16
      rust/agents/relayer/src/msg/mod.rs
  9. 26
      rust/agents/relayer/src/msg/processor.rs
  10. 36
      rust/agents/relayer/src/msg/serial_submitter.rs
  11. 1
      rust/chains/hyperlane-ethereum/Cargo.toml
  12. 13
      rust/chains/hyperlane-ethereum/src/provider.rs
  13. 1
      rust/ethers-prometheus/Cargo.toml
  14. 20
      rust/ethers-prometheus/src/json_rpc_client.rs
  15. 1
      rust/hyperlane-base/Cargo.toml
  16. 26
      rust/hyperlane-base/src/contract_sync/mod.rs
  17. 25
      rust/hyperlane-base/src/interchain_gas.rs
  18. 16
      rust/hyperlane-base/src/mailbox.rs
  19. 10
      rust/hyperlane-base/src/types/checkpoint_syncer.rs
  20. 10
      rust/hyperlane-base/src/types/local_storage.rs
  21. 8
      rust/hyperlane-base/src/types/multisig.rs
  22. 19
      rust/hyperlane-base/src/types/s3_storage.rs
  23. 1
      rust/hyperlane-core/Cargo.toml
  24. 11
      rust/hyperlane-core/src/db/iterator.rs
  25. 8
      rust/hyperlane-core/src/db/typed_db.rs
  26. 1
      rust/utils/backtrace-oneline/Cargo.toml
  27. 44
      rust/utils/backtrace-oneline/src/lib.rs

17
rust/Cargo.lock generated

@ -238,6 +238,7 @@ name = "backtrace-oneline"
version = "0.1.0"
dependencies = [
"backtrace",
"derive-new",
]
[[package]]
@ -1232,6 +1233,17 @@ dependencies = [
"syn",
]
[[package]]
name = "derive-new"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3418329ca0ad70234b9735dc4ceed10af4df60eff9c8e7b06cb5e520d92c3535"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "derive_builder"
version = "0.12.0"
@ -1688,6 +1700,7 @@ version = "0.1.0"
dependencies = [
"abigen",
"async-trait",
"derive-new",
"derive_builder",
"ethers",
"futures",
@ -2721,6 +2734,7 @@ dependencies = [
"backtrace-oneline",
"color-eyre",
"config",
"derive-new",
"ethers",
"ethers-prometheus",
"eyre",
@ -2762,6 +2776,7 @@ dependencies = [
"auto_impl 1.0.1",
"bytes",
"config",
"derive-new",
"ethers",
"ethers-providers",
"ethers-signers",
@ -2791,6 +2806,7 @@ version = "0.1.0"
dependencies = [
"abigen",
"async-trait",
"derive-new",
"ethers",
"ethers-contract",
"ethers-prometheus",
@ -4230,6 +4246,7 @@ dependencies = [
"async-trait",
"coingecko",
"config",
"derive-new",
"ethers",
"ethers-contract",
"eyre",

@ -7,6 +7,7 @@ edition = "2021"
tokio = { version = "1", features = ["rt", "macros"] }
coingecko = { git = "https://github.com/hyperlane-xyz/coingecko-rs", tag = "2022-09-14-02" }
config = "~0.13.3"
derive-new = "0.5"
serde = {version = "1.0", features = ["derive"]}
serde_json = { version = "1.0", default-features = false }
ethers = { git = "https://github.com/hyperlane-xyz/ethers-rs", tag = "2023-02-10-01" }

@ -43,7 +43,7 @@ impl GasPaymentEnforcer {
db: HyperlaneDB,
) -> Self {
let policy: Box<dyn GasPaymentPolicy> = match policy_config {
GasPaymentEnforcementPolicy::None => Box::new(GasPaymentPolicyNone::new()),
GasPaymentEnforcementPolicy::None => Box::new(GasPaymentPolicyNone),
GasPaymentEnforcementPolicy::Minimum { payment } => {
Box::new(GasPaymentPolicyMinimum::new(payment))
}

@ -1,21 +1,16 @@
use async_trait::async_trait;
use derive_new::new;
use eyre::Result;
use hyperlane_core::{HyperlaneMessage, TxCostEstimate, U256};
use crate::msg::gas_payment::GasPaymentPolicy;
#[derive(Debug)]
#[derive(Debug, new)]
pub struct GasPaymentPolicyMinimum {
minimum_payment: U256,
}
impl GasPaymentPolicyMinimum {
pub fn new(minimum_payment: U256) -> Self {
Self { minimum_payment }
}
}
#[async_trait]
impl GasPaymentPolicy for GasPaymentPolicyMinimum {
/// Returns (gas payment requirement met, current payment according to the DB)

@ -6,13 +6,7 @@ use hyperlane_core::{HyperlaneMessage, TxCostEstimate, U256};
use crate::msg::gas_payment::GasPaymentPolicy;
#[derive(Debug)]
pub struct GasPaymentPolicyNone {}
impl GasPaymentPolicyNone {
pub fn new() -> Self {
Self {}
}
}
pub struct GasPaymentPolicyNone;
#[async_trait]
impl GasPaymentPolicy for GasPaymentPolicyNone {
@ -31,7 +25,7 @@ impl GasPaymentPolicy for GasPaymentPolicyNone {
async fn test_gas_payment_policy_none() {
use hyperlane_core::HyperlaneMessage;
let policy = GasPaymentPolicyNone::new();
let policy = GasPaymentPolicyNone;
let message = HyperlaneMessage::default();

@ -4,6 +4,7 @@ use std::{
time::Duration,
};
use derive_new::new;
use eyre::Result;
use tokio::{
sync::mpsc::UnboundedSender,
@ -44,7 +45,7 @@ pub struct SponsoredCallOpArgs {
pub message_processed_sender: UnboundedSender<SubmitMessageArgs>,
}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, new)]
pub struct SponsoredCallOp(SponsoredCallOpArgs);
impl Deref for SponsoredCallOp {
@ -62,10 +63,6 @@ impl DerefMut for SponsoredCallOp {
}
impl SponsoredCallOp {
pub fn new(args: SponsoredCallOpArgs) -> Self {
Self(args)
}
#[instrument(skip(self), fields(msg=?self.message.message))]
pub async fn run(&mut self) {
loop {

@ -1,7 +1,9 @@
use std::collections::HashMap;
use std::fmt::Debug;
use std::str::FromStr;
use std::sync::Arc;
use derive_new::new;
use tokio::sync::RwLock;
use tracing::{debug, info, instrument};
@ -12,16 +14,15 @@ use hyperlane_base::{
use hyperlane_core::{
HyperlaneChain, HyperlaneMessage, Mailbox, MultisigIsm, ValidatorAnnounce, H160, H256,
};
use std::str::FromStr;
use crate::merkle_tree_builder::MerkleTreeBuilder;
#[derive(Clone)]
#[derive(Clone, new)]
pub struct MetadataBuilder {
metrics: Arc<CoreMetrics>,
chain_setup: ChainSetup,
prover_sync: Arc<RwLock<MerkleTreeBuilder>>,
validator_announce: Arc<dyn ValidatorAnnounce>,
metrics: Arc<CoreMetrics>,
}
impl Debug for MetadataBuilder {
@ -35,20 +36,6 @@ impl Debug for MetadataBuilder {
}
impl MetadataBuilder {
pub fn new(
chain_setup: ChainSetup,
prover_sync: Arc<RwLock<MerkleTreeBuilder>>,
validator_announce: Arc<dyn ValidatorAnnounce>,
metrics: Arc<CoreMetrics>,
) -> Self {
MetadataBuilder {
metrics,
chain_setup,
prover_sync,
validator_announce,
}
}
#[instrument(err, skip(mailbox))]
pub async fn fetch_metadata(
&self,

@ -1,6 +1,8 @@
use std::cmp::Ordering;
use std::time::Instant;
use derive_new::new;
use hyperlane_core::HyperlaneMessage;
pub mod gas_payment;
@ -28,23 +30,15 @@ pub mod serial_submitter;
/// - FallbackProviderSubmitter (Serialized, but if some RPC provider sucks,
/// switch everyone to new one)
#[derive(Clone, Debug)]
#[derive(Clone, Debug, new)]
pub struct SubmitMessageArgs {
pub message: HyperlaneMessage,
#[new(default)]
num_retries: u32,
#[new(value = "Instant::now()")]
last_attempted_at: Instant,
}
impl SubmitMessageArgs {
pub fn new(message: HyperlaneMessage) -> Self {
SubmitMessageArgs {
message,
num_retries: 0,
last_attempted_at: Instant::now(),
}
}
}
// The run_queue implementation is a max-heap. We want the next op to
// be min over <num_retries, nonce>, so the total ordering is
// the reverse of the natural lexicographic ordering.

@ -1,5 +1,6 @@
use std::{collections::HashMap, sync::Arc, time::Duration};
use derive_new::new;
use eyre::Result;
use prometheus::IntGauge;
use tokio::{
@ -15,38 +16,19 @@ use crate::{merkle_tree_builder::MerkleTreeBuilder, settings::matching_list::Mat
use super::SubmitMessageArgs;
#[derive(Debug)]
#[derive(Debug, new)]
pub(crate) struct MessageProcessor {
db: HyperlaneDB,
whitelist: Arc<MatchingList>,
blacklist: Arc<MatchingList>,
metrics: MessageProcessorMetrics,
prover_sync: Arc<RwLock<MerkleTreeBuilder>>,
message_nonce: u32,
send_channels: HashMap<u32, UnboundedSender<SubmitMessageArgs>>,
#[new(default)]
message_nonce: u32,
}
impl MessageProcessor {
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
db: HyperlaneDB,
whitelist: Arc<MatchingList>,
blacklist: Arc<MatchingList>,
metrics: MessageProcessorMetrics,
prover_sync: Arc<RwLock<MerkleTreeBuilder>>,
send_channels: HashMap<u32, UnboundedSender<SubmitMessageArgs>>,
) -> Self {
Self {
db,
whitelist,
blacklist,
metrics,
prover_sync,
send_channels,
message_nonce: 0,
}
}
pub(crate) fn spawn(self) -> Instrumented<JoinHandle<Result<()>>> {
let span = info_span!("MessageProcessor");
tokio::spawn(async move { self.main_loop().await }).instrument(span)

@ -2,6 +2,7 @@ use std::collections::VecDeque;
use std::sync::Arc;
use std::time::{Duration, Instant};
use derive_new::new;
use eyre::{bail, Result};
use prometheus::{IntCounter, IntGauge};
use tokio::sync::mpsc::{self, error::TryRecvError};
@ -12,8 +13,9 @@ use tracing::{debug, error, info, info_span, instrument, instrument::Instrumente
use hyperlane_base::{CachingMailbox, CoreMetrics};
use hyperlane_core::{db::HyperlaneDB, HyperlaneChain, HyperlaneDomain, Mailbox, U256};
use super::metadata_builder::MetadataBuilder;
use super::{gas_payment::GasPaymentEnforcer, SubmitMessageArgs};
use super::{
gas_payment::GasPaymentEnforcer, metadata_builder::MetadataBuilder, SubmitMessageArgs,
};
/// SerialSubmitter accepts undelivered messages over a channel from a MessageProcessor. It is
/// responsible for executing the right strategy to deliver those messages to the destination
@ -102,21 +104,23 @@ use super::{gas_payment::GasPaymentEnforcer, SubmitMessageArgs};
// TODO(webbhorn): Do we also want to await finality_blocks on source chain before attempting
// submission? Does this already happen?
#[derive(Debug)]
#[derive(Debug, new)]
pub(crate) struct SerialSubmitter {
/// Used to construct the ISM metadata needed to verify a message.
metadata_builder: MetadataBuilder,
/// Receiver for new messages to submit.
rx: mpsc::UnboundedReceiver<SubmitMessageArgs>,
/// Messages we are aware of that we want to eventually submit, but haven't yet, for
/// whatever reason. They are not in any priority order, so are held in a vector.
#[new(default)]
wait_queue: Vec<SubmitMessageArgs>,
/// Messages that are in theory deliverable, but which are waiting in a queue for their turn
/// to be dispatched. The SerialSubmitter can only dispatch one message at a time, so this
/// queue could grow.
#[new(default)]
run_queue: VecDeque<SubmitMessageArgs>,
/// Mailbox on the destination chain.
mailbox: CachingMailbox,
/// Used to construct the ISM metadata needed to verify a message.
metadata_builder: MetadataBuilder,
/// Interface to agent rocks DB for e.g. writing delivery status upon completion.
db: HyperlaneDB,
/// Metrics for serial submitter.
@ -128,28 +132,6 @@ pub(crate) struct SerialSubmitter {
}
impl SerialSubmitter {
pub(crate) fn new(
rx: mpsc::UnboundedReceiver<SubmitMessageArgs>,
mailbox: CachingMailbox,
metadata_builder: MetadataBuilder,
db: HyperlaneDB,
metrics: SerialSubmitterMetrics,
gas_payment_enforcer: Arc<GasPaymentEnforcer>,
transaction_gas_limit: Option<U256>,
) -> Self {
Self {
rx,
wait_queue: Vec::new(),
run_queue: VecDeque::new(),
mailbox,
metadata_builder,
db,
metrics,
gas_payment_enforcer,
transaction_gas_limit,
}
}
pub fn spawn(mut self) -> Instrumented<JoinHandle<Result<()>>> {
tokio::spawn(async move { self.work_loop().await })
.instrument(info_span!("serial submitter work loop"))

@ -9,6 +9,7 @@ edition = "2021"
# Main block
serde = "1.0"
serde_json = { version = "1.0", default-features = false }
derive-new = "0.5"
ethers = { git = "https://github.com/hyperlane-xyz/ethers-rs", tag = "2023-02-10-01", features = ["abigen"] }
ethers-signers = { git = "https://github.com/hyperlane-xyz/ethers-rs", tag = "2023-02-10-01", features = ["aws"] }
ethers-contract = { git = "https://github.com/hyperlane-xyz/ethers-rs", tag = "2023-02-10-01", features=["legacy"] }

@ -4,6 +4,7 @@ use std::sync::Arc;
use std::time::Duration;
use async_trait::async_trait;
use derive_new::new;
use ethers::prelude::Middleware;
use tokio::time::sleep;
use tracing::instrument;
@ -18,7 +19,7 @@ use crate::BuildableWithProvider;
/// Connection to an ethereum provider. Useful for querying information about
/// the blockchain.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, new)]
pub struct EthereumProvider<M>
where
M: Middleware,
@ -27,16 +28,6 @@ where
domain: HyperlaneDomain,
}
impl<M> EthereumProvider<M>
where
M: Middleware,
{
/// Creates a new EthereumProvider
pub fn new(provider: Arc<M>, domain: HyperlaneDomain) -> Self {
Self { provider, domain }
}
}
impl<M> HyperlaneChain for EthereumProvider<M>
where
M: Middleware + 'static,

@ -8,6 +8,7 @@ edition = "2021"
prometheus = "0.13"
ethers = { git = "https://github.com/hyperlane-xyz/ethers-rs", tag = "2023-02-10-01" }
derive_builder = "0.12"
derive-new = "0.5"
async-trait = { version = "0.1", default-features = false }
futures = "0.3"
parking_lot = { version = "0.12" }

@ -6,11 +6,11 @@ use std::time::Instant;
use async_trait::async_trait;
use derive_builder::Builder;
use derive_new::new;
use ethers::prelude::JsonRpcClient;
use maplit::hashmap;
use prometheus::{CounterVec, IntCounterVec};
use serde::de::DeserializeOwned;
use serde::Serialize;
use serde::{de::DeserializeOwned, Serialize};
pub use crate::ChainInfo;
@ -95,27 +95,13 @@ impl PrometheusJsonRpcClientConfig {
/// metrics. To make this as flexible as possible, the metric vecs need to be
/// created and named externally, they should follow the naming convention here
/// and must include the described labels.
#[derive(new)]
pub struct PrometheusJsonRpcClient<C> {
inner: C,
metrics: JsonRpcClientMetrics,
config: PrometheusJsonRpcClientConfig,
}
impl<C> PrometheusJsonRpcClient<C> {
/// Wrap a JsonRpcClient with metrics.
pub fn new(
client: C,
metrics: JsonRpcClientMetrics,
config: PrometheusJsonRpcClientConfig,
) -> Self {
Self {
inner: client,
metrics,
config,
}
}
}
impl<C> Debug for PrometheusJsonRpcClient<C>
where
C: JsonRpcClient,

@ -22,6 +22,7 @@ tracing-futures = "0.2"
tracing-subscriber = { version = "0.3", features = ["json"] }
rocksdb = "0.20"
mockall = "0.11"
derive-new = "0.5"
backtrace = { version = "0.3", optional = true }
backtrace-oneline = { path = "../utils/backtrace-oneline", optional = true }

@ -1,9 +1,10 @@
// TODO: Reapply tip buffer
// TODO: Reapply metrics
use derive_new::new;
pub use cursor::*;
use hyperlane_core::db::HyperlaneDB;
use hyperlane_core::HyperlaneDomain;
use hyperlane_core::{db::HyperlaneDB, HyperlaneDomain};
pub use interchain_gas::*;
pub use mailbox::*;
pub use metrics::ContractSyncMetrics;
@ -22,7 +23,7 @@ mod schema;
/// Extracts chain-specific data (emitted checkpoints, messages, etc) from an
/// `indexer` and fills the agent's db with this data. A CachingMailbox
/// will use a contract sync to spawn syncing tasks to keep the db up-to-date.
#[derive(Debug)]
#[derive(Debug, new)]
pub struct ContractSync<I> {
domain: HyperlaneDomain,
db: HyperlaneDB,
@ -30,22 +31,3 @@ pub struct ContractSync<I> {
index_settings: IndexSettings,
metrics: ContractSyncMetrics,
}
impl<I> ContractSync<I> {
/// Instantiate new ContractSync
pub fn new(
domain: HyperlaneDomain,
db: HyperlaneDB,
indexer: I,
index_settings: IndexSettings,
metrics: ContractSyncMetrics,
) -> Self {
Self {
domain,
db,
indexer,
index_settings,
metrics,
}
}
}

@ -1,20 +1,18 @@
use std::fmt::Debug;
use std::sync::Arc;
use derive_new::new;
use eyre::Result;
use futures_util::future::select_all;
use tokio::task::JoinHandle;
use tracing::instrument::Instrumented;
use tracing::{info_span, Instrument};
use tracing::{info_span, instrument::Instrumented, Instrument};
use hyperlane_core::db::HyperlaneDB;
use hyperlane_core::{InterchainGasPaymaster, InterchainGasPaymasterIndexer};
use hyperlane_core::{db::HyperlaneDB, InterchainGasPaymaster, InterchainGasPaymasterIndexer};
use crate::chains::IndexSettings;
use crate::{ContractSync, ContractSyncMetrics};
use crate::{chains::IndexSettings, ContractSync, ContractSyncMetrics};
/// Caching InterchainGasPaymaster type
#[derive(Debug, Clone)]
#[derive(Debug, Clone, new)]
pub struct CachingInterchainGasPaymaster {
paymaster: Arc<dyn InterchainGasPaymaster>,
db: HyperlaneDB,
@ -28,19 +26,6 @@ impl std::fmt::Display for CachingInterchainGasPaymaster {
}
impl CachingInterchainGasPaymaster {
/// Instantiate new CachingInterchainGasPaymaster
pub fn new(
paymaster: Arc<dyn InterchainGasPaymaster>,
db: HyperlaneDB,
indexer: Arc<dyn InterchainGasPaymasterIndexer>,
) -> Self {
Self {
paymaster,
db,
indexer,
}
}
/// Return handle on paymaster object
pub fn paymaster(&self) -> &Arc<dyn InterchainGasPaymaster> {
&self.paymaster

@ -3,6 +3,7 @@ use std::num::NonZeroU64;
use std::sync::Arc;
use async_trait::async_trait;
use derive_new::new;
use futures_util::future::select_all;
use tokio::task::JoinHandle;
use tracing::instrument::Instrumented;
@ -18,7 +19,7 @@ use crate::chains::IndexSettings;
use crate::{ContractSync, ContractSyncMetrics};
/// Caching Mailbox type
#[derive(Debug, Clone)]
#[derive(Debug, Clone, new)]
pub struct CachingMailbox {
mailbox: Arc<dyn Mailbox>,
db: HyperlaneDB,
@ -32,19 +33,6 @@ impl std::fmt::Display for CachingMailbox {
}
impl CachingMailbox {
/// Instantiate new CachingMailbox
pub fn new(
mailbox: Arc<dyn Mailbox>,
db: HyperlaneDB,
indexer: Arc<dyn MailboxIndexer>,
) -> Self {
Self {
mailbox,
db,
indexer,
}
}
/// Return handle on mailbox object
pub fn mailbox(&self) -> &Arc<dyn Mailbox> {
&self.mailbox

@ -68,11 +68,13 @@ impl CheckpointSyncerConf {
) -> Result<Box<dyn CheckpointSyncer>, Report> {
Ok(match self {
CheckpointSyncerConf::LocalStorage { path } => {
Box::new(LocalStorage::new(path, latest_index_gauge))
}
CheckpointSyncerConf::S3 { bucket, region } => {
Box::new(S3Storage::new(bucket, region.parse()?, latest_index_gauge))
Box::new(LocalStorage::new(path.clone(), latest_index_gauge))
}
CheckpointSyncerConf::S3 { bucket, region } => Box::new(S3Storage::new(
bucket.clone(),
region.parse()?,
latest_index_gauge,
)),
})
}
}

@ -1,4 +1,5 @@
use async_trait::async_trait;
use derive_new::new;
use eyre::{Context, Result};
use prometheus::IntGauge;
@ -6,7 +7,7 @@ use hyperlane_core::{SignedAnnouncement, SignedCheckpoint};
use crate::traits::CheckpointSyncer;
#[derive(Debug, Clone)]
#[derive(Debug, Clone, new)]
/// Type for reading/write to LocalStorage
pub struct LocalStorage {
/// base path
@ -15,13 +16,6 @@ pub struct LocalStorage {
}
impl LocalStorage {
/// Constructor
pub fn new(path: &str, latest_index: Option<IntGauge>) -> Self {
LocalStorage {
path: path.to_owned(),
latest_index,
}
}
fn checkpoint_file_path(&self, index: u32) -> String {
format!("{}/{index}.json", self.path)
}

@ -1,6 +1,7 @@
use std::collections::{hash_map::Entry, HashMap};
use std::sync::Arc;
use derive_new::new;
use ethers::prelude::Address;
use eyre::Result;
use tracing::{debug, instrument};
@ -10,18 +11,13 @@ use hyperlane_core::{MultisigSignedCheckpoint, SignedCheckpointWithSigner, H160,
use crate::CheckpointSyncer;
/// Fetches signed checkpoints from multiple validators to create MultisigSignedCheckpoints
#[derive(Clone, Debug)]
#[derive(Clone, Debug, new)]
pub struct MultisigCheckpointSyncer {
/// The checkpoint syncer for each valid validator signer address
checkpoint_syncers: HashMap<Address, Arc<dyn CheckpointSyncer>>,
}
impl MultisigCheckpointSyncer {
/// Constructor
pub fn new(checkpoint_syncers: HashMap<Address, Arc<dyn CheckpointSyncer>>) -> Self {
MultisigCheckpointSyncer { checkpoint_syncers }
}
/// Attempts to get the latest checkpoint with a quorum of signatures among validators.
///
/// First iterates through the `latest_index` of each validator's checkpoint syncer,

@ -1,9 +1,9 @@
use std::{fmt, time::Duration};
use async_trait::async_trait;
use derive_new::new;
use eyre::{bail, Result};
use futures_util::TryStreamExt;
use hyperlane_core::{SignedAnnouncement, SignedCheckpoint};
use once_cell::sync::OnceCell;
use prometheus::IntGauge;
use rusoto_core::{
@ -13,6 +13,8 @@ use rusoto_core::{
use rusoto_s3::{GetObjectError, GetObjectRequest, PutObjectRequest, S3Client, S3};
use tokio::time::timeout;
use hyperlane_core::{SignedAnnouncement, SignedCheckpoint};
use crate::CheckpointSyncer;
/// The timeout for S3 requests. Rusoto doesn't offer timeout configuration
@ -20,7 +22,7 @@ use crate::CheckpointSyncer;
/// See https://github.com/rusoto/rusoto/issues/1795.
const S3_REQUEST_TIMEOUT_SECONDS: u64 = 30;
#[derive(Clone)]
#[derive(Clone, new)]
/// Type for reading/writing to S3
pub struct S3Storage {
/// The name of the bucket.
@ -28,8 +30,10 @@ pub struct S3Storage {
/// The region of the bucket.
region: Region,
/// A client with AWS credentials.
#[new(default)]
authenticated_client: OnceCell<S3Client>,
/// A client without credentials for anonymous requests.
#[new(default)]
anonymous_client: OnceCell<S3Client>,
/// The latest seen signed checkpoint index.
latest_index: Option<IntGauge>,
@ -45,17 +49,6 @@ impl fmt::Debug for S3Storage {
}
impl S3Storage {
/// constructor
pub fn new(bucket: &str, region: Region, latest_index: Option<IntGauge>) -> Self {
Self {
bucket: bucket.to_owned(),
region,
authenticated_client: OnceCell::new(),
anonymous_client: OnceCell::new(),
latest_index,
}
}
async fn write_to_bucket(&self, key: String, body: &str) -> Result<()> {
let req = PutObjectRequest {
key,

@ -26,6 +26,7 @@ bytes = { version = "1", features = ["serde"]}
num = {version="0", features=["serde"]}
num-traits = "0.2"
num-derive = "0.3"
derive-new = "0.5"
strum = "0.24"
strum_macros = "0.24"

@ -9,17 +9,6 @@ pub struct PrefixIterator<'a, V> {
_phantom: PhantomData<*const V>,
}
impl<'a, V> PrefixIterator<'a, V> {
/// Return new prefix iterator
pub fn new(iter: DBIterator<'a>, prefix: &'a [u8]) -> Self {
Self {
iter,
prefix,
_phantom: PhantomData,
}
}
}
impl<'a, V> Iterator for PrefixIterator<'a, V>
where
V: Encode + Decode,

@ -1,12 +1,13 @@
use crate::db::{DbError, DB};
use crate::{Decode, Encode};
use derive_new::new;
type Result<T> = std::result::Result<T, DbError>;
/// DB handle for storing data tied to a specific type/entity.
///
/// Key structure: ```<type_prefix>_<additional_prefix(es)>_<key>```
#[derive(Debug, Clone)]
#[derive(Debug, Clone, new)]
pub struct TypedDB {
entity: String,
db: DB,
@ -19,11 +20,6 @@ impl AsRef<DB> for TypedDB {
}
impl TypedDB {
/// Instantiate new `TypedDB`
pub fn new(entity: String, db: DB) -> Self {
Self { entity, db }
}
fn full_prefix(&self, prefix: impl AsRef<[u8]>) -> Vec<u8> {
let mut full_prefix = vec![];
full_prefix.extend(self.entity.as_ref() as &[u8]);

@ -7,3 +7,4 @@ edition = "2021"
[dependencies]
backtrace = "0.3"
derive-new = "0.5"

@ -1,11 +1,12 @@
//! This is a port of the backtrace print.rs code which generates one-line backtraces. A lot of
//! simplifications were made for our specific use case.
//! This is a port of the backtrace print.rs code which generates one-line
//! backtraces. A lot of simplifications were made for our specific use case.
use std::ffi::c_void;
use std::fmt::{Display, Formatter};
use std::{env, fmt};
use backtrace::{Backtrace, BacktraceFrame, BacktraceSymbol, BytesOrWideString, SymbolName};
use derive_new::new;
/// Format a backtrace onto a single line.
/// Largely stolen from backtrace's Debug implementation.
@ -46,41 +47,26 @@ pub fn fmt_backtrace(
/// This type can be used to print a backtrace regardless of where the backtrace
/// itself comes from. If you have a `Backtrace` type then its `Debug`
/// implementation already uses this printing format.
///
/// The `format` will control the style in which the backtrace is
/// printed, and the `print_path` will be used to print the
/// `BytesOrWideString` instances of filenames. This type itself doesn't do
/// any printing of filenames, but this callback is required to do so.
#[derive(new)]
struct BacktraceFmt<'a, 'b> {
fmt: &'a mut fmt::Formatter<'b>,
fmt: &'a mut Formatter<'b>,
#[new(default)]
frame_index: usize,
print_path:
&'a mut (dyn FnMut(&mut fmt::Formatter<'_>, BytesOrWideString<'_>) -> fmt::Result + 'b),
line_separator: &'static str,
print_path: &'a mut (dyn FnMut(&mut Formatter<'_>, BytesOrWideString<'_>) -> fmt::Result + 'b),
}
impl<'a, 'b> BacktraceFmt<'a, 'b> {
/// Create a new `BacktraceFmt` which will write output to the provided
/// `fmt`.
///
/// The `format` argument will control the style in which the backtrace is
/// printed, and the `print_path` argument will be used to print the
/// `BytesOrWideString` instances of filenames. This type itself doesn't do
/// any printing of filenames, but this callback is required to do so.
fn new(
fmt: &'a mut fmt::Formatter<'b>,
line_separator: &'static str,
print_path: &'a mut (dyn FnMut(&mut fmt::Formatter<'_>, BytesOrWideString<'_>) -> fmt::Result
+ 'b),
) -> Self {
BacktraceFmt {
fmt,
frame_index: 0,
line_separator,
print_path,
}
}
/// Adds a frame to the backtrace output.
///
/// This commit returns an RAII instance of a `BacktraceFrameFmt` which can be used
/// to actually print a frame, and on destruction it will increment the
/// frame counter.
/// This commit returns an RAII instance of a `BacktraceFrameFmt` which can
/// be used to actually print a frame, and on destruction it will
/// increment the frame counter.
fn frame(&mut self) -> BacktraceFrameFmt<'_, 'a, 'b> {
BacktraceFrameFmt {
fmt: self,

Loading…
Cancel
Save