MCPcopy Index your code
hub / github.com/CovenantSQL/CovenantSQL / nonceGen

Function nonceGen

cmd/cql/internal/idminer.go:128–223  ·  view source on GitHub ↗
(publicKey *asymmetric.PublicKey)

Source from the content-addressed store, hash-verified

126}
127
128func 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

Callers 2

runIDMinerFunction · 0.85
runGenerateFunction · 0.85

Calls 13

IsIDPubNonceValidFunction · 0.92
InfofMethod · 0.80
DebugfMethod · 0.80
IncMethod · 0.80
WaitMethod · 0.80
FatalMethod · 0.80
WithFieldsMethod · 0.80
SerializeMethod · 0.45
AddMethod · 0.45
DifficultyMethod · 0.45
StopMethod · 0.45
PrintfMethod · 0.45

Tested by

no test coverage detected