Skip to content

Commit

Permalink
Use postgres as enterprise web3 setter (#1058)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommady authored Aug 27, 2024
1 parent 4076096 commit d5b30c3
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 60 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/Fn_Check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ jobs:
matrix:
platform: [scalable]
runs-on: ${{ matrix.platform }}
env:
ACTIONS_ALLOW_USE_UNSECURE_NODE_VERSION: true
steps:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/Main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
platform: [scalable]
runs-on: ${{ matrix.platform }}
env:
ACTIONS_ALLOW_USE_UNSECURE_NODE_VERSION: true
ENV: dev
PRIVATE_ECR_URL: 358484141435.dkr.ecr.us-west-2.amazonaws.com
PUBLIC_ECR_URL: public.ecr.aws/k6m5b6e2
Expand Down
8 changes: 3 additions & 5 deletions src/components/abciapp/src/abci/server/callback/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use {
chrono::Local,
config::abci::global_cfg::CFG,
enterprise_web3::{
Setter, ALLOWANCES, BALANCE_MAP, BLOCK, CODE_MAP, NONCE_MAP, RECEIPTS,
REDIS_CLIENT, STATE_UPDATE_LIST, TOTAL_ISSUANCE, TXS, WEB3_SERVICE_START_HEIGHT,
ALLOWANCES, BALANCE_MAP, BLOCK, CODE_MAP, NONCE_MAP, RECEIPTS,
PG_CLIENT, STATE_UPDATE_LIST, TOTAL_ISSUANCE, TXS, WEB3_SERVICE_START_HEIGHT,
},
fp_storage::hash::{Sha256, StorageHasher},
fp_storage::BorrowMut,
Expand Down Expand Up @@ -660,9 +660,7 @@ pub fn commit(s: &mut ABCISubmissionServer, req: &RequestCommit) -> ResponseComm

if CFG.enable_enterprise_web3 && td_height as u64 > *WEB3_SERVICE_START_HEIGHT {
let height = td_height as u32;
let redis_pool = REDIS_CLIENT.lock().expect("REDIS_CLIENT error");
let mut conn = redis_pool.get().expect("get redis connect");
let mut setter = Setter::new(&mut *conn, "evm".to_string());
let setter = PG_CLIENT.lock().expect("PG_CLIENT error");

let nonce_map = if let Ok(mut nonce_map) = NONCE_MAP.lock() {
take(&mut *nonce_map)
Expand Down
21 changes: 5 additions & 16 deletions src/components/contracts/baseapp/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::extensions::SignedExtra;
use abci::*;
use config::abci::global_cfg::CFG;
use enterprise_web3::{
Setter, PENDING_CODE_MAP, PENDING_STATE_UPDATE_LIST, REDIS_CLIENT,
REMOVE_PENDING_CODE_MAP, REMOVE_PENDING_STATE_UPDATE_LIST,
PENDING_CODE_MAP, PENDING_STATE_UPDATE_LIST, PG_CLIENT, REMOVE_PENDING_CODE_MAP,
REMOVE_PENDING_STATE_UPDATE_LIST,
};
use fp_core::context::RunTxMode;
use fp_evm::BlockId;
Expand Down Expand Up @@ -110,13 +110,8 @@ impl crate::BaseApp {
fp_types::actions::ethereum::Action::Transact(tx),
) = tx.function
{
let redis_pool =
REDIS_CLIENT.lock().expect("REDIS_CLIENT error");
let mut conn =
redis_pool.get().expect("get redis connect");
let mut setter =
Setter::new(&mut *conn, "evm".to_string());

let setter =
PG_CLIENT.lock().expect("PG_CLIENT error");
setter
.set_pending_tx(tx)
.map_err(|e| error!("{e:?}"))
Expand Down Expand Up @@ -239,13 +234,7 @@ impl crate::BaseApp {
fp_types::actions::ethereum::Action::Transact(tx),
) = tx.function
{
let redis_pool =
REDIS_CLIENT.lock().expect("REDIS_CLIENT error");
let mut conn =
redis_pool.get().expect("get redis connect");
let mut setter =
Setter::new(&mut *conn, "evm".to_string());

let setter = PG_CLIENT.lock().expect("PG_CLIENT error");
setter
.remove_pending_tx(tx)
.map_err(|e| error!("{:?}", e))
Expand Down
4 changes: 2 additions & 2 deletions src/components/contracts/baseapp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl BlockGasLimit {
pub fn get() -> U256 {
let td_height = LEDGER_TENDERMINT_BLOCK_HEIGHT.load(Ordering::Relaxed);
if td_height < CFG.checkpoint.max_gas_price_limit {
U256::from(u32::max_value())
U256::from(u32::MAX)
} else {
U256::from(100000000)
}
Expand All @@ -121,7 +121,7 @@ impl<I: From<U256>> ::fp_core::macros::Get<I> for BlockGasLimit {
fn get() -> I {
let td_height = LEDGER_TENDERMINT_BLOCK_HEIGHT.load(Ordering::Relaxed);
if td_height < CFG.checkpoint.max_gas_price_limit {
I::from(U256::from(u32::max_value()))
I::from(U256::from(u32::MAX))
} else {
I::from(U256::from(100000000))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ edition = "2021"

[dependencies]
lazy_static = "1.4.0"
evm-exporter = { package = "evm-exporter", git = "https://github.com/FindoraNetwork/enterprise-web3.git", tag = "1.2.1"}
evm-exporter = { package = "evm-exporter", git = "https://github.com/FindoraNetwork/enterprise-web3.git", branch = "main", features = ["postgres"]}
ethereum = { version = "0.12.0", default-features = false, features = ["with-serde"] }
primitive-types = "0.11.1"
redis = { version = "0.21", default-features = false, features = [ "tls", "r2d2" ] }
r2d2 = { version = "0.8.8"}
ruc = "1.0"
28 changes: 12 additions & 16 deletions src/components/contracts/primitives/enterprise-web3/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
use evm_exporter::{
Block as EnterpriseBlock, Getter, Receipt as EnterpriseReceipt,
State as EnterpriseState, TransactionStatus as EnterpriseTxState,
Block as EnterpriseBlock, ConnectionType, Getter, PgGetter, PgSetter,
Receipt as EnterpriseReceipt, Setter as EnterpriseSetter, State as EnterpriseState,
TransactionStatus as EnterpriseTxState,
};
use lazy_static::lazy_static;
use primitive_types::{H160, H256, U256};
use redis::Client;
use ruc::*;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

pub use evm_exporter::Setter;

pub type State = EnterpriseState;
pub type Block = EnterpriseBlock;
pub type Receipt = EnterpriseReceipt;
pub type TxState = EnterpriseTxState;
pub type Setter = dyn EnterpriseSetter;

pub struct AllowancesKey {
pub owner_address: H160,
Expand All @@ -33,8 +31,8 @@ lazy_static! {
pub static ref BLOCK: Arc<Mutex<Option<Block>>> = Arc::new(Mutex::new(None));
pub static ref RECEIPTS: Arc<Mutex<Vec<Receipt>>> = Arc::new(Mutex::new(vec![]));
pub static ref TXS: Arc<Mutex<Vec<TxState>>> = Arc::new(Mutex::new(vec![]));
pub static ref REDIS_CLIENT: Arc<Mutex<r2d2::Pool<Client>>> =
Arc::new(Mutex::new(gen_redis_client()));
pub static ref PG_CLIENT: Arc<Mutex<Box<dyn EnterpriseSetter + Send + Sync>>> =
Arc::new(Mutex::new(gen_postgres_client()));
pub static ref WEB3_SERVICE_START_HEIGHT: u64 = load_start_height();
pub static ref PENDING_CODE_MAP: Arc<Mutex<HashMap<H160, Vec<u8>>>> =
Arc::new(Mutex::new(HashMap::new()));
Expand All @@ -49,17 +47,15 @@ lazy_static! {
Arc::new(Mutex::new(Vec::new()));
}

fn gen_redis_client() -> r2d2::Pool<Client> {
let redis_addr = std::env::var("REDIS_ADDR").unwrap();
let client = Client::open(redis_addr).unwrap();
let pool = pnk!(r2d2::Pool::builder().max_size(50).build(client));
pool
fn gen_postgres_client() -> Box<dyn EnterpriseSetter + Send + Sync> {
let uri = std::env::var("POSTGRES_URI").expect("loading env POSTGRES_URI failed");
Box::new(PgSetter::new(ConnectionType::Postgres(uri), String::new()))
}

fn load_start_height() -> u64 {
let redis_pool = REDIS_CLIENT.lock().unwrap();
let mut conn = redis_pool.get().expect("get redis connect");
let mut getter = Getter::new(&mut *conn, "evm".to_string());
let uri = std::env::var("POSTGRES_URI").expect("loading env POSTGRES_URI failed");
let getter: Box<dyn Getter> =
Box::new(PgGetter::new(ConnectionType::Postgres(uri), String::new()));
let last_height = getter.latest_height().expect("redis latest_height error");
last_height as u64
}
45 changes: 27 additions & 18 deletions src/ledger/src/staking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2019,40 +2019,49 @@ impl Validator {
}
}

/// FRA delegation, include:
/// - user delegation
/// - validator's self-delegation
/// FRA delegation, includes:
/// - User delegation
/// - Validator's self-delegation
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Delegation {
/// validator pubkey => amount
/// Validator pubkey => amount
/// - `NonConfidential` FRAs amount
/// - valid for all delegators
/// delegations contains an entry for each validator this `delegator` has delegated to
/// XfrPublicKey here has to be the public key of a validator
/// - Valid for all delegators
///
/// Delegations contains an entry for each validator this `delegator` has delegated to.
/// `XfrPublicKey` here has to be the public key of a validator.
#[serde(rename = "entries")]
pub delegations: BTreeMap<XfrPublicKey, Amount>,

/// delegation rewards will be paid to this pk by default
/// Delegation rewards will be paid to this public key by default.
pub id: XfrPublicKey,
/// optional receiver address,
/// if this one exists, tokens will be paid to it instead of id

/// Optional receiver address.
/// If this one exists, tokens will be paid to it instead of `id`.
pub receiver_pk: Option<XfrPublicKey>,
/// Temporary partial undelegations of current id

/// Temporary partial undelegations of the current `id`.
pub tmp_delegators: BTreeMap<XfrPublicKey, Amount>,
/// the joint height of the delegtator

/// The joint height of the delegator.
pub start_height: BlockHeight,
/// the height at which the delegation ends

/// The height at which the delegation ends.
///
/// **NOTE:** before users can actually get the rewards,
/// they need to wait for an extra `UNBOND_BLOCK_CNT` period
/// **NOTE:** Before users can actually get the rewards,
/// they need to wait for an extra `UNBOND_BLOCK_CNT` period.
pub end_height: BlockHeight,

#[allow(missing_docs)]
pub state: DelegationState,
/// set this field when `Bond` state finished

/// Set this field when the `Bond` state is finished.
pub rwd_amount: Amount,
/// how many times you get proposer rewards

/// How many times you get proposer rewards.
pub proposer_rwd_cnt: u64,
/// how many times you get delegation rewards

/// How many times you get delegation rewards.
pub delegation_rwd_cnt: u64,
}

Expand Down

0 comments on commit d5b30c3

Please sign in to comment.