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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
// Copyright 2018 Cargill Incorporated
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![allow(renamed_and_removed_lints)]

mod externs;
pub mod log;
pub mod protocol;
pub mod protos;

use std::collections::HashMap;
use std::error::Error;
use std::string::FromUtf8Error;

pub use crate::externs::{WasmPtr, WasmPtrList};

pub struct Header {
    signer: String,
}

impl Header {
    pub fn new(signer: String) -> Header {
        Header { signer }
    }

    pub fn get_signer_public_key(&self) -> &str {
        &self.signer
    }
}

pub struct TpProcessRequest<'a> {
    payload: Vec<u8>,
    header: &'a mut Header,
    signature: String,
}

impl<'a> TpProcessRequest<'a> {
    pub fn new(payload: Vec<u8>, header: &'a mut Header, signature: String) -> TpProcessRequest {
        TpProcessRequest {
            payload,
            header,
            signature,
        }
    }

    pub fn get_payload(&self) -> &[u8] {
        &self.payload
    }

    pub fn get_header(&self) -> &Header {
        self.header
    }

    pub fn get_signature(&self) -> String {
        self.signature.to_string()
    }
}

pub trait TransactionContext {
    #[deprecated(
        since = "0.2.0",
        note = "please use `get_state_entry` or `get_state_entries` instead"
    )]
    /// get_state queries the validator state for data at each of the
    /// addresses in the given list. The addresses that have been set
    /// are returned. get_state is deprecated, please use get_state_entry or get_state_entries
    /// instead
    ///
    /// # Arguments
    ///
    /// * `addresses` - the addresses to fetch
    fn get_state(&self, addresses: &[String]) -> Result<Vec<(String, Vec<u8>)>, WasmSdkError> {
        self.get_state_entries(addresses)
    }
    /// get_state_entry queries the validator state for data at the
    /// address given. If the address is set, the data is returned.
    ///
    /// # Arguments
    ///
    /// * `address` - the address to fetch
    fn get_state_entry(&self, address: &str) -> Result<Option<Vec<u8>>, WasmSdkError> {
        Ok(self
            .get_state_entries(&[address.to_string()])?
            .into_iter()
            .map(|(_, val)| val)
            .next())
    }

    /// get_state_entries queries the validator state for data at each of the
    /// addresses in the given list. The addresses that have been set
    /// are returned.
    ///
    /// # Arguments
    ///
    /// * `addresses` - the addresses to fetch
    fn get_state_entries(
        &self,
        addresses: &[String],
    ) -> Result<Vec<(String, Vec<u8>)>, WasmSdkError>;

    #[deprecated(
        since = "0.2.0",
        note = "please use `set_state_entry` or `set_state_entries` instead"
    )]
    /// set_state requests that each address in the provided map be
    /// set in validator state to its corresponding value. set_state is deprecated, please use
    /// set_state_entry to set_state_entries instead
    ///
    /// # Arguments
    ///
    /// * `entries` - entries are a hashmap where the key is an address and value is the data
    fn set_state(&self, entries: HashMap<String, Vec<u8>>) -> Result<(), WasmSdkError> {
        let state_entries: Vec<(String, Vec<u8>)> = entries.into_iter().collect();
        self.set_state_entries(state_entries)
    }

    /// set_state_entry requests that the provided address is set in the validator state to its
    /// corresponding value.
    ///
    /// # Arguments
    ///
    /// * `address` - address of where to store the data
    /// * `data` - payload is the data to store at the address
    fn set_state_entry(&self, address: String, data: Vec<u8>) -> Result<(), WasmSdkError> {
        self.set_state_entries(vec![(address, data)])
    }

    /// set_state_entries requests that each address in the provided map be
    /// set in validator state to its corresponding value.
    ///
    /// # Arguments
    ///
    /// * `entries` - entries are a hashmap where the key is an address and value is the data
    fn set_state_entries(&self, entries: Vec<(String, Vec<u8>)>) -> Result<(), WasmSdkError>;

    /// delete_state requests that each of the provided addresses be unset
    /// in validator state. A list of successfully deleted addresses is returned.
    /// delete_state is deprecated, please use delete_state_entry to delete_state_entries instead
    ///
    /// # Arguments
    ///
    /// * `addresses` - the addresses to delete
    #[deprecated(
        since = "0.2.0",
        note = "please use `delete_state_entry` or `delete_state_entries` instead"
    )]
    fn delete_state(&self, addresses: &[String]) -> Result<Vec<String>, WasmSdkError> {
        self.delete_state_entries(addresses)
    }

    /// delete_state_entry requests that the provided address be unset
    /// in validator state. A list of successfully deleted addresses
    /// is returned.
    ///
    /// # Arguments
    ///
    /// * `address` - the address to delete
    fn delete_state_entry(&self, address: &str) -> Result<Option<String>, WasmSdkError> {
        Ok(self
            .delete_state_entries(&[address.to_string()])?
            .into_iter()
            .next())
    }

    /// delete_state_entries requests that each of the provided addresses be unset
    /// in validator state. A list of successfully deleted addresses
    /// is returned.
    ///
    /// # Arguments
    ///
    /// * `addresses` - the addresses to delete
    fn delete_state_entries(&self, addresses: &[String]) -> Result<Vec<String>, WasmSdkError>;
}

#[derive(Default)]
pub struct SabreTransactionContext {}

impl SabreTransactionContext {
    pub fn new() -> SabreTransactionContext {
        SabreTransactionContext {}
    }
}

impl TransactionContext for SabreTransactionContext {
    fn get_state_entries(
        &self,
        addresses: &[String],
    ) -> Result<Vec<(String, Vec<u8>)>, WasmSdkError> {
        unsafe {
            if addresses.is_empty() {
                return Err(WasmSdkError::InvalidTransaction("No address to get".into()));
            }
            let head = &addresses[0];
            let header_address_buffer = WasmBuffer::new(head.as_bytes())?;
            externs::create_collection(header_address_buffer.to_raw());

            for addr in addresses[1..].iter() {
                let wasm_buffer = WasmBuffer::new(addr.as_bytes())?;
                externs::add_to_collection(header_address_buffer.to_raw(), wasm_buffer.to_raw());
            }

            let results =
                WasmBuffer::from_list(externs::get_state(header_address_buffer.to_raw()))?;
            let mut result_vec = Vec::new();

            if (result_vec.len() % 2) != 0 {
                return Err(WasmSdkError::InvalidTransaction(
                    "Get state returned incorrect data fmt".into(),
                ));
            }

            for result in results.chunks(2) {
                let addr = String::from_utf8(result[0].to_bytes())?;
                result_vec.push((addr, result[1].to_bytes()))
            }
            Ok(result_vec)
        }
    }

    fn set_state_entries(&self, entries: Vec<(String, Vec<u8>)>) -> Result<(), WasmSdkError> {
        unsafe {
            let mut entries_iter = entries.iter();
            let (head, head_data) = match entries_iter.next() {
                Some((addr, data)) => (addr, data),
                None => return Err(WasmSdkError::InvalidTransaction("No entries to set".into())),
            };

            let header_address_buffer = WasmBuffer::new(head.as_bytes())?;
            externs::create_collection(header_address_buffer.to_raw());

            let wasm_head_data_buffer = WasmBuffer::new(head_data)?;
            externs::add_to_collection(
                header_address_buffer.to_raw(),
                wasm_head_data_buffer.to_raw(),
            );

            for (address, data) in entries_iter {
                let wasm_addr_buffer = WasmBuffer::new(address.as_bytes())?;
                externs::add_to_collection(
                    header_address_buffer.to_raw(),
                    wasm_addr_buffer.to_raw(),
                );

                let wasm_data_buffer = WasmBuffer::new(data)?;
                externs::add_to_collection(
                    header_address_buffer.to_raw(),
                    wasm_data_buffer.to_raw(),
                );
            }

            let result = externs::set_state(header_address_buffer.to_raw());

            if result == 0 {
                return Err(WasmSdkError::InvalidTransaction(
                    "Unable to set state".into(),
                ));
            }
        }
        Ok(())
    }

    fn delete_state_entries(&self, addresses: &[String]) -> Result<Vec<String>, WasmSdkError> {
        unsafe {
            if addresses.is_empty() {
                return Err(WasmSdkError::InvalidTransaction(
                    "No address to delete".into(),
                ));
            }
            let head = &addresses[0];
            let header_address_buffer = WasmBuffer::new(head.as_bytes())?;
            externs::create_collection(header_address_buffer.to_raw());

            for addr in addresses[1..].iter() {
                let wasm_buffer = WasmBuffer::new(addr.as_bytes())?;
                externs::add_to_collection(header_address_buffer.to_raw(), wasm_buffer.to_raw());
            }
            let result =
                WasmBuffer::from_list(externs::delete_state(header_address_buffer.to_raw()))?;
            let mut result_vec = Vec::new();
            for i in result {
                let addr = String::from_utf8(i.data)?;
                result_vec.push(addr);
            }
            Ok(result_vec)
        }
    }
}

// Mimics the sawtooth sdk TransactionHandler
pub trait TransactionHandler {
    fn family_name(&self) -> String;
    fn family_versions(&self) -> Vec<String>;
    fn namespaces(&self) -> Vec<String>;
    fn apply(
        &self,
        request: &TpProcessRequest,
        context: &mut dyn TransactionContext,
    ) -> Result<(), ApplyError>;
}

pub fn invoke_smart_permission(
    contract_addr: String,
    name: String,
    roles: Vec<String>,
    org_id: String,
    public_key: String,
    payload: &[u8],
) -> Result<i32, WasmSdkError> {
    unsafe {
        if roles.is_empty() {
            return Err(WasmSdkError::InvalidTransaction("No roles ".into()));
        }
        let head = &roles[0];
        let header_role_buffer = WasmBuffer::new(head.as_bytes())?;
        externs::create_collection(header_role_buffer.to_raw());

        for role in roles[1..].iter() {
            let wasm_buffer = WasmBuffer::new(role.as_bytes())?;
            externs::add_to_collection(header_role_buffer.to_raw(), wasm_buffer.to_raw());
        }
        let contract_addr_buffer = WasmBuffer::new(contract_addr.as_bytes())?;
        let name_buffer = WasmBuffer::new(name.as_bytes())?;
        let org_id_buffer = WasmBuffer::new(org_id.to_string().as_bytes())?;
        let public_key_buffer = WasmBuffer::new(public_key.to_string().as_bytes())?;
        let payload_buffer = WasmBuffer::new(payload)?;

        Ok(externs::invoke_smart_permission(
            contract_addr_buffer.to_raw(),
            name_buffer.to_raw(),
            header_role_buffer.to_raw(),
            org_id_buffer.to_raw(),
            public_key_buffer.to_raw(),
            payload_buffer.to_raw(),
        ))
    }
}

/// -1: Failed to deserialize payload
/// -2: Failed to deserialize signer
/// -3: apply returned InvalidTransaction
/// -4: apply returned InternalError
pub unsafe fn execute_entrypoint<F>(
    payload_ptr: WasmPtr,
    signer_ptr: WasmPtr,
    signature_ptr: WasmPtr,
    apply: F,
) -> i32
where
    F: Fn(&TpProcessRequest, &mut dyn TransactionContext) -> Result<bool, ApplyError>,
{
    let payload = if let Ok(i) = WasmBuffer::from_raw(payload_ptr) {
        i.to_bytes()
    } else {
        return -1;
    };

    let signature = if let Ok(i) = WasmBuffer::from_raw(signature_ptr) {
        match i.to_string() {
            Ok(s) => s,
            Err(_) => return -2,
        }
    } else {
        return -1;
    };

    let signer = if let Ok(i) = WasmBuffer::from_raw(signer_ptr) {
        match i.to_string() {
            Ok(s) => s,
            Err(_) => return -2,
        }
    } else {
        return -1;
    };

    let mut header = Header::new(signer);
    match apply(
        &TpProcessRequest::new(payload, &mut header, signature),
        &mut SabreTransactionContext::new(),
    ) {
        Ok(r) => {
            if r {
                1
            } else {
                0
            }
        }
        Err(ApplyError::InvalidTransaction(_)) => -3,
        Err(ApplyError::InternalError(_)) => -4,
    }
}

pub struct Request {
    roles: Vec<String>,
    org_id: String,
    public_key: String,
    payload: Vec<u8>,
}

impl Request {
    pub fn new(
        roles: Vec<String>,
        org_id: String,
        public_key: String,
        payload: Vec<u8>,
    ) -> Request {
        Request {
            roles,
            org_id,
            public_key,
            payload,
        }
    }

    pub fn get_roles(&self) -> Vec<String> {
        self.roles.clone()
    }

    pub fn get_org_id(&self) -> String {
        self.org_id.clone()
    }

    pub fn get_public_key(&self) -> String {
        self.public_key.clone()
    }

    pub fn get_state(&self, address: String) -> Result<Option<Vec<u8>>, WasmSdkError> {
        unsafe {
            let wasm_buffer = WasmBuffer::new(address.as_bytes())?;
            ptr_to_vec(externs::get_state(wasm_buffer.to_raw()))
        }
    }

    pub fn get_payload<T>(&self) -> Vec<u8> {
        self.payload.clone()
    }
}

/// Error Codes:
///
/// -1: Failed to deserialize roles
/// -2: Failed to deserialize org_id
/// -3: Failed to deserialize public_key
/// -4: Failed to deserialize payload
/// -5: Failed to execute smart permission
/// -6: StateSetError
/// -7: AllocError
/// -8: MemoryRetrievalError
/// -9: Utf8EncodeError
/// -10: ProtobufError
///
pub unsafe fn execute_smart_permission_entrypoint<F>(
    roles_ptr: WasmPtrList,
    org_id_ptr: WasmPtr,
    public_key_ptr: WasmPtr,
    payload_ptr: WasmPtr,
    has_permission: F,
) -> i32
where
    F: Fn(Request) -> Result<bool, WasmSdkError>,
{
    let roles = if let Ok(i) = WasmBuffer::from_list(roles_ptr) {
        let results: Vec<Result<String, WasmSdkError>> = i.iter().map(|x| x.to_string()).collect();

        if results.iter().any(|x| x.is_err()) {
            return -1;
        } else {
            results.into_iter().map(|x| x.unwrap()).collect()
        }
    } else {
        return -1;
    };

    let org_id = if let Ok(i) = WasmBuffer::from_raw(org_id_ptr) {
        match i.to_string() {
            Ok(s) => s,
            Err(_) => {
                return -2;
            }
        }
    } else {
        return -2;
    };

    let public_key = if let Ok(i) = WasmBuffer::from_raw(public_key_ptr) {
        match i.to_string() {
            Ok(s) => s,
            Err(_) => {
                return -3;
            }
        }
    } else {
        return -3;
    };

    let payload = if let Ok(i) = WasmBuffer::from_raw(payload_ptr) {
        i.to_bytes()
    } else {
        return -4;
    };

    match has_permission(Request::new(roles, org_id, public_key, payload)) {
        Ok(r) => {
            if r {
                1
            } else {
                0
            }
        }
        Err(WasmSdkError::StateSetError(_)) => -5,
        Err(WasmSdkError::AllocError(_)) => -6,
        Err(WasmSdkError::MemoryWriteError(_)) => -7,
        Err(WasmSdkError::MemoryRetrievalError(_)) => -8,
        Err(WasmSdkError::Utf8EncodeError(_)) => -9,
        Err(WasmSdkError::ProtobufError(_)) => -10,
        Err(WasmSdkError::InvalidTransaction(_)) => -11,
        Err(WasmSdkError::InternalError(_)) => -12,
    }
}

/// A WasmBuffer is a wrapper around a wasm pointer.
///
/// It contains a raw wasm pointer to location in executor
/// memory and a bytes repesentation of it's contents.
///
/// It offers methods for accessing the data stored at the
/// location referenced by the raw pointer.
///
pub struct WasmBuffer {
    raw: WasmPtr,
    data: Vec<u8>,
}

impl WasmBuffer {
    pub unsafe fn new(buffer: &[u8]) -> Result<WasmBuffer, WasmSdkError> {
        let raw = externs::alloc(buffer.len());

        if raw < 0 {
            return Err(WasmSdkError::AllocError(
                "Failed to allocate host memory".into(),
            ));
        }

        for (i, byte) in buffer.iter().enumerate() {
            if externs::write_byte(raw, i as u32, *byte) < 0 {
                return Err(WasmSdkError::MemoryWriteError(
                    "Failed to write data to host memory".into(),
                ));
            }
        }

        Ok(WasmBuffer {
            raw,
            data: buffer.to_vec(),
        })
    }

    pub unsafe fn from_raw(raw: WasmPtr) -> Result<WasmBuffer, WasmSdkError> {
        let data = ptr_to_vec(raw)?.unwrap_or_default();
        Ok(WasmBuffer { raw, data })
    }

    pub unsafe fn from_list(ptr: WasmPtrList) -> Result<Vec<WasmBuffer>, WasmSdkError> {
        let mut wasm_buffers = Vec::new();

        if ptr >= 0 {
            for i in 0..externs::get_ptr_collection_len(ptr) {
                let ptr = externs::get_ptr_from_collection(ptr, i as u32);

                if ptr < 0 {
                    return Err(WasmSdkError::MemoryRetrievalError(
                        "pointer not found".into(),
                    ));
                }
                wasm_buffers.push(WasmBuffer::from_raw(ptr)?);
            }
        }

        Ok(wasm_buffers)
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        self.data.clone()
    }

    pub fn to_raw(&self) -> WasmPtr {
        self.raw
    }

    pub fn to_string(&self) -> Result<String, WasmSdkError> {
        String::from_utf8(self.data.clone()).map_err(WasmSdkError::from)
    }
}

#[derive(Debug)]
pub enum WasmSdkError {
    InvalidTransaction(String),
    InternalError(String),
    StateSetError(String),
    AllocError(String),
    MemoryWriteError(String),
    MemoryRetrievalError(String),
    Utf8EncodeError(FromUtf8Error),
    ProtobufError(protobuf::ProtobufError),
}

impl std::fmt::Display for WasmSdkError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            WasmSdkError::InvalidTransaction(ref s) => write!(f, "InvalidTransactio: {}", s),
            WasmSdkError::InternalError(ref s) => write!(f, "InternalError: {}", s),
            WasmSdkError::StateSetError(ref s) => write!(f, "StateSetError: {}", s),
            WasmSdkError::AllocError(ref s) => write!(f, "AllocError: {}", s),
            WasmSdkError::MemoryWriteError(ref s) => write!(f, "MemoryWriteError: {}", s),
            WasmSdkError::MemoryRetrievalError(ref s) => write!(f, "MemoryRetrievalError: {}", s),
            WasmSdkError::Utf8EncodeError(ref err) => {
                write!(f, "Utf8EncodeError: {}", err.description())
            }
            WasmSdkError::ProtobufError(ref err) => {
                write!(f, "ProtobufError: {}", err.description())
            }
        }
    }
}

impl From<FromUtf8Error> for WasmSdkError {
    fn from(e: FromUtf8Error) -> Self {
        WasmSdkError::Utf8EncodeError(e)
    }
}

impl From<protobuf::ProtobufError> for WasmSdkError {
    fn from(e: protobuf::ProtobufError) -> Self {
        WasmSdkError::ProtobufError(e)
    }
}

#[derive(Debug)]
pub enum ApplyError {
    InvalidTransaction(String),
    InternalError(String),
}

impl std::fmt::Display for ApplyError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            ApplyError::InvalidTransaction(ref s) => write!(f, "InvalidTransaction: {}", s),
            ApplyError::InternalError(ref s) => write!(f, "InternalError: {}", s),
        }
    }
}

impl From<WasmSdkError> for ApplyError {
    fn from(e: WasmSdkError) -> Self {
        match e {
            WasmSdkError::InternalError(..) => ApplyError::InternalError(format!("{}", e)),
            _ => ApplyError::InvalidTransaction(format!("{}", e)),
        }
    }
}

unsafe fn ptr_to_vec(ptr: WasmPtr) -> Result<Option<Vec<u8>>, WasmSdkError> {
    let mut vec = Vec::new();

    for i in 0..externs::get_ptr_len(ptr) {
        vec.push(externs::read_byte(ptr as isize + i));
    }

    if vec.is_empty() {
        return Ok(None);
    }
    Ok(Some(vec))
}

#[derive(PartialOrd, PartialEq, Copy, Clone)]
pub enum LogLevel {
    Trace,
    Debug,
    Info,
    Warn,
    Error,
}

pub fn log_message(log_level: LogLevel, log_string: String) {
    unsafe {
        // WasmBuffer was created properly, log message otherwise ignore
        if let Ok(log_buffer) = WasmBuffer::new(log_string.as_bytes()) {
            match log_level {
                LogLevel::Trace => externs::log_buffer(4 as i32, log_buffer.to_raw()),
                LogLevel::Debug => externs::log_buffer(3 as i32, log_buffer.to_raw()),
                LogLevel::Info => externs::log_buffer(2 as i32, log_buffer.to_raw()),
                LogLevel::Warn => externs::log_buffer(1 as i32, log_buffer.to_raw()),
                LogLevel::Error => externs::log_buffer(0 as i32, log_buffer.to_raw()),
            };
        }
    }
}

pub fn log_level() -> LogLevel {
    unsafe {
        match externs::log_level() {
            4 => LogLevel::Trace,
            3 => LogLevel::Debug,
            2 => LogLevel::Info,
            1 => LogLevel::Warn,
            _ => LogLevel::Error,
        }
    }
}

pub fn log_enabled(lvl: LogLevel) -> bool {
    lvl >= log_level()
}