resultLoop is a standalone goroutine to submit mining result to L1 contract.
()
| 412 | |
| 413 | // resultLoop is a standalone goroutine to submit mining result to L1 contract. |
| 414 | func (w *worker) resultLoop() { |
| 415 | defer w.wg.Done() |
| 416 | errorCache := make([]miningError, 0) |
| 417 | ticker := time.NewTicker(1 * time.Minute) |
| 418 | defer ticker.Stop() |
| 419 | saveStatesTicker := time.NewTicker(5 * time.Minute) |
| 420 | defer saveStatesTicker.Stop() |
| 421 | for { |
| 422 | select { |
| 423 | case <-w.resultCh: |
| 424 | result := w.getResult() |
| 425 | if result == nil { |
| 426 | continue |
| 427 | } |
| 428 | w.lg.Info("Mining result loop get result", "shard", result.startShardId, "block", result.blockNumber, "nonce", result.nonce) |
| 429 | txHash, err := w.l1API.SubmitMinedResult( |
| 430 | context.Background(), |
| 431 | w.storageMgr.ContractAddress(), |
| 432 | *result, |
| 433 | w.config, |
| 434 | ) |
| 435 | if s, ok := w.submissionStates[result.startShardId]; ok { |
| 436 | if err != nil { |
| 437 | var dropErr errDropped |
| 438 | if errors.As(err, &dropErr) { |
| 439 | s.Dropped++ |
| 440 | w.lg.Warn("Mining transaction dropped", |
| 441 | "shard", result.startShardId, |
| 442 | "block", result.blockNumber, |
| 443 | "reason", dropErr.reason) |
| 444 | } else { |
| 445 | s.Failed++ |
| 446 | errorCache = append(errorCache, miningError{result.startShardId, result.blockNumber, err}) |
| 447 | var diff *big.Int |
| 448 | if strings.Contains(err.Error(), "StorageContract_DifficultyNotMet") { |
| 449 | info, err := w.l1API.GetMiningInfo( |
| 450 | context.Background(), |
| 451 | w.storageMgr.ContractAddress(), |
| 452 | result.startShardId, |
| 453 | ) |
| 454 | if err != nil { |
| 455 | w.lg.Warn("Failed to get es mining info", "error", err.Error()) |
| 456 | } else { |
| 457 | diff = info.Difficulty |
| 458 | } |
| 459 | } |
| 460 | if diff != nil { |
| 461 | w.lg.Error("Failed to submit mined result", "shard", result.startShardId, "block", result.blockNumber, "difficulty", diff, "error", err.Error()) |
| 462 | } else { |
| 463 | w.lg.Error("Failed to submit mined result", "shard", result.startShardId, "block", result.blockNumber, "error", err.Error()) |
| 464 | } |
| 465 | } |
| 466 | } else { |
| 467 | s.Submitted++ |
| 468 | s.LastSubmittedTime = time.Now().UnixMilli() |
| 469 | } |
| 470 | } |
| 471 | w.reportMiningResult(result, txHash, err) |
no test coverage detected