tezos_smart_rollup_host/
lib.rs

1// SPDX-FileCopyrightText: 2022-2023 TriliTech <contact@trili.tech>
2// SPDX-FileCopyrightText: 2022-2023 Marigold <contact@marigold.dev>
3// SPDX-FileCopyrightText: 2022-2023 Nomadic Labs <contact@nomadic-labs.com>
4// SPDX-FileCopyrightText: 2023 Functori <contact@functori.com>
5//
6// SPDX-License-Identifier: MIT
7
8#![doc = include_str!("../README.md")]
9#![cfg_attr(not(any(feature = "testing", feature = "std")), no_std)]
10#![deny(missing_docs)]
11#![deny(rustdoc::broken_intra_doc_links)]
12
13#[cfg(feature = "alloc")]
14extern crate alloc;
15
16pub mod dal_parameters;
17pub mod input;
18pub mod metadata;
19pub mod path;
20pub mod runtime;
21
22/// The size of a DAL parameters in bytes: 4 * size(i64) = 32 bytes.
23pub use crate::dal_parameters::DAL_PARAMETERS_SIZE;
24/// The size of a metadata in bytes: 20 (rollup address) + 4 (origination level).
25pub use crate::metadata::METADATA_SIZE;
26use path::RefPath;
27
28/// Boot path for kernels
29pub const KERNEL_BOOT_PATH: RefPath = RefPath::assert_from(b"/kernel/boot.wasm");
30
31/// Defines the errors possibly returned by an host functions.
32#[repr(i32)]
33#[derive(Debug, Copy, Clone, Eq, PartialEq)]
34pub enum Error {
35    /// The store key submitted as an argument of a host function exceeds the
36    /// authorized limit.
37    StoreKeyTooLarge = tezos_smart_rollup_core::STORE_KEY_TOO_LARGE,
38    /// The store key submitted as an argument of a host function cannot be
39    /// parsed.
40    StoreInvalidKey = tezos_smart_rollup_core::STORE_INVALID_KEY,
41    /// The contents (if any) of the store under the key submitted as an
42    /// argument of a host function is not a value.
43    StoreNotAValue = tezos_smart_rollup_core::STORE_NOT_A_VALUE,
44    /// An access in a value of the durable storage has failed, supposedly out
45    /// of bounds of a value.
46    StoreInvalidAccess = tezos_smart_rollup_core::STORE_INVALID_ACCESS,
47    /// Writing a value has exceeded 2^31 bytes.
48    StoreValueSizeExceeded = tezos_smart_rollup_core::STORE_VALUE_SIZE_EXCEEDED,
49    /// An address is out of bound of the memory.
50    MemoryInvalidAccess = tezos_smart_rollup_core::MEMORY_INVALID_ACCESS,
51    /// The input or output submitted as an argument of a host function exceeds
52    /// the authorized limit.
53    InputOutputTooLarge = tezos_smart_rollup_core::INPUT_OUTPUT_TOO_LARGE,
54    /// Generic error code for unexpected errors.
55    GenericInvalidAccess = tezos_smart_rollup_core::GENERIC_INVALID_ACCESS,
56    /// A value cannot be modified if it is readonly.
57    StoreReadonlyValue = tezos_smart_rollup_core::STORE_READONLY_VALUE,
58    /// The key was not found in storage
59    StoreNotANode = tezos_smart_rollup_core::STORE_NOT_A_NODE,
60    /// The outbox is full
61    FullOutbox = tezos_smart_rollup_core::FULL_OUTBOX,
62}
63
64impl core::fmt::Display for Error {
65    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
66        match self {
67            Self::StoreKeyTooLarge => write!(f, "StoreKeyTooLarge"),
68            Self::StoreInvalidKey => write!(f, "StoreInvalidKey"),
69            Self::StoreNotAValue => write!(f, "StoreNotAValue"),
70            Self::StoreInvalidAccess => write!(f, "StoreInvalidAccess"),
71            Self::StoreValueSizeExceeded => write!(f, "StoreValueSizeExceeded"),
72            Self::MemoryInvalidAccess => write!(f, "MemoryInvalidAccess"),
73            Self::InputOutputTooLarge => write!(f, "InputOutputTooLarge"),
74            Self::GenericInvalidAccess => write!(f, "GenericInvalidAccess"),
75            Self::StoreReadonlyValue => write!(f, "StoreReadonlyValue"),
76            Self::StoreNotANode => write!(f, "StoreNotANode"),
77            Self::FullOutbox => write!(f, "FullOutbox"),
78        }
79    }
80}
81
82impl From<i32> for Error {
83    fn from(code: i32) -> Self {
84        match code {
85            tezos_smart_rollup_core::STORE_KEY_TOO_LARGE => Self::StoreKeyTooLarge,
86            tezos_smart_rollup_core::STORE_INVALID_KEY => Self::StoreInvalidKey,
87            tezos_smart_rollup_core::STORE_NOT_A_VALUE => Self::StoreNotAValue,
88            tezos_smart_rollup_core::STORE_VALUE_SIZE_EXCEEDED => {
89                Self::StoreValueSizeExceeded
90            }
91            tezos_smart_rollup_core::STORE_INVALID_ACCESS => Self::StoreInvalidAccess,
92            tezos_smart_rollup_core::MEMORY_INVALID_ACCESS => Self::MemoryInvalidAccess,
93            tezos_smart_rollup_core::INPUT_OUTPUT_TOO_LARGE => Self::InputOutputTooLarge,
94            tezos_smart_rollup_core::GENERIC_INVALID_ACCESS => Self::GenericInvalidAccess,
95            tezos_smart_rollup_core::STORE_READONLY_VALUE => Self::StoreReadonlyValue,
96            tezos_smart_rollup_core::STORE_NOT_A_NODE => Self::StoreNotANode,
97            tezos_smart_rollup_core::FULL_OUTBOX => Self::FullOutbox,
98            _ => Error::GenericInvalidAccess,
99        }
100    }
101}
102
103impl From<i64> for Error {
104    fn from(code: i64) -> Self {
105        match i32::try_from(code) {
106            Ok(error) => error.into(),
107            Err(_) => Error::GenericInvalidAccess,
108        }
109    }
110}
111
112impl Error {
113    /// Extracts the error from the returned value as a result
114    pub fn wrap(code: i32) -> Result<usize, Self> {
115        if code >= 0 {
116            // Casting to usize is safe, since we eluded the negative values
117            Ok(code as usize)
118        } else {
119            Err(code.into())
120        }
121    }
122
123    /// Returns the code for the given error.
124    pub fn code(self) -> i32 {
125        self as i32
126    }
127}