1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use crate::mock::MockedBlockchain;
use crate::test_utils::test_env::*;
use crate::AccountId;
use crate::{
Balance, BlockHeight, EpochHeight, Gas, PromiseResult, PublicKey, StorageUsage, VMContext,
};
pub fn accounts(id: usize) -> AccountId {
AccountId::new_unchecked(
["alice", "bob", "charlie", "danny", "eugene", "fargo"][id].to_string(),
)
}
#[derive(Clone)]
pub struct VMContextBuilder {
pub context: VMContext,
}
impl Default for VMContextBuilder {
fn default() -> Self {
Self::new()
}
}
#[allow(dead_code)]
impl VMContextBuilder {
pub fn new() -> Self {
Self {
context: VMContext {
current_account_id: alice().into(),
signer_account_id: bob().into(),
signer_account_pk: vec![0u8; 32],
predecessor_account_id: bob().into(),
input: vec![],
block_index: 0,
block_timestamp: 0,
epoch_height: 0,
account_balance: 10u128.pow(26),
account_locked_balance: 0,
storage_usage: 1024 * 300,
attached_deposit: 0,
prepaid_gas: 300 * 10u64.pow(12),
random_seed: vec![0u8; 32],
is_view: false,
output_data_receivers: vec![],
},
}
}
pub fn current_account_id(&mut self, account_id: AccountId) -> &mut Self {
self.context.current_account_id = account_id.into();
self
}
pub fn signer_account_id(&mut self, account_id: AccountId) -> &mut Self {
self.context.signer_account_id = account_id.into();
self
}
pub fn signer_account_pk(&mut self, pk: PublicKey) -> &mut Self {
self.context.signer_account_pk = pk.into();
self
}
pub fn predecessor_account_id(&mut self, account_id: AccountId) -> &mut Self {
self.context.predecessor_account_id = account_id.into();
self
}
pub fn block_index(&mut self, block_index: BlockHeight) -> &mut Self {
self.context.block_index = block_index;
self
}
pub fn block_timestamp(&mut self, block_timestamp: u64) -> &mut Self {
self.context.block_timestamp = block_timestamp;
self
}
pub fn epoch_height(&mut self, epoch_height: EpochHeight) -> &mut Self {
self.context.epoch_height = epoch_height;
self
}
pub fn account_balance(&mut self, amount: Balance) -> &mut Self {
self.context.account_balance = amount;
self
}
pub fn account_locked_balance(&mut self, amount: Balance) -> &mut Self {
self.context.account_locked_balance = amount;
self
}
pub fn storage_usage(&mut self, usage: StorageUsage) -> &mut Self {
self.context.storage_usage = usage;
self
}
pub fn attached_deposit(&mut self, amount: Balance) -> &mut Self {
self.context.attached_deposit = amount;
self
}
pub fn prepaid_gas(&mut self, gas: Gas) -> &mut Self {
self.context.prepaid_gas = gas.0;
self
}
pub fn random_seed(&mut self, seed: Vec<u8>) -> &mut Self {
self.context.random_seed = seed;
self
}
pub fn is_view(&mut self, is_view: bool) -> &mut Self {
self.context.is_view = is_view;
self
}
pub fn build(&self) -> VMContext {
self.context.clone()
}
}
pub fn testing_env_with_promise_results(context: VMContext, promise_result: PromiseResult) {
let storage = crate::mock::with_mocked_blockchain(|b| b.take_storage());
crate::env::set_blockchain_interface(MockedBlockchain::new(
context,
Default::default(),
Default::default(),
vec![promise_result],
storage,
Default::default(),
None,
));
}