Skip to content

Commit

Permalink
test: genesis state tests for vesting accounts (#2004)
Browse files Browse the repository at this point in the history
closes #1806 
<!--
Please read and fill out this form before submitting your PR.

Please make sure you have reviewed our contributors guide before
submitting your
first PR.
-->

Overview

This PR is proposing a test suit to test the vesting accounts where
introduced at genesis state. It tests the scenarios that we are intend
to use for our mainnet.

**Note:** THIS PR is a copy of the PR #1993 as having the refactors of
the main rebased into it to have easier review.

The suggested changes are applied to the tests.
Thank you @cmwaters and @evan-forbes and special thanks to @rootulp for
his detailed points and for his great proposal to make the tests more
fine grains and more special.

<!-- 
Please provide an explanation of the PR, including the appropriate
context,
background, goal, and rationale. If there is an issue with this
information,
please provide a tl;dr and link the issue. 
-->

## Checklist

<!-- 
Please complete the checklist to ensure that the PR is ready to be
reviewed.

IMPORTANT:
PRs should be left in Draft until the below checklist is completed.
-->

- [ ] New and updated code has appropriate documentation
- [ ] New and updated code has new and/or updated testing
- [ ] Required CI checks are passing
- [ ] Visual proof for any user facing features like CLI or
documentation updates
- [ ] Linked issues closed with keywords

---------

Co-authored-by: Rootul P <rootulp@gmail.com>
Co-authored-by: CHAMI Rachid <chamirachid1@gmail.com>
  • Loading branch information
3 people committed Aug 14, 2023
1 parent 4e6c8b5 commit b34c488
Show file tree
Hide file tree
Showing 6 changed files with 750 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ test-race:
# TODO: Remove the -skip flag once the following tests no longer contain data races.
# https://github.com/celestiaorg/celestia-app/issues/1369
@echo "--> Running tests in race mode"
@go test -mod=readonly ./... -v -race -skip "TestPrepareProposalConsistency|TestIntegrationTestSuite|TestQGBRPCQueries|TestSquareSizeIntegrationTest|TestStandardSDKIntegrationTestSuite|TestTxsimCommandFlags|TestTxsimCommandEnvVar|TestMintIntegrationTestSuite|TestQGBCLI|TestUpgrade|TestMaliciousTestNode"
@go test -mod=readonly ./... -v -race -skip "TestPrepareProposalConsistency|TestIntegrationTestSuite|TestQGBRPCQueries|TestSquareSizeIntegrationTest|TestStandardSDKIntegrationTestSuite|TestTxsimCommandFlags|TestTxsimCommandEnvVar|TestMintIntegrationTestSuite|TestQGBCLI|TestUpgrade|TestMaliciousTestNode|TestVestingModule"
.PHONY: test-race

## test-bench: Run unit tests in bench mode.
Expand Down Expand Up @@ -197,4 +197,4 @@ txsim-build-docker:
adr-gen:
@echo "--> Downloading ADR template"
@curl -sSL https://raw.githubusercontent.com/celestiaorg/.github/main/adr-template.md > docs/architecture/adr-template.md
.PHONY: adr-gen
.PHONY: adr-gen
156 changes: 156 additions & 0 deletions pkg/genesis/vesting_accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package genesis

import (
"encoding/json"
"time"

"github.com/celestiaorg/celestia-app/app/encoding"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)

func NewGenesisRegularAccount(
address string,
balances sdk.Coins,
) (account authtypes.GenesisAccount, balance banktypes.Balance, err error) {
sdkAddr, err := sdk.AccAddressFromBech32(address)
if err != nil {
return account, balance, err
}

balance = banktypes.Balance{
Address: address,
Coins: balances,
}
bAccount := authtypes.NewBaseAccountWithAddress(sdkAddr)

return authtypes.GenesisAccount(bAccount), balance, nil
}

// NewGenesisDelayedVestingAccount creates a new DelayedVestingAccount with the
// specified parameters. It returns the created account converted to genesis
// account type and the account balance.
func NewGenesisDelayedVestingAccount(
address string,
vestingBalance,
initUnlockedCoins sdk.Coins,
endTime time.Time,
) (account authtypes.GenesisAccount, balance banktypes.Balance, err error) {
sdkAddr, err := sdk.AccAddressFromBech32(address)
if err != nil {
return account, balance, err
}

balance = banktypes.Balance{
Address: address,
Coins: initUnlockedCoins.Add(vestingBalance...),
}

bAccount := authtypes.NewBaseAccountWithAddress(sdkAddr)
vAccount := vestingtypes.NewDelayedVestingAccount(bAccount, vestingBalance, endTime.Unix())

return authtypes.GenesisAccount(vAccount), balance, nil
}

func NewGenesisPeriodicVestingAccount(
address string,
vestingBalance,
initUnlockedCoins sdk.Coins,
startTime time.Time,
periods []vestingtypes.Period,
) (account authtypes.GenesisAccount, balance banktypes.Balance, err error) {
sdkAddr, err := sdk.AccAddressFromBech32(address)
if err != nil {
return account, balance, err
}

balance = banktypes.Balance{
Address: address,
Coins: initUnlockedCoins.Add(vestingBalance...),
}

bAccount := authtypes.NewBaseAccountWithAddress(sdkAddr)
vAccount := vestingtypes.NewPeriodicVestingAccount(bAccount, vestingBalance, startTime.Unix(), periods)

return authtypes.GenesisAccount(vAccount), balance, nil
}

func NewGenesisContinuousVestingAccount(
address string,
vestingBalance,
initUnlockedCoins sdk.Coins,
startTime, endTime time.Time,
) (account authtypes.GenesisAccount, balance banktypes.Balance, err error) {
sdkAddr, err := sdk.AccAddressFromBech32(address)
if err != nil {
return account, balance, err
}

balance = banktypes.Balance{
Address: address,
Coins: initUnlockedCoins.Add(vestingBalance...),
}

bAccount := authtypes.NewBaseAccountWithAddress(sdkAddr)
vAccount := vestingtypes.NewContinuousVestingAccount(bAccount, vestingBalance, startTime.Unix(), endTime.Unix())

return authtypes.GenesisAccount(vAccount), balance, nil
}

// AddAccountsToGenesisState adds the provided accounts to the genesis state (gs) map for the auth module.
// It takes the raw genesis state (gs) and a variadic number of GenesisAccount objects (accounts) as inputs.
// Then, it updates the given genesis state and returns it.
func AddAccountsToGenesisState(encCfg encoding.Config, gs map[string]json.RawMessage, accounts ...authtypes.GenesisAccount) (map[string]json.RawMessage, error) {
var authGenState authtypes.GenesisState
err := encCfg.Codec.UnmarshalJSON(gs[authtypes.ModuleName], &authGenState)
if err != nil {
return gs, err
}

pAccs, err := authtypes.PackAccounts(accounts)
if err != nil {
return gs, err
}

// set the accounts in the genesis state
authGenState.Accounts = append(authGenState.Accounts, pAccs...)
gs[authtypes.ModuleName] = encCfg.Codec.MustMarshalJSON(&authGenState)

return gs, nil
}

// AddBalancesToGenesisState updates the genesis state by adding balances to the bank module.
func AddBalancesToGenesisState(encCfg encoding.Config, gs map[string]json.RawMessage, balances []banktypes.Balance) (map[string]json.RawMessage, error) {
var bankGenState banktypes.GenesisState
err := encCfg.Codec.UnmarshalJSON(gs[banktypes.ModuleName], &bankGenState)
if err != nil {
return gs, err
}

bankGenState.Balances = append(bankGenState.Balances, balances...)
gs[banktypes.ModuleName] = encCfg.Codec.MustMarshalJSON(&bankGenState)

return gs, nil
}

// AddGenesisAccountsWithBalancesToGenesisState adds the given genesis accounts and balances to the
// provided genesis state. It returns the updated genesis state and an error if any occurred.
func AddGenesisAccountsWithBalancesToGenesisState(
encCfg encoding.Config,
gs map[string]json.RawMessage,
gAccounts []authtypes.GenesisAccount,
balances []banktypes.Balance,
) (map[string]json.RawMessage, error) {
gs, err := AddAccountsToGenesisState(encCfg, gs, gAccounts...)
if err != nil {
return gs, err
}

gs, err = AddBalancesToGenesisState(encCfg, gs, balances)
if err != nil {
return gs, err
}
return gs, nil
}
Loading

0 comments on commit b34c488

Please sign in to comment.