entropy_programs_core/
lib.rs

1//! This supports core traits and types for supporting new architectures and programs, and interfacing with them.
2
3/// See the [`wit-bindgen` Rust guest example](https://github.com/bytecodealliance/wit-bindgen#guest-rust) for information on how to use this.
4pub mod bindgen {
5    wit_bindgen::generate!({
6        world: "program",
7        macro_export
8    });
9}
10
11pub use bindgen::Error;
12
13pub mod programs;
14
15pub use architecture::*;
16pub use programs::*;
17
18/// Each transaction-like architecture should implement these.
19pub mod architecture {
20    use super::bindgen::Error;
21    use serde::{Deserialize, Serialize};
22
23    /// Trait for defining important types associated with an architecture.
24    pub trait Architecture: Serialize + for<'de> Deserialize<'de> {
25        /// Account type for that chain(SS58, H160, etc)
26        type Address: Eq + Serialize + for<'de> Deserialize<'de> + From<Self::AddressRaw>;
27        /// Account type as it is stored in the database
28        type AddressRaw: Eq + Serialize + for<'de> Deserialize<'de> + From<Self::Address>;
29        /// Transaction request type for unsigned transactions
30        type TransactionRequest: GetSender<Self>
31            + GetReceiver<Self>
32            + Serialize
33            + for<'de> Deserialize<'de>
34            + Parse<Self>
35            + TryParse<Self>;
36    }
37
38    /// Trait for getting the the sender of a transaction.
39    pub trait GetSender<A: Architecture> {
40        fn sender(&self) -> Option<A::Address>;
41    }
42
43    /// Trait for getting the the receiver of a transaction.
44    pub trait GetReceiver<A: Architecture> {
45        fn receiver(&self) -> Option<A::Address>;
46    }
47
48    /// DEPRECATED: Use `TryParse`
49    ///
50    /// Trait for parsing a raw transaction request into its native transaction request struct.
51    pub trait Parse<A: Architecture> {
52        fn parse(raw_tx: String) -> Result<A::TransactionRequest, Error>;
53    }
54
55    /// Tries to parse a raw transaction request into its native transaction request struct.
56    pub trait TryParse<A: Architecture> {
57        fn try_parse(raw_tx: &[u8]) -> Result<A::TransactionRequest, Error>;
58    }
59}
60
61/// Includes items that should be imported into most scopes
62pub mod prelude {
63    // reexport getrandom custom handler (move to macro)
64    pub use getrandom::register_custom_getrandom;
65    // reexport all core traits
66    pub use super::architecture::*;
67
68    use core::num::NonZeroU32;
69    use getrandom::Error;
70
71    /// Custom `getrandom()` handler that always returns an error.
72    ///
73    /// `getrandom` is a commonly used package for sourcing randomness.This should return an error for now,
74    /// but in the future it might make sense for the validators to determinstically source randomness from
75    /// BABE (eg. at a certain block)
76    ///
77    /// From https://docs.rs/getrandom/latest/getrandom/macro.register_custom_getrandom.html
78    // TODO This should get throw into the macros
79    pub fn always_fail(_buf: &mut [u8]) -> Result<(), Error> {
80        let code = NonZeroU32::new(Error::CUSTOM_START.saturating_add(1)).unwrap();
81        Err(Error::from(code))
82    }
83}