mineTask actually executes a mining task
(t *taskItem)
| 626 | |
| 627 | // mineTask actually executes a mining task |
| 628 | func (w *worker) mineTask(t *taskItem) (bool, error) { |
| 629 | startTime := time.Now() |
| 630 | nonce := t.nonceStart |
| 631 | w.lg.Debug("Mining task started", "shard", t.shardIdx, "thread", t.thread, "block", t.blockNumber, "nonces", fmt.Sprintf("%d~%d", t.nonceStart, t.nonceEnd)) |
| 632 | for w.isRunning() { |
| 633 | // always use new randao to mine for each slot |
| 634 | if time.Since(startTime).Seconds() > float64(w.config.Slot) { |
| 635 | if t.thread == 0 { |
| 636 | nonceTriedTotal := (nonce - t.nonceStart) * w.config.ThreadsPerShard |
| 637 | w.lg.Warn("Mining tasks timed out", "shard", t.shardIdx, "block", t.blockNumber, |
| 638 | "noncesTried", fmt.Sprintf("%d(%.1f%%)", nonceTriedTotal, float64(nonceTriedTotal*100)/float64(w.config.NonceLimit)), |
| 639 | ) |
| 640 | miningState := w.miningStates[t.shardIdx] |
| 641 | miningState.SamplingTime = uint64(time.Since(startTime).Milliseconds()) |
| 642 | miningState.MiningPower = nonceTriedTotal * 10000 / w.config.NonceLimit |
| 643 | } |
| 644 | w.lg.Debug("Mining task timed out", "shard", t.shardIdx, "thread", t.thread, "block", t.blockNumber, "noncesTried", nonce-t.nonceStart) |
| 645 | break |
| 646 | } |
| 647 | if nonce >= t.nonceEnd { |
| 648 | samplingTime := fmt.Sprintf("%.1fs", time.Since(startTime).Seconds()) |
| 649 | if t.thread == 0 { |
| 650 | w.lg.Info("Sampling done with all nonces", |
| 651 | "samplingTime", samplingTime, "shard", t.shardIdx, "block", t.blockNumber) |
| 652 | miningState := w.miningStates[t.shardIdx] |
| 653 | miningState.SamplingTime = uint64(time.Since(startTime).Milliseconds()) |
| 654 | miningState.MiningPower = 10000 |
| 655 | } |
| 656 | w.lg.Debug("Sampling done with all nonces", |
| 657 | "samplingTime", samplingTime, "shard", t.shardIdx, "block", t.blockNumber, "thread", t.thread, "nonceEnd", nonce) |
| 658 | break |
| 659 | } |
| 660 | hash0 := initHash(t.miner, t.mixHash, nonce) |
| 661 | hash1, sampleIdxs, err := w.computeHash(t.task.shardIdx, hash0) |
| 662 | if err != nil { |
| 663 | w.lg.Error("Calculate hash error", "shard", t.shardIdx, "thread", t.thread, "block", t.blockNumber, "err", err.Error()) |
| 664 | return false, err |
| 665 | } |
| 666 | if t.requiredDiff.Cmp(new(big.Int).SetBytes(hash1.Bytes())) >= 0 { |
| 667 | w.lg.Info("Calculated a valid hash", "shard", t.shardIdx, "block", t.blockNumber, "timestamp", t.mineTime, "randao", t.mixHash, "nonce", nonce, "hash0", hash0, "hash1", hash1, "sampleIdxs", sampleIdxs) |
| 668 | dataSet, kvIdxs, sampleIdxsInKv, encodingKeys, encodedSamples, err := w.getMiningData(t.task, sampleIdxs) |
| 669 | if err != nil { |
| 670 | w.lg.Error("Get sample data failed", "kvIdxs", kvIdxs, "sampleIdxsInKv", sampleIdxsInKv, "err", err.Error()) |
| 671 | return false, err |
| 672 | } |
| 673 | w.lg.Info("Got sample data", "shard", t.shardIdx, "block", t.blockNumber, "encodedSamples", encodedSamples) |
| 674 | masks, decodeProof, inclusiveProofs, err := w.prover.GetStorageProof(dataSet, encodingKeys, sampleIdxsInKv) |
| 675 | if err != nil { |
| 676 | w.lg.Error("Get storage proof error", "kvIdx", kvIdxs, "sampleIdxsInKv", sampleIdxsInKv, "error", err.Error()) |
| 677 | return false, fmt.Errorf("get proof err: %v", err) |
| 678 | } |
| 679 | w.lg.Info("Got storage proof", "shard", t.shardIdx, "block", t.blockNumber, "kvIdx", kvIdxs, "sampleIdxsInKv", sampleIdxsInKv) |
| 680 | newResult := &result{ |
| 681 | blockNumber: t.blockNumber, |
| 682 | startShardId: t.shardIdx, |
| 683 | miner: t.miner, |
| 684 | nonce: nonce, |
| 685 | encodedData: encodedSamples, |
no test coverage detected