-
Notifications
You must be signed in to change notification settings - Fork 104
/
StratumJobETH.go
136 lines (116 loc) · 3 KB
/
StratumJobETH.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
package main
import (
"encoding/hex"
"fmt"
"time"
"github.com/holiman/uint256"
)
type StratumJobETH struct {
JobID []byte
SeedHash []byte
IsClean bool
IsFakeJob bool
PoWHeader ETHPoWHeader
}
type JSONRPCJobETH struct {
ID interface{} `json:"id"`
Method string `json:"method"`
Params []interface{} `json:"params"`
Height uint64 `json:"height"`
}
type JSONRPC2JobETH struct {
ID interface{} `json:"id"`
JSONRPC string `json:"jsonrpc"`
Result []interface{} `json:"result"`
Height uint64 `json:"height"`
}
func NewStratumJobETH(json *JSONRPCLineETH, sessionID uint32) (job *StratumJobETH, err error) {
/*
Fields in order:
["params"]
[0] job id
[1] seed hash
[2] header hash
[3] is clean
["header"] block header hex
["basefee"] (optional) base fee for EIP-1559
*/
job = new(StratumJobETH)
if len(json.Header) < 1 {
err = fmt.Errorf("notify missing field header")
return
}
header, err := Hex2Bin(json.Header)
if err != nil {
err = fmt.Errorf("failed to decode header: %s", err.Error())
return
}
baseFee, err := Hex2Bin(json.BaseFee)
if err != nil {
err = fmt.Errorf("failed to decode base fee: %s", err.Error())
return
}
if len(json.Params) < 4 {
err = fmt.Errorf("notify missing fields, should be 4 fields but only %d", len(json.Params))
return
}
jobID, ok := json.Params[0].(string)
if !ok {
err = fmt.Errorf("job id is not a string")
return
}
job.JobID, err = Hex2Bin(jobID)
if err != nil {
err = fmt.Errorf("failed to decode job id: %s", err.Error())
return
}
BinReverse(job.JobID) // btcpool使用小端字节序
seedHash, ok := json.Params[1].(string)
if !ok {
err = fmt.Errorf("seed hash is not a string")
return
}
job.SeedHash, err = Hex2Bin(seedHash)
if err != nil {
err = fmt.Errorf("failed to decode seed hash: %s", err.Error())
return
}
header = append(header, 0, 0, 0, 0) // append extra nonce
header = append(header, baseFee...) // append base fee
err = job.PoWHeader.Decode(header)
if err != nil {
err = fmt.Errorf("failed to decode block header: %s", err.Error())
return
}
// succeeded
return
}
func (job *StratumJobETH) PoWHash(extraNonce uint32) string {
if job.IsFakeJob {
return hex.EncodeToString(job.JobID)
}
return hex.EncodeToString(job.PoWHeader.Hash(extraNonce).Bytes())
}
func IsFakeJobIDETH(id string) bool {
id = HexRemovePrefix(id)
return len(id) == len(FakeJobIDETHPrefix)+16 && id[:len(FakeJobIDETHPrefix)] == FakeJobIDETHPrefix
}
func (job *StratumJobETH) ToNewFakeJob() {
job.IsFakeJob = true
now := uint64(time.Now().Unix())
job.PoWHeader.SetTime(now)
// fake job id
job.JobID = FakeJobIDETHPrefixBin
job.JobID = append(job.JobID, Uint64ToBin(now)...)
}
func (job *StratumJobETH) Height() uint64 {
return job.PoWHeader.Number.Uint64()
}
func DiffToTargetETH(diff uint64) (target string) {
var result uint256.Int
result.SetUint64(diff)
result.Div(&EthereumPoolDiff1, &result)
bin := result.Bytes32()
target = hex.EncodeToString(bin[:])
return
}