Skip to content

Commit 2322f66

Browse files
committed
fixed redundant calls
1 parent 0ff9e67 commit 2322f66

File tree

5 files changed

+45
-178
lines changed

5 files changed

+45
-178
lines changed

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,6 @@ github.com/klauspost/cpuid v0.0.0-20170728055534-ae7887de9fa5/go.mod h1:Pj4uuM52
279279
github.com/klauspost/crc32 v0.0.0-20161016154125-cb6bfca970f6/go.mod h1:+ZoRqAPRLkC4NPOvfYeR5KNOrY6TD+/sAC3HXPZgDYg=
280280
github.com/klauspost/pgzip v1.0.2-0.20170402124221-0bf5dcad4ada/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
281281
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
282-
github.com/korovkin/limiter v0.0.0-20220422174850-01f593e64cf7 h1:N3ses22gYxtKVl/lWjA9XdBcleoYw1dw2VgYBcM0ElI=
283-
github.com/korovkin/limiter v0.0.0-20220422174850-01f593e64cf7/go.mod h1:mM0lzivCxB6c8msz/LOP9lJgZxy92GXwGcNG1A7UZEE=
284282
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
285283
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
286284
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
@@ -432,8 +430,6 @@ github.com/willf/bitset v1.1.3/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPyS
432430
github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208/go.mod h1:IotVbo4F+mw0EzQ08zFqg7pK3FebNXpaMsRy2RT+Ees=
433431
github.com/xlab/treeprint v0.0.0-20180616005107-d6fb6747feb6/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg=
434432
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
435-
github.com/zenthangplus/goccm v0.0.0-20211005163543-2f2e522aca15 h1:0UwX38TojH86Dl0n4OwPMNYy8hNs2NhtzwY4RlvANlA=
436-
github.com/zenthangplus/goccm v0.0.0-20211005163543-2f2e522aca15/go.mod h1:DUzu/BC4TkgUfXP8J1P6Md73Djt+0l0CHq001Pt4weA=
437433
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
438434
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
439435
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=

internal/client/collators.go

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,6 @@ func (c *Client) FetchCollatorInfo(
243243
rank uint32,
244244
cfg config.CollatorsPoolConfig,
245245
) (CollatorInfo, error) {
246-
s := time.Now().UnixMilli()
247-
type collatorPerf struct {
248-
AccountInfo float32
249-
History float32
250-
Points float32
251-
Delegations float32
252-
}
253-
t := collatorPerf{}
254246
account, _ := types.HexDecodeString(address)
255247
var candidate candidateMetadataUnmarshal
256248
err := c.GetStorageRawAt(
@@ -269,19 +261,16 @@ func (c *Client) FetchCollatorInfo(
269261
if err != nil {
270262
return CollatorInfo{}, err
271263
}
272-
t.AccountInfo = float32(time.Now().UnixMilli()-s) / 1000.0
273264
// Get historyRounds
274265
history, err := c.FetchCollatorHistory(address, cfg.HistoryRounds)
275266
if err != nil {
276267
return CollatorInfo{}, err
277268
}
278-
t.History = float32(time.Now().UnixMilli()-s) / 1000.0
279269
// Get current points
280270
blocks, err := c.FetchCollatorBlocks(address, c.SnapRound.Number, c.SnapBlock.Hash)
281271
if err != nil {
282272
return CollatorInfo{}, err
283273
}
284-
t.Points = float32(time.Now().UnixMilli()-s) / 1000.0
285274
// Get amount from pool to avoid bugs in the candidate info data as happened in the past
286275
pool, err := c.FetchSortedCandidatePool(c.SnapBlock.Hash)
287276
if err != nil {
@@ -297,52 +286,12 @@ func (c *Client) FetchCollatorInfo(
297286
// Get delegations if requested
298287
cd := make([]DelegatorState, 0)
299288
if cfg.Revokes {
300-
var delegations struct {
301-
Delegations []candidateDelegationUnmarshal
302-
}
303-
err = c.GetStorageRawAt(
304-
"ParachainStaking",
305-
"TopDelegations",
306-
"Delegations<Balance>",
307-
c.SnapBlock.Hash,
308-
&delegations,
309-
account,
310-
)
289+
cd, err = c.getDelegations(address)
311290
if err != nil {
312-
log.Printf("Cannot load delegations %v\n", err)
291+
return CollatorInfo{}, err
313292
}
314-
// Fetch delegations
315-
ch := make(chan async.Result[DelegatorState])
316-
var wg sync.WaitGroup
317-
for _, delegation := range delegations.Delegations {
318-
wg.Add(1)
319-
delegator := delegation.Owner
320-
amount := delegation.Amount
321-
go func() {
322-
defer wg.Done()
323-
ch <- async.ResultFrom(c.FetchDelegatorState(address, delegator, amount))
324-
}()
325-
}
326-
// Wait channel
327-
go func() {
328-
wg.Wait()
329-
close(ch)
330-
}()
331-
for r := range ch {
332-
if r.Err != nil {
333-
log.Printf("Unable to fetch delegator state %v\n", r.Err)
334-
} else {
335-
cd = append(cd, r.Value)
336-
}
337-
}
338-
// Sort
339-
sort.Slice(cd[:], func(i, j int) bool {
340-
return cd[i].Amount.Balance.Cmp(cd[j].Amount.Balance) == 1
341-
})
342293
}
343294
// Done
344-
t.Delegations = float32(time.Now().UnixMilli()-s) / 1000.0
345-
log.Printf("%v: %v", address, t)
346295
return CollatorInfo{
347296
Address: address,
348297
Selected: selected,

internal/client/delegator.go

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
55
"log"
66
"math/big"
7+
"sort"
78
"strings"
89
)
910

@@ -26,22 +27,55 @@ type DelegatorState struct {
2627
RevokeRound uint32 `json:"revoke_round,omitempty"`
2728
}
2829

29-
func (c *Client) FetchDelegatorState(collator string, address string, total TokenAmount) (DelegatorState, error) {
30-
collatorAccount, _ := types.HexDecodeString(collator)
31-
// Fetch revokes for collator
32-
requests := make([]delegationScheduledRequestsUnmarshal, 0)
30+
func (c *Client) getDelegations(collator string) ([]DelegatorState, error) {
31+
account, _ := types.HexDecodeString(collator)
32+
cd := make([]DelegatorState, 0)
33+
var delegations struct {
34+
Delegations []candidateDelegationUnmarshal
35+
}
3336
err := c.GetStorageRawAt(
37+
"ParachainStaking",
38+
"TopDelegations",
39+
"Delegations<Balance>",
40+
c.SnapBlock.Hash,
41+
&delegations,
42+
account,
43+
)
44+
if err != nil {
45+
log.Printf("Cannot load delegations %v\n", err)
46+
return nil, err
47+
}
48+
// Fetch delegations
49+
requests := make([]delegationScheduledRequestsUnmarshal, 0)
50+
err = c.GetStorageRawAt(
3451
"ParachainStaking",
3552
"DelegationScheduledRequests",
3653
"Vec<DelegationScheduledRequests<DelegatorState<Balance>>>",
3754
c.SnapBlock.Hash,
3855
&requests,
39-
collatorAccount,
56+
account,
4057
)
4158
if err != nil {
42-
log.Printf("Unable to decode delegator state for %v\n", address)
43-
return DelegatorState{}, err
59+
log.Printf("Unable to decode delegator scheduled requests for %v\n", collator)
60+
return nil, err
61+
}
62+
// Get state
63+
for _, delegation := range delegations.Delegations {
64+
cd = append(cd, c.getDelegatorState(requests, delegation.Owner, delegation.Amount))
4465
}
66+
// Sort
67+
sort.Slice(cd[:], func(i, j int) bool {
68+
return cd[i].Amount.Balance.Cmp(cd[j].Amount.Balance) == 1
69+
})
70+
// Done
71+
return cd, nil
72+
}
73+
74+
func (c *Client) getDelegatorState(
75+
requests []delegationScheduledRequestsUnmarshal,
76+
address string,
77+
total TokenAmount,
78+
) DelegatorState {
4579
revokeAmount := TokenAmount{big.NewInt(0)}
4680
revokeReason := ""
4781
revokeRound := uint32(0)
@@ -66,5 +100,5 @@ func (c *Client) FetchDelegatorState(collator string, address string, total Toke
66100
RevokeReason: revokeReason,
67101
RevokeRound: revokeRound,
68102
}
69-
return r, nil
103+
return r
70104
}

internal/client/delegator_test.go

Lines changed: 0 additions & 112 deletions
This file was deleted.

internal/server/httpd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func ServeChainData(config config.HttpConfig) {
9797
panic(err)
9898
}
9999
// First update
100-
err = chainData.Update(0)
100+
err = chainData.Update(28)
101101
if err != nil {
102102
panic(err)
103103
}

0 commit comments

Comments
 (0)