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
impl Session
sourcepub fn deploy<'a, A, D, const N: usize>(
&mut self,
bytecode: &[u8],
deploy_data: D
) -> 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>>,
pub fn deploy<'a, A, D, const N: usize>( &mut self, bytecode: &[u8], deploy_data: D ) -> 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.
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.
sourcepub fn call<A, R>(
&mut self,
contract: ContractId,
fn_name: &str,
fn_arg: &A
) -> Result<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>>,
pub fn call<A, R>( &mut self, contract: ContractId, fn_name: &str, fn_arg: &A ) -> Result<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 point limit
defined in set_point_limit.
To know how many points a call spent after execution use the spent
function.
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.
sourcepub fn call_raw<V: Into<Vec<u8>>>(
&mut self,
contract: ContractId,
fn_name: &str,
fn_arg: V
) -> Result<Vec<u8>, Error>
pub fn call_raw<V: Into<Vec<u8>>>( &mut self, contract: ContractId, fn_name: &str, fn_arg: V ) -> Result<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.
pub fn initialize( &mut self, contract: ContractId, arg: Vec<u8> ) -> Result<(), Error>
sourcepub fn root(&self) -> [u8; 32]
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.
sourcepub fn set_point_limit(&mut self, limit: u64)
pub fn set_point_limit(&mut self, limit: u64)
Sets the point limit for the next call.
sourcepub fn spent(&self) -> u64
pub fn spent(&self) -> u64
Returns the number of points spent by the last call.
If neither have been called for the duration of the session, it will return 0.
sourcepub fn commit(self) -> Result<[u8; 32], Error>
pub fn commit(self) -> Result<[u8; 32], Error>
Commits the given session to disk, consuming the session and returning its state root.
pub fn take_events(&mut self) -> Vec<Event>
pub fn with_debug<C, R>(&self, c: C) -> Rwhere C: FnOnce(&[String]) -> R,
pub fn serialize_data<V>(value: &V) -> Vec<u8>where V: for<'a> Serialize<CompositeSerializer<BufferSerializer<&'a mut [u8]>, BufferScratch<&'a mut [u8; 64]>>>,
pub fn contract_metadata( &self, contract_id: &ContractId ) -> Option<&ContractMetadata>
Trait Implementations§
source§impl Drop for Session
impl Drop for Session
A session is created by leaking an using Box::leak on a SessionInner.
Therefore, the memory needs to be recovered.