Skip to content

Commit

Permalink
add eth api cache (#1017)
Browse files Browse the repository at this point in the history
Co-authored-by: shaorongqiang <shaorongqiang@email.com>
  • Loading branch information
shaorongqiang and shaorongqiang authored Oct 10, 2023
1 parent 300d1cc commit 50047d7
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/components/abciapp/src/abci/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub fn run() -> Result<()> {
let app = server::ABCISubmissionServer::new(
basedir,
format!("{}:{}", config.tendermint_host, config.tendermint_port),
CFG.enable_eth_api_service,
)?;

if CFG.enable_query_service {
Expand Down Expand Up @@ -87,7 +88,7 @@ pub fn run() -> Result<()> {
}

if CFG.enable_eth_api_service {
let base_app = app.account_base_app.clone();
let base_app = app.eth_api_base_app.clone().c(d!())?;
let evm_http = format!("{}:{}", config.abci_host, config.evm_http_port);
let evm_ws = format!("{}:{}", config.abci_host, config.evm_ws_port);
let tendermint_rpc = format!(
Expand Down
3 changes: 3 additions & 0 deletions src/components/abciapp/src/abci/server/callback/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,9 @@ pub fn commit(s: &mut ABCISubmissionServer, req: &RequestCommit) -> ResponseComm
}

IN_SAFE_ITV.store(false, Ordering::Release);
if let Some(eth_api_base_app) = &s.eth_api_base_app {
pnk!(eth_api_base_app.write().secondary_catch_up_primary());
}

if CFG.enable_enterprise_web3 && td_height as u64 > *WEB3_SERVICE_START_HEIGHT {
let height = td_height as u32;
Expand Down
27 changes: 27 additions & 0 deletions src/components/abciapp/src/abci/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ pub mod tx_sender;
pub struct ABCISubmissionServer {
pub la: Arc<RwLock<SubmissionServer<ChaChaRng, TendermintForward>>>,
pub account_base_app: Arc<RwLock<AccountBaseAPP>>,
pub eth_api_base_app: Option<Arc<RwLock<AccountBaseAPP>>>,
}

impl ABCISubmissionServer {
/// create ABCISubmissionServer
pub fn new(
basedir: Option<&str>,
tendermint_reply: String,
enable_eth_api_service: bool,
) -> Result<ABCISubmissionServer> {
let ledger_state = match basedir {
None => LedgerState::tmp_ledger(),
Expand Down Expand Up @@ -71,6 +73,30 @@ impl ABCISubmissionServer {
}
};
let account_base_app = Arc::new(RwLock::new(account_base_app));
let eth_api_base_app = if enable_eth_api_service {
let eth_api_base_app = Arc::new(RwLock::new(match basedir {
None => {
pnk!(AccountBaseAPP::new_with_secondary(
tempfile::tempdir().unwrap().path(),
CFG.disable_eth_empty_blocks,
CFG.arc_history,
CFG.arc_fresh
))
}
Some(basedir) => {
pnk!(AccountBaseAPP::new_with_secondary(
Path::new(basedir),
CFG.disable_eth_empty_blocks,
CFG.arc_history,
CFG.arc_fresh
))
}
}));
Some(eth_api_base_app)
} else {
None
};

if EVM_STAKING.set(account_base_app.clone()).is_err() {
return Err(eg!("Invalid usage."));
}
Expand All @@ -85,6 +111,7 @@ impl ABCISubmissionServer {
.c(d!())?,
)),
account_base_app,
eth_api_base_app,
})
}
}
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.5" }
fin_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.5" }
storage = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.7" }
fin_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.7" }
sha3 = "0.8"
zei = { git = "https://github.com/FindoraNetwork/zei", branch = "stable-main" }

Expand Down
82 changes: 79 additions & 3 deletions src/components/contracts/baseapp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ lazy_static! {
const APP_NAME: &str = "findora";
const CHAIN_STATE_PATH: &str = "state.db";
const CHAIN_HISTORY_DATA_PATH: &str = "history.db";
const CHAIN_STATE_SECONDARY_PATH: &str = "state_secondary.db";
const CHAIN_HISTORY_SECONDARY_DATA_PATH: &str = "history_secondary.db";
const BLOCKS_IN_DAY: u64 = 4 * 60 * 24;
const SNAPSHOT_INTERVAL: u64 = 10 * 24;

Expand Down Expand Up @@ -205,12 +207,17 @@ impl BaseApp {
BLOCKS_IN_DAY * v as u64
}),
};
let chain_state = Arc::new(RwLock::new(ChainState::create_with_opts(fdb, opts)));
let chain_state =
Arc::new(RwLock::new(ChainState::create_with_opts(fdb, opts, false)));

let rdb_path = basedir.join(CHAIN_HISTORY_DATA_PATH);
let rdb = RocksDB::open(rdb_path.as_path())?;
let chain_db =
Arc::new(RwLock::new(ChainState::new(rdb, "rocks_db".to_owned(), 0)));
let chain_db = Arc::new(RwLock::new(ChainState::new(
rdb,
"rocks_db".to_owned(),
0,
false,
)));

//Migrate any existing data from one database to the other.
BaseApp::migrate_initial_db(chain_state.clone(), chain_db.clone())?;
Expand All @@ -230,7 +237,76 @@ impl BaseApp {
event_notify: Arc::new(Notifications::new()),
})
}
pub fn new_with_secondary(
basedir: &Path,
empty_block: bool,
arc_history: (u16, Option<u16>),
is_fresh: bool,
) -> Result<Self> {
info!(
target: "baseapp",
"create new baseapp with basedir {:?}, empty_block {}, trace history {:?} days, is_fresh {}",
basedir, empty_block, arc_history, is_fresh
);

// Creates a fresh chain state db and history db
let fdb_path = basedir.join(CHAIN_STATE_PATH);
let fdb_secondary_path = basedir.join(CHAIN_STATE_SECONDARY_PATH);
let fdb =
FinDB::open_as_secondary(fdb_path.as_path(), fdb_secondary_path.as_path())?;

let opts = ChainStateOpts {
name: Some("findora_db".to_owned()),
ver_window: BLOCKS_IN_DAY * arc_history.0 as u64,
cleanup_aux: is_fresh,
interval: arc_history
.1
.map_or(SNAPSHOT_INTERVAL * arc_history.0 as u64, |v| {
BLOCKS_IN_DAY * v as u64
}),
};
let chain_state =
Arc::new(RwLock::new(ChainState::create_with_opts(fdb, opts, true)));

let rdb_path = basedir.join(CHAIN_HISTORY_DATA_PATH);
let rdb_secondary_path = basedir.join(CHAIN_HISTORY_SECONDARY_DATA_PATH);
let rdb = RocksDB::open_as_secondary(
rdb_path.as_path(),
rdb_secondary_path.as_path(),
)?;

let chain_db = Arc::new(RwLock::new(ChainState::new(
rdb,
"rocks_db".to_owned(),
0,
true,
)));

Ok(BaseApp {
name: APP_NAME.to_string(),
version: "1.0.0".to_string(),
app_version: 1,
chain_state: chain_state.clone(),
chain_db: chain_db.clone(),
check_state: Context::new(chain_state.clone(), chain_db.clone()),
deliver_state: Context::new(chain_state, chain_db),
modules: ModuleManager {
ethereum_module: module_ethereum::App::<Self>::new(empty_block),
..Default::default()
},
event_notify: Arc::new(Notifications::new()),
})
}
pub fn secondary_catch_up_primary(&self) -> Result<()> {
self.chain_state
.read()
.secondary_catch_up_primary()
.map_err(|e| eg!("chain_state secondary_catch_up_primary fail:{}", e))?;
self.chain_db
.read()
.secondary_catch_up_primary()
.map_err(|e| eg!("chain_db secondary_catch_up_primary fail:{}", e))
}
pub fn derive_app(&self) -> Self {
let chain_state = self.chain_state.clone();
let chain_db = self.chain_db.clone();
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.5" }
storage = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.7" }

# 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.5" }
fin_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.7" }
2 changes: 2 additions & 0 deletions src/components/contracts/modules/account/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fn setup() -> Context {
fdb,
"temp_db".to_string(),
100,
false,
)));

let mut rocks_path = temp_dir();
Expand All @@ -36,6 +37,7 @@ fn setup() -> Context {
rdb,
"temp_rocks_db".to_string(),
0,
false,
)));

Context::new(chain_state, chain_db)
Expand Down
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.5" }
fin_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.5" }
storage = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.7" }
fin_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.7" }

[features]
default = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fn setup() -> Context {
fdb,
"temp_db".to_string(),
100,
false,
)));

let mut rocks_path = temp_dir();
Expand All @@ -33,6 +34,7 @@ fn setup() -> Context {
rdb,
"temp_rocks_db".to_string(),
0,
false,
)));

Context::new(chain_state, chain_db)
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.5" }
fin_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.5" }
storage = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.7" }
fin_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.7" }
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.5", optional = true }
fin_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.5", optional = true }
storage = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.7", optional = true }
fin_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.7", 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.5" }
storage = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.7" }

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

[dev-dependencies]
temp_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.5" }
temp_db = { git = "https://github.com/FindoraNetwork/storage.git", tag = "v1.1.7" }
1 change: 1 addition & 0 deletions src/components/contracts/primitives/storage/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fn setup_temp_db() -> Arc<RwLock<State<TempFinDB>>> {
fdb,
"temp_db".to_string(),
100,
false,
)));
Arc::new(RwLock::new(State::new(chain_state, true)))
}
Expand Down

0 comments on commit 50047d7

Please sign in to comment.