forked from solana-labs/solana-ping-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpcPing.go
209 lines (189 loc) · 5.23 KB
/
rpcPing.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
package main
import (
"context"
"math"
"sort"
"strings"
"time"
"github.com/blocto/solana-go-sdk/client"
"github.com/blocto/solana-go-sdk/common"
"github.com/blocto/solana-go-sdk/types"
)
const WaitConfirmationQueryTimeout = 10
// TakeTime a struct to record a serious of start and end time. Start and End is used for current record and Times is for store take-time
type TakeTime struct {
Times []int64
Start int64
End int64
}
// Ping similar to solana-bench-tps. It send a transaction to the cluster
func Ping(c *client.Client, pType PingType, acct types.Account, config ClusterConfig, feeEnabled bool) (PingResult, PingResultError) {
resultErrs := []string{}
timer := TakeTime{}
result := PingResult{
Cluster: string(config.Cluster),
Hostname: config.HostName,
PingType: string(pType),
}
confirmedCount := 0
computeUnitPrice := getFee(c, acct)
for i := 0; i < config.BatchCount; i++ {
if i > 0 {
time.Sleep(time.Duration(config.BatchInverval))
}
timer.TimerStart()
if !feeEnabled || 0 == config.ComputeUnitPrice {
txhash, pingErr := Transfer(c, acct, acct, config.Receiver, time.Duration(config.TxTimeout)*time.Second)
if pingErr.HasError() {
timer.TimerStop()
if !pingErr.IsInErrorList(PingTakeTimeErrExpectionList) {
timer.Add()
}
resultErrs = append(resultErrs, string(pingErr))
continue
}
waitErr := waitConfirmation(
c,
txhash,
time.Duration(config.WaitConfirmationTimeout)*time.Second,
time.Duration(WaitConfirmationQueryTimeout)*time.Second,
time.Duration(config.StatusCheckInterval)*time.Millisecond,
)
timer.TimerStop()
if waitErr.HasError() {
resultErrs = append(resultErrs, string(waitErr))
if !waitErr.IsInErrorList(PingTakeTimeErrExpectionList) {
timer.Add()
}
continue
}
timer.Add()
confirmedCount++
} else {
txhash, blockhash, pingErr := SendPingTx(SendPingTxParam{
Client: c,
FeePayer: acct,
RequestComputeUnits: config.RequestUnits,
ComputeUnitPrice: computeUnitPrice,
ReceiverPubkey: config.Receiver,
})
if pingErr.HasError() {
timer.TimerStop()
if !pingErr.IsInErrorList(PingTakeTimeErrExpectionList) {
timer.Add()
}
resultErrs = append(resultErrs, string(pingErr))
continue
}
waitErr := waitConfirmationOrBlockhashInvalid(c, txhash, blockhash)
timer.TimerStop()
if waitErr.HasError() {
resultErrs = append(resultErrs, string(waitErr))
if !waitErr.IsInErrorList(PingTakeTimeErrExpectionList) {
timer.Add()
}
continue
}
timer.Add()
confirmedCount++
}
}
result.TimeStamp = time.Now().UTC().Unix()
result.Submitted = config.BatchCount
result.Confirmed = confirmedCount
result.Loss = (float64(result.Submitted-result.Confirmed) / float64(result.Submitted)) * 100
max, mean, min, stdDev, total := timer.Statistic()
result.Max = max
result.Mean = int64(mean)
result.Min = min
result.Stddev = int64(stdDev)
result.TakeTime = total
result.ComputeUnitPrice = computeUnitPrice
result.RequestComputeUnits = config.RequestUnits
result.Error = resultErrs
stringErrors := []string(result.Error)
if 0 == len(stringErrors) {
return result, EmptyPingResultError
}
return result, PingResultError(strings.Join(stringErrors[:], ","))
}
// TimerStart Record start time in ms format
func (t *TakeTime) TimerStart() {
t.Start = time.Now().UTC().UnixMilli()
}
// TimerStop Record stop time in ms format
func (t *TakeTime) TimerStop() {
t.End = time.Now().UTC().UnixMilli()
}
// Add save the end - stop in ms
func (t *TakeTime) Add() {
t.Times = append(t.Times, (t.End - t.Start))
}
// AddTime add a take time directly into Times
func (t *TakeTime) AddTime(ts int64) {
t.Times = append(t.Times, ts)
}
// TotalTime sum of data in Times
func (t *TakeTime) TotalTime() int64 {
sum := int64(0)
for _, ts := range t.Times {
sum += ts
}
return sum
}
// Statistic analyze data in TakeTime to return max/mean/min/stddev/sum
func (t *TakeTime) Statistic() (max int64, mean float64, min int64, stddev float64, sum int64) {
count := 0
for _, ts := range t.Times {
if ts <= 0 { // do not use 0 data because it is the bad data
continue
}
if max == 0 {
max = ts
}
if min == 0 {
min = ts
}
if ts >= max {
max = ts
}
if ts <= min {
min = ts
}
sum += ts
count++
}
if count > 0 {
mean = float64(sum) / float64(count)
for _, ts := range t.Times {
if ts > 0 { // if ts = 0 , ping fail.
stddev += math.Pow(float64(ts)-mean, 2)
}
}
stddev = math.Sqrt(stddev / float64(count))
}
return
}
func getFee(c *client.Client, account types.Account) uint64 {
// at least 1 to trigger the system send the fee tx. can be updated if the logic is fixed.
computeUnitPrice := uint64(1)
// get the max(the last 100 blocks)
fees, err := c.GetRecentPrioritizationFees(context.Background(), []common.PublicKey{account.PublicKey})
if err == nil {
sort.Slice(fees, func(i, j int) bool {
return fees[i].Slot > fees[j].Slot
})
for i := 0; i < len(fees) && i < 100; i++ {
var pFee = fees[i].PrioritizationFee
if pFee > computeUnitPrice {
computeUnitPrice = pFee
}
cap := uint64(100_000_000)
if computeUnitPrice > cap {
computeUnitPrice = cap
break
}
}
}
return computeUnitPrice
}