/
register.go
231 lines (187 loc) · 6.56 KB
/
register.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package keeper
import (
"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/unification-com/mainchain/x/beacon/types"
)
//__BEACON_ID___________________________________________________________
// GetHighestBeaconID gets the highest BEACON ID
func (k Keeper) GetHighestBeaconID(ctx sdk.Context) (beaconID uint64, err error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.HighestBeaconIDKey)
if bz == nil {
return 0, sdkerrors.Wrapf(types.ErrInvalidGenesis, "initial beacon ID hasn't been set")
}
// convert from bytes to uint64
beaconID = types.GetBeaconIDFromBytes(bz)
return beaconID, nil
}
// SetHighestBeaconID sets the new highest BEACON ID to the store
func (k Keeper) SetHighestBeaconID(ctx sdk.Context, beaconID uint64) {
store := ctx.KVStore(k.storeKey)
// convert from uint64 to bytes for storage
beaconIDbz := types.GetBeaconIDBytes(beaconID)
store.Set(types.HighestBeaconIDKey, beaconIDbz)
}
//__BEACONS_____________________________________________________________
// SetBeacon Sets the BEACON metadata struct for a beaconID
func (k Keeper) SetBeacon(ctx sdk.Context, beacon types.Beacon) error {
store := ctx.KVStore(k.storeKey)
store.Set(types.BeaconKey(beacon.BeaconId), k.cdc.MustMarshal(&beacon))
return nil
}
// GetBeacon Gets the entire BEACON metadata struct for a beaconID
func (k Keeper) GetBeacon(ctx sdk.Context, beaconID uint64) (types.Beacon, bool) {
store := ctx.KVStore(k.storeKey)
if !k.IsBeaconRegistered(ctx, beaconID) {
// return a new empty Beacon struct
return types.Beacon{}, false
}
bz := store.Get(types.BeaconKey(beaconID))
var beacon types.Beacon
k.cdc.MustUnmarshal(bz, &beacon)
return beacon, true
}
// GetBeaconOwner - get the current owner of a BEACON
func (k Keeper) GetBeaconOwner(ctx sdk.Context, beaconID uint64) sdk.AccAddress {
beacon, found := k.GetBeacon(ctx, beaconID)
if !found {
return sdk.AccAddress{}
}
accAddr, accErr := sdk.AccAddressFromBech32(beacon.Owner)
if accErr != nil {
return sdk.AccAddress{}
}
return accAddr
}
// IsBeaconRegistered Checks if the BEACON is present in the store or not
func (k Keeper) IsBeaconRegistered(ctx sdk.Context, beaconID uint64) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.BeaconKey(beaconID))
}
// GetBeaconsIterator Get an iterator over all BEACONs in which the keys are the BEACON Ids and the values are the BEACONs
func (k Keeper) GetBeaconsIterator(ctx sdk.Context) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIterator(store, types.RegisteredBeaconPrefix)
}
// IterateBeacons iterates over the all the BEACON metadata and performs a callback function
func (k Keeper) IterateBeacons(ctx sdk.Context, cb func(beacon types.Beacon) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.RegisteredBeaconPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var b types.Beacon
k.cdc.MustUnmarshal(iterator.Value(), &b)
if cb(b) {
break
}
}
}
// GetAllBeacons returns all the registered BEACON metadata from store
func (k Keeper) GetAllBeacons(ctx sdk.Context) (beacons []types.Beacon) {
k.IterateBeacons(ctx, func(wc types.Beacon) bool {
beacons = append(beacons, wc)
return false
})
return
}
// GetBeaconsFiltered retrieves BEACONs filtered by a given set of params which
// include pagination parameters along a moniker and owner address.
//
// NOTE: If no filters are provided, all proposals will be returned in paginated
// form.
func (k Keeper) GetBeaconsFiltered(ctx sdk.Context, params types.QueryBeaconsFilteredRequest) []types.Beacon {
beacons := k.GetAllBeacons(ctx)
filteredBeacons := make([]types.Beacon, 0, len(beacons))
for _, b := range beacons {
matchMoniker, matchOwner := true, true
if len(params.Moniker) > 0 {
matchMoniker = b.Moniker == params.Moniker
}
if len(params.Owner) > 0 {
matchOwner = b.Owner == params.Owner
}
if matchMoniker && matchOwner {
filteredBeacons = append(filteredBeacons, b)
}
}
// Todo - need to migrate this to proper pagination
start, end := client.Paginate(len(filteredBeacons), 1, 100, 100)
if start < 0 || end < 0 {
filteredBeacons = []types.Beacon{}
} else {
filteredBeacons = filteredBeacons[start:end]
}
return filteredBeacons
}
func (k Keeper) GetBeaconStorageLimit(ctx sdk.Context, beaconID uint64) (types.BeaconStorageLimit, bool) {
store := ctx.KVStore(k.storeKey)
if !k.HasBeaconStorageLimit(ctx, beaconID) {
return types.BeaconStorageLimit{
BeaconId: beaconID,
InStateLimit: types.DefaultStorageLimit,
}, false
}
storageKey := types.BeaconStorageLimitKey(beaconID)
bz := store.Get(storageKey)
var storage types.BeaconStorageLimit
k.cdc.MustUnmarshal(bz, &storage)
return storage, true
}
func (k Keeper) HasBeaconStorageLimit(ctx sdk.Context, beaconID uint64) bool {
store := ctx.KVStore(k.storeKey)
storageKey := types.BeaconStorageLimitKey(beaconID)
return store.Has(storageKey)
}
func (k Keeper) SetBeaconStorageLimit(ctx sdk.Context, beaconId, limit uint64) error {
store := ctx.KVStore(k.storeKey)
storageLimit := types.BeaconStorageLimit{
BeaconId: beaconId,
InStateLimit: limit,
}
store.Set(types.BeaconStorageLimitKey(beaconId), k.cdc.MustMarshal(&storageLimit))
return nil
}
func (k Keeper) IncreaseInStateStorage(ctx sdk.Context, beaconId, amount uint64) error {
beaconStorage, _ := k.GetBeaconStorageLimit(ctx, beaconId)
newInStateLimit := beaconStorage.InStateLimit + amount
err := k.SetBeaconStorageLimit(ctx, beaconId, newInStateLimit)
if err != nil {
return err
}
return nil
}
func (k Keeper) GetMaxPurchasableSlots(ctx sdk.Context, beaconId uint64) uint64 {
beaconStorage, found := k.GetBeaconStorageLimit(ctx, beaconId)
if !found {
return 0
}
maxStorageLimit := k.GetParamMaxStorageLimit(ctx)
if beaconStorage.InStateLimit >= maxStorageLimit {
return 0
}
return maxStorageLimit - beaconStorage.InStateLimit
}
// RegisterBeacon registers a BEACON in the store
func (k Keeper) RegisterNewBeacon(ctx sdk.Context, beacon types.Beacon) (uint64, error) {
beaconId, err := k.GetHighestBeaconID(ctx)
if err != nil {
return 0, err
}
beacon.BeaconId = beaconId
beacon.LastTimestampId = 0
beacon.FirstIdInState = 0
beacon.NumInState = 0
beacon.RegTime = uint64(ctx.BlockTime().Unix())
err = k.SetBeacon(ctx, beacon)
if err != nil {
return 0, err
}
err = k.SetBeaconStorageLimit(ctx, beaconId, k.GetParamDefaultStorageLimit(ctx))
if err != nil {
return 0, err
}
k.SetHighestBeaconID(ctx, beaconId+1)
return beaconId, nil
}