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

     1  package keeper
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
     7  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/store/mpt"
     8  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     9  	sdkerrors "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/errors"
    10  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/exported"
    11  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/types"
    12  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/params/subspace"
    13  	"github.com/fibonacci-chain/fbc/libs/tendermint/crypto"
    14  	"github.com/fibonacci-chain/fbc/libs/tendermint/libs/log"
    15  	tmtypes "github.com/fibonacci-chain/fbc/libs/tendermint/types"
    16  )
    17  
    18  // AccountKeeper encodes/decodes accounts using the go-amino (binary)
    19  // encoding/decoding library.
    20  type AccountKeeper struct {
    21  	// The (unexposed) key used to access the store from the Context.
    22  	key sdk.StoreKey
    23  
    24  	mptKey sdk.StoreKey
    25  
    26  	// The prototypical Account constructor.
    27  	proto func() exported.Account
    28  
    29  	// The codec codec for binary encoding/decoding of accounts.
    30  	cdc *codec.Codec
    31  
    32  	paramSubspace subspace.Subspace
    33  
    34  	observers []ObserverI
    35  }
    36  
    37  // NewAccountKeeper returns a new sdk.AccountKeeper that uses go-amino to
    38  // (binary) encode and decode concrete sdk.Accounts.
    39  // nolint
    40  func NewAccountKeeper(
    41  	cdc *codec.Codec, key, keyMpt sdk.StoreKey, paramstore subspace.Subspace, proto func() exported.Account,
    42  ) AccountKeeper {
    43  
    44  	return AccountKeeper{
    45  		key:           key,
    46  		mptKey:        keyMpt,
    47  		proto:         proto,
    48  		cdc:           cdc,
    49  		paramSubspace: paramstore.WithKeyTable(types.ParamKeyTable()),
    50  	}
    51  }
    52  
    53  // Logger returns a module-specific logger.
    54  func (ak AccountKeeper) Logger(ctx sdk.Context) log.Logger {
    55  	return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
    56  }
    57  
    58  // GetPubKey Returns the PubKey of the account at address
    59  func (ak AccountKeeper) GetPubKey(ctx sdk.Context, addr sdk.AccAddress) (crypto.PubKey, error) {
    60  	acc := ak.GetAccount(ctx, addr)
    61  	if acc == nil {
    62  		return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "account %s does not exist", addr)
    63  	}
    64  	return acc.GetPubKey(), nil
    65  }
    66  
    67  // GetSequence Returns the Sequence of the account at address
    68  func (ak AccountKeeper) GetSequence(ctx sdk.Context, addr sdk.AccAddress) (uint64, error) {
    69  	acc := ak.GetAccount(ctx, addr)
    70  	if acc == nil {
    71  		return 0, sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "account %s does not exist", addr)
    72  	}
    73  	return acc.GetSequence(), nil
    74  }
    75  
    76  // GetNextAccountNumber returns and increments the global account number counter.
    77  // If the global account number is not set, it initializes it with value 0.
    78  func (ak AccountKeeper) GetNextAccountNumber(ctx sdk.Context) uint64 {
    79  	var accNumber uint64
    80  	var store sdk.KVStore
    81  	if tmtypes.HigherThanMars(ctx.BlockHeight()) {
    82  		store = ctx.KVStore(ak.mptKey)
    83  	} else {
    84  		store = ctx.KVStore(ak.key)
    85  	}
    86  	bz := store.Get(types.GlobalAccountNumberKey)
    87  	if bz == nil {
    88  		// initialize the account numbers
    89  		accNumber = 0
    90  	} else {
    91  		err := ak.cdc.UnmarshalBinaryLengthPrefixed(bz, &accNumber)
    92  		if err != nil {
    93  			panic(err)
    94  		}
    95  	}
    96  
    97  	bz = ak.cdc.MustMarshalBinaryLengthPrefixed(accNumber + 1)
    98  	store.Set(types.GlobalAccountNumberKey, bz)
    99  	if !tmtypes.HigherThanMars(ctx.BlockHeight()) && mpt.TrieWriteAhead {
   100  		ctx.MultiStore().GetKVStore(ak.mptKey).Set(types.GlobalAccountNumberKey, bz)
   101  	}
   102  
   103  	return accNumber
   104  }
   105  
   106  // -----------------------------------------------------------------------------
   107  // Misc.
   108  
   109  func (ak AccountKeeper) decodeAccount(bz []byte) exported.Account {
   110  	val, err := ak.cdc.UnmarshalBinaryBareWithRegisteredUnmarshaller(bz, (*exported.Account)(nil))
   111  	if err == nil {
   112  		return val.(exported.Account)
   113  	}
   114  	var acc exported.Account
   115  	err = ak.cdc.UnmarshalBinaryBare(bz, &acc)
   116  	if err != nil {
   117  		panic(err)
   118  	}
   119  	return acc
   120  }