From a0c5d111c04620ace1ef65a9860965c62598b271 Mon Sep 17 00:00:00 2001 From: Amin Moghaddam Date: Mon, 3 Jun 2024 15:44:00 +0200 Subject: [PATCH] fix(fortuna): Respect legacy tx while calling fill_transaction (#1649) --- apps/fortuna/Cargo.lock | 75 +++++++++++----------- apps/fortuna/Cargo.toml | 3 +- apps/fortuna/src/chain/ethereum.rs | 99 ++++++++++++++++++++++++------ 3 files changed, 119 insertions(+), 58 deletions(-) diff --git a/apps/fortuna/Cargo.lock b/apps/fortuna/Cargo.lock index e13d7bb11..b227ce607 100644 --- a/apps/fortuna/Cargo.lock +++ b/apps/fortuna/Cargo.lock @@ -150,7 +150,7 @@ checksum = "a66537f1bb974b254c98ed142ff995236e81b9d0fe4db0575f46612cb15eb0f9" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.66", ] [[package]] @@ -250,7 +250,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.66", ] [[package]] @@ -477,7 +477,7 @@ checksum = "965ab7eb5f8f97d2a083c799f3a1b994fc397b2fe2da5d1da1626ce15a39f2b1" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.66", ] [[package]] @@ -618,7 +618,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.66", ] [[package]] @@ -844,7 +844,7 @@ dependencies = [ "proc-macro2", "quote", "strsim", - "syn 2.0.38", + "syn 2.0.66", ] [[package]] @@ -855,7 +855,7 @@ checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" dependencies = [ "darling_core", "quote", - "syn 2.0.38", + "syn 2.0.66", ] [[package]] @@ -1224,7 +1224,7 @@ dependencies = [ "reqwest", "serde", "serde_json", - "syn 2.0.38", + "syn 2.0.66", "toml 0.8.12", "walkdir", ] @@ -1242,7 +1242,7 @@ dependencies = [ "proc-macro2", "quote", "serde_json", - "syn 2.0.38", + "syn 2.0.66", ] [[package]] @@ -1268,7 +1268,7 @@ dependencies = [ "serde", "serde_json", "strum 0.26.2", - "syn 2.0.38", + "syn 2.0.66", "tempfile", "thiserror", "tiny-keccak", @@ -1502,7 +1502,7 @@ dependencies = [ [[package]] name = "fortuna" -version = "6.2.1" +version = "6.2.2" dependencies = [ "anyhow", "axum", @@ -1530,6 +1530,7 @@ dependencies = [ "serde_with", "serde_yaml", "sha3", + "thiserror", "tokio", "tower-http", "tracing", @@ -1621,7 +1622,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.66", ] [[package]] @@ -2404,7 +2405,7 @@ dependencies = [ "proc-macro-crate 1.3.1", "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.66", ] [[package]] @@ -2470,7 +2471,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.66", ] [[package]] @@ -2656,7 +2657,7 @@ dependencies = [ "phf_shared 0.11.2", "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.66", ] [[package]] @@ -2694,7 +2695,7 @@ checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.66", ] [[package]] @@ -2744,7 +2745,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae005bd773ab59b4725093fd7df83fd7892f7d8eafb48dbd7de6e024e4215f9d" dependencies = [ "proc-macro2", - "syn 2.0.38", + "syn 2.0.66", ] [[package]] @@ -2806,9 +2807,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.68" +version = "1.0.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b1106fec09662ec6dd98ccac0f81cef56984d0b49f75c92d8cbad76e20c005c" +checksum = "22244ce15aa966053a896d1accb3a6e68469b97c7f33f284b99f0d576879fc23" dependencies = [ "unicode-ident", ] @@ -2833,7 +2834,7 @@ checksum = "440f724eba9f6996b75d63681b0a92b06947f1457076d503a4d2e2c8f56442b8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.66", ] [[package]] @@ -2856,9 +2857,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.33" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] @@ -3122,7 +3123,7 @@ dependencies = [ "quote", "rust-embed-utils", "shellexpand", - "syn 2.0.38", + "syn 2.0.66", "walkdir", ] @@ -3367,7 +3368,7 @@ checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.66", ] [[package]] @@ -3451,7 +3452,7 @@ dependencies = [ "darling", "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.66", ] [[package]] @@ -3694,7 +3695,7 @@ dependencies = [ "proc-macro2", "quote", "rustversion", - "syn 2.0.38", + "syn 2.0.66", ] [[package]] @@ -3736,9 +3737,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.38" +version = "2.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b" +checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5" dependencies = [ "proc-macro2", "quote", @@ -3804,22 +3805,22 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.49" +version = "1.0.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1177e8c6d7ede7afde3585fd2513e611227efd6481bd78d2e82ba1ce16557ed4" +checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.49" +version = "1.0.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10712f02019e9288794769fba95cd6847df9874d49d871d062172f9dd41bc4cc" +checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.66", ] [[package]] @@ -3911,7 +3912,7 @@ checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.66", ] [[package]] @@ -4084,7 +4085,7 @@ checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.66", ] [[package]] @@ -4273,7 +4274,7 @@ dependencies = [ "proc-macro2", "quote", "regex", - "syn 2.0.38", + "syn 2.0.66", ] [[package]] @@ -4366,7 +4367,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.66", "wasm-bindgen-shared", ] @@ -4400,7 +4401,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.66", "wasm-bindgen-backend", "wasm-bindgen-shared", ] diff --git a/apps/fortuna/Cargo.toml b/apps/fortuna/Cargo.toml index 8657147cf..d39741dc1 100644 --- a/apps/fortuna/Cargo.toml +++ b/apps/fortuna/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fortuna" -version = "6.2.1" +version = "6.2.2" edition = "2021" [dependencies] @@ -36,6 +36,7 @@ lazy_static = "1.4.0" url = "2.5.0" chrono = { version = "0.4.38", features = ["clock", "std"] , default-features = false} backoff = { version = "0.4.0", features = ["futures", "tokio"] } +thiserror = "1.0.61" [dev-dependencies] diff --git a/apps/fortuna/src/chain/ethereum.rs b/apps/fortuna/src/chain/ethereum.rs index 9a9509329..2e33e5a1c 100644 --- a/apps/fortuna/src/chain/ethereum.rs +++ b/apps/fortuna/src/chain/ethereum.rs @@ -28,15 +28,15 @@ use { core::types::Address, middleware::{ gas_oracle::GasOracleMiddleware, - transformer::{ - Transformer, - TransformerError, - TransformerMiddleware, - }, + MiddlewareError, NonceManagerMiddleware, SignerMiddleware, }, - prelude::TransactionRequest, + prelude::{ + BlockId, + PendingTransaction, + TransactionRequest, + }, providers::{ Http, Middleware, @@ -57,6 +57,7 @@ use { Keccak256, }, std::sync::Arc, + thiserror::Error, }; // TODO: Programmatically generate this so we don't have to keep committed ABI in sync with the @@ -67,31 +68,92 @@ abigen!( ); pub type SignablePythContract = PythRandom< - TransformerMiddleware< + LegacyTxMiddleware< GasOracleMiddleware< NonceManagerMiddleware, LocalWallet>>, EthProviderOracle>, >, - LegacyTxTransformer, >, >; pub type PythContract = PythRandom>; -/// Transformer that converts a transaction into a legacy transaction if use_legacy_tx is true. +/// Middleware that converts a transaction into a legacy transaction if use_legacy_tx is true. +/// We can not use TransformerMiddleware because keeper calls fill_transaction first which bypasses +/// the transformer. #[derive(Clone, Debug)] -pub struct LegacyTxTransformer { +pub struct LegacyTxMiddleware { use_legacy_tx: bool, + inner: M, +} + +impl LegacyTxMiddleware { + pub fn new(use_legacy_tx: bool, inner: M) -> Self { + Self { + use_legacy_tx, + inner, + } + } +} + + +#[derive(Error, Debug)] +pub enum LegacyTxMiddlewareError { + #[error("{0}")] + MiddlewareError(M::Error), +} + +impl MiddlewareError for LegacyTxMiddlewareError { + type Inner = M::Error; + + fn from_err(src: M::Error) -> Self { + LegacyTxMiddlewareError::MiddlewareError(src) + } + + fn as_inner(&self) -> Option<&Self::Inner> { + match self { + LegacyTxMiddlewareError::MiddlewareError(e) => Some(e), + } + } } -impl Transformer for LegacyTxTransformer { - fn transform(&self, tx: &mut TypedTransaction) -> Result<(), TransformerError> { +#[async_trait] +impl Middleware for LegacyTxMiddleware { + type Error = LegacyTxMiddlewareError; + type Provider = M::Provider; + type Inner = M; + fn inner(&self) -> &M { + &self.inner + } + + async fn send_transaction + Send + Sync>( + &self, + tx: T, + block: Option, + ) -> std::result::Result, Self::Error> { + let mut tx = tx.into(); + if self.use_legacy_tx { + let legacy_request: TransactionRequest = tx.into(); + tx = legacy_request.into(); + } + self.inner() + .send_transaction(tx, block) + .await + .map_err(MiddlewareError::from_err) + } + + async fn fill_transaction( + &self, + tx: &mut TypedTransaction, + block: Option, + ) -> std::result::Result<(), Self::Error> { if self.use_legacy_tx { let legacy_request: TransactionRequest = (*tx).clone().into(); *tx = legacy_request.into(); - Ok(()) - } else { - Ok(()) } + self.inner() + .fill_transaction(tx, block) + .await + .map_err(MiddlewareError::from_err) } } @@ -103,9 +165,6 @@ impl SignablePythContract { let provider = Provider::::try_from(&chain_config.geth_rpc_addr)?; let chain_id = provider.get_chainid().await?; let gas_oracle = EthProviderOracle::new(provider.clone()); - let transformer = LegacyTxTransformer { - use_legacy_tx: chain_config.legacy_tx, - }; let wallet__ = private_key .parse::()? .with_chain_id(chain_id.as_u64()); @@ -114,12 +173,12 @@ impl SignablePythContract { Ok(PythRandom::new( chain_config.contract_addr, - Arc::new(TransformerMiddleware::new( + Arc::new(LegacyTxMiddleware::new( + chain_config.legacy_tx, GasOracleMiddleware::new( NonceManagerMiddleware::new(SignerMiddleware::new(provider, wallet__), address), gas_oracle, ), - transformer, )), )) }