Struct piecrust::Session

source ·
pub struct Session { /* private fields */ }
Expand description

A running mutation to a state.

Sessions are spawned using a VM instance, and can be used to call contracts with to modify their state. A sequence of these calls may then be commited to, or discarded by simply allowing the session to drop.

New contracts are to be deployed in the context of a session.

Implementations§

source§

impl Session

source

pub fn deploy<'a, A, D, const N: usize>( &mut self, bytecode: &[u8], deploy_data: D, points_limit: u64 ) -> Result<ContractId, Error>where A: 'a + for<'b> Serialize<CompositeSerializer<BufferSerializer<&'b mut [u8]>, BufferScratch<&'b mut [u8; 64]>>>, D: Into<ContractData<'a, A, N>>,

Deploy a contract, returning its ContractId. The ID is computed using a blake3 hash of the bytecode.

Since a deployment may execute some contract initialization code, that code will be metered and executed with the given points_limit.

Errors

It is possible that a collision between contract IDs occurs, even for different contract IDs. This is due to the fact that all contracts have to fit into a sparse merkle tree with 2^32 positions, and as such a 256-bit number has to be mapped into a 32-bit number.

If such a collision occurs, PersistenceError will be returned.

source

pub fn call<A, R>( &mut self, contract: ContractId, fn_name: &str, fn_arg: &A, points_limit: u64 ) -> Result<CallReceipt<R>, Error>where A: for<'b> Serialize<CompositeSerializer<BufferSerializer<&'b mut [u8]>, BufferScratch<&'b mut [u8; 64]>>>, R: Archive, R::Archived: Deserialize<R, Infallible> + for<'b> CheckBytes<DefaultValidator<'b>>,

Execute a call on the current state of this session.

Calls are atomic, meaning that on failure their execution doesn’t modify the state. They are also metered, and will execute with the given points_limit.

Errors

The call may error during execution for a wide array of reasons, the most common ones being running against the point limit and a contract panic. Calling the ‘init’ method is not allowed except for when called from the deploy method.

source

pub fn call_raw<V: Into<Vec<u8>>>( &mut self, contract: ContractId, fn_name: &str, fn_arg: V, points_limit: u64 ) -> Result<CallReceipt<Vec<u8>>, Error>

Execute a raw call on the current state of this session.

Raw calls do not specify the type of the argument or of the return. The caller is responsible for serializing the argument as the target contract expects.

For more information about calls see call.

source

pub fn feeder_call<A, R>( &mut self, contract: ContractId, fn_name: &str, fn_arg: &A, feeder: Sender<Vec<u8>> ) -> Result<CallReceipt<R>, Error>where A: for<'b> Serialize<CompositeSerializer<BufferSerializer<&'b mut [u8]>, BufferScratch<&'b mut [u8; 64]>>>, R: Archive, R::Archived: Deserialize<R, Infallible> + for<'b> CheckBytes<DefaultValidator<'b>>,

Execute a feeder call on the current state of this session.

Feeder calls are used to have the contract be able to report larger amounts of data to the host via the channel included in this call.

These calls are always performed with the maximum amount of points, since the contracts may spend quite a large amount in an effort to report data.

source

pub fn feeder_call_raw<V: Into<Vec<u8>>>( &mut self, contract: ContractId, fn_name: &str, fn_arg: V, feeder: Sender<Vec<u8>> ) -> Result<CallReceipt<Vec<u8>>, Error>

Execute a raw feeder call on the current state of this session.

See feeder_call and call_raw for more information of this type of call.

source

pub fn initialize( &mut self, contract: ContractId, arg: Vec<u8>, points_limit: u64 ) -> Result<(), Error>

source

pub fn root(&self) -> [u8; 32]

Return the state root of the current state of the session.

The state root is the root of a merkle tree whose leaves are the hashes of the state of of each contract, ordered by their contract ID.

It also doubles as the ID of a commit - the commit root.

source

pub fn commit(self) -> Result<[u8; 32], Error>

Commits the given session to disk, consuming the session and returning its state root.

source

pub fn with_debug<C, R>(&self, c: C) -> Rwhere C: FnOnce(&[String]) -> R,

source

pub fn meta(&self, name: &str) -> Option<Vec<u8>>

Returns the value of a metadata item.

source

pub fn serialize_data<V>(value: &V) -> Result<Vec<u8>, Error>where V: for<'a> Serialize<CompositeSerializer<BufferSerializer<&'a mut [u8]>, BufferScratch<&'a mut [u8; 64]>>>,

source

pub fn contract_metadata( &self, contract_id: &ContractId ) -> Option<&ContractMetadata>

Trait Implementations§

source§

impl Debug for Session

source§

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

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

impl Drop for Session

A session is created by leaking an using Box::leak on a SessionInner. Therefore, the memory needs to be recovered.

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Send for Session

source§

impl Sync for Session

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> ArchivePointee for T

§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<F, W, T, D> Deserialize<With<T, W>, D> for Fwhere W: DeserializeWith<F, T, D>, D: Fallible + ?Sized, F: ?Sized,

source§

fn deserialize( &self, deserializer: &mut D ) -> Result<With<T, W>, <D as Fallible>::Error>

Deserializes using the given deserializer
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere 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> LayoutRaw for T

source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Gets the layout of the type.
§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Pointee for T

§

type Metadata = ()

The type for metadata in pointers and references to Self.
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.
§

impl<T> Upcastable for Twhere T: Any + Send + Sync + 'static,

§

fn upcast_any_ref(&self) -> &(dyn Any + 'static)

upcast ref
§

fn upcast_any_mut(&mut self) -> &mut (dyn Any + 'static)

upcast mut ref
§

fn upcast_any_box(self: Box<T, Global>) -> Box<dyn Any, Global>

upcast boxed dyn
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more