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

use crate::convert::{from_address_v3_to_v2, from_address_v4_to_v2};
use anyhow::Context;
use cid::Cid;
use fvm_ipld_blockstore::Blockstore;
use fvm_shared::address::Address;
use serde::Serialize;

use crate::io::get_obj;
/// Account actor method.
pub type Method = fil_actor_account_state::v8::Method;

/// Account actor state.
#[derive(Serialize, Debug)]
#[serde(untagged)]
pub enum State {
    V8(fil_actor_account_state::v8::State),
    V9(fil_actor_account_state::v9::State),
    V10(fil_actor_account_state::v10::State),
    V11(fil_actor_account_state::v11::State),
    V12(fil_actor_account_state::v12::State),
    V13(fil_actor_account_state::v13::State),
}

pub fn is_v8_account_cid(cid: &Cid) -> bool {
    crate::KNOWN_CIDS.actor.account.v8.contains(cid)
}

pub fn is_v9_account_cid(cid: &Cid) -> bool {
    crate::KNOWN_CIDS.actor.account.v9.contains(cid)
}

pub fn is_v10_account_cid(cid: &Cid) -> bool {
    crate::KNOWN_CIDS.actor.account.v10.contains(cid)
}

pub fn is_v11_account_cid(cid: &Cid) -> bool {
    crate::KNOWN_CIDS.actor.account.v11.contains(cid)
}

pub fn is_v12_account_cid(cid: &Cid) -> bool {
    crate::KNOWN_CIDS.actor.account.v12.contains(cid)
}

pub fn is_v13_account_cid(cid: &Cid) -> bool {
    crate::KNOWN_CIDS.actor.account.v13.contains(cid)
}

impl State {
    pub fn load<BS>(store: &BS, code: Cid, state: Cid) -> anyhow::Result<State>
    where
        BS: Blockstore,
    {
        if is_v8_account_cid(&code) {
            return get_obj(store, &state)?
                .map(State::V8)
                .context("Actor state doesn't exist in store");
        }
        if is_v9_account_cid(&code) {
            return get_obj(store, &state)?
                .map(State::V9)
                .context("Actor state doesn't exist in store");
        }
        if is_v10_account_cid(&code) {
            return get_obj(store, &state)?
                .map(State::V10)
                .context("Actor state doesn't exist in store");
        }
        if is_v11_account_cid(&code) {
            return get_obj(store, &state)?
                .map(State::V11)
                .context("Actor state doesn't exist in store");
        }
        if is_v12_account_cid(&code) {
            return get_obj(store, &state)?
                .map(State::V12)
                .context("Actor state doesn't exist in store");
        }
        if is_v13_account_cid(&code) {
            return get_obj(store, &state)?
                .map(State::V13)
                .context("Actor state doesn't exist in store");
        }
        Err(anyhow::anyhow!("Unknown account actor code {}", code))
    }

    pub fn pubkey_address(&self) -> Address {
        match self {
            State::V8(st) => st.address,
            State::V9(st) => st.address,
            State::V10(st) => from_address_v3_to_v2(st.address),
            State::V11(st) => from_address_v3_to_v2(st.address),
            State::V12(st) => from_address_v4_to_v2(st.address),
            State::V13(st) => from_address_v4_to_v2(st.address),
        }
    }
}