Skip to content

Commit

Permalink
Added the ability to query enabled modules
Browse files Browse the repository at this point in the history
See PR forbole#35
  • Loading branch information
HarleyAppleChoi authored Aug 24, 2020
1 parent ae638d6 commit e583515
Show file tree
Hide file tree
Showing 24 changed files with 686 additions and 119 deletions.
47 changes: 17 additions & 30 deletions cmd/bdjuno/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package main

import (
"time"
"github.com/forbole/bdjuno/x/mint"
x "github.com/forbole/bdjuno/x/types"

"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/desmos-labs/juno/config"
"github.com/desmos-labs/juno/executor"
"github.com/desmos-labs/juno/parse"
"github.com/desmos-labs/juno/parse/worker"
"github.com/desmos-labs/juno/version"
"github.com/forbole/bdjuno/database"
"github.com/forbole/bdjuno/x/auth"
Expand All @@ -19,7 +19,20 @@ import (
"github.com/forbole/bdjuno/x/pricefeed"
"github.com/forbole/bdjuno/x/staking"
"github.com/forbole/bdjuno/x/supply"
"github.com/go-co-op/gocron"
)

var (
modules = []x.Module{
auth.Module{},
bank.Module{},
consensus.Module{},
distribution.Module{},
gov.Module{},
mint.Module{},
pricefeed.Module{},
staking.Module{},
supply.Module{},
}
)

func main() {
Expand Down Expand Up @@ -61,31 +74,5 @@ func SetupConfig(prefix string) func(cfg *sdk.Config) {
}

func SetupModules() {
// Register genesis handlers
worker.RegisterGenesisHandler(auth.GenesisHandler)
worker.RegisterGenesisHandler(staking.GenesisHandler)
worker.RegisterGenesisHandler(gov.GenesisHandler)

// Register block handlers
worker.RegisterBlockHandler(staking.BlockHandler)

// Register msg handlers
worker.RegisterMsgHandler(staking.MsgHandler)
worker.RegisterMsgHandler(bank.MsgHandler)
worker.RegisterMsgHandler(staking.MsgHandler)
worker.RegisterMsgHandler(gov.MsgHandler)

// Register other operations
parse.RegisterAdditionalOperation(consensus.ListenOperation)
parse.RegisterAdditionalOperation(gov.OneShotOperation)

// Register periodic operations
scheduler := gocron.NewScheduler(time.UTC)
parse.RegisterAdditionalOperation(staking.PeriodicStakingOperations(scheduler))
parse.RegisterAdditionalOperation(auth.PeriodicAuthOperations(scheduler))
parse.RegisterAdditionalOperation(supply.PeriodicSupplyOperations(scheduler))
parse.RegisterAdditionalOperation(distribution.PeriodicDistributionOperations(scheduler))
parse.RegisterAdditionalOperation(pricefeed.PeriodicPriceFeedOperations(scheduler))

scheduler.StartAsync()
x.RegisterModules(modules)
}
8 changes: 5 additions & 3 deletions database/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (db BigDipperDb) SaveTallyResults(tallys []types.TallyResult) error {
tally.Timestamp)
}
query = query[:len(query)-1] // Remove trailing ","
query += " ON CONFLICT DO NOTHING"
_, err := db.Sql.Exec(query, param...)
if err != nil {
return err
Expand All @@ -87,7 +88,7 @@ func (db BigDipperDb) SaveTallyResults(tallys []types.TallyResult) error {
// SaveTallyResult insert a single row into tally_result table
func (db BigDipperDb) SaveTallyResult(tally types.TallyResult) error {
query := `INSERT INTO tally_result(proposal_id,yes,abstain,no,no_with_veto,height,timestamp) VALUES
($1,$2,$3,$4,$5,$6,$7)`
($1,$2,$3,$4,$5,$6,$7) ON CONFLICT DO NOTHING`
_, err := db.Sql.Exec(query, tally.ProposalID,
tally.Yes,
tally.Abstain,
Expand All @@ -104,7 +105,7 @@ func (db BigDipperDb) SaveTallyResult(tally types.TallyResult) error {
// SaveVote allows to save for the given height and the message vote
func (db BigDipperDb) SaveVote(vote types.Vote) error {
query := `INSERT INTO vote(proposal_id,voter,option,height,timestamp) VALUES
($1,$2,$3,$4,$5)`
($1,$2,$3,$4,$5) ON CONFLICT DO NOTHING`
_, err := db.Sql.Exec(query,
vote.ProposalID,
vote.Voter.String(),
Expand All @@ -120,7 +121,7 @@ func (db BigDipperDb) SaveVote(vote types.Vote) error {
// SaveDeposit allows to save for the given message deposit and height
func (db BigDipperDb) SaveDeposit(deposit types.Deposit) error {
query := `INSERT INTO deposit(proposal_id,depositor,amount,total_deposit,height,timestamp) VALUES
($1,$2,$3,$4,$5,$6)`
($1,$2,$3,$4,$5,$6) ON CONFLICT DO NOTHING`
_, err := db.Sql.Exec(query,
deposit.ProposalID,
deposit.Depositor.String(),
Expand Down Expand Up @@ -150,6 +151,7 @@ func (db BigDipperDb) SaveDeposits(deposits []types.Deposit) error {
deposit.Timestamp)
}
query = query[:len(query)-1] // Remove trailing ","
query += " ON CONFLICT DO NOTHING"
_, err := db.Sql.Exec(query, param...)
if err != nil {
return err
Expand Down
46 changes: 46 additions & 0 deletions database/types/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package types

// ModuleRow represents a single row inside the modules table
type ModuleRow struct {
Module string `db:"module_name"`
}

//NewModuleRow return a new instance of ModuleRow
func NewModuleRow(name string) ModuleRow {
return ModuleRow{Module: name}
}

// Equal return true if two moduleRow is equal
func (v ModuleRow) Equal(w ModuleRow) bool {
return v.Module == w.Module
}

// ModuleRows represent an array of ModulerRow
type ModuleRows []*ModuleRow

//NewModuleRows return a new instance of ModuleRows
func NewModuleRows(names []string) ModuleRows {
rows := make([]*ModuleRow, 0)
for _, name := range names {
rows = append(rows, &ModuleRow{Module: name})
}
return rows
}

// Equal return true if two ModulesRow is equal
func (v ModuleRows) Equal(w *ModuleRows) bool {
if w == nil {
return false
}

if len(v) != len(*w) {
return false
}

for index, val := range v {
if !val.Equal(*(*w)[index]) {
return false
}
}
return true
}
29 changes: 29 additions & 0 deletions database/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package database

import (
"fmt"
)

// UpdateEnableModules allows to save enabled module into the database
func (db BigDipperDb) InsertEnableModules(modules []string) error {
//clear table first
stmt := "DELETE FROM modules"
_, err := db.Sql.Exec(stmt)
if err != nil {
return err
}

var values []interface{}
stmt = `INSERT INTO modules (module_name) VALUES`
for key, value := range modules {
stmt += fmt.Sprintf("($%d),", key+1)
values = append(values, value)
}
stmt = stmt[:len(stmt)-1] //remove tailing ","
stmt += " ON CONFLICT DO NOTHING"
_, err = db.Sql.Exec(stmt, values...)
if err != nil {
return err
}
return nil
}
19 changes: 19 additions & 0 deletions database/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package database_test

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

func (suite *DbTestSuite) TestBigDipperDb_InsertEnableModules() {
modules := []string{"auth", "bank", "consensus", "distribution", "gov", "mint", "pricefeed", "staking", "supply"}
err := suite.database.InsertEnableModules(modules)
suite.Require().NoError(err)

var results types.ModuleRows
err = suite.database.Sqlx.Select(&results, "SELECT * FROM modules")
suite.Require().NoError(err)

expected := types.NewModuleRows(modules)
suite.Require().True(results.Equal(&expected))

}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/cosmos/cosmos-sdk v0.38.5
github.com/desmos-labs/juno v0.0.0-20200706074258-15a37786a810
github.com/go-co-op/gocron v0.2.0
github.com/gogo/protobuf v1.3.1
github.com/jmoiron/sqlx v1.2.1-0.20200324155115-ee514944af4b
github.com/lib/pq v1.3.0
github.com/proullon/ramsql v0.0.0-20181213202341-817cee58a244
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ github.com/tendermint/iavl v0.13.2/go.mod h1:vE1u0XAGXYjHykd4BLp8p/yivrw2PF1Tuol
github.com/tendermint/tendermint v0.33.2/go.mod h1:25DqB7YvV1tN3tHsjWoc2vFtlwICfrub9XO6UBO+4xk=
github.com/tendermint/tendermint v0.33.6 h1:W4UOsXY4ROJZ3TLLGVVv71VXD4WK2gJRb3gzeced+mg=
github.com/tendermint/tendermint v0.33.6/go.mod h1:0yUs9eIuuDq07nQql9BmI30FtYGcEC60Tu5JzB5IezM=
github.com/tendermint/tendermint v0.33.8 h1:Xxu4QhpqcomSE0iQDw1MqLgfsa8fqtPtWFJK6zZOVso=
github.com/tendermint/tm-db v0.4.1/go.mod h1:JsJ6qzYkCGiGwm5GHl/H5GLI9XLb6qZX7PRe425dHAY=
github.com/tendermint/tm-db v0.5.0 h1:qtM5UTr1dlRnHtDY6y7MZO5Di8XAE2j3lc/pCnKJ5hQ=
github.com/tendermint/tm-db v0.5.0/go.mod h1:lSq7q5WRR/njf1LnhiZ/lIJHk2S8Y1Zyq5oP/3o9C2U=
Expand Down
3 changes: 3 additions & 0 deletions schema/09-util.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE modules(
module_name TEXT
);
45 changes: 45 additions & 0 deletions x/auth/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package auth

import (
"github.com/desmos-labs/juno/parse"
juno "github.com/desmos-labs/juno/parse/worker"
x "github.com/forbole/bdjuno/x/types"
)

// Module represent /x/Auth module
type Module struct{}

// Name return the name of the module
func (m Module) Name() string {
return "auth"
}

// BlockHandlers return a list of block handler of the module
func (m Module) BlockHandlers() []juno.BlockHandler {
return []juno.BlockHandler{}
}

// TxHandlers return a list of TxHandlers of the module
func (m Module) TxHandlers() []juno.TxHandler {
return []juno.TxHandler{}
}

// MsgHandlers return a list of MsgHandlers of the module
func (m Module) MsgHandlers() []juno.MsgHandler {
return []juno.MsgHandler{}
}

// AdditionalOperations return a list of AdditionalOperations of the module
func (m Module) AdditionalOperations() []parse.AdditionalOperation {
return []parse.AdditionalOperation{}
}

// PeriodicOperations return a list of PeriodicOperations of the module
func (m Module) PeriodicOperations() []x.PerodicOperation {
return []x.PerodicOperation{PeriodicAuthOperations}
}

// GenesisHandlers return a list of GenesisHandlers of the module
func (m Module) GenesisHandlers() []juno.GenesisHandler {
return []juno.GenesisHandler{GenesisHandler}
}
45 changes: 45 additions & 0 deletions x/bank/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package bank

import (
"github.com/desmos-labs/juno/parse"
juno "github.com/desmos-labs/juno/parse/worker"
x "github.com/forbole/bdjuno/x/types"
)

// Module represent /x/Bank module
type Module struct{}

// Name return the name of the module
func (m Module) Name() string {
return "bank"
}

// BlockHandlers return a list of block handler of the module
func (m Module) BlockHandlers() []juno.BlockHandler {
return []juno.BlockHandler{}
}

// TxHandlers return a list of TxHandlers of the module
func (m Module) TxHandlers() []juno.TxHandler {
return []juno.TxHandler{}
}

// MsgHandlers return a list of MsgHandlers of the module
func (m Module) MsgHandlers() []juno.MsgHandler {
return []juno.MsgHandler{MsgHandler}
}

// AdditionalOperations return a list of AdditionalOperations of the module
func (m Module) AdditionalOperations() []parse.AdditionalOperation {
return []parse.AdditionalOperation{}
}

// PeriodicOperations return a list of PeriodicOperations of the module
func (m Module) PeriodicOperations() []x.PerodicOperation {
return []x.PerodicOperation{}
}

// GenesisHandlers return a list of GenesisHandlers of the module
func (m Module) GenesisHandlers() []juno.GenesisHandler {
return []juno.GenesisHandler{}
}
45 changes: 45 additions & 0 deletions x/consensus/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package consensus

import (
"github.com/desmos-labs/juno/parse"
juno "github.com/desmos-labs/juno/parse/worker"
x "github.com/forbole/bdjuno/x/types"
)

// Module represent /x/Consensus module
type Module struct{}

// Name return the name of the module
func (m Module) Name() string {
return "consensus"
}

// BlockHandlers return a list of block handler of the module
func (m Module) BlockHandlers() []juno.BlockHandler {
return []juno.BlockHandler{}
}

// TxHandlers return a list of TxHandlers of the module
func (m Module) TxHandlers() []juno.TxHandler {
return []juno.TxHandler{}
}

// MsgHandlers return a list of MsgHandlers of the module
func (m Module) MsgHandlers() []juno.MsgHandler {
return []juno.MsgHandler{}
}

// AdditionalOperations return a list of AdditionalOperations of the module
func (m Module) AdditionalOperations() []parse.AdditionalOperation {
return []parse.AdditionalOperation{ListenOperation}
}

// PeriodicOperations return a list of PeriodicOperations of the module
func (m Module) PeriodicOperations() []x.PerodicOperation {
return []x.PerodicOperation{}
}

// GenesisHandlers return a list of GenesisHandlers of the module
func (m Module) GenesisHandlers() []juno.GenesisHandler {
return []juno.GenesisHandler{}
}
Loading

0 comments on commit e583515

Please sign in to comment.