github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/testing/simapp/export.go (about)

     1  package simapp
     2  
     3  import (
     4  	"encoding/json"
     5  	"log"
     6  
     7  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
     8  	tmtypes "github.com/fibonacci-chain/fbc/libs/tendermint/types"
     9  
    10  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    11  	abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types"
    12  	"github.com/fibonacci-chain/fbc/x/slashing"
    13  	"github.com/fibonacci-chain/fbc/x/staking"
    14  	"github.com/fibonacci-chain/fbc/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  //
    48  //	in favour of export at a block height
    49  func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []string) {
    50  	applyWhiteList := false
    51  
    52  	//Check if there is a whitelist
    53  	if len(jailWhiteList) > 0 {
    54  		applyWhiteList = true
    55  	}
    56  
    57  	whiteListMap := make(map[string]bool)
    58  
    59  	for _, addr := range jailWhiteList {
    60  		_, err := sdk.ValAddressFromBech32(addr)
    61  		if err != nil {
    62  			log.Fatal(err)
    63  		}
    64  		whiteListMap[addr] = true
    65  	}
    66  
    67  	/* Just to be safe, assert the invariants on current state. */
    68  	app.CrisisKeeper.AssertInvariants(ctx)
    69  
    70  	/* Handle fee distribution state. */
    71  
    72  	// withdraw all validator commission
    73  	app.StakingKeeper.IterateValidators(ctx, func(_ int64, val exported.ValidatorI) (stop bool) {
    74  		_, _ = app.DistrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator())
    75  		return false
    76  	})
    77  
    78  	// Iterate through validators by power descending, reset bond heights, and
    79  	// update bond intra-tx counters.
    80  	store := ctx.KVStore(app.keys[staking.StoreKey])
    81  	iter := sdk.KVStoreReversePrefixIterator(store, staking.ValidatorsKey)
    82  	counter := int16(0)
    83  
    84  	for ; iter.Valid(); iter.Next() {
    85  		addr := sdk.ValAddress(iter.Key()[1:])
    86  		validator, found := app.StakingKeeper.GetValidator(ctx, addr)
    87  		if !found {
    88  			panic("expected validator, not found")
    89  		}
    90  
    91  		validator.UnbondingHeight = 0
    92  		if applyWhiteList && !whiteListMap[addr.String()] {
    93  			validator.Jailed = true
    94  		}
    95  
    96  		app.StakingKeeper.SetValidator(ctx, validator)
    97  		counter++
    98  	}
    99  
   100  	iter.Close()
   101  
   102  	_ = app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
   103  
   104  	/* Handle slashing state. */
   105  
   106  	// reset start height on signing infos
   107  	app.SlashingKeeper.IterateValidatorSigningInfos(
   108  		ctx,
   109  		func(addr sdk.ConsAddress, info slashing.ValidatorSigningInfo) (stop bool) {
   110  			info.StartHeight = 0
   111  			app.SlashingKeeper.SetValidatorSigningInfo(ctx, addr, info)
   112  			return false
   113  		},
   114  	)
   115  }