1#![deny(missing_docs)]
9#![allow(async_fn_in_trait)]
10
11use std::fmt;
12
13#[doc(hidden)]
14pub use async_trait::async_trait;
15#[cfg(all(not(target_arch = "wasm32"), unix))]
16use tokio::signal::unix;
17#[cfg(not(target_arch = "wasm32"))]
18use {::tracing::debug, tokio_util::sync::CancellationToken};
19pub mod abi;
20#[cfg(not(target_arch = "wasm32"))]
21pub mod command;
22pub mod crypto;
23pub mod data_types;
24pub mod dyn_convert;
25mod graphql;
26pub mod hashed;
27pub mod http;
28pub mod identifiers;
29mod limited_writer;
30pub mod ownership;
31#[cfg(not(target_arch = "wasm32"))]
32pub mod port;
33#[cfg(with_metrics)]
34pub mod prometheus_util;
35#[cfg(not(chain))]
36mod task;
37#[cfg(not(chain))]
38pub use task::Task;
39pub mod task_processor;
40pub mod time;
41#[cfg_attr(web, path = "tracing_web.rs")]
42pub mod tracing;
43#[cfg(not(target_arch = "wasm32"))]
44pub mod tracing_opentelemetry;
45#[cfg(test)]
46mod unit_tests;
47pub mod util;
48pub mod vm;
49
50pub use graphql::BcsHexParseError;
51#[doc(hidden)]
52pub use {async_graphql, bcs, hex};
53
54#[macro_export]
69macro_rules! ensure {
70 ($cond:expr, $e:expr) => {
71 if !($cond) {
72 return Err($e.into());
73 }
74 };
75}
76
77pub fn hex_debug<T: AsRef<[u8]>>(bytes: &T, f: &mut fmt::Formatter) -> fmt::Result {
111 const ELIDE_AFTER: usize = 16;
112 let bytes = bytes.as_ref();
113 if bytes.len() <= 2 * ELIDE_AFTER {
114 write!(f, "{}", hex::encode(bytes))?;
115 } else {
116 write!(
117 f,
118 "{}..{}",
119 hex::encode(&bytes[..ELIDE_AFTER]),
120 hex::encode(&bytes[(bytes.len() - ELIDE_AFTER)..])
121 )?;
122 }
123 Ok(())
124}
125
126#[expect(clippy::ptr_arg)] pub fn hex_vec_debug(list: &Vec<Vec<u8>>, f: &mut fmt::Formatter) -> fmt::Result {
151 write!(f, "[")?;
152 for (i, bytes) in list.iter().enumerate() {
153 if i != 0 {
154 write!(f, ", ")?;
155 }
156 hex_debug(bytes, f)?;
157 }
158 write!(f, "]")
159}
160
161pub fn visit_allocative_simple<T>(_: &T, visitor: &mut allocative::Visitor<'_>) {
163 visitor.visit_simple_sized::<T>();
164}
165
166#[cfg(not(target_arch = "wasm32"))]
169pub async fn listen_for_shutdown_signals(shutdown_sender: CancellationToken) {
170 let _shutdown_guard = shutdown_sender.drop_guard();
171
172 #[cfg(unix)]
173 {
174 let mut sigint =
175 unix::signal(unix::SignalKind::interrupt()).expect("Failed to set up SIGINT handler");
176 let mut sigterm =
177 unix::signal(unix::SignalKind::terminate()).expect("Failed to set up SIGTERM handler");
178 let mut sighup =
179 unix::signal(unix::SignalKind::hangup()).expect("Failed to set up SIGHUP handler");
180
181 tokio::select! {
182 _ = sigint.recv() => debug!("Received SIGINT"),
183 _ = sigterm.recv() => debug!("Received SIGTERM"),
184 _ = sighup.recv() => debug!("Received SIGHUP"),
185 }
186 }
187
188 #[cfg(windows)]
189 {
190 tokio::signal::ctrl_c()
191 .await
192 .expect("Failed to set up Ctrl+C handler");
193 debug!("Received Ctrl+C");
194 }
195}