Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rpc): gw_get_mem_pool_state_ready #644

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/benches/benches/benchmarks/smt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl BenchExecutionEnvironment {
let generator = Generator::new(backend_manage, account_lock_manage, rollup_context);

Self::init_genesis(&store, &genesis_config, accounts);
let mem_pool_state = MemPoolState::new(Arc::new(MemStore::new(store.get_snapshot())));
let mem_pool_state = MemPoolState::new(Arc::new(MemStore::new(store.get_snapshot())), true);

BenchExecutionEnvironment {
generator,
Expand Down
4 changes: 3 additions & 1 deletion crates/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ impl Chain {

log::debug!("[complete_initial_syncing] acquire mem-pool",);
let t = Instant::now();
mem_pool.lock().await.notify_new_tip(tip_block_hash).await?;
let mut mem_pool = mem_pool.lock().await;
mem_pool.notify_new_tip(tip_block_hash).await?;
mem_pool.mem_pool_state().set_completed_initial_syncing();
log::debug!(
"[complete_initial_syncing] unlock mem-pool {}ms",
t.elapsed().as_millis()
Expand Down
2 changes: 1 addition & 1 deletion crates/mem-pool/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl MemPool {

let mem_pool_state = {
let mem_store = MemStore::new(store.get_snapshot());
Arc::new(MemPoolState::new(Arc::new(mem_store)))
Arc::new(MemPoolState::new(Arc::new(mem_store), false))
};

let mut mem_pool = MemPool {
Expand Down
14 changes: 11 additions & 3 deletions crates/rpc-server/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,10 @@ impl Registry {
let mem_pool = pool.lock().await;
mem_pool.mem_pool_state()
}
None => Arc::new(MemPoolState::new(Arc::new(MemStore::new(
store.get_snapshot(),
)))),
None => Arc::new(MemPoolState::new(
Arc::new(MemStore::new(store.get_snapshot())),
true,
)),
};
let (submit_tx, submit_rx) = async_channel::bounded(RequestSubmitter::MAX_CHANNEL_SIZE);
if let Some(mem_pool) = mem_pool.as_ref().to_owned() {
Expand Down Expand Up @@ -280,6 +281,7 @@ impl Registry {
)
.with_method("gw_get_fee_config", get_fee_config)
.with_method("gw_get_mem_pool_state_root", get_mem_pool_state_root)
.with_method("gw_get_mem_pool_state_ready", get_mem_pool_state_ready)
.with_method("gw_get_node_info", get_node_info)
.with_method("gw_reload_config", reload_config);

Expand Down Expand Up @@ -1365,6 +1367,12 @@ async fn get_mem_pool_state_root(
Ok(to_jsonh256(root))
}

async fn get_mem_pool_state_ready(
mem_pool_state: Data<Arc<MemPoolState>>,
) -> Result<bool, RpcError> {
Ok(mem_pool_state.completed_initial_syncing())
}

async fn tests_produce_block(
Params((payload,)): Params<(TestModePayload,)>,
tests_rpc_impl: Data<BoxedTestsRPCImpl>,
Expand Down
19 changes: 16 additions & 3 deletions crates/store/src/mem_pool_state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::sync::Arc;
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};

use crate::snapshot::StoreSnapshot;
use arc_swap::{ArcSwap, Guard};
Expand All @@ -11,7 +14,7 @@ use gw_db::{
schema::{Col, COLUMNS, COLUMN_ACCOUNT_SMT_BRANCH, COLUMN_ACCOUNT_SMT_LEAF, COLUMN_META},
};
use gw_types::{
packed::{self},
packed,
prelude::{Entity, FromSliceShouldBeOk, Pack, Reader, Unpack},
};

Expand All @@ -32,12 +35,14 @@ pub const META_MEM_SMT_COUNT_KEY: &[u8] = b"MEM_ACCOUNT_SMT_COUNT_KEY";

pub struct MemPoolState {
store: ArcSwap<MemStore>,
completed_initial_syncing: AtomicBool,
}

impl MemPoolState {
pub fn new(store: Arc<MemStore>) -> Self {
pub fn new(store: Arc<MemStore>, completed_initial_syncing: bool) -> Self {
Self {
store: ArcSwap::new(store),
completed_initial_syncing: AtomicBool::new(completed_initial_syncing),
}
}

Expand All @@ -50,6 +55,14 @@ impl MemPoolState {
pub fn store(&self, mem_store: Arc<MemStore>) {
self.store.store(mem_store);
}

pub fn completed_initial_syncing(&self) -> bool {
self.completed_initial_syncing.load(Ordering::SeqCst)
}

pub fn set_completed_initial_syncing(&self) {
self.completed_initial_syncing.store(true, Ordering::SeqCst);
}
}

enum Value<T> {
Expand Down