Skip to content

Commit

Permalink
[IBC] Add Event Logging system (#893)
Browse files Browse the repository at this point in the history
## Description

<!-- reviewpad:summarize:start -->
### Summary generated by Reviewpad on 13 Jul 23 12:29 UTC
This pull request introduces several changes across multiple files. Here
is a summary of the diff:

1. In the file `benchmark_state_test.go`, a slice of `uint8` is modified
to generate a random value using the `rand.Intn` function, preventing
duplicate keys.
2. In the file `persistence_module.go`, new methods `SetIBCEvent` and
`GetIBCEvents` are added to enhance the persistence module's
functionality for handling IBC events.
3. In a different file related to IBC functionality, imports are added,
and new methods `SetIBCEvent` and `GetIBCEvents` are added for storing
and retrieving IBC events at a specific height and topic.
4. In the file `persistence/types/ibc.go`, multiple changes include
adding constants, new functions for generating queries, renaming a
function, and modifying existing functions for handling IBC events and
entries.
5. In the file `bus.go`, a new function `GetEventLogger` is added to the
`bus` struct, returning a value of type `modules.EventLogger`.
6. Another file diff involves adding imports, initializing variables
related to the IBC submodule, and updating the `Create()` function in
the `ibc/host` package.
7. In `ibc_test.go`, there are modifications to test functions, adding
new test functions, and adding an `attribute` struct.
8. A new file `ibc_event_module.go` is added, defining a package,
imports, constants, types, and methods related to an event logging
system.
9. Changes in `bus_module.go` show the addition of a new method
`GetEventLogger` to the `Bus` interface.
10. In `persistence/debug.go`, functions `ClearAllIBCQuery`,
`ClearAllIBCStoreQuery`, and `ClearAllIBCEventsQuery` are added/removed,
and a modification is made to the `HandleDebugMessage` function
signature.
11. A new file `event_manager.go` is added, containing the definition of
the `EventManager` struct and implementing the `EventLogger` interface.
12. A change is made to add a new table creation statement for the
IBCEventLog table in an unspecified file.
13. Changes in the file `ics24.md` introduce a new section for an event
logging system in the IBC module, with an overview of its implementation
and usage of a persistence layer.
14. A new file `ibc_events.proto` is added, defining message types for
attributes and IBC events in the shared/core/types/proto directory.

These changes aim to introduce and enhance functionality related to IBC
events, event logging, and persistence in the project.
<!-- reviewpad:summarize:end -->

## Issue

Fixes #824 

## Type of change

Please mark the relevant option(s):

- [x] New feature, functionality or library
- [ ] Bug fix
- [ ] Code health or cleanup
- [ ] Major breaking change
- [ ] Documentation
- [ ] Other <!-- add details here if it a different type of change -->

## List of changes

- Adds the `EventLogger` interface and submodule
- Adds methods to add events to the persistence DB
- Adds methods to query the persistence DB for events by height and
topic
- Adds `IBCEvent` protobuf type
- Registers `EventLogger` to the bus for retrieval

## Testing

- [x] `make develop_test`; if any code changes were made
- [x] `make test_e2e` on [k8s
LocalNet](https://github.com/pokt-network/pocket/blob/main/build/localnet/README.md);
if any code changes were made
- [x] `e2e-devnet-test` passes tests on
[DevNet](https://pocketnetwork.notion.site/How-to-DevNet-ff1598f27efe44c09f34e2aa0051f0dd);
if any code was changed
- [x] [Docker Compose
LocalNet](https://github.com/pokt-network/pocket/blob/main/docs/development/README.md);
if any major functionality was changed or introduced
- [x] [k8s
LocalNet](https://github.com/pokt-network/pocket/blob/main/build/localnet/README.md);
if any infrastructure or configuration changes were made

## Required Checklist

- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have added, or updated, [`godoc` format
comments](https://go.dev/blog/godoc) on touched members (see:
[tip.golang.org/doc/comment](https://tip.golang.org/doc/comment))
- [x] I have tested my changes using the available tooling
- [ ] I have updated the corresponding CHANGELOG

### If Applicable Checklist

- [x] I have updated the corresponding README(s); local and/or global
- [x] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have added, or updated,
[mermaid.js](https://mermaid-js.github.io) diagrams in the corresponding
README(s)
- [ ] I have added, or updated, documentation and
[mermaid.js](https://mermaid-js.github.io) diagrams in `shared/docs/*`
if I updated `shared/*`README(s)
  • Loading branch information
h5law authored Jul 13, 2023
1 parent 41c3d32 commit f655ace
Show file tree
Hide file tree
Showing 14 changed files with 444 additions and 32 deletions.
9 changes: 9 additions & 0 deletions ibc/docs/ics24.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- [Provable Stores](#provable-stores)
- [Bulk Store Cacher](#bulk-store-cacher)
- [Caching](#caching)
- [Event Logging System](#event-logging-system)

## Overview

Expand Down Expand Up @@ -222,6 +223,14 @@ In the event of a node failure, or local changes not being propagated correctly.

_TODO: Implement this functionality_

## Event Logging System

The `EventLogger` submodule defined in [ibc_event_module.go](../../shared/modules/ibc_event_module.go) implements the Event Logging system defined in the [ICS-24 specification][ics24]. This is used to store and query IBC related events for the relayers to read packet data and timeouts, as only the proofs of these are stored in the chain state.

Events are `IBCEvent` types defined in [ibc_events.proto](../../shared/core/types/proto/ibc_events.proto). They hold the height at which they were created, a string defining their topic (what type of event it represents) and a series of key-value pairs that represent the data of the event.

The persistence layer is used for event storage and retrieval.

[ics24]: https://github.com/cosmos/ibc/blob/main/spec/core/ics-024-host-requirements/README.md
[ics20]: https://github.com/cosmos/ibc/blob/main/spec/app/ics-020-fungible-token-transfer/README.md
[smt]: https://github.com/pokt-network/smt
58 changes: 58 additions & 0 deletions ibc/events/event_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package events

import (
coreTypes "github.com/pokt-network/pocket/shared/core/types"
"github.com/pokt-network/pocket/shared/modules"
"github.com/pokt-network/pocket/shared/modules/base_modules"
)

var _ modules.EventLogger = &EventManager{}

type EventManager struct {
base_modules.IntegrableModule

logger *modules.Logger
}

func Create(bus modules.Bus, options ...modules.EventLoggerOption) (modules.EventLogger, error) {
return new(EventManager).Create(bus, options...)
}

func WithLogger(logger *modules.Logger) modules.EventLoggerOption {
return func(m modules.EventLogger) {
if mod, ok := m.(*EventManager); ok {
mod.logger = logger
}
}
}

func (*EventManager) Create(bus modules.Bus, options ...modules.EventLoggerOption) (modules.EventLogger, error) {
e := &EventManager{}

for _, option := range options {
option(e)
}

e.logger.Info().Msg("🪵 Creating Event Logger 🪵")

bus.RegisterModule(e)

return e, nil
}

func (e *EventManager) GetModuleName() string { return modules.EventLoggerModuleName }

func (e *EventManager) EmitEvent(event *coreTypes.IBCEvent) error {
wCtx := e.GetBus().GetPersistenceModule().NewWriteContext()
defer wCtx.Release()
return wCtx.SetIBCEvent(event)
}

func (e *EventManager) QueryEvents(topic string, height uint64) ([]*coreTypes.IBCEvent, error) {
rCtx, err := e.GetBus().GetPersistenceModule().NewReadContext(int64(height))
if err != nil {
return nil, err
}
defer rCtx.Release()
return rCtx.GetIBCEvents(height, topic)
}
17 changes: 16 additions & 1 deletion ibc/host/submodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"time"

"github.com/pokt-network/pocket/ibc/events"
"github.com/pokt-network/pocket/ibc/store"
"github.com/pokt-network/pocket/runtime/configs"
coreTypes "github.com/pokt-network/pocket/shared/core/types"
Expand All @@ -19,6 +20,10 @@ type ibcHost struct {
cfg *configs.IBCHostConfig
logger *modules.Logger
storesDir string

// only a single bulk store cacher and event logger are allowed
bsc modules.BulkStoreCacher
em modules.EventLogger
}

func Create(bus modules.Bus, config *configs.IBCHostConfig, options ...modules.IBCHostOption) (modules.IBCHostSubmodule, error) {
Expand Down Expand Up @@ -51,8 +56,10 @@ func (*ibcHost) Create(bus modules.Bus, config *configs.IBCHostConfig, options .
option(h)
}
h.logger.Info().Msg("🛰️ Creating IBC host 🛰️")

bus.RegisterModule(h)
_, err := store.Create(h.GetBus(),

bsc, err := store.Create(h.GetBus(),
h.cfg.BulkStoreCacher,
store.WithLogger(h.logger),
store.WithStoresDir(h.storesDir),
Expand All @@ -61,6 +68,14 @@ func (*ibcHost) Create(bus modules.Bus, config *configs.IBCHostConfig, options .
if err != nil {
return nil, err
}
h.bsc = bsc

em, err := events.Create(h.GetBus(), events.WithLogger(h.logger))
if err != nil {
return nil, err
}
h.em = em

return h, nil
}

Expand Down
3 changes: 3 additions & 0 deletions persistence/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,8 @@ func initialiseIBCTables(ctx context.Context, db *pgxpool.Conn) error {
if _, err := db.Exec(ctx, fmt.Sprintf(`%s %s %s %s`, CreateTable, IfNotExists, types.IBCStoreTableName, types.IBCStoreTableSchema)); err != nil {
return err
}
if _, err := db.Exec(ctx, fmt.Sprintf(`%s %s %s %s`, CreateTable, IfNotExists, types.IBCEventLogTableName, types.IBCEventLogTableSchema)); err != nil {
return err
}
return nil
}
3 changes: 2 additions & 1 deletion persistence/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ var nonActorClearFunctions = []func() string{
types.ClearAllGovParamsQuery,
types.ClearAllGovFlagsQuery,
types.ClearAllBlocksQuery,
types.ClearAllIBCQuery,
types.ClearAllIBCStoreQuery,
types.ClearAllIBCEventsQuery,
}

func (m *persistenceModule) HandleDebugMessage(debugMessage *messaging.DebugMessage) error {
Expand Down
46 changes: 46 additions & 0 deletions persistence/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/jackc/pgx/v5"
pTypes "github.com/pokt-network/pocket/persistence/types"
"github.com/pokt-network/pocket/shared/codec"
coreTypes "github.com/pokt-network/pocket/shared/core/types"
)

Expand Down Expand Up @@ -39,3 +40,48 @@ func (p *PostgresContext) GetIBCStoreEntry(key []byte, height int64) ([]byte, er
}
return value, nil
}

// SetIBCEvent sets the IBC event at the current height in the persitence DB
func (p *PostgresContext) SetIBCEvent(event *coreTypes.IBCEvent) error {
ctx, tx := p.getCtxAndTx()
typeStr := event.GetTopic()
eventBz, err := codec.GetCodec().Marshal(event)
if err != nil {
return err
}
eventHex := hex.EncodeToString(eventBz)
if _, err := tx.Exec(ctx, pTypes.InsertIBCEventQuery(p.Height, typeStr, eventHex)); err != nil {
return err
}
return nil
}

// GetIBCEvents returns all the IBC events at the height provided with the matching topic
func (p *PostgresContext) GetIBCEvents(height uint64, topic string) ([]*coreTypes.IBCEvent, error) {
ctx, tx := p.getCtxAndTx()
rows, err := tx.Query(ctx, pTypes.GetIBCEventQuery(height, topic))
if err != nil {
return nil, err
}
defer rows.Close()
var events []*coreTypes.IBCEvent
for rows.Next() {
var eventHex string
if err := rows.Scan(&eventHex); err != nil {
return nil, err
}
eventBz, err := hex.DecodeString(eventHex)
if err != nil {
return nil, err
}
event := &coreTypes.IBCEvent{}
if err := codec.GetCodec().Unmarshal(eventBz, event); err != nil {
return nil, err
}
events = append(events, event)
}
if err := rows.Err(); err != nil {
return nil, err
}
return events, nil
}
2 changes: 1 addition & 1 deletion persistence/test/benchmark_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ MethodLoop:
case reflect.Slice:
switch arg.Elem().Kind() {
case reflect.Uint8:
v = reflect.ValueOf([]uint8{0})
v = reflect.ValueOf([]uint8{uint8(rand.Intn(2 ^ 8 - 1))}) // needs to be random to stop dupilcate keys
case reflect.String:
v = reflect.ValueOf([]string{"abc"})
default:
Expand Down
Loading

0 comments on commit f655ace

Please sign in to comment.