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

use ::cid::Cid;
use fvm_ipld_encoding::RawBytes;

use super::*;
use crate::shim::executor::Receipt;

#[derive(Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "PascalCase")]
#[schemars(rename = "Receipt")]
pub struct ReceiptLotusJson {
    exit_code: u32,
    #[schemars(with = "LotusJson<RawBytes>")]
    #[serde(with = "crate::lotus_json")]
    r#return: RawBytes,
    gas_used: u64,
    #[schemars(with = "LotusJson<Option<Cid>>")]
    #[serde(with = "crate::lotus_json", default)] // Lotus still does `"EventsRoot": null`
    events_root: Option<Cid>,
}

impl HasLotusJson for Receipt {
    type LotusJson = ReceiptLotusJson;

    #[cfg(test)]
    fn snapshots() -> Vec<(serde_json::Value, Self)> {
        vec![
            (
                json!({
                    "ExitCode": 0,
                    "Return": "aGVsbG8gd29ybGQh",
                    "GasUsed": 0,
                    "EventsRoot": null,
                }),
                Self::V3(fvm_shared3::receipt::Receipt {
                    exit_code: fvm_shared3::error::ExitCode::new(0),
                    return_data: RawBytes::new(Vec::from_iter(*b"hello world!")),
                    gas_used: 0,
                    events_root: None,
                }),
            ),
            (
                json!({
                    "ExitCode": 0,
                    "Return": "aGVsbG8gd29ybGQh",
                    "GasUsed": 0,
                    "EventsRoot": {
                        "/": "baeaaaaa"
                    }
                }),
                Self::V3(fvm_shared3::receipt::Receipt {
                    exit_code: fvm_shared3::error::ExitCode::new(0),
                    return_data: RawBytes::new(Vec::from_iter(*b"hello world!")),
                    gas_used: 0,
                    events_root: Some(Cid::default()),
                }),
            ),
        ]
    }

    fn into_lotus_json(self) -> Self::LotusJson {
        Self::LotusJson {
            exit_code: self.exit_code().value(),
            r#return: self.return_data(),
            gas_used: self.gas_used(),
            events_root: self.events_root(),
        }
    }

    fn from_lotus_json(lotus_json: Self::LotusJson) -> Self {
        let Self::LotusJson {
            exit_code,
            r#return,
            gas_used,
            events_root,
        } = lotus_json;
        Self::V3(fvm_shared3::receipt::Receipt {
            exit_code: fvm_shared3::error::ExitCode::new(exit_code),
            return_data: r#return,
            gas_used,
            events_root,
        })
    }
}

#[test]
fn shapshots() {
    assert_all_snapshots::<Receipt>()
}

/// [Receipt] knows if it is `V2` or `V3`, but there's no way for
/// the serialized representation to retain that information,
/// so [`assert_unchanged_via_json`] tests with arbitrary input will fail.
///
/// This can only be fixed by rewriting [Receipt].
///
/// See <https://github.com/ChainSafe/forest/issues/3459>.
#[test]
#[should_panic = "cannot serialize to v2 AND v3 from the same input"]
fn cannot_call_arbitrary_tests_on_receipt() {
    use pretty_assertions::assert_eq;

    let v2 = Receipt::V2(fvm_shared2::receipt::Receipt {
        exit_code: fvm_shared2::error::ExitCode::new(0),
        return_data: RawBytes::new(Vec::from_iter(*b"hello world!")),
        gas_used: 0,
    });
    let v3 = Receipt::V3(fvm_shared3::receipt::Receipt {
        exit_code: fvm_shared3::error::ExitCode::new(0),
        return_data: RawBytes::new(Vec::from_iter(*b"hello world!")),
        gas_used: 0,
        events_root: None,
    });
    let json = json!({
        "ExitCode": 0,
        "Return": "aGVsbG8gd29ybGQh",
        "GasUsed": 0,
        "EventsRoot": null,
    });

    // they serialize to the same thing...
    assert_eq!(
        serde_json::to_value(v2.clone().into_lotus_json()).unwrap(),
        json
    );
    assert_eq!(
        serde_json::to_value(v3.clone().into_lotus_json()).unwrap(),
        json
    );

    // both of these cannot pass at the same time...
    assert_eq!(
        v2,
        serde_json::from_value::<LotusJson<_>>(json.clone())
            .unwrap()
            .into_inner(),
        "cannot serialize to v2 AND v3 from the same input"
    );
    assert_eq!(
        v3,
        serde_json::from_value::<LotusJson<_>>(json)
            .unwrap()
            .into_inner(),
        "cannot serialize to v2 AND v3 from the same input"
    );
}