-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtreasury.go
307 lines (228 loc) · 6.14 KB
/
treasury.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
package treasury
import (
"bytes"
"time"
"github.com/mr-tron/base58"
"github.com/pkg/errors"
splitter_token "github.com/code-payments/code-server/pkg/solana/splitter"
)
type TreasuryPoolState uint8
type FundingState uint8
const (
TreasuryPoolStateUnknown TreasuryPoolState = iota
TreasuryPoolStateAvailable
TreasuryPoolStateDeprecated
)
const (
FundingStateUnknown FundingState = iota
FundingStatePending
FundingStateConfirmed
FundingStateFailed
)
type Record struct {
Id uint64
DataVersion splitter_token.DataVersion
Name string
Address string
Bump uint8
Vault string
VaultBump uint8
Authority string
MerkleTreeLevels uint8
CurrentIndex uint8
HistoryListSize uint8
HistoryList []string // order maintained with on-chain state
SolanaBlock uint64
State TreasuryPoolState // currently managed manually
LastUpdatedAt time.Time
}
type FundingHistoryRecord struct {
Id uint64
Vault string
DeltaQuarks int64
TransactionId string
State FundingState
CreatedAt time.Time
}
func (r *Record) GetMostRecentRoot() string {
return r.HistoryList[r.CurrentIndex]
}
func (r *Record) GetPreviousMostRecentRoot() string {
previousIndex := r.CurrentIndex - 1
if r.CurrentIndex == 0 {
previousIndex = r.HistoryListSize - 1
}
return r.HistoryList[previousIndex]
}
func (r *Record) ContainsRecentRoot(recentRoot string) (bool, int) {
for i, historyItem := range r.HistoryList {
if historyItem == recentRoot {
deltaFromMostRecent := int(r.CurrentIndex) - i
if deltaFromMostRecent < 0 {
deltaFromMostRecent += int(r.HistoryListSize)
}
return true, deltaFromMostRecent
}
}
return false, 0
}
func (r *Record) Update(data *splitter_token.PoolAccount, solanaBlock uint64) error {
if data.DataVersion != splitter_token.DataVersion1 {
return errors.New("data version must be 1")
}
// Sanity check we're updating the right record by computing and checking
// the expected vault address
addressBytes, err := base58.Decode(r.Address)
if err != nil {
return errors.Wrap(err, "error decoding address")
}
vaultAddressBytes, _, err := splitter_token.GetPoolVaultAddress(&splitter_token.GetPoolVaultAddressArgs{
Pool: addressBytes,
})
if err != nil {
return errors.Wrap(err, "error getting vault address")
}
if !bytes.Equal(vaultAddressBytes, data.Vault) {
return errors.New("updating wrong pool record")
}
// Check to see if there are any actual updates to the treasury pool state
if solanaBlock <= r.SolanaBlock {
return ErrStaleTreasuryPoolState
}
if r.CurrentIndex == data.CurrentIndex {
var hasUpdatedHistoryList bool
for i := 0; i < int(r.HistoryListSize); i++ {
if r.HistoryList[i] != data.HistoryList[i].ToString() {
hasUpdatedHistoryList = true
break
}
}
if !hasUpdatedHistoryList {
return ErrStaleTreasuryPoolState
}
}
// It's now safe to update the record
r.CurrentIndex = data.CurrentIndex
historyList := make([]string, r.HistoryListSize)
for i, recentRoot := range data.HistoryList {
historyList[i] = recentRoot.ToString()
}
r.HistoryList = historyList
r.SolanaBlock = solanaBlock
return nil
}
func (r *Record) Validate() error {
if r.DataVersion != splitter_token.DataVersion1 {
return errors.New("data version must be 1")
}
if len(r.Name) == 0 {
return errors.New("name is required")
}
if len(r.Address) == 0 {
return errors.New("address is required")
}
if len(r.Vault) == 0 {
return errors.New("vault is required")
}
if len(r.Authority) == 0 {
return errors.New("authority is required")
}
if r.MerkleTreeLevels == 0 {
return errors.New("merkle tree levels is required")
}
if r.HistoryListSize == 0 {
return errors.New("history list size is required")
}
if r.CurrentIndex >= r.HistoryListSize {
return errors.New("current index must be less than history list size")
}
if len(r.HistoryList) != int(r.HistoryListSize) {
return errors.New("history list length must be equal to history list size")
}
for _, historyItem := range r.HistoryList {
if len(historyItem) == 0 {
return errors.New("history list values are required")
}
}
return nil
}
func (r *Record) Clone() *Record {
historyList := make([]string, len(r.HistoryList))
copy(historyList, r.HistoryList)
return &Record{
Id: r.Id,
DataVersion: r.DataVersion,
Name: r.Name,
Address: r.Address,
Bump: r.Bump,
Vault: r.Vault,
VaultBump: r.VaultBump,
Authority: r.Authority,
MerkleTreeLevels: r.MerkleTreeLevels,
CurrentIndex: r.CurrentIndex,
HistoryListSize: r.HistoryListSize,
HistoryList: historyList,
SolanaBlock: r.SolanaBlock,
State: r.State,
LastUpdatedAt: r.LastUpdatedAt,
}
}
func (r *Record) CopyTo(dst *Record) {
dst.Id = r.Id
dst.DataVersion = r.DataVersion
dst.Name = r.Name
dst.Address = r.Address
dst.Bump = r.Bump
dst.Vault = r.Vault
dst.VaultBump = r.VaultBump
dst.Authority = r.Authority
dst.MerkleTreeLevels = r.MerkleTreeLevels
dst.CurrentIndex = r.CurrentIndex
dst.HistoryListSize = r.HistoryListSize
dst.HistoryList = r.HistoryList
dst.SolanaBlock = r.SolanaBlock
dst.State = r.State
dst.LastUpdatedAt = r.LastUpdatedAt
}
func (s TreasuryPoolState) String() string {
switch s {
case TreasuryPoolStateAvailable:
return "available"
case TreasuryPoolStateDeprecated:
return "deprecated"
}
return "unknown"
}
func (r *FundingHistoryRecord) Validate() error {
if len(r.Vault) == 0 {
return errors.New("vault is required")
}
if r.DeltaQuarks == 0 {
return errors.New("quark delta is required")
}
if len(r.TransactionId) == 0 {
return errors.New("transaction id is required")
}
if r.CreatedAt.IsZero() {
return errors.New("creation time is zero")
}
return nil
}
func (r *FundingHistoryRecord) Clone() *FundingHistoryRecord {
return &FundingHistoryRecord{
Id: r.Id,
Vault: r.Vault,
DeltaQuarks: r.DeltaQuarks,
TransactionId: r.TransactionId,
State: r.State,
CreatedAt: r.CreatedAt,
}
}
func (r *FundingHistoryRecord) CopyTo(dst *FundingHistoryRecord) {
dst.Id = r.Id
dst.Vault = r.Vault
dst.DeltaQuarks = r.DeltaQuarks
dst.TransactionId = r.TransactionId
dst.State = r.State
dst.CreatedAt = r.CreatedAt
}