eosio_chain/vmapi/off_chain/
eosio.rs

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