github.com/lino-network/lino@v0.6.11/x/bandwidth/model/storage.go (about)

     1  package model
     2  
     3  import (
     4  	wire "github.com/cosmos/cosmos-sdk/codec"
     5  	sdk "github.com/cosmos/cosmos-sdk/types"
     6  
     7  	linotypes "github.com/lino-network/lino/types"
     8  )
     9  
    10  var (
    11  	bandwidthInfoSubstore = []byte{0x00}
    12  	blockInfoSubstore     = []byte{0x01}
    13  	appBandwidthSubstore  = []byte{0x02}
    14  )
    15  
    16  // BandwidthStorage - bandwidth storage
    17  type BandwidthStorage struct {
    18  	// The (unexposed) key used to access the store from the Context.
    19  	key sdk.StoreKey
    20  	cdc *wire.Codec
    21  }
    22  
    23  func NewBandwidthStorage(key sdk.StoreKey) BandwidthStorage {
    24  	cdc := wire.New()
    25  	wire.RegisterCrypto(cdc)
    26  
    27  	return BandwidthStorage{
    28  		key: key,
    29  		cdc: cdc,
    30  	}
    31  }
    32  
    33  // GetBandwidthInfo - returns bandwidth info, returns error otherwise.
    34  func (bs BandwidthStorage) GetBandwidthInfo(ctx sdk.Context) (*BandwidthInfo, sdk.Error) {
    35  	store := ctx.KVStore(bs.key)
    36  	infoByte := store.Get(GetBandwidthInfoKey())
    37  	if infoByte == nil {
    38  		return nil, ErrBandwidthInfoNotFound()
    39  	}
    40  	info := new(BandwidthInfo)
    41  	bs.cdc.MustUnmarshalBinaryLengthPrefixed(infoByte, info)
    42  	return info, nil
    43  }
    44  
    45  // SetBandwidthInfo - sets bandwidth info, returns error if any.
    46  func (bs BandwidthStorage) SetBandwidthInfo(ctx sdk.Context, info *BandwidthInfo) sdk.Error {
    47  	store := ctx.KVStore(bs.key)
    48  	infoByte := bs.cdc.MustMarshalBinaryLengthPrefixed(*info)
    49  	store.Set(GetBandwidthInfoKey(), infoByte)
    50  	return nil
    51  }
    52  
    53  // GetBlockInfo - returns cur block info, returns error otherwise.
    54  func (bs BandwidthStorage) GetBlockInfo(ctx sdk.Context) (*BlockInfo, sdk.Error) {
    55  	store := ctx.KVStore(bs.key)
    56  	infoByte := store.Get(GetBlockInfoKey())
    57  	if infoByte == nil {
    58  		return nil, ErrBlockInfoNotFound()
    59  	}
    60  	info := new(BlockInfo)
    61  	bs.cdc.MustUnmarshalBinaryLengthPrefixed(infoByte, info)
    62  	return info, nil
    63  }
    64  
    65  // SetBlockInfo - sets cur block info, returns error if any.
    66  func (bs BandwidthStorage) SetBlockInfo(ctx sdk.Context, info *BlockInfo) sdk.Error {
    67  	store := ctx.KVStore(bs.key)
    68  	infoByte := bs.cdc.MustMarshalBinaryLengthPrefixed(*info)
    69  	store.Set(GetBlockInfoKey(), infoByte)
    70  	return nil
    71  }
    72  
    73  func (bs BandwidthStorage) GetAppBandwidthInfo(ctx sdk.Context, accKey linotypes.AccountKey) (*AppBandwidthInfo, sdk.Error) {
    74  	store := ctx.KVStore(bs.key)
    75  	infoByte := store.Get(GetAppBandwidthInfoKey(accKey))
    76  	if infoByte == nil {
    77  		return nil, ErrAppBandwidthInfoNotFound()
    78  	}
    79  	info := new(AppBandwidthInfo)
    80  	bs.cdc.MustUnmarshalBinaryLengthPrefixed(infoByte, info)
    81  	return info, nil
    82  }
    83  
    84  func (bs BandwidthStorage) SetAppBandwidthInfo(ctx sdk.Context, accKey linotypes.AccountKey, info *AppBandwidthInfo) sdk.Error {
    85  	store := ctx.KVStore(bs.key)
    86  	infoByte := bs.cdc.MustMarshalBinaryLengthPrefixed(*info)
    87  	store.Set(GetAppBandwidthInfoKey(accKey), infoByte)
    88  	return nil
    89  }
    90  
    91  // GetAllAppBandwidthInfo
    92  func (bs BandwidthStorage) GetAllAppBandwidthInfo(ctx sdk.Context) ([]*AppBandwidthInfo, sdk.Error) {
    93  	allInfo := make([]*AppBandwidthInfo, 0)
    94  	store := ctx.KVStore(bs.key)
    95  	iter := sdk.KVStorePrefixIterator(store, appBandwidthSubstore)
    96  	defer iter.Close()
    97  	for ; iter.Valid(); iter.Next() {
    98  		val := iter.Value()
    99  		info := new(AppBandwidthInfo)
   100  		bs.cdc.MustUnmarshalBinaryLengthPrefixed(val, info)
   101  		allInfo = append(allInfo, info)
   102  	}
   103  	return allInfo, nil
   104  }
   105  
   106  func (bs BandwidthStorage) DoesAppBandwidthInfoExist(ctx sdk.Context, accKey linotypes.AccountKey) bool {
   107  	store := ctx.KVStore(bs.key)
   108  	return store.Has(GetAppBandwidthInfoKey(accKey))
   109  }
   110  
   111  func GetBandwidthInfoKey() []byte {
   112  	return bandwidthInfoSubstore
   113  }
   114  
   115  func GetBlockInfoKey() []byte {
   116  	return blockInfoSubstore
   117  }
   118  
   119  // GetAppBandwidthInfoKey - "app bandwidth substore" + "username"
   120  func GetAppBandwidthInfoKey(accKey linotypes.AccountKey) []byte {
   121  	return append(appBandwidthSubstore, accKey...)
   122  }