This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
/
common.go
177 lines (126 loc) · 3.52 KB
/
common.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
package data
import (
"errors"
"fmt"
"math/big"
"runtime"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/gammazero/workerpool"
)
// IsPresentInCurrentPool - Given tx hash, which was previously present in pending/ queued pool
// attempts to check whether it's present in current txpool content or not
//
// @note `txs` is current pending/ queued pool state received over
// RPC interface
func IsPresentInCurrentPool(txs map[string]map[string]*MemPoolTx, txHash common.Hash) bool {
wp := workerpool.New(runtime.NumCPU())
workCount := len(txs)
resultChan := make(chan bool, workCount)
stopChan := make(chan struct{})
var present bool
// @note ⭐️
//
// Don't copy value reference here, directly pass it during
// function invokation, while accessing value using field `k`
for k := range txs {
func(txs map[string]*MemPoolTx) {
wp.Submit(func() {
// Same as ⭐️
for k := range txs {
select {
case <-stopChan:
return
default:
if txs[k].Hash == txHash {
resultChan <- true
return
}
}
}
// If this worker couldnn't find anything of interest
resultChan <- false
})
}(txs[k])
}
// How many responses received from workers
var received int
for v := range resultChan {
if v {
present = true
// No other worker will send anything here
// which is exactly why we're fleeing
close(stopChan)
break
}
received++
if received >= workCount {
// We're done receiving all responses
// from all works we submitted
break
}
}
wp.Stop()
return present
}
// Removes prepended `0{x, X}` from hex string
func remove0x(num string) string {
return strings.Replace(strings.Replace(num, "0x", "", -1), "0X", "", -1)
}
// HexToDecimal - Converts hex encoded uint64 to decimal string
func HexToDecimal(num hexutil.Uint64) string {
_num := big.NewInt(0)
_num.SetString(remove0x(num.String()), 16)
return _num.String()
}
// BigHexToBigDecimal - Given a hex encoded big number, converts it to
// decimal big integer
func BigHexToBigDecimal(num *hexutil.Big) *big.Int {
_num := big.NewInt(0)
_num.SetString(remove0x(num.String()), 16)
return _num
}
// BigIntToBigFloat - Given big number, attempts to convert it to
// big floating point number
func BigIntToBigFloat(num *big.Int) (*big.Float, error) {
_res := big.NewFloat(0)
if _, ok := _res.SetString(num.String()); !ok {
return nil, errors.New("failed to convert big int to big float")
}
return _res, nil
}
// BigHexToBigFloat - Converts hex encoded big integer to big floating point number
//
// @note To be used for better precision flaoting point arithmetic
func BigHexToBigFloat(num *hexutil.Big) (*big.Float, error) {
return BigIntToBigFloat(BigHexToBigDecimal(num))
}
// HumanReadableGasPrice - Returns gas price paid for tx
// in Gwei unit
func HumanReadableGasPrice(num *hexutil.Big) string {
_num, err := BigHexToBigFloat(num)
if err != nil {
return "0 Gwei"
}
_den, err := BigIntToBigFloat(big.NewInt(1_000_000_000))
if err != nil {
return "0 Gwei"
}
_res := big.NewFloat(0)
_res.Quo(_num, _den)
return fmt.Sprintf("%s Gwei", _res.String())
}
// NumericGasPriceGwei - GasPrice in Gwei unit represented as
// floating point ( double precision )
func NumericGasPriceGwei(num *hexutil.Big) float64 {
_num, err := BigHexToBigFloat(num)
if err != nil {
return 0.0
}
_den := big.NewFloat(1_000_000_000)
_res := big.NewFloat(0)
_res.Quo(_num, _den)
gp, _ := _res.Float64()
return gp
}