forked from sammy007/ether-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
37 lines (30 loc) · 918 Bytes
/
util.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
package util
import (
"math/big"
"math/rand"
"strconv"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
)
var pow256 = math.BigPow(2, 256)
func Random() string {
min := int64(100000000000000)
max := int64(999999999999999)
n := rand.Int63n(max-min+1) + min
return strconv.FormatInt(n, 10)
}
func MakeTimestamp() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}
func MakeTargetHex(minerDifficulty float64) string {
minerAdjustedDifficulty := int64(minerDifficulty * 1000000 * 100)
difficulty := big.NewInt(minerAdjustedDifficulty)
diff1 := new(big.Int).Div(pow256, difficulty)
return string(hexutil.Encode(diff1.Bytes()))
}
func TargetHexToDiff(targetHex string) *big.Int {
targetBytes := common.FromHex(targetHex)
return new(big.Int).Div(pow256, new(big.Int).SetBytes(targetBytes))
}