Skip to content

Commit e2b25c2

Browse files
committed
nits
Signed-off-by: Sam Batschelet <[email protected]>
1 parent ab72912 commit e2b25c2

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

mini-kvvm/src/block/builder.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
use std::{
22
sync::{
33
atomic::{AtomicBool, Ordering},
4-
Arc, Mutex,
4+
Arc,
55
},
66
thread,
77
time::{Duration, Instant},
88
};
99

1010
use avalanche_types::rpcchainvm;
1111
use chan::chan_select;
12-
use tokio::sync::{RwLock};
1312
use crossbeam_channel::TryRecvError;
14-
13+
use tokio::sync::RwLock;
1514

1615
use crate::vm;
1716

17+
// TODO: make configurable
18+
const GOSSIP_INTERVAL: Duration = Duration::from_secs(1);
19+
const REGOSSIP_INTERVAL: Duration = Duration::from_secs(30);
20+
1821
pub trait Builder {
1922
fn build(&self);
2023
fn gossip(&self);
@@ -28,7 +31,7 @@ pub struct Timed {
2831
/// [DontBuild] indicates there's no need to build a block.
2932
/// [MayBuild] indicates the Vm should proceed to build a block.
3033
/// [Building] indicates the Vm has sent a request to the engine to build a block.
31-
pub status: Arc<Mutex<Status>>,
34+
pub status: Arc<RwLock<Status>>,
3235

3336
pub build_block_timer: Timer,
3437

@@ -54,10 +57,10 @@ pub enum Status {
5457
}
5558

5659
pub struct Timer {
57-
// Timeout Tx channel is used to reset ticker threads.
60+
/// Timeout Tx channel is used to reset ticker threads.
5861
timeout_tx: crossbeam_channel::Sender<()>,
5962

60-
// Timeout Rx channel listens.
63+
/// Timeout Rx channel listens.
6164
timeout_rx: crossbeam_channel::Receiver<()>,
6265

6366
/// New timer creation stops when true.
@@ -70,14 +73,14 @@ pub struct Timer {
7073
duration: Arc<RwLock<Duration>>,
7174
}
7275

73-
// Directs the engine when to build blocks and gossip transactions.
76+
/// Directs the engine when to build blocks and gossip transactions.
7477
impl Timed {
7578
/// Sets the initial timeout on the two stage timer if the process
7679
/// has not already begun from an earlier notification. If [buildStatus] is anything
7780
/// other than [DontBuild], then the attempt has already begun and this notification
7881
/// can be safely skipped.
7982
async fn signal_txs_ready(&mut self) {
80-
if *self.status.lock().unwrap() == Status::DontBuild {
83+
if *self.status.read().await == Status::DontBuild {
8184
return;
8285
}
8386

@@ -99,7 +102,7 @@ impl Timed {
99102
// release lock
100103
drop(vm);
101104

102-
let mut status = self.status.lock().unwrap();
105+
let mut status = self.status.write().await;
103106
*status = Status::Building;
104107
return;
105108
}
@@ -108,7 +111,7 @@ impl Timed {
108111
// [HandleGenerateBlock] invocation could lead to quiescence, building a block with
109112
// some delay, or attempting to build another block immediately
110113
pub async fn handle_generate_block(&mut self) {
111-
let mut status = self.status.lock().unwrap();
114+
let mut status = self.status.write().await;
112115

113116
if self.need_to_build().await {
114117
*status = Status::MayBuild;
@@ -118,7 +121,7 @@ impl Timed {
118121
}
119122
}
120123

121-
// needToBuild returns true if there are outstanding transactions to be issued
124+
// Returns true if there are outstanding transactions to be issued
122125
// into a block.
123126
async fn need_to_build(&self) -> bool {
124127
let mempool = self.vm.mempool.read().await;
@@ -128,7 +131,7 @@ impl Timed {
128131
/// Parses the block current status and
129132
pub async fn build_block_parse_status(&mut self) {
130133
let mut mark_building = false;
131-
match &*self.status.lock().unwrap() {
134+
match &*self.status.read().await {
132135
Status::DontBuild => {
133136
// no op
134137
}
@@ -223,14 +226,14 @@ impl Timed {
223226
ticker_duration = *duration;
224227
}
225228
reset.store(true, Ordering::Relaxed);
226-
log::debug!("timeout\n");
229+
log::debug!("timeout");
227230
break
228231
}
229232

230233
// ticker
231234
recv(ticker_rx) -> _ => {
232235
cleared.store(true, Ordering::Relaxed);
233-
log::debug!("tick\n");
236+
log::debug!("tick");
234237
break
235238
}
236239
}
@@ -242,10 +245,12 @@ impl Timed {
242245
/// considered for the next block.
243246
pub async fn build(&mut self) {
244247
log::debug!("starting build loops");
248+
245249
self.signal_txs_ready().await;
246250
let mempool = self.vm.mempool.read().await;
247251
let mempool_pending_ch = mempool.subscribe_pending();
248252
drop(mempool);
253+
249254
let stop_ch = self.stop.clone();
250255
let builder_stop_ch = self.builder_stop.clone();
251256

@@ -274,8 +279,8 @@ impl Timed {
274279
pub async fn gossip(&self) {
275280
log::debug!("starting gossip loops");
276281

277-
let gossip = chan::tick(Duration::from_millis(100));
278-
let regossip = chan::tick(Duration::from_millis(100));
282+
let gossip = chan::tick(GOSSIP_INTERVAL);
283+
let regossip = chan::tick(REGOSSIP_INTERVAL);
279284
let stop_ch = self.stop.clone();
280285

281286
while stop_ch.try_recv() == Err(TryRecvError::Empty) {

0 commit comments

Comments
 (0)