Fixed several problems and warnings

This commit is contained in:
armin 2018-04-28 21:27:40 +02:00
parent dfdeb5e52c
commit bda6330fbb
8 changed files with 51 additions and 57 deletions

View file

@ -24,8 +24,6 @@
#include "vendor/secp256k1/src/ecmult.h" #include "vendor/secp256k1/src/ecmult.h"
#include "vendor/secp256k1/src/eckey_impl.h" #include "vendor/secp256k1/src/eckey_impl.h"
static int secp256k1_eckey_pubkey_parse(secp256k1_ge_t *elem, const unsigned char *pub, int size);
#include "mmapf.h" #include "mmapf.h"
#undef ASSERT #undef ASSERT
@ -424,7 +422,7 @@ int secp256k1_scalar_add_b32(void * out, void * a, void *b) {
return 0; return 0;
} }
inline static void _priv_add(unsigned char *priv, unsigned char add, int p) { inline static void priv_add(unsigned char *priv, unsigned char add, int p) {
priv[p] += add; priv[p] += add;
if (priv[p] < add) { if (priv[p] < add) {
priv[--p] += 1; priv[--p] += 1;
@ -439,13 +437,13 @@ inline static void _priv_add(unsigned char *priv, unsigned char add, int p) {
} }
void priv_add_uint8(unsigned char *priv, unsigned char add) { void priv_add_uint8(unsigned char *priv, unsigned char add) {
_priv_add(priv, add, 31); priv_add(priv, add, 31);
} }
void priv_add_uint32(unsigned char *priv, unsigned int add) { void priv_add_uint32(unsigned char *priv, unsigned int add) {
int p = 31; int p = 31;
while (add) { while (add) {
_priv_add(priv, add & 255, p--); priv_add(priv, add & 255, p--);
add >>= 8; add >>= 8;
} }
} }

View file

@ -153,31 +153,32 @@ static const uint32_t KR[5] = {
0x00000000u /* Round 5: 0 */ 0x00000000u /* Round 5: 0 */
}; };
static inline void byteswap32(uint32_t *v) static inline void byteswap32(uint32_t *v) {
{ union {
union { uint32_t w; uint8_t b[4]; } x, y; uint32_t w;
uint8_t b[4];
} x, y;
x.w = *v; x.w = *v;
y.b[0] = x.b[3]; y.b[0] = x.b[3];
y.b[1] = x.b[2]; y.b[1] = x.b[2];
y.b[2] = x.b[1]; y.b[2] = x.b[1];
y.b[3] = x.b[0]; y.b[3] = x.b[0];
*v = y.w; *v = y.w;
/* Wipe temporary variables */ /* Wipe temporary variables */
x.w = y.w = 0; x.w = y.w = 0;
} }
static inline void byteswap_digest(uint32_t *p) static inline void byteswap_digest(uint32_t *p) { // @suppress("Unused static function")
{ unsigned int i;
unsigned int i;
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
byteswap32(p++); byteswap32(p++);
byteswap32(p++); byteswap32(p++);
byteswap32(p++); byteswap32(p++);
byteswap32(p++); byteswap32(p++);
} }
} }
/* The RIPEMD160 compression function. */ /* The RIPEMD160 compression function. */

View file

@ -59,9 +59,7 @@ void bytestr::set(const byte* fromBytes, int len) {
} }
} }
bool bytestr::isEmpty(void){ bool bytestr::isEmpty(void) { return (bytes.empty()); }
return bytes.empty();
}
const byte& bytestr::operator[](const int index) const { const byte& bytestr::operator[](const int index) const {
return (bytes[index]); return (bytes[index]);

View file

@ -42,7 +42,7 @@ public class ClientWorker implements Runnable {
try { try {
cmd = in.readLine(); cmd = in.readLine();
} catch (IOException e) { } catch (IOException e) {
log.error("Couldn't read command from communication channel"); log.error("Couldn't read command from communication channel", e);
closeCommChannel(); closeCommChannel();
return; return;
} }
@ -101,7 +101,13 @@ public class ClientWorker implements Runnable {
private void retrieveResult() { private void retrieveResult() {
try { try {
String[] result = in.readLine().split(" "); // TODO: null check for in.readLine() String tmpRes = in.readLine();
if (tmpRes == null) {
log.warn("Couldn't read result from {}", clientSocket.getRemoteSocketAddress());
return;
}
String[] result = tmpRes.split(" ");
KeyRange kr = new KeyRange(Long.parseLong(result[0], 16), Long.parseLong(result[1], 16)); KeyRange kr = new KeyRange(Long.parseLong(result[0], 16), Long.parseLong(result[1], 16));
synchronized (keyServer) { synchronized (keyServer) {

View file

@ -28,10 +28,10 @@ public class CommServer {
ExecutorService es = Executors.newCachedThreadPool(); ExecutorService es = Executors.newCachedThreadPool();
while (true) { while (true) { // NOSONAR
try { try {
Socket socket = serverSocket.accept(); Socket socket = serverSocket.accept();
log.debug("Got connection from " + socket.getInetAddress()); log.debug("Got connection from {}", socket.getInetAddress());
es.execute(new ClientWorker(socket, keyServer)); es.execute(new ClientWorker(socket, keyServer));
} catch (IOException e) { } catch (IOException e) {
@ -52,7 +52,7 @@ public class CommServer {
this.serverSocket = new ServerSocket(port); this.serverSocket = new ServerSocket(port);
} catch (IOException e) { } catch (IOException e) {
log.error("Couldn't allocate server socket", e); log.error("Couldn't allocate server socket", e);
throw new CommException(e);
} }
} }

View file

@ -29,9 +29,7 @@ public class KeyRange {
public Long getTotal() { public Long getTotal() {
// asserted that <= MAX_BITS, first long contains all significant bits // asserted that <= MAX_BITS, first long contains all significant bits
Long total = getEnd() - getStart() + 1; // +1 bc start is included too return (getEnd() - getStart() + 1); // +1 bc start is included too
return total;
} }
@Override @Override

View file

@ -32,7 +32,8 @@ public class KeyTreeServer implements KeyServer {
prune(pruneKey); prune(pruneKey);
if (maxWorkSpan > 0) { if (maxWorkSpan > 0) {
log.info("Starting KeyTree concierge"); log.info("Starting KeyTree concierge with timeout {}s",
TimeUnit.MILLISECONDS.toSeconds(maxWorkSpan));
ScheduledExecutorService es = Executors.newSingleThreadScheduledExecutor(); ScheduledExecutorService es = Executors.newSingleThreadScheduledExecutor();
es.scheduleWithFixedDelay(new KTConcierge(root, maxWorkSpan), 0, 30, TimeUnit.SECONDS); es.scheduleWithFixedDelay(new KTConcierge(root, maxWorkSpan), 0, 30, TimeUnit.SECONDS);
} else { } else {
@ -51,9 +52,7 @@ public class KeyTreeServer implements KeyServer {
keyEnd.or(keyStart); // set keyEnd to keyStart keyEnd.or(keyStart); // set keyEnd to keyStart
keyEnd.flip(0, index + 1 - depth); // reverse last bits keyEnd.flip(0, index + 1 - depth); // reverse last bits
KeyRange kr = new KeyRange(keyStart, keyEnd); return new KeyRange(keyStart, keyEnd);
return kr;
} }
public void setSearched(Long from, Long to) { public void setSearched(Long from, Long to) {
@ -81,7 +80,7 @@ public class KeyTreeServer implements KeyServer {
} }
public String printDot() { public String printDot() {
StringBuffer buffer = new StringBuffer(); StringBuilder buffer = new StringBuilder();
buffer.append("strict graph { \n"); buffer.append("strict graph { \n");
Queue<KeyTree> frontier = new LinkedList<>(); Queue<KeyTree> frontier = new LinkedList<>();
@ -233,12 +232,10 @@ public class KeyTreeServer implements KeyServer {
private KeyTree generateKeyTree(int depth) { private KeyTree generateKeyTree(int depth) {
assert depth > 0; assert depth > 0;
KeyTree root = new KeyTree(true, null); KeyTree genRoot = new KeyTree(true, null);
log.info("Starting recGenTree"); recGenKeyTree(genRoot, depth - 1);
recGenKeyTree(root, depth - 1);
log.info("Ending recGenTree");
return root; return genRoot;
} }
private void recGenKeyTree(KeyTree kt, int depth) { private void recGenKeyTree(KeyTree kt, int depth) {

View file

@ -1,21 +1,17 @@
package org.btcollider.cnc.keysrv; package org.btcollider.cnc.keysrv;
import static org.junit.jupiter.api.Assertions.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.btcollider.cnc.dto.KeyRange; import org.btcollider.cnc.dto.KeyRange;
import org.btcollider.cnc.keysrv.impl.keytree.KeyTreeServer;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
class KeyServerTest { class KeyServerTest {
@Test @Test
void testMultiStagedServer() { void testMultiStagedServer() {
KeyServer ks = KSFactory.build(40, 30, 0, TimeUnit.SECONDS); KeyServer ks = KSFactory.build(40, 30, 0, TimeUnit.SECONDS);
KeyRange kr = ks.getRange(); KeyRange kr = ks.getRange();
ks.setInWork(kr); ks.setInWork(kr);
ks.setSearched(kr); ks.setSearched(kr);
} }
} }