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

     1  package model
     2  
     3  import (
     4  	"strconv"
     5  
     6  	wire "github.com/cosmos/cosmos-sdk/codec"
     7  	sdk "github.com/cosmos/cosmos-sdk/types"
     8  
     9  	linotypes "github.com/lino-network/lino/types"
    10  	"github.com/lino-network/lino/utils"
    11  	"github.com/lino-network/lino/x/vote/types"
    12  )
    13  
    14  var (
    15  	VoterSubstore         = []byte{0x01} // SubStore for voter info.
    16  	LinoStakeStatSubStore = []byte{0x02} // SubStore for lino stake statistic
    17  )
    18  
    19  // VoteStorage - vote storage
    20  type VoteStorage struct {
    21  	key sdk.StoreKey
    22  	cdc *wire.Codec
    23  }
    24  
    25  // NewVoteStorage - new vote storage
    26  func NewVoteStorage(key sdk.StoreKey) VoteStorage {
    27  	cdc := wire.New()
    28  	wire.RegisterCrypto(cdc)
    29  	cdc.Seal()
    30  
    31  	vs := VoteStorage{
    32  		key: key,
    33  		cdc: cdc,
    34  	}
    35  
    36  	return vs
    37  }
    38  
    39  // DoesVoterExist - check if voter exist in KVStore or not
    40  func (vs VoteStorage) DoesVoterExist(ctx sdk.Context, accKey linotypes.AccountKey) bool {
    41  	store := ctx.KVStore(vs.key)
    42  	return store.Has(GetVoterKey(accKey))
    43  }
    44  
    45  // GetVoter - get voter from KVStore
    46  func (vs VoteStorage) GetVoter(ctx sdk.Context, accKey linotypes.AccountKey) (*Voter, sdk.Error) {
    47  	store := ctx.KVStore(vs.key)
    48  	voterByte := store.Get(GetVoterKey(accKey))
    49  	if voterByte == nil {
    50  		return nil, types.ErrVoterNotFound()
    51  	}
    52  	voter := new(Voter)
    53  	vs.cdc.MustUnmarshalBinaryLengthPrefixed(voterByte, voter)
    54  	return voter, nil
    55  }
    56  
    57  // SetVoter - set voter to KVStore
    58  func (vs VoteStorage) SetVoter(ctx sdk.Context, voter *Voter) {
    59  	store := ctx.KVStore(vs.key)
    60  	voterByte := vs.cdc.MustMarshalBinaryLengthPrefixed(*voter)
    61  	store.Set(GetVoterKey(voter.Username), voterByte)
    62  }
    63  
    64  // // DeleteVoter - delete voter from KVStore
    65  // // should never be deleted.
    66  // func (vs VoteStorage) DeleteVoter(ctx sdk.Context, username linotypes.AccountKey) sdk.Error {
    67  // 	store := ctx.KVStore(vs.key)
    68  // 	store.Delete(GetVoterKey(username))
    69  // 	return nil
    70  // }
    71  
    72  // SetLinoStakeStat - set lino power statistic at given day
    73  func (vs VoteStorage) SetLinoStakeStat(ctx sdk.Context, day int64, lps *LinoStakeStat) {
    74  	store := ctx.KVStore(vs.key)
    75  	lpsByte := vs.cdc.MustMarshalBinaryLengthPrefixed(*lps)
    76  	store.Set(GetLinoStakeStatKey(day), lpsByte)
    77  }
    78  
    79  // GetLinoStakeStat - get lino power statistic at given day
    80  func (vs VoteStorage) GetLinoStakeStat(ctx sdk.Context, day int64) (*LinoStakeStat, sdk.Error) {
    81  	store := ctx.KVStore(vs.key)
    82  	bz := store.Get(GetLinoStakeStatKey(day))
    83  	if bz == nil {
    84  		return nil, types.ErrStakeStatNotFound(day)
    85  	}
    86  	linoStakeStat := new(LinoStakeStat)
    87  	vs.cdc.MustUnmarshalBinaryLengthPrefixed(bz, linoStakeStat)
    88  	return linoStakeStat, nil
    89  }
    90  
    91  // StoreMap - map of all substores
    92  func (vs VoteStorage) StoreMap(ctx sdk.Context) utils.StoreMap {
    93  	store := ctx.KVStore(vs.key)
    94  	substores := []utils.SubStore{
    95  		{
    96  			Store:      store,
    97  			Prefix:     VoterSubstore,
    98  			ValCreator: func() interface{} { return new(Voter) },
    99  			Decoder:    vs.cdc.MustUnmarshalBinaryLengthPrefixed,
   100  		},
   101  		{
   102  			Store:      store,
   103  			Prefix:     LinoStakeStatSubStore,
   104  			ValCreator: func() interface{} { return new(LinoStakeStat) },
   105  			Decoder:    vs.cdc.MustUnmarshalBinaryLengthPrefixed,
   106  		},
   107  	}
   108  	return utils.NewStoreMap(substores)
   109  }
   110  
   111  // GetLinoStakeStatKey - get lino power statistic at day from KVStore
   112  func GetLinoStakeStatKey(day int64) []byte {
   113  	return append(LinoStakeStatSubStore, strconv.FormatInt(day, 10)...)
   114  }
   115  
   116  // GetVoterKey - "voter substore" + "voter"
   117  func GetVoterKey(me linotypes.AccountKey) []byte {
   118  	return append(VoterSubstore, me...)
   119  }