rust_chain/vmapi/off_chain/
eosio.rs

1use crate::structs::*;
2
3use crate::transaction::{
4	Transaction,
5};
6
7use crate::action::{
8	PermissionLevel,
9};
10
11use crate::privileged::{
12	BlockchainParameters,
13};
14
15use crate::name::{
16	Name,
17};
18
19use crate::serializer::{
20	Packer,
21	Encoder
22};
23
24use crate::{
25	// vec,
26	vec::Vec,
27};
28
29use core::slice;
30
31use chaintester::{
32    get_vm_api_client,
33	interfaces::{
34		TApplySyncClient,
35	}
36};
37
38///
39pub fn memcpy( dst: *mut u8, src: *const u8, length: usize) -> *mut u8 {
40    let mut _dst = unsafe {
41        slice::from_raw_parts_mut(dst, length)
42    };
43
44    let _src = unsafe {
45        slice::from_raw_parts(src, length)
46    };
47	_dst.copy_from_slice(_src);
48	dst
49}
50
51///
52pub fn eosio_memcpy( dst: *mut u8, src: *const u8, length: usize) {
53	memcpy(dst, src, length);
54}
55
56///
57pub fn slice_copy( dst: &mut [u8], src: &[u8]) {
58	check(dst.len() == src.len(), "copy_slice: length not the same!");
59	eosio_memcpy(dst.as_mut_ptr(), src.as_ptr(), dst.len());
60}
61
62///
63pub fn get_active_producers() -> Vec<Name> {
64	let ret = get_vm_api_client().get_active_producers();
65	let data = ret.unwrap();
66	let mut ret: Vec<Name> = vec![Name::default();data.len()/8];
67	let mut i = 0usize;
68	for v in &mut ret {
69		v.unpack(data[i..].into());
70		i += 8;
71	}
72	ret
73}
74
75//permissions.h
76/// Checks if a transaction is authorized by a provided set of keys and permissions
77pub fn check_transaction_authorization(
78	trx: &Transaction,
79	perms: &Vec<PermissionLevel>,
80	pubkeys: &Vec<PublicKey>
81) -> i32 {
82	let ret = get_vm_api_client().check_transaction_authorization(Encoder::pack(trx), Encoder::pack(pubkeys), Encoder::pack(perms));
83    ret.unwrap()
84}
85
86/// Checks if a permission is authorized by a provided delay and a provided set of keys and permissions
87pub fn check_permission_authorization(
88	account: Name,
89	permission: Name,
90	perms: &Vec<PermissionLevel>,
91	pubkeys: &Vec<PublicKey>,
92	delay_us: u64
93) -> i32 {
94	let perms_data = Encoder::pack(perms);
95	let pubkeys_data = Encoder::pack(pubkeys);
96	let ret = get_vm_api_client().check_permission_authorization(account.n.into(), permission.n.into(), pubkeys_data, perms_data, delay_us.into());
97    ret.unwrap()
98}
99
100///
101pub fn get_permission_last_used(account: Name, permission: Name ) -> TimePoint {
102	let ret = get_vm_api_client().get_permission_last_used(account.n.into(), permission.n.into());
103    let elapsed = ret.unwrap();
104	return TimePoint{elapsed: elapsed as u64};
105}
106
107///
108pub fn get_account_creation_time(account: Name) -> TimePoint {
109	let ret = get_vm_api_client().get_account_creation_time(account.n.into());
110    let elapsed = ret.unwrap();
111	return TimePoint{elapsed: elapsed as u64};
112}
113
114///
115pub fn read_action_data() -> Vec<u8> {
116	let ret = get_vm_api_client().read_action_data();
117    ret.unwrap()
118}
119
120///
121pub fn action_data_size() -> usize {
122	let ret = get_vm_api_client().action_data_size();
123    ret.unwrap() as usize
124}
125
126///
127pub fn require_recipient(name: Name) {
128	let ret = get_vm_api_client().require_recipient(name.n.into());
129    ret.unwrap()
130}
131
132///
133pub fn require_auth(name: Name) {
134	let ret = get_vm_api_client().require_auth(name.n.into());
135    ret.unwrap()
136}
137
138///
139pub fn has_auth(name: Name) -> bool {
140	let ret = get_vm_api_client().has_auth(name.n.into());
141    ret.unwrap()
142}
143
144///
145pub fn require_auth2(name: Name, permission: Name) {
146	let ret = get_vm_api_client().require_auth2(name.n.into(), permission.n.into());
147    ret.unwrap()
148}
149
150///
151pub fn is_account(name: Name) -> bool {
152	let ret = get_vm_api_client().is_account(name.n.into());
153    ret.unwrap()
154}
155
156///
157pub fn send_inline(_serialized_action: &[u8]) {
158	let ret = get_vm_api_client().send_inline(_serialized_action.to_vec());
159    ret.unwrap();
160}
161
162///
163pub fn send_context_free_inline(_serialized_action: &[u8]) {
164	let ret = get_vm_api_client().send_context_free_inline(_serialized_action.to_vec());
165    ret.unwrap();
166}
167
168///
169pub fn publication_time() -> TimePoint {
170	let ret = get_vm_api_client().publication_time();
171    let elapsed = ret.unwrap();
172	return TimePoint{elapsed: elapsed.into()};
173}
174
175///
176pub fn current_receiver() -> Name {
177	let ret = get_vm_api_client().current_receiver();
178    let n = ret.unwrap();
179	return Name{n: n.into()};
180}
181
182///
183pub fn eosio_assert(test: bool, msg: &str) {
184	let ret = get_vm_api_client().eosio_assert(test, msg.into());
185    ret.unwrap()
186}
187
188///
189pub fn eosio_assert_message(test: u32, msg: *const u8, msg_len: u32) {
190	if test >= 1 {
191		return;
192	}
193
194	let dst = unsafe {
195        slice::from_raw_parts(msg, msg_len as usize)
196    };
197
198	if !get_vm_api_client().is_in_apply() {
199		panic!("{}", String::from_utf8(dst.to_vec()).unwrap());
200	}
201
202	let ret = get_vm_api_client().eosio_assert_message(false, dst.into());
203    ret.unwrap();
204}
205
206///
207pub fn eosio_assert_code(test: u32, code: u64) {
208	let _test = match test {
209		0 => false,
210		_ => true,
211	};
212	let ret = get_vm_api_client().eosio_assert_code(_test, code.into());
213    ret.unwrap()
214}
215
216
217///
218pub fn check(test: bool, msg: &str) {
219	if test {
220		return
221	}
222	eosio_assert_message(0, msg.as_ptr(), msg.len() as u32);
223}
224
225///
226pub fn eosio_exit(code: i32) {
227	let ret = get_vm_api_client().eosio_exit(code);
228    ret.unwrap()
229}
230
231///
232pub fn current_time() -> TimePoint {
233	let ret = get_vm_api_client().current_time();
234    let elapsed = ret.unwrap().into();
235	return TimePoint{elapsed: elapsed};
236}
237
238///
239pub fn is_feature_activated(feature_digest: &Checksum256) -> bool {
240	let ret = get_vm_api_client().is_feature_activated(feature_digest.data.into());
241    ret.unwrap()
242}
243
244///
245pub fn get_sender() -> Name {
246	let ret = get_vm_api_client().get_sender();
247    let n = ret.unwrap().into();
248	return Name{n: n};
249}
250
251/// return resource limits of ram, net, and cpu.
252pub fn get_resource_limits(account: Name) -> (i64, i64, i64) {
253	let _ret = get_vm_api_client().get_resource_limits(account.n.into());
254    let ret = _ret.unwrap();
255	(
256		ret.ram_bytes.unwrap(),
257	    ret.net_weight.unwrap(),
258    	ret.cpu_weight.unwrap(),
259    )
260}
261
262///
263pub fn set_resource_limits(account: Name, ram_bytes: i64, net_weight: i64, cpu_weight: i64) {
264	let ret = get_vm_api_client().set_resource_limits(account.n.into(), ram_bytes, net_weight, cpu_weight);
265    ret.unwrap()
266}
267
268//TODO:
269///
270pub fn set_proposed_producers(producer_keys: &Vec<ProducerKey>) -> i64 {
271	let packed = Encoder::pack(producer_keys);
272	let ret = get_vm_api_client().set_proposed_producers(packed);
273    ret.unwrap()
274}
275
276//TODO:
277///
278pub fn set_proposed_producers_ex(producer_keys: &Vec<ProducerAuthority>) -> i64 {
279	let packed = Encoder::pack(producer_keys);
280	let ret = get_vm_api_client().set_proposed_producers_ex(1u64.into(), packed);
281    ret.unwrap()
282}
283
284///
285pub fn is_privileged(account: Name) -> bool {
286	let ret = get_vm_api_client().is_privileged(account.n.into());
287    ret.unwrap()
288}
289
290///
291pub fn set_privileged(account: Name, is_priv: bool) {
292	let ret = get_vm_api_client().set_privileged(account.n.into(), is_priv);
293    ret.unwrap();
294}
295
296///
297pub fn set_blockchain_parameters(params: &BlockchainParameters) {
298	let data = Encoder::pack(params);
299	let ret = get_vm_api_client().set_blockchain_parameters_packed(data);
300    ret.unwrap();
301}
302
303///
304pub fn get_blockchain_parameters() -> BlockchainParameters {
305	let mut params = BlockchainParameters::default();
306	let ret = get_vm_api_client().get_blockchain_parameters_packed();
307    let data = ret.unwrap();
308	params.unpack(&data);
309	return params;
310}
311
312///
313pub fn preactivate_feature(_feature_digest:  &Checksum256) {
314}
315
316///
317pub fn send_deferred(sender_id: &Uint128, payer: Name, serialized_transaction: &[u8], replace_existing: u32) {
318    let _sender_id = unsafe {
319        slice::from_raw_parts(sender_id as *const Uint128 as *const u8, 16)
320    };
321
322	let ret = get_vm_api_client().send_deferred(_sender_id.into(), payer.n.into(), serialized_transaction.into(), replace_existing as i32);
323    ret.unwrap()
324}
325
326///
327pub fn cancel_deferred(sender_id: &Uint128) -> i32 {
328    let _sender_id = unsafe {
329        slice::from_raw_parts(sender_id as *const Uint128 as *const u8, 16)
330    };
331	let ret = get_vm_api_client().cancel_deferred(_sender_id.into());
332    ret.unwrap()
333}
334
335///
336pub fn read_transaction() -> Transaction {
337	let ret = get_vm_api_client().read_transaction();
338    let data = ret.unwrap();
339	let mut ret = Transaction::default();
340	ret.unpack(&data);
341	return ret;
342}
343
344///
345pub fn transaction_size() -> usize {
346	let ret = get_vm_api_client().transaction_size();
347    ret.unwrap() as usize
348}
349
350///
351pub fn tapos_block_num() -> u32 {
352	let ret = get_vm_api_client().tapos_block_num();
353    ret.unwrap() as u32
354}
355
356///
357pub fn tapos_block_prefix() -> u32 {
358	let ret = get_vm_api_client().tapos_block_prefix();
359    ret.unwrap() as u32
360}
361
362///
363pub fn expiration() -> u32 {
364	let ret = get_vm_api_client().expiration();
365    ret.unwrap() as u32
366}
367
368///
369pub fn get_action(tp: u32, index: u32) -> Vec<u8> {
370	let ret = get_vm_api_client().get_action(tp as i32, index as i32);
371    ret.unwrap()
372}
373
374///
375pub fn get_context_free_data(index: u32) -> Vec<u8> {
376	let ret = get_vm_api_client().get_context_free_data(index as i32);
377    ret.unwrap()
378}