diff --git a/Cargo.toml b/Cargo.toml index 89627a2..752728a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,4 +1,5 @@ [workspace] +resolver = "2" members = [ "redis-versioned-kv", "evm-exporter", diff --git a/Dockerfile b/Dockerfile index ea474e9..df198f4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ -FROM docker.io/rust:1.70.0-slim AS builder +FROM docker.io/rust:slim-buster AS builder -RUN apt-get update -y && apt-get install -y libssl-dev pkg-config make perl clang +RUN apt-get update -y && apt-get install -y libssl-dev pkg-config make perl clang llvm ENV OPENSSL_LIB_DIR="/usr/lib/x86_64-linux-gnu" ENV OPENSSL_INCLUDE_DIR="/usr/include/openssl" COPY . /enterprise-web3 diff --git a/evm-exporter/Cargo.toml b/evm-exporter/Cargo.toml index 98ed9fe..4975930 100644 --- a/evm-exporter/Cargo.toml +++ b/evm-exporter/Cargo.toml @@ -9,14 +9,13 @@ edition = "2021" primitive-types = "0.11.1" thiserror = "1.0.34" ethereum-types = { version = "0.13.1", default-features = false, features = ["serialize"] } - -redis = { version = "0.21", default-features = false } +sqlx = { version = "0.7", default-features = false, features = ["postgres", "time", "bigdecimal"] } +redis = { version = "0.25", default-features = false } redis-versioned-kv = { path = "../redis-versioned-kv" } hex = "0.4.3" uint = "0.9.3" sha3 = "0.8" libsecp256k1 = { version = "0.5", features = ["static-context", "hmac"] } - ethereum = { version = "0.12.0", default-features = false, features = ["with-serde"] } serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" \ No newline at end of file +serde_json = "1.0" diff --git a/evm-exporter/src/error.rs b/evm-exporter/src/error.rs index c5079db..6a30658 100644 --- a/evm-exporter/src/error.rs +++ b/evm-exporter/src/error.rs @@ -5,6 +5,9 @@ pub enum Error { #[error(transparent)] RedisError(#[from] redis::RedisError), + #[error(transparent)] + PostgresError(#[from] sqlx::Error), + #[error(transparent)] FromHexError(#[from] hex::FromHexError), diff --git a/evm-exporter/src/getter.rs b/evm-exporter/src/getter.rs index 27b96a1..e6ae0be 100644 --- a/evm-exporter/src/getter.rs +++ b/evm-exporter/src/getter.rs @@ -1,21 +1,142 @@ use { - crate::{keys, AccountBasic, Block, Receipt, Result, TransactionStatus}, + crate::{keys, AccountBasic, Block, ConnectionType, Receipt, Result, TransactionStatus}, primitive_types::{H160, H256, U256}, - redis::{Commands, ConnectionLike}, + redis::{Commands, Connection}, redis_versioned_kv::VersionedKVCommand, + sqlx::PgConnection, }; -pub struct Getter<'a, C> { - conn: &'a mut C, +pub trait Getter { + fn new(conn: ConnectionType, something: String) -> Self + where + Self: std::marker::Sized; + fn latest_height(&mut self) -> Result; + fn lowest_height(&mut self) -> Result; + fn get_balance(&mut self, height: u32, address: H160) -> Result; + fn get_nonce(&mut self, height: u32, address: H160) -> Result; + fn get_byte_code(&mut self, height: u32, address: H160) -> Result>; + fn get_account_basic(&mut self, height: u32, address: H160) -> Result; + fn addr_state_exists(&mut self, height: u32, address: H160) -> Result; + fn get_state(&mut self, height: u32, address: H160, index: H256) -> Result; + fn get_block_hash_by_height(&mut self, height: U256) -> Result>; + fn get_height_by_block_hash(&mut self, block_hash: H256) -> Result>; + fn get_block_by_hash(&mut self, block_hash: H256) -> Result>; + fn get_transaction_receipt_by_block_hash( + &mut self, + block_hash: H256, + ) -> Result>>; + fn get_transaction_status_by_block_hash( + &mut self, + block_hash: H256, + ) -> Result>>; + fn get_transaction_index_by_tx_hash(&mut self, tx_hash: H256) -> Result>; + fn get_pending_balance(&mut self, address: H160) -> Result>; + fn get_pending_nonce(&mut self, address: H160) -> Result>; + fn get_pending_byte_code(&mut self, address: H160) -> Result>>; + fn get_pending_state(&mut self, address: H160, index: H256) -> Result>; + fn get_total_issuance(&mut self, height: u32) -> Result; + fn get_allowances(&mut self, height: u32, owner: H160, spender: H160) -> Result; +} + +pub struct PgGetter { + conn: PgConnection, +} + +impl Getter for PgGetter { + fn new(connection: ConnectionType, _something: String) -> Self { + if let ConnectionType::Postgres(conn) = connection { + Self { conn } + } else { + panic!("Invalid connection type for Postgres") + } + } + fn latest_height(&mut self) -> Result { + Ok(0) + } + fn lowest_height(&mut self) -> Result { + Ok(0) + } + fn get_balance(&mut self, height: u32, address: H160) -> Result { + Ok(U256::zero()) + } + fn get_nonce(&mut self, height: u32, address: H160) -> Result { + Ok(U256::zero()) + } + fn get_byte_code(&mut self, height: u32, address: H160) -> Result> { + Ok(vec![0]) + } + fn get_account_basic(&mut self, height: u32, address: H160) -> Result { + Ok(AccountBasic { + balance: self.get_balance(height, address)?, + code: self.get_byte_code(height, address)?, + nonce: self.get_nonce(height, address)?, + }) + } + fn addr_state_exists(&mut self, height: u32, address: H160) -> Result { + Ok(true) + } + fn get_state(&mut self, height: u32, address: H160, index: H256) -> Result { + Ok(H256::zero()) + } + fn get_block_hash_by_height(&mut self, height: U256) -> Result> { + Ok(Some(H256::zero())) + } + fn get_height_by_block_hash(&mut self, block_hash: H256) -> Result> { + Ok(Some(U256::zero())) + } + fn get_block_by_hash(&mut self, block_hash: H256) -> Result> { + Ok(None) + } + fn get_transaction_receipt_by_block_hash( + &mut self, + block_hash: H256, + ) -> Result>> { + Ok(None) + } + fn get_transaction_status_by_block_hash( + &mut self, + block_hash: H256, + ) -> Result>> { + Ok(None) + } + fn get_transaction_index_by_tx_hash(&mut self, tx_hash: H256) -> Result> { + Ok(None) + } + fn get_pending_balance(&mut self, address: H160) -> Result> { + Ok(Some(U256::zero())) + } + fn get_pending_nonce(&mut self, address: H160) -> Result> { + Ok(Some(U256::zero())) + } + fn get_pending_byte_code(&mut self, address: H160) -> Result>> { + Ok(None) + } + fn get_pending_state(&mut self, address: H160, index: H256) -> Result> { + Ok(Some(H256::zero())) + } + fn get_total_issuance(&mut self, height: u32) -> Result { + Ok(U256::zero()) + } + fn get_allowances(&mut self, height: u32, owner: H160, spender: H160) -> Result { + Ok(U256::zero()) + } +} + +pub struct RedisGetter { + conn: Connection, pub prefix: String, } -impl<'a, C: ConnectionLike> Getter<'a, C> { - pub fn new(conn: &'a mut C, prefix: String) -> Self { - Self { conn, prefix } +impl Getter for RedisGetter { + fn new(connection: ConnectionType, prefix: String) -> Self { + if let ConnectionType::Redis(conn) = connection { + Self { conn, prefix } + } else { + panic!("Invalid connection type for Redis") + } } - pub fn latest_height(&mut self) -> Result { + fn latest_height(&mut self) -> Result { let height_key = keys::latest_height_key(&self.prefix); let height: Option = self.conn.get(height_key)?; match height { @@ -23,7 +144,7 @@ impl<'a, C: ConnectionLike> Getter<'a, C> { _ => Ok(0), } } - pub fn lowest_height(&mut self) -> Result { + fn lowest_height(&mut self) -> Result { let height_key = keys::lowest_height_key(&self.prefix); let height: Option = self.conn.get(height_key)?; match height { @@ -31,7 +152,7 @@ impl<'a, C: ConnectionLike> Getter<'a, C> { _ => Ok(0), } } - pub fn get_balance(&mut self, height: u32, address: H160) -> Result { + fn get_balance(&mut self, height: u32, address: H160) -> Result { let balance_key = keys::balance_key(&self.prefix, address); let balance: Option = self.conn.vkv_get(balance_key, height)?; let balance = if let Some(s) = balance { @@ -42,7 +163,7 @@ impl<'a, C: ConnectionLike> Getter<'a, C> { Ok(balance) } - pub fn get_nonce(&mut self, height: u32, address: H160) -> Result { + fn get_nonce(&mut self, height: u32, address: H160) -> Result { let nonce_key = keys::nonce_key(&self.prefix, address); let nonce: Option = self.conn.vkv_get(nonce_key, height)?; let nonce = if let Some(s) = nonce { @@ -53,7 +174,7 @@ impl<'a, C: ConnectionLike> Getter<'a, C> { Ok(nonce) } - pub fn get_byte_code(&mut self, height: u32, address: H160) -> Result> { + fn get_byte_code(&mut self, height: u32, address: H160) -> Result> { let code_key = keys::code_key(&self.prefix, address); let code: Option = self.conn.vkv_get(code_key, height)?; let code = if let Some(s) = code { @@ -64,7 +185,7 @@ impl<'a, C: ConnectionLike> Getter<'a, C> { Ok(code) } - pub fn get_account_basic(&mut self, height: u32, address: H160) -> Result { + fn get_account_basic(&mut self, height: u32, address: H160) -> Result { Ok(AccountBasic { balance: self.get_balance(height, address)?, code: self.get_byte_code(height, address)?, @@ -72,13 +193,13 @@ impl<'a, C: ConnectionLike> Getter<'a, C> { }) } - pub fn addr_state_exists(&mut self, height: u32, address: H160) -> Result { + fn addr_state_exists(&mut self, height: u32, address: H160) -> Result { let state_addr_key = keys::state_addr_key(&self.prefix, address); let value: Option = self.conn.vkv_get(state_addr_key, height)?; Ok(value.is_some()) } - pub fn get_state(&mut self, height: u32, address: H160, index: H256) -> Result { + fn get_state(&mut self, height: u32, address: H160, index: H256) -> Result { let state_key = keys::state_key(&self.prefix, address, index); let value: Option = self.conn.vkv_get(state_key, height)?; let val = if let Some(s) = value { @@ -89,7 +210,7 @@ impl<'a, C: ConnectionLike> Getter<'a, C> { Ok(val) } - pub fn get_block_hash_by_height(&mut self, height: U256) -> Result> { + fn get_block_hash_by_height(&mut self, height: U256) -> Result> { let block_hash_key = keys::block_hash_key(&self.prefix, height); let value: Option = self.conn.get::<&str, Option>(&block_hash_key)?; if let Some(hash) = value { @@ -99,7 +220,7 @@ impl<'a, C: ConnectionLike> Getter<'a, C> { } } - pub fn get_height_by_block_hash(&mut self, block_hash: H256) -> Result> { + fn get_height_by_block_hash(&mut self, block_hash: H256) -> Result> { let block_height_key = keys::block_height_key(&self.prefix, block_hash); let value: Option = self.conn.get::<&str, Option>(&block_height_key)?; if let Some(hash) = value { @@ -109,7 +230,7 @@ impl<'a, C: ConnectionLike> Getter<'a, C> { } } - pub fn get_block_by_hash(&mut self, block_hash: H256) -> Result> { + fn get_block_by_hash(&mut self, block_hash: H256) -> Result> { let block_key = keys::block_key(&self.prefix, block_hash); let value: Option = self.conn.get::<&str, Option>(&block_key)?; if let Some(block) = value { @@ -119,7 +240,7 @@ impl<'a, C: ConnectionLike> Getter<'a, C> { } } - pub fn get_transaction_receipt_by_block_hash( + fn get_transaction_receipt_by_block_hash( &mut self, block_hash: H256, ) -> Result>> { @@ -132,7 +253,7 @@ impl<'a, C: ConnectionLike> Getter<'a, C> { } } - pub fn get_transaction_status_by_block_hash( + fn get_transaction_status_by_block_hash( &mut self, block_hash: H256, ) -> Result>> { @@ -146,10 +267,7 @@ impl<'a, C: ConnectionLike> Getter<'a, C> { } } - pub fn get_transaction_index_by_tx_hash( - &mut self, - tx_hash: H256, - ) -> Result> { + fn get_transaction_index_by_tx_hash(&mut self, tx_hash: H256) -> Result> { let transaction_index_key = keys::transaction_index_key(&self.prefix, tx_hash); let value: Option = self.conn.get(transaction_index_key)?; @@ -160,7 +278,7 @@ impl<'a, C: ConnectionLike> Getter<'a, C> { } } - pub fn get_pending_balance(&mut self, address: H160) -> Result> { + fn get_pending_balance(&mut self, address: H160) -> Result> { let balance_key = keys::pending_balance_key(&self.prefix, address); let balance: Option = self.conn.get(balance_key)?; let balance = if let Some(s) = balance { @@ -171,7 +289,7 @@ impl<'a, C: ConnectionLike> Getter<'a, C> { Ok(balance) } - pub fn get_pending_nonce(&mut self, address: H160) -> Result> { + fn get_pending_nonce(&mut self, address: H160) -> Result> { let nonce_key = keys::pending_nonce_key(&self.prefix, address); let nonce: Option = self.conn.get(nonce_key)?; let nonce = if let Some(s) = nonce { @@ -182,7 +300,7 @@ impl<'a, C: ConnectionLike> Getter<'a, C> { Ok(nonce) } - pub fn get_pending_byte_code(&mut self, address: H160) -> Result>> { + fn get_pending_byte_code(&mut self, address: H160) -> Result>> { let code_key = keys::pending_code_key(&self.prefix, address); let code: Option = self.conn.get(code_key)?; let code = if let Some(s) = code { @@ -193,7 +311,7 @@ impl<'a, C: ConnectionLike> Getter<'a, C> { Ok(code) } - pub fn get_pending_state(&mut self, address: H160, index: H256) -> Result> { + fn get_pending_state(&mut self, address: H160, index: H256) -> Result> { let state_key = keys::pending_state_key(&self.prefix, address, index); let value: Option = self.conn.get(state_key)?; let val = if let Some(s) = value { @@ -204,7 +322,7 @@ impl<'a, C: ConnectionLike> Getter<'a, C> { Ok(val) } - pub fn get_total_issuance(&mut self, height: u32) -> Result { + fn get_total_issuance(&mut self, height: u32) -> Result { let key = keys::total_issuance_key(&self.prefix); let value: Option = self.conn.vkv_get(key, height)?; let val = if let Some(s) = value { @@ -215,7 +333,7 @@ impl<'a, C: ConnectionLike> Getter<'a, C> { Ok(val) } - pub fn get_allowances(&mut self, height: u32, owner: H160, spender: H160) -> Result { + fn get_allowances(&mut self, height: u32, owner: H160, spender: H160) -> Result { let key = keys::allowances_key(&self.prefix, owner, spender); let value: Option = self.conn.vkv_get(key, height)?; let val = if let Some(s) = value { diff --git a/evm-exporter/src/lib.rs b/evm-exporter/src/lib.rs index b9c649d..0d33b38 100644 --- a/evm-exporter/src/lib.rs +++ b/evm-exporter/src/lib.rs @@ -14,3 +14,8 @@ pub use setter::*; mod utils; pub use utils::*; + +pub enum ConnectionType { + Redis(redis::Connection), + Postgres(sqlx::PgConnection), +} diff --git a/evm-exporter/src/setter.rs b/evm-exporter/src/setter.rs index 949252d..645e578 100644 --- a/evm-exporter/src/setter.rs +++ b/evm-exporter/src/setter.rs @@ -4,41 +4,172 @@ use { keys, types::{Block, TransactionStatus}, utils::recover_signer, - Receipt, + ConnectionType, Receipt, }, ethereum::LegacyTransaction, primitive_types::{H160, H256, U256}, - redis::{Commands, ConnectionLike}, + redis::{Commands, Connection}, redis_versioned_kv::VersionedKVCommand, + sqlx::PgConnection, }; -pub struct Setter<'a, C> { - conn: &'a mut C, +pub trait Setter { + fn new(conn: ConnectionType, something: String) -> Self + where + Self: std::marker::Sized; + fn clear(&mut self) -> Result<()>; + fn set_height(&mut self, height: u32) -> Result<()>; + fn set_lowest_height(&mut self, height: u32) -> Result<()>; + fn set_balance(&mut self, height: u32, address: H160, balance: U256) -> Result<()>; + fn remove_balance(&mut self, height: u32, address: H160) -> Result<()>; + fn set_nonce(&mut self, height: u32, address: H160, nonce: U256) -> Result<()>; + fn remove_nonce(&mut self, height: u32, address: H160) -> Result<()>; + fn set_byte_code(&mut self, height: u32, address: H160, code: Vec) -> Result<()>; + fn remove_byte_code(&mut self, height: u32, address: H160) -> Result<()>; + fn set_state(&mut self, height: u32, address: H160, index: H256, value: H256) -> Result<()>; + fn remove_state(&mut self, height: u32, address: H160, index: H256) -> Result<()>; + fn set_block_info( + &mut self, + block: Block, + receipts: Vec, + statuses: Vec, + ) -> Result<()>; + fn remove_block_info(&mut self, height: U256) -> Result<()>; + fn set_pending_tx(&mut self, transaction: LegacyTransaction) -> Result<()>; + fn set_pending_code(&mut self, address: H160, code: Vec) -> Result<()>; + fn set_pending_state(&mut self, address: H160, index: H256, value: H256) -> Result<()>; + fn remove_pending_tx(&mut self, transaction: LegacyTransaction) -> Result<()>; + fn remove_pending_code(&mut self, address: H160) -> Result<()>; + fn remove_pending_state(&mut self, address: H160, index: H256) -> Result<()>; + fn set_total_issuance(&mut self, height: u32, value: U256) -> Result<()>; + fn set_allowances( + &mut self, + height: u32, + owner: H160, + spender: H160, + value: U256, + ) -> Result<()>; +} + +pub struct PgSetter { + conn: PgConnection, +} + +impl Setter for PgSetter { + fn new(connection: ConnectionType, _something: String) -> Self { + if let ConnectionType::Postgres(conn) = connection { + Self { conn } + } else { + panic!("Invalid connection type for Postgres") + } + } + fn clear(&mut self) -> Result<()> { + Ok(()) + } + fn set_height(&mut self, height: u32) -> Result<()> { + Ok(()) + } + fn set_lowest_height(&mut self, height: u32) -> Result<()> { + Ok(()) + } + fn set_balance(&mut self, height: u32, address: H160, balance: U256) -> Result<()> { + Ok(()) + } + fn remove_balance(&mut self, height: u32, address: H160) -> Result<()> { + Ok(()) + } + fn set_nonce(&mut self, height: u32, address: H160, nonce: U256) -> Result<()> { + Ok(()) + } + fn remove_nonce(&mut self, height: u32, address: H160) -> Result<()> { + Ok(()) + } + fn set_byte_code(&mut self, height: u32, address: H160, code: Vec) -> Result<()> { + Ok(()) + } + fn remove_byte_code(&mut self, height: u32, address: H160) -> Result<()> { + Ok(()) + } + fn set_state(&mut self, height: u32, address: H160, index: H256, value: H256) -> Result<()> { + Ok(()) + } + fn remove_state(&mut self, height: u32, address: H160, index: H256) -> Result<()> { + Ok(()) + } + fn set_block_info( + &mut self, + block: Block, + receipts: Vec, + statuses: Vec, + ) -> Result<()> { + Ok(()) + } + fn remove_block_info(&mut self, height: U256) -> Result<()> { + Ok(()) + } + fn set_pending_tx(&mut self, transaction: LegacyTransaction) -> Result<()> { + Ok(()) + } + fn set_pending_code(&mut self, address: H160, code: Vec) -> Result<()> { + Ok(()) + } + fn set_pending_state(&mut self, address: H160, index: H256, value: H256) -> Result<()> { + Ok(()) + } + fn remove_pending_tx(&mut self, transaction: LegacyTransaction) -> Result<()> { + Ok(()) + } + fn remove_pending_code(&mut self, address: H160) -> Result<()> { + Ok(()) + } + fn remove_pending_state(&mut self, address: H160, index: H256) -> Result<()> { + Ok(()) + } + fn set_total_issuance(&mut self, height: u32, value: U256) -> Result<()> { + Ok(()) + } + fn set_allowances( + &mut self, + height: u32, + owner: H160, + spender: H160, + value: U256, + ) -> Result<()> { + Ok(()) + } +} + +pub struct RedisSetter { + conn: Connection, pub prefix: String, } -impl<'a, C: ConnectionLike> Setter<'a, C> { - pub fn new(conn: &'a mut C, prefix: String) -> Self { - Self { conn, prefix } +impl Setter for RedisSetter { + fn new(connection: ConnectionType, prefix: String) -> Self { + if let ConnectionType::Redis(conn) = connection { + Self { conn, prefix } + } else { + panic!("Invalid connection type for Redis") + } } - pub fn clear(&mut self) -> Result<()> { - redis::cmd("FLUSHDB").arg("SYNC").query(self.conn)?; + fn clear(&mut self) -> Result<()> { + redis::cmd("FLUSHDB").arg("SYNC").query(&mut self.conn)?; Ok(()) } - pub fn set_height(&mut self, height: u32) -> Result<()> { + fn set_height(&mut self, height: u32) -> Result<()> { let height_key = keys::latest_height_key(&self.prefix); self.conn.set(height_key, format!("{}", height))?; Ok(()) } - pub fn set_lowest_height(&mut self, height: u32) -> Result<()> { + fn set_lowest_height(&mut self, height: u32) -> Result<()> { let height_key = keys::lowest_height_key(&self.prefix); self.conn.set(height_key, format!("{}", height))?; Ok(()) } - pub fn set_balance(&mut self, height: u32, address: H160, balance: U256) -> Result<()> { + fn set_balance(&mut self, height: u32, address: H160, balance: U256) -> Result<()> { let balance_key = keys::balance_key(&self.prefix, address); self.conn .vkv_set(balance_key, height, serde_json::to_string(&balance)?)?; @@ -46,43 +177,37 @@ impl<'a, C: ConnectionLike> Setter<'a, C> { Ok(()) } - pub fn remove_balance(&mut self, height: u32, address: H160) -> Result<()> { + fn remove_balance(&mut self, height: u32, address: H160) -> Result<()> { let balance_key = keys::balance_key(&self.prefix, address); self.conn.vkv_del(balance_key, height)?; Ok(()) } - pub fn set_nonce(&mut self, height: u32, address: H160, nonce: U256) -> Result<()> { + fn set_nonce(&mut self, height: u32, address: H160, nonce: U256) -> Result<()> { let nonce_key = keys::nonce_key(&self.prefix, address); self.conn .vkv_set(nonce_key, height, serde_json::to_string(&nonce)?)?; Ok(()) } - pub fn remove_nonce(&mut self, height: u32, address: H160) -> Result<()> { + fn remove_nonce(&mut self, height: u32, address: H160) -> Result<()> { let nonce_key = keys::nonce_key(&self.prefix, address); self.conn.vkv_del(nonce_key, height)?; Ok(()) } - pub fn set_byte_code(&mut self, height: u32, address: H160, code: Vec) -> Result<()> { + fn set_byte_code(&mut self, height: u32, address: H160, code: Vec) -> Result<()> { let code_key = keys::code_key(&self.prefix, address); self.conn.vkv_set(code_key, height, hex::encode(code))?; Ok(()) } - pub fn remove_byte_code(&mut self, height: u32, address: H160) -> Result<()> { + fn remove_byte_code(&mut self, height: u32, address: H160) -> Result<()> { let code_key = keys::code_key(&self.prefix, address); self.conn.vkv_del(code_key, height)?; Ok(()) } - pub fn set_state( - &mut self, - height: u32, - address: H160, - index: H256, - value: H256, - ) -> Result<()> { + fn set_state(&mut self, height: u32, address: H160, index: H256, value: H256) -> Result<()> { let key = keys::state_key(&self.prefix, address, index); self.conn .vkv_set(key, height, serde_json::to_string(&value)?)?; @@ -92,7 +217,7 @@ impl<'a, C: ConnectionLike> Setter<'a, C> { Ok(()) } - pub fn remove_state(&mut self, height: u32, address: H160, index: H256) -> Result<()> { + fn remove_state(&mut self, height: u32, address: H160, index: H256) -> Result<()> { let key = keys::state_key(&self.prefix, address, index); self.conn.vkv_del(key, height)?; let state_addr_key = keys::state_addr_key(&self.prefix, address); @@ -100,7 +225,7 @@ impl<'a, C: ConnectionLike> Setter<'a, C> { Ok(()) } - pub fn set_block_info( + fn set_block_info( &mut self, block: Block, receipts: Vec, @@ -139,7 +264,7 @@ impl<'a, C: ConnectionLike> Setter<'a, C> { Ok(()) } - pub fn remove_block_info(&mut self, height: U256) -> Result<()> { + fn remove_block_info(&mut self, height: U256) -> Result<()> { let block_hash_key = keys::block_hash_key(&self.prefix, height); let block_hash: H256 = match self .conn @@ -176,7 +301,7 @@ impl<'a, C: ConnectionLike> Setter<'a, C> { Ok(()) } - pub fn set_pending_tx(&mut self, transaction: LegacyTransaction) -> Result<()> { + fn set_pending_tx(&mut self, transaction: LegacyTransaction) -> Result<()> { let sign_address = recover_signer(&transaction)?; let height_key = keys::latest_height_key(&self.prefix); @@ -211,14 +336,14 @@ impl<'a, C: ConnectionLike> Setter<'a, C> { Ok(()) } - pub fn set_pending_code(&mut self, address: H160, code: Vec) -> Result<()> { + fn set_pending_code(&mut self, address: H160, code: Vec) -> Result<()> { let pending_code_key = keys::pending_code_key(&self.prefix, address); self.conn .set(pending_code_key, serde_json::to_string(&code)?)?; Ok(()) } - pub fn set_pending_state(&mut self, address: H160, index: H256, value: H256) -> Result<()> { + fn set_pending_state(&mut self, address: H160, index: H256, value: H256) -> Result<()> { let pending_state_key = keys::pending_state_key(&self.prefix, address, index); self.conn .set(pending_state_key, serde_json::to_string(&value)?)?; @@ -226,7 +351,7 @@ impl<'a, C: ConnectionLike> Setter<'a, C> { Ok(()) } - pub fn remove_pending_tx(&mut self, transaction: LegacyTransaction) -> Result<()> { + fn remove_pending_tx(&mut self, transaction: LegacyTransaction) -> Result<()> { let sign_address = recover_signer(&transaction)?; let pending_balance_key = keys::pending_balance_key(&self.prefix, sign_address); @@ -238,27 +363,27 @@ impl<'a, C: ConnectionLike> Setter<'a, C> { Ok(()) } - pub fn remove_pending_code(&mut self, address: H160) -> Result<()> { + fn remove_pending_code(&mut self, address: H160) -> Result<()> { let pending_code_key = keys::pending_code_key(&self.prefix, address); self.conn.del(pending_code_key)?; Ok(()) } - pub fn remove_pending_state(&mut self, address: H160, index: H256) -> Result<()> { + fn remove_pending_state(&mut self, address: H160, index: H256) -> Result<()> { let pending_state_key = keys::pending_state_key(&self.prefix, address, index); self.conn.del(pending_state_key)?; Ok(()) } - pub fn set_total_issuance(&mut self, height: u32, value: U256) -> Result<()> { + fn set_total_issuance(&mut self, height: u32, value: U256) -> Result<()> { let key = keys::total_issuance_key(&self.prefix); self.conn .vkv_set(key, height, serde_json::to_string(&value)?)?; Ok(()) } - pub fn set_allowances( + fn set_allowances( &mut self, height: u32, owner: H160, diff --git a/redis-versioned-kv/Cargo.toml b/redis-versioned-kv/Cargo.toml index 88e2583..1e91bcf 100644 --- a/redis-versioned-kv/Cargo.toml +++ b/redis-versioned-kv/Cargo.toml @@ -6,5 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -redis = { version = "0.21", default-features = false } +redis = { version = "0.25", default-features = false } diff --git a/rocksdb-exporter/Cargo.toml b/rocksdb-exporter/Cargo.toml index 8b5558a..28470c4 100644 --- a/rocksdb-exporter/Cargo.toml +++ b/rocksdb-exporter/Cargo.toml @@ -12,8 +12,7 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" rocksdb = { version = "0.17.0", default-features = false } -redis = { version = "0.21", default-features = false, features = [ "tls", "r2d2" ] } -r2d2 = { version = "0.8.8"} +redis = { version = "0.25", default-features = false, features = [ "tls-native-tls" ] } evm-exporter = { path = "../evm-exporter" } hex = "0.4.3" bech32 = "0.7.2" @@ -25,4 +24,4 @@ toml = "0.5.8" [features] default = [] -cluster_redis = ["redis/cluster"] \ No newline at end of file +cluster_redis = ["redis/cluster"] diff --git a/rocksdb-exporter/src/main.rs b/rocksdb-exporter/src/main.rs index ac9805a..6519e56 100644 --- a/rocksdb-exporter/src/main.rs +++ b/rocksdb-exporter/src/main.rs @@ -3,7 +3,7 @@ mod evm_rocksdb_storage; use { config::Config, - evm_exporter::{Getter, Setter, PREFIX}, + evm_exporter::{ConnectionType, Getter, RedisGetter, RedisSetter, Setter, PREFIX}, evm_rocksdb_storage::{ evm_rocksdb::RocksDB, get_account_info, get_block_info, get_current_height, }, @@ -26,17 +26,16 @@ fn main() { #[cfg(not(feature = "cluster_redis"))] let client = pnk!(redis::Client::open(config.redis_url[0].as_ref())); - let pool = Arc::new(pnk!(r2d2::Pool::builder().max_size(50).build(client))); - let mut conn = pnk!(pool.get()); - let mut setter = Setter::new(&mut *conn, PREFIX.to_string()); + let conn = pnk!(client.get_connection()); + let mut setter: RedisSetter = Setter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let current_height = pnk!(get_current_height(&hisdb)); let mut height = if config.clear { pnk!(setter.clear()); U256::zero() } else { - let mut conn = pnk!(pool.get()); - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let conn = pnk!(client.get_connection()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); U256::from(pnk!(getter.latest_height())) }; diff --git a/web3-service/Cargo.toml b/web3-service/Cargo.toml index 74337a5..a6dce99 100644 --- a/web3-service/Cargo.toml +++ b/web3-service/Cargo.toml @@ -24,8 +24,7 @@ jsonrpc-core = "18.0" jsonrpc-pubsub = "18.0" jsonrpc-derive = "18.0" -redis = { version = "0.21", default-features = false, features = [ "tls", "r2d2" ] } -r2d2 = { version = "0.8.8"} +redis = { version = "0.25", default-features = false, features = [ "tls-native-tls" ] } attohttpc = { version = "0.18", default-features = false, features = ["compress", "json", "tls-rustls"] } tokio = { version = "1.21.0", features = ["full"] } diff --git a/web3-service/src/main.rs b/web3-service/src/main.rs index 8d1cc05..7f63cf4 100644 --- a/web3-service/src/main.rs +++ b/web3-service/src/main.rs @@ -44,16 +44,17 @@ fn main() { config.redis_url.clone() )); #[cfg(not(feature = "cluster_redis"))] - let client = pnk!(redis::Client::open(config.redis_url[0].as_ref())); - let pool = Arc::new(pnk!(r2d2::Pool::builder().max_size(50).build(client))); - REDIS_POOL.set(pool.clone()).expect("REDIS_POOL set error"); + let client = Arc::new(pnk!(redis::Client::open(config.redis_url[0].as_ref()))); + REDIS_POOL + .set(client.clone()) + .expect("REDIS_POOL set error"); - pnk!(init_upstream(pool.clone())); + pnk!(init_upstream(client.clone())); let tm_client = Arc::new(pnk!(HttpClient::new(config.tendermint_url.as_str()))); let eth = EthService::new( config.chain_id, config.gas_price, - pool.clone(), + client.clone(), tm_client, config.tendermint_url.as_str(), ); @@ -62,14 +63,17 @@ fn main() { let debug = DebugApiImpl::new( config.chain_id, config.gas_price, - pool.clone(), + client.clone(), config.tendermint_url.as_str(), ); let health = HealthApiImpl::new(); - let filter = EthFilterApiImpl::new(pool.clone()); - let subscriber_notify = Arc::new(SubscriberNotify::new(pool.clone(), &config.tendermint_url)); + let filter = EthFilterApiImpl::new(client.clone()); + let subscriber_notify = Arc::new(SubscriberNotify::new( + client.clone(), + &config.tendermint_url, + )); pnk!(subscriber_notify.start()); - let pub_sub = EthPubSubApiImpl::new(pool, subscriber_notify); + let pub_sub = EthPubSubApiImpl::new(client, subscriber_notify); let mut io = MetaIoHandler::default(); io.extend_with(eth.to_delegate()); diff --git a/web3-service/src/notify/subscriber_notify.rs b/web3-service/src/notify/subscriber_notify.rs index bde9d9a..0100b5e 100644 --- a/web3-service/src/notify/subscriber_notify.rs +++ b/web3-service/src/notify/subscriber_notify.rs @@ -1,7 +1,7 @@ use { crate::notify::notifications::Notifications, ethereum_types::{H256, U256}, - evm_exporter::{Getter, PREFIX}, + evm_exporter::{ConnectionType, Getter, RedisGetter, PREFIX}, ruc::*, serde_json::Value, sha2::{Digest, Sha256}, @@ -21,7 +21,7 @@ pub struct SubscriberNotify { pub struct SubscriberNotify { tm_url: String, millis: u64, - redis_pool: Arc>, + redis_pool: Arc, pub logs_event_notify: Arc>, pub new_heads_event_notify: Arc>, pub new_pending_tx_hash_event_notify: Arc>, @@ -44,7 +44,7 @@ impl SubscriberNotify { } #[cfg(not(feature = "cluster_redis"))] - pub fn new(redis_pool: Arc>, tm_url: &str) -> Self { + pub fn new(redis_pool: Arc, tm_url: &str) -> Self { Self { tm_url: String::from(tm_url), millis: 2000, @@ -64,54 +64,52 @@ impl SubscriberNotify { let syncing_event_notify = self.syncing_event_notify.clone(); let redis_pool = self.redis_pool.clone(); - let mut conn = self.redis_pool.get().c(d!())?; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let conn = self.redis_pool.get_connection().c(d!())?; + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let mut last_height = getter.latest_height().c(d!())?; let mut last_txhash = vec![]; let mut last_status = Option::None; let tm_url = self.tm_url.clone(); - thread::spawn(move || { - let tm_url = tm_url; - loop { - thread::sleep(ten_millis); + thread::spawn(move || loop { + thread::sleep(ten_millis); - if let Ok(mut conn) = redis_pool.get() { - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); - if let Ok(height) = getter.latest_height() { - if last_height != height { - for h in (last_height + 1)..=height { - logs_event_notify.notify(U256::from(h)).unwrap_or_default(); - new_heads_event_notify - .notify(U256::from(h)) - .unwrap_or_default(); - } - last_height = height; - } - }; - } - - if let Ok(hashs) = get_pending_hash(&tm_url) { - for hash in &hashs { - if !last_txhash.contains(hash) { - new_pending_tx_hash_event_notify - .notify(*hash) + if let Ok(conn) = redis_pool.get_connection() { + let mut getter: RedisGetter = + Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); + if let Ok(height) = getter.latest_height() { + if last_height != height { + for h in (last_height + 1)..=height { + logs_event_notify.notify(U256::from(h)).unwrap_or_default(); + new_heads_event_notify + .notify(U256::from(h)) .unwrap_or_default(); } + last_height = height; } - last_txhash = hashs; - } - if let Ok(status) = get_sync_status(&tm_url) { - let is_send = match last_status { - Some(last) => last != status, - None => true, - }; - if is_send { - syncing_event_notify.notify(status).unwrap_or_default(); - last_status = Some(status); + }; + } + + if let Ok(hashs) = get_pending_hash(&tm_url) { + for hash in &hashs { + if !last_txhash.contains(hash) { + new_pending_tx_hash_event_notify + .notify(*hash) + .unwrap_or_default(); } } + last_txhash = hashs; + } + if let Ok(status) = get_sync_status(&tm_url) { + let is_send = match last_status { + Some(last) => last != status, + None => true, + }; + if is_send { + syncing_event_notify.notify(status).unwrap_or_default(); + last_status = Some(status); + } } }); Ok(()) diff --git a/web3-service/src/rpc/debug.rs b/web3-service/src/rpc/debug.rs index 77cf11d..25f5c15 100644 --- a/web3-service/src/rpc/debug.rs +++ b/web3-service/src/rpc/debug.rs @@ -1,4 +1,5 @@ use boa_engine::Context; +use evm_exporter::{ConnectionType, RedisGetter}; use { super::{ @@ -32,7 +33,7 @@ use { pub struct DebugApiImpl { chain_id: u32, gas_price: u64, - pool: Arc>, + pool: Arc, mutex: Mutex, tendermint_url: String, } @@ -40,7 +41,7 @@ pub struct DebugApiImpl { pub struct DebugApiImpl { chain_id: u32, gas_price: u64, - pool: Arc>, + pool: Arc, mutex: Mutex, tendermint_url: String, } @@ -49,7 +50,7 @@ impl DebugApiImpl { pub fn new( chain_id: u32, gas_price: u64, - pool: Arc>, + pool: Arc, tendermint_url: &str, ) -> Self { Self { @@ -63,7 +64,7 @@ impl DebugApiImpl { pub fn new( chain_id: u32, gas_price: u64, - pool: Arc>, + pool: Arc, tendermint_url: &str, ) -> Self { Self { @@ -224,12 +225,12 @@ impl DebugApi for DebugApiImpl { number: BlockNumber, params: Option, ) -> Result> { - let mut conn = self.pool.get().map_err(|e| { + let conn = self.pool.get_connection().map_err(|e| { let mut err = Error::internal_error(); err.message = format!("{:?}", e); err })?; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let height = match block_number_to_height(Some(number), &mut getter) { Ok(h) => h, Err(e) => { @@ -263,12 +264,12 @@ impl DebugApi for DebugApiImpl { params: Option, ) -> Result> { log::info!(target: "debug api", "trace_transaction block_hash:{:?} ", block_hash); - let mut conn = self.pool.get().map_err(|e| { + let conn = self.pool.get_connection().map_err(|e| { let mut err = Error::internal_error(); err.message = format!("{:?}", e); err })?; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let block = getter .get_block_by_hash(block_hash) .map_err(|e| { @@ -369,12 +370,12 @@ impl DebugApi for DebugApiImpl { params: Option, ) -> Result { log::info!(target: "debug api", "trace_transaction number:{:?} request:{:?} ", number,request); - let mut conn = self.pool.get().map_err(|e| { + let conn = self.pool.get_connection().map_err(|e| { let mut err = Error::internal_error(); err.message = format!("{:?}", e); err })?; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let height = block_number_to_height(Some(number), &mut getter).map_err(|e| { let mut err = Error::internal_error(); err.message = format!("{:?}", e); @@ -410,12 +411,12 @@ impl DebugApi for DebugApiImpl { fn trace_transaction(&self, tx_hash: H256, params: Option) -> Result { log::info!(target: "debug api", "trace_transaction tx_hash:{:?}", tx_hash); - let mut conn = self.pool.get().map_err(|e| { + let conn = self.pool.get_connection().map_err(|e| { let mut err = Error::internal_error(); err.message = format!("{:?}", e); err })?; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let (block_hash, index) = getter .get_transaction_index_by_tx_hash(tx_hash) .map_err(|e| { diff --git a/web3-service/src/rpc/debugapi/jsvm/params.rs b/web3-service/src/rpc/debugapi/jsvm/params.rs index eed5b53..a024061 100644 --- a/web3-service/src/rpc/debugapi/jsvm/params.rs +++ b/web3-service/src/rpc/debugapi/jsvm/params.rs @@ -7,15 +7,15 @@ use { chrono::{DateTime, UTC}, ethereum_types::{H160, H256, U256}, evm::Opcode, - evm_exporter::{Getter, PREFIX}, + evm_exporter::{ConnectionType, Getter, RedisGetter, PREFIX}, once_cell::sync::OnceCell, ruc::{eg, Result as RucResult}, std::{str::FromStr, sync::Arc}, }; -static REDIS_POOL: OnceCell>> = OnceCell::new(); +static REDIS_POOL: OnceCell> = OnceCell::new(); #[inline(always)] -pub fn init_upstream(redis_pool: Arc>) -> RucResult<()> { +pub fn init_upstream(redis_pool: Arc) -> RucResult<()> { REDIS_POOL.set(redis_pool).map_err(|_| eg!()) } @@ -909,9 +909,10 @@ impl DB { })?; if let Some(pool) = REDIS_POOL.get().as_ref() { let info = pool - .get() - .map(|mut conn| { - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + .get_connection() + .map(|conn| { + let mut getter: RedisGetter = + Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); match getter.get_balance(height, address) { Ok(b) => b, _ => U256::zero(), @@ -953,9 +954,10 @@ impl DB { })?; if let Some(pool) = REDIS_POOL.get().as_ref() { let info = pool - .get() - .map(|mut conn| { - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + .get_connection() + .map(|conn| { + let mut getter: RedisGetter = + Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); match getter.get_nonce(height, address) { Ok(b) => b, _ => U256::zero(), @@ -997,9 +999,10 @@ impl DB { })?; if let Some(pool) = REDIS_POOL.get().as_ref() { let info = pool - .get() - .map(|mut conn| { - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + .get_connection() + .map(|conn| { + let mut getter: RedisGetter = + Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); match getter.get_byte_code(height, address) { Ok(b) => b, _ => vec![], @@ -1068,9 +1071,10 @@ impl DB { })?; if let Some(pool) = REDIS_POOL.get().as_ref() { let info = pool - .get() - .map(|mut conn| { - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + .get_connection() + .map(|conn| { + let mut getter: RedisGetter = + Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); match getter.get_state(height, address, index) { Ok(b) => b, _ => H256::zero(), @@ -1114,9 +1118,10 @@ impl DB { })?; if let Some(pool) = REDIS_POOL.get().as_ref() { let info = pool - .get() - .map(|mut conn| { - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + .get_connection() + .map(|conn| { + let mut getter: RedisGetter = + Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); match getter.addr_state_exists(height, address) { Ok(b) => b, _ => false, diff --git a/web3-service/src/rpc/eth.rs b/web3-service/src/rpc/eth.rs index 64088a9..93c89d9 100644 --- a/web3-service/src/rpc/eth.rs +++ b/web3-service/src/rpc/eth.rs @@ -14,7 +14,7 @@ use { executor::stack::{StackExecutor, StackSubstateMetadata}, {ExitError, ExitReason}, }, - evm_exporter::{public_key, Getter, TransactionStatus, PREFIX}, + evm_exporter::{public_key, ConnectionType, Getter, RedisGetter, TransactionStatus, PREFIX}, jsonrpc_core::{futures::future, BoxFuture, Error, ErrorCode, Result, Value}, lazy_static::lazy_static, sha3::{Digest, Keccak256}, @@ -42,7 +42,7 @@ lazy_static! { pub struct EthService { chain_id: u32, gas_price: u64, - pool: Arc>, + pool: Arc, tm_client: Arc, tendermint_url: String, } @@ -50,7 +50,7 @@ pub struct EthService { pub struct EthService { chain_id: u32, gas_price: u64, - pool: Arc>, + pool: Arc, tm_client: Arc, tendermint_url: String, } @@ -60,7 +60,7 @@ impl EthService { pub fn new( chain_id: u32, gas_price: u64, - pool: Arc>, + pool: Arc, tm_client: Arc, tendermint_url: &str, ) -> Self { @@ -76,7 +76,7 @@ impl EthService { pub fn new( chain_id: u32, gas_price: u64, - pool: Arc>, + pool: Arc, tm_client: Arc, tendermint_url: &str, ) -> Self { @@ -254,7 +254,7 @@ impl EthApi for EthService { fn balance(&self, address: H160, number: Option) -> BoxFuture> { log::info!(target: "eth api", "balance address:{:?} number:{:?}", &address, &number); - let mut conn = match self.pool.get() { + let conn = match self.pool.get_connection() { Ok(conn) => conn, Err(e) => { return Box::pin(future::err(internal_err(format!( @@ -264,7 +264,7 @@ impl EthApi for EthService { } }; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); if let Some(BlockNumber::Pending) = number { match getter.get_pending_balance(address) { @@ -310,7 +310,7 @@ impl EthApi for EthService { fn call(&self, request: CallRequest, number: Option) -> BoxFuture> { log::info!(target: "eth api", "call request:{:?} number:{:?}", &request, &number); - let mut conn = match self.pool.get() { + let conn = match self.pool.get_connection() { Ok(conn) => conn, Err(e) => { return Box::pin(future::err(internal_err(format!( @@ -319,7 +319,7 @@ impl EthApi for EthService { )))); } }; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let is_pending = matches!(number, Some(BlockNumber::Pending)); @@ -382,7 +382,7 @@ impl EthApi for EthService { fn author(&self) -> BoxFuture> { log::info!(target: "eth api", "author"); - let mut conn = match self.pool.get() { + let conn = match self.pool.get_connection() { Ok(conn) => conn, Err(e) => { return Box::pin(future::err(internal_err(format!( @@ -391,7 +391,7 @@ impl EthApi for EthService { )))); } }; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let height = match getter.latest_height() { Ok(h) => h, @@ -448,7 +448,7 @@ impl EthApi for EthService { fn block_number(&self) -> BoxFuture> { log::info!(target: "eth api", "block_number"); - let mut conn = match self.pool.get() { + let conn = match self.pool.get_connection() { Ok(conn) => conn, Err(e) => { return Box::pin(future::err(internal_err(format!( @@ -457,7 +457,7 @@ impl EthApi for EthService { )))); } }; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); match getter.latest_height() { Ok(height) => Box::pin(future::ok(U256::from(height))), @@ -475,7 +475,7 @@ impl EthApi for EthService { number: Option, ) -> BoxFuture> { log::info!(target: "eth api", "storage_at address:{:?} index:{:?} number:{:?}", &address, &index, &number); - let mut conn = match self.pool.get() { + let conn = match self.pool.get_connection() { Ok(conn) => conn, Err(e) => { return Box::pin(future::err(internal_err(format!( @@ -484,7 +484,7 @@ impl EthApi for EthService { )))); } }; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); if let Some(BlockNumber::Pending) = number { match getter.get_pending_state(address, H256::from_uint(&index)) { Ok(value) => { @@ -520,7 +520,7 @@ impl EthApi for EthService { fn block_by_hash(&self, hash: H256, full: bool) -> BoxFuture>> { log::info!(target: "eth api", "block_by_hash hash:{:?} full:{:?}", &hash, &full); - let mut conn = match self.pool.get() { + let conn = match self.pool.get_connection() { Ok(conn) => conn, Err(e) => { return Box::pin(future::err(internal_err(format!( @@ -529,7 +529,7 @@ impl EthApi for EthService { )))); } }; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let block = match getter.get_block_by_hash(hash) { Ok(value) => { @@ -574,7 +574,7 @@ impl EthApi for EthService { full: bool, ) -> BoxFuture>> { log::info!(target: "eth api", "block_by_number number:{:?} full:{:?}", &number, &full); - let mut conn = match self.pool.get() { + let conn = match self.pool.get_connection() { Ok(conn) => conn, Err(e) => { return Box::pin(future::err(internal_err(format!( @@ -583,7 +583,7 @@ impl EthApi for EthService { )))); } }; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let height = match block_number_to_height(Some(number), &mut getter) { Ok(h) => h, @@ -653,7 +653,7 @@ impl EthApi for EthService { number: Option, ) -> BoxFuture> { log::info!(target: "eth api", "transaction_count address:{:?} number:{:?}", &address, &number); - let mut conn = match self.pool.get() { + let conn = match self.pool.get_connection() { Ok(conn) => conn, Err(e) => { return Box::pin(future::err(internal_err(format!( @@ -662,7 +662,7 @@ impl EthApi for EthService { )))); } }; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); if let Some(BlockNumber::Pending) = number { match getter.get_pending_nonce(address) { Ok(nonce) => { @@ -698,7 +698,7 @@ impl EthApi for EthService { fn block_transaction_count_by_hash(&self, hash: H256) -> BoxFuture>> { log::info!(target: "eth api", "block_transaction_count_by_hash hash:{:?}", &hash); - let mut conn = match self.pool.get() { + let conn = match self.pool.get_connection() { Ok(conn) => conn, Err(e) => { return Box::pin(future::err(internal_err(format!( @@ -707,7 +707,7 @@ impl EthApi for EthService { )))); } }; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let block = match getter.get_block_by_hash(hash) { Ok(value) => { if let Some(hash_index) = value { @@ -731,7 +731,7 @@ impl EthApi for EthService { number: BlockNumber, ) -> BoxFuture>> { log::info!(target: "eth api", "block_transaction_count_by_number number:{:?}", &number); - let mut conn = match self.pool.get() { + let conn = match self.pool.get_connection() { Ok(conn) => conn, Err(e) => { return Box::pin(future::err(internal_err(format!( @@ -740,7 +740,7 @@ impl EthApi for EthService { )))); } }; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let height = match block_number_to_height(Some(number), &mut getter) { Ok(h) => h, @@ -801,7 +801,7 @@ impl EthApi for EthService { if address == H160::from_low_u64_be(0x1000) { return Box::pin(future::ok(Bytes::new(b"fra".to_vec()))); } - let mut conn = match self.pool.get() { + let conn = match self.pool.get_connection() { Ok(conn) => conn, Err(e) => { return Box::pin(future::err(internal_err(format!( @@ -810,7 +810,7 @@ impl EthApi for EthService { )))); } }; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); if let Some(BlockNumber::Pending) = number { match getter.get_pending_byte_code(address) { Ok(byte_code) => { @@ -902,7 +902,7 @@ impl EthApi for EthService { number: Option, ) -> BoxFuture> { log::info!(target: "eth api", "estimate_gas request:{:?} number:{:?}", &request, &number); - let mut conn = match self.pool.get() { + let conn = match self.pool.get_connection() { Ok(conn) => conn, Err(e) => { return Box::pin(future::err(internal_err(format!( @@ -911,7 +911,7 @@ impl EthApi for EthService { )))); } }; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let is_pending = matches!(number, Some(BlockNumber::Pending)); let height = match block_number_to_height(number, &mut getter) { @@ -1074,7 +1074,7 @@ impl EthApi for EthService { fn transaction_by_hash(&self, tx_hash: H256) -> BoxFuture>> { log::info!(target: "eth api", "transaction_by_hash tx_hash:{:?}", &tx_hash); - let mut conn = match self.pool.get() { + let conn = match self.pool.get_connection() { Ok(conn) => conn, Err(e) => { return Box::pin(future::err(internal_err(format!( @@ -1083,7 +1083,7 @@ impl EthApi for EthService { )))); } }; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let (hash, index) = match getter.get_transaction_index_by_tx_hash(tx_hash) { Ok(value) => { if let Some(hash_index) = value { @@ -1152,7 +1152,7 @@ impl EthApi for EthService { index: Index, ) -> BoxFuture>> { log::info!(target: "eth api", "transaction_by_block_hash_and_index hash:{:?} index:{:?}", &hash, &index); - let mut conn = match self.pool.get() { + let conn = match self.pool.get_connection() { Ok(conn) => conn, Err(e) => { return Box::pin(future::err(internal_err(format!( @@ -1161,7 +1161,7 @@ impl EthApi for EthService { )))); } }; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let block = match getter.get_block_by_hash(hash) { Ok(value) => { if let Some(hash_index) = value { @@ -1215,7 +1215,7 @@ impl EthApi for EthService { index: Index, ) -> BoxFuture>> { log::info!(target: "eth api", "transaction_by_block_number_and_index number:{:?} index:{:?}", &number, &index); - let mut conn = match self.pool.get() { + let conn = match self.pool.get_connection() { Ok(conn) => conn, Err(e) => { return Box::pin(future::err(internal_err(format!( @@ -1224,7 +1224,7 @@ impl EthApi for EthService { )))); } }; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let height = match block_number_to_height(Some(number), &mut getter) { Ok(h) => h, @@ -1300,7 +1300,7 @@ impl EthApi for EthService { fn transaction_receipt(&self, tx_hash: H256) -> BoxFuture>> { log::info!(target: "eth api", "transaction_receipt tx_hash:{:?}", &tx_hash); - let mut conn = match self.pool.get() { + let conn = match self.pool.get_connection() { Ok(conn) => conn, Err(e) => { return Box::pin(future::err(internal_err(format!( @@ -1309,7 +1309,7 @@ impl EthApi for EthService { )))); } }; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let (hash, index) = match getter.get_transaction_index_by_tx_hash(tx_hash) { Ok(value) => { if let Some(hash_index) = value { @@ -1464,7 +1464,7 @@ impl EthApi for EthService { fn logs(&self, filter: Filter) -> BoxFuture>> { log::info!(target: "eth api", "logs filter:{:?}", &filter); - let mut conn = match self.pool.get() { + let conn = match self.pool.get_connection() { Ok(conn) => conn, Err(e) => { return Box::pin(future::err(internal_err(format!( @@ -1473,7 +1473,7 @@ impl EthApi for EthService { )))); } }; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let mut ret: Vec = Vec::new(); if let Some(block_hash) = filter.block_hash { diff --git a/web3-service/src/rpc/eth_filter.rs b/web3-service/src/rpc/eth_filter.rs index 7a8985b..c453107 100644 --- a/web3-service/src/rpc/eth_filter.rs +++ b/web3-service/src/rpc/eth_filter.rs @@ -1,7 +1,7 @@ use { super::{eth::filter_block_logs, internal_err, MAX_PAST_LOGS, MAX_STORED_FILTERS}, ethereum_types::{H256, U256}, - evm_exporter::{Block, Getter, TransactionStatus, PREFIX}, + evm_exporter::{Block, ConnectionType, Getter, RedisGetter, TransactionStatus, PREFIX}, futures::executor::ThreadPool, jsonrpc_core::Result, lazy_static::lazy_static, @@ -28,11 +28,11 @@ const FILTER_RETAIN_THRESHOLD: u64 = 100; pub struct EthFilterApiImpl { filter_pool: FilterPool, - redis_pool: Arc>, + redis_pool: Arc, } impl EthFilterApiImpl { - pub fn new(redis_pool: Arc>) -> Self { + pub fn new(redis_pool: Arc) -> Self { let pool = Arc::new(Mutex::new(BTreeMap::new())); let instance = Self { filter_pool: pool.clone(), @@ -42,11 +42,11 @@ impl EthFilterApiImpl { instance } fn block_number(&self) -> Result { - let mut conn = self + let conn = self .redis_pool - .get() + .get_connection() .map_err(|e| internal_err(e.to_string()))?; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); getter .latest_height() .map_err(|e| internal_err(e.to_string())) @@ -85,11 +85,11 @@ impl EthFilterApiImpl { } async fn filter_pool_task( - redis_pool: Arc>, + redis_pool: Arc, filter_pool: Arc>>, ) { - let mut conn = redis_pool.get().expect("get redis connect"); - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let conn = redis_pool.get_connection().expect("get redis connect"); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let mut last_height = getter.latest_height().expect("redis latest_height error"); let ten_millis = time::Duration::from_millis(100); loop { @@ -123,11 +123,11 @@ impl EthFilterApiImpl { } fn get_block(&self, height: u64) -> Result> { - let mut conn = self + let conn = self .redis_pool - .get() + .get_connection() .map_err(|e| internal_err(e.to_string()))?; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let hash = match getter .get_block_hash_by_height(U256::from(height)) .map_err(|e| internal_err(e.to_string()))? @@ -142,11 +142,11 @@ impl EthFilterApiImpl { .map_err(|e| internal_err(e.to_string())) } fn get_transaction_statuses(&self, height: u64) -> Result>> { - let mut conn = self + let conn = self .redis_pool - .get() + .get_connection() .map_err(|e| internal_err(e.to_string()))?; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let hash = match getter .get_block_hash_by_height(U256::from(height)) .map_err(|e| internal_err(e.to_string()))? diff --git a/web3-service/src/rpc/eth_pubsub.rs b/web3-service/src/rpc/eth_pubsub.rs index 421fa9e..0b4e7cd 100644 --- a/web3-service/src/rpc/eth_pubsub.rs +++ b/web3-service/src/rpc/eth_pubsub.rs @@ -1,7 +1,7 @@ use { crate::notify::SubscriberNotify, ethereum_types::{H256, U256}, - evm_exporter::{Block, Getter, Receipt, PREFIX}, + evm_exporter::{Block, ConnectionType, Getter, Receipt, RedisGetter, PREFIX}, futures::{ executor::ThreadPool, task::{FutureObj, Spawn, SpawnError}, @@ -37,15 +37,12 @@ impl Spawn for SubscriptionTaskExecutor { } } pub struct EthPubSubApiImpl { - redis_pool: Arc>, + redis_pool: Arc, subscriptions: SubscriptionManager, subscriber_notify: Arc, } impl EthPubSubApiImpl { - pub fn new( - redis_pool: Arc>, - subscriber_notify: Arc, - ) -> Self { + pub fn new(redis_pool: Arc, subscriber_notify: Arc) -> Self { Self { redis_pool, subscriptions: SubscriptionManager::new(Arc::new(SubscriptionTaskExecutor)), @@ -77,8 +74,8 @@ impl EthPubSubApi for EthPubSubApiImpl { .logs_event_notify .notification_stream() .filter_map(move |block_height| { - let info = redis_pool.get().map(|mut conn| { - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let info = redis_pool.get_connection().map(| conn| { + let mut getter:RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); match getter.get_block_hash_by_height(block_height) { Ok(Some(hash)) => { let block = match getter.get_block_by_hash(hash) { @@ -129,8 +126,8 @@ impl EthPubSubApi for EthPubSubApiImpl { .new_heads_event_notify .notification_stream() .filter_map(move |block_height| { - let block = redis_pool.get().map(|mut conn|{ - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()) ; + let block = redis_pool.get_connection().map(| conn|{ + let mut getter:RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()) ; match getter.get_block_hash_by_height(block_height) { Ok(Some(hash)) => { match getter.get_block_by_hash(hash) { diff --git a/web3-service/src/utils/mod.rs b/web3-service/src/utils/mod.rs index 002ba92..c01a4f6 100644 --- a/web3-service/src/utils/mod.rs +++ b/web3-service/src/utils/mod.rs @@ -1,8 +1,8 @@ -use {evm_exporter::Getter, redis::ConnectionLike, ruc::*, web3_rpc_core::types::BlockNumber}; +use {evm_exporter::Getter, ruc::*, web3_rpc_core::types::BlockNumber}; -pub fn block_number_to_height( +pub fn block_number_to_height( block_number: Option, - getter: &mut Getter, + getter: &mut dyn Getter, ) -> Result { let height = match block_number.unwrap_or(BlockNumber::Latest) { BlockNumber::Hash { diff --git a/web3-service/src/vm/precompile/frc20/mod.rs b/web3-service/src/vm/precompile/frc20/mod.rs index ff038d4..3f14d10 100644 --- a/web3-service/src/vm/precompile/frc20/mod.rs +++ b/web3-service/src/vm/precompile/frc20/mod.rs @@ -10,7 +10,7 @@ use { executor::stack::{PrecompileFailure, PrecompileOutput}, Context, ExitSucceed, }, - evm_exporter::{Getter, PREFIX}, + evm_exporter::{ConnectionType, Getter, RedisGetter, PREFIX}, evm_runtime::ExitError, log::debug, slices::u8_slice, @@ -88,16 +88,16 @@ impl FRC20 { 0x1000 } fn get_balance(&self, addr: H160) -> EvmResult { - let mut conn = REDIS_POOL + let conn = REDIS_POOL .get() .ok_or_else(|| ExitError::Other(Cow::from("REDIS_POOL get error"))) .and_then(|redis_pool| { - redis_pool.get().map_err(|e| { + redis_pool.get_connection().map_err(|e| { ExitError::Other(Cow::from(format!("redis get connect error:{:?}", e))) }) })?; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let amount = getter .get_balance(self.height, addr) .map_err(|e| ExitError::Other(Cow::from(format!("redis get value error:{:?}", e))))?; @@ -107,16 +107,16 @@ impl FRC20 { } fn get_allowance(&self, owner: H160, spender: H160) -> EvmResult { - let mut conn = REDIS_POOL + let conn = REDIS_POOL .get() .ok_or_else(|| ExitError::Other(Cow::from("REDIS_POOL get error"))) .and_then(|redis_pool| { - redis_pool.get().map_err(|e| { + redis_pool.get_connection().map_err(|e| { ExitError::Other(Cow::from(format!("redis get connect error:{:?}", e))) }) })?; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let amount = getter .get_allowances(self.height, owner, spender) .map_err(|e| ExitError::Other(Cow::from(format!("redis get value error:{:?}", e))))?; @@ -257,16 +257,16 @@ impl FRC20 { gasometer.record_cost(GAS_TOTAL_SUPPLY)?; input.expect_arguments(0)?; - let mut conn = REDIS_POOL + let conn = REDIS_POOL .get() .ok_or_else(|| ExitError::Other(Cow::from("REDIS_POOL get error"))) .and_then(|redis_pool| { - redis_pool.get().map_err(|e| { + redis_pool.get_connection().map_err(|e| { ExitError::Other(Cow::from(format!("redis get connect error:{:?}", e))) }) })?; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let amount: U256 = getter .get_total_issuance(self.height) .map_err(|e| ExitError::Other(Cow::from(format!("redis get value error:{:?}", e))))?; diff --git a/web3-service/src/vm/precompile/mod.rs b/web3-service/src/vm/precompile/mod.rs index c258621..94c313d 100644 --- a/web3-service/src/vm/precompile/mod.rs +++ b/web3-service/src/vm/precompile/mod.rs @@ -22,16 +22,15 @@ use { identity::Identity, modexp::Modexp, once_cell::sync::{Lazy, OnceCell}, - r2d2::Pool, ripemd160_precompile::Ripemd160, ruc::*, sha256::Sha256, std::{collections::BTreeMap, sync::Arc}, }; #[cfg(feature = "cluster_redis")] -pub static REDIS_POOL: OnceCell>> = OnceCell::new(); +pub static REDIS_POOL: OnceCell> = OnceCell::new(); #[cfg(not(feature = "cluster_redis"))] -pub static REDIS_POOL: OnceCell>> = OnceCell::new(); +pub static REDIS_POOL: OnceCell> = OnceCell::new(); pub type PrecompileResult = core::result::Result; diff --git a/web3-service/src/vm/stack.rs b/web3-service/src/vm/stack.rs index cafe16b..97f79d0 100644 --- a/web3-service/src/vm/stack.rs +++ b/web3-service/src/vm/stack.rs @@ -5,7 +5,7 @@ use { backend::{Backend, Basic}, executor::stack::{Accessed, StackState, StackSubstateMetadata}, }, - evm_exporter::{Getter, PREFIX}, + evm_exporter::{ConnectionType, Getter, RedisGetter, PREFIX}, jsonrpc_core::Value, ruc::{d, eg, RucResult}, std::{ @@ -22,7 +22,7 @@ pub struct Web3EvmStackstate<'config> { chain_id: u32, height: u32, origin: H160, - pool: Arc>, + pool: Arc, tendermint_url: String, metadata: StackSubstateMetadata<'config>, deletes: BTreeSet, @@ -39,7 +39,7 @@ pub struct Web3EvmStackstate<'config> { height: u32, is_pending: bool, origin: H160, - pool: Arc>, + pool: Arc, tendermint_url: String, metadata: StackSubstateMetadata<'config>, deletes: BTreeSet, @@ -58,7 +58,7 @@ impl<'config> Web3EvmStackstate<'config> { height: u32, is_pending: bool, origin: H160, - pool: Arc>, + pool: Arc, tendermint_url: &str, metadata: StackSubstateMetadata<'config>, ) -> Self { @@ -87,7 +87,7 @@ impl<'config> Web3EvmStackstate<'config> { height: u32, is_pending: bool, origin: H160, - pool: Arc>, + pool: Arc, tendermint_url: &str, metadata: StackSubstateMetadata<'config>, ) -> Self { @@ -131,8 +131,9 @@ impl<'config> Backend for Web3EvmStackstate<'config> { fn block_hash(&self, height: U256) -> H256 { let func = || -> ruc::Result { - let mut conn = self.pool.get().c(d!())?; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let conn = self.pool.get_connection().c(d!())?; + let mut getter: RedisGetter = + Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); getter .get_block_hash_by_height(height) .c(d!())? @@ -143,8 +144,9 @@ impl<'config> Backend for Web3EvmStackstate<'config> { fn block_number(&self) -> U256 { let func = || -> ruc::Result { - let mut conn = self.pool.get().c(d!())?; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let conn = self.pool.get_connection().c(d!())?; + let mut getter: RedisGetter = + Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); getter.latest_height().c(d!()).map(U256::from) }; func().unwrap_or_default() @@ -174,8 +176,9 @@ impl<'config> Backend for Web3EvmStackstate<'config> { fn block_timestamp(&self) -> U256 { let func = || -> ruc::Result { - let mut conn = self.pool.get().c(d!())?; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let conn = self.pool.get_connection().c(d!())?; + let mut getter: RedisGetter = + Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let block_hash = getter .get_block_hash_by_height(U256::from(self.height)) .c(d!())? @@ -191,8 +194,9 @@ impl<'config> Backend for Web3EvmStackstate<'config> { fn block_difficulty(&self) -> U256 { let func = || -> ruc::Result { - let mut conn = self.pool.get().c(d!())?; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let conn = self.pool.get_connection().c(d!())?; + let mut getter: RedisGetter = + Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let block_hash = getter .get_block_hash_by_height(U256::from(self.height)) .c(d!())? @@ -208,8 +212,9 @@ impl<'config> Backend for Web3EvmStackstate<'config> { fn block_gas_limit(&self) -> U256 { let func = || -> ruc::Result { - let mut conn = self.pool.get().c(d!())?; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let conn = self.pool.get_connection().c(d!())?; + let mut getter: RedisGetter = + Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let block_hash = getter .get_block_hash_by_height(U256::from(self.height)) .c(d!())? @@ -233,8 +238,9 @@ impl<'config> Backend for Web3EvmStackstate<'config> { fn exists(&self, address: H160) -> bool { let func = || -> ruc::Result { - let mut conn = self.pool.get().c(d!())?; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let conn = self.pool.get_connection().c(d!())?; + let mut getter: RedisGetter = + Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let balance = getter.get_balance(self.height, address).c(d!())?; let nonce = getter.get_nonce(self.height, address).c(d!())?; Ok(nonce != U256::zero() && balance != U256::zero()) @@ -244,8 +250,9 @@ impl<'config> Backend for Web3EvmStackstate<'config> { fn basic(&self, address: H160) -> Basic { let func = || -> ruc::Result { - let mut conn = self.pool.get().c(d!())?; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let conn = self.pool.get_connection().c(d!())?; + let mut getter: RedisGetter = + Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let balance = if self.is_pending { getter @@ -275,8 +282,9 @@ impl<'config> Backend for Web3EvmStackstate<'config> { return value.to_vec(); } let func = || -> ruc::Result> { - let mut conn = self.pool.get().c(d!())?; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let conn = self.pool.get_connection().c(d!())?; + let mut getter: RedisGetter = + Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); Ok(if self.is_pending { getter @@ -296,8 +304,9 @@ impl<'config> Backend for Web3EvmStackstate<'config> { } let func = || -> ruc::Result { - let mut conn = self.pool.get().c(d!())?; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let conn = self.pool.get_connection().c(d!())?; + let mut getter: RedisGetter = + Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); Ok(if self.is_pending { getter @@ -396,8 +405,9 @@ impl<'config> StackState<'config> for Web3EvmStackstate<'config> { fn is_empty(&self, address: H160) -> bool { let func = || -> ruc::Result { - let mut conn = self.pool.get().c(d!())?; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let conn = self.pool.get_connection().c(d!())?; + let mut getter: RedisGetter = + Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let balance = getter.get_balance(self.height, address).c(d!())?; let nonce = getter.get_nonce(self.height, address).c(d!())?; let code = getter.get_byte_code(self.height, address).c(d!())?; @@ -459,11 +469,11 @@ impl<'config> StackState<'config> for Web3EvmStackstate<'config> { } fn transfer(&mut self, transfer: evm::Transfer) -> Result<(), evm::ExitError> { - let mut conn = self + let conn = self .pool - .get() + .get_connection() .map_err(|_| evm::ExitError::Other(Cow::from("redis connect error")))?; - let mut getter = Getter::new(&mut *conn, PREFIX.to_string()); + let mut getter: RedisGetter = Getter::new(ConnectionType::Redis(conn), PREFIX.to_string()); let default_source_balance = getter .get_balance(self.height, transfer.source)