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