diff --git a/ibc/events/event_manager.go b/ibc/events/event_manager.go index 19e48cb95..0b233e139 100644 --- a/ibc/events/event_manager.go +++ b/ibc/events/event_manager.go @@ -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" ) @@ -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 } diff --git a/ibc/store/provable_store.go b/ibc/store/provable_store.go index df4612725..0ab6bd6c7 100644 --- a/ibc/store/provable_store.go +++ b/ibc/store/provable_store.go @@ -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 } diff --git a/ibc/store/provable_store_test.go b/ibc/store/provable_store_test.go index 174d62827..a62160a62 100644 --- a/ibc/store/provable_store_test.go +++ b/ibc/store/provable_store_test.go @@ -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)) diff --git a/persistence/gov.go b/persistence/gov.go index 5ddec2884..73694ac6e 100644 --- a/persistence/gov.go +++ b/persistence/gov.go @@ -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" diff --git a/persistence/ibc.go b/persistence/ibc.go index fc9affea4..0544d7197 100644 --- a/persistence/ibc.go +++ b/persistence/ibc.go @@ -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 @@ -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 diff --git a/persistence/test/ibc_test.go b/persistence/test/ibc_test.go index 2fcf86f4e..b2885c155 100644 --- a/persistence/test/ibc_test.go +++ b/persistence/test/ibc_test.go @@ -75,7 +75,7 @@ func TestIBC_GetIBCStoreEntry(t *testing.T) { testCases := []struct { name string - height int64 + height uint64 key []byte expectedValue []byte expectedErr error @@ -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, @@ -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, @@ -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" @@ -242,10 +240,6 @@ 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), @@ -253,8 +247,11 @@ func TestGetIBCEvent(t *testing.T) { }) 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)) } @@ -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) diff --git a/persistence/types/ibc.go b/persistence/types/ibc.go index a783bcf82..7b0d01ee5 100644 --- a/persistence/types/ibc.go +++ b/persistence/types/ibc.go @@ -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, @@ -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, @@ -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, diff --git a/shared/core/types/ibc_events.go b/shared/core/types/ibc_events.go new file mode 100644 index 000000000..3c3c7580a --- /dev/null +++ b/shared/core/types/ibc_events.go @@ -0,0 +1,5 @@ +package types + +func NewAttribute(key, value []byte) *Attribute { + return &Attribute{Key: key, Value: value} +} diff --git a/shared/core/types/proto/ibc_events.proto b/shared/core/types/proto/ibc_events.proto index 15041214a..0ad47774b 100644 --- a/shared/core/types/proto/ibc_events.proto +++ b/shared/core/types/proto/ibc_events.proto @@ -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; -} diff --git a/shared/modules/ibc_event_module.go b/shared/modules/ibc_event_module.go index 56a80f07f..d3f628770 100644 --- a/shared/modules/ibc_event_module.go +++ b/shared/modules/ibc_event_module.go @@ -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" @@ -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) } diff --git a/shared/modules/persistence_module.go b/shared/modules/persistence_module.go index b510d835b..6ee724d63 100644 --- a/shared/modules/persistence_module.go +++ b/shared/modules/persistence_module.go @@ -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 @@ -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) }