github.com/lino-network/lino@v0.6.11/x/global/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  	"github.com/lino-network/lino/types"
    10  	"github.com/lino-network/lino/utils"
    11  )
    12  
    13  var (
    14  	TimeEventListSubStore = []byte{0x00} // SubStore for time event list
    15  	TimeSubStore          = []byte{0x01} // SubStore for time
    16  	EventErrorSubStore    = []byte{0x02} // SubStore failed events and errors
    17  	BCErrorSubStore       = []byte{0x03} // SubStore failed blockchain event and errors
    18  )
    19  
    20  // GlobalStorage - global storage
    21  type GlobalStorage struct {
    22  	// The (unexposed) key used to access the store from the Context.
    23  	key sdk.StoreKey
    24  	cdc *wire.Codec
    25  }
    26  
    27  // NewGlobalStorage - new global storage
    28  func NewGlobalStorage(key sdk.StoreKey, cdc *wire.Codec) GlobalStorage {
    29  	return GlobalStorage{
    30  		key: key,
    31  		cdc: cdc,
    32  	}
    33  }
    34  
    35  func (gs GlobalStorage) CanEncode(event types.Event) bool {
    36  	_, err := gs.cdc.MarshalBinaryLengthPrefixed(event)
    37  	return err == nil
    38  }
    39  
    40  // GetTimeEventList - get time event list at given unix time
    41  func (gs GlobalStorage) GetTimeEventList(ctx sdk.Context, unixTime int64) *types.TimeEventList {
    42  	store := ctx.KVStore(gs.key)
    43  	listByte := store.Get(GetTimeEventListKey(unixTime))
    44  	// event doesn't exist
    45  	if listByte == nil {
    46  		return &types.TimeEventList{
    47  			Events: nil,
    48  		}
    49  	}
    50  	lst := new(types.TimeEventList)
    51  	gs.cdc.MustUnmarshalBinaryLengthPrefixed(listByte, lst)
    52  	return lst
    53  }
    54  
    55  // SetTimeEventList - set time event list at given unix time
    56  func (gs GlobalStorage) SetTimeEventList(ctx sdk.Context, unixTime int64, lst *types.TimeEventList) {
    57  	store := ctx.KVStore(gs.key)
    58  	listByte := gs.cdc.MustMarshalBinaryLengthPrefixed(*lst)
    59  	store.Set(GetTimeEventListKey(unixTime), listByte)
    60  }
    61  
    62  // RemoveTimeEventList - remove time event list at given unix time
    63  func (gs GlobalStorage) RemoveTimeEventList(ctx sdk.Context, unixTime int64) {
    64  	store := ctx.KVStore(gs.key)
    65  	store.Delete(GetTimeEventListKey(unixTime))
    66  }
    67  
    68  // GetGlobalTime - get global time from KVStore
    69  func (gs GlobalStorage) GetGlobalTime(ctx sdk.Context) *GlobalTime {
    70  	store := ctx.KVStore(gs.key)
    71  	timeBytes := store.Get(GetTimeKey())
    72  	if timeBytes == nil {
    73  		panic("Global Time is not Initialized at genesis")
    74  	}
    75  	globalTime := new(GlobalTime)
    76  	gs.cdc.MustUnmarshalBinaryLengthPrefixed(timeBytes, globalTime)
    77  	return globalTime
    78  }
    79  
    80  // SetGlobalTime - set global time to KVStore
    81  func (gs GlobalStorage) SetGlobalTime(ctx sdk.Context, globalTime *GlobalTime) {
    82  	store := ctx.KVStore(gs.key)
    83  	timeBytes := gs.cdc.MustMarshalBinaryLengthPrefixed(*globalTime)
    84  	store.Set(GetTimeKey(), timeBytes)
    85  }
    86  
    87  // GetEventErrors - get global time from KVStore
    88  func (gs GlobalStorage) GetEventErrors(ctx sdk.Context) []EventError {
    89  	store := ctx.KVStore(gs.key)
    90  	bz := store.Get(GetEventErrorKey())
    91  	if bz == nil {
    92  		return nil
    93  	}
    94  	errors := make([]EventError, 0)
    95  	gs.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &errors)
    96  	return errors
    97  }
    98  
    99  // SetGlobalTime - set global time to KVStore
   100  func (gs GlobalStorage) SetEventErrors(ctx sdk.Context, errs []EventError) {
   101  	store := ctx.KVStore(gs.key)
   102  	bz := gs.cdc.MustMarshalBinaryLengthPrefixed(errs)
   103  	store.Set(GetEventErrorKey(), bz)
   104  }
   105  
   106  // GetEventErrors - get global time from KVStore
   107  func (gs GlobalStorage) GetBCErrors(ctx sdk.Context) []types.BCEventErr {
   108  	store := ctx.KVStore(gs.key)
   109  	bz := store.Get(GetBCErrorKey())
   110  	if bz == nil {
   111  		return nil
   112  	}
   113  	errors := make([]types.BCEventErr, 0)
   114  	gs.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &errors)
   115  	return errors
   116  }
   117  
   118  // SetGlobalTime - set global time to KVStore
   119  func (gs GlobalStorage) SetBCErrors(ctx sdk.Context, errs []types.BCEventErr) {
   120  	store := ctx.KVStore(gs.key)
   121  	bz := gs.cdc.MustMarshalBinaryLengthPrefixed(errs)
   122  	store.Set(GetBCErrorKey(), bz)
   123  }
   124  
   125  func (gs GlobalStorage) PartialStoreMap(ctx sdk.Context) utils.StoreMap {
   126  	store := ctx.KVStore(gs.key)
   127  	stores := []utils.SubStore{
   128  		{
   129  			Store:      store,
   130  			Prefix:     TimeEventListSubStore,
   131  			ValCreator: func() interface{} { return new(types.TimeEventList) },
   132  			Decoder:    gs.cdc.MustUnmarshalBinaryLengthPrefixed,
   133  		},
   134  	}
   135  	return utils.NewStoreMap(stores)
   136  }
   137  
   138  // GetTimeEventListKey - get time event list from KVStore
   139  func GetTimeEventListKey(unixTime int64) []byte {
   140  	return append(TimeEventListSubStore, strconv.FormatInt(unixTime, 10)...)
   141  }
   142  
   143  // GetTimeKey - "time substore"
   144  func GetTimeKey() []byte {
   145  	return TimeSubStore
   146  }
   147  
   148  // GetEventErrorKey - "event error substore"
   149  func GetEventErrorKey() []byte {
   150  	return EventErrorSubStore
   151  }
   152  
   153  // GetEventErrorKey - "bc event error substore"
   154  func GetBCErrorKey() []byte {
   155  	return BCErrorSubStore
   156  }