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