Skip to content

Commit e3446fb

Browse files
committed
refactor: remove the ArtifactChunk type
1 parent 43f31c0 commit e3446fb

File tree

8 files changed

+47
-74
lines changed

8 files changed

+47
-74
lines changed

rs/p2p/state_sync_manager/src/ongoing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ impl OngoingStateSync {
329329

330330
let chunk_add_result = tokio::task::spawn_blocking(move || {
331331
let chunk = parse_chunk_handler_response(response, chunk_id, metrics)?;
332-
Ok(tracker.lock().unwrap().add_chunk(chunk))
332+
Ok(tracker.lock().unwrap().add_chunk(chunk_id, chunk))
333333
})
334334
.await
335335
.map_err(|err| DownloadChunkError::RequestError {

rs/p2p/state_sync_manager/src/routes/chunk.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use ic_logger::ReplicaLogger;
1313
use ic_protobuf::p2p::v1 as pb;
1414
use ic_types::{
1515
artifact::StateSyncArtifactId,
16-
chunkable::{ArtifactChunk, ChunkId},
16+
chunkable::{Chunk, ChunkId},
1717
};
1818
use prost::Message;
1919

@@ -100,7 +100,7 @@ pub(crate) fn parse_chunk_handler_response(
100100
response: Response<Bytes>,
101101
chunk_id: ChunkId,
102102
metrics: OngoingStateSyncMetrics,
103-
) -> Result<ArtifactChunk, DownloadChunkError> {
103+
) -> Result<Chunk, DownloadChunkError> {
104104
let (parts, body) = response.into_parts();
105105

106106
match parts.status {
@@ -127,11 +127,7 @@ pub(crate) fn parse_chunk_handler_response(
127127
}
128128
})?;
129129

130-
let chunk = ArtifactChunk {
131-
chunk_id,
132-
chunk: pb.data,
133-
};
134-
Ok(chunk)
130+
Ok(pb.data)
135131
}
136132
StatusCode::NO_CONTENT => Err(DownloadChunkError::NoContent),
137133
StatusCode::TOO_MANY_REQUESTS => Err(DownloadChunkError::Overloaded),

rs/p2p/state_sync_manager/tests/common.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use ic_metrics::MetricsRegistry;
1616
use ic_p2p_test_utils::mocks::{MockChunkable, MockStateSync};
1717
use ic_types::{
1818
artifact::{StateSyncArtifactId, StateSyncMessage},
19-
chunkable::{ArtifactChunk, ArtifactErrorCode, Chunk, ChunkId, Chunkable},
19+
chunkable::{ArtifactErrorCode, Chunk, ChunkId, Chunkable},
2020
crypto::CryptoHash,
2121
state_sync::{Manifest, MetaManifest, StateSyncVersion},
2222
CryptoHashOfState, Height, NodeId, PrincipalId,
@@ -281,23 +281,23 @@ impl Chunkable for FakeChunkable {
281281

282282
fn add_chunk(
283283
&mut self,
284-
artifact_chunk: ArtifactChunk,
284+
chunk_id: ChunkId,
285+
chunk: Chunk,
285286
) -> Result<StateSyncMessage, ArtifactErrorCode> {
286287
for set in self.chunk_sets.iter_mut() {
287288
if set.is_empty() {
288289
continue;
289290
}
290-
if set.remove(&artifact_chunk.chunk_id) {
291+
if set.remove(&chunk_id) {
291292
break;
292293
} else {
293-
panic!("Downloaded chunk {} twice", artifact_chunk.chunk_id)
294+
panic!("Downloaded chunk {} twice", chunk_id)
294295
}
295296
}
296297

297298
// Add chunk to state if not part of manifest
298-
if !is_manifest_chunk(artifact_chunk.chunk_id) {
299-
self.local_state
300-
.add_chunk(artifact_chunk.chunk_id, artifact_chunk.chunk.len())
299+
if !is_manifest_chunk(chunk_id) {
300+
self.local_state.add_chunk(chunk_id, chunk.len())
301301
}
302302

303303
let elems = self.chunk_sets.iter().map(|set| set.len()).sum::<usize>();
@@ -342,10 +342,11 @@ impl Chunkable for SharableMockChunkable {
342342
}
343343
fn add_chunk(
344344
&mut self,
345-
artifact_chunk: ArtifactChunk,
345+
chunk_id: ChunkId,
346+
chunk: Chunk,
346347
) -> Result<StateSyncMessage, ArtifactErrorCode> {
347348
self.add_chunks_calls.fetch_add(1, Ordering::SeqCst);
348-
self.mock.lock().unwrap().add_chunk(artifact_chunk)
349+
self.mock.lock().unwrap().add_chunk(chunk_id, chunk)
349350
}
350351
}
351352

rs/p2p/state_sync_manager/tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ fn test_state_sync_abortion() {
735735
c2.clear();
736736
c2.get_mut()
737737
.expect_add_chunk()
738-
.returning(|_| Err(ArtifactErrorCode::ChunkVerificationFailed));
738+
.returning(|_, _| Err(ArtifactErrorCode::ChunkVerificationFailed));
739739
c2.get_mut()
740740
.expect_chunks_to_download()
741741
.returning(|| Box::new(vec![ChunkId::from(1)].into_iter()) as Box<_>);

rs/p2p/test_utils/src/mocks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use ic_interfaces::p2p::{
77
state_sync::StateSyncClient,
88
};
99
use ic_quic_transport::{ConnId, SendError, Transport};
10-
use ic_types::{artifact::PriorityFn, chunkable::ArtifactChunk};
10+
use ic_types::artifact::PriorityFn;
1111
use ic_types::{
1212
artifact::{StateSyncArtifactId, StateSyncMessage},
1313
chunkable::{ArtifactErrorCode, Chunkable},
@@ -61,7 +61,7 @@ mock! {
6161

6262
impl Chunkable for Chunkable{
6363
fn chunks_to_download(&self) -> Box<dyn Iterator<Item = ChunkId>>;
64-
fn add_chunk(&mut self, artifact_chunk: ArtifactChunk) -> Result<StateSyncMessage, ArtifactErrorCode>;
64+
fn add_chunk(&mut self, chunk_id: ChunkId, chunk: Chunk) -> Result<StateSyncMessage, ArtifactErrorCode>;
6565
}
6666
}
6767

rs/state_manager/src/state_sync/chunkable.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ use ic_sys::mmap::ScopedMmap;
1313
use ic_types::{
1414
artifact::StateSyncMessage,
1515
chunkable::{
16-
ArtifactChunk,
1716
ArtifactErrorCode::{self, ChunkVerificationFailed, ChunksMoreNeeded},
18-
ChunkId, Chunkable,
17+
Chunk, ChunkId, Chunkable,
1918
},
2019
malicious_flags::MaliciousFlags,
2120
state_sync::{
@@ -1159,21 +1158,22 @@ impl Chunkable for IncompleteState {
11591158

11601159
fn add_chunk(
11611160
&mut self,
1162-
artifact_chunk: ArtifactChunk,
1161+
chunk_id: ChunkId,
1162+
chunk: Chunk,
11631163
) -> Result<StateSyncMessage, ArtifactErrorCode> {
1164-
let ix = artifact_chunk.chunk_id.get();
1165-
let payload = &artifact_chunk.chunk;
1164+
let ix = chunk_id.get();
1165+
let payload = &chunk;
11661166
match &mut self.state {
11671167
DownloadState::Complete(ref artifact) => {
11681168
debug!(
11691169
self.log,
1170-
"Received chunk {} on completed state {}", artifact_chunk.chunk_id, self.height
1170+
"Received chunk {} on completed state {}", chunk_id, self.height
11711171
);
11721172

11731173
Ok(*artifact.clone())
11741174
}
11751175
DownloadState::Blank => {
1176-
if artifact_chunk.chunk_id == META_MANIFEST_CHUNK {
1176+
if chunk_id == META_MANIFEST_CHUNK {
11771177
let meta_manifest = decode_meta_manifest(payload).map_err(|err| {
11781178
warn!(
11791179
self.log,

rs/state_manager/tests/common/mod.rs

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ use ic_test_utilities_tmpdir::tmpdir;
2222
use ic_types::{
2323
artifact::StateSyncMessage,
2424
chunkable::{
25-
ArtifactChunk,
2625
ArtifactErrorCode::{ChunkVerificationFailed, ChunksMoreNeeded},
27-
ChunkId, Chunkable,
26+
Chunk, ChunkId, Chunkable,
2827
},
2928
consensus::certification::{Certification, CertificationContent},
3029
crypto::Signed,
@@ -394,8 +393,8 @@ pub fn pipe_state_sync(src: StateSyncMessage, mut dst: Box<dyn Chunkable>) -> St
394393
.expect("State sync not completed.")
395394
}
396395

397-
fn alter_chunk_data(chunk: &mut ArtifactChunk) {
398-
let mut chunk_data = chunk.chunk.clone();
396+
fn alter_chunk_data(chunk: &mut Chunk) {
397+
let mut chunk_data = chunk.clone();
399398
match chunk_data.last_mut() {
400399
Some(last) => {
401400
// Alter the last element of chunk_data.
@@ -406,7 +405,7 @@ fn alter_chunk_data(chunk: &mut ArtifactChunk) {
406405
chunk_data = vec![9; 100];
407406
}
408407
}
409-
chunk.chunk = chunk_data;
408+
*chunk = chunk_data;
410409
}
411410

412411
/// Pipe the meta-manifest (chunk 0) from src to dest.
@@ -423,19 +422,16 @@ pub fn pipe_meta_manifest(
423422

424423
let id = ids[0];
425424

426-
let mut chunk = ArtifactChunk {
427-
chunk_id: id,
428-
chunk: src
429-
.clone()
430-
.get_chunk(id)
431-
.unwrap_or_else(|| panic!("Requested unknown chunk {}", id)),
432-
};
425+
let mut chunk = src
426+
.clone()
427+
.get_chunk(id)
428+
.unwrap_or_else(|| panic!("Requested unknown chunk {}", id));
433429

434430
if use_bad_chunk {
435431
alter_chunk_data(&mut chunk);
436432
}
437433

438-
match dst.add_chunk(chunk) {
434+
match dst.add_chunk(id, chunk) {
439435
Ok(msg) => Ok(msg),
440436
Err(ChunksMoreNeeded) => Err(StateSyncErrorCode::ChunksMoreNeeded),
441437
Err(ChunkVerificationFailed) => Err(StateSyncErrorCode::MetaManifestVerificationFailed),
@@ -460,19 +456,16 @@ pub fn pipe_manifest(
460456
assert!(ids.iter().all(|id| manifest_chunks.contains(id)));
461457

462458
for (index, id) in ids.iter().enumerate() {
463-
let mut chunk = ArtifactChunk {
464-
chunk_id: *id,
465-
chunk: src
466-
.clone()
467-
.get_chunk(*id)
468-
.unwrap_or_else(|| panic!("Requested unknown chunk {}", id)),
469-
};
459+
let mut chunk = src
460+
.clone()
461+
.get_chunk(*id)
462+
.unwrap_or_else(|| panic!("Requested unknown chunk {}", id));
470463

471464
if use_bad_chunk && index == ids.len() / 2 {
472465
alter_chunk_data(&mut chunk);
473466
}
474467

475-
match dst.add_chunk(chunk) {
468+
match dst.add_chunk(*id, chunk) {
476469
Ok(msg) => {
477470
return Ok(msg);
478471
}
@@ -506,19 +499,16 @@ pub fn pipe_partial_state_sync(
506499
omitted_chunks = true;
507500
continue;
508501
}
509-
let mut chunk = ArtifactChunk {
510-
chunk_id: *id,
511-
chunk: src
512-
.clone()
513-
.get_chunk(*id)
514-
.unwrap_or_else(|| panic!("Requested unknown chunk {}", id)),
515-
};
502+
let mut chunk = src
503+
.clone()
504+
.get_chunk(*id)
505+
.unwrap_or_else(|| panic!("Requested unknown chunk {}", id));
516506

517507
if use_bad_chunk && index == ids.len() / 2 {
518508
alter_chunk_data(&mut chunk);
519509
}
520510

521-
match dst.add_chunk(chunk) {
511+
match dst.add_chunk(*id, chunk) {
522512
Ok(msg) => {
523513
return Ok(msg);
524514
}

rs/types/types/src/chunkable.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
//! For more context please check `<https://youtu.be/WaNJINjGleg>`
1313
//!
1414
use crate::artifact::{Artifact, StateSyncMessage};
15-
use ic_protobuf::p2p::v1 as p2p_pb;
1615
use phantom_newtype::Id;
1716

1817
pub type Chunk = Vec<u8>;
@@ -25,16 +24,8 @@ pub enum ArtifactErrorCode {
2524
}
2625

2726
/// The chunk type.
28-
pub type ChunkId = Id<ArtifactChunk, u32>;
29-
30-
/// An artifact chunk.
31-
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
32-
pub struct ArtifactChunk {
33-
// Chunk number/id for this chunk
34-
pub chunk_id: ChunkId,
35-
// Payload for the chunk
36-
pub chunk: Chunk,
37-
}
27+
pub struct ChunkIdTag;
28+
pub type ChunkId = Id<ChunkIdTag, u32>;
3829

3930
/// Interface providing access to artifact.
4031
pub trait ChunkableArtifact {
@@ -45,12 +36,7 @@ pub trait Chunkable {
4536
fn chunks_to_download(&self) -> Box<dyn Iterator<Item = ChunkId>>;
4637
fn add_chunk(
4738
&mut self,
48-
artifact_chunk: ArtifactChunk,
39+
chunk_id: ChunkId,
40+
chunk: Chunk,
4941
) -> Result<StateSyncMessage, ArtifactErrorCode>;
5042
}
51-
52-
impl From<ArtifactChunk> for p2p_pb::StateSyncChunkResponse {
53-
fn from(chunk: ArtifactChunk) -> Self {
54-
Self { data: chunk.chunk }
55-
}
56-
}

0 commit comments

Comments
 (0)