github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/app/export.go (about) 1 package app 2 3 import ( 4 "encoding/json" 5 "log" 6 7 abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types" 8 tmtypes "github.com/fibonacci-chain/fbc/libs/tendermint/types" 9 10 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec" 11 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/simapp" 12 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 13 "github.com/fibonacci-chain/fbc/x/slashing" 14 "github.com/fibonacci-chain/fbc/x/staking" 15 "github.com/fibonacci-chain/fbc/x/staking/exported" 16 17 ethcdc "github.com/fibonacci-chain/fbc/app/codec" 18 ) 19 20 // NewDefaultGenesisState generates the default state for the application. 21 func NewDefaultGenesisState() simapp.GenesisState { 22 _ = ethcdc.MakeCodec(ModuleBasics) 23 return ModuleBasics.DefaultGenesis() 24 } 25 26 // ExportAppStateAndValidators exports the state of the application for a genesis 27 // file. 28 func (app *FBChainApp) ExportAppStateAndValidators( 29 forZeroHeight bool, jailWhiteList []string, 30 ) (appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) { 31 32 // Creates context with current height and checks txs for ctx to be usable by start of next block 33 ctx := app.NewContext(true, abci.Header{Height: app.LastBlockHeight()}) 34 35 if forZeroHeight { 36 app.prepForZeroHeightGenesis(ctx, jailWhiteList) 37 } 38 39 // Export genesis to be used by SDK modules 40 genState := app.mm.ExportGenesis(ctx) 41 appState, err = codec.MarshalJSONIndent(app.marshal.GetCdc(), genState) 42 if err != nil { 43 return nil, nil, err 44 } 45 46 // Write validators to staking module to be used by TM node 47 //todo need resolve 48 // validators = staking.WriteValidators(ctx, app.StakingKeeper) 49 return appState, validators, nil 50 } 51 52 // prepare for fresh start at zero height 53 // NOTE zero height genesis is a temporary feature which will be deprecated 54 // 55 // in favour of export at a block height 56 func (app *FBChainApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []string) { 57 applyWhiteList := false 58 59 //Check if there is a whitelist 60 if len(jailWhiteList) > 0 { 61 applyWhiteList = true 62 } 63 64 whiteListMap := make(map[string]bool) 65 66 for _, addr := range jailWhiteList { 67 _, err := sdk.ValAddressFromBech32(addr) 68 if err != nil { 69 log.Fatal(err) 70 } 71 whiteListMap[addr] = true 72 } 73 74 /* Just to be safe, assert the invariants on current state. */ 75 app.CrisisKeeper.AssertInvariants(ctx) 76 77 /* Handle fee distribution state. */ 78 79 // withdraw all validator commission 80 app.StakingKeeper.IterateValidators(ctx, func(_ int64, val exported.ValidatorI) (stop bool) { 81 _, _ = app.DistrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator()) 82 return false 83 }) 84 //todo need resolved later 85 // withdraw all delegator rewards 86 //dels := app.StakingKeeper.GetAllDelegations(ctx) 87 //for _, delegation := range dels { 88 // _, _ = app.DistrKeeper.WithdrawDelegationRewards(ctx, delegation.DelegatorAddress, delegation.ValidatorAddress) 89 //} 90 91 // clear validator slash events 92 //app.DistrKeeper.DeleteAllValidatorSlashEvents(ctx) 93 94 // clear validator historical rewards 95 //app.DistrKeeper.DeleteAllValidatorHistoricalRewards(ctx) 96 97 // set context height to zero 98 //height := ctx.BlockHeight() 99 //ctx.SetBlockHeight(0) 100 // 101 //// reinitialize all validators 102 //app.StakingKeeper.IterateValidators(ctx, func(_ int64, val exported.ValidatorI) (stop bool) { 103 // // donate any unwithdrawn outstanding reward fraction tokens to the community pool 104 // scraps := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, val.GetOperator()) 105 // feePool := app.DistrKeeper.GetFeePool(ctx) 106 // feePool.CommunityPool = feePool.CommunityPool.Add(scraps...) 107 // app.DistrKeeper.SetFeePool(ctx, feePool) 108 // 109 // app.DistrKeeper.Hooks().AfterValidatorCreated(ctx, val.GetOperator()) 110 // return false 111 //}) 112 113 //todo need resolve 114 // reinitialize all delegations 115 //for _, del := range dels { 116 // app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, del.DelegatorAddress, del.ValidatorAddress) 117 // app.DistrKeeper.Hooks().AfterDelegationModified(ctx, del.DelegatorAddress, del.ValidatorAddress) 118 //} 119 120 // reset context height 121 //ctx.SetBlockHeight(height) 122 123 /* Handle staking state. */ 124 125 //todo need resolve 126 //// iterate through redelegations, reset creation height 127 //app.StakingKeeper.IterateRedelegations(ctx, func(_ int64, red staking.Redelegation) (stop bool) { 128 // for i := range red.Entries { 129 // red.Entries[i].CreationHeight = 0 130 // } 131 // app.StakingKeeper.SetRedelegation(ctx, red) 132 // return false 133 //}) 134 // 135 //// iterate through unbonding delegations, reset creation height 136 //app.StakingKeeper.IterateUnbondingDelegations(ctx, func(_ int64, ubd staking.UnbondingDelegation) (stop bool) { 137 // for i := range ubd.Entries { 138 // ubd.Entries[i].CreationHeight = 0 139 // } 140 // app.StakingKeeper.SetUnbondingDelegation(ctx, ubd) 141 // return false 142 //}) 143 144 // Iterate through validators by power descending, reset bond heights, and 145 // update bond intra-tx counters. 146 store := ctx.KVStore(app.keys[staking.StoreKey]) 147 iter := sdk.KVStoreReversePrefixIterator(store, staking.ValidatorsKey) 148 counter := int16(0) 149 150 for ; iter.Valid(); iter.Next() { 151 addr := sdk.ValAddress(iter.Key()[1:]) 152 validator, found := app.StakingKeeper.GetValidator(ctx, addr) 153 if !found { 154 panic("expected validator, not found") 155 } 156 157 validator.UnbondingHeight = 0 158 if applyWhiteList && !whiteListMap[addr.String()] { 159 validator.Jailed = true 160 } 161 162 app.StakingKeeper.SetValidator(ctx, validator) 163 counter++ 164 } 165 166 iter.Close() 167 168 _ = app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx) 169 170 /* Handle slashing state. */ 171 172 // reset start height on signing infos 173 app.SlashingKeeper.IterateValidatorSigningInfos( 174 ctx, 175 func(addr sdk.ConsAddress, info slashing.ValidatorSigningInfo) (stop bool) { 176 info.StartHeight = 0 177 app.SlashingKeeper.SetValidatorSigningInfo(ctx, addr, info) 178 return false 179 }, 180 ) 181 }