From 97698d597fd1fef1023c78a0eaef7a6f01af7356 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Thu, 7 Oct 2021 14:01:35 +0800 Subject: [PATCH 01/35] x/ditr handleMsg add go routines for refreching rewards and commision --- modules/distribution/handle_block.go | 2 +- modules/distribution/handle_msg.go | 4 +++ .../distribution/utils_delegator_rewards.go | 32 ++++++++++--------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/modules/distribution/handle_block.go b/modules/distribution/handle_block.go index 1a6a01a42..5a3e4ee08 100644 --- a/modules/distribution/handle_block.go +++ b/modules/distribution/handle_block.go @@ -18,7 +18,7 @@ func (m *Module) HandleBlock(b *tmctypes.ResultBlock, _ []*juno.Tx, _ *tmctypes. go m.updateValidatorsCommissionAmounts(b.Block.Height) // Update the delegators commissions amounts - go m.refreshDelegatorsRewardsAmounts(b.Block.Height) + go m.refreshDelegatorsRewardsAmounts(b.Block.Height, true) return nil } diff --git a/modules/distribution/handle_msg.go b/modules/distribution/handle_msg.go index 2c23441f6..860458020 100644 --- a/modules/distribution/handle_msg.go +++ b/modules/distribution/handle_msg.go @@ -23,8 +23,12 @@ func (m *Module) HandleMsg(_ int, msg sdk.Msg, tx *juno.Tx) error { switch cosmosMsg := msg.(type) { case *distrtypes.MsgWithdrawValidatorCommission: delegatorAddr = cosmosMsg.GetSigners()[0].String() + go m.refreshDelegatorsRewardsAmounts(tx.Height, false) + go m.updateValidatorsCommissionAmounts(tx.Height) case *distrtypes.MsgWithdrawDelegatorReward: delegatorAddr = cosmosMsg.DelegatorAddress + go m.refreshDelegatorsRewardsAmounts(tx.Height, false) + go m.updateValidatorsCommissionAmounts(tx.Height) default: return nil } diff --git a/modules/distribution/utils_delegator_rewards.go b/modules/distribution/utils_delegator_rewards.go index 75ecce83e..a0babf377 100644 --- a/modules/distribution/utils_delegator_rewards.go +++ b/modules/distribution/utils_delegator_rewards.go @@ -10,21 +10,23 @@ import ( // refreshDelegatorsRewardsAmounts refreshes the rewards associated with all the delegators for the given height, // deleting the ones existing and downloading them from scratch. -func (m *Module) refreshDelegatorsRewardsAmounts(height int64) { - interval := m.cfg.DistributionFrequency - if interval == 0 { - log.Debug().Str("module", "distribution").Msg("delegator rewards refresh interval set to 0. Skipping refresh") - return - } - - hasRewards, err := m.db.HasDelegatorRewards() - if err != nil { - log.Error().Str("module", "distribution").Err(err).Int64("height", height). - Msg("error while checking delegators reward") - } - - if hasRewards && height%interval != 0 { - return +func (m *Module) refreshDelegatorsRewardsAmounts(height int64, checkInterval bool) { + if checkInterval { + interval := m.cfg.DistributionFrequency + if interval == 0 { + log.Debug().Str("module", "distribution").Msg("delegator rewards refresh interval set to 0. Skipping refresh") + return + } + + hasRewards, err := m.db.HasDelegatorRewards() + if err != nil { + log.Error().Str("module", "distribution").Err(err).Int64("height", height). + Msg("error while checking delegators reward") + } + + if hasRewards && height%interval != 0 { + return + } } // Get the delegators From cece5af2da671adf8905a04a7ba55216a923928d Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Sat, 9 Oct 2021 11:12:53 +0800 Subject: [PATCH 02/35] move interval check to handle_block --- modules/distribution/handle_block.go | 19 ++++++++++++++++++- modules/distribution/handle_msg.go | 4 ++-- .../distribution/utils_delegator_rewards.go | 19 +------------------ 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/modules/distribution/handle_block.go b/modules/distribution/handle_block.go index 5a3e4ee08..239a37e62 100644 --- a/modules/distribution/handle_block.go +++ b/modules/distribution/handle_block.go @@ -17,8 +17,25 @@ func (m *Module) HandleBlock(b *tmctypes.ResultBlock, _ []*juno.Tx, _ *tmctypes. // Update the validator commissions go m.updateValidatorsCommissionAmounts(b.Block.Height) + //get interval cfg and verify if func refreshDelegatorsRewardsAmounts() should be executed + interval := m.cfg.RewardsFrequency + if interval == 0 { + log.Debug().Str("module", "distribution").Msg("delegator rewards refresh interval set to 0. Skipping refresh") + return nil + } + + hasRewards, err := m.db.HasDelegatorRewards() + if err != nil { + log.Error().Str("module", "distribution").Err(err).Int64("height", b.Block.Height). + Msg("error while checking delegators reward") + } + + if hasRewards && b.Block.Height%interval != 0 { + return nil + } + // Update the delegators commissions amounts - go m.refreshDelegatorsRewardsAmounts(b.Block.Height, true) + go m.refreshDelegatorsRewardsAmounts(b.Block.Height) return nil } diff --git a/modules/distribution/handle_msg.go b/modules/distribution/handle_msg.go index 860458020..d0ee3ebaa 100644 --- a/modules/distribution/handle_msg.go +++ b/modules/distribution/handle_msg.go @@ -23,11 +23,11 @@ func (m *Module) HandleMsg(_ int, msg sdk.Msg, tx *juno.Tx) error { switch cosmosMsg := msg.(type) { case *distrtypes.MsgWithdrawValidatorCommission: delegatorAddr = cosmosMsg.GetSigners()[0].String() - go m.refreshDelegatorsRewardsAmounts(tx.Height, false) + go m.refreshDelegatorsRewardsAmounts(tx.Height) go m.updateValidatorsCommissionAmounts(tx.Height) case *distrtypes.MsgWithdrawDelegatorReward: delegatorAddr = cosmosMsg.DelegatorAddress - go m.refreshDelegatorsRewardsAmounts(tx.Height, false) + go m.refreshDelegatorsRewardsAmounts(tx.Height) go m.updateValidatorsCommissionAmounts(tx.Height) default: return nil diff --git a/modules/distribution/utils_delegator_rewards.go b/modules/distribution/utils_delegator_rewards.go index a8de0f699..c6bbba5b1 100644 --- a/modules/distribution/utils_delegator_rewards.go +++ b/modules/distribution/utils_delegator_rewards.go @@ -10,24 +10,7 @@ import ( // refreshDelegatorsRewardsAmounts refreshes the rewards associated with all the delegators for the given height, // deleting the ones existing and downloading them from scratch. -func (m *Module) refreshDelegatorsRewardsAmounts(height int64, checkInterval bool) { - if checkInterval { - interval := m.cfg.RewardsFrequency - if interval == 0 { - log.Debug().Str("module", "distribution").Msg("delegator rewards refresh interval set to 0. Skipping refresh") - return - } - - hasRewards, err := m.db.HasDelegatorRewards() - if err != nil { - log.Error().Str("module", "distribution").Err(err).Int64("height", height). - Msg("error while checking delegators reward") - } - - if hasRewards && height%interval != 0 { - return - } - } +func (m *Module) refreshDelegatorsRewardsAmounts(height int64) { // Get the delegators delegators, err := m.db.GetDelegators() From 371b1a94ddbd4bd53bb62570c2adf101a8416c94 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Sat, 9 Oct 2021 11:17:34 +0800 Subject: [PATCH 03/35] update comment --- modules/distribution/handle_block.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/distribution/handle_block.go b/modules/distribution/handle_block.go index 239a37e62..740dceb58 100644 --- a/modules/distribution/handle_block.go +++ b/modules/distribution/handle_block.go @@ -17,7 +17,7 @@ func (m *Module) HandleBlock(b *tmctypes.ResultBlock, _ []*juno.Tx, _ *tmctypes. // Update the validator commissions go m.updateValidatorsCommissionAmounts(b.Block.Height) - //get interval cfg and verify if func refreshDelegatorsRewardsAmounts() should be executed + // Get interval cfg which decides when to execute "go m.refreshDelegatorsRewardsAmounts(b.Block.Height)" interval := m.cfg.RewardsFrequency if interval == 0 { log.Debug().Str("module", "distribution").Msg("delegator rewards refresh interval set to 0. Skipping refresh") From 4e6ff3316cb7f5cc2a090843f5c975747ee3f599 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Tue, 12 Oct 2021 11:51:24 +0800 Subject: [PATCH 04/35] make shouldUpdateDelegatorRewardsAmounts a function --- modules/distribution/handle_block.go | 21 +++---------------- .../distribution/utils_delegator_rewards.go | 16 ++++++++++++++ 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/modules/distribution/handle_block.go b/modules/distribution/handle_block.go index 740dceb58..3e2594a99 100644 --- a/modules/distribution/handle_block.go +++ b/modules/distribution/handle_block.go @@ -17,26 +17,11 @@ func (m *Module) HandleBlock(b *tmctypes.ResultBlock, _ []*juno.Tx, _ *tmctypes. // Update the validator commissions go m.updateValidatorsCommissionAmounts(b.Block.Height) - // Get interval cfg which decides when to execute "go m.refreshDelegatorsRewardsAmounts(b.Block.Height)" - interval := m.cfg.RewardsFrequency - if interval == 0 { - log.Debug().Str("module", "distribution").Msg("delegator rewards refresh interval set to 0. Skipping refresh") - return nil + // Update the delegators commissions amounts upon reaching interval or no rewards saved yet + if m.shouldUpdateDelegatorRewardsAmounts(b.Block.Height) { + go m.refreshDelegatorsRewardsAmounts(b.Block.Height) } - hasRewards, err := m.db.HasDelegatorRewards() - if err != nil { - log.Error().Str("module", "distribution").Err(err).Int64("height", b.Block.Height). - Msg("error while checking delegators reward") - } - - if hasRewards && b.Block.Height%interval != 0 { - return nil - } - - // Update the delegators commissions amounts - go m.refreshDelegatorsRewardsAmounts(b.Block.Height) - return nil } diff --git a/modules/distribution/utils_delegator_rewards.go b/modules/distribution/utils_delegator_rewards.go index c6bbba5b1..68af04ffa 100644 --- a/modules/distribution/utils_delegator_rewards.go +++ b/modules/distribution/utils_delegator_rewards.go @@ -39,6 +39,22 @@ func (m *Module) refreshDelegatorsRewardsAmounts(height int64) { } } +// shouldUpdateDelegatorRewardsAmounts tells whether or not the delegatos rewards amounts should be updated at the given height +func (m *Module) shouldUpdateDelegatorRewardsAmounts(height int64) bool { + interval := m.cfg.RewardsFrequency + if interval == 0 { + log.Debug().Str("module", "distribution").Msg("delegator rewards refresh interval set to 0. Skipping refresh") + } + + hasRewards, err := m.db.HasDelegatorRewards() + if err != nil { + log.Error().Str("module", "distribution").Err(err).Int64("height", height). + Msg("error while checking delegators reward") + } + + return !hasRewards || height%interval == 0 +} + // RefreshDelegatorRewards refreshes the rewards associated to the given delegator for the given height, // deleting the ones existing and downloading them from scratch. func (m *Module) RefreshDelegatorRewards(height int64, delegator string) error { From ba35512d3eb138becee8bf67905f1b64051534cc Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Tue, 12 Oct 2021 12:19:28 +0800 Subject: [PATCH 05/35] remove uneccessary update/refresh in handle msg --- modules/distribution/handle_msg.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/distribution/handle_msg.go b/modules/distribution/handle_msg.go index d0ee3ebaa..3c0187a2d 100644 --- a/modules/distribution/handle_msg.go +++ b/modules/distribution/handle_msg.go @@ -23,12 +23,10 @@ func (m *Module) HandleMsg(_ int, msg sdk.Msg, tx *juno.Tx) error { switch cosmosMsg := msg.(type) { case *distrtypes.MsgWithdrawValidatorCommission: delegatorAddr = cosmosMsg.GetSigners()[0].String() - go m.refreshDelegatorsRewardsAmounts(tx.Height) go m.updateValidatorsCommissionAmounts(tx.Height) case *distrtypes.MsgWithdrawDelegatorReward: delegatorAddr = cosmosMsg.DelegatorAddress go m.refreshDelegatorsRewardsAmounts(tx.Height) - go m.updateValidatorsCommissionAmounts(tx.Height) default: return nil } From 8758b7fae6c97051d89f5f15a3d4f1a25f0dda02 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Tue, 12 Oct 2021 19:23:12 +0800 Subject: [PATCH 06/35] fix no return value for shouldUpdateDelegatorRewardsAmounts --- modules/distribution/utils_delegator_rewards.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/distribution/utils_delegator_rewards.go b/modules/distribution/utils_delegator_rewards.go index 68af04ffa..e20cbff41 100644 --- a/modules/distribution/utils_delegator_rewards.go +++ b/modules/distribution/utils_delegator_rewards.go @@ -44,12 +44,14 @@ func (m *Module) shouldUpdateDelegatorRewardsAmounts(height int64) bool { interval := m.cfg.RewardsFrequency if interval == 0 { log.Debug().Str("module", "distribution").Msg("delegator rewards refresh interval set to 0. Skipping refresh") + return false } hasRewards, err := m.db.HasDelegatorRewards() if err != nil { log.Error().Str("module", "distribution").Err(err).Int64("height", height). Msg("error while checking delegators reward") + return false } return !hasRewards || height%interval == 0 From 30f23c561001b6f528ec0995814d0bc06629557e Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Wed, 13 Oct 2021 19:00:15 +0800 Subject: [PATCH 07/35] add schema vesting_account --- database/schema/01-auth.sql | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/database/schema/01-auth.sql b/database/schema/01-auth.sql index e2465a40b..4cb95b403 100644 --- a/database/schema/01-auth.sql +++ b/database/schema/01-auth.sql @@ -1,4 +1,19 @@ CREATE TABLE account ( address TEXT NOT NULL PRIMARY KEY +); + +CREATE TYPE VESTING_PERIOD +( + length TEXT NOT NULL, + amounts COIN[] NOT NULL DEFAULT '{}' +); + +CREATE TABLE vesting_account +( + address TEXT NOT NULL REFERENCES account (address) PRIMARY KEY, + original_vesting COIN[] NOT NULL DEFAULT '{}', + end_time TIMESTAMP NOT NULL, + start_time TIMESTAMP NOT NULL, + vesting_periods VESTING_PERIOD[] NOT NULL DEFAULT '{}' ); \ No newline at end of file From c1a358831086ef488a7931a0d628bb6ef397aa2e Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Fri, 15 Oct 2021 10:14:04 +0800 Subject: [PATCH 08/35] schema --- database/schema/01-auth.sql | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/database/schema/01-auth.sql b/database/schema/01-auth.sql index 4cb95b403..40ea92dd3 100644 --- a/database/schema/01-auth.sql +++ b/database/schema/01-auth.sql @@ -3,17 +3,12 @@ CREATE TABLE account address TEXT NOT NULL PRIMARY KEY ); -CREATE TYPE VESTING_PERIOD -( - length TEXT NOT NULL, - amounts COIN[] NOT NULL DEFAULT '{}' -); CREATE TABLE vesting_account ( - address TEXT NOT NULL REFERENCES account (address) PRIMARY KEY, - original_vesting COIN[] NOT NULL DEFAULT '{}', - end_time TIMESTAMP NOT NULL, - start_time TIMESTAMP NOT NULL, - vesting_periods VESTING_PERIOD[] NOT NULL DEFAULT '{}' + address TEXT NOT NULL REFERENCES account (address) PRIMARY KEY, + original_vesting JSONB NOT NULL DEFAULT '{}', + end_time TIMESTAMP NOT NULL, + start_time TIMESTAMP NOT NULL, + vesting_periods JSONB NOT NULL DEFAULT '{}' ); \ No newline at end of file From d60069081d979184adf0f3090933cc1dc457cdde Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Fri, 15 Oct 2021 10:22:55 +0800 Subject: [PATCH 09/35] add structure in auth/handle_genesis.go --- modules/auth/handle_genesis.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/modules/auth/handle_genesis.go b/modules/auth/handle_genesis.go index 00ab20945..d6e7b0e59 100644 --- a/modules/auth/handle_genesis.go +++ b/modules/auth/handle_genesis.go @@ -13,6 +13,7 @@ import ( func (m *Module) HandleGenesis(_ *tmtypes.GenesisDoc, appState map[string]json.RawMessage) error { log.Debug().Str("module", "auth").Msg("parsing genesis") + // Handle Genesis account addresses accounts, err := GetGenesisAccounts(appState, m.cdc) if err != nil { return fmt.Errorf("error while getting genesis accounts: %s", err) @@ -23,5 +24,17 @@ func (m *Module) HandleGenesis(_ *tmtypes.GenesisDoc, appState map[string]json.R return fmt.Errorf("error while storing genesis accounts: %s", err) } + // Handle Genesis vesting account details + vestingAccounts, err := GetGenesisVestingAccounts(appState) + if err != nil { + return fmt.Errorf("error while getting genesis vesting accounts: %s", err) + } + + paramsNumber := 5 + err = m.db.SaveVestingAccounts(paramsNumber, vestingAccounts) + if err != nil { + return fmt.Errorf("error while storing genesis vesting accounts: %s", err) + } + return nil } From e85ecf095e2f506cf4c35e35a4ba95e562a6bf28 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Fri, 15 Oct 2021 10:26:11 +0800 Subject: [PATCH 10/35] add types for JSON handling and for auth types/auth --- modules/auth/types.go | 31 +++++++++++++++++++++++++++ types/auth.go | 49 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 modules/auth/types.go diff --git a/modules/auth/types.go b/modules/auth/types.go new file mode 100644 index 000000000..0259d6974 --- /dev/null +++ b/modules/auth/types.go @@ -0,0 +1,31 @@ +package auth + +import sdk "github.com/cosmos/cosmos-sdk/types" + +// --- Types are defined according to genesis file structure --- + +type Accounts struct { + Accounts []AccountDetails `json:"accounts"` +} + +type AccountDetails struct { + AccountType string `json:"@type"` + BaseVestingAccount BaseVestingAccount `json:"base_vesting_account"` + StartTime string `json:"start_time"` + VestingPeriods []Period `json:"vesting_periods"` +} + +type BaseVestingAccount struct { + BaseAccount BaseAccount `json:"base_account"` + OriginalVesting []sdk.Coin `json:"original_vesting"` + EndTime string `json:"end_time"` +} + +type BaseAccount struct { + Address string `json:"address"` +} + +type Period struct { + Length string `json:"length"` + Amount []sdk.Coin `json:"amount"` +} diff --git a/types/auth.go b/types/auth.go index 6c06f7434..3741f7714 100644 --- a/types/auth.go +++ b/types/auth.go @@ -1,5 +1,7 @@ package types +import sdk "github.com/cosmos/cosmos-sdk/types" + // Account represents a chain account type Account struct { Address string @@ -11,3 +13,50 @@ func NewAccount(address string) Account { Address: address, } } + +// --------------- For Vesting Account --------------- + +// Account represents a chain account +type VestingAccount struct { + Address string + OriginalVesting sdk.Coins + EndTime string + StartTime string + VestingPeriods []VestingPeriod +} + +// NewAccount builds a new VestingAccount instance +func NewVestingAccount(address string, originalVesting sdk.Coins, endTime string, startTime string, vestingPeriods []VestingPeriod) VestingAccount { + return VestingAccount{ + Address: address, + OriginalVesting: originalVesting, + EndTime: endTime, + StartTime: startTime, + VestingPeriods: vestingPeriods, + } +} + +func (u VestingAccount) Equal(v VestingAccount) bool { + for index, periodU := range u.VestingPeriods { + periodV := v.VestingPeriods[index] + if periodU.Amounts.String() != periodV.Amounts.String() { + return false + } + } + return u.Address == v.Address && + u.OriginalVesting.String() == v.OriginalVesting.String() && + u.EndTime == v.EndTime && + u.StartTime == v.StartTime +} + +type VestingPeriod struct { + Length string + Amounts sdk.Coins +} + +func NewVestingPeriod(length string, amounts sdk.Coins) VestingPeriod { + return VestingPeriod{ + Length: length, + Amounts: amounts, + } +} From 73719ad80eb6b2f2dc18942469ffbc325cc7a97e Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Fri, 15 Oct 2021 10:27:01 +0800 Subject: [PATCH 11/35] add get genesis vesting accounts method in auth_accounts.go --- modules/auth/auth_accounts.go | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/modules/auth/auth_accounts.go b/modules/auth/auth_accounts.go index 767f34062..ca8b5bde2 100644 --- a/modules/auth/auth_accounts.go +++ b/modules/auth/auth_accounts.go @@ -2,6 +2,7 @@ package auth import ( "encoding/json" + "strings" "github.com/cosmos/cosmos-sdk/codec" authttypes "github.com/cosmos/cosmos-sdk/x/auth/types" @@ -32,6 +33,42 @@ func GetGenesisAccounts(appState map[string]json.RawMessage, cdc codec.Marshaler return accounts, nil } +// GetGenesisVestingAccounts parses the given appState and returns the desired genesis vesting account details +func GetGenesisVestingAccounts(appState map[string]json.RawMessage) ([]types.VestingAccount, error) { + log.Debug().Str("module", "auth (vesting)").Msg("parsing genesis") + + var authState Accounts + if err := json.Unmarshal(appState[authttypes.ModuleName], &authState); err != nil { + return nil, err + } + + // Store the accounts + accounts := []types.VestingAccount{} + for _, account := range authState.Accounts { + accountType := strings.Split(account.AccountType, ".")[3] + + if accountType == "PeriodicVestingAccount" { + // Prepare vesting periods array for creating new Account instance + vestingPeriods := make([]types.VestingPeriod, len(account.VestingPeriods)) + for index, period := range account.VestingPeriods { + vestingPeriods[index] = types.NewVestingPeriod(period.Length, period.Amount) + } + + // Create new Account instance + vestingAccount := types.NewVestingAccount( + account.BaseVestingAccount.BaseAccount.Address, + account.BaseVestingAccount.OriginalVesting, + account.BaseVestingAccount.EndTime, + account.StartTime, + vestingPeriods, + ) + accounts = append(accounts, vestingAccount) + } + } + + return accounts, nil +} + // -------------------------------------------------------------------------------------------------------------------- // GetAccounts returns the account data for the given addresses From 0616f38d9b8f9a86b7e381200440c31b8a68e59e Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Fri, 15 Oct 2021 10:27:54 +0800 Subject: [PATCH 12/35] add db.SaveVestingAccounts --- database/auth.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/database/auth.go b/database/auth.go index d513da6bd..2cf693627 100644 --- a/database/auth.go +++ b/database/auth.go @@ -1,6 +1,7 @@ package database import ( + "encoding/json" "fmt" dbutils "github.com/forbole/bdjuno/v2/database/utils" @@ -52,6 +53,41 @@ func (db *Db) saveAccounts(paramsNumber int, accounts []types.Account) error { return nil } +func (db *Db) SaveVestingAccounts(paramsNumber int, vestingAccount []types.VestingAccount) error { + if len(vestingAccount) == 0 { + return nil + } + stmt := `INSERT INTO vesting_account (address, original_vesting, end_time, start_time, vesting_periods) VALUES ` + var params []interface{} + + for i, account := range vestingAccount { + ai := i * paramsNumber + stmt += fmt.Sprintf("($%d,$%d,$%d,$%d,$%d),", ai+1, ai+2, ai+3, ai+4, ai+5) + + originalVestingBz, err := json.Marshal(account.OriginalVesting) + if err != nil { + return err + } + + VestingPeriodsBz, err := json.Marshal(account.VestingPeriods) + if err != nil { + return err + } + + params = append(params, account.Address, string(originalVestingBz), account.EndTime, account.StartTime, string(VestingPeriodsBz)) + } + + stmt = stmt[:len(stmt)-1] + stmt += `ON CONFLICT (address) DO NOTHING` + + _, err := db.Sql.Exec(stmt, params...) + if err != nil { + return err + } + + return nil +} + // GetAccounts returns all the accounts that are currently stored inside the database. func (db *Db) GetAccounts() ([]string, error) { var rows []string From 1f9f209a6728d0f8ae7837184237b381bdb4c43f Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Fri, 15 Oct 2021 19:52:55 +0800 Subject: [PATCH 13/35] fix schema --- database/schema/01-auth.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/database/schema/01-auth.sql b/database/schema/01-auth.sql index 40ea92dd3..9f594f69e 100644 --- a/database/schema/01-auth.sql +++ b/database/schema/01-auth.sql @@ -8,7 +8,7 @@ CREATE TABLE vesting_account ( address TEXT NOT NULL REFERENCES account (address) PRIMARY KEY, original_vesting JSONB NOT NULL DEFAULT '{}', - end_time TIMESTAMP NOT NULL, - start_time TIMESTAMP NOT NULL, + end_time TEXT NOT NULL, + start_time TEXT NOT NULL, vesting_periods JSONB NOT NULL DEFAULT '{}' ); \ No newline at end of file From cc055ba7dcd45a51b3b5216d17d44977267235ff Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Tue, 19 Oct 2021 10:42:27 +0800 Subject: [PATCH 14/35] add schema(move create type COIN to auth.sql) --- database/schema/01-auth.sql | 29 +++++++++++++++++++++++++++++ database/schema/02-bank.sql | 6 ------ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/database/schema/01-auth.sql b/database/schema/01-auth.sql index e2465a40b..3e1adb4bb 100644 --- a/database/schema/01-auth.sql +++ b/database/schema/01-auth.sql @@ -1,4 +1,33 @@ CREATE TABLE account ( address TEXT NOT NULL PRIMARY KEY +); + +/* ---- Moved from back.sql for vesting account usage ---- */ +CREATE TYPE COIN AS +( + denom TEXT, + amount TEXT +); + +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 INT NOT NULL REFERENCES vesting_account (id), + period_order INT NOT NULL, + length TEXT NOT NULL, + amount COIN[] NOT NULL DEFAULT '{}' ); \ No newline at end of file diff --git a/database/schema/02-bank.sql b/database/schema/02-bank.sql index 4fc688498..9a2408d1c 100644 --- a/database/schema/02-bank.sql +++ b/database/schema/02-bank.sql @@ -1,9 +1,3 @@ -CREATE TYPE COIN AS -( - denom TEXT, - amount TEXT -); - /* ---- SUPPLY ---- */ CREATE TABLE supply From e0e4f64a494afefe4cb2ea56d38ca903f6b4c892 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Tue, 19 Oct 2021 10:55:37 +0800 Subject: [PATCH 15/35] implement GetGenesisVestingAccounts and use in HandleGenesis --- modules/auth/auth_vesting_accounts.go | 35 +++++++++++++++++++++++++++ modules/auth/handle_genesis.go | 15 ++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 modules/auth/auth_vesting_accounts.go diff --git a/modules/auth/auth_vesting_accounts.go b/modules/auth/auth_vesting_accounts.go new file mode 100644 index 000000000..9fa74c511 --- /dev/null +++ b/modules/auth/auth_vesting_accounts.go @@ -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" +) + +// GetGenesisAccounts parses the given appState and returns the genesis 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 + } + + // Create VestingAccount 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 +} diff --git a/modules/auth/handle_genesis.go b/modules/auth/handle_genesis.go index 00ab20945..436e5c1b1 100644 --- a/modules/auth/handle_genesis.go +++ b/modules/auth/handle_genesis.go @@ -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") + // Handle account addresses + log.Debug().Str("module", "auth").Msg("parsing genesis") 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") + 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 } From 6a532412a2160fd509dad20fb3904da00bd4baaf Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Tue, 19 Oct 2021 11:06:15 +0800 Subject: [PATCH 16/35] implement SaveVestingAccounts and the storing of 3 diff. vesting accounts into DB --- database/auth.go | 147 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 146 insertions(+), 1 deletion(-) diff --git a/database/auth.go b/database/auth.go index d513da6bd..cb59ee427 100644 --- a/database/auth.go +++ b/database/auth.go @@ -2,13 +2,18 @@ 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/lib/pq" "github.com/forbole/bdjuno/v2/types" ) -// SaveAccounts saves the given accounts inside the database +// SaveAccounts saves the given account addresses inside the database func (db *Db) SaveAccounts(accounts []types.Account) error { paramsNumber := 1 slices := dbutils.SplitAccounts(accounts, paramsNumber) @@ -52,6 +57,146 @@ func (db *Db) saveAccounts(paramsNumber int, accounts []types.Account) error { return nil } +// SaveVestingAccounts saves the given vesting account details inside the database, including: +// type, address, original vesting, endTime, startTime, and vesting periods +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: + ContinuousVestingAccount := types.NewContinuousVestingAccount(*vestingAccount) + err := db.storeContinuousVestingAccount(ContinuousVestingAccount) + if err != nil { + return fmt.Errorf("error while storing Continuous Vesting Account: %s", err) + } + + case *vestingtypes.DelayedVestingAccount: + DelayedVestingAccount := types.NewDelayedVestingAccount(*vestingAccount) + err := db.storeDelayedVestingAccount(DelayedVestingAccount) + if err != nil { + return fmt.Errorf("error while storing Delayed Vesting Account: %s", err) + } + + case *vestingtypes.PeriodicVestingAccount: + PeriodicVestingAccount := types.NewPeriodicVestingAccount(*vestingAccount) + err := db.storePeriodicVestingAccount(PeriodicVestingAccount) + if err != nil { + return fmt.Errorf("error while storing Periodic Vesting Account: %s", err) + } + } + } + + return nil +} + +//storeContinuousVestingAccount stores the vesting account details of type ContinuousVestingAccount into the database +func (db *Db) storeContinuousVestingAccount(account types.ContinuousVestingAccount) 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` + + _, err := db.Sql.Exec(stmt, + account.Type, + account.Address, + pq.Array(dbtypes.NewDbCoins(account.OriginalVesting)), + time.Unix(account.EndTime, 0).Format(time.RFC3339), + time.Unix(account.StartTime, 0).Format(time.RFC3339), + ) + if err != nil { + return err + } + return nil +} + +//storeDelayedVestingAccount stores the vesting account details of type DelayedVestingAccount into the database +func (db *Db) storeDelayedVestingAccount(account types.DelayedVestingAccount) error { + stmt := ` +INSERT INTO vesting_account (type, address, original_vesting, end_time) +VALUES ($1, $2, $3, $4) +ON CONFLICT (address) DO UPDATE + SET original_vesting = excluded.original_vesting, + end_time = excluded.end_time` + + _, err := db.Sql.Exec( + stmt, + account.Type, + account.Address, + pq.Array(dbtypes.NewDbCoins(account.OriginalVesting)), + time.Unix(account.EndTime, 0).Format(time.RFC3339), + ) + if err != nil { + return err + } + + return nil +} + +//storePeriodicVestingAccount stores the vesting account details of type PeriodicVestingAccount into the database +func (db *Db) storePeriodicVestingAccount(account types.PeriodicVestingAccount) 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 type = excluded.type, + original_vesting = excluded.original_vesting, + end_time = excluded.end_time, + start_time = excluded.start_time + RETURNING id ` + + var vestingAccountId int + err := db.Sql.QueryRow( + stmt, + account.Type, + account.Address, + pq.Array(dbtypes.NewDbCoins(account.OriginalVesting)), + time.Unix(account.EndTime, 0).Format(time.RFC3339), + time.Unix(account.StartTime, 0).Format(time.RFC3339), + ).Scan(&vestingAccountId) + + if err != nil { + return fmt.Errorf("error while saving Periodic Vesting Account: %s", err) + } + + err = db.storeVestingPeriods(vestingAccountId, account.VestingPeriods) + if err != nil { + return fmt.Errorf("error while storing vesting periods: %s", err) + } + + return nil +} + +//storePeriodicVestingAccount stores the vesting periods of type PeriodicVestingAccount into the database +func (db *Db) storeVestingPeriods(vestingAccountId 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, vestingAccountId, order, period.Length, amount) + } + stmt = stmt[:len(stmt)-1] + + _, err := db.Sql.Exec(stmt, params...) + if err != nil { + return err + } + + return nil +} + // GetAccounts returns all the accounts that are currently stored inside the database. func (db *Db) GetAccounts() ([]string, error) { var rows []string From 91bdf1788d2bc32ff7db341f90365610e41f268a Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Tue, 19 Oct 2021 11:07:38 +0800 Subject: [PATCH 17/35] add structs in types/auth.go: ContinuousVestingAccount/ DelayedVestingAccount/ PeriodicVestingAccount --- types/auth.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/types/auth.go b/types/auth.go index 6c06f7434..8b60549ff 100644 --- a/types/auth.go +++ b/types/auth.go @@ -1,5 +1,10 @@ package types +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" +) + // Account represents a chain account type Account struct { Address string @@ -11,3 +16,65 @@ func NewAccount(address string) Account { Address: address, } } + +// --------------- For Vesting Accounts --------------- + +// ContinuousVestingAccount represents a Continuous Vesting Account +type ContinuousVestingAccount struct { + Type string + Address string + OriginalVesting sdk.Coins + EndTime int64 + StartTime int64 +} + +// NewContinuousVestingAccount builds a new ContinuousVestingAccount instance +func NewContinuousVestingAccount(account vestingtypes.ContinuousVestingAccount) ContinuousVestingAccount { + return ContinuousVestingAccount{ + Type: "ContinuousVestingAccount", + Address: account.Address, + OriginalVesting: account.OriginalVesting, + EndTime: account.EndTime, + StartTime: account.StartTime, + } +} + +// ContinuousVestingAccount represents a Delayed Vesting Account +type DelayedVestingAccount struct { + Type string + Address string + OriginalVesting sdk.Coins + EndTime int64 +} + +// NewDelayedVestingAccount builds a new DelayedVestingAccount instance +func NewDelayedVestingAccount(account vestingtypes.DelayedVestingAccount) DelayedVestingAccount { + return DelayedVestingAccount{ + Type: "DelayedVestingAccount", + Address: account.Address, + OriginalVesting: account.OriginalVesting, + EndTime: account.EndTime, + } +} + +// PeriodicVestingAccount represents a Periodic Vesting Account +type PeriodicVestingAccount struct { + Type string + Address string + OriginalVesting sdk.Coins + EndTime int64 + StartTime int64 + VestingPeriods []vestingtypes.Period +} + +// NewPeriodicVestingAccount builds a new PeriodicVestingAccount instance +func NewPeriodicVestingAccount(account vestingtypes.PeriodicVestingAccount) PeriodicVestingAccount { + return PeriodicVestingAccount{ + Type: "PeriodicVestingAccount", + Address: account.Address, + OriginalVesting: account.OriginalVesting, + EndTime: account.EndTime, + StartTime: account.StartTime, + VestingPeriods: account.VestingPeriods, + } +} From 563f8ea2d1816812dfc437d271b5e6da4ba2e8fb Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Tue, 19 Oct 2021 11:14:02 +0800 Subject: [PATCH 18/35] modify sql comments --- database/schema/01-auth.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/database/schema/01-auth.sql b/database/schema/01-auth.sql index 3e1adb4bb..f448b23f1 100644 --- a/database/schema/01-auth.sql +++ b/database/schema/01-auth.sql @@ -3,13 +3,14 @@ CREATE TABLE account address TEXT NOT NULL PRIMARY KEY ); -/* ---- Moved from back.sql for vesting account usage ---- */ +/* ---- 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, From 31efda6f658af84104ef23731068ba99bd9f2383 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Tue, 19 Oct 2021 11:28:28 +0800 Subject: [PATCH 19/35] delete GetGenesisVestingAccounts from auth_accounts.go --- modules/auth/auth_accounts.go | 37 ----------------------------------- modules/auth/types.go | 31 ----------------------------- 2 files changed, 68 deletions(-) delete mode 100644 modules/auth/types.go diff --git a/modules/auth/auth_accounts.go b/modules/auth/auth_accounts.go index ca8b5bde2..767f34062 100644 --- a/modules/auth/auth_accounts.go +++ b/modules/auth/auth_accounts.go @@ -2,7 +2,6 @@ package auth import ( "encoding/json" - "strings" "github.com/cosmos/cosmos-sdk/codec" authttypes "github.com/cosmos/cosmos-sdk/x/auth/types" @@ -33,42 +32,6 @@ func GetGenesisAccounts(appState map[string]json.RawMessage, cdc codec.Marshaler return accounts, nil } -// GetGenesisVestingAccounts parses the given appState and returns the desired genesis vesting account details -func GetGenesisVestingAccounts(appState map[string]json.RawMessage) ([]types.VestingAccount, error) { - log.Debug().Str("module", "auth (vesting)").Msg("parsing genesis") - - var authState Accounts - if err := json.Unmarshal(appState[authttypes.ModuleName], &authState); err != nil { - return nil, err - } - - // Store the accounts - accounts := []types.VestingAccount{} - for _, account := range authState.Accounts { - accountType := strings.Split(account.AccountType, ".")[3] - - if accountType == "PeriodicVestingAccount" { - // Prepare vesting periods array for creating new Account instance - vestingPeriods := make([]types.VestingPeriod, len(account.VestingPeriods)) - for index, period := range account.VestingPeriods { - vestingPeriods[index] = types.NewVestingPeriod(period.Length, period.Amount) - } - - // Create new Account instance - vestingAccount := types.NewVestingAccount( - account.BaseVestingAccount.BaseAccount.Address, - account.BaseVestingAccount.OriginalVesting, - account.BaseVestingAccount.EndTime, - account.StartTime, - vestingPeriods, - ) - accounts = append(accounts, vestingAccount) - } - } - - return accounts, nil -} - // -------------------------------------------------------------------------------------------------------------------- // GetAccounts returns the account data for the given addresses diff --git a/modules/auth/types.go b/modules/auth/types.go deleted file mode 100644 index 0259d6974..000000000 --- a/modules/auth/types.go +++ /dev/null @@ -1,31 +0,0 @@ -package auth - -import sdk "github.com/cosmos/cosmos-sdk/types" - -// --- Types are defined according to genesis file structure --- - -type Accounts struct { - Accounts []AccountDetails `json:"accounts"` -} - -type AccountDetails struct { - AccountType string `json:"@type"` - BaseVestingAccount BaseVestingAccount `json:"base_vesting_account"` - StartTime string `json:"start_time"` - VestingPeriods []Period `json:"vesting_periods"` -} - -type BaseVestingAccount struct { - BaseAccount BaseAccount `json:"base_account"` - OriginalVesting []sdk.Coin `json:"original_vesting"` - EndTime string `json:"end_time"` -} - -type BaseAccount struct { - Address string `json:"address"` -} - -type Period struct { - Length string `json:"length"` - Amount []sdk.Coin `json:"amount"` -} From 918ba27abbf9b7063c1f757b2179aef221a9777c Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Tue, 19 Oct 2021 11:37:34 +0800 Subject: [PATCH 20/35] modif comments GetGenesisVestingAccounts --- modules/auth/auth_vesting_accounts.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/auth/auth_vesting_accounts.go b/modules/auth/auth_vesting_accounts.go index 9fa74c511..cd707ef55 100644 --- a/modules/auth/auth_vesting_accounts.go +++ b/modules/auth/auth_vesting_accounts.go @@ -8,7 +8,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/vesting/exported" ) -// GetGenesisAccounts parses the given appState and returns the genesis accounts +// 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 { From 6fcac22e9fe48a2fa0eb7968963cb7f061d8719f Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Tue, 19 Oct 2021 11:38:41 +0800 Subject: [PATCH 21/35] comment modif: Build vestingAccounts Array --- modules/auth/auth_vesting_accounts.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/auth/auth_vesting_accounts.go b/modules/auth/auth_vesting_accounts.go index cd707ef55..9476ebd9c 100644 --- a/modules/auth/auth_vesting_accounts.go +++ b/modules/auth/auth_vesting_accounts.go @@ -15,7 +15,7 @@ func GetGenesisVestingAccounts(appState map[string]json.RawMessage, cdc codec.Ma return nil, err } - // Create VestingAccount array + // Build vestingAccounts Array vestingAccounts := []exported.VestingAccount{} for _, account := range authState.Accounts { var accountI authttypes.AccountI From 1b318944f68065a0567729b54b299022494614ec Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Tue, 19 Oct 2021 11:41:18 +0800 Subject: [PATCH 22/35] linter issue: vestingAccountId -> vestingAccountID --- database/auth.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/database/auth.go b/database/auth.go index cb59ee427..60657c1da 100644 --- a/database/auth.go +++ b/database/auth.go @@ -150,7 +150,7 @@ ON CONFLICT (address) DO UPDATE start_time = excluded.start_time RETURNING id ` - var vestingAccountId int + var vestingAccountID int err := db.Sql.QueryRow( stmt, account.Type, @@ -158,13 +158,13 @@ ON CONFLICT (address) DO UPDATE pq.Array(dbtypes.NewDbCoins(account.OriginalVesting)), time.Unix(account.EndTime, 0).Format(time.RFC3339), time.Unix(account.StartTime, 0).Format(time.RFC3339), - ).Scan(&vestingAccountId) + ).Scan(&vestingAccountID) if err != nil { return fmt.Errorf("error while saving Periodic Vesting Account: %s", err) } - err = db.storeVestingPeriods(vestingAccountId, account.VestingPeriods) + err = db.storeVestingPeriods(vestingAccountID, account.VestingPeriods) if err != nil { return fmt.Errorf("error while storing vesting periods: %s", err) } @@ -173,7 +173,7 @@ ON CONFLICT (address) DO UPDATE } //storePeriodicVestingAccount stores the vesting periods of type PeriodicVestingAccount into the database -func (db *Db) storeVestingPeriods(vestingAccountId int, vestingPeriods []vestingtypes.Period) error { +func (db *Db) storeVestingPeriods(vestingAccountID int, vestingPeriods []vestingtypes.Period) error { stmt := ` INSERT INTO vesting_period (vesting_account_id, period_order, length, amount) VALUES ` @@ -185,7 +185,7 @@ VALUES ` order := i amount := pq.Array(dbtypes.NewDbCoins(period.Amount)) - params = append(params, vestingAccountId, order, period.Length, amount) + params = append(params, vestingAccountID, order, period.Length, amount) } stmt = stmt[:len(stmt)-1] From c0061950024aa5ec308a8f28248562f836efc7f1 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Tue, 19 Oct 2021 16:37:05 +0800 Subject: [PATCH 23/35] add unit test --- database/auth_test.go | 131 ++++++++++++++++++++++++++++++++++++ database/types/auth.go | 147 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 278 insertions(+) diff --git a/database/auth_test.go b/database/auth_test.go index c5363f5ce..74b5eac5b 100644 --- a/database/auth_test.go +++ b/database/auth_test.go @@ -1,8 +1,12 @@ package database_test import ( + "time" + sdk "github.com/cosmos/cosmos-sdk/types" authttypes "github.com/cosmos/cosmos-sdk/x/auth/types" + "github.com/cosmos/cosmos-sdk/x/auth/vesting/exported" + vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" "github.com/forbole/bdjuno/v2/types" @@ -71,3 +75,130 @@ func (suite *DbTestSuite) TestBigDipperDb_GetAccounts() { suite.Require().Equal(acc, accounts[index]) } } + +func (suite *DbTestSuite) Test_SaveVestingAccounts() { + // --- Save account addresses for foreign key contraint --- + queries := []string{ + `INSERT INTO account (address) VALUES ('cosmos1ltzt0z992ke6qgmtjxtygwzn36km4cy6cqdknt')`, + `INSERT INTO account (address) VALUES ('cosmos1re6zjpyczs0w7flrl6uacl0r4teqtyg62crjsn')`, + `INSERT INTO account (address) VALUES ('cosmos1eg47ue0l85lzkfgc4leske6hcah8cz3qajpjy2')`, + } + for _, query := range queries { + _, err := suite.database.Sql.Exec(query) + suite.Require().NoError(err) + } + var accountRows []dbtypes.AccountRow + suite.database.Sqlx.Select(&accountRows, `SELECT * FROM account`) + + // --- Prepare VestingAccounts for saving into DB --- + sdkCoins := sdk.NewCoins(sdk.NewCoin("desmos", sdk.NewInt(10))) + // ContinuousVestingAccount + address1, err := sdk.AccAddressFromBech32("cosmos1ltzt0z992ke6qgmtjxtygwzn36km4cy6cqdknt") + suite.Require().NoError(err) + ContinuousVestingAccount := vestingtypes.NewContinuousVestingAccount( + authttypes.NewBaseAccountWithAddress(address1), + sdkCoins, + time.Date(1990, 9, 9, 00, 00, 00, 000, time.UTC).Unix(), // Start Time + time.Date(2020, 9, 9, 00, 00, 00, 000, time.UTC).Unix(), // End Time + ) + + // DelayedVestingAccount + address2, _ := sdk.AccAddressFromBech32("cosmos1re6zjpyczs0w7flrl6uacl0r4teqtyg62crjsn") + DelayedVestingAccount := vestingtypes.NewDelayedVestingAccount( + authttypes.NewBaseAccountWithAddress(address2), + sdkCoins, + time.Date(2020, 9, 9, 00, 00, 00, 000, time.UTC).Unix(), // End Time + ) + + // PeriodicVestingAccount + address3, _ := sdk.AccAddressFromBech32("cosmos1eg47ue0l85lzkfgc4leske6hcah8cz3qajpjy2") + periods := []vestingtypes.Period{ + { + Length: 2629743, + Amount: sdkCoins, + }, + { + Length: 7889229, + Amount: sdkCoins, + }, + } + PeriodicVestingAccount := vestingtypes.NewPeriodicVestingAccount( + authttypes.NewBaseAccountWithAddress(address3), + sdkCoins, + time.Date(1990, 9, 9, 00, 00, 00, 000, time.UTC).Unix(), // Start Time + periods, + ) + + // VestingAccounts + vestingAccounts := []exported.VestingAccount{ + ContinuousVestingAccount, + DelayedVestingAccount, + PeriodicVestingAccount, + } + + // --- Save the data into DB --- + err = suite.database.SaveVestingAccounts(vestingAccounts) + suite.Require().NoError(err) + + // --- Verify Continuous Vesting Account --- + var continuousVestingAccountRow []dbtypes.ContinuousVestingAccountRow + err = suite.database.Sqlx.Select(&continuousVestingAccountRow, `SELECT * FROM vesting_account WHERE type = 'ContinuousVestingAccount'`) + suite.Require().NoError(err) + suite.Require().Len(continuousVestingAccountRow, 1, "ContinuousVestingAccount type should contain only one row") + + expectedContinuousVestingAccountRow := dbtypes.NewContinuousVestingAccountRow( + 1, + "ContinuousVestingAccount", + "cosmos1ltzt0z992ke6qgmtjxtygwzn36km4cy6cqdknt", + dbtypes.NewDbCoins(sdkCoins), + time.Date(2020, 9, 9, 00, 00, 00, 000, time.UTC), // EndTime + time.Date(1990, 9, 9, 00, 00, 00, 000, time.UTC), // StartTime + ) + suite.Require().True(expectedContinuousVestingAccountRow.Equal(continuousVestingAccountRow[0])) + + // --- Verify Delayed Vesting Account --- + var delayedVestingAccountRow []dbtypes.DelayedVestingAccountRow + err = suite.database.Sqlx.Select(&delayedVestingAccountRow, `SELECT id, type, address, original_vesting, end_time FROM vesting_account WHERE type = 'DelayedVestingAccount'`) + suite.Require().NoError(err) + suite.Require().Len(delayedVestingAccountRow, 1, "DelayedVestingAccountRow type should contain only one row") + + expectedDelayedVestingAccountRow := dbtypes.NewDelayedVestingAccountRow( + 2, + "DelayedVestingAccount", + "cosmos1re6zjpyczs0w7flrl6uacl0r4teqtyg62crjsn", + dbtypes.NewDbCoins(sdkCoins), + time.Date(2020, 9, 9, 00, 00, 00, 000, time.UTC), // EndTime + ) + suite.Require().True(expectedDelayedVestingAccountRow.Equal(delayedVestingAccountRow[0])) + + // --- Verify Periodic Vesting Account --- + var periodicVestingAccountRow []dbtypes.PeriodicVestingAccountRow + err = suite.database.Sqlx.Select(&periodicVestingAccountRow, `SELECT * FROM vesting_account WHERE type = 'PeriodicVestingAccount'`) + suite.Require().NoError(err) + suite.Require().Len(delayedVestingAccountRow, 1, "DelayedVestingAccountRow type should contain only one row") + + expectedPeriodicVestingAccountRow := dbtypes.NewPeriodicVestingAccountRow( + 3, + "PeriodicVestingAccount", + "cosmos1eg47ue0l85lzkfgc4leske6hcah8cz3qajpjy2", + dbtypes.NewDbCoins(sdkCoins), + time.Date(2020, 9, 9, 00, 00, 00, 000, time.UTC), // EndTime + time.Date(1990, 9, 9, 00, 00, 00, 000, time.UTC), // StartTime + ) + suite.Require().True(expectedPeriodicVestingAccountRow.Equal(periodicVestingAccountRow[0])) + + // --- Verify vesting periods --- + var vestingPeriodRows []dbtypes.VestingPeriodRow + err = suite.database.Sqlx.Select(&vestingPeriodRows, `SELECT * FROM vesting_period`) + suite.Require().NoError(err) + suite.Require().Len(vestingPeriodRows, 2, "vestingPeriodRows should contain only 2 rows") + + expectedVestingPeriods := []dbtypes.VestingPeriodRow{ + dbtypes.NewVestingPeriodRow(3, 0, "2629743", dbtypes.NewDbCoins(sdkCoins)), + dbtypes.NewVestingPeriodRow(3, 1, "7889229", dbtypes.NewDbCoins(sdkCoins)), + } + for index, vestingPeriod := range expectedVestingPeriods { + suite.Require().True(vestingPeriod.Equal(vestingPeriodRows[index])) + } + +} diff --git a/database/types/auth.go b/database/types/auth.go index 8c604c195..bd246eff8 100644 --- a/database/types/auth.go +++ b/database/types/auth.go @@ -1,5 +1,10 @@ package types +import ( + "fmt" + "time" +) + // AccountRow represents a single row inside the account table type AccountRow struct { Address string `db:"address"` @@ -16,3 +21,145 @@ func NewAccountRow(address string) AccountRow { func (a AccountRow) Equal(b AccountRow) bool { return a.Address == b.Address } + +// --------------- For Vesting Accounts --------------- + +// ContinuousVestingAccountRow represents a single row inside the vesting_account table +type ContinuousVestingAccountRow struct { + Id int `db:"id"` + Type string `db:"type"` + Address string `db:"address"` + OriginalVesting *DbCoins `db:"original_vesting"` + EndTime time.Time `db:"end_time"` + StartTime time.Time `db:"start_time"` +} + +// NewContinuousVestingAccountRow allows to build a new DB ContinuousVestingAccountRow +func NewContinuousVestingAccountRow( + id int, + accountType string, + address string, + originalVesting DbCoins, + endTime time.Time, + startTime time.Time, +) ContinuousVestingAccountRow { + return ContinuousVestingAccountRow{ + Id: id, + Type: accountType, + Address: address, + OriginalVesting: &originalVesting, + EndTime: endTime, + StartTime: startTime, + } +} + +// Equal tells whether a and b contain the same data +func (a ContinuousVestingAccountRow) Equal(b ContinuousVestingAccountRow) bool { + return a.Id == b.Id && + a.Type == b.Type && + a.Address == b.Address && + a.OriginalVesting.Equal(b.OriginalVesting) +} + +// DelayedVestingAccountRow represents a single row inside the vesting_account table +type DelayedVestingAccountRow struct { + Id int `db:"id"` + Type string `db:"type"` + Address string `db:"address"` + OriginalVesting *DbCoins `db:"original_vesting"` + EndTime time.Time `db:"end_time"` +} + +// NewDelayedVestingAccountRow allows to build a new DB DelayedVestingAccountRow +func NewDelayedVestingAccountRow( + id int, + accountType string, + address string, + originalVesting DbCoins, + endTime time.Time, +) DelayedVestingAccountRow { + return DelayedVestingAccountRow{ + Id: id, + Type: accountType, + Address: address, + OriginalVesting: &originalVesting, + EndTime: endTime, + } +} + +// Equal tells whether a and b contain the same data +func (a DelayedVestingAccountRow) Equal(b DelayedVestingAccountRow) bool { + return a.Id == b.Id && + a.Type == b.Type && + a.Address == b.Address && + a.OriginalVesting.Equal(b.OriginalVesting) +} + +// PeriodicVestingAccountRow represents a single row inside the vesting_account table +type PeriodicVestingAccountRow struct { + Id int `db:"id"` + Type string `db:"type"` + Address string `db:"address"` + OriginalVesting *DbCoins `db:"original_vesting"` + EndTime time.Time `db:"end_time"` + StartTime time.Time `db:"start_time"` +} + +// NewPeriodicVestingAccountRow allows to build a new DB PeriodicVestingAccountRow +func NewPeriodicVestingAccountRow( + id int, + accountType string, + address string, + originalVesting DbCoins, + endTime time.Time, + startTime time.Time, +) PeriodicVestingAccountRow { + return PeriodicVestingAccountRow{ + Id: id, + Type: accountType, + Address: address, + OriginalVesting: &originalVesting, + EndTime: endTime, + StartTime: startTime, + } +} + +// Equal tells whether a and b contain the same data +func (a PeriodicVestingAccountRow) Equal(b PeriodicVestingAccountRow) bool { + return a.Id == b.Id && + a.Type == b.Type && + a.Address == b.Address && + a.OriginalVesting.Equal(b.OriginalVesting) +} + +// VestingPeriodRow represents a Periodic Vesting Account +type VestingPeriodRow struct { + VestingAccountID int `db:"vesting_account_id"` + PeriodOrder int `db:"period_order"` + Length string `db:"length"` + Amount *DbCoins `db:"amount"` +} + +// NewPeriodicVestingAccountRow allows to build a new DB PeriodicVestingAccountRow +func NewVestingPeriodRow( + vestingAccountID int, + periodOrder int, + length string, + amount DbCoins, +) VestingPeriodRow { + return VestingPeriodRow{ + VestingAccountID: vestingAccountID, + PeriodOrder: periodOrder, + Length: length, + Amount: &amount, + } +} + +// Equal tells whether a and b contain the same data +func (a VestingPeriodRow) Equal(b VestingPeriodRow) bool { + fmt.Println(a, b) + return a.VestingAccountID == b.VestingAccountID && + a.PeriodOrder == b.PeriodOrder && + a.Length == b.Length && + a.Amount.Equal(b.Amount) +} From d6550913638b0185e64ce1031f957957ecadd7e2 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Tue, 19 Oct 2021 16:41:53 +0800 Subject: [PATCH 24/35] linter: Id -> ID --- database/types/auth.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/database/types/auth.go b/database/types/auth.go index bd246eff8..642d96ce3 100644 --- a/database/types/auth.go +++ b/database/types/auth.go @@ -26,7 +26,7 @@ func (a AccountRow) Equal(b AccountRow) bool { // ContinuousVestingAccountRow represents a single row inside the vesting_account table type ContinuousVestingAccountRow struct { - Id int `db:"id"` + ID int `db:"id"` Type string `db:"type"` Address string `db:"address"` OriginalVesting *DbCoins `db:"original_vesting"` @@ -44,7 +44,7 @@ func NewContinuousVestingAccountRow( startTime time.Time, ) ContinuousVestingAccountRow { return ContinuousVestingAccountRow{ - Id: id, + ID: id, Type: accountType, Address: address, OriginalVesting: &originalVesting, @@ -55,7 +55,7 @@ func NewContinuousVestingAccountRow( // Equal tells whether a and b contain the same data func (a ContinuousVestingAccountRow) Equal(b ContinuousVestingAccountRow) bool { - return a.Id == b.Id && + return a.ID == b.ID && a.Type == b.Type && a.Address == b.Address && a.OriginalVesting.Equal(b.OriginalVesting) @@ -63,7 +63,7 @@ func (a ContinuousVestingAccountRow) Equal(b ContinuousVestingAccountRow) bool { // DelayedVestingAccountRow represents a single row inside the vesting_account table type DelayedVestingAccountRow struct { - Id int `db:"id"` + ID int `db:"id"` Type string `db:"type"` Address string `db:"address"` OriginalVesting *DbCoins `db:"original_vesting"` @@ -79,7 +79,7 @@ func NewDelayedVestingAccountRow( endTime time.Time, ) DelayedVestingAccountRow { return DelayedVestingAccountRow{ - Id: id, + ID: id, Type: accountType, Address: address, OriginalVesting: &originalVesting, @@ -89,7 +89,7 @@ func NewDelayedVestingAccountRow( // Equal tells whether a and b contain the same data func (a DelayedVestingAccountRow) Equal(b DelayedVestingAccountRow) bool { - return a.Id == b.Id && + return a.ID == b.ID && a.Type == b.Type && a.Address == b.Address && a.OriginalVesting.Equal(b.OriginalVesting) @@ -97,7 +97,7 @@ func (a DelayedVestingAccountRow) Equal(b DelayedVestingAccountRow) bool { // PeriodicVestingAccountRow represents a single row inside the vesting_account table type PeriodicVestingAccountRow struct { - Id int `db:"id"` + ID int `db:"id"` Type string `db:"type"` Address string `db:"address"` OriginalVesting *DbCoins `db:"original_vesting"` @@ -115,7 +115,7 @@ func NewPeriodicVestingAccountRow( startTime time.Time, ) PeriodicVestingAccountRow { return PeriodicVestingAccountRow{ - Id: id, + ID: id, Type: accountType, Address: address, OriginalVesting: &originalVesting, @@ -126,7 +126,7 @@ func NewPeriodicVestingAccountRow( // Equal tells whether a and b contain the same data func (a PeriodicVestingAccountRow) Equal(b PeriodicVestingAccountRow) bool { - return a.Id == b.Id && + return a.ID == b.ID && a.Type == b.Type && a.Address == b.Address && a.OriginalVesting.Equal(b.OriginalVesting) From 71eb9f229c6fd5457509941db13f7772f1cfd7ef Mon Sep 17 00:00:00 2001 From: Magic Cat Date: Tue, 19 Oct 2021 16:26:07 +0100 Subject: [PATCH 25/35] lint --- database/auth.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/database/auth.go b/database/auth.go index 60657c1da..4849f114a 100644 --- a/database/auth.go +++ b/database/auth.go @@ -92,7 +92,7 @@ func (db *Db) SaveVestingAccounts(vestingAccounts []exported.VestingAccount) err return nil } -//storeContinuousVestingAccount stores the vesting account details of type ContinuousVestingAccount into the database +// storeContinuousVestingAccount stores the vesting account details of type ContinuousVestingAccount into the database func (db *Db) storeContinuousVestingAccount(account types.ContinuousVestingAccount) error { stmt := ` INSERT INTO vesting_account (type, address, original_vesting, end_time, start_time) @@ -115,7 +115,7 @@ ON CONFLICT (address) DO UPDATE return nil } -//storeDelayedVestingAccount stores the vesting account details of type DelayedVestingAccount into the database +// storeDelayedVestingAccount stores the vesting account details of type DelayedVestingAccount into the database func (db *Db) storeDelayedVestingAccount(account types.DelayedVestingAccount) error { stmt := ` INSERT INTO vesting_account (type, address, original_vesting, end_time) @@ -138,7 +138,7 @@ ON CONFLICT (address) DO UPDATE return nil } -//storePeriodicVestingAccount stores the vesting account details of type PeriodicVestingAccount into the database +// storePeriodicVestingAccount stores the vesting account details of type PeriodicVestingAccount into the database func (db *Db) storePeriodicVestingAccount(account types.PeriodicVestingAccount) error { stmt := ` INSERT INTO vesting_account (type, address, original_vesting, end_time, start_time) @@ -172,7 +172,7 @@ ON CONFLICT (address) DO UPDATE return nil } -//storePeriodicVestingAccount stores the vesting periods of type PeriodicVestingAccount into the database +// storePeriodicVestingAccount stores the vesting periods of type PeriodicVestingAccount into the database func (db *Db) storeVestingPeriods(vestingAccountID int, vestingPeriods []vestingtypes.Period) error { stmt := ` INSERT INTO vesting_period (vesting_account_id, period_order, length, amount) From 600230c63b8c9487a896595e92cd5414507adf00 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Wed, 20 Oct 2021 10:16:12 +0800 Subject: [PATCH 26/35] remove fmt.Println --- database/types/auth.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/database/types/auth.go b/database/types/auth.go index 642d96ce3..dd6c30e73 100644 --- a/database/types/auth.go +++ b/database/types/auth.go @@ -1,7 +1,6 @@ package types import ( - "fmt" "time" ) @@ -157,7 +156,6 @@ func NewVestingPeriodRow( // Equal tells whether a and b contain the same data func (a VestingPeriodRow) Equal(b VestingPeriodRow) bool { - fmt.Println(a, b) return a.VestingAccountID == b.VestingAccountID && a.PeriodOrder == b.PeriodOrder && a.Length == b.Length && From 39fc74d2a9960c184cf37590ce73aa6fb87b2622 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Wed, 20 Oct 2021 16:35:23 +0800 Subject: [PATCH 27/35] fix comments, remove redundant store-to-db methods --- database/auth.go | 115 +++++++++++++---------------------------------- go.mod | 1 + types/auth.go | 67 --------------------------- 3 files changed, 32 insertions(+), 151 deletions(-) diff --git a/database/auth.go b/database/auth.go index 4849f114a..9dcf95cda 100644 --- a/database/auth.go +++ b/database/auth.go @@ -8,12 +8,13 @@ import ( 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" ) -// SaveAccounts saves the given account addresses inside the database +// SaveAccounts saves the given accounts inside the database func (db *Db) SaveAccounts(accounts []types.Account) error { paramsNumber := 1 slices := dbutils.SplitAccounts(accounts, paramsNumber) @@ -57,32 +58,28 @@ func (db *Db) saveAccounts(paramsNumber int, accounts []types.Account) error { return nil } -// SaveVestingAccounts saves the given vesting account details inside the database, including: -// type, address, original vesting, endTime, startTime, and vesting periods +// 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) { + switch account.(type) { case *vestingtypes.ContinuousVestingAccount: - ContinuousVestingAccount := types.NewContinuousVestingAccount(*vestingAccount) - err := db.storeContinuousVestingAccount(ContinuousVestingAccount) + err := db.storeVestingAccount(account) if err != nil { return fmt.Errorf("error while storing Continuous Vesting Account: %s", err) } case *vestingtypes.DelayedVestingAccount: - DelayedVestingAccount := types.NewDelayedVestingAccount(*vestingAccount) - err := db.storeDelayedVestingAccount(DelayedVestingAccount) + err := db.storeVestingAccount(account) if err != nil { return fmt.Errorf("error while storing Delayed Vesting Account: %s", err) } case *vestingtypes.PeriodicVestingAccount: - PeriodicVestingAccount := types.NewPeriodicVestingAccount(*vestingAccount) - err := db.storePeriodicVestingAccount(PeriodicVestingAccount) + err := db.storeVestingAccount(account) if err != nil { return fmt.Errorf("error while storing Periodic Vesting Account: %s", err) } @@ -92,88 +89,38 @@ func (db *Db) SaveVestingAccounts(vestingAccounts []exported.VestingAccount) err return nil } -// storeContinuousVestingAccount stores the vesting account details of type ContinuousVestingAccount into the database -func (db *Db) storeContinuousVestingAccount(account types.ContinuousVestingAccount) error { +func (db *Db) storeVestingAccount(account exported.VestingAccount) 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` - - _, err := db.Sql.Exec(stmt, - account.Type, - account.Address, - pq.Array(dbtypes.NewDbCoins(account.OriginalVesting)), - time.Unix(account.EndTime, 0).Format(time.RFC3339), - time.Unix(account.StartTime, 0).Format(time.RFC3339), - ) - if err != nil { - return err - } - return nil -} - -// storeDelayedVestingAccount stores the vesting account details of type DelayedVestingAccount into the database -func (db *Db) storeDelayedVestingAccount(account types.DelayedVestingAccount) error { - stmt := ` -INSERT INTO vesting_account (type, address, original_vesting, end_time) -VALUES ($1, $2, $3, $4) -ON CONFLICT (address) DO UPDATE - SET original_vesting = excluded.original_vesting, - end_time = excluded.end_time` - - _, err := db.Sql.Exec( - stmt, - account.Type, - account.Address, - pq.Array(dbtypes.NewDbCoins(account.OriginalVesting)), - time.Unix(account.EndTime, 0).Format(time.RFC3339), - ) - if err != nil { - return err - } - - return nil -} - -// storePeriodicVestingAccount stores the vesting account details of type PeriodicVestingAccount into the database -func (db *Db) storePeriodicVestingAccount(account types.PeriodicVestingAccount) 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 type = excluded.type, - original_vesting = excluded.original_vesting, - end_time = excluded.end_time, - start_time = excluded.start_time - RETURNING id ` - - var vestingAccountID int - err := db.Sql.QueryRow( - stmt, - account.Type, - account.Address, - pq.Array(dbtypes.NewDbCoins(account.OriginalVesting)), - time.Unix(account.EndTime, 0).Format(time.RFC3339), - time.Unix(account.StartTime, 0).Format(time.RFC3339), - ).Scan(&vestingAccountID) + 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 fmt.Errorf("error while saving Periodic Vesting Account: %s", err) + return fmt.Errorf("error while saving Vesting Account: %s", err) } - err = db.storeVestingPeriods(vestingAccountID, account.VestingPeriods) - if err != nil { - return fmt.Errorf("error while storing vesting periods: %s", err) + if periodicVestingAccount, ok := account.(*vestingtypes.PeriodicVestingAccount); ok { + return db.storeVestingPeriods(vestingAccountRowID, periodicVestingAccount.VestingPeriods) } return nil } -// storePeriodicVestingAccount stores the vesting periods of type PeriodicVestingAccount into the database -func (db *Db) storeVestingPeriods(vestingAccountID int, vestingPeriods []vestingtypes.Period) error { +// 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 ` @@ -185,13 +132,13 @@ VALUES ` order := i amount := pq.Array(dbtypes.NewDbCoins(period.Amount)) - params = append(params, vestingAccountID, order, period.Length, amount) + params = append(params, id, order, period.Length, amount) } stmt = stmt[:len(stmt)-1] _, err := db.Sql.Exec(stmt, params...) if err != nil { - return err + return fmt.Errorf("error while saving vesting periods: %s", err) } return nil diff --git a/go.mod b/go.mod index 66743daae..40909109e 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/stretchr/testify v1.7.0 github.com/tendermint/tendermint v0.34.12 google.golang.org/grpc v1.37.0 + google.golang.org/protobuf v1.27.1 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) diff --git a/types/auth.go b/types/auth.go index 8b60549ff..6c06f7434 100644 --- a/types/auth.go +++ b/types/auth.go @@ -1,10 +1,5 @@ package types -import ( - sdk "github.com/cosmos/cosmos-sdk/types" - vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" -) - // Account represents a chain account type Account struct { Address string @@ -16,65 +11,3 @@ func NewAccount(address string) Account { Address: address, } } - -// --------------- For Vesting Accounts --------------- - -// ContinuousVestingAccount represents a Continuous Vesting Account -type ContinuousVestingAccount struct { - Type string - Address string - OriginalVesting sdk.Coins - EndTime int64 - StartTime int64 -} - -// NewContinuousVestingAccount builds a new ContinuousVestingAccount instance -func NewContinuousVestingAccount(account vestingtypes.ContinuousVestingAccount) ContinuousVestingAccount { - return ContinuousVestingAccount{ - Type: "ContinuousVestingAccount", - Address: account.Address, - OriginalVesting: account.OriginalVesting, - EndTime: account.EndTime, - StartTime: account.StartTime, - } -} - -// ContinuousVestingAccount represents a Delayed Vesting Account -type DelayedVestingAccount struct { - Type string - Address string - OriginalVesting sdk.Coins - EndTime int64 -} - -// NewDelayedVestingAccount builds a new DelayedVestingAccount instance -func NewDelayedVestingAccount(account vestingtypes.DelayedVestingAccount) DelayedVestingAccount { - return DelayedVestingAccount{ - Type: "DelayedVestingAccount", - Address: account.Address, - OriginalVesting: account.OriginalVesting, - EndTime: account.EndTime, - } -} - -// PeriodicVestingAccount represents a Periodic Vesting Account -type PeriodicVestingAccount struct { - Type string - Address string - OriginalVesting sdk.Coins - EndTime int64 - StartTime int64 - VestingPeriods []vestingtypes.Period -} - -// NewPeriodicVestingAccount builds a new PeriodicVestingAccount instance -func NewPeriodicVestingAccount(account vestingtypes.PeriodicVestingAccount) PeriodicVestingAccount { - return PeriodicVestingAccount{ - Type: "PeriodicVestingAccount", - Address: account.Address, - OriginalVesting: account.OriginalVesting, - EndTime: account.EndTime, - StartTime: account.StartTime, - VestingPeriods: account.VestingPeriods, - } -} From 9be34487f848cf8c8ea02de14a23132ab4701455 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Wed, 20 Oct 2021 16:38:06 +0800 Subject: [PATCH 28/35] rm unit test for save vesting account ftm --- database/auth_test.go | 131 ------------------------------------------ 1 file changed, 131 deletions(-) diff --git a/database/auth_test.go b/database/auth_test.go index 74b5eac5b..c5363f5ce 100644 --- a/database/auth_test.go +++ b/database/auth_test.go @@ -1,12 +1,8 @@ package database_test import ( - "time" - sdk "github.com/cosmos/cosmos-sdk/types" authttypes "github.com/cosmos/cosmos-sdk/x/auth/types" - "github.com/cosmos/cosmos-sdk/x/auth/vesting/exported" - vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" "github.com/forbole/bdjuno/v2/types" @@ -75,130 +71,3 @@ func (suite *DbTestSuite) TestBigDipperDb_GetAccounts() { suite.Require().Equal(acc, accounts[index]) } } - -func (suite *DbTestSuite) Test_SaveVestingAccounts() { - // --- Save account addresses for foreign key contraint --- - queries := []string{ - `INSERT INTO account (address) VALUES ('cosmos1ltzt0z992ke6qgmtjxtygwzn36km4cy6cqdknt')`, - `INSERT INTO account (address) VALUES ('cosmos1re6zjpyczs0w7flrl6uacl0r4teqtyg62crjsn')`, - `INSERT INTO account (address) VALUES ('cosmos1eg47ue0l85lzkfgc4leske6hcah8cz3qajpjy2')`, - } - for _, query := range queries { - _, err := suite.database.Sql.Exec(query) - suite.Require().NoError(err) - } - var accountRows []dbtypes.AccountRow - suite.database.Sqlx.Select(&accountRows, `SELECT * FROM account`) - - // --- Prepare VestingAccounts for saving into DB --- - sdkCoins := sdk.NewCoins(sdk.NewCoin("desmos", sdk.NewInt(10))) - // ContinuousVestingAccount - address1, err := sdk.AccAddressFromBech32("cosmos1ltzt0z992ke6qgmtjxtygwzn36km4cy6cqdknt") - suite.Require().NoError(err) - ContinuousVestingAccount := vestingtypes.NewContinuousVestingAccount( - authttypes.NewBaseAccountWithAddress(address1), - sdkCoins, - time.Date(1990, 9, 9, 00, 00, 00, 000, time.UTC).Unix(), // Start Time - time.Date(2020, 9, 9, 00, 00, 00, 000, time.UTC).Unix(), // End Time - ) - - // DelayedVestingAccount - address2, _ := sdk.AccAddressFromBech32("cosmos1re6zjpyczs0w7flrl6uacl0r4teqtyg62crjsn") - DelayedVestingAccount := vestingtypes.NewDelayedVestingAccount( - authttypes.NewBaseAccountWithAddress(address2), - sdkCoins, - time.Date(2020, 9, 9, 00, 00, 00, 000, time.UTC).Unix(), // End Time - ) - - // PeriodicVestingAccount - address3, _ := sdk.AccAddressFromBech32("cosmos1eg47ue0l85lzkfgc4leske6hcah8cz3qajpjy2") - periods := []vestingtypes.Period{ - { - Length: 2629743, - Amount: sdkCoins, - }, - { - Length: 7889229, - Amount: sdkCoins, - }, - } - PeriodicVestingAccount := vestingtypes.NewPeriodicVestingAccount( - authttypes.NewBaseAccountWithAddress(address3), - sdkCoins, - time.Date(1990, 9, 9, 00, 00, 00, 000, time.UTC).Unix(), // Start Time - periods, - ) - - // VestingAccounts - vestingAccounts := []exported.VestingAccount{ - ContinuousVestingAccount, - DelayedVestingAccount, - PeriodicVestingAccount, - } - - // --- Save the data into DB --- - err = suite.database.SaveVestingAccounts(vestingAccounts) - suite.Require().NoError(err) - - // --- Verify Continuous Vesting Account --- - var continuousVestingAccountRow []dbtypes.ContinuousVestingAccountRow - err = suite.database.Sqlx.Select(&continuousVestingAccountRow, `SELECT * FROM vesting_account WHERE type = 'ContinuousVestingAccount'`) - suite.Require().NoError(err) - suite.Require().Len(continuousVestingAccountRow, 1, "ContinuousVestingAccount type should contain only one row") - - expectedContinuousVestingAccountRow := dbtypes.NewContinuousVestingAccountRow( - 1, - "ContinuousVestingAccount", - "cosmos1ltzt0z992ke6qgmtjxtygwzn36km4cy6cqdknt", - dbtypes.NewDbCoins(sdkCoins), - time.Date(2020, 9, 9, 00, 00, 00, 000, time.UTC), // EndTime - time.Date(1990, 9, 9, 00, 00, 00, 000, time.UTC), // StartTime - ) - suite.Require().True(expectedContinuousVestingAccountRow.Equal(continuousVestingAccountRow[0])) - - // --- Verify Delayed Vesting Account --- - var delayedVestingAccountRow []dbtypes.DelayedVestingAccountRow - err = suite.database.Sqlx.Select(&delayedVestingAccountRow, `SELECT id, type, address, original_vesting, end_time FROM vesting_account WHERE type = 'DelayedVestingAccount'`) - suite.Require().NoError(err) - suite.Require().Len(delayedVestingAccountRow, 1, "DelayedVestingAccountRow type should contain only one row") - - expectedDelayedVestingAccountRow := dbtypes.NewDelayedVestingAccountRow( - 2, - "DelayedVestingAccount", - "cosmos1re6zjpyczs0w7flrl6uacl0r4teqtyg62crjsn", - dbtypes.NewDbCoins(sdkCoins), - time.Date(2020, 9, 9, 00, 00, 00, 000, time.UTC), // EndTime - ) - suite.Require().True(expectedDelayedVestingAccountRow.Equal(delayedVestingAccountRow[0])) - - // --- Verify Periodic Vesting Account --- - var periodicVestingAccountRow []dbtypes.PeriodicVestingAccountRow - err = suite.database.Sqlx.Select(&periodicVestingAccountRow, `SELECT * FROM vesting_account WHERE type = 'PeriodicVestingAccount'`) - suite.Require().NoError(err) - suite.Require().Len(delayedVestingAccountRow, 1, "DelayedVestingAccountRow type should contain only one row") - - expectedPeriodicVestingAccountRow := dbtypes.NewPeriodicVestingAccountRow( - 3, - "PeriodicVestingAccount", - "cosmos1eg47ue0l85lzkfgc4leske6hcah8cz3qajpjy2", - dbtypes.NewDbCoins(sdkCoins), - time.Date(2020, 9, 9, 00, 00, 00, 000, time.UTC), // EndTime - time.Date(1990, 9, 9, 00, 00, 00, 000, time.UTC), // StartTime - ) - suite.Require().True(expectedPeriodicVestingAccountRow.Equal(periodicVestingAccountRow[0])) - - // --- Verify vesting periods --- - var vestingPeriodRows []dbtypes.VestingPeriodRow - err = suite.database.Sqlx.Select(&vestingPeriodRows, `SELECT * FROM vesting_period`) - suite.Require().NoError(err) - suite.Require().Len(vestingPeriodRows, 2, "vestingPeriodRows should contain only 2 rows") - - expectedVestingPeriods := []dbtypes.VestingPeriodRow{ - dbtypes.NewVestingPeriodRow(3, 0, "2629743", dbtypes.NewDbCoins(sdkCoins)), - dbtypes.NewVestingPeriodRow(3, 1, "7889229", dbtypes.NewDbCoins(sdkCoins)), - } - for index, vestingPeriod := range expectedVestingPeriods { - suite.Require().True(vestingPeriod.Equal(vestingPeriodRows[index])) - } - -} From 234f23b995dc9b62dece799045247030039b6089 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Wed, 20 Oct 2021 16:40:55 +0800 Subject: [PATCH 29/35] schema: length TEXT -> BIGINT --- database/schema/01-auth.sql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/database/schema/01-auth.sql b/database/schema/01-auth.sql index f448b23f1..16f9689ba 100644 --- a/database/schema/01-auth.sql +++ b/database/schema/01-auth.sql @@ -27,8 +27,8 @@ CREATE UNIQUE INDEX vesting_account_address_idx ON vesting_account (address); CREATE TABLE vesting_period ( - vesting_account_id INT NOT NULL REFERENCES vesting_account (id), - period_order INT NOT NULL, - length TEXT NOT NULL, + vesting_account_id BIGINT NOT NULL REFERENCES vesting_account (id), + period_order BIGINT NOT NULL, + length BIGINT NOT NULL, amount COIN[] NOT NULL DEFAULT '{}' ); \ No newline at end of file From 4df7892ed3555b095ab39257cdf7ff193a88059d Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Wed, 20 Oct 2021 16:45:39 +0800 Subject: [PATCH 30/35] fix go.mod --- go.mod | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 40909109e..b6ca48c10 100644 --- a/go.mod +++ b/go.mod @@ -15,8 +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/protobuf v1.27.1 + google.golang.org/grpc v1.38.0 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) From ace57db3893df8b6c5f5cee2ea95aca5cb733247 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Wed, 20 Oct 2021 17:29:53 +0800 Subject: [PATCH 31/35] rm custom row types from database/types/auth.go --- database/types/auth.go | 145 ----------------------------------------- 1 file changed, 145 deletions(-) diff --git a/database/types/auth.go b/database/types/auth.go index dd6c30e73..8c604c195 100644 --- a/database/types/auth.go +++ b/database/types/auth.go @@ -1,9 +1,5 @@ package types -import ( - "time" -) - // AccountRow represents a single row inside the account table type AccountRow struct { Address string `db:"address"` @@ -20,144 +16,3 @@ func NewAccountRow(address string) AccountRow { func (a AccountRow) Equal(b AccountRow) bool { return a.Address == b.Address } - -// --------------- For Vesting Accounts --------------- - -// ContinuousVestingAccountRow represents a single row inside the vesting_account table -type ContinuousVestingAccountRow struct { - ID int `db:"id"` - Type string `db:"type"` - Address string `db:"address"` - OriginalVesting *DbCoins `db:"original_vesting"` - EndTime time.Time `db:"end_time"` - StartTime time.Time `db:"start_time"` -} - -// NewContinuousVestingAccountRow allows to build a new DB ContinuousVestingAccountRow -func NewContinuousVestingAccountRow( - id int, - accountType string, - address string, - originalVesting DbCoins, - endTime time.Time, - startTime time.Time, -) ContinuousVestingAccountRow { - return ContinuousVestingAccountRow{ - ID: id, - Type: accountType, - Address: address, - OriginalVesting: &originalVesting, - EndTime: endTime, - StartTime: startTime, - } -} - -// Equal tells whether a and b contain the same data -func (a ContinuousVestingAccountRow) Equal(b ContinuousVestingAccountRow) bool { - return a.ID == b.ID && - a.Type == b.Type && - a.Address == b.Address && - a.OriginalVesting.Equal(b.OriginalVesting) -} - -// DelayedVestingAccountRow represents a single row inside the vesting_account table -type DelayedVestingAccountRow struct { - ID int `db:"id"` - Type string `db:"type"` - Address string `db:"address"` - OriginalVesting *DbCoins `db:"original_vesting"` - EndTime time.Time `db:"end_time"` -} - -// NewDelayedVestingAccountRow allows to build a new DB DelayedVestingAccountRow -func NewDelayedVestingAccountRow( - id int, - accountType string, - address string, - originalVesting DbCoins, - endTime time.Time, -) DelayedVestingAccountRow { - return DelayedVestingAccountRow{ - ID: id, - Type: accountType, - Address: address, - OriginalVesting: &originalVesting, - EndTime: endTime, - } -} - -// Equal tells whether a and b contain the same data -func (a DelayedVestingAccountRow) Equal(b DelayedVestingAccountRow) bool { - return a.ID == b.ID && - a.Type == b.Type && - a.Address == b.Address && - a.OriginalVesting.Equal(b.OriginalVesting) -} - -// PeriodicVestingAccountRow represents a single row inside the vesting_account table -type PeriodicVestingAccountRow struct { - ID int `db:"id"` - Type string `db:"type"` - Address string `db:"address"` - OriginalVesting *DbCoins `db:"original_vesting"` - EndTime time.Time `db:"end_time"` - StartTime time.Time `db:"start_time"` -} - -// NewPeriodicVestingAccountRow allows to build a new DB PeriodicVestingAccountRow -func NewPeriodicVestingAccountRow( - id int, - accountType string, - address string, - originalVesting DbCoins, - endTime time.Time, - startTime time.Time, -) PeriodicVestingAccountRow { - return PeriodicVestingAccountRow{ - ID: id, - Type: accountType, - Address: address, - OriginalVesting: &originalVesting, - EndTime: endTime, - StartTime: startTime, - } -} - -// Equal tells whether a and b contain the same data -func (a PeriodicVestingAccountRow) Equal(b PeriodicVestingAccountRow) bool { - return a.ID == b.ID && - a.Type == b.Type && - a.Address == b.Address && - a.OriginalVesting.Equal(b.OriginalVesting) -} - -// VestingPeriodRow represents a Periodic Vesting Account -type VestingPeriodRow struct { - VestingAccountID int `db:"vesting_account_id"` - PeriodOrder int `db:"period_order"` - Length string `db:"length"` - Amount *DbCoins `db:"amount"` -} - -// NewPeriodicVestingAccountRow allows to build a new DB PeriodicVestingAccountRow -func NewVestingPeriodRow( - vestingAccountID int, - periodOrder int, - length string, - amount DbCoins, -) VestingPeriodRow { - return VestingPeriodRow{ - VestingAccountID: vestingAccountID, - PeriodOrder: periodOrder, - Length: length, - Amount: &amount, - } -} - -// Equal tells whether a and b contain the same data -func (a VestingPeriodRow) Equal(b VestingPeriodRow) bool { - return a.VestingAccountID == b.VestingAccountID && - a.PeriodOrder == b.PeriodOrder && - a.Length == b.Length && - a.Amount.Equal(b.Amount) -} From 2dcdeaf96b5ec119598106ab8e60418b0a578259 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Wed, 20 Oct 2021 17:32:40 +0800 Subject: [PATCH 32/35] merge 2 cases(continuous and deleyed vesting account; move storeVestingPeriods to SaveVestingAccounts --- database/auth.go | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/database/auth.go b/database/auth.go index 9dcf95cda..5596df35e 100644 --- a/database/auth.go +++ b/database/auth.go @@ -65,31 +65,27 @@ func (db *Db) SaveVestingAccounts(vestingAccounts []exported.VestingAccount) err } for _, account := range vestingAccounts { - switch account.(type) { + switch vestingAccount := account.(type) { case *vestingtypes.ContinuousVestingAccount: - err := db.storeVestingAccount(account) - if err != nil { - return fmt.Errorf("error while storing Continuous Vesting Account: %s", err) - } - case *vestingtypes.DelayedVestingAccount: - err := db.storeVestingAccount(account) + _, err := db.storeVestingAccount(account) if err != nil { - return fmt.Errorf("error while storing Delayed Vesting Account: %s", err) + return err } case *vestingtypes.PeriodicVestingAccount: - err := db.storeVestingAccount(account) + vestingAccountRowID, err := db.storeVestingAccount(account) if err != nil { - return fmt.Errorf("error while storing Periodic Vesting Account: %s", err) + return err } + db.storeVestingPeriods(vestingAccountRowID, vestingAccount.VestingPeriods) } } return nil } -func (db *Db) storeVestingAccount(account exported.VestingAccount) error { +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) @@ -109,14 +105,10 @@ func (db *Db) storeVestingAccount(account exported.VestingAccount) error { ).Scan(&vestingAccountRowID) if err != nil { - return fmt.Errorf("error while saving Vesting Account: %s", err) + return vestingAccountRowID, fmt.Errorf("error while saving Vesting Account of type %v: %s", proto.MessageName(account), err) } - if periodicVestingAccount, ok := account.(*vestingtypes.PeriodicVestingAccount); ok { - return db.storeVestingPeriods(vestingAccountRowID, periodicVestingAccount.VestingPeriods) - } - - return nil + return vestingAccountRowID, nil } // storeVestingPeriods handles storing the vesting periods of PeriodicVestingAccount type From decd364d5589a9be38be9bf5f6bea365423ec0eb Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Wed, 20 Oct 2021 17:43:25 +0800 Subject: [PATCH 33/35] correctly merge 2 cases --- database/auth.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/database/auth.go b/database/auth.go index 5596df35e..03beb8ce2 100644 --- a/database/auth.go +++ b/database/auth.go @@ -66,8 +66,7 @@ func (db *Db) SaveVestingAccounts(vestingAccounts []exported.VestingAccount) err for _, account := range vestingAccounts { switch vestingAccount := account.(type) { - case *vestingtypes.ContinuousVestingAccount: - case *vestingtypes.DelayedVestingAccount: + case *vestingtypes.ContinuousVestingAccount, *vestingtypes.DelayedVestingAccount: _, err := db.storeVestingAccount(account) if err != nil { return err From 4d7e1ec06eb0c0c6a9c24938390bab2aa696a319 Mon Sep 17 00:00:00 2001 From: huichiaotsou Date: Wed, 20 Oct 2021 17:56:09 +0800 Subject: [PATCH 34/35] revert DBG to prev. version --- database/auth.go | 2 +- modules/auth/handle_genesis.go | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/database/auth.go b/database/auth.go index 03beb8ce2..63237c22f 100644 --- a/database/auth.go +++ b/database/auth.go @@ -77,7 +77,7 @@ func (db *Db) SaveVestingAccounts(vestingAccounts []exported.VestingAccount) err if err != nil { return err } - db.storeVestingPeriods(vestingAccountRowID, vestingAccount.VestingPeriods) + return db.storeVestingPeriods(vestingAccountRowID, vestingAccount.VestingPeriods) } } diff --git a/modules/auth/handle_genesis.go b/modules/auth/handle_genesis.go index 436e5c1b1..a0fa1b072 100644 --- a/modules/auth/handle_genesis.go +++ b/modules/auth/handle_genesis.go @@ -11,8 +11,6 @@ import ( // HandleGenesis implements modules.GenesisModule func (m *Module) HandleGenesis(_ *tmtypes.GenesisDoc, appState map[string]json.RawMessage) error { - - // Handle account addresses log.Debug().Str("module", "auth").Msg("parsing genesis") accounts, err := GetGenesisAccounts(appState, m.cdc) if err != nil { @@ -23,8 +21,6 @@ func (m *Module) HandleGenesis(_ *tmtypes.GenesisDoc, appState map[string]json.R return fmt.Errorf("error while storing genesis accounts: %s", err) } - // Handle storing vesting accounts - log.Debug().Str("module", "auth/vesting").Msg("parsing genesis") vestingAccounts, err := GetGenesisVestingAccounts(appState, m.cdc) if err != nil { return fmt.Errorf("error while getting genesis vesting accounts: %s", err) From 83c661a4ba4e82fe173f0d3d69cdf9b1e311fa48 Mon Sep 17 00:00:00 2001 From: Magic Cat Date: Wed, 20 Oct 2021 15:15:38 +0100 Subject: [PATCH 35/35] Updated SaveVestingAccounts --- database/auth.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/database/auth.go b/database/auth.go index 63237c22f..a5bee32a9 100644 --- a/database/auth.go +++ b/database/auth.go @@ -77,7 +77,10 @@ func (db *Db) SaveVestingAccounts(vestingAccounts []exported.VestingAccount) err if err != nil { return err } - return db.storeVestingPeriods(vestingAccountRowID, vestingAccount.VestingPeriods) + err = db.storeVestingPeriods(vestingAccountRowID, vestingAccount.VestingPeriods) + if err != nil { + return err + } } }