pub struct Environment<'a, 'b, 'hooks> {
    pub global_context: &'a mut GlobalContext<'b, 'hooks>,
    pub contract_context: &'a ContractContext,
    pub call_stack: &'a mut CallStack,
    pub sender: Option<PrincipalData>,
    pub caller: Option<PrincipalData>,
    pub sponsor: Option<PrincipalData>,
}
Expand description

Environments pack a reference to the global context (which is basically the db), the current contract context, a call stack, the current sender, caller, and sponsor (if one exists). Essentially, the point of the Environment struct is to prevent all the eval functions from including all of these items in their method signatures individually. Because these different contexts can be mixed and matched (i.e., in a contract-call, you change contract context), a single “invocation” will end up creating multiple environment objects as context changes occur.

Fields§

§global_context: &'a mut GlobalContext<'b, 'hooks>§contract_context: &'a ContractContext§call_stack: &'a mut CallStack§sender: Option<PrincipalData>§caller: Option<PrincipalData>§sponsor: Option<PrincipalData>

Implementations§

§

impl<'a, 'b, 'hooks> Environment<'a, 'b, 'hooks>

pub fn new( global_context: &'a mut GlobalContext<'b, 'hooks>, contract_context: &'a ContractContext, call_stack: &'a mut CallStack, sender: Option<PrincipalData>, caller: Option<PrincipalData>, sponsor: Option<PrincipalData> ) -> Environment<'a, 'b, 'hooks>

Returns an Environment value & checks the types of the contract sender, caller, and sponsor

Panics

Panics if the Value types for sender (Principal), caller (Principal), or sponsor (Optional Principal) are incorrect.

pub fn nest_as_principal<'c>( &'c mut self, sender: PrincipalData ) -> Environment<'c, 'b, 'hooks>

Leaving sponsor value as is for this new context (as opposed to setting it to None)

pub fn nest_with_caller<'c>( &'c mut self, caller: PrincipalData ) -> Environment<'c, 'b, 'hooks>

pub fn eval_read_only_with_rules( &mut self, contract_identifier: &QualifiedContractIdentifier, program: &str, rules: ASTRules ) -> Result<Value, Error>

pub fn eval_raw_with_rules( &mut self, program: &str, rules: ASTRules ) -> Result<Value, Error>

pub fn run_free<F, A>(&mut self, to_run: F) -> Awhere F: FnOnce(&mut Environment<'_, '_, '_>) -> A,

Used only for contract-call! cost short-circuiting. Once the short-circuited cost has been evaluated and assessed, the contract-call! itself is executed “for free”.

pub fn epoch(&self) -> &StacksEpochId

This is the epoch of the the block that this transaction is executing within. Note: in the current plans for 2.1, there is also a contract-specific Clarity version which governs which native functions are available / defined. That is separate from this epoch identifier, and most Clarity VM changes should consult that value instead. This epoch identifier is used for determining how cost functions should be applied.

pub fn execute_contract( &mut self, contract: &QualifiedContractIdentifier, tx_name: &str, args: &[SymbolicExpression], read_only: bool ) -> Result<Value, Error>

pub fn execute_contract_allow_private( &mut self, contract: &QualifiedContractIdentifier, tx_name: &str, args: &[SymbolicExpression], read_only: bool ) -> Result<Value, Error>

This method is exposed for callers that need to invoke a private method directly. For example, this is used by the Stacks chainstate for invoking private methods on the pox-2 contract. This should not be called by user transaction processing.

pub fn execute_function_as_transaction( &mut self, function: &DefinedFunction, args: &[Value], next_contract_context: Option<&ContractContext> ) -> Result<Value, Error>

pub fn evaluate_at_block( &mut self, bhh: StacksBlockId, closure: &SymbolicExpression, local: &LocalContext<'_> ) -> Result<Value, Error>

pub fn initialize_contract( &mut self, contract_identifier: QualifiedContractIdentifier, contract_content: &str, ast_rules: ASTRules ) -> Result<(), Error>

pub fn initialize_contract_from_ast( &mut self, contract_identifier: QualifiedContractIdentifier, contract_version: ClarityVersion, contract_content: &ContractAST, contract_string: &str ) -> Result<(), Error>

pub fn stx_transfer( &mut self, from: &PrincipalData, to: &PrincipalData, amount: u128, memo: &BuffData ) -> Result<Value, Error>

Top-level STX-transfer, invoked by TokenTransfer transactions. Only commits if the inner stx_transfer_consolidated() returns an (ok true) value. Rolls back if it returns an (err ..) value, or if the method itself fails for some reason (miners should never build blocks that spend non-existent STX in a top-level token-transfer)

pub fn run_as_transaction<F, O, E>(&mut self, f: F) -> Result<O, E>where F: FnOnce(&mut Environment<'a, 'b, 'hooks>) -> Result<O, E>, E: From<Error>,

pub fn push_to_event_batch(&mut self, event: StacksTransactionEvent)

pub fn construct_print_transaction_event( contract_id: &QualifiedContractIdentifier, value: &Value ) -> StacksTransactionEvent

pub fn register_print_event(&mut self, value: Value) -> Result<(), Error>

pub fn register_stx_transfer_event( &mut self, sender: PrincipalData, recipient: PrincipalData, amount: u128, memo: BuffData ) -> Result<(), Error>

pub fn register_stx_burn_event( &mut self, sender: PrincipalData, amount: u128 ) -> Result<(), Error>

pub fn register_nft_transfer_event( &mut self, sender: PrincipalData, recipient: PrincipalData, value: Value, asset_identifier: AssetIdentifier ) -> Result<(), Error>

pub fn register_nft_mint_event( &mut self, recipient: PrincipalData, value: Value, asset_identifier: AssetIdentifier ) -> Result<(), Error>

pub fn register_nft_burn_event( &mut self, sender: PrincipalData, value: Value, asset_identifier: AssetIdentifier ) -> Result<(), Error>

pub fn register_ft_transfer_event( &mut self, sender: PrincipalData, recipient: PrincipalData, amount: u128, asset_identifier: AssetIdentifier ) -> Result<(), Error>

pub fn register_ft_mint_event( &mut self, recipient: PrincipalData, amount: u128, asset_identifier: AssetIdentifier ) -> Result<(), Error>

pub fn register_ft_burn_event( &mut self, sender: PrincipalData, amount: u128, asset_identifier: AssetIdentifier ) -> Result<(), Error>

Trait Implementations§

§

impl CostTracker for Environment<'_, '_, '_>

§

fn compute_cost( &mut self, cost_function: ClarityCostFunction, input: &[u64] ) -> Result<ExecutionCost, CostErrors>

§

fn add_cost(&mut self, cost: ExecutionCost) -> Result<(), CostErrors>

§

fn add_memory(&mut self, memory: u64) -> Result<(), CostErrors>

§

fn drop_memory(&mut self, memory: u64)

§

fn reset_memory(&mut self)

§

fn short_circuit_contract_call( &mut self, contract: &QualifiedContractIdentifier, function: &ClarityName, input: &[u64] ) -> Result<bool, CostErrors>

Check if the given contract-call should be short-circuited. If so: this charges the cost to the CostTracker, and return true If not: return false

Auto Trait Implementations§

§

impl<'a, 'b, 'hooks> !RefUnwindSafe for Environment<'a, 'b, 'hooks>

§

impl<'a, 'b, 'hooks> !Send for Environment<'a, 'b, 'hooks>

§

impl<'a, 'b, 'hooks> !Sync for Environment<'a, 'b, 'hooks>

§

impl<'a, 'b, 'hooks> Unpin for Environment<'a, 'b, 'hooks>

§

impl<'a, 'b, 'hooks> !UnwindSafe for Environment<'a, 'b, 'hooks>

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> 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<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

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

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

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> Same for T

§

type Output = T

Should always be 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<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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