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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Runtime, extended representation of a [Receipt] for [Invocation]s and storage.
//!
//! [Receipt]: homestar_invocation::Receipt
//! [Invocation]: homestar_invocation::Invocation

use anyhow::anyhow;
use diesel::{
    backend::Backend,
    deserialize::{self, FromSql},
    serialize::{self, IsNull, Output, ToSql},
    sql_types::Binary,
    sqlite::Sqlite,
    AsExpression, FromSqlRow, Identifiable, Insertable, Queryable, Selectable,
};
use homestar_invocation::{
    authority::{Issuer, UcanPrf},
    consts,
    ipld::{DagCborRef, DagJson},
    task, Pointer, Receipt as InvocationReceipt,
};
use homestar_wasm::io::Arg;
use libipld::{cbor::DagCborCodec, cid::Cid, prelude::Codec, serde::from_ipld, Ipld};
use semver::Version;
use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, fmt};

pub(crate) mod metadata;

/// General version key for receipts.
pub const VERSION_KEY: &str = "version";
/// [Receipt] header tag, for sharing over libp2p.
pub const RECEIPT_TAG: &str = "ipvm/receipt";

const CID_KEY: &str = "cid";
const INSTRUCTION_KEY: &str = "instruction";
const RAN_KEY: &str = "ran";
const OUT_KEY: &str = "out";
const ISSUER_KEY: &str = "iss";
const METADATA_KEY: &str = "meta";
const PROOF_KEY: &str = "prf";

/// Receipt for [Invocation], including it's own Cid and a Cid for an [Instruction].
///
/// See [homestar_invocation::Receipt] for more info on some internal
/// fields.
///
/// [Invocation]: homestar_invocation::Invocation
/// [Instruction]: homestar_invocation::task::Instruction
#[derive(Debug, Clone, PartialEq, Queryable, Insertable, Identifiable, Selectable)]
#[diesel(table_name = crate::db::schema::receipts, primary_key(cid))]
pub struct Receipt {
    cid: Pointer,
    ran: Pointer,
    instruction: Pointer,
    out: task::Result<Ipld>,
    meta: LocalIpld,
    issuer: Option<Issuer>,
    prf: UcanPrf,
    version: String,
}

impl fmt::Display for Receipt {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Receipt: [cid: {}, instruction: {}, ran: {}, output: {:?}, metadata: {:?}, issuer: {:?}, version: {}]",
            self.cid, self.instruction, self.ran, self.out, self.meta.0, self.issuer, self.version,
        )
    }
}

impl Receipt {
    /// Generate a receipt.
    pub fn new(
        cid: Cid,
        instruction: Pointer,
        invocation_receipt: &InvocationReceipt<Ipld>,
    ) -> Self {
        Self {
            cid: Pointer::new(cid),
            ran: invocation_receipt.ran().to_owned(),
            instruction,
            out: invocation_receipt.out().to_owned(),
            meta: LocalIpld(invocation_receipt.meta().to_owned()),
            issuer: invocation_receipt.issuer().to_owned(),
            prf: invocation_receipt.prf().to_owned(),
            version: consts::INVOCATION_VERSION.to_string(),
        }
    }

    /// Return a runtime [Receipt] given an [Instruction] [Pointer] and
    /// [UCAN Invocation Receipt].
    ///
    /// [Instruction]: homestar_invocation::task::Instruction
    /// [UCAN Invocation Receipt]: homestar_invocation::Receipt
    pub fn try_with(
        instruction: Pointer,
        invocation_receipt: &InvocationReceipt<Ipld>,
    ) -> anyhow::Result<Self> {
        let cid = invocation_receipt.to_cid()?;
        Ok(Receipt::new(cid, instruction, invocation_receipt))
    }

    /// Capsule-wrapper for [InvocationReceipt] to to be shared over libp2p as
    /// DagCbor encoded bytes.
    pub fn invocation_capsule(
        invocation_receipt: &InvocationReceipt<Ipld>,
    ) -> anyhow::Result<Vec<u8>> {
        let receipt_ipld = Ipld::from(invocation_receipt);
        let capsule = if let Ipld::Map(mut map) = receipt_ipld {
            map.insert(VERSION_KEY.into(), consts::INVOCATION_VERSION.into());
            Ok(Ipld::Map(BTreeMap::from([(
                RECEIPT_TAG.into(),
                Ipld::Map(map),
            )])))
        } else {
            Err(anyhow!("receipt to Ipld conversion is not a map"))
        }?;

        DagCborCodec.encode(&capsule)
    }

    /// Get Ipld metadata on a [Receipt].
    pub fn meta(&self) -> &Ipld {
        self.meta.inner()
    }

    /// Set Ipld metadata on a [Receipt].
    pub fn set_meta(&mut self, meta: Ipld) {
        self.meta = LocalIpld(meta)
    }

    /// Get unique identifier of receipt.
    pub fn cid(&self) -> Cid {
        self.cid.cid()
    }

    /// Get unique identifier of receipt as a [String].
    pub fn cid_as_string(&self) -> String {
        self.cid().to_string()
    }

    /// Get inner Cid as bytes.
    pub fn cid_as_bytes(&self) -> Vec<u8> {
        self.cid().to_bytes()
    }

    /// Return the Pointer-wrapped Cid of the [Receipt]'s associated [Instruction].
    ///
    /// [Instruction]: homestar_invocation::task::Instruction
    pub fn instruction(&self) -> &Pointer {
        &self.instruction
    }

    /// Get instruction [Pointer] inner Cid as bytes.
    pub fn instruction_cid_as_bytes(&self) -> Vec<u8> {
        self.instruction.cid().to_bytes()
    }

    /// Get Cid in [Receipt] as a [String].
    pub fn ran(&self) -> String {
        self.ran.to_string()
    }

    /// Get executed result/value in [Receipt] as Ipld.
    pub fn output(&self) -> &task::Result<Ipld> {
        &self.out
    }

    /// Return [task::Result] output as [Arg] for execution.
    pub fn output_as_arg(&self) -> task::Result<Arg> {
        match self.out.to_owned() {
            task::Result::Ok(res) => task::Result::Ok(res.into()),
            task::Result::Error(res) => task::Result::Error(res.into()),
            task::Result::Just(res) => task::Result::Just(res.into()),
        }
    }

    /// Get executed result/value in [Receipt] as encoded Cbor.
    pub fn output_encoded(&self) -> anyhow::Result<Vec<u8>> {
        let ipld = Ipld::from(self.out.to_owned());
        DagCborCodec.encode(&ipld)
    }

    /// Return semver [Version] of [Receipt].
    pub fn version(&self) -> Result<Version, semver::Error> {
        Version::parse(&self.version)
    }
}

impl TryFrom<Receipt> for Vec<u8> {
    type Error = anyhow::Error;

    fn try_from(receipt: Receipt) -> Result<Self, Self::Error> {
        let receipt_ipld = Ipld::from(receipt);
        DagCborCodec.encode(&receipt_ipld)
    }
}

impl TryFrom<Vec<u8>> for Receipt {
    type Error = anyhow::Error;

    fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
        let ipld: Ipld = DagCborCodec.decode(&bytes)?;
        ipld.try_into()
    }
}

impl From<Receipt> for InvocationReceipt<Ipld> {
    fn from(receipt: Receipt) -> Self {
        InvocationReceipt::new(
            receipt.ran,
            receipt.out,
            receipt.meta.0,
            receipt.issuer,
            receipt.prf,
        )
    }
}

impl From<&Receipt> for InvocationReceipt<Ipld> {
    fn from(receipt: &Receipt) -> Self {
        InvocationReceipt::new(
            receipt.ran.clone(),
            receipt.out.clone(),
            receipt.meta.0.clone(),
            receipt.issuer.clone(),
            receipt.prf.clone(),
        )
    }
}

impl From<Receipt> for Ipld {
    fn from(receipt: Receipt) -> Self {
        Ipld::Map(BTreeMap::from([
            (CID_KEY.into(), receipt.cid.into()),
            (RAN_KEY.into(), receipt.ran.into()),
            (INSTRUCTION_KEY.into(), receipt.instruction.into()),
            (OUT_KEY.into(), receipt.out.into()),
            (METADATA_KEY.into(), receipt.meta.0),
            (
                ISSUER_KEY.into(),
                receipt
                    .issuer
                    .map(|issuer| issuer.to_string().into())
                    .unwrap_or(Ipld::Null),
            ),
            (PROOF_KEY.into(), receipt.prf.into()),
            (VERSION_KEY.into(), receipt.version.into()),
        ]))
    }
}

impl TryFrom<Ipld> for Receipt {
    type Error = anyhow::Error;

    fn try_from(ipld: Ipld) -> Result<Self, Self::Error> {
        let map = from_ipld::<BTreeMap<String, Ipld>>(ipld)?;
        let cid = from_ipld(
            map.get(CID_KEY)
                .ok_or_else(|| anyhow!("missing {CID_KEY}"))?
                .to_owned(),
        )?;
        let ran = map
            .get(RAN_KEY)
            .ok_or_else(|| anyhow!("missing {RAN_KEY}"))?
            .try_into()?;
        let instruction = map
            .get(INSTRUCTION_KEY)
            .ok_or_else(|| anyhow!("missing {INSTRUCTION_KEY}"))?
            .try_into()?;
        let out = map
            .get(OUT_KEY)
            .ok_or_else(|| anyhow!("missing {OUT_KEY}"))?;
        let meta = map
            .get(METADATA_KEY)
            .ok_or_else(|| anyhow!("missing {METADATA_KEY}"))?;
        let issuer = map
            .get(ISSUER_KEY)
            .and_then(|ipld| match ipld {
                Ipld::Null => None,
                ipld => Some(ipld),
            })
            .and_then(|ipld| from_ipld(ipld.to_owned()).ok())
            .map(Issuer::new);
        let prf = map
            .get(PROOF_KEY)
            .ok_or_else(|| anyhow!("missing {PROOF_KEY}"))?;
        let version = from_ipld(
            map.get(VERSION_KEY)
                .ok_or_else(|| anyhow!("missing {VERSION_KEY}"))?
                .to_owned(),
        )?;

        Ok(Receipt {
            cid: Pointer::new(cid),
            ran,
            instruction,
            out: task::Result::try_from(out)?,
            meta: LocalIpld(meta.to_owned()),
            issuer,
            prf: UcanPrf::try_from(prf)?,
            version,
        })
    }
}

impl DagJson for Receipt where Ipld: From<Receipt> {}

/// Wrapper-type for Ipld in order integrate to/from for local storage/db.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, AsExpression, FromSqlRow)]
#[diesel(sql_type = Binary)]
pub struct LocalIpld(Ipld);

impl LocalIpld {
    /// Convert into owned, inner Ipld.
    pub fn into_inner(self) -> Ipld {
        self.0
    }

    /// Convert into referenced, inner Ipld.
    pub fn inner(&self) -> &Ipld {
        &self.0
    }
}

impl ToSql<Binary, Sqlite> for LocalIpld
where
    [u8]: ToSql<Binary, Sqlite>,
{
    fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
        out.set_value(DagCborCodec.encode(&self.0)?);
        Ok(IsNull::No)
    }
}

impl<DB> FromSql<Binary, DB> for LocalIpld
where
    DB: Backend,
    *const [u8]: FromSql<Binary, DB>,
{
    fn from_sql(bytes: DB::RawValue<'_>) -> deserialize::Result<Self> {
        let raw_bytes = <*const [u8] as FromSql<Binary, DB>>::from_sql(bytes)?;
        let raw_bytes: &[u8] = unsafe { &*raw_bytes };
        let decoded = DagCborCodec.decode(raw_bytes)?;
        Ok(LocalIpld(decoded))
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::{
        db::{schema, Database},
        settings::Settings,
        test_utils::{self, db::MemoryDb},
    };
    use diesel::prelude::*;

    #[test]
    fn invocation_into_receipt() {
        let (invocation, receipt) = test_utils::receipt::receipts();
        assert_eq!(invocation.ran().to_string(), receipt.ran());
        assert_eq!(invocation.out(), receipt.output());
        assert_eq!(invocation.meta(), &receipt.meta.0);
        assert_eq!(invocation.issuer(), &receipt.issuer);
        assert_eq!(invocation.prf(), &receipt.prf);
        assert_eq!(invocation.to_cid().unwrap(), receipt.cid());

        let output_bytes = DagCborCodec
            .encode::<Ipld>(&invocation.out().clone().into())
            .unwrap();
        assert_eq!(output_bytes, receipt.output_encoded().unwrap());

        let receipt_from_invocation =
            Receipt::try_with(receipt.instruction.clone(), &invocation).unwrap();
        assert_eq!(receipt_from_invocation, receipt);

        let invocation_from_receipt = InvocationReceipt::try_from(receipt).unwrap();
        assert_eq!(invocation_from_receipt, invocation);
    }

    #[test]
    fn receipt_sql_roundtrip() {
        let mut conn = MemoryDb::setup_connection_pool(Settings::load().unwrap().node(), None)
            .unwrap()
            .conn()
            .unwrap();
        let (_, receipt) = test_utils::receipt::receipts();

        let rows_inserted = diesel::insert_into(schema::receipts::table)
            .values(&receipt)
            .execute(&mut conn)
            .unwrap();

        assert_eq!(1, rows_inserted);
        let inserted_receipt = schema::receipts::table.load::<Receipt>(&mut conn).unwrap();
        assert_eq!(vec![receipt.clone()], inserted_receipt);
    }

    #[test]
    fn receipt_to_json() {
        let (_, receipt) = test_utils::receipt::receipts();
        assert_eq!(
            receipt.to_json_string().unwrap(),
            format!(
                r#"{{"cid":{{"/":"{}"}},"instruction":{{"/":"{}"}},"iss":null,"meta":null,"out":["ok",true],"prf":[],"ran":{{"/":"{}"}},"version":"{}"}}"#,
                receipt.cid(),
                receipt.instruction(),
                receipt.ran(),
                consts::INVOCATION_VERSION
            )
        );
    }

    #[test]
    fn receipt_bytes_roundtrip() {
        let (_, receipt) = test_utils::receipt::receipts();
        let bytes: Vec<u8> = receipt.clone().try_into().unwrap();
        let from_bytes = Receipt::try_from(bytes).unwrap();

        assert_eq!(receipt, from_bytes);
    }
}