github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/genutil/gentx.go (about)

     1  package genutil
     2  
     3  // DONTCOVER
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  
     9  	abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types"
    10  
    11  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    12  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    13  	authexported "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/exported"
    14  	authtypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/types"
    15  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/genutil/types"
    16  	stakingtypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/staking/types"
    17  )
    18  
    19  // SetGenTxsInAppGenesisState - sets the genesis transactions in the app genesis state
    20  func SetGenTxsInAppGenesisState(cdc *codec.Codec, appGenesisState map[string]json.RawMessage,
    21  	genTxs []authtypes.StdTx) (map[string]json.RawMessage, error) {
    22  
    23  	genesisState := GetGenesisStateFromAppState(cdc, appGenesisState)
    24  	// convert all the GenTxs to JSON
    25  	genTxsBz := make([]json.RawMessage, 0, len(genTxs))
    26  	for _, genTx := range genTxs {
    27  		txBz, err := cdc.MarshalJSON(genTx)
    28  		if err != nil {
    29  			return appGenesisState, err
    30  		}
    31  		genTxsBz = append(genTxsBz, txBz)
    32  	}
    33  
    34  	genesisState.GenTxs = genTxsBz
    35  	return SetGenesisStateInAppState(cdc, appGenesisState, genesisState), nil
    36  }
    37  
    38  // ValidateAccountInGenesis checks that the provided key has sufficient
    39  // coins in the genesis accounts
    40  func ValidateAccountInGenesis(appGenesisState map[string]json.RawMessage,
    41  	genAccIterator types.GenesisAccountsIterator,
    42  	key sdk.Address, coins sdk.Coins, cdc *codec.Codec) error {
    43  
    44  	accountIsInGenesis := false
    45  
    46  	// TODO: refactor out bond denom to common state area
    47  	stakingDataBz := appGenesisState[stakingtypes.ModuleName]
    48  	var stakingData stakingtypes.GenesisState
    49  	cdc.MustUnmarshalJSON(stakingDataBz, &stakingData)
    50  	bondDenom := stakingData.Params.BondDenom
    51  
    52  	genUtilDataBz := appGenesisState[stakingtypes.ModuleName]
    53  	var genesisState GenesisState
    54  	cdc.MustUnmarshalJSON(genUtilDataBz, &genesisState)
    55  
    56  	var err error
    57  	genAccIterator.IterateGenesisAccounts(cdc, appGenesisState,
    58  		func(acc authexported.Account) (stop bool) {
    59  			accAddress := acc.GetAddress()
    60  			accCoins := acc.GetCoins()
    61  
    62  			// Ensure that account is in genesis
    63  			if accAddress.Equals(key) {
    64  
    65  				// Ensure account contains enough funds of default bond denom
    66  				if coins.AmountOf(bondDenom).GT(accCoins.AmountOf(bondDenom)) {
    67  					err = fmt.Errorf(
    68  						"account %v is in genesis, but it only has %v%v available to stake, not %v%v",
    69  						key.String(), accCoins.AmountOf(bondDenom), bondDenom, coins.AmountOf(bondDenom), bondDenom,
    70  					)
    71  					return true
    72  				}
    73  				accountIsInGenesis = true
    74  				return true
    75  			}
    76  			return false
    77  		},
    78  	)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	if !accountIsInGenesis {
    84  		return fmt.Errorf("account %s in not in the app_state.accounts array of genesis.json", key)
    85  	}
    86  
    87  	return nil
    88  }
    89  
    90  type deliverTxfn func(abci.RequestDeliverTx) abci.ResponseDeliverTx
    91  
    92  // DeliverGenTxs - deliver a genesis transaction
    93  func DeliverGenTxs(ctx sdk.Context, cdc *codec.Codec, genTxs []json.RawMessage,
    94  	stakingKeeper types.StakingKeeper, deliverTx deliverTxfn) []abci.ValidatorUpdate {
    95  
    96  	for _, genTx := range genTxs {
    97  		var tx authtypes.StdTx
    98  		cdc.MustUnmarshalJSON(genTx, &tx)
    99  		bz := cdc.MustMarshalBinaryLengthPrefixed(tx)
   100  		res := deliverTx(abci.RequestDeliverTx{Tx: bz})
   101  		if !res.IsOK() {
   102  			panic(res.Log)
   103  		}
   104  	}
   105  	return stakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
   106  }