(publicKey *asymmetric.PublicKey)
| 126 | } |
| 127 | |
| 128 | func nonceGen(publicKey *asymmetric.PublicKey) *mine.NonceInfo { |
| 129 | publicKeyBytes := publicKey.Serialize() |
| 130 | |
| 131 | cpuCount := runtime.NumCPU() |
| 132 | ConsoleLog.Infof("cpu: %#v\n", cpuCount) |
| 133 | stopCh := make(chan struct{}) |
| 134 | nonceCh := make(chan mine.NonceInfo, cpuCount) |
| 135 | progressCh := make(chan int, 100) |
| 136 | var wg sync.WaitGroup |
| 137 | |
| 138 | rand.Seed(time.Now().UnixNano()) |
| 139 | step := 256 / cpuCount |
| 140 | for i := 0; i < cpuCount; i++ { |
| 141 | wg.Add(1) |
| 142 | go func(i int) { |
| 143 | defer wg.Done() |
| 144 | startBit := i * step |
| 145 | position := startBit / 64 |
| 146 | shift := uint(startBit % 64) |
| 147 | ConsoleLog.Debugf("position: %#v, shift: %#v, i: %#v", position, shift, i) |
| 148 | var start mine.Uint256 |
| 149 | if position == 0 { |
| 150 | start = mine.Uint256{A: uint64(1<<shift) + uint64(rand.Uint32())} |
| 151 | } else if position == 1 { |
| 152 | start = mine.Uint256{B: uint64(1<<shift) + uint64(rand.Uint32())} |
| 153 | } else if position == 2 { |
| 154 | start = mine.Uint256{C: uint64(1<<shift) + uint64(rand.Uint32())} |
| 155 | } else if position == 3 { |
| 156 | start = mine.Uint256{D: uint64(1<<shift) + uint64(rand.Uint32())} |
| 157 | } |
| 158 | |
| 159 | for j := start; ; j.Inc() { |
| 160 | select { |
| 161 | case <-stopCh: |
| 162 | return |
| 163 | default: |
| 164 | currentHash := mine.HashBlock(publicKeyBytes, j) |
| 165 | currentDifficulty := currentHash.Difficulty() |
| 166 | progressCh <- currentDifficulty |
| 167 | if currentDifficulty >= difficulty { |
| 168 | nonce := mine.NonceInfo{ |
| 169 | Nonce: j, |
| 170 | Difficulty: currentDifficulty, |
| 171 | Hash: currentHash, |
| 172 | } |
| 173 | nonceCh <- nonce |
| 174 | return |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | }(i) |
| 179 | } |
| 180 | |
| 181 | wg.Add(1) |
| 182 | go func() { |
| 183 | defer wg.Done() |
| 184 | var count, current int |
| 185 |
no test coverage detected