Skip to content

Commit 1dfc853

Browse files
authored
Make clone_unchecked return a Result (#4819)
## Motivation Remove the potential panic in reentrant_collection_view.rs ## Proposal * Make clone_unchecked return a Result * Remove dead code ## Test Plan CI ## Release Plan - These changes should be backported to the latest `devnet` branch
1 parent 56b6b0d commit 1dfc853

31 files changed

+157
-161
lines changed

linera-core/src/chain_worker/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ where
248248
&mut self,
249249
) -> Result<OwnedRwLockReadGuard<ChainStateView<StorageClient::Context>>, WorkerError> {
250250
if self.shared_chain_view.is_none() {
251-
self.shared_chain_view = Some(Arc::new(RwLock::new(self.chain.clone_unchecked())));
251+
self.shared_chain_view = Some(Arc::new(RwLock::new(self.chain.clone_unchecked()?)));
252252
}
253253

254254
Ok(self

linera-service/src/exporter/runloops/block_processor/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ mod test {
192192
NodeOptions::default(),
193193
0,
194194
signal.clone(),
195-
exporter_storage.clone(),
195+
exporter_storage.clone()?,
196196
vec![],
197197
);
198198
let mut block_processor = BlockProcessor::new(
@@ -328,7 +328,7 @@ mod test {
328328
NodeOptions::default(),
329329
0,
330330
signal.clone(),
331-
exporter_storage.clone(),
331+
exporter_storage.clone()?,
332332
vec![],
333333
);
334334
let mut block_processor = BlockProcessor::new(
@@ -444,7 +444,7 @@ mod test {
444444
NodeOptions::default(),
445445
0,
446446
signal.clone(),
447-
exporter_storage.clone(),
447+
exporter_storage.clone()?,
448448
vec![],
449449
);
450450
let mut block_processor = BlockProcessor::new(
@@ -531,7 +531,7 @@ mod test {
531531
NodeOptions::default(),
532532
0,
533533
signal.clone(),
534-
exporter_storage.clone(),
534+
exporter_storage.clone()?,
535535
vec![],
536536
);
537537
let mut block_processor = BlockProcessor::new(
@@ -640,7 +640,7 @@ mod test {
640640
NodeOptions::default(),
641641
0,
642642
signal.clone(),
643-
exporter_storage.clone(),
643+
exporter_storage.clone()?,
644644
vec![],
645645
);
646646
let mut block_processor = BlockProcessor::new(

linera-service/src/exporter/runloops/indexer/indexer_exporter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl Exporter {
6868
self.work_queue_size,
6969
destination_state.load(Ordering::Acquire) as usize,
7070
outgoing_stream,
71-
storage.clone(),
71+
storage.clone()?,
7272
);
7373

7474
let mut acknowledgement_task =

linera-service/src/exporter/runloops/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ where
107107
options,
108108
limits.work_queue_size.into(),
109109
shutdown_signal.clone(),
110-
exporter_storage.clone(),
110+
exporter_storage.clone()?,
111111
destination_config.destinations.clone(),
112112
);
113113

linera-service/src/exporter/runloops/task_manager.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ where
112112

113113
fn spawn(&mut self, id: DestinationId) {
114114
let exporter_builder = &self.exporters_builder;
115-
let join_handle = exporter_builder.spawn(id.clone(), self.storage.clone());
115+
let storage = self.storage.clone().expect("Failed to clone storage");
116+
let join_handle = exporter_builder.spawn(id.clone(), storage);
116117
self.join_handles.insert(id, join_handle);
117118
}
118119
}

linera-service/src/exporter/runloops/validator_exporter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ impl Exporter {
6262
let (mut task_queue, task_receiver) = TaskQueue::new(
6363
self.work_queue_size,
6464
destination_state.load(Ordering::Acquire) as usize,
65-
storage.clone(),
65+
storage.clone()?,
6666
);
6767

68-
let export_task = ExportTask::new(node, storage.clone(), destination_state);
68+
let export_task = ExportTask::new(node, storage.clone()?, destination_state);
6969

7070
tokio::select! {
7171

linera-service/src/exporter/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ where
6868
);
6969

7070
let states = view.destination_states.get().clone();
71-
let canonical_state = view.canonical_state.clone_unchecked();
71+
let canonical_state = view.canonical_state.clone_unchecked()?;
7272

7373
Ok((view, canonical_state, states))
7474
}

linera-service/src/exporter/storage.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,14 @@ where
142142
self.shared_canonical_state.push(block)
143143
}
144144

145-
fn clone(&mut self) -> Self {
146-
Self {
145+
fn clone(&mut self) -> Result<Self, ExporterError> {
146+
Ok(Self {
147147
storage: self.storage.clone(),
148-
shared_canonical_state: self.shared_canonical_state.clone(),
148+
shared_canonical_state: self.shared_canonical_state.clone()?,
149149
blobs_cache: self.blobs_cache.clone(),
150150
blocks_cache: self.blocks_cache.clone(),
151151
destination_states: self.destination_states.clone(),
152-
}
152+
})
153153
}
154154
}
155155

@@ -202,8 +202,8 @@ where
202202
self.shared_storage.destination_states.load_state(id)
203203
}
204204

205-
pub(crate) fn clone(&mut self) -> Self {
206-
ExporterStorage::new(self.shared_storage.clone())
205+
pub(crate) fn clone(&mut self) -> Result<Self, ExporterError> {
206+
Ok(ExporterStorage::new(self.shared_storage.clone()?))
207207
}
208208

209209
pub(crate) fn get_latest_index(&self) -> usize {
@@ -240,7 +240,7 @@ where
240240

241241
let mut shared_storage =
242242
SharedStorage::new(storage, canonical_state, destination_states, limits);
243-
let exporter_storage = ExporterStorage::new(shared_storage.clone());
243+
let exporter_storage = ExporterStorage::new(shared_storage.clone()?);
244244

245245
Ok((
246246
Self {
@@ -422,13 +422,13 @@ where
422422
}
423423
}
424424

425-
fn clone(&mut self) -> Self {
426-
Self {
425+
fn clone(&mut self) -> Result<Self, ExporterError> {
426+
Ok(Self {
427427
count: self.count,
428428
state_cache: self.state_cache.clone(),
429429
state_updates_buffer: self.state_updates_buffer.clone(),
430-
state_context: self.state_context.clone_unchecked(),
431-
}
430+
state_context: self.state_context.clone_unchecked()?,
431+
})
432432
}
433433

434434
/// Returns the latest index of the canonical state.

linera-service/src/proxy/grpc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,9 +369,9 @@ where
369369
ViewError::KeyTooLong | ViewError::ArithmeticError(_) => {
370370
Status::out_of_range(err.to_string())
371371
}
372-
ViewError::NotFound(_)
373-
| ViewError::CannotAcquireCollectionEntry
374-
| ViewError::MissingEntries => Status::not_found(err.to_string()),
372+
ViewError::NotFound(_) | ViewError::MissingEntries => {
373+
Status::not_found(err.to_string())
374+
}
375375
};
376376
status.set_source(Arc::new(err));
377377
status

linera-views-derive/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ fn generate_clonable_view_code(input: ItemStruct) -> Result<TokenStream2, Error>
380380
let name = &field.ident;
381381
let ty = &field.ty;
382382
clone_constraints.push(quote! { #ty: ClonableView });
383-
clone_fields.push(quote! { #name: self.#name.clone_unchecked() });
383+
clone_fields.push(quote! { #name: self.#name.clone_unchecked()? });
384384
}
385385

386386
Ok(quote! {
@@ -390,10 +390,10 @@ fn generate_clonable_view_code(input: ItemStruct) -> Result<TokenStream2, Error>
390390
#(#clone_constraints,)*
391391
Self: linera_views::views::View,
392392
{
393-
fn clone_unchecked(&mut self) -> Self {
394-
Self {
393+
fn clone_unchecked(&mut self) -> Result<Self, linera_views::ViewError> {
394+
Ok(Self {
395395
#(#clone_fields,)*
396-
}
396+
})
397397
}
398398
}
399399
})

0 commit comments

Comments
 (0)