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
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
c67e718
[WIP] feat(gnovm, tm2): Yet Another Emit/Event
notJoon Feb 13, 2024
d7cb7d9
add test for e&e and change file names
notJoon Feb 22, 2024
94ab8ef
Merge branch 'master' into emit-event
notJoon Mar 26, 2024
c06f079
fixup
notJoon Mar 26, 2024
5360fa9
feat: EmitEvent in gno
r3v4s Mar 28, 2024
79f5752
fmt
notJoon Mar 29, 2024
fd84dc5
Merge branch 'master' into emit-event
notJoon Apr 1, 2024
35ef5f8
Event holds more metadata
notJoon Apr 1, 2024
0afd6cd
run go generate
notJoon Apr 1, 2024
6625e7e
fix lint error
notJoon Apr 1, 2024
2a87886
Merge branch 'master' into emit-event
notJoon Apr 2, 2024
d7f1fed
pkgPath in Events
notJoon Apr 2, 2024
2a6084a
re-organize code
notJoon Apr 2, 2024
97e2c07
print attribute
notJoon Apr 2, 2024
860e9bd
print metadata finish
notJoon Apr 2, 2024
63c5829
fix CI failing
notJoon Apr 2, 2024
943ac20
Merge branch 'master' into emit-event
notJoon Apr 3, 2024
866250e
change timestamp to expose the block header data
notJoon Apr 11, 2024
2a99d96
Merge branch 'master' into emit-event
notJoon Apr 11, 2024
3e5636b
fix failing error
notJoon Apr 11, 2024
be033a7
remove height, don't panic
notJoon Apr 12, 2024
52e5412
expose function name
notJoon Apr 12, 2024
1a9a524
save
notJoon Apr 12, 2024
f054d67
event stringfy
notJoon Apr 12, 2024
2adfdd7
fix CI
notJoon Apr 12, 2024
ebb387a
json style event string
notJoon Apr 13, 2024
bb80a2e
Update gnovm/tests/file.go
notJoon Apr 17, 2024
61bc407
fix: apply suggestions
notJoon Apr 17, 2024
e76e231
fix: apply suggestions
notJoon Apr 17, 2024
87d4d9e
Merge branch 'master' into emit-event
notJoon Apr 17, 2024
929eb6d
fix: resolve simple comments
notJoon Apr 18, 2024
49f1d84
moving previous DetailedEvent type into gnovm dir and renamed it
notJoon Apr 18, 2024
9b183fb
valid json
notJoon Apr 18, 2024
d2a0c98
fix: lint, remove leftover
notJoon Apr 18, 2024
59c020f
fix: apply some comments
notJoon Apr 19, 2024
13d7b4f
use EventString
notJoon Apr 19, 2024
9f726ca
remove leftover
notJoon Apr 19, 2024
271651a
Update gnovm/stdlibs/std/emit_event.go
notJoon Apr 22, 2024
b207069
Update gnovm/stdlibs/std/emit_event.go
notJoon Apr 22, 2024
d1c28cc
Update tm2/pkg/bft/abci/types/types.go
notJoon Apr 22, 2024
849ac83
Merge branch 'master' into emit-event
notJoon Apr 22, 2024
2e9d9ed
add very basic test
notJoon Apr 22, 2024
1d83801
add more tests
notJoon Apr 23, 2024
2ab7577
Update gnovm/stdlibs/std/emit_event.go
moul Apr 23, 2024
5a16ac6
explicit regex, remove unnecessary marshaling step.
notJoon Apr 23, 2024
ffb66ae
update comment
notJoon Apr 23, 2024
e49793a
store result to EventString
notJoon Apr 23, 2024
ff0877f
Update tm2/pkg/crypto/keys/client/maketx.go
moul Apr 24, 2024
8ddfd0f
Update gno.land/cmd/gnoland/testdata/event.txtar
notJoon Apr 24, 2024
55b6704
Update gnovm/stdlibs/std/emit_event_test.go
notJoon Apr 24, 2024
a7ae1a7
Update gnovm/stdlibs/std/native.go
notJoon Apr 24, 2024
191c130
Update gnovm/stdlibs/std/emit_event.gno
notJoon Apr 24, 2024
1474c0d
Update gnovm/stdlibs/std/context.go
notJoon Apr 24, 2024
150fe46
no regex
notJoon Apr 24, 2024
ed5f542
fixup (maybe)
notJoon Apr 25, 2024
67a40e2
fixup
notJoon Apr 25, 2024
ae5292a
Merge branch 'emit-event' of https://github.com/notJoon/gno-core into…
notJoon Apr 25, 2024
6cd31f1
fix: CI failing
notJoon Apr 25, 2024
c8219e4
Apply suggestions from code review
moul Apr 26, 2024
7e391a9
change field name and remove EmittedEvents, no global consst in test
notJoon Apr 29, 2024
96f7ca5
panic
notJoon Apr 29, 2024
0358dd6
Merge branch 'master' into emit-event
notJoon Apr 29, 2024
317108a
func name change
notJoon Apr 29, 2024
b0d9b28
Merge branch 'master' into emit-event
moul Apr 29, 2024
7a5fd74
use attrs for field name and change to use loadpkg
notJoon Apr 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions gno.land/cmd/gnoland/testdata/event.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
loadpkg gno.land/p/demo/ufmt

# start a new node
gnoland start

gnokey maketx addpkg -pkgdir $WORK -pkgpath gno.land/r/demo/ee -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
stdout OK!

gnokey maketx call -pkgpath gno.land/r/demo/ee -func Hello -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
notJoon marked this conversation as resolved.
Show resolved Hide resolved


-- ee.gno --
package ee

import (
"std"
)

const EventAttrKeySender = "sender"

func Hello() {
std.EmitEvent(
notJoon marked this conversation as resolved.
Show resolved Hide resolved
EventAttrKeySender,
[]string{"value1", "value2"},
)
}
5 changes: 5 additions & 0 deletions gno.land/pkg/sdk/vm/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
OrigSendSpent: new(std.Coins),
OrigPkgAddr: pkgAddr.Bech32(),
Banker: NewSDKBanker(vm, ctx),
EventLogger: ctx.EventLogger(),
}
// Parse and run the files, construct *PV.
m2 := gno.NewMachineWithOptions(
Expand Down Expand Up @@ -264,6 +265,7 @@
OrigSendSpent: new(std.Coins),
OrigPkgAddr: pkgAddr.Bech32(),
Banker: NewSDKBanker(vm, ctx),
EventLogger: ctx.EventLogger(),
}
// Construct machine and evaluate.
m := gno.NewMachineWithOptions(
Expand Down Expand Up @@ -335,6 +337,7 @@
OrigSendSpent: new(std.Coins),
OrigPkgAddr: pkgAddr.Bech32(),
Banker: NewSDKBanker(vm, ctx),
EventLogger: ctx.EventLogger(),
}
// Parse and run the files, construct *PV.
buf := new(bytes.Buffer)
Expand Down Expand Up @@ -467,6 +470,7 @@
// OrigSendSpent: nil,
OrigPkgAddr: pkgAddr.Bech32(),
Banker: NewSDKBanker(vm, ctx), // safe as long as ctx is a fork to be discarded.
EventLogger: ctx.EventLogger(),

Check warning on line 473 in gno.land/pkg/sdk/vm/keeper.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/keeper.go#L473

Added line #L473 was not covered by tests
}
m := gno.NewMachineWithOptions(
gno.MachineOptions{
Expand Down Expand Up @@ -527,6 +531,7 @@
// OrigSendSpent: nil,
OrigPkgAddr: pkgAddr.Bech32(),
Banker: NewSDKBanker(vm, ctx), // safe as long as ctx is a fork to be discarded.
EventLogger: ctx.EventLogger(),

Check warning on line 534 in gno.land/pkg/sdk/vm/keeper.go

View check run for this annotation

Codecov / codecov/patch

gno.land/pkg/sdk/vm/keeper.go#L534

Added line #L534 was not covered by tests
}
m := gno.NewMachineWithOptions(
gno.MachineOptions{
Expand Down
25 changes: 25 additions & 0 deletions gnovm/stdlibs/native.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gnovm/stdlibs/std/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ type ExecContext struct {
OrigSend std.Coins
OrigSendSpent *std.Coins // mutable
Banker BankerInterface
EventLogger *sdk.EventLogger
}
4 changes: 4 additions & 0 deletions gnovm/stdlibs/std/emit_event.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package std

func EmitEvent(typ string, attrs []string) { emitEvent(typ, attrs) }
func emitEvent(typ string, attrs []string)
notJoon marked this conversation as resolved.
Show resolved Hide resolved
35 changes: 35 additions & 0 deletions gnovm/stdlibs/std/emit_event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package std

// ref: https://github.com/gnolang/gno/pull/853
notJoon marked this conversation as resolved.
Show resolved Hide resolved

import (
"fmt"

gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/tm2/pkg/sdk"
)

func X_emitEvent(m *gno.Machine, typ string, attrs []string) {
eventAttrs := make([]sdk.EventAttribute, len(attrs)/2)
pkgPath := CurrentRealmPath(m)
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved

attrLen := len(attrs)
if attrLen%2 != 0 {
panic(fmt.Sprintf("attributes has an odd number of elements. current length: %d", attrLen))
notJoon marked this conversation as resolved.
Show resolved Hide resolved
}

for i := 0; i < attrLen; i += 2 {
eventAttrs[i/2] = sdk.EventAttribute{
Key: attrs[i],
Value: attrs[i+1],
}
}

timestamp := GetTimestamp(m)
height := GetHeight(m)

event := sdk.NewEvent(typ, pkgPath, height, timestamp, eventAttrs...)

ctx := m.Context.(ExecContext)
ctx.EventLogger.EmitEvent(event)
}
4 changes: 4 additions & 0 deletions gnovm/stdlibs/std/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func GetHeight(m *gno.Machine) int64 {
return m.Context.(ExecContext).Height
}

func GetTimestamp(m *gno.Machine) int64 {
return m.Context.(ExecContext).Timestamp
}

func X_origSend(m *gno.Machine) (denoms []string, amounts []int64) {
os := m.Context.(ExecContext).OrigSend
return ExpandCoins(os)
Expand Down
3 changes: 3 additions & 0 deletions gnovm/tests/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"strconv"
"strings"

"github.com/gnolang/gno/tm2/pkg/sdk"

notJoon marked this conversation as resolved.
Show resolved Hide resolved
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/stdlibs"
"github.com/gnolang/gno/tm2/pkg/crypto"
Expand Down Expand Up @@ -50,6 +52,7 @@ func testMachineCustom(store gno.Store, pkgPath string, stdout io.Writer, maxAll
OrigSend: send,
OrigSendSpent: new(std.Coins),
Banker: banker,
EventLogger: sdk.NewEventLogger(),
}
m := gno.NewMachineWithOptions(gno.MachineOptions{
PkgPath: "", // set later.
Expand Down
2 changes: 1 addition & 1 deletion gnovm/tests/files/zrealm_natbind0.gno
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func main() {
// "Closure": {
// "@type": "/gno.RefValue",
// "Escaped": true,
// "ObjectID": "a7f5397443359ea76c50be82c77f1f893a060925:6"
// "ObjectID": "a7f5397443359ea76c50be82c77f1f893a060925:7"
// },
// "FileName": "native.gno",
// "IsMethod": false,
Expand Down
1 change: 1 addition & 0 deletions tm2/pkg/crypto/keys/client/maketx.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func ExecSignAndBroadcast(
io.Println("OK!")
io.Println("GAS WANTED:", bres.DeliverTx.GasWanted)
io.Println("GAS USED: ", bres.DeliverTx.GasUsed)
io.Println("EVENTS: ", bres.DeliverTx.Events)

zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
1 change: 1 addition & 0 deletions tm2/pkg/sdk/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ func (app *BaseApp) runMsgs(ctx Context, msgs []Msg, mode RunTxMode) (result Res
// each result.
data = append(data, msgResult.Data...)
events = append(events, msgResult.Events...)
events = append(events, ctx.EventLogger().Events()...)
notJoon marked this conversation as resolved.
Show resolved Hide resolved
// TODO append msgevent from ctx. XXX XXX

// stop execution and return on first failed message
Expand Down
45 changes: 45 additions & 0 deletions tm2/pkg/sdk/events.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package sdk

import (
"fmt"

abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
)

Expand Down Expand Up @@ -35,3 +37,46 @@ func (em *EventLogger) EmitEvents(events []Event) {
// ----------------------------------------------------------------------------

type Event = abci.Event

func NewEvent(eventType string, pkgPath string, height, timestamp int64, attrs ...EventAttribute) Event {
return AttributedEvent{
Type: eventType,
PkgPath: pkgPath,
Attributes: attrs,
Height: height,
Timestamp: timestamp,
notJoon marked this conversation as resolved.
Show resolved Hide resolved
}
}

type AttributedEvent struct {
notJoon marked this conversation as resolved.
Show resolved Hide resolved
Type string
PkgPath string
Height int64
Timestamp int64
Attributes []EventAttribute
}

func (e AttributedEvent) AssertABCIEvent() {}

func (e AttributedEvent) String() string {
return fmt.Sprintf(
"type: %s, pkgPath: %s, height: %d, timestamp: %d, attributes: %s",
e.Type, e.PkgPath, e.Height, e.Timestamp, e.Attributes,
)
}

type EventAttribute struct {
Key string
Value string
}

func NewEventAttribute(key, value string) EventAttribute {
notJoon marked this conversation as resolved.
Show resolved Hide resolved
return EventAttribute{
Key: key,
Value: value,
}
}

func (ea EventAttribute) String() string {
return fmt.Sprintf("%s: %s", ea.Key, ea.Value)
}
2 changes: 2 additions & 0 deletions tm2/pkg/sdk/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ var Package = amino.RegisterPackage(amino.NewPackage(
).
WithTypes(
Result{},
AttributedEvent{},
EventAttribute{},
notJoon marked this conversation as resolved.
Show resolved Hide resolved
))
2 changes: 1 addition & 1 deletion tm2/pkg/sdk/sdk.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ message Result {
abci.ResponseBase response_base = 1 [json_name = "ResponseBase"];
sint64 gas_wanted = 2 [json_name = "GasWanted"];
sint64 gas_used = 3 [json_name = "GasUsed"];
}
}
Loading