Skip to content

Commit

Permalink
feat: add message_type module and store msg types inside `message_t…
Browse files Browse the repository at this point in the history
…ype` table (#702)

Closes: #XXXX

<!-- Add a description of the changes that this PR introduces and the
files that
are the most critical to review. -->

Changes:
- Added `message_type` module
- Added msg handler to store message types inside `message_type` table
- Added new `messages_by_type` function to allow to query messages by
their types
- Added `v5` migrator to parse all msg types already stored in db and
save them inside `message_type` table

---

*All items are required. Please add a note to the item if the item is
not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type
prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json)
in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch
- [x] provided a link to the relevant issue or specification
- [x] added a changelog entry to `CHANGELOG.md`
- [x] included comments for [documenting Go
code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] reviewed "Files changed" and left comments if necessary
- [x] confirmed all CI checks have passed

*All items are required. Please add a note if the item is not applicable
and please add
your handle next to the items reviewed if you only reviewed selected
items.*

I have...

- [ ] confirmed the correct [type
prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json)
in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
MonikaCat committed Feb 6, 2024
1 parent 6100e8f commit 82836df
Show file tree
Hide file tree
Showing 21 changed files with 329 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased
- ([\#610](https://github.com/forbole/bdjuno/pull/610)) Add support for gov `v1` proposals
- ([\#652](https://github.com/forbole/bdjuno/pull/652)) Update gov module parsing
- ([\#702](https://github.com/forbole/bdjuno/pull/702)) Add `message_type` module and store msg types inside `message_type` table, add `messages_by_type` function to allow to query messages by their types

## Version v4.0.0
## Notes
Expand Down
2 changes: 2 additions & 0 deletions cmd/migrate/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"github.com/spf13/cobra"

v3 "github.com/forbole/bdjuno/v4/cmd/migrate/v3"
v5 "github.com/forbole/bdjuno/v4/cmd/migrate/v5"
)

type Migrator func(parseCfg *parsecmdtypes.Config) error

var (
migrations = map[string]Migrator{
"v3": v3.RunMigration,
"v5": v5.RunMigration,
}
)

Expand Down
43 changes: 43 additions & 0 deletions cmd/migrate/v5/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package v5

import (
"fmt"

v5db "github.com/forbole/bdjuno/v4/database/migrate/v5"
parse "github.com/forbole/juno/v5/cmd/parse/types"
"github.com/forbole/juno/v5/database"
"github.com/forbole/juno/v5/database/postgresql"
"github.com/forbole/juno/v5/types/config"
)

// RunMigration runs the migrations to v5
func RunMigration(parseConfig *parse.Config) error {
cfg, err := GetConfig()
if err != nil {
return fmt.Errorf("error while reading config: %s", err)
}

// Migrate the database
err = migrateDb(cfg, parseConfig)
if err != nil {
return fmt.Errorf("error while migrating database: %s", err)
}

return nil
}

func migrateDb(cfg config.Config, parseConfig *parse.Config) error {
// Build the codec
encodingConfig := parseConfig.GetEncodingConfigBuilder()()

// Get the db
databaseCtx := database.NewContext(cfg.Database, encodingConfig, parseConfig.GetLogger())
db, err := postgresql.Builder(databaseCtx)
if err != nil {
return fmt.Errorf("error while building the db: %s", err)
}

// Build the migrator and perform the migrations
migrator := v5db.NewMigrator(db.(*postgresql.Database))
return migrator.Migrate()
}
29 changes: 29 additions & 0 deletions cmd/migrate/v5/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package v5

import (
"fmt"
"os"
"path"

"github.com/forbole/juno/v5/types/config"
"gopkg.in/yaml.v3"
)

// GetConfig returns the configuration reading it from the config.yaml file present inside the home directory
func GetConfig() (config.Config, error) {
file := path.Join(config.HomePath, "config.yaml")

// Make sure the path exists
if _, err := os.Stat(file); os.IsNotExist(err) {
return config.Config{}, fmt.Errorf("config file does not exist")
}

bz, err := os.ReadFile(file)
if err != nil {
return config.Config{}, fmt.Errorf("error while reading config file: %s", err)
}

var cfg config.Config
err = yaml.Unmarshal(bz, &cfg)
return cfg, err
}
7 changes: 4 additions & 3 deletions database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
tmtypes "github.com/cometbft/cometbft/types"
sdk "github.com/cosmos/cosmos-sdk/types"

simappparams "cosmossdk.io/simapp/params"
"github.com/forbole/juno/v5/types/params"

"github.com/stretchr/testify/suite"

_ "github.com/proullon/ramsql/driver"
Expand All @@ -42,7 +43,7 @@ type DbTestSuite struct {

func (suite *DbTestSuite) SetupTest() {
// Create the codec
codec := simappparams.MakeTestEncodingConfig()
codec := params.MakeTestEncodingConfig()

// Build the database
dbCfg := dbconfig.NewDatabaseConfig(
Expand All @@ -56,7 +57,7 @@ func (suite *DbTestSuite) SetupTest() {
100000,
100,
)
db, err := database.Builder(junodb.NewContext(dbCfg, &codec, logging.DefaultLogger()))
db, err := database.Builder(junodb.NewContext(dbCfg, codec, logging.DefaultLogger()))
suite.Require().NoError(err)

bigDipperDb, ok := (db).(*database.Db)
Expand Down
16 changes: 16 additions & 0 deletions database/message_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package database

import (
types "github.com/forbole/bdjuno/v4/types"
)

// SaveMessageType stores the given message type inside the database
func (db *Db) SaveMessageType(msg *types.MessageType) error {
stmt := `
INSERT INTO message_type(type, module, label, height)
VALUES ($1, $2, $3, $4)
ON CONFLICT (type) DO NOTHING`

_, err := db.SQL.Exec(stmt, msg.Type, msg.Module, msg.Label, msg.Height)
return err
}
53 changes: 53 additions & 0 deletions database/migrate/v5/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package v5

import (
"fmt"

utils "github.com/forbole/bdjuno/v4/modules/utils"
"github.com/forbole/bdjuno/v4/types"
)

// Migrate implements database.Migrator
func (db *Migrator) Migrate() error {
msgTypes, err := db.getMsgTypesFromMessageTable()
if err != nil {
return fmt.Errorf("error while getting message types rows: %s", err)
}

for _, msgType := range msgTypes {
// migrate message types
err = db.migrateMsgTypes(types.NewMessageType(
msgType.Type,
utils.GetModuleNameFromTypeURL(msgType.Type),
utils.GetMsgFromTypeURL(msgType.Type),
msgType.Height))

if err != nil {
return err
}
}
return nil
}

// getMsgTypesFromMessageTable retrieves messages types stored in database inside message table
func (db *Migrator) getMsgTypesFromMessageTable() ([]MessageRow, error) {
smt := "SELECT DISTINCT ON (type) type, transaction_hash, height FROM message ORDER BY type DESC"
var rows []MessageRow
err := db.SQL.Select(&rows, smt)
if err != nil {
return nil, err
}

return rows, nil
}

// migrateMsgTypes stores the given message type inside the database
func (db *Migrator) migrateMsgTypes(msg *types.MessageType) error {
stmt := `
INSERT INTO message_type(type, module, label, height)
VALUES ($1, $2, $3, $4)
ON CONFLICT (type) DO NOTHING`

_, err := db.SQL.Exec(stmt, msg.Type, msg.Module, msg.Label, msg.Height)
return err
}
21 changes: 21 additions & 0 deletions database/migrate/v5/migrator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package v5

import (
"github.com/jmoiron/sqlx"

"github.com/forbole/juno/v5/database"
"github.com/forbole/juno/v5/database/postgresql"
)

var _ database.Migrator = &Migrator{}

// Migrator represents the database migrator that should be used to migrate from v4 of the database to v5
type Migrator struct {
SQL *sqlx.DB
}

func NewMigrator(db *postgresql.Database) *Migrator {
return &Migrator{
SQL: db.SQL,
}
}
10 changes: 10 additions & 0 deletions database/migrate/v5/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package v5

type MessageRow struct {
TransactionHash string `db:"transaction_hash"`
Index int64 `db:"index"`
Type string `db:"type"`
Value string `db:"value"`
InvolvedAccountsAddresses string `db:"involved_accounts_addresses"`
Height int64 `db:"height"`
}
23 changes: 22 additions & 1 deletion database/schema/00-cosmos.sql
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,21 @@ CREATE INDEX transaction_hash_index ON transaction (hash);
CREATE INDEX transaction_height_index ON transaction (height);
CREATE INDEX transaction_partition_id_index ON transaction (partition_id);

CREATE TABLE message_type
(
type TEXT NOT NULL UNIQUE,
module TEXT NOT NULL,
label TEXT NOT NULL,
height BIGINT NOT NULL
);
CREATE INDEX message_type_module_index ON message_type (module);
CREATE INDEX message_type_type_index ON message_type (type);

CREATE TABLE message
(
transaction_hash TEXT NOT NULL,
index BIGINT NOT NULL,
type TEXT NOT NULL,
type TEXT NOT NULL REFERENCES message_type(type),
value JSONB NOT NULL,
involved_accounts_addresses TEXT[] NOT NULL,

Expand Down Expand Up @@ -101,6 +111,17 @@ WHERE (cardinality(types) = 0 OR type = ANY (types))
ORDER BY height DESC LIMIT "limit" OFFSET "offset"
$$ LANGUAGE sql STABLE;

CREATE FUNCTION messages_by_type(
types text [],
"limit" bigint DEFAULT 100,
"offset" bigint DEFAULT 0)
RETURNS SETOF message AS
$$
SELECT * FROM message
WHERE (cardinality(types) = 0 OR type = ANY (types))
ORDER BY height DESC LIMIT "limit" OFFSET "offset"
$$ LANGUAGE sql STABLE;

CREATE TABLE pruning
(
last_pruned_height BIGINT NOT NULL
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ go 1.20

require (
cosmossdk.io/math v1.2.0
cosmossdk.io/simapp v0.0.0-20230602123434-616841b9704d
github.com/cometbft/cometbft v0.37.2
github.com/cosmos/cosmos-sdk v0.47.4
github.com/cosmos/gogoproto v1.4.10
github.com/desmos-labs/desmos/v6 v6.1.0
github.com/forbole/juno/v5 v5.2.0
github.com/forbole/juno/v5 v5.2.1-0.20240201075935-851426ddd905
github.com/go-co-op/gocron v1.37.0
github.com/golangci/golangci-lint v1.55.2
github.com/jmoiron/sqlx v1.3.5
Expand Down Expand Up @@ -37,6 +36,7 @@ require (
cosmossdk.io/depinject v1.0.0-alpha.4 // indirect
cosmossdk.io/errors v1.0.0 // indirect
cosmossdk.io/log v1.1.1-0.20230704160919-88f2c830b0ca // indirect
cosmossdk.io/simapp v0.0.0-20230712090904-031162fbb96e // indirect
cosmossdk.io/tools/rosetta v0.2.1 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/4meepo/tagalign v1.3.3 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,8 @@ cosmossdk.io/log v1.1.1-0.20230704160919-88f2c830b0ca h1:msenprh2BLLRwNT7zN56TbB
cosmossdk.io/log v1.1.1-0.20230704160919-88f2c830b0ca/go.mod h1:PkIAKXZvaxrTRc++z53XMRvFk8AcGGWYHcMIPzVYX9c=
cosmossdk.io/math v1.2.0 h1:8gudhTkkD3NxOP2YyyJIYYmt6dQ55ZfJkDOaxXpy7Ig=
cosmossdk.io/math v1.2.0/go.mod h1:l2Gnda87F0su8a/7FEKJfFdJrM0JZRXQaohlgJeyQh0=
cosmossdk.io/simapp v0.0.0-20230602123434-616841b9704d h1:G24nV8KQ5tcSLJEYPUEpKxuX4usvpQg5r7LhCLYPs1o=
cosmossdk.io/simapp v0.0.0-20230602123434-616841b9704d/go.mod h1:xbjky3L3DJEylaho6gXplkrMvJ5sFgv+qNX+Nn47bzY=
cosmossdk.io/simapp v0.0.0-20230712090904-031162fbb96e h1:1V2wHlfP6CqektxgLggSHVA+7nxzXSbzK6caRQI+vE8=
cosmossdk.io/simapp v0.0.0-20230712090904-031162fbb96e/go.mod h1:Egfk2ftC9TVcJJZ/uyB8rlu9CSk9QP2OHEyvK6aRl3g=
cosmossdk.io/tools/rosetta v0.2.1 h1:ddOMatOH+pbxWbrGJKRAawdBkPYLfKXutK9IETnjYxw=
cosmossdk.io/tools/rosetta v0.2.1/go.mod h1:Pqdc1FdvkNV3LcNIkYWt2RQY6IP1ge6YWZk8MhhO9Hw=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
Expand Down Expand Up @@ -752,8 +752,8 @@ github.com/firefart/nonamedreturns v1.0.4/go.mod h1:TDhe/tjI1BXo48CmYbUduTV7BdIg
github.com/fjl/gencodec v0.0.0-20220412091415-8bb9e558978c/go.mod h1:AzA8Lj6YtixmJWL+wkKoBGsLWy9gFrAzi4g+5bCKwpY=
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0=
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/forbole/juno/v5 v5.2.0 h1:NEaUReU723OfArHpcDjTofsgxAiAMtwsa6ZPvQYUiVc=
github.com/forbole/juno/v5 v5.2.0/go.mod h1:wl9jxyKnDx6rElBkc6Zn8i9DInBpMwMeoX6Kxkp71KM=
github.com/forbole/juno/v5 v5.2.1-0.20240201075935-851426ddd905 h1:nmNDGWsEDFLFWl4+taRg9zDyzZaApb2Ge2+8XiwbaEk=
github.com/forbole/juno/v5 v5.2.1-0.20240201075935-851426ddd905/go.mod h1:XHTUsu81JTPEDvVrVgirag5pkV5sDBSdEUluBtJdFfk=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVBjqR7JHJk0brhHOZYGmfBYOrK0ZhYMEtBr4=
github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8/go.mod h1:ZhphrRTfi2rbfLwlschooIH4+wKKDR4Pdxhh+TRoA20=
Expand Down
4 changes: 2 additions & 2 deletions modules/actions/module.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package actions

import (
"cosmossdk.io/simapp/params"
"github.com/forbole/juno/v5/modules"
"github.com/forbole/juno/v5/node"
"github.com/forbole/juno/v5/node/builder"
nodeconfig "github.com/forbole/juno/v5/node/config"
"github.com/forbole/juno/v5/types/config"
"github.com/forbole/juno/v5/types/params"

modulestypes "github.com/forbole/bdjuno/v4/modules/types"
)
Expand All @@ -26,7 +26,7 @@ type Module struct {
sources *modulestypes.Sources
}

func NewModule(cfg config.Config, encodingConfig *params.EncodingConfig) *Module {
func NewModule(cfg config.Config, encodingConfig params.EncodingConfig) *Module {
bz, err := cfg.GetBytes()
if err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion modules/auth/handle_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (m *Module) HandleMsgExec(index int, _ *authz.MsgExec, _ int, executedMsg s

// HandleMsg implements modules.MessageModule
func (m *Module) HandleMsg(_ int, msg sdk.Msg, tx *juno.Tx) error {
addresses, err := m.messagesParser(m.cdc, msg)
addresses, err := m.messagesParser(tx)
if err != nil {
log.Error().Str("module", "auth").Err(err).
Str("operation", "refresh account").
Expand Down
27 changes: 27 additions & 0 deletions modules/message_type/handle_msg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package message_type

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/gogoproto/proto"
utils "github.com/forbole/bdjuno/v4/modules/utils"
msgtypes "github.com/forbole/bdjuno/v4/types"

"github.com/forbole/juno/v5/types"
)

// HandleMsg represents a message handler that stores the given message inside the proper database table
func (m *Module) HandleMsg(
index int, msg sdk.Msg, tx *types.Tx) error {
// Save message type
err := m.db.SaveMessageType(msgtypes.NewMessageType(
proto.MessageName(msg),
utils.GetModuleNameFromTypeURL(proto.MessageName(msg)),
utils.GetMsgFromTypeURL(proto.MessageName(msg)),
tx.Height))

if err != nil {
return err
}

return nil
}
Loading

0 comments on commit 82836df

Please sign in to comment.