Skip to content

Commit

Permalink
improve accumulator module
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkCherepovskyi committed Oct 30, 2024
1 parent 1d4c4dc commit 00b7d29
Show file tree
Hide file tree
Showing 20 changed files with 176 additions and 66 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
build/

config.local.yaml
Dockerfile
# Coverage
coverage.*
vendor
32 changes: 0 additions & 32 deletions Dockerfile

This file was deleted.

9 changes: 8 additions & 1 deletion database/accumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (db *Db) SaveAdmin(address string, vestingCount, lastVestingTime, vestingPe
// -------------------------------------------------------------------------------------------------------------------

// SaveAccumulatorParams allows to store the given params inside the database
func (db *Db) SaveAccumulatorParams(params *types.MintParams) error {
func (db *Db) SaveAccumulatorParams(params *types.AccumulatorParams) error {
paramsBz, err := json.Marshal(&params.Params)
if err != nil {
return fmt.Errorf("error while marshaling accumulator params: %s", err)
Expand All @@ -52,3 +52,10 @@ func (db *Db) SaveAccumulatorParams(params *types.MintParams) error {

return nil
}

// GetAdmins returns all the admins that are currently stored inside the database.
func (db *Db) GetAdmins() ([]dbtypes.AdminVestingRow, error) {
var rows []dbtypes.AdminVestingRow
err := db.Sqlx.Select(&rows, `SELECT * FROM admins_vesting WHERE last_vesting_time + INTERVAL '1 second' * vesting_time > NOW();`)
return rows, err
}
1 change: 1 addition & 0 deletions database/schema/15-accumulator.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ CREATE TABLE accumulator_params
(
one_row_id BOOLEAN NOT NULL DEFAULT TRUE PRIMARY KEY,
params JSONB NOT NULL,
height BIGINT NOT NULL,
CHECK (one_row_id)
);
-- +migrate Down
Expand Down
30 changes: 30 additions & 0 deletions database/types/admin_vesting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package types

type AdminVestingRow struct {
ID int64 `db:"id"`
Address string `db:"address"`
VestingPeriod int64 `db:"vesting_period"`
RewardsPerPeriod DbCoins `db:"reward_per_period"`
LastVestingTime int64 `db:"last_vesting_time"`
VestingCounter int64 `db:"vesting_counter"`
VestingPeriodsCount int64 `db:"vesting_periods_count"`
Denom string `db:"denom"`
}

func NewAdminVestingRow(id int64, address string, vestingPeriod int64, rewardsPerPeriod DbCoins, lastVestingTime, vestingCounter int64, denom string) AdminVestingRow {
return AdminVestingRow{
ID: id,
Address: address,
VestingPeriod: vestingPeriod,
RewardsPerPeriod: rewardsPerPeriod,
LastVestingTime: lastVestingTime,
VestingCounter: vestingCounter,
Denom: denom,
}
}

func (r AdminVestingRow) Equal(s AdminVestingRow) bool {
return r.RewardsPerPeriod.Equal(&s.RewardsPerPeriod) &&
r.ID == s.ID &&
r.Address == s.Address
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/go-co-op/gocron v1.37.0
github.com/gogo/protobuf v1.3.3
github.com/golangci/golangci-lint v1.61.0
github.com/hyle-team/bridgeless-core v0.0.0-20240822124409-90dc63eee926
github.com/hyle-team/bridgeless-core/v12 v12.0.0-20241030111407-6377be1385ce
github.com/jmoiron/sqlx v1.3.5
github.com/lib/pq v1.10.9
github.com/pelletier/go-toml v1.9.5
Expand Down Expand Up @@ -335,7 +335,7 @@ require (

replace (
github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0
github.com/cosmos/cosmos-sdk => github.com/hyle-team/cosmos-sdk v0.46.26
github.com/cosmos/cosmos-sdk v0.46.13 => github.com/hyle-team/cosmos-sdk v0.46.26-0.20241030103636-3505e4eaa455
github.com/cosmos/ibc-go/v6 => github.com/hyle-team/ibc-go/v6 v6.1.6
github.com/forbole/juno/v4 => github.com/hyle-team/juno-bridgeless/v4 v4.0.0
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -765,10 +765,10 @@ github.com/holiman/uint256 v1.2.1/go.mod h1:y4ga/t+u+Xwd7CpDgZESaRcWy0I7XMlTMA25
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huin/goupnp v1.0.3 h1:N8No57ls+MnjlB+JPiCVSOyy/ot7MJTqlo7rn+NYSqQ=
github.com/huin/goupnp v1.0.3/go.mod h1:ZxNlw5WqJj6wSsRK5+YfflQGXYfccj5VgQsMNixHM7Y=
github.com/hyle-team/bridgeless-core v0.0.0-20240822124409-90dc63eee926 h1:cfsHbvMmRxVjxoApli5nZxI1R0OJOhMDlM6goDaZW5c=
github.com/hyle-team/bridgeless-core v0.0.0-20240822124409-90dc63eee926/go.mod h1:E416XZ8/nZ4Te3gzqYFcasErEvv8JHXpR9nMBMdxLI8=
github.com/hyle-team/cosmos-sdk v0.46.26 h1:ZB88ZV3aK/6r7Echo6lZOrwvCsQZSRaEMt9udjLXwnw=
github.com/hyle-team/cosmos-sdk v0.46.26/go.mod h1:JkEh06V4aORJl8/nrLelLCzc5xKPoDuIYgaF8M6AwFw=
github.com/hyle-team/bridgeless-core/v12 v12.0.0-20241030111407-6377be1385ce h1:FeHsjb9clxOf2z73w0Pr10q1WheOHX2hjm0KzxrKTTE=
github.com/hyle-team/bridgeless-core/v12 v12.0.0-20241030111407-6377be1385ce/go.mod h1:aBCCdhgclu3iIsfrVpXbaEeRzkvLT1MXYGy1a6FPVBc=
github.com/hyle-team/cosmos-sdk v0.46.26-0.20241030103636-3505e4eaa455 h1:IPxGu1hNMxBs7vVipBPYB89eSUIyD3NxLkYKBijBgXc=
github.com/hyle-team/cosmos-sdk v0.46.26-0.20241030103636-3505e4eaa455/go.mod h1:JkEh06V4aORJl8/nrLelLCzc5xKPoDuIYgaF8M6AwFw=
github.com/hyle-team/ibc-go/v6 v6.1.6 h1:vAkKQq1Ila+NscPDAjRiq9OvmoDxmHlkbycQh1ipng0=
github.com/hyle-team/ibc-go/v6 v6.1.6/go.mod h1:UO7H/uWsmiOMeU54Fj9rspvISRGvylOIZYNnXBT4YY4=
github.com/hyle-team/juno-bridgeless/v4 v4.0.0 h1:ECwMMCYYNdqNkuvKhqQ44GEhZZoxkhvMV88wKmbDPqE=
Expand Down
33 changes: 33 additions & 0 deletions modules/accumulator/handle_genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package accumulator

import (
"encoding/json"
"fmt"
accumulatortypes "github.com/cosmos/cosmos-sdk/x/accumulator/types"

tmtypes "github.com/tendermint/tendermint/types"

"github.com/forbole/bdjuno/v4/types"

"github.com/rs/zerolog/log"
)

// HandleGenesis implements modules.Module
func (m *Module) HandleGenesis(doc *tmtypes.GenesisDoc, appState map[string]json.RawMessage) error {
log.Debug().Str("module", "accumulator").Msg("parsing genesis")

// Read the genesis state
var genState accumulatortypes.GenesisState
err := m.cdc.UnmarshalJSON(appState[accumulatortypes.ModuleName], &genState)
if err != nil {
return fmt.Errorf("error while reading mint genesis data: %s", err)
}

// Save the params
err = m.db.SaveAccumulatorParams(types.NewAccumulatorParams(genState.Params, doc.InitialHeight))
if err != nil {
return fmt.Errorf("error while storing genesis mint params: %s", err)
}

return nil
}
41 changes: 41 additions & 0 deletions modules/accumulator/handle_msg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package accumulator

import (
"github.com/cosmos/cosmos-sdk/x/authz"
"github.com/rs/zerolog/log"

sdk "github.com/cosmos/cosmos-sdk/types"
accumulator "github.com/cosmos/cosmos-sdk/x/accumulator/types"
juno "github.com/forbole/juno/v4/types"
)

// HandleMsgExec implements modules.AuthzMessageModule
func (m *Module) HandleMsgExec(index int, _ *authz.MsgExec, _ int, executedMsg sdk.Msg, tx *juno.Tx) error {
return m.HandleMsg(index, executedMsg, tx)
}

// HandleMsg implements modules.MessageModule
func (m *Module) HandleMsg(_ int, msg sdk.Msg, tx *juno.Tx) error {
log.Debug().Str("module", "accumulator").Msg("handle msg")

switch cosmosMsg := msg.(type) {
case *accumulator.MsgAddAdmin:
return m.handleMsgAddAdmin(tx, cosmosMsg)
default:
break
}

return nil
}

// handleMsgAddAdmin save a new admin to db
func (m *Module) handleMsgAddAdmin(_ *juno.Tx, msg *accumulator.MsgAddAdmin) error {
return m.db.SaveAdmin(
msg.Address,
msg.VestingPeriodsCount,
0,
msg.VestingPeriod,
msg.RewardPerPeriod,
msg.Denom,
)
}
14 changes: 5 additions & 9 deletions modules/accumulator/handle_periodic_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@ package accumulator

import (
"fmt"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/go-co-op/gocron"
"github.com/rs/zerolog/log"
"math"
)

// RegisterPeriodicOperations implements modules.Module
func (m *Module) RegisterPeriodicOperations(scheduler *gocron.Scheduler) error {
log.Debug().Str("module", m.Name()).Msg("setting up periodic tasks")
pagination := &query.PageRequest{
Limit: math.MaxInt32,
}

logErr := log.Error().Str("module", m.Name())

Expand All @@ -25,16 +20,17 @@ func (m *Module) RegisterPeriodicOperations(scheduler *gocron.Scheduler) error {
return
}

admins, err := m.keeper.GetAdmins(pagination, height)
admins, err := m.db.GetAdmins()
if err != nil {
logErr.Err(err).Msg("unable to get nfts")
logErr.Err(err).Msg("unable to get admins")
return
}

for _, admin := range admins {
err = m.db.SaveAdmin(admin.Address, admin.VestingPeriodsCount, admin.LastVestingTime, admin.VestingPeriod, admin.RewardPerPeriod, admin.Denom)
vestingInfo, err := m.keeper.GetAdminByAddress(admin.Address, height)
err = m.db.SaveAdmin(admin.Address, vestingInfo.VestingPeriodsCount, vestingInfo.LastVestingTime, vestingInfo.VestingPeriod, vestingInfo.RewardPerPeriod, admin.Denom)
if err != nil {
logErr.Err(err).Msg("unable to save nft")
logErr.Err(err).Msg("unable to save admin vesting info")
}
}

Expand Down
3 changes: 2 additions & 1 deletion modules/accumulator/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
)

var (
_ modules.Module = &Module{}
_ modules.Module = &Module{}
_ modules.GenesisModule = &Module{}
)

// Module represents the x/accumulator module
Expand Down
19 changes: 16 additions & 3 deletions modules/accumulator/source/local/source.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package local

import (
"cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
accumulatortypes "github.com/cosmos/cosmos-sdk/x/accumulator/types"
"github.com/forbole/juno/v4/node/local"

accumulatorkeeper "github.com/forbole/bdjuno/v4/modules/accumulator/source"
"github.com/forbole/juno/v4/node/local"
)

var (
Expand All @@ -27,6 +26,20 @@ func NewSource(source *local.Source, nk accumulatortypes.QueryServer) *Source {
}
}

func (s Source) GetAdminByAddress(address string, height int64) (*accumulatortypes.Admin, error) {
ctx, err := s.LoadHeight(height)
if err != nil {
return nil, errors.Wrap(err, "failed to get height context")
}

response, err := s.q.GetAdminByAddress(ctx, &accumulatortypes.QueryAdminByAddress{Address: address})
if err != nil {
return nil, errors.Wrap(err, "failed to query all nfts")
}

return &response.Admin, nil
}

func (s Source) GetAdmins(pagination *query.PageRequest, height int64) ([]accumulatortypes.Admin, error) {
ctx, err := s.LoadHeight(height)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions modules/accumulator/source/remote/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,16 @@ func (s Source) GetAdmins(pagination *query.PageRequest, height int64) ([]accumu

return response.Admins, nil
}

func (s Source) GetAdminByAddress(address string, height int64) (*accumulatortypes.Admin, error) {
ctx := remote.GetHeightRequestContext(s.Ctx, height)

response, err := s.accumulatorClient.GetAdminByAddress(ctx, &accumulatortypes.QueryAdminByAddress{
Address: address,
})
if err != nil {
return nil, errors.Wrap(err, "failed to query all nfts")
}

return &response.Admin, nil
}
1 change: 1 addition & 0 deletions modules/accumulator/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ import (

type Source interface {
GetAdmins(req *query.PageRequest, height int64) ([]types.Admin, error)
GetAdminByAddress(address string, height int64) (*types.Admin, error)
}
18 changes: 9 additions & 9 deletions modules/bridge/handle_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package bridge

import (
"github.com/cosmos/cosmos-sdk/x/authz"
bridge "github.com/hyle-team/bridgeless-core/x/bridge/types"
bridge "github.com/hyle-team/bridgeless-core/v12/x/bridge/types"
"github.com/rs/zerolog/log"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -19,26 +19,26 @@ func (m *Module) HandleMsg(_ int, msg sdk.Msg, tx *juno.Tx) error {

switch cosmosMsg := msg.(type) {
case *bridge.MsgSubmitTransactions:
return nil
return m.handleMsgSubmitTransactions(tx, cosmosMsg)
// chains
case *bridge.MsgDeleteChain:
return nil
return m.handleMsgDeleteChain(tx, cosmosMsg)
case *bridge.MsgInsertChain:
return nil
return m.handleMsgInsertChain(tx, cosmosMsg)

// token info
case *bridge.MsgAddTokenInfo:
return nil
return m.handleMsgAddTokenInfo(tx, cosmosMsg)
case *bridge.MsgRemoveTokenInfo:
return nil
return m.handleMsgRemoveTokenInfo(tx, cosmosMsg)

// token
case *bridge.MsgUpdateToken:
return nil
return m.handleMsgUpdateToken(tx, cosmosMsg)
case *bridge.MsgDeleteToken:
return nil
return m.handleMsgDeleteToken(tx, cosmosMsg)
case *bridge.MsgInsertToken:
return nil
return m.handleMsgInsertToken(tx, cosmosMsg)

default:
break
Expand Down
2 changes: 1 addition & 1 deletion modules/bridge/handle_msg_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package bridge

import (
juno "github.com/forbole/juno/v4/types"
bridge "github.com/hyle-team/bridgeless-core/x/bridge/types"
bridge "github.com/hyle-team/bridgeless-core/v12/x/bridge/types"
)

// handleMsgInsertChain allows to properly handle a MsgRemoveTokenInfo
Expand Down
7 changes: 6 additions & 1 deletion modules/bridge/handle_msg_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package bridge

import (
juno "github.com/forbole/juno/v4/types"
bridge "github.com/hyle-team/bridgeless-core/x/bridge/types"
bridge "github.com/hyle-team/bridgeless-core/v12/x/bridge/types"
)

// handleMsgInsertToken allows to properly handle a MsgInsertToken
Expand All @@ -14,3 +14,8 @@ func (m *Module) handleMsgInsertToken(tx *juno.Tx, msg *bridge.MsgInsertToken) e
func (m *Module) handleMsgDeleteToken(tx *juno.Tx, msg *bridge.MsgDeleteToken) error {
return nil
}

// handleMsgUpdateToken allows to properly handle a MsgUpdateToken
func (m *Module) handleMsgUpdateToken(tx *juno.Tx, msg *bridge.MsgUpdateToken) error {
return nil
}
2 changes: 1 addition & 1 deletion modules/bridge/handle_msg_token_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package bridge

import (
juno "github.com/forbole/juno/v4/types"
bridge "github.com/hyle-team/bridgeless-core/x/bridge/types"
bridge "github.com/hyle-team/bridgeless-core/v12/x/bridge/types"
)

// handleMsgAddTokenInfo allows to properly handle a MsgAddTokenInfo
Expand Down
Loading

0 comments on commit 00b7d29

Please sign in to comment.