-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
stdlib.go
232 lines (202 loc) · 4.74 KB
/
stdlib.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
package types
import (
"errors"
sdk "github.com/cosmos/cosmos-sdk/types"
wire "github.com/cosmos/cosmos-sdk/wire"
)
type ListMapper interface { // Solidity list like structure
Len(sdk.Context) int64
Get(sdk.Context, int64, interface{})
Set(sdk.Context, int64, interface{})
Push(sdk.Context, interface{})
Iterate(sdk.Context, interface{}, func(sdk.Context, int64))
}
type listMapper struct {
key sdk.StoreKey
cdc *wire.Codec
lk []byte
}
func NewListMapper(cdc *wire.Codec, key sdk.StoreKey) ListMapper {
lk, err := cdc.MarshalBinary(int64(-1))
if err != nil {
panic(err)
}
return listMapper{
key: key,
cdc: cdc,
lk: lk,
}
}
func (lm listMapper) Len(ctx sdk.Context) int64 {
store := ctx.KVStore(lm.key)
bz := store.Get(lm.lk)
if bz == nil {
zero, err := lm.cdc.MarshalBinary(0)
if err != nil {
panic(err)
}
store.Set(lm.lk, zero)
return 0
}
var res int64
if err := lm.cdc.UnmarshalBinary(bz, &res); err != nil {
panic(err)
}
return res
}
func (lm listMapper) Get(ctx sdk.Context, index int64, ptr interface{}) {
if index < 0 {
panic(errors.New(""))
}
store := ctx.KVStore(lm.key)
bz := store.Get(marshalInt64(lm.cdc, index))
if err := lm.cdc.UnmarshalBinary(bz, ptr); err != nil {
panic(err)
}
}
func (lm listMapper) Set(ctx sdk.Context, index int64, value interface{}) {
if index < 0 {
panic(errors.New(""))
}
store := ctx.KVStore(lm.key)
bz, err := lm.cdc.MarshalBinary(value)
if err != nil {
panic(err)
}
store.Set(marshalInt64(lm.cdc, index), bz)
}
func (lm listMapper) Push(ctx sdk.Context, value interface{}) {
length := lm.Len(ctx)
lm.Set(ctx, length, value)
store := ctx.KVStore(lm.key)
store.Set(lm.lk, marshalInt64(lm.cdc, length+1))
}
func (lm listMapper) Iterate(ctx sdk.Context, ptr interface{}, fn func(sdk.Context, int64)) {
length := lm.Len(ctx)
for i := int64(0); i < length; i++ {
lm.Get(ctx, i, ptr)
fn(ctx, i)
}
}
type QueueMapper interface {
Push(sdk.Context, interface{})
Peek(sdk.Context, interface{})
Pop(sdk.Context)
IsEmpty(sdk.Context) bool
Iterate(sdk.Context, interface{}, func(sdk.Context) bool)
}
type queueMapper struct {
key sdk.StoreKey
cdc *wire.Codec
ik []byte
}
func NewQueueMapper(cdc *wire.Codec, key sdk.StoreKey) QueueMapper {
ik, err := cdc.MarshalBinary(int64(-1))
if err != nil {
panic(err)
}
return queueMapper{
key: key,
cdc: cdc,
ik: ik,
}
}
type queueInfo struct {
// begin <= elems < end
Begin int64
End int64
}
func (info queueInfo) validateBasic() error {
if info.End < info.Begin || info.Begin < 0 || info.End < 0 {
return errors.New("")
}
return nil
}
func (info queueInfo) isEmpty() bool {
return info.Begin == info.End
}
func (qm queueMapper) getQueueInfo(store sdk.KVStore) queueInfo {
bz := store.Get(qm.ik)
if bz == nil {
store.Set(qm.ik, marshalQueueInfo(qm.cdc, queueInfo{0, 0}))
return queueInfo{0, 0}
}
var info queueInfo
if err := qm.cdc.UnmarshalBinary(bz, &info); err != nil {
panic(err)
}
if err := info.validateBasic(); err != nil {
panic(err)
}
return info
}
func (qm queueMapper) setQueueInfo(store sdk.KVStore, info queueInfo) {
bz, err := qm.cdc.MarshalBinary(info)
if err != nil {
panic(err)
}
store.Set(qm.ik, bz)
}
func (qm queueMapper) Push(ctx sdk.Context, value interface{}) {
store := ctx.KVStore(qm.key)
info := qm.getQueueInfo(store)
bz, err := qm.cdc.MarshalBinary(value)
if err != nil {
panic(err)
}
store.Set(marshalInt64(qm.cdc, info.End), bz)
info.End++
qm.setQueueInfo(store, info)
}
func (qm queueMapper) Peek(ctx sdk.Context, ptr interface{}) {
store := ctx.KVStore(qm.key)
info := qm.getQueueInfo(store)
bz := store.Get(marshalInt64(qm.cdc, info.Begin))
if err := qm.cdc.UnmarshalBinary(bz, ptr); err != nil {
panic(err)
}
}
func (qm queueMapper) Pop(ctx sdk.Context) {
store := ctx.KVStore(qm.key)
info := qm.getQueueInfo(store)
store.Delete(marshalInt64(qm.cdc, info.Begin))
info.Begin++
qm.setQueueInfo(store, info)
}
func (qm queueMapper) IsEmpty(ctx sdk.Context) bool {
store := ctx.KVStore(qm.key)
info := qm.getQueueInfo(store)
return info.isEmpty()
}
func (qm queueMapper) Iterate(ctx sdk.Context, ptr interface{}, fn func(sdk.Context) bool) {
store := ctx.KVStore(qm.key)
info := qm.getQueueInfo(store)
var i int64
for i = info.Begin; i < info.End; i++ {
key := marshalInt64(qm.cdc, i)
bz := store.Get(key)
if err := qm.cdc.UnmarshalBinary(bz, ptr); err != nil {
panic(err)
}
store.Delete(key)
if fn(ctx) {
break
}
}
info.Begin = i
qm.setQueueInfo(store, info)
}
func marshalQueueInfo(cdc *wire.Codec, info queueInfo) []byte {
bz, err := cdc.MarshalBinary(info)
if err != nil {
panic(err)
}
return bz
}
func marshalInt64(cdc *wire.Codec, i int64) []byte {
bz, err := cdc.MarshalBinary(i)
if err != nil {
panic(err)
}
return bz
}