t3rn_sdk_primitives/
signal.rs

1use crate::Debug;
2use codec::{Decode, Encode, MaxEncodedLen};
3use scale_info::TypeInfo;
4
5/// This trait provides access for a provider to send/recieve signals.
6/// This is a bidirectional trait, both 3vm & sdk implement a variant of this with a differing error type.
7///
8/// This enables a contract to provide some feedback to the circuit and tell it if it should break or not.
9/// This also allows a pallet to enable signalling.
10pub trait Signaller<Hash>
11where
12    Hash: Encode + Decode + Debug + Clone + PartialEq + Eq,
13{
14    type Result;
15    fn signal(signal: &ExecutionSignal<Hash>) -> Self::Result;
16}
17
18/// A representation of a signal
19#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, MaxEncodedLen, TypeInfo)]
20pub struct ExecutionSignal<Hash>
21where
22    Hash: Encode + Decode + Debug + Clone + PartialEq + Eq,
23{
24    /// The current step for the signal
25    pub step: u32,
26    /// The signal type
27    pub kind: SignalKind,
28    /// The id associated with the execution
29    pub execution_id: Hash,
30}
31
32impl<Hash> ExecutionSignal<Hash>
33where
34    Hash: Encode + Decode + Debug + Clone + PartialEq + Eq,
35{
36    pub fn new(execution_id: &Hash, step: Option<u32>, kind: SignalKind) -> Self {
37        ExecutionSignal {
38            execution_id: execution_id.clone(),
39            step: step.unwrap_or(0),
40            kind,
41        }
42    }
43}
44
45#[derive(Debug, PartialEq, Eq, Clone, Copy, Decode, Encode, MaxEncodedLen, TypeInfo)]
46pub enum SignalKind {
47    /// Allows the contract to finish execution in an optimistic manner
48    Complete,
49    /// Allows the contract to break execution as soon as possible
50    Kill(KillReason),
51}
52
53#[derive(Debug, PartialEq, Eq, Clone, Copy, Decode, Encode, MaxEncodedLen, TypeInfo)]
54pub enum KillReason {
55    /// The contract indicated that execution should be killed due to some undefined behavior
56    Unhandled,
57    /// A specific reason to kill which signals there may be some issue with encoding/decoding
58    Codec,
59    /// The contract indicated that a user defined timeout tripped
60    Timeout,
61}