Skip to content

Commit 8031d9b

Browse files
committed
refactor: make the Chunkable trait generic
1 parent a08e93c commit 8031d9b

File tree

8 files changed

+29
-31
lines changed

8 files changed

+29
-31
lines changed

rs/interfaces/src/p2p/state_sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub trait StateSyncClient: Send + Sync {
1515
fn start_state_sync(
1616
&self,
1717
id: &StateSyncArtifactId,
18-
) -> Option<Box<dyn Chunkable + Send + Sync>>;
18+
) -> Option<Box<dyn Chunkable<StateSyncMessage> + Send + Sync>>;
1919
/// Returns true if a state sync with the specified Id can be cancelled because a newer state is available.
2020
/// The result of this function is only meaningful the Id refers to a active state sync started with `start_state_sync`.
2121
/// TODO: (NET-1469) In the future this API should be made safer by only allowing the id of a previously initiated state sync.

rs/p2p/state_sync_manager/src/ongoing.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ struct OngoingStateSync {
7070
downloading_chunks: JoinMap<ChunkId, DownloadResult>,
7171
// State sync
7272
state_sync: Arc<dyn StateSyncClient>,
73-
tracker: Arc<Mutex<Box<dyn Chunkable + Send + Sync>>>,
73+
tracker: Arc<Mutex<Box<dyn Chunkable<StateSyncMessage> + Send + Sync>>>,
7474
state_sync_finished: bool,
7575
}
7676

@@ -88,7 +88,7 @@ pub(crate) fn start_ongoing_state_sync(
8888
log: ReplicaLogger,
8989
rt: &Handle,
9090
metrics: OngoingStateSyncMetrics,
91-
tracker: Arc<Mutex<Box<dyn Chunkable + Send + Sync>>>,
91+
tracker: Arc<Mutex<Box<dyn Chunkable<StateSyncMessage> + Send + Sync>>>,
9292
artifact_id: StateSyncArtifactId,
9393
state_sync: Arc<dyn StateSyncClient>,
9494
transport: Arc<dyn Transport>,
@@ -296,7 +296,7 @@ impl OngoingStateSync {
296296
async fn download_chunk_task(
297297
peer_id: NodeId,
298298
client: Arc<dyn Transport>,
299-
tracker: Arc<Mutex<Box<dyn Chunkable + Send + Sync>>>,
299+
tracker: Arc<Mutex<Box<dyn Chunkable<StateSyncMessage> + Send + Sync>>>,
300300
artifact_id: StateSyncArtifactId,
301301
chunk_id: ChunkId,
302302
metrics: OngoingStateSyncMetrics,
@@ -409,7 +409,7 @@ mod tests {
409409
.body(compress_empty_bytes())
410410
.unwrap())
411411
});
412-
let mut c = MockChunkable::default();
412+
let mut c = MockChunkable::<StateSyncMessage>::default();
413413
c.expect_chunks_to_download()
414414
.returning(|| Box::new(std::iter::once(ChunkId::from(1))));
415415

@@ -448,7 +448,7 @@ mod tests {
448448
.body(compress_empty_bytes())
449449
.unwrap())
450450
});
451-
let mut c = MockChunkable::default();
451+
let mut c = MockChunkable::<StateSyncMessage>::default();
452452
c.expect_chunks_to_download()
453453
.returning(|| Box::new(std::iter::once(ChunkId::from(1))));
454454
c.expect_add_chunk()
@@ -494,7 +494,7 @@ mod tests {
494494
.body(compress_empty_bytes())
495495
.unwrap())
496496
});
497-
let mut c = MockChunkable::default();
497+
let mut c = MockChunkable::<StateSyncMessage>::default();
498498
// Endless iterator
499499
c.expect_chunks_to_download()
500500
.returning(|| Box::new(std::iter::once(ChunkId::from(1))));

rs/p2p/state_sync_manager/tests/common.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ impl StateSyncClient for FakeStateSync {
197197
fn start_state_sync(
198198
&self,
199199
id: &StateSyncArtifactId,
200-
) -> Option<Box<dyn Chunkable + Send + Sync>> {
200+
) -> Option<Box<dyn Chunkable<StateSyncMessage> + Send + Sync>> {
201201
if !self.uses_global() && id.height > self.current_height() && !self.disconnected() {
202202
return Some(Box::new(FakeChunkable::new(
203203
self.local_state.clone(),
@@ -263,7 +263,7 @@ impl FakeChunkable {
263263
}
264264
}
265265

266-
impl Chunkable for FakeChunkable {
266+
impl Chunkable<StateSyncMessage> for FakeChunkable {
267267
/// Returns iterator for chunks to download.
268268
/// Tries to first download metamanifest, then manifest and then chunks. Does not advance if previous didn't complete.
269269
fn chunks_to_download(&self) -> Box<dyn Iterator<Item = ChunkId>> {
@@ -311,7 +311,7 @@ impl Chunkable for FakeChunkable {
311311

312312
#[derive(Clone, Default)]
313313
pub struct SharableMockChunkable {
314-
mock: Arc<Mutex<MockChunkable>>,
314+
mock: Arc<Mutex<MockChunkable<StateSyncMessage>>>,
315315
chunks_to_download_calls: Arc<AtomicUsize>,
316316
add_chunks_calls: Arc<AtomicUsize>,
317317
}
@@ -323,7 +323,7 @@ impl SharableMockChunkable {
323323
..Default::default()
324324
}
325325
}
326-
pub fn get_mut(&self) -> MutexGuard<'_, MockChunkable> {
326+
pub fn get_mut(&self) -> MutexGuard<'_, MockChunkable<StateSyncMessage>> {
327327
self.mock.lock().unwrap()
328328
}
329329
pub fn add_chunks_calls(&self) -> usize {
@@ -335,7 +335,7 @@ impl SharableMockChunkable {
335335
}
336336
}
337337

338-
impl Chunkable for SharableMockChunkable {
338+
impl Chunkable<StateSyncMessage> for SharableMockChunkable {
339339
fn chunks_to_download(&self) -> Box<dyn Iterator<Item = ChunkId>> {
340340
self.chunks_to_download_calls.fetch_add(1, Ordering::SeqCst);
341341
self.mock.lock().unwrap().chunks_to_download()
@@ -390,7 +390,7 @@ impl StateSyncClient for SharableMockStateSync {
390390
fn start_state_sync(
391391
&self,
392392
id: &StateSyncArtifactId,
393-
) -> Option<Box<dyn Chunkable + Send + Sync>> {
393+
) -> Option<Box<dyn Chunkable<StateSyncMessage> + Send + Sync>> {
394394
self.start_state_sync_calls.fetch_add(1, Ordering::SeqCst);
395395
self.mock.lock().unwrap().start_state_sync(id)
396396
}

rs/p2p/test_utils/src/mocks.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ mock! {
2626
fn start_state_sync(
2727
&self,
2828
id: &StateSyncArtifactId,
29-
) -> Option<Box<dyn Chunkable + Send + Sync>>;
29+
) -> Option<Box<dyn Chunkable<StateSyncMessage> + Send + Sync>>;
3030

3131
fn should_cancel(&self, id: &StateSyncArtifactId) -> bool;
3232

@@ -58,11 +58,11 @@ mock! {
5858
}
5959

6060
mock! {
61-
pub Chunkable {}
61+
pub Chunkable<T> {}
6262

63-
impl Chunkable for Chunkable{
63+
impl<T> Chunkable<T> for Chunkable<T> {
6464
fn chunks_to_download(&self) -> Box<dyn Iterator<Item = ChunkId>>;
65-
fn add_chunk(&mut self, chunk_id: ChunkId, chunk: Chunk) -> Result<StateSyncMessage, ArtifactErrorCode>;
65+
fn add_chunk(&mut self, chunk_id: ChunkId, chunk: Chunk) -> Result<T, ArtifactErrorCode>;
6666
}
6767
}
6868

rs/state_manager/src/state_sync.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl StateSync {
3434
pub fn create_chunkable_state(
3535
&self,
3636
id: &StateSyncArtifactId,
37-
) -> Box<dyn Chunkable + Send + Sync> {
37+
) -> Box<dyn Chunkable<StateSyncMessage> + Send + Sync> {
3838
info!(self.log, "Starting state sync @{}", id.height);
3939

4040
Box::new(crate::state_sync::chunkable::IncompleteState::new(
@@ -239,7 +239,7 @@ impl StateSyncClient for StateSync {
239239
fn start_state_sync(
240240
&self,
241241
id: &StateSyncArtifactId,
242-
) -> Option<Box<dyn Chunkable + Send + Sync>> {
242+
) -> Option<Box<dyn Chunkable<StateSyncMessage> + Send + Sync>> {
243243
if self.get_priority_function()(id, &()) != Priority::Fetch {
244244
return None;
245245
}

rs/state_manager/src/state_sync/chunkable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ impl IncompleteState {
11251125
}
11261126
}
11271127

1128-
impl Chunkable for IncompleteState {
1128+
impl Chunkable<StateSyncMessage> for IncompleteState {
11291129
fn chunks_to_download(&self) -> Box<dyn Iterator<Item = ChunkId>> {
11301130
match self.state {
11311131
DownloadState::Blank => Box::new(std::iter::once(META_MANIFEST_CHUNK)),

rs/state_manager/tests/common/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,10 @@ pub enum StateSyncErrorCode {
390390
OtherChunkVerificationFailed,
391391
}
392392

393-
pub fn pipe_state_sync(src: StateSyncMessage, mut dst: Box<dyn Chunkable>) -> StateSyncMessage {
393+
pub fn pipe_state_sync(
394+
src: StateSyncMessage,
395+
mut dst: Box<dyn Chunkable<StateSyncMessage>>,
396+
) -> StateSyncMessage {
394397
pipe_partial_state_sync(&src, &mut *dst, &Default::default(), false)
395398
.expect("State sync not completed.")
396399
}
@@ -414,7 +417,7 @@ fn alter_chunk_data(chunk: &mut Chunk) {
414417
/// Alter the chunk data if `use_bad_chunk` is set to true.
415418
pub fn pipe_meta_manifest(
416419
src: &StateSyncMessage,
417-
dst: &mut dyn Chunkable,
420+
dst: &mut dyn Chunkable<StateSyncMessage>,
418421
use_bad_chunk: bool,
419422
) -> Result<StateSyncMessage, StateSyncErrorCode> {
420423
let ids: Vec<_> = dst.chunks_to_download().collect();
@@ -445,7 +448,7 @@ pub fn pipe_meta_manifest(
445448
/// Alter the data of the chunk in the middle position if `use_bad_chunk` is set to true.
446449
pub fn pipe_manifest(
447450
src: &StateSyncMessage,
448-
dst: &mut dyn Chunkable,
451+
dst: &mut dyn Chunkable<StateSyncMessage>,
449452
use_bad_chunk: bool,
450453
) -> Result<StateSyncMessage, StateSyncErrorCode> {
451454
let ids: Vec<_> = dst.chunks_to_download().collect();
@@ -484,7 +487,7 @@ pub fn pipe_manifest(
484487
/// Alter the data of the chunk in the middle position if `use_bad_chunk` is set to true.
485488
pub fn pipe_partial_state_sync(
486489
src: &StateSyncMessage,
487-
dst: &mut dyn Chunkable,
490+
dst: &mut dyn Chunkable<StateSyncMessage>,
488491
omit: &HashSet<ChunkId>,
489492
use_bad_chunk: bool,
490493
) -> Result<StateSyncMessage, StateSyncErrorCode> {

rs/types/types/src/chunkable.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
//!
1212
//! For more context please check `<https://youtu.be/WaNJINjGleg>`
1313
//!
14-
use crate::state_sync::StateSyncMessage;
1514
use phantom_newtype::Id;
1615

1716
pub type Chunk = Vec<u8>;
@@ -27,11 +26,7 @@ pub enum ArtifactErrorCode {
2726
pub struct ChunkIdTag;
2827
pub type ChunkId = Id<ChunkIdTag, u32>;
2928

30-
pub trait Chunkable {
29+
pub trait Chunkable<T> {
3130
fn chunks_to_download(&self) -> Box<dyn Iterator<Item = ChunkId>>;
32-
fn add_chunk(
33-
&mut self,
34-
chunk_id: ChunkId,
35-
chunk: Chunk,
36-
) -> Result<StateSyncMessage, ArtifactErrorCode>;
31+
fn add_chunk(&mut self, chunk_id: ChunkId, chunk: Chunk) -> Result<T, ArtifactErrorCode>;
3732
}

0 commit comments

Comments
 (0)