ink_engine/
exec_context.rs

1// Copyright (C) Use Ink (UK) Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use super::types::{
16    BlockNumber,
17    BlockTimestamp,
18    H160,
19};
20use ink_primitives::U256;
21
22/// The context of a contract execution.
23#[cfg_attr(test, derive(Debug, PartialEq, Eq))]
24#[derive(Default)]
25pub struct ExecContext {
26    /// The caller of the contract execution. Might be user or another contract.
27    ///
28    /// TODO check next comment
29    /// We don't know the specifics of the `AccountId` ‒ like how many bytes or what
30    /// type of default `AccountId` makes sense ‒ they are left to be initialized
31    /// by the crate which uses the `engine`. Methods which require a caller might
32    /// panic when it has not been set.
33    pub caller: H160,
34    /// The callee of the contract execution. Might be user or another contract.
35    ///
36    /// We don't know the specifics of the `AccountId` ‒ like how many bytes or what
37    /// type of default `AccountId` makes sense ‒ they are left to be initialized
38    /// by the crate which uses the `engine`. Methods which require a callee might
39    /// panic when it has not been set.
40    pub callee: Option<H160>,
41    /// The value transferred to the contract as part of the call.
42    pub value_transferred: U256,
43    /// The current block number.
44    pub block_number: BlockNumber,
45    /// The current block timestamp.
46    pub block_timestamp: BlockTimestamp,
47    /// Known contract accounts
48    pub contracts: Vec<H160>,
49}
50
51impl ExecContext {
52    /// Creates a new execution context.
53    pub fn new() -> Self {
54        Default::default()
55    }
56
57    /// Returns the callee.
58    pub fn callee(&self) -> H160 {
59        self.callee.expect("no callee has been set")
60    }
61
62    /// Resets the execution context
63    pub fn reset(&mut self) {
64        *self = Default::default();
65    }
66
67    /// Set the block timestamp for the execution context.
68    pub fn set_block_timestamp(&mut self, block_timestamp: BlockTimestamp) {
69        self.block_timestamp = block_timestamp
70    }
71
72    /// Set the block number for the execution context.
73    pub fn set_block_number(&mut self, block_number: BlockNumber) {
74        self.block_number = block_number
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use super::ExecContext;
81    use crate::types::H160;
82
83    #[test]
84    fn basic_operations() {
85        let mut exec_cont = ExecContext::new();
86
87        exec_cont.callee = Some(H160::from([13; 20]));
88        exec_cont.caller = H160::from([14; 20]);
89        exec_cont.value_transferred = 15.into();
90        assert_eq!(exec_cont.callee(), H160::from([13; 20]));
91
92        exec_cont.reset();
93
94        let new_exec_cont = ExecContext::new();
95        assert_eq!(exec_cont, new_exec_cont);
96    }
97}