fil_actors_runtime/
lib.rs

1// Copyright 2019-2022 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4use builtin::HAMT_BIT_WIDTH;
5use cid::Cid;
6use fvm_ipld_amt::Amt;
7use fvm_ipld_blockstore::Blockstore;
8use fvm_ipld_hamt::{BytesKey, Error as HamtError, Hamt};
9use fvm_shared::address::Address;
10use fvm_shared::bigint::BigInt;
11pub use fvm_shared::BLOCKS_PER_EPOCH as EXPECTED_LEADERS_PER_EPOCH;
12use serde::de::DeserializeOwned;
13use serde::Serialize;
14use unsigned_varint::decode::Error as UVarintError;
15pub use {fvm_ipld_amt, fvm_ipld_hamt};
16
17pub use self::actor_error::*;
18pub use self::builtin::*;
19pub use self::util::*;
20use crate::runtime::Runtime;
21
22#[cfg(feature = "fil-actor")]
23use crate::runtime::hash_algorithm::FvmHashSha256;
24
25#[cfg(not(feature = "fil-actor"))]
26use fvm_ipld_hamt::Sha256;
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        pub extern "C" fn invoke(param: u32) -> u32 {
41            $crate::runtime::fvm::trampoline::<$target>(param)
42        }
43    };
44}
45
46#[cfg(feature = "fil-actor")]
47type Hasher = FvmHashSha256;
48
49#[cfg(not(feature = "fil-actor"))]
50type Hasher = Sha256;
51
52/// Map type to be used within actors. The underlying type is a HAMT.
53pub type Map<'bs, BS, V> = Hamt<&'bs BS, V, BytesKey, Hasher>;
54
55/// Array type used within actors. The underlying type is an AMT.
56pub type Array<'bs, V, BS> = Amt<V, &'bs BS>;
57
58/// Deal weight
59pub type DealWeight = BigInt;
60
61/// Create a hamt with a custom bitwidth.
62#[inline]
63pub fn make_empty_map<BS, V>(store: &'_ BS, bitwidth: u32) -> Map<'_, BS, V>
64where
65    BS: Blockstore,
66    V: DeserializeOwned + Serialize,
67{
68    Map::<_, V>::new_with_bit_width(store, bitwidth)
69}
70
71/// Create a map with a root cid.
72#[inline]
73pub fn make_map_with_root<'bs, BS, V>(
74    root: &Cid,
75    store: &'bs BS,
76) -> Result<Map<'bs, BS, V>, HamtError>
77where
78    BS: Blockstore,
79    V: DeserializeOwned + Serialize,
80{
81    Map::<_, V>::load_with_bit_width(root, store, HAMT_BIT_WIDTH)
82}
83
84/// Create a map with a root cid.
85#[inline]
86pub fn make_map_with_root_and_bitwidth<'bs, BS, V>(
87    root: &Cid,
88    store: &'bs BS,
89    bitwidth: u32,
90) -> Result<Map<'bs, BS, V>, HamtError>
91where
92    BS: Blockstore,
93    V: DeserializeOwned + Serialize,
94{
95    Map::<_, V>::load_with_bit_width(root, store, bitwidth)
96}
97
98pub fn u64_key(k: u64) -> BytesKey {
99    let mut bz = unsigned_varint::encode::u64_buffer();
100    let slice = unsigned_varint::encode::u64(k, &mut bz);
101    slice.into()
102}
103
104pub fn parse_uint_key(s: &[u8]) -> Result<u64, UVarintError> {
105    let (v, _) = unsigned_varint::decode::u64(s)?;
106    Ok(v)
107}
108
109pub trait Keyer {
110    fn key(&self) -> BytesKey;
111}
112
113impl Keyer for Address {
114    fn key(&self) -> BytesKey {
115        self.to_bytes().into()
116    }
117}
118
119impl Keyer for u64 {
120    fn key(&self) -> BytesKey {
121        u64_key(*self)
122    }
123}