Some fixes, server POW is working now
This commit is contained in:
parent
aafa4a4a11
commit
634bcffa9d
@ -122,6 +122,7 @@ public class JabitServerConfig {
|
||||
|
||||
@Bean
|
||||
public Set<BitmessageAddress> admins() {
|
||||
security();
|
||||
return Utils.readOrCreateList(
|
||||
ADMIN_LIST,
|
||||
"# Admins can send commands to the server.\n"
|
||||
|
@ -27,6 +27,7 @@ import ch.dissem.bitmessage.extensions.pow.ProofOfWorkRequest;
|
||||
import ch.dissem.bitmessage.ports.CustomCommandHandler;
|
||||
import ch.dissem.bitmessage.ports.ProofOfWorkEngine;
|
||||
import ch.dissem.bitmessage.server.repository.ServerProofOfWorkRepository;
|
||||
import ch.dissem.bitmessage.utils.UnixTime;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -39,6 +40,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
import static ch.dissem.bitmessage.extensions.pow.ProofOfWorkRequest.Request.CALCULATING;
|
||||
import static ch.dissem.bitmessage.extensions.pow.ProofOfWorkRequest.Request.COMPLETE;
|
||||
import static ch.dissem.bitmessage.utils.UnixTime.DAY;
|
||||
|
||||
/**
|
||||
* @author Christian Basler
|
||||
@ -61,6 +63,12 @@ public class ProofOfWorkRequestHandler implements CustomCommandHandler, Internal
|
||||
doMissingProofOfWork();
|
||||
}
|
||||
}, 15_000); // After 15 seconds
|
||||
new Timer().schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
repo.cleanupTasks(7 * DAY);
|
||||
}
|
||||
}, 60_000, DAY * 1000); // First time after 1 minute, then daily
|
||||
}
|
||||
|
||||
public void doMissingProofOfWork() {
|
||||
@ -77,11 +85,12 @@ public class ProofOfWorkRequestHandler implements CustomCommandHandler, Internal
|
||||
CryptoCustomMessage<ProofOfWorkRequest> cryptoMessage = CryptoCustomMessage.read(message,
|
||||
ProofOfWorkRequest::read);
|
||||
ProofOfWorkRequest request = decrypt(cryptoMessage);
|
||||
if (request == null)
|
||||
if (request == null) {
|
||||
return CustomMessage.error(
|
||||
"Unknown sender. Please ask the server's administrator to add you as a client. " +
|
||||
"For this he'll need your identity."
|
||||
);
|
||||
}
|
||||
switch (request.getRequest()) {
|
||||
case CALCULATE:
|
||||
if (!repo.hasTask(request.getInitialHash())) {
|
||||
|
@ -19,6 +19,7 @@ package ch.dissem.bitmessage.server.repository;
|
||||
import ch.dissem.bitmessage.extensions.pow.ProofOfWorkRequest;
|
||||
import ch.dissem.bitmessage.repository.JdbcConfig;
|
||||
import ch.dissem.bitmessage.repository.JdbcHelper;
|
||||
import ch.dissem.bitmessage.utils.UnixTime;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
@ -46,10 +47,11 @@ public class ServerProofOfWorkRepository extends JdbcHelper {
|
||||
public void storeTask(ProofOfWorkRequest request) {
|
||||
try (Connection connection = config.getConnection()) {
|
||||
PreparedStatement ps = connection.prepareStatement(
|
||||
"INSERT INTO ProofOfWorkTask (initial_hash, client, target) VALUES (?, ?, ?)");
|
||||
"INSERT INTO ProofOfWorkTask (initial_hash, client, target, timestamp) VALUES (?, ?, ?, ?)");
|
||||
ps.setBytes(1, request.getInitialHash());
|
||||
ps.setString(2, request.getSender().getAddress());
|
||||
ps.setBytes(3, request.getData());
|
||||
ps.setLong(4, UnixTime.now());
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
@ -112,6 +114,17 @@ public class ServerProofOfWorkRepository extends JdbcHelper {
|
||||
}
|
||||
}
|
||||
|
||||
public void cleanupTasks(long ageInSeconds) {
|
||||
try (Connection connection = config.getConnection()) {
|
||||
PreparedStatement ps = connection.prepareStatement(
|
||||
"DELETE FROM ProofOfWorkTask WHERE timestamp < ?");
|
||||
ps.setLong(1, UnixTime.now(-ageInSeconds));
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Task {
|
||||
public final byte[] initialHash;
|
||||
public final byte[] target;
|
||||
|
@ -1,7 +1,7 @@
|
||||
CREATE TABLE ProofOfWorkTask (
|
||||
initial_hash BINARY(64) NOT NULL PRIMARY KEY,
|
||||
client VARCHAR(40) NOT NULL,
|
||||
initial_hash BINARY(64) NOT NULL PRIMARY KEY,
|
||||
client VARCHAR(40) NOT NULL,
|
||||
target BINARY(32),
|
||||
nonce BINARY(8),
|
||||
status VARCHAR(20),
|
||||
timestamp BIGINT NOT NULL,
|
||||
);
|
Loading…
Reference in New Issue
Block a user