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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
use crate::structs::*;

use crate::{
	eosio_println,
	print::{
		Printable,
	},
};

use crate::transaction::{
	Transaction,
};

use crate::action::{
	PermissionLevel,
};

use crate::privileged::{
	BlockchainParameters,
};

use crate::name::{
	Name,
};

use crate::serializer::{
	Packer,
};

use crate::{
	// vec,
	vec::Vec,
};

use core::slice;

use chaintester::{
    get_vm_api_client,
	interfaces::{
		TApplySyncClient,
	}
};

///
pub fn memcpy( dst: *mut u8, src: *const u8, length: usize) -> *mut u8 {
    let mut _dst = unsafe {
        slice::from_raw_parts_mut(dst, length)
    };

    let _src = unsafe {
        slice::from_raw_parts(src, length)
    };
	_dst.copy_from_slice(_src);
	dst
}

///
pub fn eosio_memcpy( dst: *mut u8, src: *const u8, length: usize) -> *mut u8 {
	return memcpy(dst, src, length);
}

///
pub fn get_active_producers() -> Vec<Name> {
	let data = get_vm_api_client().get_active_producers().unwrap();
	let mut ret: Vec<Name> = vec![Name::default();data.len()/8];
	let mut i = 0usize;
	for v in &mut ret {
		v.unpack(data[i..].into());
		i += 8;
	}
	ret
}

//permissions.h
/// Checks if a transaction is authorized by a provided set of keys and permissions
pub fn check_transaction_authorization(
	trx: &Transaction,
	perms: &Vec<PermissionLevel>,
	pubkeys: &Vec<PublicKey>
) -> i32 {
	get_vm_api_client().check_transaction_authorization(trx.pack(), pubkeys.pack(), perms.pack()).unwrap()
}

/// Checks if a permission is authorized by a provided delay and a provided set of keys and permissions
pub fn check_permission_authorization(
	account: Name,
	permission: Name,
	perms: &Vec<PermissionLevel>,
	pubkeys: &Vec<PublicKey>,
	delay_us: u64
) -> i32 {
	let perms_data = perms.pack();
	let pubkeys_data = pubkeys.pack();
	get_vm_api_client().check_permission_authorization(account.n.into(), permission.n.into(), pubkeys_data, perms_data, delay_us.into()).unwrap()
}

///
pub fn get_permission_last_used(account: Name, permission: Name ) -> TimePoint {
	let elapsed = get_vm_api_client().get_permission_last_used(account.n.into(), permission.n.into()).unwrap();
	return TimePoint{elapsed: elapsed as u64};
}

///
pub fn get_account_creation_time(account: Name) -> TimePoint {
	let elapsed = get_vm_api_client().get_account_creation_time(account.n.into()).unwrap();
	return TimePoint{elapsed: elapsed as u64};
}

///
pub fn read_action_data() -> Vec<u8> {
	return get_vm_api_client().read_action_data().unwrap();
}

///
pub fn action_data_size() -> usize {
	get_vm_api_client().action_data_size().unwrap() as usize
}

///
pub fn require_recipient(name: Name) {
	get_vm_api_client().require_recipient(name.n.into()).unwrap()
}

///
pub fn require_auth(name: Name) {
	get_vm_api_client().require_auth(name.n.into()).unwrap()
}

///
pub fn has_auth(name: Name) -> bool {
	get_vm_api_client().has_auth(name.n.into()).unwrap()
}

///
pub fn require_auth2(name: Name, permission: Name) {
	get_vm_api_client().require_auth2(name.n.into(), permission.n.into()).unwrap()
}

///
pub fn is_account(name: Name) -> bool {
	get_vm_api_client().is_account(name.n.into()).unwrap()
}

///
pub fn send_inline(_serialized_action: &[u8]) {
	get_vm_api_client().send_inline(_serialized_action.to_vec()).unwrap();
}

///
pub fn send_context_free_inline(_serialized_action: &[u8]) {
	get_vm_api_client().send_context_free_inline(_serialized_action.to_vec()).unwrap();
}

///
pub fn publication_time() -> TimePoint {
	let elapsed = get_vm_api_client().publication_time().unwrap();
	return TimePoint{elapsed: elapsed.into()};
}

///
pub fn current_receiver() -> Name {
	let n = get_vm_api_client().current_receiver().unwrap();
	return Name{n: n.into()};
}

///
pub fn eosio_assert(test: bool, msg: &str) {
	get_vm_api_client().eosio_assert(test, msg.into()).unwrap()
}

///
pub fn eosio_assert_message(test: u32, msg: *const u8, msg_len: u32) {
	if test >= 1 {
		return;
	}

	let dst = unsafe {
        slice::from_raw_parts(msg, msg_len as usize)
    };

	get_vm_api_client().eosio_assert_message(false, dst.into()).unwrap();
}

///
pub fn eosio_assert_code(test: u32, code: u64) {
	let _test = match test {
		0 => false,
		_ => true,
	};
	get_vm_api_client().eosio_assert_code(_test, code.into()).unwrap()
}


///
pub fn check(test: bool, msg: &str) {
	if test {
		return
	}
	eosio_assert_message(0, msg.as_ptr(), msg.len() as u32);
}

///
pub fn eosio_exit(code: i32) {
	get_vm_api_client().eosio_exit(code).unwrap()
}

///
pub fn current_time() -> TimePoint {
	let elapsed = get_vm_api_client().current_time().unwrap().into();
	return TimePoint{elapsed: elapsed};
}

///
pub fn is_feature_activated(feature_digest: &Checksum256) -> bool {
	get_vm_api_client().is_feature_activated(feature_digest.data.into()).unwrap()
}

///
pub fn get_sender() -> Name {
	let n = get_vm_api_client().get_sender().unwrap().into();
	return Name{n: n};
}

/// return resource limits of ram, net, and cpu.
pub fn get_resource_limits(account: Name) -> (i64, i64, i64) {
	let ret = get_vm_api_client().get_resource_limits(account.n.into()).unwrap();
	(ret.ram_bytes.unwrap(), ret.net_weight.unwrap(), ret.cpu_weight.unwrap())
}

///
pub fn set_resource_limits(account: Name, ram_bytes: i64, net_weight: i64, cpu_weight: i64) {
	get_vm_api_client().set_resource_limits(account.n.into(), ram_bytes, net_weight, cpu_weight).unwrap()
}

//TODO:
///
pub fn set_proposed_producers(producer_keys: &Vec<ProducerKey>) -> i64 {
	let packed = producer_keys.pack();
	get_vm_api_client().set_proposed_producers(packed).unwrap()
}

//TODO:
///
pub fn set_proposed_producers_ex(producer_keys: &Vec<ProducerAuthority>) -> i64 {
	let packed = producer_keys.pack();
	get_vm_api_client().set_proposed_producers_ex(1u64.into(), packed).unwrap()
}

///
pub fn is_privileged(account: Name) -> bool {
	return get_vm_api_client().is_privileged(account.n.into()).unwrap();
}

///
pub fn set_privileged(account: Name, is_priv: bool) {
	get_vm_api_client().set_privileged(account.n.into(), is_priv).unwrap();
}

///
pub fn set_blockchain_parameters(params: &BlockchainParameters) {
	let data = params.pack();
	get_vm_api_client().set_blockchain_parameters_packed(data).unwrap();
}

///
pub fn get_blockchain_parameters() -> BlockchainParameters {
	let mut ret = BlockchainParameters::default();
	let data = get_vm_api_client().get_blockchain_parameters_packed().unwrap();
	ret.unpack(&data);
	return ret;
}

///
pub fn preactivate_feature(_feature_digest:  &Checksum256) {
}

///
pub fn send_deferred(sender_id: &Uint128, payer: Name, serialized_transaction: &[u8], replace_existing: u32) {
    let _sender_id = unsafe {
        slice::from_raw_parts(sender_id as *const Uint128 as *const u8, 16)
    };

	get_vm_api_client().send_deferred(_sender_id.into(), payer.n.into(), serialized_transaction.into(), replace_existing as i32).unwrap()
}

///
pub fn cancel_deferred(sender_id: &Uint128) -> i32 {
    let _sender_id = unsafe {
        slice::from_raw_parts(sender_id as *const Uint128 as *const u8, 16)
    };
	get_vm_api_client().cancel_deferred(_sender_id.into()).unwrap()
}

///
pub fn read_transaction() -> Transaction {
	let data = get_vm_api_client().read_transaction().unwrap();
	let mut ret = Transaction::default();
	ret.unpack(&data);
	return ret;
}

///
pub fn transaction_size() -> usize {
	get_vm_api_client().transaction_size().unwrap() as usize
}

///
pub fn tapos_block_num() -> i32 {
	get_vm_api_client().tapos_block_num().unwrap()
}

///
pub fn tapos_block_prefix() -> i32 {
	get_vm_api_client().tapos_block_prefix().unwrap()
}

///
pub fn expiration() -> u32 {
	get_vm_api_client().expiration().unwrap() as u32
}

///
pub fn get_action(tp: u32, index: u32) -> Vec<u8> {
	get_vm_api_client().get_action(tp as i32, index as i32).unwrap()
}

///
pub fn get_context_free_data(index: u32) -> Vec<u8> {
	get_vm_api_client().get_context_free_data(index as i32).unwrap()
}