Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gnovm, tm2): implement event emission with std.Emit #1653

Merged
merged 65 commits into from
Apr 30, 2024

Conversation

notJoon
Copy link
Member

@notJoon notJoon commented Feb 13, 2024

Description

Succeed in my predecessor's legacy.

I have implemented the output to show the path where the event occurred, as discussed in #1833. Also made it print the timestamp or block height together.

Key Changes

In this change, event emission functionality has been added to the Gno. The main changes include:

  1. Introducing of the emitEvent function:

    • The emitEvent function emits an event based on the given type and attributes
    • Attributes are passed as an even-length of array key-value pairs
    • When emitting an event, the current package path, timestamp, and block height information are recorded along with the event(discussed in [chain] Support for on-chain Realm Events #1833). This metadata provides additional context about where the event occured.
  2. Additional of event-related types and functions in the sdk packages:

    • NewEvent creates a new Event object based on the provided information.
    • ArributedEvent struct contains informations such as event type, package path, block height, timestamp and attributes. But I'm not sure how to utilize the eventType yet. So, I've just put it as a placeholder which will be a disscussion for another time.
    • EventArribute represents an attribute of an event and consists of a key-value pair
    • NewEventArribute creates a new EventAttribute object based on the given key-value pair.

Example

package ee

import (
	"std"
)

const (
	EventSender = "sender"
	EventReceiver = "receiver"
)

func Sender(){
    SubSender()
    SubReceiver()
}

func SubSender() {
    std.Emit(
        EventSender,
		"key1", "value1",
		"key2", "value2",
		"key3", "value3",
    )  
}

func SubReceiver() {
    std.Emit(
        EventReceiver,
        "bar", "baz",
    )
}

func Receiver() {
    std.Emit(
        EventReceiver,
        "foo", "bar",
    )
}

Result

[
  "{\"type\":\"sender\",\"pkg_path\":\"gno.land/r/demo/ee\",\"identifier\":\"SubSender\",\"timestamp\":1713846501,\"attributes\":[{\"key\":\"key1\",\"value\":\"value1\"},{\"key\":\"key2\",\"value\":\"value2\"},{\"key\":\"key3\",\"value\":\"value3\"}]}",
  "{\"type\":\"receiver\",\"pkg_path\":\"gno.land/r/demo/ee\",\"identifier\":\"SubReceiver\",\"timestamp\":1713846501,\"attributes\":[{\"key\":\"bar\",\"value\":\"baz\"}]}"
]

Related Issue/PR

#575 emit & event built-in functions (@r3v4s)
#853 feat: event & emit in gno (@anarcher) <- previous work.
#975 [META] Gno Wishlist / Feature Request Dump (@zivkovicmilos)

@github-actions github-actions bot added 📦 🤖 gnovm Issues or PRs gnovm related 📦 🌐 tendermint v2 Issues or PRs tm2 related labels Feb 13, 2024
Copy link

codecov bot commented Feb 13, 2024

Codecov Report

Attention: Patch coverage is 38.88889% with 11 lines in your changes are missing coverage. Please review.

Project coverage is 46.72%. Comparing base (7bf662a) to head (7a5fd74).
Report is 5 commits behind head on master.

Files Patch % Lines
tm2/pkg/bft/abci/types/types.go 0.00% 7 Missing ⚠️
gno.land/pkg/sdk/vm/keeper.go 60.00% 2 Missing ⚠️
tm2/pkg/crypto/keys/client/maketx.go 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1653      +/-   ##
==========================================
+ Coverage   46.64%   46.72%   +0.08%     
==========================================
  Files         492      492              
  Lines       69624    69762     +138     
==========================================
+ Hits        32476    32597     +121     
- Misses      34442    34454      +12     
- Partials     2706     2711       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@zivkovicmilos
Copy link
Member

Will take a look at this and give feedback 🙏

@notJoon
Copy link
Member Author

notJoon commented Feb 13, 2024

Will take a look at this and give feedback 🙏

Thank you @zivkovicmilos! I'm still trying to understand most things, every feedback would be greatly helpful

@github-actions github-actions bot added the 📦 ⛰️ gno.land Issues or PRs gno.land package related label Mar 26, 2024
Copy link
Member

@zivkovicmilos zivkovicmilos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly believe this is the way to go 💯

The solution proposed here is simple, elegant, minimal.
It doesn't force us to modify the transaction result in order to accommodate event data, which I see as a huge plus -- it's completely backwards compatible.

We just need to figure out how to pipe the event emissions from the Gno code (and what the API should be, I'm hoping something within std), and we should be good to go 🚀

gnovm/stdlibs/std/emit_event.go Outdated Show resolved Hide resolved
gnovm/stdlibs/std/emit_event_test.go Outdated Show resolved Hide resolved
gnovm/stdlibs/std/emit_event_test.go Outdated Show resolved Hide resolved
tm2/pkg/sdk/baseapp.go Outdated Show resolved Hide resolved
gnovm/stdlibs/std/emit_event.go Outdated Show resolved Hide resolved
- it just prints event(s) for now
@r3v4s r3v4s self-assigned this Mar 28, 2024
@r3v4s r3v4s added breaking change Functionality that contains breaking changes 🌱 feature New update to Gno labels Mar 28, 2024
@notJoon notJoon changed the title [WIP] feat(gnovm, tm2): Yet Another Emit/Event feat(gnovm, tm2): Yet Another Emit/Event Mar 29, 2024
@notJoon notJoon marked this pull request as ready for review April 1, 2024 09:32
Copy link
Member

@zivkovicmilos zivkovicmilos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving, again, because the changes are solid

Please check the leftover comments, but don't resolve them on the GitHub UI 🙏
Let the comment author / core team resolve the comment after you reply / ping them as soon as the fix has been pushed

Copy link
Member

@moul moul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take into account my previous comment.

@moul moul merged commit 5429655 into gnolang:master Apr 30, 2024
200 checks passed
@notJoon notJoon deleted the emit-event branch April 30, 2024 18:38
gfanton pushed a commit to gfanton/gno that referenced this pull request May 1, 2024
)

# Description

Succeed in my predecessor's legacy.

I have implemented the output to show the path where the event occurred,
as discussed in gnolang#1833. Also made it print the timestamp or block height
together.

## Key Changes

In this change, event emission functionality has been added to the Gno.
The main changes include:

1. Introducing of the `emitEvent` function:
- The `emitEvent` function emits an event based on the given type and
attributes
    - Attributes are passed as an even-length of array key-value pairs
- When emitting an event, the current _package path_, _timestamp_, and
_block height_ information are recorded along with the event(discussed
in gnolang#1833). This metadata provides additional context about where the
event occured.

2. Additional of event-related types and functions in the `sdk`
packages:
- `NewEvent` creates a new `Event` object based on the provided
information.
- `ArributedEvent` struct contains informations such as event type,
package path, block height, timestamp and attributes. But I'm not sure
how to utilize the `eventType` yet. So, I've just put it as a
placeholder which will be a disscussion for another time.
- `EventArribute` represents an attribute of an event and consists of a
key-value pair
- `NewEventArribute` creates a new `EventAttribute` object based on the
given key-value pair.

## Example

```go
package ee

import (
	"std"
)

const (
	EventSender = "sender"
	EventReceiver = "receiver"
)

func Sender(){
    SubSender()
    SubReceiver()
}

func SubSender() {
    std.Emit(
        EventSender,
		"key1", "value1",
		"key2", "value2",
		"key3", "value3",
    )  
}

func SubReceiver() {
    std.Emit(
        EventReceiver,
        "bar", "baz",
    )
}

func Receiver() {
    std.Emit(
        EventReceiver,
        "foo", "bar",
    )
}
```

### Result

```json
[
  "{\"type\":\"sender\",\"pkg_path\":\"gno.land/r/demo/ee\",\"identifier\":\"SubSender\",\"timestamp\":1713846501,\"attributes\":[{\"key\":\"key1\",\"value\":\"value1\"},{\"key\":\"key2\",\"value\":\"value2\"},{\"key\":\"key3\",\"value\":\"value3\"}]}",
  "{\"type\":\"receiver\",\"pkg_path\":\"gno.land/r/demo/ee\",\"identifier\":\"SubReceiver\",\"timestamp\":1713846501,\"attributes\":[{\"key\":\"bar\",\"value\":\"baz\"}]}"
]
```

## Related Issue/PR

gnolang#575 emit & event built-in functions (@r3v4s)
gnolang#853 feat: event & emit in gno (@anarcher) <- previous work.
gnolang#975 [META] Gno Wishlist / Feature Request Dump (@zivkovicmilos)

---------

Co-authored-by: n3wbie <r3v4@onbloc.xyz>
Co-authored-by: Manfred Touron <94029+moul@users.noreply.github.com>
@r3v4s
Copy link
Contributor

r3v4s commented May 3, 2024

FYI, for everyone who has been in this PR
Please take a look at this bug #2028

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Functionality that contains breaking changes 📦 🌐 tendermint v2 Issues or PRs tm2 related 📦 ⛰️ gno.land Issues or PRs gno.land package related 📦 🤖 gnovm Issues or PRs gnovm related 🌱 feature New update to Gno
Projects
Status: No status
Status: Done
Status: ✅ Done
Status: Done
Status: Done
Development

Successfully merging this pull request may close these issues.

[chain] Support for on-chain Realm Events