t3rn_sdk_primitives/
state.rs

1use crate::{storage::BoundedVec, xc::Chain, BTreeMap, Debug, Vec, MAX_PARAMETERS_IN_FUNCTION};
2use codec::{Decode, Encode, MaxEncodedLen};
3use t3rn_types::side_effect::SideEffect;
4
5/// Some new side effects to submit
6#[derive(Encode, Decode, MaxEncodedLen)]
7pub struct SideEffects<AccountId, Balance, Hash>
8where
9    Hash: Encode + Decode,
10    AccountId: Encode + Decode,
11    Balance: Encode + Decode,
12{
13    /// Indication of the current execution id
14    pub execution_id: Hash,
15    /// A fixed vector of side effects, bound by `MAX_PARAMETERS_IN_FUNCTION`
16    pub side_effects: BoundedVec<Chain<AccountId, Balance, Hash>, MAX_PARAMETERS_IN_FUNCTION>,
17}
18
19// HELLO: This has to have field parity with Circuit::LocalStateExecutionView
20/// The local state returned by the circuit for an execution
21#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, Default)]
22pub struct ExecutionState<Hash>
23where
24    Hash: Encode + Decode + Debug + Clone + PartialEq + Eq,
25{
26    /// An abstract state mapped by 32 bit hash and a vector of bytes
27    pub state: BTreeMap<[u8; 32], Vec<u8>>,
28    /// A multi-dimensional vector of side effects, indexed by `step`
29    pub side_effects: Vec<Vec<SideEffect<[u8; 32], u64, u128>>>,
30    /// Current step and the length of steps
31    pub steps_cnt: (u32, u32),
32    /// The id for this execution
33    pub execution_id: Hash,
34}
35
36/// A handler trait that allows generics to provide some execution_id
37pub trait GetExecutionId<Hash>
38where
39    Hash: Encode + Decode + Debug + Clone + PartialEq + Eq,
40{
41    fn get_execution_id(&self) -> &Hash;
42}
43
44impl<Hash> GetExecutionId<Hash> for ExecutionState<Hash>
45where
46    Hash: Encode + Decode + Debug + Clone + PartialEq + Eq,
47{
48    fn get_execution_id(&self) -> &Hash {
49        &self.execution_id
50    }
51}
52
53/// A handler trait that allows generics to provide some steps
54pub trait GetSteps {
55    fn get_index(&self) -> u32;
56
57    fn get_len(&self) -> u32;
58
59    fn reached_end(&self) -> bool;
60}
61
62impl<Hash> GetSteps for ExecutionState<Hash>
63where
64    Hash: Encode + Decode + Debug + Clone + PartialEq + Eq,
65{
66    fn get_index(&self) -> u32 {
67        self.steps_cnt.0
68    }
69
70    fn get_len(&self) -> u32 {
71        self.steps_cnt.1
72    }
73
74    fn reached_end(&self) -> bool {
75        self.steps_cnt.0 >= self.steps_cnt.1
76    }
77}
78
79/// A marker trait that implies a generic type implements all available getters
80pub trait Getters<Hash>: GetExecutionId<Hash> + GetSteps
81where
82    Hash: Encode + Decode + Debug + Clone + PartialEq + Eq,
83{
84}
85
86impl<Hash> Getters<Hash> for ExecutionState<Hash> where
87    Hash: Encode + Decode + Debug + Clone + PartialEq + Eq
88{
89}