/
app.go
319 lines (304 loc) · 8.25 KB
/
app.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package pre_persistence
import (
"bytes"
"fmt"
"github.com/pokt-network/pocket/shared/types"
typesGenesis "github.com/pokt-network/pocket/shared/types/genesis"
"github.com/syndtr/goleveldb/leveldb/iterator"
"github.com/syndtr/goleveldb/leveldb/util"
"google.golang.org/protobuf/proto"
)
func (m *PrePersistenceContext) GetAppExists(address []byte) (exists bool, err error) {
db := m.Store()
key := append(AppPrefixKey, address...)
if found := db.Contains(key); !found {
return false, nil
}
bz, err := db.Get(key)
if err != nil {
return false, err
}
if bz == nil {
return false, nil
}
if bytes.Contains(bz, DeletedPrefixKey) {
return false, nil
}
return true, nil
}
func (m *PrePersistenceContext) GetApp(address []byte) (app *typesGenesis.App, err error) {
app = &typesGenesis.App{}
db := m.Store()
key := append(AppPrefixKey, address...)
bz, err := db.Get(key)
if err != nil {
return nil, err
}
if bz == nil {
return nil, nil
}
if bytes.Contains(bz, DeletedPrefixKey) {
return nil, nil
}
if err = proto.Unmarshal(bz, app); err != nil {
return nil, err
}
return app, nil
}
func (m *PrePersistenceContext) GetAllApps(height int64) (apps []*typesGenesis.App, err error) {
codec := types.GetCodec()
apps = make([]*typesGenesis.App, 0)
var it iterator.Iterator
if height == m.Height {
db := m.Store()
it = db.NewIterator(&util.Range{
Start: AppPrefixKey,
Limit: PrefixEndBytes(AppPrefixKey),
})
} else {
key := HeightKey(height, AppPrefixKey)
it = m.Parent.GetCommitDB().NewIterator(&util.Range{
Start: key,
Limit: PrefixEndBytes(key),
})
}
defer it.Release()
for valid := it.First(); valid; valid = it.Next() {
bz := it.Value()
if bytes.Contains(bz, DeletedPrefixKey) {
continue
}
a := typesGenesis.App{}
if err := codec.Unmarshal(bz, &a); err != nil {
return nil, err
}
apps = append(apps, &a)
}
return
}
func (m *PrePersistenceContext) InsertApplication(address []byte, publicKey []byte, output []byte, paused bool, status int, maxRelays string, stakedTokens string, chains []string, pausedHeight int64, unstakingHeight int64) error {
if exists, _ := m.GetAppExists(address); exists {
return fmt.Errorf("already exists in world state")
}
codec := types.GetCodec()
db := m.Store()
key := append(AppPrefixKey, address...)
app := typesGenesis.App{
Address: address,
PublicKey: publicKey,
Paused: paused,
Status: int32(status),
Chains: chains,
MaxRelays: maxRelays,
StakedTokens: stakedTokens,
PausedHeight: uint64(pausedHeight),
UnstakingHeight: unstakingHeight,
Output: output,
}
bz, err := codec.Marshal(&app)
if err != nil {
return err
}
return db.Put(key, bz)
}
func (m *PrePersistenceContext) UpdateApplication(address []byte, maxRelaysToAdd string, amountToAdd string, chainsToUpdate []string) error {
app, err := m.GetApp(address)
if err != nil {
return err
}
codec := types.GetCodec()
db := m.Store()
key := append(AppPrefixKey, address...)
// compute new values
stakedTokens, err := types.StringToBigInt(app.StakedTokens)
if err != nil {
return err
}
stakedTokensToAddI, err := types.StringToBigInt(amountToAdd)
if err != nil {
return err
}
stakedTokens.Add(stakedTokens, stakedTokensToAddI)
maxRelays, err := types.StringToBigInt(app.MaxRelays)
if err != nil {
return err
}
maxRelaysToAddI, err := types.StringToBigInt(maxRelaysToAdd)
if err != nil {
return err
}
maxRelays.Add(maxRelays, maxRelaysToAddI)
// update values
app.MaxRelays = types.BigIntToString(maxRelays)
app.StakedTokens = types.BigIntToString(stakedTokens)
app.Chains = chainsToUpdate
// marshal
bz, err := codec.Marshal(app)
if err != nil {
return err
}
return db.Put(key, bz)
}
func (m *PrePersistenceContext) DeleteApplication(address []byte) error {
exists, err := m.GetAppExists(address)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("does not exist in world state: %v", address)
}
db := m.Store()
key := append(AppPrefixKey, address...)
return db.Put(key, DeletedPrefixKey)
}
func (m *PrePersistenceContext) GetAppsReadyToUnstake(height int64, _ int) (apps []*types.UnstakingActor, err error) { // TODO delete unused parameter
db := m.Store()
unstakingKey := append(UnstakingAppPrefixKey, types.Int64ToBytes(height)...)
if has := db.Contains(unstakingKey); !has {
return nil, nil
}
val, err := db.Get(unstakingKey)
if err != nil {
return nil, err
}
if val == nil {
return make([]*types.UnstakingActor, 0), nil
}
unstakingApps := types.UnstakingActors{}
if err := proto.Unmarshal(val, &unstakingApps); err != nil {
return nil, err
}
for _, app := range unstakingApps.UnstakingActors {
apps = append(apps, app)
}
return
}
func (m *PrePersistenceContext) GetAppStatus(address []byte) (status int, err error) {
app, err := m.GetApp(address)
if err != nil {
return types.ZeroInt, err
}
if app == nil {
return types.ZeroInt, fmt.Errorf("does not exist in world state")
}
return int(app.Status), nil
}
func (m *PrePersistenceContext) SetAppUnstakingHeightAndStatus(address []byte, unstakingHeight int64, status int) error {
app, err := m.GetApp(address)
if err != nil {
return err
}
if app == nil {
return fmt.Errorf("does not exist in world state: %v", address)
}
codec := types.GetCodec()
unstakingApps := types.UnstakingActors{}
db := m.Store()
key := append(AppPrefixKey, address...)
app.UnstakingHeight = unstakingHeight
app.Status = int32(status)
bz, err := codec.Marshal(app)
if err != nil {
return err
}
if err := db.Put(key, bz); err != nil {
return err
}
unstakingKey := append(UnstakingAppPrefixKey, types.Int64ToBytes(unstakingHeight)...)
if found := db.Contains(unstakingKey); found {
val, err := db.Get(unstakingKey)
if err != nil {
return err
}
if err := proto.Unmarshal(val, &unstakingApps); err != nil {
return err
}
}
unstakingApps.UnstakingActors = append(unstakingApps.UnstakingActors, &types.UnstakingActor{
Address: app.Address,
StakeAmount: app.StakedTokens,
OutputAddress: app.Output,
})
unstakingBz, err := codec.Marshal(&unstakingApps)
if err != nil {
return err
}
return db.Put(unstakingKey, unstakingBz)
}
func (m *PrePersistenceContext) GetAppPauseHeightIfExists(address []byte) (int64, error) {
app, err := m.GetApp(address)
if err != nil {
return types.ZeroInt, err
}
if app == nil {
return types.ZeroInt, fmt.Errorf("does not exist in world state")
}
return int64(app.PausedHeight), nil
}
// SetAppsStatusAndUnstakingHeightPausedBefore : This unstakes the actors that have reached max pause height
func (m *PrePersistenceContext) SetAppsStatusAndUnstakingHeightPausedBefore(pausedBeforeHeight, unstakingHeight int64, status int) error {
db := m.Store()
codec := types.GetCodec()
it := db.NewIterator(&util.Range{
Start: AppPrefixKey,
Limit: PrefixEndBytes(AppPrefixKey),
})
defer it.Release()
for valid := it.First(); valid; valid = it.Next() {
app := typesGenesis.App{}
bz := it.Value()
if bytes.Contains(bz, DeletedPrefixKey) {
continue
}
if err := codec.Unmarshal(bz, &app); err != nil {
return err
}
if app.PausedHeight < uint64(pausedBeforeHeight) {
app.UnstakingHeight = unstakingHeight
app.Status = int32(status)
if err := m.SetAppUnstakingHeightAndStatus(app.Address, app.UnstakingHeight, status); err != nil {
return err
}
bz, err := codec.Marshal(&app)
if err != nil {
return err
}
if err := db.Put(it.Key(), bz); err != nil {
return err
}
}
}
return nil
}
func (m *PrePersistenceContext) SetAppPauseHeight(address []byte, height int64) error {
codec := types.GetCodec()
db := m.Store()
app, err := m.GetApp(address)
if err != nil {
return err
}
if app == nil {
return fmt.Errorf("does not exist in world state: %v", address)
}
if app.PausedHeight == types.HeightNotUsed {
app.Paused = true
} else {
app.Paused = false
}
app.PausedHeight = uint64(height)
bz, err := codec.Marshal(app)
if err != nil {
return err
}
return db.Put(append(AppPrefixKey, address...), bz)
}
func (m *PrePersistenceContext) GetAppOutputAddress(operator []byte) (output []byte, err error) {
app, err := m.GetApp(operator)
if err != nil {
return nil, err
}
if app == nil {
return nil, fmt.Errorf("does not exist in world state")
}
return app.Output, nil
}