-
Notifications
You must be signed in to change notification settings - Fork 4
/
treasury.go
248 lines (199 loc) · 6.5 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
package transaction_v2
import (
"context"
"errors"
"sync"
"time"
"github.com/sirupsen/logrus"
"github.com/code-payments/code-server/pkg/kin"
"github.com/code-payments/code-server/pkg/code/common"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/treasury"
)
// todo: any other treasury-related things we can put here?
func init() {
cachedTreasuryMetadatas = make(map[string]*cachedTreasuryMetadata)
}
var (
treasuryCacheMu sync.RWMutex
cachedTreasuryMetadatas map[string]*cachedTreasuryMetadata
)
type cachedTreasuryMetadata struct {
name string
stateAccount *common.Account
stateBump uint8
vaultAccount *common.Account
vaultBump uint8
mostRecentRoot string
lastUpdatedAt time.Time
}
func getCachedTreasuryMetadataByNameOrAddress(ctx context.Context, data code_data.Provider, nameOrAddress string, maxAge time.Duration) (*cachedTreasuryMetadata, error) {
treasuryCacheMu.RLock()
cached, ok := cachedTreasuryMetadatas[nameOrAddress]
if ok && time.Since(cached.lastUpdatedAt) < maxAge {
treasuryCacheMu.RUnlock()
return cached, nil
}
treasuryCacheMu.RUnlock()
treasuryCacheMu.Lock()
defer treasuryCacheMu.Unlock()
cached, ok = cachedTreasuryMetadatas[nameOrAddress]
if ok && time.Since(cached.lastUpdatedAt) < maxAge {
return cached, nil
}
var isName bool
_, err := common.NewAccountFromPublicKeyString(nameOrAddress)
if err != nil {
isName = true
}
var treasuryPoolRecord *treasury.Record
if isName {
treasuryPoolRecord, err = data.GetTreasuryPoolByName(ctx, nameOrAddress)
if err != nil {
return nil, err
}
} else {
treasuryPoolRecord, err = data.GetTreasuryPoolByAddress(ctx, nameOrAddress)
if err != nil {
return nil, err
}
}
stateAccount, err := common.NewAccountFromPublicKeyString(treasuryPoolRecord.Address)
if err != nil {
return nil, err
}
vaultAccount, err := common.NewAccountFromPublicKeyString(treasuryPoolRecord.Vault)
if err != nil {
return nil, err
}
cached = &cachedTreasuryMetadata{
name: treasuryPoolRecord.Name,
stateAccount: stateAccount,
stateBump: treasuryPoolRecord.Bump,
vaultAccount: vaultAccount,
vaultBump: treasuryPoolRecord.VaultBump,
mostRecentRoot: treasuryPoolRecord.GetMostRecentRoot(),
lastUpdatedAt: time.Now(),
}
cachedTreasuryMetadatas[treasuryPoolRecord.Name] = cached
cachedTreasuryMetadatas[treasuryPoolRecord.Address] = cached
return cached, nil
}
type treasuryPoolStats struct {
totalAvailableFunds uint64
totalDeficit uint64
lastUpdatedAt time.Time
}
// todo: Assumes constant and single treasury pool
func (s *transactionServer) treasuryPoolMonitor(ctx context.Context, name string) {
log := s.log.WithFields(logrus.Fields{
"method": "treasuryPoolMonitor",
"treasury": name,
})
var initialLoadCompleted bool
var treasuryPoolRecord *treasury.Record
var err error
for {
func() {
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
start := time.Now()
defer time.Sleep(s.conf.treasuryPoolStatsRefreshInterval.Get(ctx) - time.Since(start))
if treasuryPoolRecord == nil {
treasuryPoolRecord, err = s.data.GetTreasuryPoolByName(ctx, name)
if err != nil {
log.WithError(err).Warn("failure getting treasury pool record")
return
}
}
totalAvailable, err := s.data.GetTotalAvailableTreasuryPoolFunds(ctx, treasuryPoolRecord.Vault)
if err != nil {
log.WithError(err).Warn("failure getting total funds")
return
}
totalDeficit, err := s.data.GetTotalTreasuryPoolDeficitFromCommitments(ctx, treasuryPoolRecord.Address)
if err != nil {
log.WithError(err).Warn("failure getting total deficit")
return
}
s.treasuryPoolStatsLock.Lock()
defer s.treasuryPoolStatsLock.Unlock()
s.currentTreasuryPoolStatsByName[name] = &treasuryPoolStats{
totalAvailableFunds: totalAvailable,
totalDeficit: totalDeficit,
lastUpdatedAt: time.Now(),
}
if !initialLoadCompleted {
initialLoadCompleted = true
s.treasuryPoolStatsInitialLoadWgByName[name].Done()
}
}()
}
}
func (s *transactionServer) selectTreasuryPoolForAdvance(ctx context.Context, quarks uint64) (string, error) {
log := s.log.WithFields(logrus.Fields{
"method": "selectTreasuryPoolForAdvance",
"quarks": quarks,
})
for _, bucket := range []uint64{
kin.ToQuarks(1_000_000),
kin.ToQuarks(100_000),
kin.ToQuarks(10_000),
kin.ToQuarks(1_000),
kin.ToQuarks(100),
kin.ToQuarks(10),
kin.ToQuarks(1),
} {
// Not a multiple of the bucket
if quarks%bucket != 0 {
continue
}
// Sanity check for something too large to allow
if quarks > 9*bucket {
continue
}
return s.treasuryPoolNameByBaseAmount[bucket], nil
}
// If we reach this point, we must have allowed a bucket multiple that wasn't really allowed
log.Warn("no treasury selected to handle advance")
return "", errors.New("treasury not available")
}
func (s *transactionServer) areAllTreasuryPoolsAvailable(ctx context.Context) (bool, error) {
for _, treasuryPoolName := range s.treasuryPoolNameByBaseAmount {
isTreasuryAvailable, err := s.isTreasuryPoolAvailable(ctx, treasuryPoolName)
if err != nil {
return false, err
} else if !isTreasuryAvailable {
return false, nil
}
}
return true, nil
}
func (s *transactionServer) isTreasuryPoolAvailable(ctx context.Context, name string) (bool, error) {
log := s.log.WithFields(logrus.Fields{
"method": "isTreasuryPoolAvailable",
"treasury": name,
})
s.treasuryPoolStatsInitialLoadWgByName[name].Wait()
s.treasuryPoolStatsLock.RLock()
defer s.treasuryPoolStatsLock.RUnlock()
currentStats := s.currentTreasuryPoolStatsByName[name]
// Have a small threshold where we allow stats to be outdated to allow for blips
if time.Since(currentStats.lastUpdatedAt) >= time.Minute {
log.Warn("treasury pool stats are outdated")
return false, errors.New("treasury pool stats are outdated")
}
if currentStats.totalDeficit >= currentStats.totalAvailableFunds {
log.Warn("treasury pool isn't available because all funds are accounted for")
return false, nil
}
remaining := currentStats.totalAvailableFunds - currentStats.totalDeficit
// Try to maintain some available funds to avoid any potential deadlocks.
// If we happen to go slightly over, it's not the end of the world either.
percentAvailable := float64(remaining) / float64(currentStats.totalAvailableFunds)
if percentAvailable < 0.05 {
log.Warn("treasury pool isn't available because most funds are accounted for")
return false, nil
}
return true, nil
}