Skip to main content

Schema

Struct Schema 

Source
pub struct Schema { /* private fields */ }
Expand description

A schema, representing set of types (i.e. rust code) as a data structure. The schema allows any included type’s borsh serialization to be displayed as a human readable string, and the type’s JSON serialisation to be re-serialised to borsh without depending on the original Rust type. It is also serialisable and therefore, once generated for a rollup, can be imported and used with non-Rust languages, enabling toolkits in any language to implement the same functionality as above.

A schema can be instantiated for any type that implements either UniversalWallet or OverrideSchema. In turn, UniversalWallet is intended to be automatically derived using the UniversalWallet macro.

Implementations§

Source§

impl Schema

Source

pub fn of_single_type<T>() -> Result<Schema, SchemaError>
where T: UniversalWallet,

Instantiate a schema for a single type. This root type will be at index 0

Examples found in repository?
examples/trim.rs (line 17)
4fn main() {
5    let filter_variants = |name: &str, variant: &str| match name {
6        "Transaction" => variant == "V0",
7        "RuntimeCall" => matches!(variant, "Exchange" | "Bank"),
8        "CallMessage" => matches!(variant, "Keeper" | "TransferWithMemo"),
9        "KeeperAction" => matches!(
10            variant,
11            "UpdateOraclePrices" | "UpdateMarkPrices" | "UpdateOraclePricesWithPythProofs"
12        ),
13        "UniquenessData" => variant == "Generation",
14        _ => panic!("'{name}::{variant}' is unknown"),
15    };
16
17    let our_schema = Schema::of_single_type::<Transaction>().unwrap();
18    let remote: SchemaFile = serde_json::from_str(include_str!("schema.json")).unwrap();
19
20    let left = trim(&our_schema, &filter_variants);
21    let right = trim(&remote.schema, &filter_variants);
22
23    let left = serde_json::to_string_pretty(&left).unwrap();
24    let right = serde_json::to_string_pretty(&right).unwrap();
25    let mut faults = 0usize;
26    for (l, r) in left.lines().zip(right.lines()) {
27        if l != r {
28            faults = faults.saturating_add(1);
29            println!("< {r}");
30            println!("> {l}");
31            if faults >= 10 {
32                break;
33            }
34        } else {
35            println!("  {l}");
36        }
37    }
38    assert_eq!(left, right);
39}
Source

pub fn of_rollup_types_with_chain_data<Transaction, UnsignedTransaction, RuntimeCall, Address>( chain_data: ChainData, ) -> Result<Schema, SchemaError>
where Transaction: UniversalWallet, UnsignedTransaction: UniversalWallet, RuntimeCall: UniversalWallet, Address: UniversalWallet,

Instantiate a schema for a standard set of rollup types: its complete transaction, its unsigned transaction, and its call message type. The types will be accessible using the indices stored in root_type_indices (in the above order); they can also be queried using the RollupRoots enum through the _rollup-tagged functions on the schema

Source

pub fn chain_data(&self) -> &ChainData

Get the chain data for the schema.

Source

pub fn metadata_hash(&self) -> Result<[u8; 32], SchemaError>

Source

pub fn from_json(input: &str) -> Result<Schema, Error>

Source

pub fn rollup_expected_index( &self, rollup_type: RollupRoots, ) -> Result<usize, SchemaError>

Source

pub fn display( &self, type_index: usize, input: &[u8], ) -> Result<String, SchemaError>

Use the schema to display the given type using the provided borsh encoded input

Source

pub fn json_to_borsh( &self, type_index: usize, input: &str, ) -> Result<Vec<u8>, SchemaError>

Use the schema to convert a serde-compatible JSON string of the given type into its borsh encoding

Source

pub fn fill_template_from_json( &self, root_index: usize, template_name: &str, input: &str, ) -> Result<Vec<u8>, SchemaError>

Use a stub JSON to create a full type using the named template.

Source

pub fn templates(&self, index: usize) -> Result<Vec<String>, SchemaError>

Lists all templates available for the given root type.

Source

pub fn chain_hash(&self) -> Result<[u8; 32], SchemaError>

Returns the chain ID calculated using the merkle root of all the schema types, combined with any chain-specific metadata. This allows the chain ID to be used for verification of the schema (and thus verification that a transaction claiming to correspond to a given schema will have the effect it claims).

Source

pub fn types(&self) -> &[Ty<IndexLinking>]

Source

pub fn serde_metadata(&self) -> &[ContainerSerdeMetadata]

Source

pub fn root_types(&self) -> &[usize]

Trait Implementations§

Source§

impl BorshDeserialize for Schema

Source§

fn deserialize_reader<__R>(reader: &mut __R) -> Result<Schema, Error>
where __R: Read,

Source§

fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>

Deserializes this instance from a given slice of bytes. Updates the buffer to point at the remaining bytes.
Source§

fn try_from_slice(v: &[u8]) -> Result<Self, Error>

Deserialize this instance from a slice of bytes.
Source§

fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>
where R: Read,

Source§

impl BorshSerialize for Schema

Source§

fn serialize<__W>(&self, writer: &mut __W) -> Result<(), Error>
where __W: Write,

Source§

impl Debug for Schema

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Schema

Source§

fn default() -> Schema

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Schema

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Schema, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Schema

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl TypeResolver for Schema

Auto Trait Implementations§

§

impl !Freeze for Schema

§

impl !RefUnwindSafe for Schema

§

impl Send for Schema

§

impl Sync for Schema

§

impl Unpin for Schema

§

impl UnsafeUnpin for Schema

§

impl !UnwindSafe for Schema

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,