github.com/Finschia/finschia-sdk@v0.48.1/x/genutil/gentx.go (about)

     1  package genutil
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	abci "github.com/tendermint/tendermint/abci/types"
     8  
     9  	"github.com/Finschia/finschia-sdk/client"
    10  	"github.com/Finschia/finschia-sdk/codec"
    11  	sdk "github.com/Finschia/finschia-sdk/types"
    12  	bankexported "github.com/Finschia/finschia-sdk/x/bank/exported"
    13  	"github.com/Finschia/finschia-sdk/x/genutil/types"
    14  	stakingtypes "github.com/Finschia/finschia-sdk/x/staking/types"
    15  )
    16  
    17  // SetGenTxsInAppGenesisState - sets the genesis transactions in the app genesis state
    18  func SetGenTxsInAppGenesisState(
    19  	cdc codec.JSONCodec, txJSONEncoder sdk.TxEncoder, appGenesisState map[string]json.RawMessage, genTxs []sdk.Tx,
    20  ) (map[string]json.RawMessage, error) {
    21  	genesisState := types.GetGenesisStateFromAppState(cdc, appGenesisState)
    22  	genTxsBz := make([]json.RawMessage, 0, len(genTxs))
    23  
    24  	for _, genTx := range genTxs {
    25  		txBz, err := txJSONEncoder(genTx)
    26  		if err != nil {
    27  			return appGenesisState, err
    28  		}
    29  
    30  		genTxsBz = append(genTxsBz, txBz)
    31  	}
    32  
    33  	genesisState.GenTxs = genTxsBz
    34  	return types.SetGenesisStateInAppState(cdc, appGenesisState, genesisState), nil
    35  }
    36  
    37  // ValidateAccountInGenesis checks that the provided account has a sufficient
    38  // balance in the set of genesis accounts.
    39  func ValidateAccountInGenesis(
    40  	appGenesisState map[string]json.RawMessage, genBalIterator types.GenesisBalancesIterator,
    41  	addr sdk.Address, coins sdk.Coins, cdc codec.JSONCodec,
    42  ) error {
    43  	var stakingData stakingtypes.GenesisState
    44  	cdc.MustUnmarshalJSON(appGenesisState[stakingtypes.ModuleName], &stakingData)
    45  	bondDenom := stakingData.Params.BondDenom
    46  
    47  	var err error
    48  
    49  	accountIsInGenesis := false
    50  
    51  	genBalIterator.IterateGenesisBalances(cdc, appGenesisState,
    52  		func(bal bankexported.GenesisBalance) (stop bool) {
    53  			accAddress := bal.GetAddress()
    54  			accCoins := bal.GetCoins()
    55  
    56  			// ensure that account is in genesis
    57  			if accAddress.Equals(addr) {
    58  				// ensure account contains enough funds of default bond denom
    59  				if coins.AmountOf(bondDenom).GT(accCoins.AmountOf(bondDenom)) {
    60  					err = fmt.Errorf(
    61  						"account %s has a balance in genesis, but it only has %v%s available to stake, not %v%s",
    62  						addr, accCoins.AmountOf(bondDenom), bondDenom, coins.AmountOf(bondDenom), bondDenom,
    63  					)
    64  
    65  					return true
    66  				}
    67  
    68  				accountIsInGenesis = true
    69  				return true
    70  			}
    71  
    72  			return false
    73  		},
    74  	)
    75  
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	if !accountIsInGenesis {
    81  		return fmt.Errorf("account %s does not have a balance in the genesis state", addr)
    82  	}
    83  
    84  	return nil
    85  }
    86  
    87  type deliverTxfn func(abci.RequestDeliverTx) abci.ResponseDeliverTx
    88  
    89  // DeliverGenTxs iterates over all genesis txs, decodes each into a Tx and
    90  // invokes the provided deliverTxfn with the decoded Tx. It returns the result
    91  // of the staking module's ApplyAndReturnValidatorSetUpdates.
    92  func DeliverGenTxs(
    93  	ctx sdk.Context, genTxs []json.RawMessage,
    94  	stakingKeeper types.StakingKeeper, deliverTx deliverTxfn,
    95  	txEncodingConfig client.TxEncodingConfig,
    96  ) ([]abci.ValidatorUpdate, error) {
    97  	for _, genTx := range genTxs {
    98  		tx, err := txEncodingConfig.TxJSONDecoder()(genTx)
    99  		if err != nil {
   100  			panic(err)
   101  		}
   102  
   103  		bz, err := txEncodingConfig.TxEncoder()(tx)
   104  		if err != nil {
   105  			panic(err)
   106  		}
   107  
   108  		res := deliverTx(abci.RequestDeliverTx{Tx: bz})
   109  		if !res.IsOK() {
   110  			panic(res.Log)
   111  		}
   112  	}
   113  
   114  	return stakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
   115  }