fil_actors_runtime_v8/
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// workaround for a compiler bug, see https://github.com/rust-lang/rust/issues/55779
7extern crate serde;
8
9use builtin::HAMT_BIT_WIDTH;
10use cid::Cid;
11use fvm_ipld_amt::Amt;
12use fvm_ipld_blockstore::Blockstore;
13use fvm_ipld_hamt::{BytesKey, Error as HamtError, Hamt};
14use fvm_shared::bigint::BigInt;
15pub use fvm_shared::BLOCKS_PER_EPOCH as EXPECTED_LEADERS_PER_EPOCH;
16use serde::de::DeserializeOwned;
17use serde::Serialize;
18use unsigned_varint::decode::Error as UVarintError;
19pub use {fvm_ipld_amt, fvm_ipld_hamt};
20
21pub use self::actor_error::*;
22pub use self::builtin::*;
23pub use self::util::*;
24
25pub mod actor_error;
26pub mod builtin;
27pub mod runtime;
28pub mod util;
29
30/// Map type to be used within actors. The underlying type is a HAMT.
31pub type Map<'bs, BS, V> = Hamt<&'bs BS, V, BytesKey>;
32
33/// Array type used within actors. The underlying type is an AMT.
34pub type Array<'bs, V, BS> = Amt<V, &'bs BS>;
35
36/// Deal weight
37pub type DealWeight = BigInt;
38
39/// Create a HAMT with a custom bit-width.
40#[inline]
41pub fn make_empty_map<BS, V>(store: &'_ BS, bitwidth: u32) -> Map<'_, BS, V>
42where
43    BS: Blockstore,
44    V: DeserializeOwned + Serialize,
45{
46    Map::<_, V>::new_with_bit_width(store, bitwidth)
47}
48
49/// Create a map with a root Cid.
50#[inline]
51pub fn make_map_with_root<'bs, BS, V>(
52    root: &Cid,
53    store: &'bs BS,
54) -> Result<Map<'bs, BS, V>, HamtError>
55where
56    BS: Blockstore,
57    V: DeserializeOwned + Serialize,
58{
59    Map::<_, V>::load_with_bit_width(root, store, HAMT_BIT_WIDTH)
60}
61
62/// Create a map with a root Cid.
63#[inline]
64pub fn make_map_with_root_and_bitwidth<'bs, BS, V>(
65    root: &Cid,
66    store: &'bs BS,
67    bitwidth: u32,
68) -> Result<Map<'bs, BS, V>, HamtError>
69where
70    BS: Blockstore,
71    V: DeserializeOwned + Serialize,
72{
73    Map::<_, V>::load_with_bit_width(root, store, bitwidth)
74}
75
76pub fn u64_key(k: u64) -> BytesKey {
77    let mut bz = unsigned_varint::encode::u64_buffer();
78    let slice = unsigned_varint::encode::u64(k, &mut bz);
79    slice.into()
80}
81
82pub fn parse_uint_key(s: &[u8]) -> Result<u64, UVarintError> {
83    let (v, _) = unsigned_varint::decode::u64(s)?;
84    Ok(v)
85}