Merge PR #4398: Refactor genaccounts genesis state
This commit is contained in:
parent
65be4dbf09
commit
80767474d7
|
@ -84,7 +84,7 @@ func appStateFromGenesisFileFn(r *rand.Rand, accs []simulation.Account, genesisT
|
|||
cdc.MustUnmarshalJSON(bytes, &genesis)
|
||||
var appState GenesisState
|
||||
cdc.MustUnmarshalJSON(genesis.AppState, &appState)
|
||||
accounts := genaccounts.GetGenesisStateFromAppState(cdc, appState).Accounts
|
||||
accounts := genaccounts.GetGenesisStateFromAppState(cdc, appState)
|
||||
|
||||
var newAccs []simulation.Account
|
||||
for _, acc := range accounts {
|
||||
|
@ -165,8 +165,7 @@ func appStateRandomizedFn(r *rand.Rand, accs []simulation.Account, genesisTimest
|
|||
genesisAccounts = append(genesisAccounts, gacc)
|
||||
}
|
||||
|
||||
genaccsGenesis := genaccounts.NewGenesisState(genesisAccounts)
|
||||
genesisState[genaccounts.ModuleName] = cdc.MustMarshalJSON(genaccsGenesis)
|
||||
genesisState[genaccounts.ModuleName] = cdc.MustMarshalJSON(genesisAccounts)
|
||||
|
||||
authGenesis := auth.NewGenesisState(
|
||||
nil,
|
||||
|
|
|
@ -73,14 +73,17 @@ func AddGenesisAccountCmd(ctx *server.Context, cdc *codec.Codec,
|
|||
}
|
||||
|
||||
// add genesis account to the app state
|
||||
var genesisState genaccounts.GenesisState
|
||||
cdc.MustUnmarshalJSON(appState[genaccounts.ModuleName], &genesisState)
|
||||
if genesisState.Accounts.Contains(addr) {
|
||||
var genesisAccounts genaccounts.GenesisAccounts
|
||||
|
||||
cdc.MustUnmarshalJSON(appState[genaccounts.ModuleName], &genesisAccounts)
|
||||
|
||||
if genesisAccounts.Contains(addr) {
|
||||
return fmt.Errorf("cannot add account at existing address %v", addr)
|
||||
}
|
||||
genesisState.Accounts = append(genesisState.Accounts, genAcc)
|
||||
|
||||
genesisStateBz := cdc.MustMarshalJSON(genesisState)
|
||||
genesisAccounts = append(genesisAccounts, genAcc)
|
||||
|
||||
genesisStateBz := cdc.MustMarshalJSON(genaccounts.GenesisState(genesisAccounts))
|
||||
appState[genaccounts.ModuleName] = genesisStateBz
|
||||
|
||||
appStateJSON, err := cdc.MarshalJSON(appState)
|
||||
|
|
|
@ -21,5 +21,5 @@ func ExportGenesis(ctx sdk.Context, accountKeeper AccountKeeper) GenesisState {
|
|||
},
|
||||
)
|
||||
|
||||
return NewGenesisState(accounts)
|
||||
return accounts
|
||||
}
|
||||
|
|
|
@ -88,15 +88,13 @@ func NewGenesisAccountI(acc auth.Account) (GenesisAccount, error) {
|
|||
|
||||
// convert GenesisAccount to auth.Account
|
||||
func (ga *GenesisAccount) ToAccount() auth.Account {
|
||||
|
||||
bacc := auth.NewBaseAccount(ga.Address, ga.Coins.Sort(),
|
||||
nil, ga.AccountNumber, ga.Sequence)
|
||||
bacc := auth.NewBaseAccount(ga.Address, ga.Coins.Sort(), nil, ga.AccountNumber, ga.Sequence)
|
||||
|
||||
if !ga.OriginalVesting.IsZero() {
|
||||
|
||||
baseVestingAcc := auth.NewBaseVestingAccount(
|
||||
bacc, ga.OriginalVesting, ga.DelegatedFree,
|
||||
ga.DelegatedVesting, ga.EndTime)
|
||||
ga.DelegatedVesting, ga.EndTime,
|
||||
)
|
||||
|
||||
switch {
|
||||
case ga.StartTime != 0 && ga.EndTime != 0:
|
||||
|
|
|
@ -10,16 +10,7 @@ import (
|
|||
)
|
||||
|
||||
// State to Unmarshal
|
||||
type GenesisState struct {
|
||||
Accounts GenesisAccounts `json:"accounts"`
|
||||
}
|
||||
|
||||
// NewGenesisState creates a new GenesisState object
|
||||
func NewGenesisState(accounts GenesisAccounts) GenesisState {
|
||||
return GenesisState{
|
||||
Accounts: accounts,
|
||||
}
|
||||
}
|
||||
type GenesisState GenesisAccounts
|
||||
|
||||
// get the genesis state from the expected app state
|
||||
func GetGenesisStateFromAppState(cdc *codec.Codec, appState map[string]json.RawMessage) GenesisState {
|
||||
|
@ -27,6 +18,7 @@ func GetGenesisStateFromAppState(cdc *codec.Codec, appState map[string]json.RawM
|
|||
if appState[ModuleName] != nil {
|
||||
cdc.MustUnmarshalJSON(appState[ModuleName], &genesisState)
|
||||
}
|
||||
|
||||
return genesisState
|
||||
}
|
||||
|
||||
|
@ -41,11 +33,11 @@ func SetGenesisStateInAppState(cdc *codec.Codec,
|
|||
|
||||
// Sanitize sorts accounts and coin sets.
|
||||
func (gs GenesisState) Sanitize() {
|
||||
sort.Slice(gs.Accounts, func(i, j int) bool {
|
||||
return gs.Accounts[i].AccountNumber < gs.Accounts[j].AccountNumber
|
||||
sort.Slice(gs, func(i, j int) bool {
|
||||
return gs[i].AccountNumber < gs[j].AccountNumber
|
||||
})
|
||||
|
||||
for _, acc := range gs.Accounts {
|
||||
for _, acc := range gs {
|
||||
acc.Coins = acc.Coins.Sort()
|
||||
}
|
||||
}
|
||||
|
@ -54,8 +46,8 @@ func (gs GenesisState) Sanitize() {
|
|||
// ensures that there are no duplicate accounts in the genesis state and any
|
||||
// provided vesting accounts are valid.
|
||||
func ValidateGenesis(genesisState GenesisState) error {
|
||||
addrMap := make(map[string]bool, len(genesisState.Accounts))
|
||||
for _, acc := range genesisState.Accounts {
|
||||
addrMap := make(map[string]bool, len(genesisState))
|
||||
for _, acc := range genesisState {
|
||||
addrStr := acc.Address.String()
|
||||
|
||||
// disallow any duplicate accounts
|
||||
|
|
|
@ -28,17 +28,17 @@ func TestSanitize(t *testing.T) {
|
|||
})
|
||||
genAcc2 := NewGenesisAccount(&authAcc2)
|
||||
|
||||
genesisState := NewGenesisState([]GenesisAccount{genAcc1, genAcc2})
|
||||
genesisState := GenesisState([]GenesisAccount{genAcc1, genAcc2})
|
||||
require.NoError(t, ValidateGenesis(genesisState))
|
||||
require.True(t, genesisState.Accounts[0].AccountNumber > genesisState.Accounts[1].AccountNumber)
|
||||
require.Equal(t, genesisState.Accounts[0].Coins[0].Denom, "bcoin")
|
||||
require.Equal(t, genesisState.Accounts[0].Coins[1].Denom, "acoin")
|
||||
require.Equal(t, genesisState.Accounts[1].Address, addr2)
|
||||
require.True(t, genesisState[0].AccountNumber > genesisState[1].AccountNumber)
|
||||
require.Equal(t, genesisState[0].Coins[0].Denom, "bcoin")
|
||||
require.Equal(t, genesisState[0].Coins[1].Denom, "acoin")
|
||||
require.Equal(t, genesisState[1].Address, addr2)
|
||||
genesisState.Sanitize()
|
||||
require.False(t, genesisState.Accounts[0].AccountNumber > genesisState.Accounts[1].AccountNumber)
|
||||
require.Equal(t, genesisState.Accounts[1].Address, addr1)
|
||||
require.Equal(t, genesisState.Accounts[1].Coins[0].Denom, "acoin")
|
||||
require.Equal(t, genesisState.Accounts[1].Coins[1].Denom, "bcoin")
|
||||
require.False(t, genesisState[0].AccountNumber > genesisState[1].AccountNumber)
|
||||
require.Equal(t, genesisState[1].Address, addr1)
|
||||
require.Equal(t, genesisState[1].Coins[0].Denom, "acoin")
|
||||
require.Equal(t, genesisState[1].Coins[1].Denom, "bcoin")
|
||||
}
|
||||
|
||||
var (
|
||||
|
@ -57,7 +57,7 @@ func TestValidateGenesisDuplicateAccounts(t *testing.T) {
|
|||
genAccs[0] = NewGenesisAccount(&acc1)
|
||||
genAccs[1] = NewGenesisAccount(&acc1)
|
||||
|
||||
genesisState := NewGenesisState(genAccs)
|
||||
genesisState := GenesisState(genAccs)
|
||||
err := ValidateGenesis(genesisState)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
@ -73,13 +73,13 @@ func TestValidateGenesisInvalidAccounts(t *testing.T) {
|
|||
genAccs[0] = NewGenesisAccount(&acc1)
|
||||
genAccs[1] = NewGenesisAccount(&acc2)
|
||||
|
||||
genesisState := NewGenesisState(genAccs)
|
||||
genesisState.Accounts[0].OriginalVesting = genesisState.Accounts[0].Coins
|
||||
genesisState := GenesisState(genAccs)
|
||||
genesisState[0].OriginalVesting = genesisState[0].Coins
|
||||
err := ValidateGenesis(genesisState)
|
||||
require.Error(t, err)
|
||||
|
||||
genesisState.Accounts[0].StartTime = 1548888000
|
||||
genesisState.Accounts[0].EndTime = 1548775410
|
||||
genesisState[0].StartTime = 1548888000
|
||||
genesisState[0].EndTime = 1548775410
|
||||
err = ValidateGenesis(genesisState)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
|
|
@ -6,13 +6,11 @@ import (
|
|||
)
|
||||
|
||||
// initialize accounts and deliver genesis transactions
|
||||
func InitGenesis(ctx sdk.Context, cdc *codec.Codec,
|
||||
accountKeeper AccountKeeper, genesisState GenesisState) {
|
||||
|
||||
func InitGenesis(ctx sdk.Context, _ *codec.Codec, accountKeeper AccountKeeper, genesisState GenesisState) {
|
||||
genesisState.Sanitize()
|
||||
|
||||
// load the accounts
|
||||
for _, gacc := range genesisState.Accounts {
|
||||
for _, gacc := range genesisState {
|
||||
acc := gacc.ToAccount()
|
||||
acc = accountKeeper.NewAccount(ctx, acc) // set account number
|
||||
accountKeeper.SetAccount(ctx, acc)
|
||||
|
|
|
@ -50,7 +50,7 @@ func (AppModuleBasic) IterateGenesisAccounts(cdc *codec.Codec, appGenesis map[st
|
|||
iterateFn func(auth.Account) (stop bool)) {
|
||||
|
||||
genesisState := GetGenesisStateFromAppState(cdc, appGenesis)
|
||||
for _, genAcc := range genesisState.Accounts {
|
||||
for _, genAcc := range genesisState {
|
||||
acc := genAcc.ToAccount()
|
||||
if iterateFn(acc) {
|
||||
break
|
||||
|
|
Loading…
Reference in New Issue