mirror of
https://github.com/serai-dex/serai.git
synced 2025-01-18 16:55:16 +00:00
Clarify Arc RwLocks and sleeps in coordinator
This commit is contained in:
parent
ad5522d854
commit
72633d6421
3 changed files with 29 additions and 16 deletions
|
@ -39,9 +39,7 @@ pub mod tests;
|
||||||
|
|
||||||
// This is a static to satisfy lifetime expectations
|
// This is a static to satisfy lifetime expectations
|
||||||
lazy_static::lazy_static! {
|
lazy_static::lazy_static! {
|
||||||
static ref NEW_TRIBUTARIES: Arc<RwLock<VecDeque<TributarySpec>>> = Arc::new(
|
static ref NEW_TRIBUTARIES: RwLock<VecDeque<TributarySpec>> = RwLock::new(VecDeque::new());
|
||||||
RwLock::new(VecDeque::new())
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn run<D: Db, Pro: Processor, P: P2p>(
|
async fn run<D: Db, Pro: Processor, P: P2p>(
|
||||||
|
@ -79,6 +77,8 @@ async fn run<D: Db, Pro: Processor, P: P2p>(
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
|
// TODO: Should this use a notification system for new blocks?
|
||||||
|
// Right now it's sleeping for half the block time.
|
||||||
Ok(()) => sleep(Duration::from_secs(3)).await,
|
Ok(()) => sleep(Duration::from_secs(3)).await,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("couldn't communicate with serai node: {e}");
|
log::error!("couldn't communicate with serai node: {e}");
|
||||||
|
@ -93,8 +93,11 @@ async fn run<D: Db, Pro: Processor, P: P2p>(
|
||||||
{
|
{
|
||||||
struct ActiveTributary<D: Db, P: P2p> {
|
struct ActiveTributary<D: Db, P: P2p> {
|
||||||
spec: TributarySpec,
|
spec: TributarySpec,
|
||||||
tributary: Tributary<D, Transaction, P>,
|
tributary: Arc<RwLock<Tributary<D, Transaction, P>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Arc so this can be shared between the Tributary scanner task and the P2P task
|
||||||
|
// Write locks on this may take a while to acquire
|
||||||
let tributaries = Arc::new(RwLock::new(HashMap::<[u8; 32], ActiveTributary<D, P>>::new()));
|
let tributaries = Arc::new(RwLock::new(HashMap::<[u8; 32], ActiveTributary<D, P>>::new()));
|
||||||
|
|
||||||
async fn add_tributary<D: Db, P: P2p>(
|
async fn add_tributary<D: Db, P: P2p>(
|
||||||
|
@ -116,7 +119,10 @@ async fn run<D: Db, Pro: Processor, P: P2p>(
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
tributaries.insert(tributary.genesis(), ActiveTributary { spec, tributary });
|
tributaries.insert(
|
||||||
|
tributary.genesis(),
|
||||||
|
ActiveTributary { spec, tributary: Arc::new(RwLock::new(tributary)) },
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reload active tributaries from the database
|
// Reload active tributaries from the database
|
||||||
|
@ -140,10 +146,9 @@ async fn run<D: Db, Pro: Processor, P: P2p>(
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
loop {
|
loop {
|
||||||
// The following handle_new_blocks function may take an arbitrary amount of time
|
// The following handle_new_blocks function may take an arbitrary amount of time
|
||||||
// If registering a new tributary waited for a lock on the tributaries table, the
|
// Accordingly, it may take a long time to acquire a write lock on the tributaries table
|
||||||
// substrate scanner may wait on a lock for an arbitrary amount of time
|
// By definition of NEW_TRIBUTARIES, we allow tributaries to be added almost immediately,
|
||||||
// By instead using the distinct NEW_TRIBUTARIES, there should be minimal
|
// meaning the Substrate scanner won't become blocked on this
|
||||||
// competition/blocking
|
|
||||||
{
|
{
|
||||||
let mut new_tributaries = NEW_TRIBUTARIES.write().await;
|
let mut new_tributaries = NEW_TRIBUTARIES.write().await;
|
||||||
while let Some(spec) = new_tributaries.pop_front() {
|
while let Some(spec) = new_tributaries.pop_front() {
|
||||||
|
@ -159,20 +164,22 @@ async fn run<D: Db, Pro: Processor, P: P2p>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unknown-length read acquisition. This would risk screwing over the P2P process EXCEPT
|
// TODO: Instead of holding this lock long term, should this take in Arc RwLock and
|
||||||
// they both use read locks. Accordingly, they can co-exist
|
// re-acquire read locks?
|
||||||
for ActiveTributary { spec, tributary } in tributaries.read().await.values() {
|
for ActiveTributary { spec, tributary } in tributaries.read().await.values() {
|
||||||
tributary::scanner::handle_new_blocks::<_, _, P>(
|
tributary::scanner::handle_new_blocks::<_, _, P>(
|
||||||
&mut tributary_db,
|
&mut tributary_db,
|
||||||
&key,
|
&key,
|
||||||
&mut processor,
|
&mut processor,
|
||||||
spec,
|
spec,
|
||||||
tributary,
|
&*tributary.read().await,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
sleep(Duration::from_secs(3)).await;
|
// Sleep for half the block time
|
||||||
|
// TODO: Should we define a notification system for when a new block occurs?
|
||||||
|
sleep(Duration::from_secs(Tributary::<D, Transaction, P>::block_time() / 2)).await;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -190,7 +197,10 @@ async fn run<D: Db, Pro: Processor, P: P2p>(
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
if tributary.tributary.handle_message(&msg.msg).await {
|
// This is misleading being read, as it will mutate the Tributary, yet there's
|
||||||
|
// greater efficiency when it is read
|
||||||
|
// The safety of it is also justified by Tributary::handle_message's documentation
|
||||||
|
if tributary.tributary.read().await.handle_message(&msg.msg).await {
|
||||||
P2p::broadcast(&p2p, msg.kind, msg.msg).await;
|
P2p::broadcast(&p2p, msg.kind, msg.msg).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
# Tributary
|
# Tributary
|
||||||
|
|
||||||
A micro-blockchain to provide consensus and ordering to P2P communication.
|
A verifiable, ordered broadcast layer implemented as a BFT micro-blockchain.
|
||||||
|
|
|
@ -240,6 +240,9 @@ impl<D: Db, T: Transaction, P: P2p> Network for TendermintNetwork<D, T, P> {
|
||||||
type Weights = Arc<Validators>;
|
type Weights = Arc<Validators>;
|
||||||
type Block = TendermintBlock;
|
type Block = TendermintBlock;
|
||||||
|
|
||||||
|
// These are in seconds and create a six-second block time.
|
||||||
|
// The block time is the latency on message delivery (where a message is some piece of data
|
||||||
|
// embedded in a transaction), hence why it should be kept low.
|
||||||
const BLOCK_PROCESSING_TIME: u32 = 3;
|
const BLOCK_PROCESSING_TIME: u32 = 3;
|
||||||
const LATENCY_TIME: u32 = 1;
|
const LATENCY_TIME: u32 = 1;
|
||||||
|
|
||||||
|
@ -307,7 +310,7 @@ impl<D: Db, T: Transaction, P: P2p> Network for TendermintNetwork<D, T, P> {
|
||||||
hex::encode(hash),
|
hex::encode(hash),
|
||||||
hex::encode(self.genesis)
|
hex::encode(self.genesis)
|
||||||
);
|
);
|
||||||
sleep(Duration::from_secs(30)).await;
|
sleep(Duration::from_secs(Tendermint::<D, T, P>::block_time())).await;
|
||||||
}
|
}
|
||||||
_ => return invalid_block(),
|
_ => return invalid_block(),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue