/
utils.go
246 lines (225 loc) · 7.3 KB
/
utils.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
package website
import (
_ "embed"
"fmt"
"math/big"
"sort"
"strings"
"github.com/flashbots/relayscan/common"
"github.com/flashbots/relayscan/database"
"github.com/olekukonko/tablewriter"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
var (
// Printer for pretty printing numbers
printer = message.NewPrinter(language.English)
// Caser is used for casing strings
caser = cases.Title(language.English)
)
func weiToEth(wei string) string {
weiBigInt := new(big.Int)
weiBigInt.SetString(wei, 10)
ethValue := weiBigIntToEthBigFloat(weiBigInt)
return ethValue.String()
}
func weiBigIntToEthBigFloat(wei *big.Int) (ethValue *big.Float) {
// wei / 10^18
fbalance := new(big.Float)
fbalance.SetString(wei.String())
ethValue = new(big.Float).Quo(fbalance, big.NewFloat(1e18))
return
}
func prettyInt(i uint64) string {
return printer.Sprintf("%d", i)
}
func caseIt(s string) string {
return caser.String(s)
}
func percent(cnt, total uint64) string {
p := float64(cnt) / float64(total) * 100
return printer.Sprintf("%.2f", p)
}
func builderTable(builders []*database.TopBuilderEntry) string {
buildersEntries := [][]string{}
for _, builder := range builders {
buildersEntries = append(buildersEntries, []string{
builder.ExtraData,
printer.Sprintf("%d", builder.NumBlocks),
builder.Percent,
})
}
tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString)
table.SetHeader([]string{"Builder extra_data", "Blocks", "%"})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetAutoWrapText(false)
table.SetCenterSeparator("|")
table.AppendBulk(buildersEntries)
table.Render()
// fmt.Println(tableString.String())
return tableString.String()
}
func builderProfitTable(entries []*database.BuilderProfitEntry) string {
tableEntries := [][]string{}
for _, builder := range entries {
tableEntries = append(tableEntries, []string{
builder.ExtraData,
printer.Sprintf("%d", builder.NumBlocks),
printer.Sprintf("%d", builder.NumBlocksProfit),
printer.Sprintf("%d", builder.NumBlocksSubsidised),
builder.ProfitTotal,
builder.SubsidiesTotal,
})
}
tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString)
table.SetHeader([]string{"Builder extra_data", "Blocks", "Blocks profit", "Blocks subsidy", "Profit total", "Subsidies total"})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetAutoWrapText(false)
table.SetCenterSeparator("|")
table.AppendBulk(tableEntries)
table.Render()
// fmt.Println(tableString.String())
return tableString.String()
}
func relayTable(relays []*database.TopRelayEntry) string {
relayEntries := [][]string{}
for _, relay := range relays {
relayEntries = append(relayEntries, []string{
relay.Relay,
printer.Sprintf("%d", relay.NumPayloads),
relay.Percent,
})
}
tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString)
table.SetHeader([]string{"Relay", "Payloads", "%"})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetAutoWrapText(false)
table.SetCenterSeparator("|")
table.AppendBulk(relayEntries)
table.Render()
// fmt.Println(tableString.String())
return tableString.String()
}
func strToBigFloat(f string) *big.Float {
bf := new(big.Float)
bf.SetString(f)
return bf
}
func addFloatStrings(f1, f2 string, decimals int) string {
bf1 := strToBigFloat(f1)
bf2 := strToBigFloat(f2)
return new(big.Float).Add(bf1, bf2).Text('f', decimals)
}
func divFloatStrings(f1, f2 string, decimals int) string {
bf1 := strToBigFloat(f1)
bf2 := strToBigFloat(f2)
return new(big.Float).Quo(bf1, bf2).Text('f', decimals)
}
func consolidateBuilderEntries(builders []*database.TopBuilderEntry) []*database.TopBuilderEntry {
// Get total builder payloads, and build consolidated builder list
buildersMap := make(map[string]*database.TopBuilderEntry)
buildersNumPayloads := uint64(0)
for _, entry := range builders {
buildersNumPayloads += entry.NumBlocks
updated := false
for k, v := range common.BuilderAliases {
// Check if this is one of the known aliases.
if v(entry.ExtraData) {
updated = true
topBuilderEntry, isKnown := buildersMap[k]
if isKnown {
topBuilderEntry.NumBlocks += entry.NumBlocks
topBuilderEntry.Aliases = append(topBuilderEntry.Aliases, entry.ExtraData)
} else {
buildersMap[k] = &database.TopBuilderEntry{
ExtraData: k,
NumBlocks: entry.NumBlocks,
Aliases: []string{entry.ExtraData},
}
}
break
}
}
if !updated {
buildersMap[entry.ExtraData] = entry
}
}
// Prepare top builders by summary stats
resp := []*database.TopBuilderEntry{}
for _, entry := range buildersMap {
p := float64(entry.NumBlocks) / float64(buildersNumPayloads) * 100
entry.Percent = fmt.Sprintf("%.2f", p)
resp = append(resp, entry)
}
sort.Slice(resp, func(i, j int) bool {
return resp[i].NumBlocks > resp[j].NumBlocks
})
return resp
}
func consolidateBuilderProfitEntries(entries []*database.BuilderProfitEntry) []*database.BuilderProfitEntry {
buildersMap := make(map[string]*database.BuilderProfitEntry)
buildersNumPayloads := uint64(0)
for _, entry := range entries {
buildersNumPayloads += entry.NumBlocks
updated := false
for k, v := range common.BuilderAliases {
// Check if this is one of the known aliases.
if v(entry.ExtraData) {
updated = true
entryConsolidated, isKnown := buildersMap[k]
if isKnown {
entryConsolidated.Aliases = append(entryConsolidated.Aliases, entry.ExtraData)
entryConsolidated.NumBlocks += entry.NumBlocks
entryConsolidated.NumBlocksProfit += entry.NumBlocksProfit
entryConsolidated.NumBlocksSubsidised += entry.NumBlocksSubsidised
entryConsolidated.ProfitTotal = addFloatStrings(entryConsolidated.ProfitTotal, entry.ProfitTotal, 4)
entryConsolidated.SubsidiesTotal = addFloatStrings(entryConsolidated.SubsidiesTotal, entry.SubsidiesTotal, 4)
entryConsolidated.ProfitPerBlockAvg = divFloatStrings(entryConsolidated.ProfitTotal, fmt.Sprint(entryConsolidated.NumBlocks), 4)
} else {
buildersMap[k] = &database.BuilderProfitEntry{
ExtraData: k,
NumBlocks: entry.NumBlocks,
NumBlocksProfit: entry.NumBlocksProfit,
NumBlocksSubsidised: entry.NumBlocksSubsidised,
ProfitTotal: entry.ProfitTotal,
SubsidiesTotal: entry.SubsidiesTotal,
ProfitPerBlockAvg: entry.ProfitPerBlockAvg,
Aliases: []string{entry.ExtraData},
}
}
break
}
}
if !updated {
buildersMap[entry.ExtraData] = entry
}
}
resp := []*database.BuilderProfitEntry{}
for _, entry := range buildersMap {
resp = append(resp, entry)
}
sort.Slice(resp, func(i, j int) bool {
return strToBigFloat(resp[i].ProfitTotal).Cmp(strToBigFloat(resp[j].ProfitTotal)) > 0
})
return resp
}
func prepareRelaysEntries(relays []*database.TopRelayEntry) []*database.TopRelayEntry {
numPayloads := uint64(0)
resp := []*database.TopRelayEntry{}
for _, entry := range relays {
if entry.NumPayloads == 0 {
continue
}
resp = append(resp, entry)
numPayloads += entry.NumPayloads
}
for i, entry := range resp {
p := float64(entry.NumPayloads) / float64(numPayloads) * 100
resp[i].Percent = fmt.Sprintf("%.2f", p)
}
return resp
}