Skip to content

Commit

Permalink
feat(rpc): gw_get_mem_pool_state_ready (godwokenrises#644)
Browse files Browse the repository at this point in the history
* feat(rpc): gw_get_mem_pool_state_ready

Query whether a node is ready to serve

* refactor: rename to completed_initial_syncing
  • Loading branch information
zeroqn committed Apr 21, 2022
1 parent c33e49f commit 1c072df
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
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 @@ -256,7 +256,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 @@ -177,7 +177,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 @@ -204,9 +204,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 @@ -310,6 +311,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 @@ -1471,6 +1473,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

0 comments on commit 1c072df

Please sign in to comment.