actors_runtime/
lib.rs

1// Copyright 2019-2022 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4// TODO: disable everything else when not using runtime-wasm
5
6#[macro_use]
7extern crate lazy_static;
8// workaround for a compiler bug, see https://github.com/rust-lang/rust/issues/55779
9extern crate serde;
10
11use builtin::HAMT_BIT_WIDTH;
12use cid::Cid;
13use fvm_ipld_amt::Amt;
14use fvm_ipld_hamt::{BytesKey, Error as HamtError, Hamt};
15use fvm_shared::bigint::BigInt;
16use fvm_shared::blockstore::Blockstore;
17pub use fvm_shared::BLOCKS_PER_EPOCH as EXPECTED_LEADERS_PER_EPOCH;
18use serde::de::DeserializeOwned;
19use serde::Serialize;
20use unsigned_varint::decode::Error as UVarintError;
21pub use {fvm_ipld_amt, fvm_ipld_hamt};
22
23pub use self::actor_error::*;
24pub use self::builtin::*;
25pub use self::util::*;
26use crate::runtime::Runtime;
27
28pub mod actor_error;
29pub mod builtin;
30pub mod runtime;
31pub mod util;
32
33#[cfg(feature = "test_utils")]
34pub mod test_utils;
35
36#[macro_export]
37macro_rules! wasm_trampoline {
38    ($target:ty) => {
39        #[no_mangle]
40        #[cfg(target_arch = "wasm32")]
41        pub extern "C" fn invoke(param: u32) -> u32 {
42            $crate::runtime::fvm::trampoline::<$target>(param)
43        }
44    };
45}
46
47/// Map type to be used within actors. The underlying type is a HAMT.
48pub type Map<'bs, BS, V> = Hamt<&'bs BS, V, BytesKey>;
49
50/// Array type used within actors. The underlying type is an AMT.
51pub type Array<'bs, V, BS> = Amt<V, &'bs BS>;
52
53/// Deal weight
54pub type DealWeight = BigInt;
55
56/// Create a hamt with a custom bitwidth.
57#[inline]
58pub fn make_empty_map<BS, V>(store: &'_ BS, bitwidth: u32) -> Map<'_, BS, V>
59where
60    BS: Blockstore,
61    V: DeserializeOwned + Serialize,
62{
63    Map::<_, V>::new_with_bit_width(store, bitwidth)
64}
65
66/// Create a map with a root cid.
67#[inline]
68pub fn make_map_with_root<'bs, BS, V>(
69    root: &Cid,
70    store: &'bs BS,
71) -> Result<Map<'bs, BS, V>, HamtError>
72where
73    BS: Blockstore,
74    V: DeserializeOwned + Serialize,
75{
76    Map::<_, V>::load_with_bit_width(root, store, HAMT_BIT_WIDTH)
77}
78
79/// Create a map with a root cid.
80#[inline]
81pub fn make_map_with_root_and_bitwidth<'bs, BS, V>(
82    root: &Cid,
83    store: &'bs BS,
84    bitwidth: u32,
85) -> Result<Map<'bs, BS, V>, HamtError>
86where
87    BS: Blockstore,
88    V: DeserializeOwned + Serialize,
89{
90    Map::<_, V>::load_with_bit_width(root, store, bitwidth)
91}
92
93pub fn u64_key(k: u64) -> BytesKey {
94    let mut bz = unsigned_varint::encode::u64_buffer();
95    let slice = unsigned_varint::encode::u64(k, &mut bz);
96    slice.to_vec().into()
97}
98
99pub fn parse_uint_key(s: &[u8]) -> Result<u64, UVarintError> {
100    let (v, _) = unsigned_varint::decode::u64(s)?;
101    Ok(v)
102}