# Module `0x1::event`
The Event module defines an EventHandleGenerator
that is used to create
EventHandle
s with unique GUIDs. It contains a counter for the number
of EventHandle
s it generates. An EventHandle
is used to count the number of
events emitted to a handle and emit events to the event store.
- [Struct `GUIDWrapper`](#0x1_event_GUIDWrapper)
- [Struct `EventHandle`](#0x1_event_EventHandle)
- [Resource `EventHandleGenerator`](#0x1_event_EventHandleGenerator)
- [Function `new_event_handle`](#0x1_event_new_event_handle)
- [Function `emit_event`](#0x1_event_emit_event)
- [Function `guid`](#0x1_event_guid)
- [Function `write_to_event_store`](#0x1_event_write_to_event_store)
- [Function `destroy_handle`](#0x1_event_destroy_handle)
- [Module Specification](#@Module_Specification_0)
use 0x1::bcs;
use 0x1::guid;
## Struct `GUIDWrapper`
Wrapper for a GUID for layout compatibility with legacy EventHandle id's
struct GUIDWrapper has drop, store
Fields
-
len_bytes: u8
-
-
guid: guid::GUID
-
## Struct `EventHandle`
A handle for an event such that:
1. Other modules can emit events to this handle.
2. Storage can use this handle to prove the total number of events that happened in the past.
struct EventHandle<T: drop, store> has store
Fields
-
counter: u64
-
Total number of events emitted to this event stream.
-
guid: event::GUIDWrapper
-
A globally unique ID for this event stream.
## Resource `EventHandleGenerator`
Deprecated. Only kept around so Diem clients know how to deserialize existing EventHandleGenerator's
struct EventHandleGenerator has key
Fields
-
counter: u64
-
-
addr: address
-
## Function `new_event_handle`
Use EventHandleGenerator to generate a unique event handle for sig
public fun new_event_handle<T: drop, store>(account: &signer): event::EventHandle<T>
Implementation
public fun new_event_handle<T: drop + store>(account: &signer): EventHandle<T> {
// must be 24 for compatibility with legacy Event ID's--see comment on GUIDWrapper
let len_bytes = 24u8;
EventHandle<T> {
counter: 0,
guid: GUIDWrapper { len_bytes, guid: guid::create(account) }
}
}
## Function `emit_event`
Emit an event with payload msg
by using handle_ref
's key and counter.
public fun emit_event<T: drop, store>(handle_ref: &mut event::EventHandle<T>, msg: T)
Implementation
public fun emit_event<T: drop + store>(handle_ref: &mut EventHandle<T>, msg: T) {
write_to_event_store<T>(bcs::to_bytes(&handle_ref.guid.guid), handle_ref.counter, msg);
handle_ref.counter = handle_ref.counter + 1;
}
## Function `guid`
Return the GUIID associated with this EventHandle
public fun guid<T: drop, store>(handle_ref: &event::EventHandle<T>): &guid::GUID
Implementation
public fun guid<T: drop + store>(handle_ref: &EventHandle<T>): &GUID {
&handle_ref.guid.guid
}
## Function `write_to_event_store`
Log msg
as the count
th event associated with the event stream identified by guid
fun write_to_event_store<T: drop, store>(guid: vector<u8>, count: u64, msg: T)
Implementation
native fun write_to_event_store<T: drop + store>(guid: vector<u8>, count: u64, msg: T);
## Function `destroy_handle`
Destroy a unique handle.
public fun destroy_handle<T: drop, store>(handle: event::EventHandle<T>)
Implementation
public fun destroy_handle<T: drop + store>(handle: EventHandle<T>) {
EventHandle<T> { counter: _, guid: _ } = handle;
}
## Module Specification
Functions of the event module are mocked out using the intrinsic
pragma. They are implemented in the prover's prelude.
pragma intrinsic = true;
Determines equality between the guids of two event handles. Since fields of intrinsic
structs cannot be accessed, this function is provided.
fun spec_guid_eq<T>(h1: EventHandle<T>, h2: EventHandle<T>): bool {
// The implementation currently can just use native equality since the mocked prover
// representation does not have the `counter` field.
h1 == h2
}