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};
19use ink_primitives::{
20 Address,
21 U256,
22};
23
24/// The context of a contract execution.
25#[cfg_attr(test, derive(Debug, PartialEq, Eq))]
26#[derive(Default)]
27pub struct ExecContext {
28 /// The caller of the contract execution. Might be user or another contract.
29 ///
30 /// TODO check next comment
31 /// We don't know the specifics of the `AccountId` ‒ like how many bytes or what
32 /// type of default `AccountId` makes sense ‒ they are left to be initialized
33 /// by the crate which uses the `engine`. Methods which require a caller might
34 /// panic when it has not been set.
35 pub caller: Address,
36 /// The callee of the contract execution. Might be user or another contract.
37 ///
38 /// We don't know the specifics of the `AccountId` ‒ like how many bytes or what
39 /// type of default `AccountId` makes sense ‒ they are left to be initialized
40 /// by the crate which uses the `engine`. Methods which require a callee might
41 /// panic when it has not been set.
42 pub callee: Option<Address>,
43 /// The value transferred to the contract as part of the call.
44 pub value_transferred: U256,
45 /// The current block number.
46 pub block_number: BlockNumber,
47 /// The current block timestamp.
48 pub block_timestamp: BlockTimestamp,
49 /// Known contract accounts
50 pub contracts: Vec<Address>,
51}
52
53impl ExecContext {
54 /// Creates a new execution context.
55 pub fn new() -> Self {
56 Default::default()
57 }
58
59 /// Returns the callee.
60 pub fn callee(&self) -> Address {
61 self.callee.expect("no callee has been set")
62 }
63
64 /// Resets the execution context
65 pub fn reset(&mut self) {
66 *self = Default::default();
67 }
68
69 /// Set the block timestamp for the execution context.
70 pub fn set_block_timestamp(&mut self, block_timestamp: BlockTimestamp) {
71 self.block_timestamp = block_timestamp
72 }
73
74 /// Set the block number for the execution context.
75 pub fn set_block_number(&mut self, block_number: BlockNumber) {
76 self.block_number = block_number
77 }
78}
79
80#[cfg(test)]
81mod tests {
82 use super::{
83 Address,
84 ExecContext,
85 };
86
87 #[test]
88 fn basic_operations() {
89 let mut exec_cont = ExecContext::new();
90
91 exec_cont.callee = Some(Address::from([13; 20]));
92 exec_cont.caller = Address::from([14; 20]);
93 exec_cont.value_transferred = 15.into();
94 assert_eq!(exec_cont.callee(), Address::from([13; 20]));
95
96 exec_cont.reset();
97
98 let new_exec_cont = ExecContext::new();
99 assert_eq!(exec_cont, new_exec_cont);
100 }
101}