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 vesting account handling #232

Merged
merged 41 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
97698d5
x/ditr handleMsg add go routines for refreching rewards and commision
huichiaotsou Oct 7, 2021
9a5283e
Merge branch 'v2/cosmos/stargate' into v2/aaron/update_rewards_commis…
huichiaotsou Oct 7, 2021
8e43f3f
merge
huichiaotsou Oct 8, 2021
91176e8
Merge branch 'v2/aaron/update_rewards_commission' of https://github.c…
huichiaotsou Oct 8, 2021
cece5af
move interval check to handle_block
huichiaotsou Oct 9, 2021
371b1a9
update comment
huichiaotsou Oct 9, 2021
4e6ff33
make shouldUpdateDelegatorRewardsAmounts a function
huichiaotsou Oct 12, 2021
ba35512
remove uneccessary update/refresh in handle msg
huichiaotsou Oct 12, 2021
8758b7f
fix no return value for shouldUpdateDelegatorRewardsAmounts
huichiaotsou Oct 12, 2021
30f23c5
add schema vesting_account
huichiaotsou Oct 13, 2021
c1a3588
schema
huichiaotsou Oct 15, 2021
6354fab
Merge branch 'v2/cosmos/stargate' into v2/aaron/vesting_account
huichiaotsou Oct 15, 2021
d600690
add structure in auth/handle_genesis.go
huichiaotsou Oct 15, 2021
e85ecf0
add types for JSON handling and for auth types/auth
huichiaotsou Oct 15, 2021
73719ad
add get genesis vesting accounts method in auth_accounts.go
huichiaotsou Oct 15, 2021
0616f38
add db.SaveVestingAccounts
huichiaotsou Oct 15, 2021
1f9f209
fix schema
huichiaotsou Oct 15, 2021
cc055ba
add schema(move create type COIN to auth.sql)
huichiaotsou Oct 19, 2021
e0e4f64
implement GetGenesisVestingAccounts and use in HandleGenesis
huichiaotsou Oct 19, 2021
6a53241
implement SaveVestingAccounts and the storing of 3 diff. vesting acco…
huichiaotsou Oct 19, 2021
91bdf17
add structs in types/auth.go: ContinuousVestingAccount/ DelayedVestin…
huichiaotsou Oct 19, 2021
563f8ea
modify sql comments
huichiaotsou Oct 19, 2021
7a5d81a
Merge remote-tracking branch 'remotes/origin/v2/aaron/vesting_account…
huichiaotsou Oct 19, 2021
31efda6
delete GetGenesisVestingAccounts from auth_accounts.go
huichiaotsou Oct 19, 2021
918ba27
modif comments GetGenesisVestingAccounts
huichiaotsou Oct 19, 2021
6fcac22
comment modif: Build vestingAccounts Array
huichiaotsou Oct 19, 2021
1b31894
linter issue: vestingAccountId -> vestingAccountID
huichiaotsou Oct 19, 2021
c006195
add unit test
huichiaotsou Oct 19, 2021
d655091
linter: Id -> ID
huichiaotsou Oct 19, 2021
71eb9f2
lint
MonikaCat Oct 19, 2021
600230c
remove fmt.Println
huichiaotsou Oct 20, 2021
39fc74d
fix comments, remove redundant store-to-db methods
huichiaotsou Oct 20, 2021
9be3448
rm unit test for save vesting account ftm
huichiaotsou Oct 20, 2021
234f23b
schema: length TEXT -> BIGINT
huichiaotsou Oct 20, 2021
4df7892
fix go.mod
huichiaotsou Oct 20, 2021
ace57db
rm custom row types from database/types/auth.go
huichiaotsou Oct 20, 2021
2dcdeaf
merge 2 cases(continuous and deleyed vesting account; move storeVesti…
huichiaotsou Oct 20, 2021
decd364
correctly merge 2 cases
huichiaotsou Oct 20, 2021
4d7e1ec
revert DBG to prev. version
huichiaotsou Oct 20, 2021
83c661a
Updated SaveVestingAccounts
MonikaCat Oct 20, 2021
2d39fd4
Merge branch 'v2/cosmos/stargate' into v2/aaron/vesting_account
mergify[bot] Oct 20, 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
83 changes: 83 additions & 0 deletions database/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ package database

import (
"fmt"
"time"

"github.com/cosmos/cosmos-sdk/x/auth/vesting/exported"
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
dbtypes "github.com/forbole/bdjuno/v2/database/types"
dbutils "github.com/forbole/bdjuno/v2/database/utils"
"github.com/gogo/protobuf/proto"
"github.com/lib/pq"

"github.com/forbole/bdjuno/v2/types"
)
Expand Down Expand Up @@ -52,6 +58,83 @@ func (db *Db) saveAccounts(paramsNumber int, accounts []types.Account) error {
return nil
}

// SaveVestingAccounts saves the given vesting accounts inside the database
func (db *Db) SaveVestingAccounts(vestingAccounts []exported.VestingAccount) error {
if len(vestingAccounts) == 0 {
return nil
}

for _, account := range vestingAccounts {
switch vestingAccount := account.(type) {
case *vestingtypes.ContinuousVestingAccount, *vestingtypes.DelayedVestingAccount:
_, err := db.storeVestingAccount(account)
if err != nil {
return err
}

case *vestingtypes.PeriodicVestingAccount:
vestingAccountRowID, err := db.storeVestingAccount(account)
if err != nil {
return err
}
db.storeVestingPeriods(vestingAccountRowID, vestingAccount.VestingPeriods)
huichiaotsou marked this conversation as resolved.
Show resolved Hide resolved
}
}

return nil
}

func (db *Db) storeVestingAccount(account exported.VestingAccount) (int, error) {
stmt := `
INSERT INTO vesting_account (type, address, original_vesting, end_time, start_time)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (address) DO UPDATE
SET original_vesting = excluded.original_vesting,
end_time = excluded.end_time,
start_time = excluded.start_time
RETURNING id `

var vestingAccountRowID int
err := db.Sql.QueryRow(stmt,
proto.MessageName(account),
account.GetAddress().String(),
pq.Array(dbtypes.NewDbCoins(account.GetOriginalVesting())),
time.Unix(account.GetEndTime(), 0),
time.Unix(account.GetStartTime(), 0),
).Scan(&vestingAccountRowID)

if err != nil {
return vestingAccountRowID, fmt.Errorf("error while saving Vesting Account of type %v: %s", proto.MessageName(account), err)
}

return vestingAccountRowID, nil
}

// storeVestingPeriods handles storing the vesting periods of PeriodicVestingAccount type
func (db *Db) storeVestingPeriods(id int, vestingPeriods []vestingtypes.Period) error {
stmt := `
INSERT INTO vesting_period (vesting_account_id, period_order, length, amount)
VALUES `

var params []interface{}
for i, period := range vestingPeriods {
ai := i * 4
stmt += fmt.Sprintf("($%d,$%d,$%d,$%d),", ai+1, ai+2, ai+3, ai+4)

order := i
amount := pq.Array(dbtypes.NewDbCoins(period.Amount))
params = append(params, id, order, period.Length, amount)
}
stmt = stmt[:len(stmt)-1]

_, err := db.Sql.Exec(stmt, params...)
huichiaotsou marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return fmt.Errorf("error while saving vesting periods: %s", err)
}

return nil
}

// GetAccounts returns all the accounts that are currently stored inside the database.
func (db *Db) GetAccounts() ([]string, error) {
var rows []string
Expand Down
30 changes: 30 additions & 0 deletions database/schema/01-auth.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
CREATE TABLE account
(
address TEXT NOT NULL PRIMARY KEY
);

/* ---- Moved from bank.sql for vesting account usage ---- */
CREATE TYPE COIN AS
(
denom TEXT,
amount TEXT
);

/* ---- AUTH/ VESTING ACCOUNT ---- */
CREATE TABLE vesting_account
(
id SERIAL PRIMARY KEY NOT NULL,
type TEXT NOT NULL,
address TEXT NOT NULL REFERENCES account (address),
original_vesting COIN[] NOT NULL DEFAULT '{}',
end_time TIMESTAMP WITHOUT TIME ZONE NOT NULL,
start_time TIMESTAMP WITHOUT TIME ZONE
);
/* ---- start_time can be empty on DelayedVestingAccount ---- */

CREATE UNIQUE INDEX vesting_account_address_idx ON vesting_account (address);


CREATE TABLE vesting_period
(
vesting_account_id BIGINT NOT NULL REFERENCES vesting_account (id),
period_order BIGINT NOT NULL,
length BIGINT NOT NULL,
amount COIN[] NOT NULL DEFAULT '{}'
);
6 changes: 0 additions & 6 deletions database/schema/02-bank.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
CREATE TYPE COIN AS
(
denom TEXT,
amount TEXT
);

/* ---- SUPPLY ---- */

CREATE TABLE supply
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/spf13/cobra v1.1.3
github.com/stretchr/testify v1.7.0
github.com/tendermint/tendermint v0.34.12
google.golang.org/grpc v1.37.0
google.golang.org/grpc v1.38.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)

Expand Down
35 changes: 35 additions & 0 deletions modules/auth/auth_vesting_accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package auth

import (
"encoding/json"

"github.com/cosmos/cosmos-sdk/codec"
authttypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/auth/vesting/exported"
)

// GetGenesisVestingAccounts parses the given appState and returns the genesis vesting accounts
func GetGenesisVestingAccounts(appState map[string]json.RawMessage, cdc codec.Marshaler) ([]exported.VestingAccount, error) {
var authState authttypes.GenesisState
if err := cdc.UnmarshalJSON(appState[authttypes.ModuleName], &authState); err != nil {
return nil, err
}

// Build vestingAccounts Array
vestingAccounts := []exported.VestingAccount{}
for _, account := range authState.Accounts {
var accountI authttypes.AccountI
err := cdc.UnpackAny(account, &accountI)
if err != nil {
return nil, err
}

vestingAccount, ok := accountI.(exported.VestingAccount)
if !ok {
continue
}
vestingAccounts = append(vestingAccounts, vestingAccount)
}

return vestingAccounts, nil
}
15 changes: 13 additions & 2 deletions modules/auth/handle_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,28 @@ import (

// HandleGenesis implements modules.GenesisModule
func (m *Module) HandleGenesis(_ *tmtypes.GenesisDoc, appState map[string]json.RawMessage) error {
log.Debug().Str("module", "auth").Msg("parsing genesis")
huichiaotsou marked this conversation as resolved.
Show resolved Hide resolved

// Handle account addresses
log.Debug().Str("module", "auth").Msg("parsing genesis")
huichiaotsou marked this conversation as resolved.
Show resolved Hide resolved
accounts, err := GetGenesisAccounts(appState, m.cdc)
if err != nil {
return fmt.Errorf("error while getting genesis accounts: %s", err)
}

err = m.db.SaveAccounts(accounts)
if err != nil {
return fmt.Errorf("error while storing genesis accounts: %s", err)
}

// Handle storing vesting accounts
log.Debug().Str("module", "auth/vesting").Msg("parsing genesis")
huichiaotsou marked this conversation as resolved.
Show resolved Hide resolved
vestingAccounts, err := GetGenesisVestingAccounts(appState, m.cdc)
if err != nil {
return fmt.Errorf("error while getting genesis vesting accounts: %s", err)
}
err = m.db.SaveVestingAccounts(vestingAccounts)
if err != nil {
return fmt.Errorf("error while storing genesis vesting accounts: %s", err)
}

return nil
}