-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore.go
221 lines (178 loc) · 4.76 KB
/
store.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
package memory
import (
"context"
"sort"
"sync"
"time"
"github.com/code-payments/code-server/pkg/database/query"
timelock_token "github.com/code-payments/code-server/pkg/solana/timelock/v1"
"github.com/code-payments/code-server/pkg/code/data/timelock"
)
type store struct {
mu sync.Mutex
records []*timelock.Record
last uint64
}
type ById []*timelock.Record
func (a ById) Len() int { return len(a) }
func (a ById) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ById) Less(i, j int) bool { return a[i].Id < a[j].Id }
// New returns a new in memory timelock.Store
func New() timelock.Store {
return &store{}
}
// Save implements timelock.Store.Save
func (s *store) Save(_ context.Context, data *timelock.Record) error {
if err := data.Validate(); err != nil {
return err
}
s.mu.Lock()
defer s.mu.Unlock()
s.last++
if item := s.find(data); item != nil {
if data.Block <= item.Block {
return timelock.ErrStaleTimelockState
}
var unlockAt *uint64
if data.UnlockAt != nil {
value := *data.UnlockAt
unlockAt = &value
}
item.DataVersion = data.DataVersion
item.CloseAuthority = data.CloseAuthority
item.TimeAuthority = data.TimeAuthority
item.VaultState = data.VaultState
item.UnlockAt = unlockAt
item.Block = data.Block
item.LastUpdatedAt = time.Now()
item.CopyTo(data)
} else {
if data.Id == 0 {
data.Id = s.last
}
data.LastUpdatedAt = time.Now()
c := data.Clone()
s.records = append(s.records, c)
}
return nil
}
// GetByAddress implements timelock.Store.GetByAddress
func (s *store) GetByAddress(_ context.Context, address string) (*timelock.Record, error) {
s.mu.Lock()
defer s.mu.Unlock()
if item := s.findByAddress(address); item != nil {
return item.Clone(), nil
}
return nil, timelock.ErrTimelockNotFound
}
// GetByVault implements timelock.Store.GetByVault
func (s *store) GetByVault(_ context.Context, vault string) (*timelock.Record, error) {
s.mu.Lock()
defer s.mu.Unlock()
if item := s.findByVault(vault); item != nil {
return item.Clone(), nil
}
return nil, timelock.ErrTimelockNotFound
}
// GetByVaultBatch implements timelock.Store.GetByVaultBatch
func (s *store) GetByVaultBatch(ctx context.Context, vaults ...string) (map[string]*timelock.Record, error) {
s.mu.Lock()
defer s.mu.Unlock()
res := make(map[string]*timelock.Record)
for _, vault := range vaults {
item := s.findByVault(vault)
if item == nil {
return nil, timelock.ErrTimelockNotFound
}
res[vault] = item.Clone()
}
return res, nil
}
// GetAllByState implements timelock.Store.GetAllByState
func (s *store) GetAllByState(ctx context.Context, state timelock_token.TimelockState, cursor query.Cursor, limit uint64, direction query.Ordering) ([]*timelock.Record, error) {
s.mu.Lock()
defer s.mu.Unlock()
if items := s.findByState(state); len(items) > 0 {
res := s.filter(items, cursor, limit, direction)
if len(res) == 0 {
return nil, timelock.ErrTimelockNotFound
}
return res, nil
}
return nil, timelock.ErrTimelockNotFound
}
// GetCountByState implements timelock.Store.GetCountByState
func (s *store) GetCountByState(ctx context.Context, state timelock_token.TimelockState) (uint64, error) {
s.mu.Lock()
defer s.mu.Unlock()
items := s.findByState(state)
return uint64(len(items)), nil
}
func (s *store) find(data *timelock.Record) *timelock.Record {
for _, item := range s.records {
if item.Id == data.Id {
return item
}
if data.Address == item.Address {
return item
}
}
return nil
}
func (s *store) findByAddress(address string) *timelock.Record {
for _, item := range s.records {
if address == item.Address {
return item
}
}
return nil
}
func (s *store) findByVault(vault string) *timelock.Record {
for _, item := range s.records {
if vault == item.VaultAddress {
return item
}
}
return nil
}
func (s *store) findByState(state timelock_token.TimelockState) []*timelock.Record {
res := make([]*timelock.Record, 0)
for _, item := range s.records {
if item.VaultState == state {
res = append(res, item.Clone())
}
}
return res
}
func (s *store) filter(items []*timelock.Record, cursor query.Cursor, limit uint64, direction query.Ordering) []*timelock.Record {
var start uint64
start = 0
if direction == query.Descending {
start = s.last + 1
}
if len(cursor) > 0 {
start = cursor.ToUint64()
}
var res []*timelock.Record
for _, item := range items {
if item.Id > start && direction == query.Ascending {
res = append(res, item)
}
if item.Id < start && direction == query.Descending {
res = append(res, item)
}
}
if direction == query.Descending {
sort.Sort(sort.Reverse(ById(res)))
}
if len(res) >= int(limit) {
return res[:limit]
}
return res
}
func (s *store) reset() {
s.mu.Lock()
defer s.mu.Unlock()
s.records = nil
s.last = 0
}