github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/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/ethereum/go-ethereum/common" 34 "github.com/ethereum/go-ethereum/common/hexutil" 35 "github.com/ethereum/go-ethereum/consensus" 36 "github.com/ethereum/go-ethereum/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(ctx context.Context, 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(ctx, 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 121 if err := ethash.Seal(ctx, chain, block, results, stop); err != nil { 122 ethash.config.Log.Error("Failed to restart sealing after update", "err", err) 123 } 124 } 125 // Wait for all miners to terminate and return the block 126 pend.Wait() 127 }() 128 return nil 129 } 130 131 // mine is the actual proof-of-work miner that searches for a nonce starting from 132 // seed that results in correct final block difficulty. 133 func (ethash *Ethash) mine(block *types.Block, id int, seed uint64, abort chan struct{}, found chan *types.Block) { 134 // Extract some data from the header 135 var ( 136 header = block.Header() 137 hash = ethash.SealHash(header).Bytes() 138 target = new(big.Int).Div(two256, header.Difficulty) 139 number = header.Number.Uint64() 140 dataset = ethash.dataset(number, false) 141 ) 142 // Start generating random nonces until we abort or find a good one 143 var ( 144 attempts = int64(0) 145 nonce = seed 146 powBuffer = new(big.Int) 147 ) 148 logger := ethash.config.Log.New("miner", id) 149 logger.Trace("Started ethash search for new nonces", "seed", seed) 150 search: 151 for { 152 select { 153 case <-abort: 154 // Mining terminated, update stats and abort 155 logger.Trace("Ethash nonce search aborted", "attempts", nonce-seed) 156 ethash.hashrate.Mark(attempts) 157 break search 158 159 default: 160 // We don't have to update hash rate on every nonce, so update after after 2^X nonces 161 attempts++ 162 if (attempts % (1 << 15)) == 0 { 163 ethash.hashrate.Mark(attempts) 164 attempts = 0 165 } 166 // Compute the PoW value of this nonce 167 digest, result := hashimotoFull(dataset.dataset, hash, nonce) 168 if powBuffer.SetBytes(result).Cmp(target) <= 0 { 169 // Correct nonce found, create a new header with it 170 header = types.CopyHeader(header) 171 header.Nonce = types.EncodeNonce(nonce) 172 header.MixDigest = common.BytesToHash(digest) 173 174 // Seal and return a block (if still needed) 175 select { 176 case found <- block.WithSeal(header): 177 logger.Trace("Ethash nonce found and reported", "attempts", nonce-seed, "nonce", nonce) 178 case <-abort: 179 logger.Trace("Ethash nonce found but discarded", "attempts", nonce-seed, "nonce", nonce) 180 } 181 break search 182 } 183 nonce++ 184 } 185 } 186 // Datasets are unmapped in a finalizer. Ensure that the dataset stays live 187 // during sealing so it's not unmapped while being read. 188 runtime.KeepAlive(dataset) 189 } 190 191 // This is the timeout for HTTP requests to notify external miners. 192 const remoteSealerTimeout = 1 * time.Second 193 194 type remoteSealer struct { 195 works map[common.Hash]*types.Block 196 rates map[common.Hash]hashrate 197 currentBlock *types.Block 198 currentWork [4]string 199 notifyCtx context.Context 200 cancelNotify context.CancelFunc // cancels all notification requests 201 reqWG sync.WaitGroup // tracks notification request goroutines 202 203 ethash *Ethash 204 noverify bool 205 notifyURLs []string 206 results chan<- *types.Block 207 workCh chan *sealTask // Notification channel to push new work and relative result channel to remote sealer 208 fetchWorkCh chan *sealWork // Channel used for remote sealer to fetch mining work 209 submitWorkCh chan *mineResult // Channel used for remote sealer to submit their mining result 210 fetchRateCh chan chan uint64 // Channel used to gather submitted hash rate for local or remote sealer. 211 submitRateCh chan *hashrate // Channel used for remote sealer to submit their mining hashrate 212 requestExit chan struct{} 213 exitCh chan struct{} 214 } 215 216 // sealTask wraps a seal block with relative result channel for remote sealer thread. 217 type sealTask struct { 218 block *types.Block 219 results chan<- *types.Block 220 } 221 222 // mineResult wraps the pow solution parameters for the specified block. 223 type mineResult struct { 224 nonce types.BlockNonce 225 mixDigest common.Hash 226 hash common.Hash 227 228 errc chan error 229 } 230 231 // hashrate wraps the hash rate submitted by the remote sealer. 232 type hashrate struct { 233 id common.Hash 234 ping time.Time 235 rate uint64 236 237 done chan struct{} 238 } 239 240 // sealWork wraps a seal work package for remote sealer. 241 type sealWork struct { 242 errc chan error 243 res chan [4]string 244 } 245 246 func startRemoteSealer(ethash *Ethash, urls []string, noverify bool) *remoteSealer { 247 ctx, cancel := context.WithCancel(context.Background()) 248 s := &remoteSealer{ 249 ethash: ethash, 250 noverify: noverify, 251 notifyURLs: urls, 252 notifyCtx: ctx, 253 cancelNotify: cancel, 254 works: make(map[common.Hash]*types.Block), 255 rates: make(map[common.Hash]hashrate), 256 workCh: make(chan *sealTask), 257 fetchWorkCh: make(chan *sealWork), 258 submitWorkCh: make(chan *mineResult), 259 fetchRateCh: make(chan chan uint64), 260 submitRateCh: make(chan *hashrate), 261 requestExit: make(chan struct{}), 262 exitCh: make(chan struct{}), 263 } 264 go s.loop() 265 return s 266 } 267 268 func (s *remoteSealer) loop() { 269 defer func() { 270 s.ethash.config.Log.Trace("Ethash remote sealer is exiting") 271 s.cancelNotify() 272 s.reqWG.Wait() 273 close(s.exitCh) 274 }() 275 276 ticker := time.NewTicker(5 * time.Second) 277 defer ticker.Stop() 278 279 for { 280 select { 281 case work := <-s.workCh: 282 // Update current work with new received block. 283 // Note same work can be past twice, happens when changing CPU threads. 284 s.results = work.results 285 s.makeWork(work.block) 286 s.notifyWork() 287 288 case work := <-s.fetchWorkCh: 289 // Return current mining work to remote miner. 290 if s.currentBlock == nil { 291 work.errc <- errNoMiningWork 292 } else { 293 work.res <- s.currentWork 294 } 295 296 case result := <-s.submitWorkCh: 297 // Verify submitted PoW solution based on maintained mining blocks. 298 if s.submitWork(result.nonce, result.mixDigest, result.hash) { 299 result.errc <- nil 300 } else { 301 result.errc <- errInvalidSealResult 302 } 303 304 case result := <-s.submitRateCh: 305 // Trace remote sealer's hash rate by submitted value. 306 s.rates[result.id] = hashrate{rate: result.rate, ping: time.Now()} 307 close(result.done) 308 309 case req := <-s.fetchRateCh: 310 // Gather all hash rate submitted by remote sealer. 311 var total uint64 312 for _, rate := range s.rates { 313 // this could overflow 314 total += rate.rate 315 } 316 req <- total 317 318 case <-ticker.C: 319 // Clear stale submitted hash rate. 320 for id, rate := range s.rates { 321 if time.Since(rate.ping) > 10*time.Second { 322 delete(s.rates, id) 323 } 324 } 325 // Clear stale pending blocks 326 if s.currentBlock != nil { 327 for hash, block := range s.works { 328 if block.NumberU64()+staleThreshold <= s.currentBlock.NumberU64() { 329 delete(s.works, hash) 330 } 331 } 332 } 333 334 case <-s.requestExit: 335 return 336 } 337 } 338 } 339 340 // makeWork creates a work package for external miner. 341 // 342 // The work package consists of 3 strings: 343 // result[0], 32 bytes hex encoded current block header pow-hash 344 // result[1], 32 bytes hex encoded seed hash used for DAG 345 // result[2], 32 bytes hex encoded boundary condition ("target"), 2^256/difficulty 346 // result[3], hex encoded block number 347 func (s *remoteSealer) makeWork(block *types.Block) { 348 hash := s.ethash.SealHash(block.Header()) 349 s.currentWork[0] = hash.Hex() 350 s.currentWork[1] = common.BytesToHash(SeedHash(block.NumberU64())).Hex() 351 s.currentWork[2] = common.BytesToHash(new(big.Int).Div(two256, block.Difficulty()).Bytes()).Hex() 352 s.currentWork[3] = hexutil.EncodeBig(block.Number()) 353 354 // Trace the seal work fetched by remote sealer. 355 s.currentBlock = block 356 s.works[hash] = block 357 } 358 359 // notifyWork notifies all the specified mining endpoints of the availability of 360 // new work to be processed. 361 func (s *remoteSealer) notifyWork() { 362 work := s.currentWork 363 364 // Encode the JSON payload of the notification. When NotifyFull is set, 365 // this is the complete block header, otherwise it is a JSON array. 366 var blob []byte 367 if s.ethash.config.NotifyFull { 368 blob, _ = json.Marshal(s.currentBlock.Header()) 369 } else { 370 blob, _ = json.Marshal(work) 371 } 372 373 s.reqWG.Add(len(s.notifyURLs)) 374 for _, url := range s.notifyURLs { 375 go s.sendNotification(s.notifyCtx, url, blob, work) 376 } 377 } 378 379 func (s *remoteSealer) sendNotification(ctx context.Context, url string, json []byte, work [4]string) { 380 defer s.reqWG.Done() 381 382 req, err := http.NewRequest("POST", url, bytes.NewReader(json)) 383 if err != nil { 384 s.ethash.config.Log.Warn("Can't create remote miner notification", "err", err) 385 return 386 } 387 ctx, cancel := context.WithTimeout(ctx, remoteSealerTimeout) 388 defer cancel() 389 req = req.WithContext(ctx) 390 req.Header.Set("Content-Type", "application/json") 391 392 resp, err := http.DefaultClient.Do(req) 393 if err != nil { 394 s.ethash.config.Log.Warn("Failed to notify remote miner", "err", err) 395 } else { 396 s.ethash.config.Log.Trace("Notified remote miner", "miner", url, "hash", work[0], "target", work[2]) 397 resp.Body.Close() 398 } 399 } 400 401 // submitWork verifies the submitted pow solution, returning 402 // whether the solution was accepted or not (not can be both a bad pow as well as 403 // any other error, like no pending work or stale mining result). 404 func (s *remoteSealer) submitWork(nonce types.BlockNonce, mixDigest common.Hash, sealhash common.Hash) bool { 405 if s.currentBlock == nil { 406 s.ethash.config.Log.Error("Pending work without block", "sealhash", sealhash) 407 return false 408 } 409 // Make sure the work submitted is present 410 block := s.works[sealhash] 411 if block == nil { 412 s.ethash.config.Log.Warn("Work submitted but none pending", "sealhash", sealhash, "curnumber", s.currentBlock.NumberU64()) 413 return false 414 } 415 // Verify the correctness of submitted result. 416 header := block.Header() 417 header.Nonce = nonce 418 header.MixDigest = mixDigest 419 420 start := time.Now() 421 if !s.noverify { 422 if err := s.ethash.verifySeal(nil, header, true); err != nil { 423 s.ethash.config.Log.Warn("Invalid proof-of-work submitted", "sealhash", sealhash, "elapsed", common.PrettyDuration(time.Since(start)), "err", err) 424 return false 425 } 426 } 427 // Make sure the result channel is assigned. 428 if s.results == nil { 429 s.ethash.config.Log.Warn("Ethash result channel is empty, submitted mining result is rejected") 430 return false 431 } 432 s.ethash.config.Log.Trace("Verified correct proof-of-work", "sealhash", sealhash, "elapsed", common.PrettyDuration(time.Since(start))) 433 434 // Solutions seems to be valid, return to the miner and notify acceptance. 435 solution := block.WithSeal(header) 436 437 // The submitted solution is within the scope of acceptance. 438 if solution.NumberU64()+staleThreshold > s.currentBlock.NumberU64() { 439 select { 440 case s.results <- solution: 441 s.ethash.config.Log.Debug("Work submitted is acceptable", "number", solution.NumberU64(), "sealhash", sealhash, "hash", solution.Hash()) 442 return true 443 default: 444 s.ethash.config.Log.Warn("Sealing result is not read by miner", "mode", "remote", "sealhash", sealhash) 445 return false 446 } 447 } 448 // The submitted block is too old to accept, drop it. 449 s.ethash.config.Log.Warn("Work submitted is too old", "number", solution.NumberU64(), "sealhash", sealhash, "hash", solution.Hash()) 450 return false 451 }