1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
#![feature(associated_type_defaults)]
mod bech32;
pub mod default_context;
pub mod default_signature;
mod dispatch;
mod encode;
mod error;
pub mod hooks;
mod prefix;
mod response;
mod serde_address;
#[cfg(test)]
mod tests;
pub mod transaction;
pub use crate::bech32::AddressBech32;
use borsh::{BorshDeserialize, BorshSerialize};
use core::fmt::{self, Debug, Display};
pub use dispatch::{DispatchCall, Genesis};
pub use error::Error;
pub use jmt::SimpleHasher as Hasher;
pub use prefix::Prefix;
pub use response::CallResponse;
pub use sov_rollup_interface::traits::AddressTrait;
use sov_state::{Storage, Witness, WorkingSet};
use thiserror::Error;
impl AsRef<[u8]> for Address {
fn as_ref(&self) -> &[u8] {
&self.addr
}
}
impl AddressTrait for Address {}
#[derive(PartialEq, Clone, Eq, borsh::BorshDeserialize, borsh::BorshSerialize)]
pub struct Address {
addr: [u8; 32],
}
impl<'a> TryFrom<&'a [u8]> for Address {
type Error = anyhow::Error;
fn try_from(addr: &'a [u8]) -> Result<Self, Self::Error> {
if addr.len() != 32 {
anyhow::bail!("Address must be 32 bytes long");
}
let mut addr_bytes = [0u8; 32];
addr_bytes.copy_from_slice(addr);
Ok(Self { addr: addr_bytes })
}
}
impl From<[u8; 32]> for Address {
fn from(addr: [u8; 32]) -> Self {
Self { addr }
}
}
impl Display for Address {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", AddressBech32::from(self))
}
}
impl Debug for Address {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", AddressBech32::from(self))
}
}
impl From<AddressBech32> for Address {
fn from(addr: AddressBech32) -> Self {
Self {
addr: addr.to_byte_array(),
}
}
}
#[derive(Error, Debug)]
pub enum SigVerificationError {
#[error("Bad signature {0}")]
BadSignature(String),
}
/// Signature used in the Module System.
pub trait Signature {
type PublicKey;
fn verify(
&self,
pub_key: &Self::PublicKey,
msg_hash: [u8; 32],
) -> Result<(), SigVerificationError>;
}
/// A type that can't be instantiated.
#[derive(Debug, PartialEq)]
pub enum NonInstantiable {}
/// PublicKey used in the Module System.
pub trait PublicKey {
fn to_address<A: AddressTrait>(&self) -> A;
}
/// The `Spec` trait configures certain key primitives to be used by a by a particular instance of a rollup.
/// `Spec` is almost always implemented on a Context object; since all Modules are generic
/// over a Context, rollup developers can easily optimize their code for different environments
/// by simply swapping out the Context (and by extension, the Spec).
///
/// For example, a rollup running in a STARK-based zkvm like Risc0 might pick Sha256 or Poseidon as its preferred hasher,
/// while a rollup running in an elliptic-curve based SNARK such as `Placeholder` from the =nil; foundation might
/// prefer a Pedersen hash. By using a generic Context and Spec, a rollup developer can trivially customize their
/// code for either (or both!) of these environments without touching their module implementations.
pub trait Spec {
/// The Address type used on the rollup. Typically calculated as the hash of a public key.
#[cfg(feature = "native")]
type Address: AddressTrait
+ BorshSerialize
+ BorshDeserialize
+ Into<AddressBech32>
+ From<AddressBech32>;
/// The Address type used on the rollup. Typically calculated as the hash of a public key.
#[cfg(not(feature = "native"))]
type Address: AddressTrait + BorshSerialize + BorshDeserialize;
/// Authenticated state storage used by the rollup. Typically some variant of a merkle-patricia trie.
type Storage: Storage + Clone;
/// The public key used for digital signatures
type PublicKey: borsh::BorshDeserialize + borsh::BorshSerialize + Eq + Clone + Debug + PublicKey;
/// The hasher preferred by the rollup, such as Sha256 or Poseidon.
type Hasher: Hasher;
/// The digital signature scheme used by the rollup
type Signature: borsh::BorshDeserialize
+ borsh::BorshSerialize
+ Eq
+ Clone
+ Debug
+ Signature<PublicKey = Self::PublicKey>;
/// A structure containing the non-deterministic inputs from the prover to the zk-circuit
type Witness: Witness;
}
/// A context contains information which is passed to modules during
/// transaction execution. Currently, context includes the sender of the transaction
/// as recovered from its signature.
///
/// Context objects also implement the [`Spec`] trait, which specifies the types to be used in this
/// instance of the state transition function. By making modules generic over a `Context`, developers
/// can easily update their cryptography to conform to the needs of different zk-proof systems.
pub trait Context: Spec + Clone + Debug + PartialEq {
/// Sender of the transaction.
fn sender(&self) -> &Self::Address;
/// Constructor for the Context.
fn new(sender: Self::Address) -> Self;
}
impl<T> Genesis for T
where
T: Module,
{
type Context = <Self as Module>::Context;
type Config = <Self as Module>::Config;
fn genesis(
&self,
config: &Self::Config,
working_set: &mut WorkingSet<<<Self as Genesis>::Context as Spec>::Storage>,
) -> Result<(), Error> {
<Self as Module>::genesis(self, config, working_set)
}
}
/// Every module has to implement this trait.
/// All the methods have a default implementation that can't be invoked (because they take `NonInstantiable` parameter).
/// This allows developers to override only some of the methods in their implementation and safely ignore the others.
pub trait Module {
/// Execution context.
type Context: Context;
/// Configuration for the genesis method.
type Config;
/// Module defined argument to the call method.
type CallMessage: Debug + BorshSerialize + BorshDeserialize = NonInstantiable;
/// Genesis is called when a rollup is deployed and can be used to set initial state values in the module.
fn genesis(
&self,
_config: &Self::Config,
_working_set: &mut WorkingSet<<Self::Context as Spec>::Storage>,
) -> Result<(), Error> {
Ok(())
}
/// Call allows interaction with the module and invokes state changes.
/// It takes a module defined type and a context as parameters.
fn call(
&self,
_message: Self::CallMessage,
_context: &Self::Context,
_working_set: &mut WorkingSet<<Self::Context as Spec>::Storage>,
) -> Result<CallResponse, Error> {
unreachable!()
}
}
/// Every module has to implement this trait.
pub trait ModuleInfo {
type Context: Context;
/// Module constructor.
fn new() -> Self;
/// Returns address of the module.
fn address(&self) -> &<Self::Context as Spec>::Address;
}
/// A StateTransitionRunner needs to implement this if
/// the RPC service is needed
pub trait RpcRunner {
type Context: Context;
fn get_storage(&self) -> <Self::Context as Spec>::Storage;
}