mine method
Mine data until hash <= target or maxAttempts reached.
shouldStop is checked every iteration and may be used to cancel
long-running mining from a caller (for example a UI or timeout).
Implementation
MineResult mine(
String data,
BigInt target, {
int maxAttempts = 1000000,
bool Function()? shouldStop,
}) {
if (target < BigInt.zero) throw ArgumentError('target must be >= 0');
BigInt bestHash = BigInt.from(1) << 255; // large sentinel
int bestNonce = 0;
for (var i = 0; i < maxAttempts; i++) {
if (shouldStop != null && shouldStop()) {
return MineResult(bestNonce, bestHash, i, success: false);
}
final nonce = _rand.nextInt(1 << 30);
final h = hashFn(data, nonce);
if (h < bestHash) {
bestHash = h;
bestNonce = nonce;
}
if (h <= target) return MineResult(nonce, h, i + 1, success: true);
}
return MineResult(bestNonce, bestHash, maxAttempts, success: false);
}