Block: log warning if solve() runs a bit long

This commit is contained in:
Andreas Schildbach 2023-04-01 13:34:16 +02:00
parent 7879f24164
commit 472af481cc

View File

@ -23,6 +23,7 @@ import org.bitcoinj.base.Coin;
import org.bitcoinj.base.Sha256Hash;
import org.bitcoinj.base.VarInt;
import org.bitcoinj.base.internal.Buffers;
import org.bitcoinj.base.internal.Stopwatch;
import org.bitcoinj.base.internal.TimeUtils;
import org.bitcoinj.base.internal.ByteUtils;
import org.bitcoinj.base.internal.InternalUtils;
@ -445,6 +446,8 @@ public class Block extends Message {
*/
@VisibleForTesting
public void solve() {
Duration warningThreshold = Duration.ofSeconds(5);
Stopwatch watch = Stopwatch.start();
while (true) {
try {
// Is our proof of work valid yet?
@ -452,6 +455,11 @@ public class Block extends Message {
return;
// No, so increment the nonce and try again.
setNonce(getNonce() + 1);
if (watch.isRunning() && watch.elapsed().compareTo(warningThreshold) > 0) {
watch.stop();
log.warn("trying to solve block for longer than {} seconds", warningThreshold.getSeconds());
}
} catch (VerificationException e) {
throw new RuntimeException(e); // Cannot happen.
}