• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bernardladenthin / BitcoinAddressFinder / #353

21 Jul 2025 10:01PM UTC coverage: 71.342% (+1.2%) from 70.173%
#353

push

bernardladenthin
Implement ZeroMQ instead raw socket. Add and fix some tests.

43 of 47 new or added lines in 4 files covered. (91.49%)

4 existing lines in 2 files now uncovered.

1536 of 2153 relevant lines covered (71.34%)

0.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.89
/src/main/java/net/ladenthin/bitcoinaddressfinder/KeyProducerJavaZmq.java
1
// @formatter:off
2
/**
3
 * Copyright 2025 Bernard Ladenthin bernard.ladenthin@gmail.com
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *    http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
// @formatter:on
19
package net.ladenthin.bitcoinaddressfinder;
20

21

22
import net.ladenthin.bitcoinaddressfinder.configuration.CKeyProducerJavaZmq;
23
import org.zeromq.ZContext;
24
import org.zeromq.ZMQ;
25

26
import java.math.BigInteger;
27
import org.zeromq.SocketType;
28

29
public class KeyProducerJavaZmq extends KeyProducerJava<CKeyProducerJavaZmq> {
30

31
    private final KeyUtility keyUtility;
32
    private final ZContext context;
33
    private final ZMQ.Socket socket;
34

35
    private volatile boolean shouldStop = false;
1✔
36

37
    public KeyProducerJavaZmq(CKeyProducerJavaZmq config, KeyUtility keyUtility, BitHelper bitHelper) {
38
        super(config);
1✔
39
        this.keyUtility = keyUtility;
1✔
40
        this.context = new ZContext();
1✔
41
        this.socket = context.createSocket(SocketType.PULL);
1✔
42

43
        if (cKeyProducerJava.mode == CKeyProducerJavaZmq.Mode.BIND) {
1✔
44
            socket.bind(cKeyProducerJava.address);
1✔
45
        } else {
46
            socket.connect(cKeyProducerJava.address);
1✔
47
        }
48

49
        socket.setReceiveTimeOut(cKeyProducerJava.timeoutMillis);
1✔
50
    }
1✔
51

52
    @Override
53
    public BigInteger[] createSecrets(int overallWorkSize, boolean returnStartSecretOnly) throws NoMoreSecretsAvailableException {
54
        int count = returnStartSecretOnly ? 1 : overallWorkSize;
1✔
55
        BigInteger[] secrets = new BigInteger[count];
1✔
56

57
        for (int i = 0; i < count; i++) {
1✔
58
            if (shouldStop) {
1✔
NEW
59
                throw new NoMoreSecretsAvailableException("Interrupted before receiving key.");
×
60
            }
61

62
            try {
63
                byte[] msg = socket.recv();
1✔
64

65
                if (msg == null) {
1✔
66
                    throw new NoMoreSecretsAvailableException("Timeout while receiving key.");
1✔
67
                }
68

69
                if (msg.length != PublicKeyBytes.PRIVATE_KEY_MAX_NUM_BYTES) {
1✔
NEW
70
                    throw new NoMoreSecretsAvailableException("Received malformed key of length " + msg.length);
×
71
                }
72

73
                BigInteger key = keyUtility.bigIntegerFromUnsignedByteArray(msg);
1✔
74
                secrets[i] = key;
1✔
75

76
                if (cKeyProducerJava.logReceivedSecret) {
1✔
NEW
77
                    System.out.println("Received key: " + keyUtility.bigIntegerToFixedLengthHex(key));
×
78
                }
79
            } catch (org.zeromq.ZMQException e) {
1✔
80
                if (shouldStop || e.getErrorCode() == ZMQ.Error.ETERM.getCode()) {
1✔
81
                    throw new NoMoreSecretsAvailableException("Receive interrupted due to socket/context shutdown", e);
1✔
82
                }
NEW
83
                throw e; // rethrow other unexpected ZMQ errors
×
84
            }
1✔
85
        }
86

87
        return secrets;
1✔
88
    }
89

90
    @Override
91
    public void interrupt() {
92
        shouldStop = true;
1✔
93
        close();
1✔
94
    }
1✔
95

96
    public void close() {
97
        socket.close();
1✔
98
        context.close();
1✔
99
    }
1✔
100
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc