-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: genesis state tests for vesting accounts (#2004)
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
1 parent
4e6c8b5
commit b34c488
Showing
6 changed files
with
750 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.