Skip to content

Commit

Permalink
[IBC] Change Events to not have a Height field and use uint64 in quer…
Browse files Browse the repository at this point in the history
…ies (#931)
  • Loading branch information
h5law authored and red-0ne committed Aug 2, 2023
1 parent 990321e commit 21d7024
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 40 deletions.
9 changes: 5 additions & 4 deletions ibc/events/event_manager.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package events

import (
coreTypes "github.com/pokt-network/pocket/shared/core/types"
core_types "github.com/pokt-network/pocket/shared/core/types"
"github.com/pokt-network/pocket/shared/modules"
"github.com/pokt-network/pocket/shared/modules/base_modules"
)
Expand Down Expand Up @@ -42,14 +42,15 @@ func (*EventManager) Create(bus modules.Bus, options ...modules.EventLoggerOptio

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

func (e *EventManager) EmitEvent(event *coreTypes.IBCEvent) error {
func (e *EventManager) EmitEvent(event *core_types.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))
func (e *EventManager) QueryEvents(topic string, height uint64) ([]*core_types.IBCEvent, error) {
currHeight := e.GetBus().GetConsensusModule().CurrentHeight()
rCtx, err := e.GetBus().GetPersistenceModule().NewReadContext(int64(currHeight))
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions ibc/store/provable_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func newProvableStore(bus modules.Bus, prefix coreTypes.CommitmentPrefix, privat
// keys are automatically prefixed with the CommitmentPrefix if not present
func (p *provableStore) Get(key []byte) ([]byte, error) {
prefixed := applyPrefix(p.prefix, key)
currHeight := int64(p.bus.GetConsensusModule().CurrentHeight())
rCtx, err := p.bus.GetPersistenceModule().NewReadContext(currHeight)
currHeight := p.bus.GetConsensusModule().CurrentHeight()
rCtx, err := p.bus.GetPersistenceModule().NewReadContext(int64(currHeight))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion ibc/store/provable_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ func newPersistenceMock(t *testing.T,
EXPECT().
GetIBCStoreEntry(gomock.Any(), gomock.Any()).
DoAndReturn(
func(key []byte, _ int64) ([]byte, error) {
func(key []byte, _ uint64) ([]byte, error) {
value, ok := dbMap[hex.EncodeToString(key)]
if !ok {
return nil, coreTypes.ErrIBCKeyDoesNotExist(string(key))
Expand Down
5 changes: 5 additions & 0 deletions persistence/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ func (p *PostgresContext) GetVersionAtHeight(height int64) (string, error) {
return "", nil
}

// TODO(#882): Implement this function
func (p *PostgresContext) GetRevisionNumber(height int64) uint64 {
return 1
}

// TODO: Implement this function
func (p *PostgresContext) GetSupportedChains(height int64) ([]string, error) {
// This is a placeholder function for the RPC endpoint "v1/query/supportedchains"
Expand Down
6 changes: 3 additions & 3 deletions persistence/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (
// SetIBCStoreEntry sets the key value pair in the IBC store postgres table at the current height
func (p *PostgresContext) SetIBCStoreEntry(key, value []byte) error {
ctx, tx := p.getCtxAndTx()
if _, err := tx.Exec(ctx, pTypes.InsertIBCStoreEntryQuery(p.Height, key, value)); err != nil {
if _, err := tx.Exec(ctx, pTypes.InsertIBCStoreEntryQuery(uint64(p.Height), key, value)); err != nil {
return err
}
return nil
}

// GetIBCStoreEntry returns the stored value for the key at the height provided from the IBC store table
func (p *PostgresContext) GetIBCStoreEntry(key []byte, height int64) ([]byte, error) {
func (p *PostgresContext) GetIBCStoreEntry(key []byte, height uint64) ([]byte, error) {
ctx, tx := p.getCtxAndTx()
row := tx.QueryRow(ctx, pTypes.GetIBCStoreEntryQuery(height, key))
var valueHex string
Expand Down Expand Up @@ -50,7 +50,7 @@ func (p *PostgresContext) SetIBCEvent(event *coreTypes.IBCEvent) error {
return err
}
eventHex := hex.EncodeToString(eventBz)
if _, err := tx.Exec(ctx, pTypes.InsertIBCEventQuery(p.Height, typeStr, eventHex)); err != nil {
if _, err := tx.Exec(ctx, pTypes.InsertIBCEventQuery(uint64(p.Height), typeStr, eventHex)); err != nil {
return err
}
return nil
Expand Down
20 changes: 8 additions & 12 deletions persistence/test/ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestIBC_GetIBCStoreEntry(t *testing.T) {

testCases := []struct {
name string
height int64
height uint64
key []byte
expectedValue []byte
expectedErr error
Expand Down Expand Up @@ -133,13 +133,12 @@ var (
baseAttributeValue = []byte("testValue")
)

func TestIBCSetEvent(t *testing.T) {
func TestIBC_SetIBCEvent(t *testing.T) {
// Setup database
db := NewTestPostgresContext(t, 1)
// Add a single event at height 1
event := new(coreTypes.IBCEvent)
event.Topic = "test"
event.Height = 1
event.Attributes = append(event.Attributes, &coreTypes.Attribute{
Key: baseAttributeKey,
Value: baseAttributeValue,
Expand Down Expand Up @@ -216,7 +215,6 @@ func TestIBCSetEvent(t *testing.T) {
db.Height = int64(tc.height)
event := new(coreTypes.IBCEvent)
event.Topic = tc.topic
event.Height = tc.height
for _, attr := range tc.attributes {
event.Attributes = append(event.Attributes, &coreTypes.Attribute{
Key: attr.key,
Expand All @@ -233,7 +231,7 @@ func TestIBCSetEvent(t *testing.T) {
}
}

func TestGetIBCEvent(t *testing.T) {
func TestIBC_GetIBCEvent(t *testing.T) {
// Setup database
db := NewTestPostgresContext(t, 1)
// Add events "testKey0", "testKey1", "testKey2", "testKey3"
Expand All @@ -242,19 +240,18 @@ func TestGetIBCEvent(t *testing.T) {
for i := 0; i < 4; i++ {
event := new(coreTypes.IBCEvent)
event.Topic = "test"
event.Height = uint64(i + 1)
if i == 3 {
event.Height = uint64(i) // add a second event at height 3
}
s := strconv.Itoa(i)
event.Attributes = append(event.Attributes, &coreTypes.Attribute{
Key: []byte("testKey" + s),
Value: []byte("testValue" + s),
})
events = append(events, event)
}
for _, event := range events {
db.Height = int64(event.Height)
for i, event := range events {
db.Height = int64(i + 1)
if i == 3 { // add 2 events at height 3
db.Height = int64(i)
}
require.NoError(t, db.SetIBCEvent(event))
}

Expand Down Expand Up @@ -301,7 +298,6 @@ func TestGetIBCEvent(t *testing.T) {
require.NoError(t, err)
require.Len(t, got, tc.expectedLength)
for i, index := range tc.eventsIndexes {
require.Equal(t, events[index].Height, got[i].Height)
require.Equal(t, events[index].Topic, got[i].Topic)
require.Equal(t, events[index].Attributes[0].Key, got[i].Attributes[0].Key)
require.Equal(t, events[index].Attributes[0].Value, got[i].Attributes[0].Value)
Expand Down
6 changes: 3 additions & 3 deletions persistence/types/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (
)

// InsertIBCStoreEntryQuery returns the query to insert a key/value pair into the ibc_entries table
func InsertIBCStoreEntryQuery(height int64, key, value []byte) string {
func InsertIBCStoreEntryQuery(height uint64, key, value []byte) string {
return fmt.Sprintf(
`INSERT INTO %s(height, key, value) VALUES(%d, '%s', '%s')`,
IBCStoreTableName,
Expand All @@ -34,7 +34,7 @@ func InsertIBCStoreEntryQuery(height int64, key, value []byte) string {
}

// InsertIBCEventQuery returns the query to insert an event into the ibc_events table
func InsertIBCEventQuery(height int64, topic, eventHex string) string {
func InsertIBCEventQuery(height uint64, topic, eventHex string) string {
return fmt.Sprintf(
`INSERT INTO %s(height, topic, event) VALUES(%d, '%s', '%s')`,
IBCEventLogTableName,
Expand All @@ -45,7 +45,7 @@ func InsertIBCEventQuery(height int64, topic, eventHex string) string {
}

// GetIBCStoreEntryQuery returns the latest value for the key at the height provided or at the last updated height
func GetIBCStoreEntryQuery(height int64, key []byte) string {
func GetIBCStoreEntryQuery(height uint64, key []byte) string {
return fmt.Sprintf(
`SELECT value FROM %s WHERE height <= %d AND key = '%s' ORDER BY height DESC LIMIT 1`,
IBCStoreTableName,
Expand Down
5 changes: 5 additions & 0 deletions shared/core/types/ibc_events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package types

func NewAttribute(key, value []byte) *Attribute {
return &Attribute{Key: key, Value: value}
}
16 changes: 5 additions & 11 deletions shared/core/types/proto/ibc_events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@ package core;

option go_package = "github.com/pokt-network/pocket/shared/core/types";

// Attribute represents a key-value pair in an IBC event
message IBCEvent {
string topic = 1;
repeated Attribute attributes = 2;
}

message Attribute {
bytes key = 1;
bytes value = 2;
}

// IBCEvent are used after a series of insertions/updates/deletions to the IBC store
// they capture the type of changes made, such as creating a new light client, or
// opening a connection. They also capture the height at which the change was made
// and the different key-value pairs that were modified in the attributes field.
message IBCEvent {
string topic = 1;
uint64 height = 2;
repeated Attribute attributes = 3;
}
6 changes: 3 additions & 3 deletions shared/modules/ibc_event_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package modules
//go:generate mockgen -destination=./mocks/ibc_event_module_mock.go github.com/pokt-network/pocket/shared/modules EventLogger

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

const EventLoggerModuleName = "event_logger"
Expand All @@ -16,6 +16,6 @@ type EventLogger interface {
Submodule
eventLoggerFactory

EmitEvent(event *coreTypes.IBCEvent) error
QueryEvents(topic string, height uint64) ([]*coreTypes.IBCEvent, error)
EmitEvent(event *core_types.IBCEvent) error
QueryEvents(topic string, height uint64) ([]*core_types.IBCEvent, error)
}
3 changes: 2 additions & 1 deletion shared/modules/persistence_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ type PersistenceReadContext interface {

// Version queries
GetVersionAtHeight(height int64) (string, error) // TODO: Implement this
GetRevisionNumber(height int64) uint64 // TODO(#882): Implement this

// Supported Chains Queries
GetSupportedChains(height int64) ([]string, error) // TODO: Implement this
Expand Down Expand Up @@ -245,7 +246,7 @@ type PersistenceReadContext interface {

// IBC Queries
// GetIBCStoreEntry returns the value of the key at the given height from the ibc_entries table
GetIBCStoreEntry(key []byte, height int64) ([]byte, error)
GetIBCStoreEntry(key []byte, height uint64) ([]byte, error)
// GetIBCEvent returns the matching IBC events for any topic at the height provied
GetIBCEvents(height uint64, topic string) ([]*coreTypes.IBCEvent, error)
}
Expand Down

0 comments on commit 21d7024

Please sign in to comment.