forked from hacash/miner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_miningresult.go
213 lines (188 loc) · 5.62 KB
/
api_miningresult.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
package minerrelayservice
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/hacash/core/fields"
"github.com/hacash/miner/message"
"github.com/hacash/mint/difficulty"
"net/http"
)
// Query mining results
func (api *RelayService) queryMiningResult(r *http.Request, w http.ResponseWriter, bodybytes []byte) {
if api.ldb == nil {
ResponseErrorString(w, "store database not init.")
return
}
onlyWorth := CheckParamBool(r, "only_worth", false)
idxStart := CheckParamUint64(r, "idx_start", 0)
idxLimit := CheckParamUint64(r, "idx_limit", 0)
if idxLimit > 50 {
// Search by index, up to 50
idxLimit = 50
}
// Query key
var queryKeys = make([][]byte, 0)
var queryHeis = make([]uint64, 0)
var queryAddrs = make([]fields.Address, 0)
if idxLimit > 0 {
// Query by auto incrementing index
max := idxStart + idxLimit
for i := idxStart; i < max; i++ {
kob1 := fields.VarUint5(i)
idxkey, _ := kob1.Serialize()
k1 := []byte("mri" + string(idxkey))
if v1, e1 := api.ldb.Get(k1, nil); e1 == nil {
kk := []byte("mr" + string(v1))
queryKeys = append(queryKeys, kk) // append
hei := fields.BlockHeight(0)
hei.Parse(v1, 0)
queryHeis = append(queryHeis, uint64(hei)) // append
rwdaddr := fields.Address{}
rwdaddr.Parse(v1, 5)
queryAddrs = append(queryAddrs, rwdaddr) // append
}
}
} else {
// It can be queried through the block height and reward address
addrstr := CheckParamString(r, "reward_address", "")
rwdaddr, e1 := fields.CheckReadableAddress(addrstr)
if e1 != nil {
ResponseErrorString(w, "reward address format error")
return
}
blkheiStart := CheckParamUint64(r, "block_height_start", 0)
blkheiLimit := CheckParamUint64(r, "block_height_limit", 0)
if blkheiLimit > 50 {
// Search by index, up to 50
blkheiLimit = 50
}
if blkheiLimit == 0 {
data := ResponseCreateData("list", []int{})
ResponseData(w, data)
return
}
max := blkheiStart + blkheiLimit
for i := blkheiStart; i < max; i++ {
kob2 := fields.VarUint5(i)
heikey, _ := kob2.Serialize()
keybuf := bytes.NewBuffer(heikey)
keybuf.Write(*rwdaddr)
keybts := keybuf.Bytes()
k1 := []byte("mr" + string(keybts))
queryKeys = append(queryKeys, k1) // append
queryHeis = append(queryHeis, i) // append
queryAddrs = append(queryAddrs, *rwdaddr) // append
}
}
// Return array
retlist := make([]interface{}, 0)
// Return null
if len(queryKeys) == 0 {
//fmt.Println("len(queryKeys) == 0")
data := ResponseCreateData("list", retlist)
ResponseData(w, data)
return
}
// Query data
queryObjs := make([]*StoreItemUserMiningResult, 0)
for i, key := range queryKeys {
//fmt.Println(key)
if v1, e1 := api.ldb.Get(key, nil); e1 == nil {
var stoitem = &StoreItemUserMiningResult{}
if _, e2 := stoitem.Parse(v1, 0); e2 == nil {
stoitem.blockHeight = queryHeis[i]
stoitem.rewardAddress = queryAddrs[i]
queryObjs = append(queryObjs, stoitem)
}
}
}
// Return null
if len(queryObjs) == 0 {
//fmt.Println("len(queryObjs) == 0")
data := ResponseCreateData("list", retlist)
ResponseData(w, data)
return
}
// Parse the array and calculate hash worth
for _, obj := range queryObjs {
des := obj.Describe()
if onlyWorth {
delete(des, "head_nonce")
delete(des, "coinbase_nonce")
delete(des, "mint_success")
delete(des, "result_hash")
if idxLimit == 0 {
delete(des, "reward_address")
}
}
retlist = append(retlist, des)
}
// ok
data := ResponseCreateData("list", retlist)
ResponseData(w, data)
}
// Submit mining results
func (api *RelayService) submitMiningResult(r *http.Request, w http.ResponseWriter, bodybytes []byte) {
// Mining success
isMintSuccess := CheckParamBool(r, "mint_success", false)
// Record reward address
addrstr := CheckParamString(r, "reward_address", "")
rwdaddr, e1 := fields.CheckReadableAddress(addrstr)
if e1 != nil {
ResponseErrorString(w, "reward address format error")
return
}
blkhei := CheckParamUint64(r, "block_height", 0)
if blkhei == 0 {
ResponseErrorString(w, "block_height must give")
return
}
// seek
tarstuff := api.checkoutMiningStuff(blkhei)
if tarstuff == nil {
ResponseError(w, fmt.Errorf("not find mining stuff of block height %d", blkhei))
return
}
// head nonce
bts1 := CheckParamString(r, "head_nonce", "")
headNonce, ehn := hex.DecodeString(bts1)
if ehn != nil || len(headNonce) != 4 {
ResponseErrorString(w, "head_nonce format error")
return
}
// coinbase nonce
bts2 := CheckParamString(r, "coinbase_nonce", "")
coinbaseNonce, ecbn := hex.DecodeString(bts2)
if ecbn != nil || len(coinbaseNonce) != 32 {
ResponseErrorString(w, "coinbase_nonce format error")
return
}
// Return value
var retdata = map[string]interface{}{}
// Check whether the mining is successful
var isRealMintSuccess = false
newstuff, newhx := tarstuff.CalculateBlockHashByBothNonce(headNonce, coinbaseNonce, true)
if isMintSuccess {
// Report mining success message
// Judge whether the hash meets the requirements
newblock := newstuff.GetHeadMetaBlock()
if difficulty.CheckHashDifficultySatisfyByBlock(newhx, newblock) {
rptmsg := message.MsgReportMiningResult{
MintSuccessed: 1,
BlockHeight: fields.BlockHeight(blkhei),
HeadNonce: headNonce,
CoinbaseNonce: coinbaseNonce,
}
// send message
message.MsgSendToTcpConn(api.service_tcp, message.MinerWorkMsgTypeReportMiningResult, rptmsg.Serialize())
isRealMintSuccess = true
}
// 不满足难度要求,什么都不干,
}
// Statistical computing power
go api.saveMiningResultToStore(*rwdaddr, isRealMintSuccess, newstuff)
// ok
retdata["stat"] = "ok"
ResponseData(w, retdata)
}