github.com/okex/exchain@v1.8.0/libs/ibc-go/testing/simapp/export.go (about) 1 package simapp 2 3 import ( 4 "encoding/json" 5 "log" 6 7 "github.com/okex/exchain/libs/cosmos-sdk/codec" 8 tmtypes "github.com/okex/exchain/libs/tendermint/types" 9 10 sdk "github.com/okex/exchain/libs/cosmos-sdk/types" 11 abci "github.com/okex/exchain/libs/tendermint/abci/types" 12 "github.com/okex/exchain/x/slashing" 13 "github.com/okex/exchain/x/staking" 14 "github.com/okex/exchain/x/staking/exported" 15 //slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" 16 //stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" 17 ) 18 19 // ExportAppStateAndValidators exports the state of the application for a genesis 20 // file. 21 func (app *SimApp) ExportAppStateAndValidators( 22 forZeroHeight bool, jailWhiteList []string, 23 ) (appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) { 24 25 // Creates context with current height and checks txs for ctx to be usable by start of next block 26 ctx := app.NewContext(true, abci.Header{Height: app.LastBlockHeight()}) 27 28 if forZeroHeight { 29 app.prepForZeroHeightGenesis(ctx, jailWhiteList) 30 } 31 32 // Export genesis to be used by SDK modules 33 genState := app.mm.ExportGenesis(ctx) 34 appState, err = codec.MarshalJSONIndent(app.marshal.GetCdc(), genState) 35 if err != nil { 36 return nil, nil, err 37 } 38 39 // Write validators to staking module to be used by TM node 40 //todo need resolve 41 // validators = staking.WriteValidators(ctx, app.StakingKeeper) 42 return appState, validators, nil 43 } 44 45 // prepare for fresh start at zero height 46 // NOTE zero height genesis is a temporary feature which will be deprecated 47 // in favour of export at a block height 48 func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []string) { 49 applyWhiteList := false 50 51 //Check if there is a whitelist 52 if len(jailWhiteList) > 0 { 53 applyWhiteList = true 54 } 55 56 whiteListMap := make(map[string]bool) 57 58 for _, addr := range jailWhiteList { 59 _, err := sdk.ValAddressFromBech32(addr) 60 if err != nil { 61 log.Fatal(err) 62 } 63 whiteListMap[addr] = true 64 } 65 66 /* Just to be safe, assert the invariants on current state. */ 67 app.CrisisKeeper.AssertInvariants(ctx) 68 69 /* Handle fee distribution state. */ 70 71 // withdraw all validator commission 72 app.StakingKeeper.IterateValidators(ctx, func(_ int64, val exported.ValidatorI) (stop bool) { 73 _, _ = app.DistrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator()) 74 return false 75 }) 76 77 // Iterate through validators by power descending, reset bond heights, and 78 // update bond intra-tx counters. 79 store := ctx.KVStore(app.keys[staking.StoreKey]) 80 iter := sdk.KVStoreReversePrefixIterator(store, staking.ValidatorsKey) 81 counter := int16(0) 82 83 for ; iter.Valid(); iter.Next() { 84 addr := sdk.ValAddress(iter.Key()[1:]) 85 validator, found := app.StakingKeeper.GetValidator(ctx, addr) 86 if !found { 87 panic("expected validator, not found") 88 } 89 90 validator.UnbondingHeight = 0 91 if applyWhiteList && !whiteListMap[addr.String()] { 92 validator.Jailed = true 93 } 94 95 app.StakingKeeper.SetValidator(ctx, validator) 96 counter++ 97 } 98 99 iter.Close() 100 101 _ = app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx) 102 103 /* Handle slashing state. */ 104 105 // reset start height on signing infos 106 app.SlashingKeeper.IterateValidatorSigningInfos( 107 ctx, 108 func(addr sdk.ConsAddress, info slashing.ValidatorSigningInfo) (stop bool) { 109 info.StartHeight = 0 110 app.SlashingKeeper.SetValidatorSigningInfo(ctx, addr, info) 111 return false 112 }, 113 ) 114 }