github.com/DxChainNetwork/dxc@v0.8.1-0.20220824085222-1162e304b6e7/consensus/ethash/sealer.go (about) 1 // Copyright 2017 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package ethash 18 19 import ( 20 "bytes" 21 "context" 22 crand "crypto/rand" 23 "encoding/json" 24 "errors" 25 "math" 26 "math/big" 27 "math/rand" 28 "net/http" 29 "runtime" 30 "sync" 31 "time" 32 33 "github.com/DxChainNetwork/dxc/common" 34 "github.com/DxChainNetwork/dxc/common/hexutil" 35 "github.com/DxChainNetwork/dxc/consensus" 36 "github.com/DxChainNetwork/dxc/core/types" 37 ) 38 39 const ( 40 // staleThreshold is the maximum depth of the acceptable stale but valid ethash solution. 41 staleThreshold = 7 42 ) 43 44 var ( 45 errNoMiningWork = errors.New("no mining work available yet") 46 errInvalidSealResult = errors.New("invalid or stale proof-of-work solution") 47 ) 48 49 // Seal implements consensus.Engine, attempting to find a nonce that satisfies 50 // the block's difficulty requirements. 51 func (ethash *Ethash) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error { 52 // If we're running a fake PoW, simply return a 0 nonce immediately 53 if ethash.config.PowMode == ModeFake || ethash.config.PowMode == ModeFullFake { 54 header := block.Header() 55 header.Nonce, header.MixDigest = types.BlockNonce{}, common.Hash{} 56 select { 57 case results <- block.WithSeal(header): 58 default: 59 ethash.config.Log.Warn("Sealing result is not read by miner", "mode", "fake", "sealhash", ethash.SealHash(block.Header())) 60 } 61 return nil 62 } 63 // If we're running a shared PoW, delegate sealing to it 64 if ethash.shared != nil { 65 return ethash.shared.Seal(chain, block, results, stop) 66 } 67 // Create a runner and the multiple search threads it directs 68 abort := make(chan struct{}) 69 70 ethash.lock.Lock() 71 threads := ethash.threads 72 if ethash.rand == nil { 73 seed, err := crand.Int(crand.Reader, big.NewInt(math.MaxInt64)) 74 if err != nil { 75 ethash.lock.Unlock() 76 return err 77 } 78 ethash.rand = rand.New(rand.NewSource(seed.Int64())) 79 } 80 ethash.lock.Unlock() 81 if threads == 0 { 82 threads = runtime.NumCPU() 83 } 84 if threads < 0 { 85 threads = 0 // Allows disabling local mining without extra logic around local/remote 86 } 87 // Push new work to remote sealer 88 if ethash.remote != nil { 89 ethash.remote.workCh <- &sealTask{block: block, results: results} 90 } 91 var ( 92 pend sync.WaitGroup 93 locals = make(chan *types.Block) 94 ) 95 for i := 0; i < threads; i++ { 96 pend.Add(1) 97 go func(id int, nonce uint64) { 98 defer pend.Done() 99 ethash.mine(block, id, nonce, abort, locals) 100 }(i, uint64(ethash.rand.Int63())) 101 } 102 // Wait until sealing is terminated or a nonce is found 103 go func() { 104 var result *types.Block 105 select { 106 case <-stop: 107 // Outside abort, stop all miner threads 108 close(abort) 109 case result = <-locals: 110 // One of the threads found a block, abort all others 111 select { 112 case results <- result: 113 default: 114 ethash.config.Log.Warn("Sealing result is not read by miner", "mode", "local", "sealhash", ethash.SealHash(block.Header())) 115 } 116 close(abort) 117 case <-ethash.update: 118 // Thread count was changed on user request, restart 119 close(abort) 120 if err := ethash.Seal(chain, block, results, stop); err != nil { 121 ethash.config.Log.Error("Failed to restart sealing after update", "err", err) 122 } 123 } 124 // Wait for all miners to terminate and return the block 125 pend.Wait() 126 }() 127 return nil 128 } 129 130 // mine is the actual proof-of-work miner that searches for a nonce starting from 131 // seed that results in correct final block difficulty. 132 func (ethash *Ethash) mine(block *types.Block, id int, seed uint64, abort chan struct{}, found chan *types.Block) { 133 // Extract some data from the header 134 var ( 135 header = block.Header() 136 hash = ethash.SealHash(header).Bytes() 137 target = new(big.Int).Div(two256, header.Difficulty) 138 number = header.Number.Uint64() 139 dataset = ethash.dataset(number, false) 140 ) 141 // Start generating random nonces until we abort or find a good one 142 var ( 143 attempts = int64(0) 144 nonce = seed 145 powBuffer = new(big.Int) 146 ) 147 logger := ethash.config.Log.New("miner", id) 148 logger.Trace("Started ethash search for new nonces", "seed", seed) 149 search: 150 for { 151 select { 152 case <-abort: 153 // Mining terminated, update stats and abort 154 logger.Trace("Ethash nonce search aborted", "attempts", nonce-seed) 155 ethash.hashrate.Mark(attempts) 156 break search 157 158 default: 159 // We don't have to update hash rate on every nonce, so update after after 2^X nonces 160 attempts++ 161 if (attempts % (1 << 15)) == 0 { 162 ethash.hashrate.Mark(attempts) 163 attempts = 0 164 } 165 // Compute the PoW value of this nonce 166 digest, result := hashimotoFull(dataset.dataset, hash, nonce) 167 if powBuffer.SetBytes(result).Cmp(target) <= 0 { 168 // Correct nonce found, create a new header with it 169 header = types.CopyHeader(header) 170 header.Nonce = types.EncodeNonce(nonce) 171 header.MixDigest = common.BytesToHash(digest) 172 173 // Seal and return a block (if still needed) 174 select { 175 case found <- block.WithSeal(header): 176 logger.Trace("Ethash nonce found and reported", "attempts", nonce-seed, "nonce", nonce) 177 case <-abort: 178 logger.Trace("Ethash nonce found but discarded", "attempts", nonce-seed, "nonce", nonce) 179 } 180 break search 181 } 182 nonce++ 183 } 184 } 185 // Datasets are unmapped in a finalizer. Ensure that the dataset stays live 186 // during sealing so it's not unmapped while being read. 187 runtime.KeepAlive(dataset) 188 } 189 190 // This is the timeout for HTTP requests to notify external miners. 191 const remoteSealerTimeout = 1 * time.Second 192 193 type remoteSealer struct { 194 works map[common.Hash]*types.Block 195 rates map[common.Hash]hashrate 196 currentBlock *types.Block 197 currentWork [4]string 198 notifyCtx context.Context 199 cancelNotify context.CancelFunc // cancels all notification requests 200 reqWG sync.WaitGroup // tracks notification request goroutines 201 202 ethash *Ethash 203 noverify bool 204 notifyURLs []string 205 results chan<- *types.Block 206 workCh chan *sealTask // Notification channel to push new work and relative result channel to remote sealer 207 fetchWorkCh chan *sealWork // Channel used for remote sealer to fetch mining work 208 submitWorkCh chan *mineResult // Channel used for remote sealer to submit their mining result 209 fetchRateCh chan chan uint64 // Channel used to gather submitted hash rate for local or remote sealer. 210 submitRateCh chan *hashrate // Channel used for remote sealer to submit their mining hashrate 211 requestExit chan struct{} 212 exitCh chan struct{} 213 } 214 215 // sealTask wraps a seal block with relative result channel for remote sealer thread. 216 type sealTask struct { 217 block *types.Block 218 results chan<- *types.Block 219 } 220 221 // mineResult wraps the pow solution parameters for the specified block. 222 type mineResult struct { 223 nonce types.BlockNonce 224 mixDigest common.Hash 225 hash common.Hash 226 227 errc chan error 228 } 229 230 // hashrate wraps the hash rate submitted by the remote sealer. 231 type hashrate struct { 232 id common.Hash 233 ping time.Time 234 rate uint64 235 236 done chan struct{} 237 } 238 239 // sealWork wraps a seal work package for remote sealer. 240 type sealWork struct { 241 errc chan error 242 res chan [4]string 243 } 244 245 func startRemoteSealer(ethash *Ethash, urls []string, noverify bool) *remoteSealer { 246 ctx, cancel := context.WithCancel(context.Background()) 247 s := &remoteSealer{ 248 ethash: ethash, 249 noverify: noverify, 250 notifyURLs: urls, 251 notifyCtx: ctx, 252 cancelNotify: cancel, 253 works: make(map[common.Hash]*types.Block), 254 rates: make(map[common.Hash]hashrate), 255 workCh: make(chan *sealTask), 256 fetchWorkCh: make(chan *sealWork), 257 submitWorkCh: make(chan *mineResult), 258 fetchRateCh: make(chan chan uint64), 259 submitRateCh: make(chan *hashrate), 260 requestExit: make(chan struct{}), 261 exitCh: make(chan struct{}), 262 } 263 go s.loop() 264 return s 265 } 266 267 func (s *remoteSealer) loop() { 268 defer func() { 269 s.ethash.config.Log.Trace("Ethash remote sealer is exiting") 270 s.cancelNotify() 271 s.reqWG.Wait() 272 close(s.exitCh) 273 }() 274 275 ticker := time.NewTicker(5 * time.Second) 276 defer ticker.Stop() 277 278 for { 279 select { 280 case work := <-s.workCh: 281 // Update current work with new received block. 282 // Note same work can be past twice, happens when changing CPU threads. 283 s.results = work.results 284 s.makeWork(work.block) 285 s.notifyWork() 286 287 case work := <-s.fetchWorkCh: 288 // Return current mining work to remote miner. 289 if s.currentBlock == nil { 290 work.errc <- errNoMiningWork 291 } else { 292 work.res <- s.currentWork 293 } 294 295 case result := <-s.submitWorkCh: 296 // Verify submitted PoW solution based on maintained mining blocks. 297 if s.submitWork(result.nonce, result.mixDigest, result.hash) { 298 result.errc <- nil 299 } else { 300 result.errc <- errInvalidSealResult 301 } 302 303 case result := <-s.submitRateCh: 304 // Trace remote sealer's hash rate by submitted value. 305 s.rates[result.id] = hashrate{rate: result.rate, ping: time.Now()} 306 close(result.done) 307 308 case req := <-s.fetchRateCh: 309 // Gather all hash rate submitted by remote sealer. 310 var total uint64 311 for _, rate := range s.rates { 312 // this could overflow 313 total += rate.rate 314 } 315 req <- total 316 317 case <-ticker.C: 318 // Clear stale submitted hash rate. 319 for id, rate := range s.rates { 320 if time.Since(rate.ping) > 10*time.Second { 321 delete(s.rates, id) 322 } 323 } 324 // Clear stale pending blocks 325 if s.currentBlock != nil { 326 for hash, block := range s.works { 327 if block.NumberU64()+staleThreshold <= s.currentBlock.NumberU64() { 328 delete(s.works, hash) 329 } 330 } 331 } 332 333 case <-s.requestExit: 334 return 335 } 336 } 337 } 338 339 // makeWork creates a work package for external miner. 340 // 341 // The work package consists of 3 strings: 342 // result[0], 32 bytes hex encoded current block header pow-hash 343 // result[1], 32 bytes hex encoded seed hash used for DAG 344 // result[2], 32 bytes hex encoded boundary condition ("target"), 2^256/difficulty 345 // result[3], hex encoded block number 346 func (s *remoteSealer) makeWork(block *types.Block) { 347 hash := s.ethash.SealHash(block.Header()) 348 s.currentWork[0] = hash.Hex() 349 s.currentWork[1] = common.BytesToHash(SeedHash(block.NumberU64())).Hex() 350 s.currentWork[2] = common.BytesToHash(new(big.Int).Div(two256, block.Difficulty()).Bytes()).Hex() 351 s.currentWork[3] = hexutil.EncodeBig(block.Number()) 352 353 // Trace the seal work fetched by remote sealer. 354 s.currentBlock = block 355 s.works[hash] = block 356 } 357 358 // notifyWork notifies all the specified mining endpoints of the availability of 359 // new work to be processed. 360 func (s *remoteSealer) notifyWork() { 361 work := s.currentWork 362 363 // Encode the JSON payload of the notification. When NotifyFull is set, 364 // this is the complete block header, otherwise it is a JSON array. 365 var blob []byte 366 if s.ethash.config.NotifyFull { 367 blob, _ = json.Marshal(s.currentBlock.Header()) 368 } else { 369 blob, _ = json.Marshal(work) 370 } 371 372 s.reqWG.Add(len(s.notifyURLs)) 373 for _, url := range s.notifyURLs { 374 go s.sendNotification(s.notifyCtx, url, blob, work) 375 } 376 } 377 378 func (s *remoteSealer) sendNotification(ctx context.Context, url string, json []byte, work [4]string) { 379 defer s.reqWG.Done() 380 381 req, err := http.NewRequest("POST", url, bytes.NewReader(json)) 382 if err != nil { 383 s.ethash.config.Log.Warn("Can't create remote miner notification", "err", err) 384 return 385 } 386 ctx, cancel := context.WithTimeout(ctx, remoteSealerTimeout) 387 defer cancel() 388 req = req.WithContext(ctx) 389 req.Header.Set("Content-Type", "application/json") 390 391 resp, err := http.DefaultClient.Do(req) 392 if err != nil { 393 s.ethash.config.Log.Warn("Failed to notify remote miner", "err", err) 394 } else { 395 s.ethash.config.Log.Trace("Notified remote miner", "miner", url, "hash", work[0], "target", work[2]) 396 resp.Body.Close() 397 } 398 } 399 400 // submitWork verifies the submitted pow solution, returning 401 // whether the solution was accepted or not (not can be both a bad pow as well as 402 // any other error, like no pending work or stale mining result). 403 func (s *remoteSealer) submitWork(nonce types.BlockNonce, mixDigest common.Hash, sealhash common.Hash) bool { 404 if s.currentBlock == nil { 405 s.ethash.config.Log.Error("Pending work without block", "sealhash", sealhash) 406 return false 407 } 408 // Make sure the work submitted is present 409 block := s.works[sealhash] 410 if block == nil { 411 s.ethash.config.Log.Warn("Work submitted but none pending", "sealhash", sealhash, "curnumber", s.currentBlock.NumberU64()) 412 return false 413 } 414 // Verify the correctness of submitted result. 415 header := block.Header() 416 header.Nonce = nonce 417 header.MixDigest = mixDigest 418 419 start := time.Now() 420 if !s.noverify { 421 if err := s.ethash.verifySeal(nil, header, true); err != nil { 422 s.ethash.config.Log.Warn("Invalid proof-of-work submitted", "sealhash", sealhash, "elapsed", common.PrettyDuration(time.Since(start)), "err", err) 423 return false 424 } 425 } 426 // Make sure the result channel is assigned. 427 if s.results == nil { 428 s.ethash.config.Log.Warn("Ethash result channel is empty, submitted mining result is rejected") 429 return false 430 } 431 s.ethash.config.Log.Trace("Verified correct proof-of-work", "sealhash", sealhash, "elapsed", common.PrettyDuration(time.Since(start))) 432 433 // Solutions seems to be valid, return to the miner and notify acceptance. 434 solution := block.WithSeal(header) 435 436 // The submitted solution is within the scope of acceptance. 437 if solution.NumberU64()+staleThreshold > s.currentBlock.NumberU64() { 438 select { 439 case s.results <- solution: 440 s.ethash.config.Log.Debug("Work submitted is acceptable", "number", solution.NumberU64(), "sealhash", sealhash, "hash", solution.Hash()) 441 return true 442 default: 443 s.ethash.config.Log.Warn("Sealing result is not read by miner", "mode", "remote", "sealhash", sealhash) 444 return false 445 } 446 } 447 // The submitted block is too old to accept, drop it. 448 s.ethash.config.Log.Warn("Work submitted is too old", "number", solution.NumberU64(), "sealhash", sealhash, "hash", solution.Hash()) 449 return false 450 }