Skip to content

Commit 3f33ebf

Browse files
committed
speedup staking requests
1 parent f8f461a commit 3f33ebf

File tree

5 files changed

+31
-17
lines changed

5 files changed

+31
-17
lines changed

pkg/api/interfaces.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type storage interface {
5252
GetWhalesPoolMemberInfo(ctx context.Context, pool, member tongo.AccountID) (core.Nominator, error)
5353
GetWhalesPoolInfo(ctx context.Context, id tongo.AccountID) (abi.GetParams_WhalesNominatorResult, abi.GetStakingStatusResult, int, uint64, error)
5454
GetParticipatingInTfPools(ctx context.Context, member tongo.AccountID) ([]core.Nominator, error)
55-
GetTFPools(ctx context.Context, onlyVerified bool) ([]core.TFPool, error)
55+
GetTFPools(ctx context.Context, onlyVerified bool, availableFor *tongo.AccountID) ([]core.TFPool, error)
5656
GetTFPool(ctx context.Context, pool tongo.AccountID) (core.TFPool, error)
5757
GetLiquidPool(ctx context.Context, pool tongo.AccountID) (core.LiquidPool, error)
5858
GetParticipatingInLiquidPools(ctx context.Context, member tongo.AccountID) ([]core.Nominator, error)

pkg/api/staking_handlers.go

+5-14
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@ func (h *Handler) GetStakingPoolInfo(ctx context.Context, params oas.GetStakingP
8989

9090
func (h *Handler) GetStakingPools(ctx context.Context, params oas.GetStakingPoolsParams) (*oas.GetStakingPoolsOK, error) {
9191
var result oas.GetStakingPoolsOK
92-
tfPools, err := h.storage.GetTFPools(ctx, !params.IncludeUnverified.Value)
93-
if err != nil {
94-
return nil, toError(http.StatusInternalServerError, err)
95-
}
96-
var minTF, minWhales int64
9792
var availableFor *tongo.AccountID
9893
var participatePools []tongo.AccountID
9994
if params.AvailableFor.IsSet() {
@@ -110,13 +105,12 @@ func (h *Handler) GetStakingPools(ctx context.Context, params oas.GetStakingPool
110105
participatePools = append(participatePools, p.Pool)
111106
}
112107
}
108+
tfPools, err := h.storage.GetTFPools(ctx, !params.IncludeUnverified.Value, availableFor)
109+
if err != nil {
110+
return nil, toError(http.StatusInternalServerError, err)
111+
}
112+
var minTF, minWhales int64
113113
for _, p := range tfPools {
114-
if availableFor != nil && !slices.Contains(participatePools, p.Address) &&
115-
(p.Nominators >= p.MaxNominators || //hide nominators without slots
116-
p.ValidatorShare < 4000 || //hide validators which take less than 40%
117-
p.MinNominatorStake < 10_000_000_000_000) { //hide nominators with unsafe minimal stake
118-
continue
119-
}
120114
info, _ := h.addressBook.GetTFPoolInfo(p.Address)
121115
pool := convertStakingTFPool(p, info, h.state.GetAPY())
122116
if minTF == 0 || pool.MinStake < minTF {
@@ -147,7 +141,6 @@ func (h *Handler) GetStakingPools(ctx context.Context, params oas.GetStakingPool
147141
}
148142
result.Pools = append(result.Pools, pool)
149143
}
150-
151144
liquidPools, err := h.storage.GetLiquidPools(ctx, !params.IncludeUnverified.Value)
152145
if err != nil {
153146
return nil, toError(http.StatusInternalServerError, err)
@@ -171,7 +164,6 @@ func (h *Handler) GetStakingPools(ctx context.Context, params oas.GetStakingPool
171164
p.Name = info.Name
172165
result.Pools = append(result.Pools, convertLiquidStaking(p, cycleStart, cycleEnd))
173166
}
174-
175167
slices.SortFunc(result.Pools, func(a, b oas.PoolInfo) int {
176168
if a.Apy == b.Apy {
177169
return 0
@@ -202,7 +194,6 @@ func (h *Handler) GetStakingPools(ctx context.Context, params oas.GetStakingPool
202194
Socials: references.TonstakersSocialLinks,
203195
},
204196
})
205-
206197
return &result, nil
207198
}
208199

pkg/litestorage/config.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package litestorage
22

33
import (
44
"context"
5+
"time"
56

7+
cache "github.com/Code-Hex/go-generics-cache"
68
"github.com/prometheus/client_golang/prometheus"
79
"github.com/tonkeeper/opentonapi/pkg/core"
810
"github.com/tonkeeper/tongo/tlb"
@@ -14,7 +16,17 @@ func (c *LiteStorage) GetLastConfig(ctx context.Context) (tlb.ConfigParams, erro
1416
storageTimeHistogramVec.WithLabelValues("get_last_config").Observe(v)
1517
}))
1618
defer timer.ObserveDuration()
17-
return c.client.GetConfigAll(ctx, 0)
19+
config, prs := c.configCache.Get(1)
20+
if prs {
21+
return config, nil
22+
23+
}
24+
config, err := c.client.GetConfigAll(ctx, 0)
25+
if err != nil {
26+
return tlb.ConfigParams{}, err
27+
}
28+
c.configCache.Set(1, config, cache.WithExpiration(time.Second*1)) //todo: remove
29+
return config, err
1830
}
1931

2032
func (c *LiteStorage) GetConfigFromBlock(ctx context.Context, id ton.BlockID) (tlb.ConfigParams, error) {

pkg/litestorage/litestorage.go

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type LiteStorage struct {
8888
// trackingAccounts is a list of accounts we track. Defined with ACCOUNTS env variable.
8989
trackingAccounts map[tongo.AccountID]struct{}
9090
pubKeyByAccountID *xsync.MapOf[tongo.AccountID, ed25519.PublicKey]
91+
configCache cache.Cache[int, tlb.ConfigParams]
9192

9293
stopCh chan struct{}
9394
// mu protects trimmedConfigBase64.
@@ -168,6 +169,7 @@ func NewLiteStorage(log *zap.Logger, cli *liteapi.Client, opts ...Option) (*Lite
168169
accountInterfacesCache: xsync.NewTypedMapOf[tongo.AccountID, []abi.ContractInterface](hashAccountID),
169170
pubKeyByAccountID: xsync.NewTypedMapOf[tongo.AccountID, ed25519.PublicKey](hashAccountID),
170171
tvmLibraryCache: cache.NewLRUCache[string, boc.Cell](10000, "tvm_libraries"),
172+
configCache: cache.NewLRUCache[int, tlb.ConfigParams](4, "config"),
171173
}
172174
storage.knownAccounts["tf_pools"] = o.tfPools
173175
storage.knownAccounts["jettons"] = o.jettons

pkg/litestorage/stacking.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"math/big"
88

99
"github.com/tonkeeper/tongo/tlb"
10+
"github.com/tonkeeper/tongo/ton"
1011

1112
"github.com/prometheus/client_golang/prometheus"
1213

@@ -159,13 +160,21 @@ func (s *LiteStorage) GetTFPool(ctx context.Context, pool tongo.AccountID) (core
159160
VerifiedSources: bytes.Equal(hash, references.TFPoolCodeHash[:]),
160161
}, nil
161162
}
162-
func (s *LiteStorage) GetTFPools(ctx context.Context, onlyVerified bool) ([]core.TFPool, error) {
163+
func (s *LiteStorage) GetTFPools(ctx context.Context, onlyVerified bool, availableFor *ton.AccountID) ([]core.TFPool, error) {
163164
var result []core.TFPool
164165
for _, a := range s.knownAccounts["tf_pools"] {
165166
p, err := s.GetTFPool(ctx, a)
166167
if err != nil {
167168
continue
168169
}
170+
if availableFor != nil {
171+
var i big.Int
172+
i.SetBytes(availableFor.Address[:])
173+
_, _, err := abi.GetNominatorData(ctx, s.executor, a, tlb.Int257(i))
174+
if err != nil && p.Nominators >= p.MaxNominators {
175+
continue
176+
}
177+
}
169178
result = append(result, p)
170179
}
171180
return result, nil

0 commit comments

Comments
 (0)