github.com/lino-network/lino@v0.6.11/x/validator/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 "github.com/lino-network/lino/utils" 9 "github.com/lino-network/lino/x/validator/types" 10 ) 11 12 var ( 13 ValidatorSubstore = []byte{0x00} 14 ValidatorListSubstore = []byte{0x01} 15 ElectionVoteListSubstore = []byte{0x02} 16 ) 17 18 type ValidatorStorage struct { 19 key sdk.StoreKey 20 cdc *wire.Codec 21 } 22 23 func NewValidatorStorage(key sdk.StoreKey) ValidatorStorage { 24 cdc := wire.New() 25 wire.RegisterCrypto(cdc) 26 vs := ValidatorStorage{ 27 key: key, 28 cdc: cdc, 29 } 30 return vs 31 } 32 33 // func (vs ValidatorStorage) DoesValidatorExist(ctx sdk.Context, accKey linotypes.AccountKey) bool { 34 // store := ctx.KVStore(vs.key) 35 // return store.Has(GetValidatorKey(accKey)) 36 // } 37 38 func (vs ValidatorStorage) GetValidator(ctx sdk.Context, accKey linotypes.AccountKey) (*Validator, sdk.Error) { 39 store := ctx.KVStore(vs.key) 40 validatorByte := store.Get(GetValidatorKey(accKey)) 41 if validatorByte == nil { 42 return nil, types.ErrValidatorNotFound(accKey) 43 } 44 validator := new(Validator) 45 vs.cdc.MustUnmarshalBinaryLengthPrefixed(validatorByte, validator) 46 return validator, nil 47 } 48 49 func (vs ValidatorStorage) SetValidator(ctx sdk.Context, accKey linotypes.AccountKey, validator *Validator) { 50 store := ctx.KVStore(vs.key) 51 validatorByte := vs.cdc.MustMarshalBinaryLengthPrefixed(*validator) 52 store.Set(GetValidatorKey(accKey), validatorByte) 53 } 54 55 func (vs ValidatorStorage) GetValidatorList(ctx sdk.Context) *ValidatorList { 56 store := ctx.KVStore(vs.key) 57 listByte := store.Get(GetValidatorListKey()) 58 if listByte == nil { 59 panic("Validator List should be initialized during genesis") 60 } 61 lst := new(ValidatorList) 62 vs.cdc.MustUnmarshalBinaryLengthPrefixed(listByte, lst) 63 return lst 64 } 65 66 func (vs ValidatorStorage) SetValidatorList(ctx sdk.Context, lst *ValidatorList) { 67 store := ctx.KVStore(vs.key) 68 listByte := vs.cdc.MustMarshalBinaryLengthPrefixed(*lst) 69 store.Set(GetValidatorListKey(), listByte) 70 } 71 72 func (vs ValidatorStorage) GetElectionVoteList(ctx sdk.Context, accKey linotypes.AccountKey) *ElectionVoteList { 73 store := ctx.KVStore(vs.key) 74 lstByte := store.Get(GetElectionVoteListKey(accKey)) 75 if lstByte == nil { 76 // valid empty value. 77 return &ElectionVoteList{} 78 } 79 lst := new(ElectionVoteList) 80 vs.cdc.MustUnmarshalBinaryLengthPrefixed(lstByte, lst) 81 return lst 82 } 83 84 func (vs ValidatorStorage) SetElectionVoteList(ctx sdk.Context, accKey linotypes.AccountKey, lst *ElectionVoteList) { 85 store := ctx.KVStore(vs.key) 86 lstByte := vs.cdc.MustMarshalBinaryLengthPrefixed(*lst) 87 store.Set(GetElectionVoteListKey(accKey), lstByte) 88 } 89 90 func (vs ValidatorStorage) StoreMap(ctx sdk.Context) utils.StoreMap { 91 store := ctx.KVStore(vs.key) 92 stores := []utils.SubStore{ 93 { 94 Store: store, 95 Prefix: ValidatorSubstore, 96 ValCreator: func() interface{} { return new(Validator) }, 97 Decoder: vs.cdc.MustUnmarshalBinaryLengthPrefixed, 98 }, 99 { 100 Store: store, 101 Prefix: ValidatorListSubstore, 102 ValCreator: func() interface{} { return new(ValidatorList) }, 103 Decoder: vs.cdc.MustUnmarshalBinaryLengthPrefixed, 104 }, 105 { 106 Store: store, 107 Prefix: ElectionVoteListSubstore, 108 ValCreator: func() interface{} { return new(ElectionVoteList) }, 109 Decoder: vs.cdc.MustUnmarshalBinaryLengthPrefixed, 110 }, 111 } 112 return utils.NewStoreMap(stores) 113 } 114 115 func GetValidatorKey(accKey linotypes.AccountKey) []byte { 116 return append(ValidatorSubstore, accKey...) 117 } 118 119 func GetElectionVoteListKey(accKey linotypes.AccountKey) []byte { 120 return append(ElectionVoteListSubstore, accKey...) 121 } 122 123 func GetValidatorListKey() []byte { 124 return ValidatorListSubstore 125 }