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: add message_type module and store msg types inside message_type table #702

Merged
merged 5 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
15 changes: 8 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ go 1.20

require (
cosmossdk.io/math v1.2.0
cosmossdk.io/simapp v0.0.0-20230224204036-a6adb0821462
cosmossdk.io/simapp v0.0.0-20230712090904-031162fbb96e
github.com/cometbft/cometbft v0.37.2
github.com/cosmos/cosmos-sdk v0.47.2
github.com/cosmos/cosmos-sdk v0.47.4
github.com/cosmos/gogoproto v1.4.10
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 All @@ -35,7 +35,8 @@ require (
cosmossdk.io/api v0.3.1 // indirect
cosmossdk.io/core v0.5.1 // indirect
cosmossdk.io/depinject v1.0.0-alpha.3 // indirect
cosmossdk.io/errors v1.0.0-beta.7 // indirect
cosmossdk.io/errors v1.0.0 // indirect
cosmossdk.io/log v1.1.1-0.20230704160919-88f2c830b0ca // 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 Expand Up @@ -220,8 +221,8 @@ require (
github.com/nishanths/predeclared v0.2.2 // indirect
github.com/nunnatsa/ginkgolinter v0.14.1 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/petermattis/goid v0.0.0-20221215004737-a150e88a970d // indirect
github.com/pelletier/go-toml/v2 v2.0.7 // indirect
github.com/petermattis/goid v0.0.0-20230317030725-371a4b8eda08 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/polyfloyd/go-errorlint v1.4.5 // indirect
Expand Down Expand Up @@ -320,4 +321,4 @@ require (

// This is to avoid warnings while running the binary
// See here: https://github.com/desmos-labs/desmos/pull/1131#discussion_r1194090419
replace github.com/cosmos/gogoproto => github.com/cosmos/gogoproto v1.4.8
replace github.com/cosmos/gogoproto => github.com/cosmos/gogoproto v1.4.10
Loading
Loading