Skip to content

Commit

Permalink
chore(pallet-gear): Add log::error! calls before each `unreachable!…
Browse files Browse the repository at this point in the history
…` call (#4084)
  • Loading branch information
techraed authored Jul 26, 2024
1 parent 7ee3afe commit 9cdc3d0
Show file tree
Hide file tree
Showing 8 changed files with 983 additions and 226 deletions.
13 changes: 9 additions & 4 deletions core/src/reservation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
ids::{prelude::*, MessageId, ReservationId},
message::IncomingDispatch,
};
use alloc::collections::BTreeMap;
use alloc::{collections::BTreeMap, format};
use gear_core_errors::ReservationError;
use scale_info::{
scale::{Decode, Encode},
Expand Down Expand Up @@ -184,10 +184,15 @@ impl GasReserver {
);

if maybe_reservation.is_some() {
unreachable!(
"Duplicate reservation was created with message id {} and nonce {}",
self.message_id, self.nonce.0,
let err_msg = format!(
"GasReserver::reserve: created a duplicate reservation. \
Message id - {message_id}, nonce - {nonce}",
message_id = self.message_id,
nonce = self.nonce.0
);

log::error!("{err_msg}");
unreachable!("{err_msg}");
}

Ok(id)
Expand Down
581 changes: 481 additions & 100 deletions pallets/gear/src/internal.rs

Large diffs are not rendered by default.

88 changes: 67 additions & 21 deletions pallets/gear/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1017,8 +1017,15 @@ pub mod pallet {
GasAllowanceOf::<T>::get()
.saturating_add(DbWeightOf::<T>::get().writes(1).ref_time()),
);
TaskPoolOf::<T>::add(bn, task)
.unwrap_or_else(|e| unreachable!("Scheduling logic invalidated! {:?}", e));
TaskPoolOf::<T>::add(bn, task.clone()).unwrap_or_else(|e| {
let err_msg = format!(
"process_tasks: failed adding not processed last task to task pool. \
Bn - {bn:?}, task - {task:?}. Got error - {e:?}"
);

log::error!("{err_msg}");
unreachable!("{err_msg}");
});
}

// Stopping iteration over blocks if no resources left.
Expand All @@ -1043,7 +1050,11 @@ pub mod pallet {
pub(crate) fn enable_lazy_pages() {
let prefix = ProgramStorageOf::<T>::pages_final_prefix();
if !LazyPagesRuntimeInterface::try_to_enable_lazy_pages(prefix) {
unreachable!("By some reasons we cannot run lazy-pages on this machine");
let err_msg =
"enable_lazy_pages: By some reasons we cannot run lazy-pages on this machine";

log::error!("{err_msg}");
unreachable!("{err_msg}");
}
}

Expand Down Expand Up @@ -1112,12 +1123,15 @@ pub mod pallet {

// By the invariant set in CodeStorage trait, original code can't exist in storage
// without the instrumented code
let original_code = T::CodeStorage::get_original_code(code_id).unwrap_or_else(||
unreachable!(
"Code storage is corrupted: instrumented code with id {:?} exists while original not",
code_id
)
);
let original_code = T::CodeStorage::get_original_code(code_id).unwrap_or_else(|| {
let err_msg = format!(
"reinstrument_code: failed to get original code, while instrumented exists. \
Code id - {code_id}"
);

log::error!("{err_msg}");
unreachable!("{err_msg}")
});

let code = Code::try_new(
original_code,
Expand Down Expand Up @@ -1273,8 +1287,13 @@ pub mod pallet {
entry: MessageEntry::Init,
};

QueueOf::<T>::queue(dispatch)
.unwrap_or_else(|e| unreachable!("Messages storage corrupted: {e:?}"));
QueueOf::<T>::queue(dispatch).unwrap_or_else(|e| {
let err_msg =
format!("do_create_program: failed queuing message. Got error - {e:?}");

log::error!("{err_msg}");
unreachable!("{err_msg}");
});

Self::deposit_event(program_event);
Self::deposit_event(event);
Expand Down Expand Up @@ -1562,7 +1581,7 @@ pub mod pallet {

// Reading message, if found, or failing extrinsic.
let mailboxed = Self::read_message(origin.clone(), message_id, reason)
.ok_or(Error::<T>::MessageNotFound)?;
.map_err(|_| Error::<T>::MessageNotFound)?;

let (builtins, _) = T::BuiltinDispatcherFactory::create();
if Self::is_active(&builtins, mailboxed.source()) {
Expand All @@ -1576,8 +1595,12 @@ pub mod pallet {
message.into_stored_dispatch(origin.cast(), mailboxed.source(), mailboxed.id());

// Queueing dispatch.
QueueOf::<T>::queue(dispatch)
.unwrap_or_else(|e| unreachable!("Message queue corrupted! {:?}", e));
QueueOf::<T>::queue(dispatch).unwrap_or_else(|e| {
let err_msg = format!("claim_value: failed queuing message. Got error - {e:?}");

log::error!("{err_msg}");
unreachable!("{err_msg}");
});
}

Ok(().into())
Expand Down Expand Up @@ -1800,13 +1823,32 @@ pub mod pallet {
entry: MessageEntry::Handle,
});

QueueOf::<T>::queue(message)
.unwrap_or_else(|e| unreachable!("Messages storage corrupted: {e:?}"));
QueueOf::<T>::queue(message).unwrap_or_else(|e| {
let err_msg =
format!("send_message_impl: failed queuing message. Got error - {e:?}");

log::error!("{err_msg}");
unreachable!("{err_msg}");
});
} else {
let message = message.into_stored(origin.cast());
// Take data for the error log
let message_id = message.id();
let source = origin.cast::<ProgramId>();
let destination = message.destination();

let message = message.into_stored(source);
let message: UserMessage = message
.try_into()
.unwrap_or_else(|_| unreachable!("Signal message sent to user"));
.unwrap_or_else(|_| {
// Signal message sent to user
let err_msg = format!(
"send_message_impl: failed convertion from stored into user message. \
Message id - {message_id}, program id - {source}, destination - {destination}",
);

log::error!("{err_msg}");
unreachable!("{err_msg}")
});

let existence_requirement = if keep_alive {
ExistenceRequirement::KeepAlive
Expand Down Expand Up @@ -1849,7 +1891,7 @@ pub mod pallet {

// Reading message, if found, or failing extrinsic.
let mailboxed = Self::read_message(origin.clone(), reply_to_id, reason)
.ok_or(Error::<T>::MessageNotFound)?;
.map_err(|_| Error::<T>::MessageNotFound)?;

Self::check_gas_limit(gas_limit)?;

Expand Down Expand Up @@ -1898,8 +1940,12 @@ pub mod pallet {
};

// Queueing dispatch.
QueueOf::<T>::queue(dispatch)
.unwrap_or_else(|e| unreachable!("Message queue corrupted! {:?}", e));
QueueOf::<T>::queue(dispatch).unwrap_or_else(|e| {
let err_msg = format!("send_reply_impl: failed queuing message. Got error - {e:?}");

log::error!("{err_msg}");
unreachable!("{err_msg}");
});

// Depositing pre-generated event.
Self::deposit_event(event);
Expand Down
Loading

0 comments on commit 9cdc3d0

Please sign in to comment.