Skip to content

Commit 17864f3

Browse files
committed
Load currency reserve state from DB cache in GetMints
1 parent bcea9e1 commit 17864f3

File tree

12 files changed

+358
-173
lines changed

12 files changed

+358
-173
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
filippo.io/edwards25519 v1.1.0
77
github.com/aws/aws-sdk-go-v2 v0.17.0
88
github.com/bits-and-blooms/bloom/v3 v3.1.0
9-
github.com/code-payments/code-protobuf-api v1.19.1-0.20250825175844-b5a79ae87f0a
9+
github.com/code-payments/code-protobuf-api v1.19.1-0.20250826132123-e79bc35cff0f
1010
github.com/code-payments/code-vm-indexer v0.1.11-0.20241028132209-23031e814fba
1111
github.com/emirpasic/gods v1.12.0
1212
github.com/envoyproxy/protoc-gen-validate v1.2.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ github.com/code-payments/code-protobuf-api v1.19.1-0.20250825175528-dab522122ad6
8686
github.com/code-payments/code-protobuf-api v1.19.1-0.20250825175528-dab522122ad6/go.mod h1:ee6TzKbgMS42ZJgaFEMG3c4R3dGOiffHSu6MrY7WQvs=
8787
github.com/code-payments/code-protobuf-api v1.19.1-0.20250825175844-b5a79ae87f0a h1:oVponiCqodzMIR/9gucEbxG4pCQSFwcUQnydJjPv01U=
8888
github.com/code-payments/code-protobuf-api v1.19.1-0.20250825175844-b5a79ae87f0a/go.mod h1:ee6TzKbgMS42ZJgaFEMG3c4R3dGOiffHSu6MrY7WQvs=
89+
github.com/code-payments/code-protobuf-api v1.19.1-0.20250826132123-e79bc35cff0f h1:ujiDkjcrsk4wZOUQuaYytSQowUnOpT2icmCi6tVc8eY=
90+
github.com/code-payments/code-protobuf-api v1.19.1-0.20250826132123-e79bc35cff0f/go.mod h1:ee6TzKbgMS42ZJgaFEMG3c4R3dGOiffHSu6MrY7WQvs=
8991
github.com/code-payments/code-vm-indexer v0.1.11-0.20241028132209-23031e814fba h1:Bkp+gmeb6Y2PWXfkSCTMBGWkb2P1BujRDSjWeI+0j5I=
9092
github.com/code-payments/code-vm-indexer v0.1.11-0.20241028132209-23031e814fba/go.mod h1:jSiifpiBpyBQ8q2R0MGEbkSgWC6sbdRTyDBntmW+j1E=
9193
github.com/containerd/continuity v0.0.0-20190827140505-75bee3e2ccb6 h1:NmTXa/uVnDyp0TY5MKi197+3HWcnYWfnHGyaFthlnGw=

pkg/code/data/currency/memory/store.go

Lines changed: 95 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,74 +6,88 @@ import (
66
"sync"
77
"time"
88

9-
"github.com/code-payments/code-server/pkg/database/query"
109
"github.com/code-payments/code-server/pkg/code/data/currency"
10+
"github.com/code-payments/code-server/pkg/database/query"
1111
)
1212

1313
const (
1414
dateFormat = "2006-01-02"
1515
)
1616

1717
type store struct {
18-
currencyStoreMu sync.Mutex
19-
currencyStore []*currency.ExchangeRateRecord
20-
lastIndex uint64
18+
mu sync.Mutex
19+
exchangeRateRecords []*currency.ExchangeRateRecord
20+
lastExchangeRateIndex uint64
21+
reserveRecords []*currency.ReserveRecord
22+
lastReserveIndex uint64
23+
}
24+
25+
type RateByTime []*currency.ExchangeRateRecord
26+
27+
func (a RateByTime) Len() int { return len(a) }
28+
func (a RateByTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
29+
func (a RateByTime) Less(i, j int) bool {
30+
// DESC order (most recent first)
31+
return a[i].Time.Unix() > a[j].Time.Unix()
2132
}
2233

23-
type ByTime []*currency.ExchangeRateRecord
34+
type ReserveByTime []*currency.ReserveRecord
2435

25-
func (a ByTime) Len() int { return len(a) }
26-
func (a ByTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
27-
func (a ByTime) Less(i, j int) bool {
36+
func (a ReserveByTime) Len() int { return len(a) }
37+
func (a ReserveByTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
38+
func (a ReserveByTime) Less(i, j int) bool {
2839
// DESC order (most recent first)
2940
return a[i].Time.Unix() > a[j].Time.Unix()
3041
}
3142

3243
func New() currency.Store {
3344
return &store{
34-
currencyStore: make([]*currency.ExchangeRateRecord, 0),
35-
lastIndex: 1,
45+
exchangeRateRecords: make([]*currency.ExchangeRateRecord, 0),
46+
lastExchangeRateIndex: 1,
3647
}
3748
}
3849

3950
func (s *store) reset() {
40-
s.currencyStoreMu.Lock()
41-
s.currencyStore = make([]*currency.ExchangeRateRecord, 0)
42-
s.currencyStoreMu.Unlock()
51+
s.mu.Lock()
52+
s.exchangeRateRecords = make([]*currency.ExchangeRateRecord, 0)
53+
s.lastExchangeRateIndex = 1
54+
s.reserveRecords = make([]*currency.ReserveRecord, 0)
55+
s.lastReserveIndex = 1
56+
s.mu.Unlock()
4357
}
4458

45-
func (s *store) Put(ctx context.Context, data *currency.MultiRateRecord) error {
46-
s.currencyStoreMu.Lock()
47-
defer s.currencyStoreMu.Unlock()
59+
func (s *store) PutExchangeRates(ctx context.Context, data *currency.MultiRateRecord) error {
60+
s.mu.Lock()
61+
defer s.mu.Unlock()
4862

4963
// Not ideal but fine for testing the currency store
50-
for _, item := range s.currencyStore {
64+
for _, item := range s.exchangeRateRecords {
5165
if item.Time.Unix() == data.Time.Unix() {
5266
return currency.ErrExists
5367
}
5468
}
5569

5670
for symbol, item := range data.Rates {
57-
s.currencyStore = append(s.currencyStore, &currency.ExchangeRateRecord{
58-
Id: s.lastIndex,
71+
s.exchangeRateRecords = append(s.exchangeRateRecords, &currency.ExchangeRateRecord{
72+
Id: s.lastExchangeRateIndex,
5973
Rate: item,
6074
Time: data.Time,
6175
Symbol: symbol,
6276
})
63-
s.lastIndex = s.lastIndex + 1
77+
s.lastExchangeRateIndex = s.lastExchangeRateIndex + 1
6478
}
6579

6680
return nil
6781
}
6882

69-
func (s *store) Get(ctx context.Context, symbol string, t time.Time) (*currency.ExchangeRateRecord, error) {
70-
s.currencyStoreMu.Lock()
71-
defer s.currencyStoreMu.Unlock()
83+
func (s *store) GetExchangeRate(ctx context.Context, symbol string, t time.Time) (*currency.ExchangeRateRecord, error) {
84+
s.mu.Lock()
85+
defer s.mu.Unlock()
7286

7387
// Not ideal but fine for testing the currency store
7488
var results []*currency.ExchangeRateRecord
75-
for _, item := range s.currencyStore {
76-
if item.Symbol == symbol && item.Time.Unix() <= t.Unix() {
89+
for _, item := range s.exchangeRateRecords {
90+
if item.Symbol == symbol && item.Time.Unix() <= t.Unix() && item.Time.Format(dateFormat) == t.Format(dateFormat) {
7791
results = append(results, item)
7892
}
7993
}
@@ -82,24 +96,25 @@ func (s *store) Get(ctx context.Context, symbol string, t time.Time) (*currency.
8296
return nil, currency.ErrNotFound
8397
}
8498

85-
sort.Sort(ByTime(results))
99+
sort.Sort(RateByTime(results))
86100

87101
return results[0], nil
88102
}
89103

90-
func (s *store) GetAll(ctx context.Context, t time.Time) (*currency.MultiRateRecord, error) {
91-
s.currencyStoreMu.Lock()
92-
defer s.currencyStoreMu.Unlock()
104+
func (s *store) GetAllExchangeRates(ctx context.Context, t time.Time) (*currency.MultiRateRecord, error) {
105+
s.mu.Lock()
106+
defer s.mu.Unlock()
93107

94108
// Not ideal but fine for testing the currency store
95-
sort.Sort(ByTime(s.currencyStore))
109+
sort.Sort(RateByTime(s.exchangeRateRecords))
96110

97111
result := currency.MultiRateRecord{
98112
Rates: make(map[string]float64),
99113
}
100-
for _, item := range s.currencyStore {
114+
for _, item := range s.exchangeRateRecords {
101115
if item.Time.Unix() <= t.Unix() && item.Time.Format(dateFormat) == t.Format(dateFormat) {
102116
result.Rates[item.Symbol] = item.Rate
117+
result.Time = item.Time
103118
}
104119
}
105120

@@ -110,15 +125,15 @@ func (s *store) GetAll(ctx context.Context, t time.Time) (*currency.MultiRateRec
110125
return &result, nil
111126
}
112127

113-
func (s *store) GetRange(ctx context.Context, symbol string, interval query.Interval, start time.Time, end time.Time, ordering query.Ordering) ([]*currency.ExchangeRateRecord, error) {
114-
s.currencyStoreMu.Lock()
115-
defer s.currencyStoreMu.Unlock()
128+
func (s *store) GetExchangeRatesInRange(ctx context.Context, symbol string, interval query.Interval, start time.Time, end time.Time, ordering query.Ordering) ([]*currency.ExchangeRateRecord, error) {
129+
s.mu.Lock()
130+
defer s.mu.Unlock()
116131

117-
sort.Sort(ByTime(s.currencyStore))
132+
sort.Sort(RateByTime(s.exchangeRateRecords))
118133

119134
// Not ideal but fine for testing the currency store
120135
var all []*currency.ExchangeRateRecord
121-
for _, item := range s.currencyStore {
136+
for _, item := range s.exchangeRateRecords {
122137
if item.Symbol == symbol && item.Time.Unix() >= start.Unix() && item.Time.Unix() <= end.Unix() {
123138
all = append(all, item)
124139
}
@@ -138,3 +153,47 @@ func (s *store) GetRange(ctx context.Context, symbol string, interval query.Inte
138153

139154
return all, nil
140155
}
156+
157+
func (s *store) PutReserveRecord(ctx context.Context, data *currency.ReserveRecord) error {
158+
s.mu.Lock()
159+
defer s.mu.Unlock()
160+
161+
// Not ideal but fine for testing the currency store
162+
for _, item := range s.reserveRecords {
163+
if item.Mint == data.Mint && item.Time.Unix() == data.Time.Unix() {
164+
return currency.ErrExists
165+
}
166+
}
167+
168+
s.reserveRecords = append(s.reserveRecords, &currency.ReserveRecord{
169+
Id: s.lastReserveIndex,
170+
Mint: data.Mint,
171+
SupplyFromBonding: data.SupplyFromBonding,
172+
CoreMintLocked: data.CoreMintLocked,
173+
Time: data.Time,
174+
})
175+
s.lastReserveIndex = s.lastReserveIndex + 1
176+
177+
return nil
178+
}
179+
180+
func (s *store) GetReserveAtTime(ctx context.Context, mint string, t time.Time) (*currency.ReserveRecord, error) {
181+
s.mu.Lock()
182+
defer s.mu.Unlock()
183+
184+
// Not ideal but fine for testing the currency store
185+
var results []*currency.ReserveRecord
186+
for _, item := range s.reserveRecords {
187+
if item.Mint == mint && item.Time.Unix() <= t.Unix() && item.Time.Format(dateFormat) == t.Format(dateFormat) {
188+
results = append(results, item)
189+
}
190+
}
191+
192+
if len(results) == 0 {
193+
return nil, currency.ErrNotFound
194+
}
195+
196+
sort.Sort(ReserveByTime(results))
197+
198+
return results[0], nil
199+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package memory
2+
3+
import (
4+
"testing"
5+
6+
"github.com/code-payments/code-server/pkg/code/data/currency/tests"
7+
)
8+
9+
func TestCurrencyMemoryStore(t *testing.T) {
10+
testStore := New()
11+
teardown := func() {
12+
testStore.(*store).reset()
13+
}
14+
tests.RunTests(t, testStore, teardown)
15+
}

0 commit comments

Comments
 (0)