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