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: handle feegrant module #276

Merged
merged 50 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
6465956
register feegrant module
MonikaCat Nov 26, 2021
64f0a12
Add feegrant types
MonikaCat Nov 26, 2021
44b7d37
Create table
MonikaCat Nov 26, 2021
65af9dc
Add hadle msg
MonikaCat Nov 26, 2021
6d76d08
Updated handle msg
MonikaCat Nov 26, 2021
011b840
Updated fee grant types
MonikaCat Nov 26, 2021
7a7e6ac
Merge branch 'v2/cosmos/v0.44.x' of https://github.com/forbole/bdjuno…
MonikaCat Dec 2, 2021
eec9a8e
Updated feegrant schema
MonikaCat Dec 2, 2021
3013022
add fix cmd for feegrant allowance
huichiaotsou Dec 2, 2021
447ffd2
Update schema
MonikaCat Dec 2, 2021
fb9311c
Updated error msgs
MonikaCat Dec 2, 2021
0fe3080
Update comment
MonikaCat Dec 2, 2021
10cdda6
Update comment
MonikaCat Dec 2, 2021
29ecb69
Updated methods name
MonikaCat Dec 2, 2021
826f83c
rm space
MonikaCat Dec 2, 2021
196bf16
Update database/schema/12-feegrant.sql
MonikaCat Dec 3, 2021
daef885
Update database/feegrant.go
MonikaCat Dec 3, 2021
cd195ed
Store accounts before processing allowances
MonikaCat Dec 3, 2021
ec95dcd
Add feegrant types
MonikaCat Dec 3, 2021
7addbdf
Update methods with new feegrant types
MonikaCat Dec 3, 2021
0b0b33e
Updated hasura metadata
MonikaCat Dec 3, 2021
f80b807
Add block module
MonikaCat Dec 3, 2021
8053139
Updated go.mod
MonikaCat Dec 3, 2021
7cfb123
Updated feegrant.sql
MonikaCat Dec 7, 2021
ac894d3
Updated feegrant hasura metadata
MonikaCat Dec 7, 2021
2335982
Updated FeeGrant struct type to feegrant.Grant
MonikaCat Dec 7, 2021
3d5fc20
Update height param in removeExpiredFeeGrantAllowances method
MonikaCat Dec 7, 2021
b2874ff
lint
MonikaCat Dec 7, 2021
761e6fb
Add unit test
MonikaCat Dec 7, 2021
3bcb65f
Update feegrant.sql
MonikaCat Dec 7, 2021
038fd3d
Fix typo
MonikaCat Dec 7, 2021
43b577d
Update feegrant.sql
MonikaCat Dec 7, 2021
8b0d439
update address wrapper
MonikaCat Dec 7, 2021
a5ff508
.
MonikaCat Dec 8, 2021
a9b030d
Update test.go
MonikaCat Dec 8, 2021
f9b3cea
Rm unused import
MonikaCat Dec 8, 2021
d6df1ec
fixed tests and lint
RiccardoM Dec 8, 2021
972f8b2
Update modules/feegrant/handle_block.go
MonikaCat Dec 8, 2021
068d9a5
Update modules/feegrant/handle_block.go
MonikaCat Dec 8, 2021
1e14577
Update modules/feegrant/handle_msg.go
MonikaCat Dec 8, 2021
e0249a6
Update modules/feegrant/handle_msg.go
MonikaCat Dec 8, 2021
648f8dc
Update modules/feegrant/handle_msg.go
MonikaCat Dec 8, 2021
cab657e
Update modules/feegrant/handle_msg.go
MonikaCat Dec 8, 2021
c8a926f
Update modules/feegrant/module.go
MonikaCat Dec 8, 2021
67a9ac3
Update makefile
MonikaCat Dec 8, 2021
2ab49df
Merge branch 'm/feegrant-v0.44.x' of https://github.com/forbole/bdjun…
MonikaCat Dec 8, 2021
2eeac5a
Update feegrant tx parsing in cmd fix
MonikaCat Dec 9, 2021
b57f85a
Update cmd/fix/feegrant/allowance.go
MonikaCat Dec 10, 2021
a241136
Fix lint
MonikaCat Dec 10, 2021
ec8427d
removed unused QueryBlock method
RiccardoM Dec 13, 2021
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ stop-docker-test:

start-docker-test: stop-docker-test
@echo "Starting Docker container..."
@docker run --name bdjuno-test-db -e POSTGRES_USER=bdjuno -e POSTGRES_PASSWORD=password -e POSTGRES_DB=bdjuno -d -p 5433:5432 postgres
@docker run --name bdjuno-test-db -e POSTGRES_USER=bdjuno -e POSTGRES_PASSWORD=password -e POSTGRES_DB=bdjuno -d -p 6433:5432 postgres
MonikaCat marked this conversation as resolved.
Show resolved Hide resolved
.PHONY: start-docker-test

test-unit: start-docker-test
Expand Down
100 changes: 100 additions & 0 deletions cmd/fix/feegrant/allowance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package feegrant

import (
"encoding/hex"
"fmt"
"strings"

"github.com/forbole/bdjuno/v2/modules/feegrant"
"github.com/forbole/bdjuno/v2/utils"

"github.com/forbole/juno/v2/cmd/parse"
"github.com/forbole/juno/v2/types/config"
"github.com/spf13/cobra"

"github.com/forbole/bdjuno/v2/database"

feegranttypes "github.com/cosmos/cosmos-sdk/x/feegrant"
)

// allowanceCmd returns the Cobra command allowing to fix all things related to fee grant allowance
func allowanceCmd(parseConfig *parse.Config) *cobra.Command {
return &cobra.Command{
Use: "allowance",
Short: "Fix granted and revoked allowances to the latest height",
RunE: func(cmd *cobra.Command, args []string) error {
parseCtx, err := parse.GetParsingContext(parseConfig)
if err != nil {
return err
}

// Get the database
db := database.Cast(parseCtx.Database)

// Build feegrant module
feegrantModule := feegrant.NewModule(parseCtx.EncodingConfig.Marshaler, db)

// Get latest height
height, err := parseCtx.Node.LatestHeight()
if err != nil {
return fmt.Errorf("error while getting chain latest block height: %s", err)
}

startHeight := config.Cfg.Parser.StartHeight

// Handle messages realated to feegrant allowance of each block from the start height
for h := startHeight; h <= height; h++ {
MonikaCat marked this conversation as resolved.
Show resolved Hide resolved
err = refreshAllowance(parseCtx, h, feegrantModule)
if err != nil {
fmt.Printf("error while refreshing allowance: %s \n", err)
}
}

return nil
},
}
}

func refreshAllowance(parseCtx *parse.Context, blockHeight int64, feegrantModule *feegrant.Module) error {
// Get the block details
block, err := utils.QueryBlock(parseCtx.Node, blockHeight)
if err != nil {
return err
}

if len(block.Block.Txs) != 0 {
for _, tx := range block.Block.Txs {

// Get the tx details
junoTx, err := parseCtx.Node.Tx(hex.EncodeToString(tx.Hash()))
if err != nil {
return fmt.Errorf("error while getting tx details: %s", err)
}

// Handle feegrant module messages
for _, msg := range junoTx.GetMsgs() {
if msgGrantAllowance, ok := msg.(*feegranttypes.MsgGrantAllowance); ok {
fmt.Println("handling MsgGrantAllowance tx hash:", strings.ToUpper(hex.EncodeToString(tx.Hash())))

err = feegrantModule.HandleMsgGrantAllowance(junoTx, msgGrantAllowance)
if err != nil {
return fmt.Errorf("error while handling MsgGrantAllowance: %s", err)
}
}

if msgRevokeAllowance, ok := msg.(*feegranttypes.MsgRevokeAllowance); ok {
fmt.Println("handling MsgRevokeAllowance tx hash:", strings.ToUpper(hex.EncodeToString(tx.Hash())))

err = feegrantModule.HandleMsgRevokeAllowance(junoTx, msgRevokeAllowance)
if err != nil {
return fmt.Errorf("error while handling MsgRevokeAllowance: %s", err)
}
}

}

}
}

return nil
}
20 changes: 20 additions & 0 deletions cmd/fix/feegrant/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package feegrant

import (
"github.com/forbole/juno/v2/cmd/parse"
"github.com/spf13/cobra"
)

// NewFeegrantCmd returns the Cobra command that allows to fix all the things related to the x/feegrant module
func NewFeegrantCmd(parseConfig *parse.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "feegrant",
Short: "Fix things related to the x/feegrant module",
}

cmd.AddCommand(
allowanceCmd(parseConfig),
)

return cmd
}
2 changes: 2 additions & 0 deletions cmd/fix/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/spf13/cobra"

fixauth "github.com/forbole/bdjuno/v2/cmd/fix/auth"
fixfeegrant "github.com/forbole/bdjuno/v2/cmd/fix/feegrant"
fixgov "github.com/forbole/bdjuno/v2/cmd/fix/gov"
fixstaking "github.com/forbole/bdjuno/v2/cmd/fix/staking"
)
Expand All @@ -19,6 +20,7 @@ func NewFixCmd(parseCfg *parse.Config) *cobra.Command {

cmd.AddCommand(
fixauth.NewAuthCmd(parseCfg),
fixfeegrant.NewFeegrantCmd(parseCfg),
fixgov.NewGovCmd(parseCfg),
fixstaking.NewStakingCmd(parseCfg),
)
Expand Down
2 changes: 1 addition & 1 deletion database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (suite *DbTestSuite) SetupTest() {
dbCfg := dbconfig.NewDatabaseConfig(
"bdjuno",
"localhost",
5433,
6433,
MonikaCat marked this conversation as resolved.
Show resolved Hide resolved
"bdjuno",
"password",
"",
Expand Down
51 changes: 51 additions & 0 deletions database/feegrant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package database

import (
"fmt"

"github.com/cosmos/cosmos-sdk/codec"

"github.com/forbole/bdjuno/v2/types"
)

// SaveFeeGrantAllowance allows to store the fee grant allowances for the given block height
func (db *Db) SaveFeeGrantAllowance(allowance types.FeeGrant) error {
// Store the accounts
var accounts []types.Account
accounts = append(accounts, types.NewAccount(allowance.Granter), types.NewAccount(allowance.Grantee))
err := db.SaveAccounts(accounts)
if err != nil {
return fmt.Errorf("error while storing fee grant allowance accounts: %s", err)
}

stmt := `
INSERT INTO fee_grant_allowance(grantee_address, granter_address, allowance, height)
VALUES ($1, $2, $3, $4)
ON CONFLICT ON CONSTRAINT unique_fee_grant_allowance DO UPDATE
SET allowance = excluded.allowance,
height = excluded.height
WHERE fee_grant_allowance.height <= excluded.height`

allowanceJSON, err := codec.ProtoMarshalJSON(allowance.Allowance, nil)
if err != nil {
return fmt.Errorf("error while marshaling grant allowance: %s", err)
}
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved

_, err = db.Sql.Exec(stmt, allowance.Grantee, allowance.Granter, allowanceJSON, allowance.Height)
if err != nil {
return fmt.Errorf("error while saving fee grant allowance: %s", err)
}

return nil
}

// DeleteFeeGrantAllowance removes the fee grant allowance data from the database
func (db *Db) DeleteFeeGrantAllowance(allowance types.GrantRemoval) error {
stmt := `DELETE FROM fee_grant_allowance WHERE grantee_address = $1 AND granter_address = $2 AND height <= $3`
_, err := db.Sql.Exec(stmt, allowance.Grantee, allowance.Granter, allowance.Height)

if err != nil {
return fmt.Errorf("error while deleting grant allowance: %s", err)
}
return nil
}
72 changes: 72 additions & 0 deletions database/feegrant_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package database_test

import (
sdk "github.com/cosmos/cosmos-sdk/types"
feegranttypes "github.com/cosmos/cosmos-sdk/x/feegrant"

dbtypes "github.com/forbole/bdjuno/v2/database/types"
"github.com/forbole/bdjuno/v2/types"
)

func (suite *DbTestSuite) TestBigDipperDb_SaveFeeGrantAllowance() {
allowance := &feegranttypes.BasicAllowance{SpendLimit: nil, Expiration: nil}
granter, err := sdk.AccAddressFromBech32("cosmos1ltzt0z992ke6qgmtjxtygwzn36km4cy6cqdknt")
suite.Require().NoError(err)

grantee, err := sdk.AccAddressFromBech32("cosmos1re6zjpyczs0w7flrl6uacl0r4teqtyg62crjsn")
suite.Require().NoError(err)

feeGrant, err := feegranttypes.NewGrant(granter, grantee, allowance)
suite.Require().NoError(err)

// Store the allowance
err = suite.database.SaveFeeGrantAllowance(types.NewFeeGrant(feeGrant, 121622))
suite.Require().NoError(err)

// Test double insertion
err = suite.database.SaveFeeGrantAllowance(types.NewFeeGrant(feeGrant, 121622))
suite.Require().NoError(err, "storing existing grant allowance should return no error")

// Verify the data
var rows []dbtypes.FeeAllowanceRow
err = suite.database.Sqlx.Select(&rows, `SELECT * FROM fee_grant_allowance`)
suite.Require().NoError(err)
suite.Require().Len(rows, 1)
suite.Require().Equal(rows[0].Granter, granter.String())
suite.Require().Equal(rows[0].Grantee, grantee.String())
suite.Require().Equal(rows[0].Height, int64(121622))

var stored feegranttypes.FeeAllowanceI
err = suite.database.EncodingConfig.Marshaler.UnmarshalInterfaceJSON([]byte(rows[0].Allowance), &stored)
suite.Require().NoError(err)
suite.Require().Equal(allowance, stored)
}

func (suite *DbTestSuite) TestBigDipperDb_RemoveFeeGrantAllowance() {
allowance := &feegranttypes.BasicAllowance{SpendLimit: nil, Expiration: nil}
granter, err := sdk.AccAddressFromBech32("cosmos1ltzt0z992ke6qgmtjxtygwzn36km4cy6cqdknt")
suite.Require().NoError(err)

grantee, err := sdk.AccAddressFromBech32("cosmos1re6zjpyczs0w7flrl6uacl0r4teqtyg62crjsn")
suite.Require().NoError(err)

feeGrant, err := feegranttypes.NewGrant(granter, grantee, allowance)
suite.Require().NoError(err)

err = suite.database.SaveFeeGrantAllowance(types.NewFeeGrant(feeGrant, 121622))
suite.Require().NoError(err)

// Delete the data
err = suite.database.DeleteFeeGrantAllowance(types.NewGrantRemoval(
"cosmos1re6zjpyczs0w7flrl6uacl0r4teqtyg62crjsn",
"cosmos1ltzt0z992ke6qgmtjxtygwzn36km4cy6cqdknt",
122222,
))
suite.Require().NoError(err)

// verify the data
var count int
err = suite.database.Sql.QueryRow(`SELECT COUNT(*) FROM redelegation`).Scan(&count)
suite.Require().NoError(err)
suite.Require().Equal(0, count)
}
10 changes: 10 additions & 0 deletions database/schema/12-feegrant.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE fee_grant_allowance
(
id SERIAL NOT NULL PRIMARY KEY,
grantee_address TEXT NOT NULL REFERENCES account (address),
granter_address TEXT NOT NULL REFERENCES account (address),
allowance JSONB NOT NULL DEFAULT '{}'::JSONB,
height BIGINT NOT NULL,
CONSTRAINT unique_fee_grant_allowance UNIQUE(grantee_address, granter_address)
);
CREATE INDEX fee_grant_allowance_height_index ON fee_grant_allowance (height);
2 changes: 1 addition & 1 deletion database/types/coins.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (coins DbCoins) ToCoins() sdk.Coins {
return sdkCoins
}

//_______________________________________________________
// --------------------------------------------------------------------------------------------------------------------

// DbDecCoin represents the information stored inside the database about a single coin
type DbDecCoin struct {
Expand Down
10 changes: 10 additions & 0 deletions database/types/feegrant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package types

// FeeAllowanceRow represents a single row inside the fee_grant_allowance table
type FeeAllowanceRow struct {
ID uint64 `db:"id"`
Grantee string `db:"grantee_address"`
Granter string `db:"granter_address"`
Allowance string `db:"allowance"`
Height int64 `db:"height"`
}
2 changes: 1 addition & 1 deletion database/types/staking_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (v ValidatorInfoRow) Equal(w ValidatorInfoRow) bool {
v.Height == w.Height
}

//________________________________________________________________
// --------------------------------------------------------------------------------------------------------------------

// ValidatorDescriptionRow represent a row in validator_description
type ValidatorDescriptionRow struct {
Expand Down
7 changes: 1 addition & 6 deletions database/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ 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
Expand All @@ -18,7 +13,7 @@ func (v ModuleRow) Equal(w ModuleRow) bool {
// ModuleRows represent an array of ModulerRow
type ModuleRows []*ModuleRow

//NewModuleRows return a new instance of ModuleRows
// NewModuleRows return a new instance of ModuleRows
func NewModuleRows(names []string) ModuleRows {
rows := make([]*ModuleRow, 0)
for _, name := range names {
Expand Down
2 changes: 1 addition & 1 deletion database/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func (db *Db) InsertEnableModules(modules []string) error {
return nil
}

//clear table first
// Remove the existing modules
stmt := "DELETE FROM modules WHERE TRUE"
_, err := db.Sql.Exec(stmt)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module github.com/forbole/bdjuno/v2
go 1.16

require (
github.com/cosmos/cosmos-sdk v0.44.2
github.com/cosmos/cosmos-sdk v0.44.3
github.com/cosmos/gaia/v6 v6.0.0-rc1
github.com/forbole/juno/v2 v2.0.0-20211020184842-e358a33007ff
github.com/forbole/juno/v2 v2.0.0-20211122103136-7926db0202f2
github.com/go-co-op/gocron v1.10.0
github.com/gogo/protobuf v1.3.3
github.com/jmoiron/sqlx v1.2.1-0.20200324155115-ee514944af4b
Expand All @@ -15,7 +15,7 @@ require (
github.com/rs/zerolog v1.26.0
github.com/spf13/cobra v1.2.1
github.com/stretchr/testify v1.7.0
github.com/tendermint/tendermint v0.34.13
github.com/tendermint/tendermint v0.34.14
google.golang.org/grpc v1.40.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
Expand Down
Loading