fvm_shared/event/
mod.rs

1// Copyright 2021-2023 Protocol Labs
2// SPDX-License-Identifier: Apache-2.0, MIT
3use bitflags::bitflags;
4use fvm_ipld_encoding::{strict_bytes, tuple::*};
5use serde::{Deserialize, Serialize};
6
7use crate::ActorID;
8
9/// Event with extra information stamped by the FVM. This is the structure that gets committed
10/// on-chain via the receipt.
11#[derive(Serialize_tuple, Deserialize_tuple, PartialEq, Eq, Clone, Debug)]
12pub struct StampedEvent {
13    /// Carries the ID of the actor that emitted this event.
14    pub emitter: ActorID,
15    /// The event as emitted by the actor.
16    pub event: ActorEvent,
17}
18
19impl StampedEvent {
20    pub fn new(emitter: ActorID, event: ActorEvent) -> Self {
21        Self { emitter, event }
22    }
23}
24
25/// An event as originally emitted by the actor.
26#[derive(Serialize_tuple, Deserialize_tuple, PartialEq, Eq, Clone, Debug)]
27#[serde(transparent)]
28pub struct ActorEvent {
29    pub entries: Vec<Entry>,
30}
31
32impl From<Vec<Entry>> for ActorEvent {
33    fn from(entries: Vec<Entry>) -> Self {
34        Self { entries }
35    }
36}
37
38bitflags! {
39    /// Flags associated with an Event entry.
40    #[derive(Deserialize, Serialize, Copy, Clone, Eq, PartialEq, Debug)]
41    #[repr(transparent)] // we pass this type through a syscall
42    #[serde(transparent)]
43    pub struct Flags: u64 {
44        const FLAG_INDEXED_KEY      = 0b00000001;
45        const FLAG_INDEXED_VALUE    = 0b00000010;
46        const FLAG_INDEXED_ALL      = Self::FLAG_INDEXED_KEY.bits() | Self::FLAG_INDEXED_VALUE.bits();
47    }
48}
49
50/// A key value entry inside an Event.
51#[derive(Serialize_tuple, Deserialize_tuple, PartialEq, Eq, Clone, Debug)]
52pub struct Entry {
53    /// A bitmap conveying metadata or hints about this entry.
54    pub flags: Flags,
55    /// The key of this event.
56    pub key: String,
57    /// The value's codec. Must be IPLD_RAW (0x55) for now according to FIP-0049.
58    pub codec: u64,
59    /// The event's value.
60    #[serde(with = "strict_bytes")]
61    pub value: Vec<u8>,
62}