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
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT
// Copyright 2021-2023 Protocol Labs
// SPDX-License-Identifier: Apache-2.0, MIT

use std::collections::BTreeMap;

use crate::utils::db::CborStoreExt as _;
use anyhow::{ensure, Context as _};
use cid::Cid;
use fvm_ipld_blockstore::Blockstore;
use itertools::Itertools as _;
use num::FromPrimitive;
use serde::{Deserialize, Serialize};

/// This should be the latest enumeration of all builtin actors
pub use fil_actors_shared::v11::runtime::builtins::Type as BuiltinActor;

/// A list of [`BuiltinActor`]s to their CIDs
// Theoretically, this struct could just have fields for all the actors,
// acting as a kind of perfect hash map, but performance will be fine as-is
// #[derive(Serialize, Deserialize, Debug)]
#[derive(Debug, PartialEq, Default)]
pub struct BuiltinActorManifest {
    builtin2cid: BTreeMap<BuiltinActor, Cid>,
    /// The CID that this manifest was built from
    actor_list_cid: Cid,
}

// We need to implement Serialize and Deserialize manually because `BuiltinActor` is not `Serialize` or `Deserialize`,
// and it's an external dependency. Additionally, we want the name of the actor to be visible in the serialized form,
// even though it's not reliable for deserialization.
impl Serialize for BuiltinActorManifest {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeMap;

        let builtin2cid = self
            .builtin2cid
            .iter()
            .map(|(&k, v)| (k.name(), k as i32, v.to_string()))
            .collect::<Vec<_>>();
        let actor_list_cid = self.actor_list_cid.to_string();
        let mut map = serializer.serialize_map(Some(2))?;
        map.serialize_entry("actors", &builtin2cid)?;
        map.serialize_entry("actor_list_cid", &actor_list_cid)?;
        map.end()
    }
}

struct BuiltinActorManifestVisitor;

impl<'de> serde::de::Visitor<'de> for BuiltinActorManifestVisitor {
    type Value = BuiltinActorManifest;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("a map with 'actor_list_cid' and 'actors' keys")
    }

    fn visit_map<A: serde::de::MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
        let mut actor_list_cid: Option<Cid> = None;
        let mut builtin2cid: Option<Vec<(String, i32, String)>> = None;
        while let Some(key) = map.next_key::<String>()? {
            match key.as_str() {
                "actor_list_cid" => {
                    if actor_list_cid.is_some() {
                        return Err(serde::de::Error::custom("duplicate actor_list_cid key"));
                    }
                    actor_list_cid = Some(
                        map.next_value::<String>()?
                            .try_into()
                            .map_err(serde::de::Error::custom)?,
                    );
                }
                "actors" => {
                    if builtin2cid.is_some() {
                        return Err(serde::de::Error::custom("duplicate actors key"));
                    }
                    builtin2cid = Some(map.next_value()?);
                }
                _ => return Err(serde::de::Error::custom(format!("unexpected key: {}", key))),
            }
        }
        let actor_list_cid =
            actor_list_cid.ok_or_else(|| serde::de::Error::custom("missing actor_list_cid key"))?;
        let builtin2cid: BTreeMap<BuiltinActor, Cid> = builtin2cid
            .ok_or_else(|| serde::de::Error::custom("missing actors key"))?
            .into_iter()
            .map(|(_, k, v)| {
                Ok((
                    BuiltinActor::from_i32(k)
                        .ok_or_else(|| serde::de::Error::custom("invalid builtin actor"))?,
                    v.try_into().map_err(serde::de::Error::custom)?,
                ))
            })
            .collect::<Result<_, _>>()?;

        // Assert that all mandatory actors are present
        if !BuiltinActorManifest::MANDATORY_BUILTINS
            .iter()
            .all(|builtin| builtin2cid.iter().any(|(id, _)| id == builtin))
        {
            return Err(serde::de::Error::custom("missing mandatory actor"));
        }

        Ok(BuiltinActorManifest {
            builtin2cid,
            actor_list_cid,
        })
    }
}

impl<'de> Deserialize<'de> for BuiltinActorManifest {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        deserializer.deserialize_map(BuiltinActorManifestVisitor)
    }
}

// Manifest2.builtin2cid must be a BTreeMap
static_assertions::assert_not_impl_all!(BuiltinActor: std::hash::Hash);

impl BuiltinActorManifest {
    const MANDATORY_BUILTINS: &'static [BuiltinActor] = &[BuiltinActor::Init, BuiltinActor::System];
    pub fn load_manifest(b: &impl Blockstore, manifest_cid: &Cid) -> anyhow::Result<Self> {
        let (manifest_version, actor_list_cid) = b.get_cbor_required::<(u32, Cid)>(manifest_cid)?;
        ensure!(
            manifest_version == 1,
            "unsupported manifest version {}",
            manifest_version
        );
        Self::load_v1_actor_list(b, &actor_list_cid)
    }
    pub fn load_v1_actor_list(b: &impl Blockstore, actor_list_cid: &Cid) -> anyhow::Result<Self> {
        let mut actor_list = b.get_cbor_required::<Vec<(String, Cid)>>(actor_list_cid)?;
        actor_list.sort();
        ensure!(
            actor_list.iter().map(|(name, _cid)| name).all_unique(),
            "duplicate actor name in actor list"
        );
        let mut name2cid = BTreeMap::from_iter(actor_list);
        let mut builtin2cid = BTreeMap::new();
        for builtin in ALL_BUILTINS {
            if let Some(cid) = name2cid.remove(builtin.name()) {
                builtin2cid.insert(*builtin, cid);
            }
        }
        for mandatory_builtin in Self::MANDATORY_BUILTINS {
            ensure!(
                builtin2cid.contains_key(mandatory_builtin),
                "actor list does not contain mandatory actor {}",
                mandatory_builtin.name()
            )
        }
        if !name2cid.is_empty() {
            tracing::warn!("unknown actors in list: [{}]", name2cid.keys().join(", "))
        }
        Ok(Self {
            builtin2cid,
            actor_list_cid: *actor_list_cid,
        })
    }
    // Return anyhow::Result instead of Option because we know our users are all at the root of an error chain
    pub fn get(&self, builtin: BuiltinActor) -> anyhow::Result<Cid> {
        self.builtin2cid
            .get(&builtin)
            .copied()
            .with_context(|| format!("builtin actor {} is not in the manifest", builtin.name()))
    }
    pub fn get_system(&self) -> Cid {
        static_assert_contains_matching!(
            BuiltinActorManifest::MANDATORY_BUILTINS,
            BuiltinActor::System
        );
        self.get(BuiltinActor::System).unwrap()
    }
    pub fn get_init(&self) -> Cid {
        static_assert_contains_matching!(
            BuiltinActorManifest::MANDATORY_BUILTINS,
            BuiltinActor::Init
        );
        self.get(BuiltinActor::Init).unwrap()
    }
    /// The CID that this manifest was built from, also known as the `actors CID`
    #[doc(alias = "actors_cid")]
    pub fn source_cid(&self) -> Cid {
        self.actor_list_cid
    }
    pub fn builtin_actors(&self) -> impl ExactSizeIterator<Item = (BuiltinActor, Cid)> + '_ {
        self.builtin2cid.iter().map(|(k, v)| (*k, *v)) // std::iter::Copied doesn't play well with the tuple here
    }
}

// https://github.com/ChainSafe/fil-actor-states/issues/171
macro_rules! exhaustive {
    ($vis:vis const $ident:ident: &[$ty:ty] = &[$($variant:path),* $(,)?];) => {
        $vis const $ident: &[$ty] = &[$($variant,)*];
        const _: () = {
            fn check_exhaustive(it: $ty) {
                match it {
                    $(
                        $variant => {},
                    )*
                }
            }
        };

    }
}

exhaustive! {
    const ALL_BUILTINS: &[BuiltinActor] = &[
        BuiltinActor::System,
        BuiltinActor::Init,
        BuiltinActor::Cron,
        BuiltinActor::Account,
        BuiltinActor::Power,
        BuiltinActor::Miner,
        BuiltinActor::Market,
        BuiltinActor::PaymentChannel,
        BuiltinActor::Multisig,
        BuiltinActor::Reward,
        BuiltinActor::VerifiedRegistry,
        BuiltinActor::DataCap,
        BuiltinActor::Placeholder,
        BuiltinActor::EVM,
        BuiltinActor::EAM,
        BuiltinActor::EthAccount,
    ];
}

macro_rules! static_assert_contains_matching {
    ($slice:expr, $must_match:pat) => {
        const _: () = {
            let slice = $slice;
            let mut cur_ix = slice.len();
            'ok: {
                while let Some(new_ix) = cur_ix.checked_sub(1) {
                    cur_ix = new_ix;
                    #[allow(clippy::indexing_slicing)]
                    match slice[cur_ix] {
                        $must_match => break 'ok,
                        _ => continue,
                    }
                }
                panic!("slice did not contain a match")
            }
        };
    };
}
pub(crate) use static_assert_contains_matching;

#[cfg(test)]
mod test {
    use cid::multihash::{Code::Blake2b256, MultihashDigest as _};
    use fvm_ipld_encoding::DAG_CBOR;

    use super::*;

    fn create_random_cid() -> Cid {
        let data = rand::random::<[u8; 32]>();
        Cid::new_v1(DAG_CBOR, Blake2b256.digest(&data))
    }

    fn create_manifest() -> BuiltinActorManifest {
        let builtin2cid = ALL_BUILTINS
            .iter()
            .map(|&builtin| (builtin, create_random_cid()))
            .collect();
        BuiltinActorManifest {
            builtin2cid,
            actor_list_cid: create_random_cid(),
        }
    }

    #[test]
    fn test_manifest_serde_roundtrip() {
        let manifest = create_manifest();

        let serialized = serde_json::to_string(&manifest).unwrap();
        let deserialized: BuiltinActorManifest = serde_json::from_str(&serialized).unwrap();

        assert_eq!(manifest, deserialized);
    }

    #[test]
    fn test_manifest_serde_roundtrip_missing_trailing() {
        let mut manifest = create_manifest();
        // No last actor is okay and is required for backwards compatibility (networks without,
        // e.g., the EAM actor)
        manifest.builtin2cid.pop_last();

        let serialized = serde_json::to_string(&manifest).unwrap();
        let deserialized: BuiltinActorManifest = serde_json::from_str(&serialized).unwrap();

        assert_eq!(manifest, deserialized);
    }

    #[test]
    fn test_manifest_serde_roundtrip_with_missing_mandatory_actor() {
        let mut manifest = create_manifest();
        manifest.builtin2cid.remove(&BuiltinActor::System);

        let serialized = serde_json::to_string(&manifest).unwrap();
        let deserialized: Result<BuiltinActorManifest, serde_json::Error> =
            serde_json::from_str(&serialized);
        assert!(deserialized.is_err());
    }
}