Skip to content

Commit

Permalink
Fix web3 rpc (#1028)
Browse files Browse the repository at this point in the history
* add time log

* fix_web3_rpc

* fix fn show rewards

---------

Co-authored-by: shaorongqiang <shaorongqiang@email.com>
  • Loading branch information
shaorongqiang and shaorongqiang authored Oct 27, 2023
1 parent 0ba9a56 commit b8e88ae
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 32 deletions.
1 change: 1 addition & 0 deletions src/components/abciapp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protobuf = "2.16"
toml = "0.5.8"
regex = "1"
clap = "2.33.3"
chrono = "0.4.31"

actix-cors = "0.5.4"
actix-rt = "1.1.0"
Expand Down
21 changes: 21 additions & 0 deletions src/components/abciapp/src/abci/server/callback/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! # Impl function of tendermint ABCI
//!

use chrono::Local;
use fp_storage::BorrowMut;

mod utils;
Expand Down Expand Up @@ -59,6 +60,9 @@ use {

pub(crate) static TENDERMINT_BLOCK_HEIGHT: AtomicI64 = AtomicI64::new(0);

pub(crate) static BEGIN_BLOCK_TIME: AtomicI64 = AtomicI64::new(0);
pub(crate) static END_BLOCK_TIME: AtomicI64 = AtomicI64::new(0);

lazy_static! {
// save the request parameters from the begin_block for use in the end_block
static ref REQ_BEGIN_BLOCK: Arc<Mutex<RequestBeginBlock>> =
Expand Down Expand Up @@ -235,6 +239,10 @@ pub fn begin_block(
LEDGER_TENDERMINT_BLOCK_HEIGHT.swap(header.height, Ordering::Relaxed);
*REQ_BEGIN_BLOCK.lock() = req.clone();

let start = Local::now().timestamp_millis();
BEGIN_BLOCK_TIME.swap(start, Ordering::Relaxed);
info!(target: "abcitime", "begin_block height:{}, milliseconds:{}", header.height, start);

let mut la = s.la.write();

// set height first
Expand Down Expand Up @@ -581,6 +589,10 @@ pub fn end_block(
{
resp.validator_updates = evm_resp.validator_updates;
}
let start = BEGIN_BLOCK_TIME.load(Ordering::Relaxed);
let end = Local::now().timestamp_millis();
END_BLOCK_TIME.swap(end, Ordering::Relaxed);
info!(target: "abcitime", "end_block height:{}, end:{}-start:{}={}", header.height, end, start, end - start);

resp
}
Expand All @@ -603,8 +615,12 @@ pub fn commit(s: &mut ABCISubmissionServer, req: &RequestCommit) -> ResponseComm
.and_then(|s| fs::write(&path, s).c(d!(path))));

let mut r = ResponseCommit::new();
let begin_la_hash = Local::now().timestamp_millis();
let la_hash = state.get_state_commitment().0.as_ref().to_vec();
let begin_cs_hash = Local::now().timestamp_millis();
let cs_hash = s.account_base_app.write().commit(req).data;
let end_cs_hash = Local::now().timestamp_millis();
info!(target: "abcitime", "commit height:{}, la_hash:{} cs_hash:{}", td_height, begin_cs_hash - begin_la_hash, end_cs_hash - begin_cs_hash);

if CFG.checkpoint.disable_evm_block_height < td_height
&& td_height < CFG.checkpoint.enable_frc20_height
Expand All @@ -613,6 +629,9 @@ pub fn commit(s: &mut ABCISubmissionServer, req: &RequestCommit) -> ResponseComm
} else {
r.set_data(app_hash("commit", td_height, la_hash, cs_hash));
}
let end = END_BLOCK_TIME.load(Ordering::Relaxed);
let commit = Local::now().timestamp_millis();
info!(target: "abcitime", "commit height:{}, commit:{}-end:{}={}", td_height, commit, end, commit - end);

IN_SAFE_ITV.store(false, Ordering::Release);
if let Some(eth_api_base_app) = &s.eth_api_base_app {
Expand All @@ -621,6 +640,8 @@ pub fn commit(s: &mut ABCISubmissionServer, req: &RequestCommit) -> ResponseComm
.borrow_mut()
.secondary_catch_up_primary());
}
let catch_up = Local::now().timestamp_millis();
info!(target: "abcitime", "catch_up height:{}, catch_up:{}-commit:{}={}", td_height, catch_up, commit, catch_up - commit);

if CFG.enable_enterprise_web3 && td_height as u64 > *WEB3_SERVICE_START_HEIGHT {
let height = td_height as u32;
Expand Down
4 changes: 2 additions & 2 deletions src/components/contracts/baseapp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ protobuf = "2.16"
ruc = "1.0"
serde = {version = "1.0.124", features = ["derive"]}
serde_json = "1.0.40"
storage = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.8" }
fin_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.8" }
storage = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.9" }
fin_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.9" }
sha3 = "0.8"
zei = { git = "https://github.com/FindoraNetwork/zei", branch = "stable-main" }

Expand Down
23 changes: 19 additions & 4 deletions src/components/contracts/baseapp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use notify::*;
use parking_lot::RwLock;
use primitive_types::{H160, H256, U256};
use ruc::{eg, Result};
use std::path::PathBuf;
use std::sync::atomic::Ordering;
use std::{borrow::BorrowMut, path::Path, sync::Arc};
use storage::state::{ChainState, ChainStateOpts};
Expand All @@ -60,6 +61,8 @@ const SNAPSHOT_INTERVAL: u64 = 10 * 24;

#[derive(Clone)]
pub struct BaseApp {
pub basedir: PathBuf,
pub opts: ChainStateOpts,
/// application name from abci.Info
pub name: String,
/// application's version string
Expand Down Expand Up @@ -207,8 +210,11 @@ impl BaseApp {
BLOCKS_IN_DAY * v as u64
}),
};
let chain_state =
Arc::new(RwLock::new(ChainState::create_with_opts(fdb, opts, false)));
let chain_state = Arc::new(RwLock::new(ChainState::create_with_opts(
fdb,
opts.clone(),
false,
)));

let rdb_path = basedir.join(CHAIN_HISTORY_DATA_PATH);
let rdb = RocksDB::open(rdb_path.as_path())?;
Expand All @@ -223,6 +229,8 @@ impl BaseApp {
BaseApp::migrate_initial_db(chain_state.clone(), chain_db.clone())?;

Ok(BaseApp {
basedir: basedir.into(),
opts,
name: APP_NAME.to_string(),
version: "1.0.0".to_string(),
app_version: 1,
Expand Down Expand Up @@ -265,8 +273,11 @@ impl BaseApp {
BLOCKS_IN_DAY * v as u64
}),
};
let chain_state =
Arc::new(RwLock::new(ChainState::create_with_opts(fdb, opts, true)));
let chain_state = Arc::new(RwLock::new(ChainState::create_with_opts(
fdb,
opts.clone(),
true,
)));

let rdb_path = basedir.join(CHAIN_HISTORY_DATA_PATH);
let rdb_secondary_path = basedir.join(CHAIN_HISTORY_SECONDARY_DATA_PATH);
Expand All @@ -283,6 +294,8 @@ impl BaseApp {
)));

Ok(BaseApp {
basedir: basedir.into(),
opts,
name: APP_NAME.to_string(),
version: "1.0.0".to_string(),
app_version: 1,
Expand Down Expand Up @@ -314,6 +327,8 @@ impl BaseApp {
let chain_db = self.chain_db.clone();

BaseApp {
basedir: self.basedir.clone(),
opts: self.opts.clone(),
name: APP_NAME.to_string(),
version: "1.0.0".to_string(),
app_version: 1,
Expand Down
4 changes: 2 additions & 2 deletions src/components/contracts/modules/account/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ primitive-types = { version = "0.11.1", default-features = false, features = ["r
ruc = "1.0"
serde = { version = "1.0.124", features = ["derive"] }
serde_json = "1.0.64"
storage = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.8" }
storage = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.9" }

# primitives, don't depend on any modules
fp-core = { path = "../../primitives/core" }
Expand All @@ -29,4 +29,4 @@ config = { path = "../../../config"}
rand_chacha = "0.2"
parking_lot = "0.12"
zei = { git = "https://github.com/FindoraNetwork/zei", branch = "stable-main" }
fin_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.8" }
fin_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.9" }
4 changes: 2 additions & 2 deletions src/components/contracts/modules/ethereum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ enterprise-web3 = { path = "../../primitives/enterprise-web3" }
baseapp = { path = "../../baseapp" }
fp-mocks = { path = "../../primitives/mocks" }
module-account = { path = "../account" }
storage = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.8" }
fin_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.8" }
storage = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.9" }
fin_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.9" }

[features]
default = []
Expand Down
4 changes: 2 additions & 2 deletions src/components/contracts/modules/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ fp-traits = { path = "../../primitives/traits" }
fp-types = { path = "../../primitives/types" }
fp-utils = { path = "../../primitives/utils" }
config = { path = "../../../config"}
storage = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.8" }
fin_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.8" }
storage = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.9" }
fin_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.9" }
ledger = { path = "../../../../ledger" }
enterprise-web3 = { path = "../../primitives/enterprise-web3" }
module-ethereum = { path = "../ethereum" }
Expand Down
4 changes: 2 additions & 2 deletions src/components/contracts/primitives/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ parking_lot = "0.12"
primitive-types = { version = "0.11.1", default-features = false, features = ["rlp", "byteorder", "serde"] }
ruc = "1.0"
serde = { version = "1.0.124", features = ["derive"] }
storage = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.8", optional = true }
fin_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.8", optional = true }
storage = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.9", optional = true }
fin_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.9", optional = true }
serde_with = { version = "1.9.4"}

# primitives
Expand Down
4 changes: 2 additions & 2 deletions src/components/contracts/primitives/storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ ruc = "1.0"
serde = { version = "1.0.124", features = ["derive"] }
serde_json = "1.0"
sha2 = "0.9.5"
storage = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.8" }
storage = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.9" }

# primitives
fp-core = { path = "../core" }
config = { path = "../../../config"}

[dev-dependencies]
temp_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.8" }
temp_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.9" }
3 changes: 3 additions & 0 deletions src/components/contracts/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ tokio = { version = "1.10.1", features = ["full"] }
lru = "0.7"
num_cpus = "1.13"

storage = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.9" }
fin_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.9" }

# modules
module-ethereum = { path = "../modules/ethereum"}
module-evm = { path = "../modules/evm"}
Expand Down
74 changes: 72 additions & 2 deletions src/components/contracts/rpc/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use ethereum::{
};
use ethereum_types::{BigEndianHash, Bloom, H160, H256, H512, H64, U256, U64};
use evm::{ExitError, ExitReason};
use fin_db::{FinDB, RocksDB};
use fp_evm::{BlockId, Runner, TransactionStatus};
use fp_rpc_core::types::{
Block, BlockNumber, BlockTransactions, Bytes, CallRequest, Filter, FilteredParams,
Expand All @@ -33,11 +34,15 @@ use lazy_static::lazy_static;
use parking_lot::RwLock;
use sha3::{Digest, Keccak256};
use std::{collections::BTreeMap, convert::Into, ops::Range, sync::Arc};
use storage::state::{ChainState, State};
use tendermint::abci::Code;
use tendermint_rpc::{Client, HttpClient};
use tokio::runtime::{Handle, Runtime};
use tracing::{debug, warn};

const CHAIN_STATE_PATH: &str = "state.db";
const CHAIN_HISTORY_DATA_PATH: &str = "history.db";

lazy_static! {
static ref RT: Runtime =
Runtime::new().expect("Failed to create thread pool executor");
Expand Down Expand Up @@ -358,7 +363,36 @@ impl EthApi for EthApiImpl {
.read()
.create_context_at(block.header.number.as_u64())
.ok_or_else(|| internal_err("failed to create context"))?;

{
let (basedir, opts) = {
let app = account_base_app.read();
(app.basedir.clone(), app.opts.clone())
};

// Creates a fresh chain state db and history db
let fdb_path = basedir.join(CHAIN_STATE_PATH);
let fdb = FinDB::open_read_only(fdb_path.as_path())
.map_err(|e| internal_err(format!("failed to get block: {}", e)))?;

ctx.state = Arc::new(RwLock::new(State::new(
Arc::new(RwLock::new(ChainState::create_with_opts(fdb, opts, true))),
true,
)));

let rdb_path = basedir.join(CHAIN_HISTORY_DATA_PATH);
let rdb = RocksDB::open_read_only(rdb_path.as_path())
.map_err(|e| internal_err(format!("failed to get block: {}", e)))?;

ctx.db = Arc::new(RwLock::new(State::new(
Arc::new(RwLock::new(ChainState::new(
rdb,
"rocks_db".to_owned(),
0,
true,
))),
false,
)));
}
ctx.header
.mut_time()
.set_seconds(block.header.timestamp as i64);
Expand Down Expand Up @@ -852,13 +886,49 @@ impl EthApi for EthApiImpl {
let execute_call_or_create = move |request: CallRequest,
gas_limit|
-> Result<ExecuteResult> {
let ctx = account_base_app
let mut ctx = account_base_app
.read()
.create_query_context(if pending { None } else { Some(0) }, false)
.map_err(|err| {
internal_err(format!("create query context error: {err:?}"))
})?;
{
let (basedir, opts) = {
let app = account_base_app.read();
(app.basedir.clone(), app.opts.clone())
};

// Creates a fresh chain state db and history db
let fdb_path = basedir.join(CHAIN_STATE_PATH);
let fdb =
FinDB::open_read_only(fdb_path.as_path()).map_err(|e| {
internal_err(format!("failed to get block: {}", e))
})?;

ctx.state = Arc::new(RwLock::new(State::new(
Arc::new(RwLock::new(ChainState::create_with_opts(
fdb, opts, true,
))),
true,
)));

let rdb_path = basedir.join(CHAIN_HISTORY_DATA_PATH);

let rdb =
RocksDB::open_read_only(rdb_path.as_path()).map_err(|e| {
internal_err(format!("failed to get block: {}", e))
})?;

ctx.db = Arc::new(RwLock::new(State::new(
Arc::new(RwLock::new(ChainState::new(
rdb,
"rocks_db".to_owned(),
0,
true,
))),
false,
)));
}
let CallRequest {
from,
to,
Expand Down
11 changes: 8 additions & 3 deletions src/components/finutils/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,19 +323,24 @@ pub fn show(basic: bool) -> Result<()> {
(i, addr)
})?;

let validator_address = H160::from_str(&addr).c(d!())?;
let evm_staking_address = get_evm_staking_address()?;
let url = format!("{}:8545", get_serv_addr()?);
let address = utils::get_trigger_on_contract_address(&url, evm_staking_address)?;
let (bound_amount, unbound_amount) = utils::get_evm_delegation_info(
&url,
address,
H160::from_str(&addr).c(d!())?,
validator_address,
mapping_address(kp.get_pk_ref()),
)?;

let address = utils::get_claim_on_contract_address(&url, evm_staking_address)?;
let reward =
utils::get_reward_info(&url, address, mapping_address(kp.get_pk_ref()))?;
let reward = utils::get_reward_info(
&url,
address,
validator_address,
mapping_address(kp.get_pk_ref()),
)?;

println!(
"\x1b[31;01mYour Delegation:\x1b[00m\nbound_amount:{:?}\nunbound_amount:{:?}\nreward:{:?}",
Expand Down
Loading

0 comments on commit b8e88ae

Please sign in to comment.