SHA256
19 12 25
Переделал и запросы и тесты (но ещё не тетил)
This commit is contained in:
+21
-20
@@ -9,7 +9,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
|
||||
*
|
||||
* Позволяет:
|
||||
* - получить ConnectionContext по sessionId;
|
||||
* - получить все активные подключения пользователя по loginId;
|
||||
* - получить все активные подключения пользователя по login;
|
||||
* - удалить подключение при закрытии WebSocket.
|
||||
*
|
||||
* найти все подключения пользователя:
|
||||
@@ -34,27 +34,27 @@ public final class ActiveConnectionsRegistry {
|
||||
// sessionId (String) -> ConnectionContext
|
||||
private final ConcurrentHashMap<String, ConnectionContext> bySessionId = new ConcurrentHashMap<>();
|
||||
|
||||
// loginId -> множество ConnectionContext для этого пользователя
|
||||
private final ConcurrentHashMap<Long, Set<ConnectionContext>> byLoginId = new ConcurrentHashMap<>();
|
||||
// login (String) -> множество ConnectionContext для этого пользователя
|
||||
private final ConcurrentHashMap<String, Set<ConnectionContext>> byLogin = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* Зарегистрировать авторизованное подключение.
|
||||
* Ожидается, что в ctx уже выставлены loginId и sessionId.
|
||||
* Ожидается, что в ctx уже выставлены login и sessionId.
|
||||
*/
|
||||
public void register(ConnectionContext ctx) {
|
||||
if (ctx == null) return;
|
||||
|
||||
String sessionId = ctx.getSessionId();
|
||||
Long loginId = ctx.getLoginId();
|
||||
String login = ctx.getLogin();
|
||||
|
||||
if (sessionId == null || loginId == null) {
|
||||
if (sessionId == null || login == null || login.isBlank()) {
|
||||
return;
|
||||
}
|
||||
|
||||
bySessionId.put(sessionId, ctx);
|
||||
|
||||
byLoginId
|
||||
.computeIfAbsent(loginId, id -> new CopyOnWriteArraySet<>())
|
||||
byLogin
|
||||
.computeIfAbsent(login, id -> new CopyOnWriteArraySet<>())
|
||||
.add(ctx);
|
||||
}
|
||||
|
||||
@@ -65,18 +65,18 @@ public final class ActiveConnectionsRegistry {
|
||||
if (ctx == null) return;
|
||||
|
||||
String sessionId = ctx.getSessionId();
|
||||
Long loginId = ctx.getLoginId();
|
||||
String login = ctx.getLogin();
|
||||
|
||||
if (sessionId != null) {
|
||||
bySessionId.remove(sessionId);
|
||||
}
|
||||
|
||||
if (loginId != null) {
|
||||
Set<ConnectionContext> set = byLoginId.get(loginId);
|
||||
if (login != null) {
|
||||
Set<ConnectionContext> set = byLogin.get(login);
|
||||
if (set != null) {
|
||||
set.remove(ctx);
|
||||
if (set.isEmpty()) {
|
||||
byLoginId.remove(loginId);
|
||||
byLogin.remove(login);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -90,13 +90,13 @@ public final class ActiveConnectionsRegistry {
|
||||
|
||||
ConnectionContext ctx = bySessionId.remove(sessionId);
|
||||
if (ctx != null) {
|
||||
Long loginId = ctx.getLoginId();
|
||||
if (loginId != null) {
|
||||
Set<ConnectionContext> set = byLoginId.get(loginId);
|
||||
String login = ctx.getLogin();
|
||||
if (login != null) {
|
||||
Set<ConnectionContext> set = byLogin.get(login);
|
||||
if (set != null) {
|
||||
set.remove(ctx);
|
||||
if (set.isEmpty()) {
|
||||
byLoginId.remove(loginId);
|
||||
byLogin.remove(login);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -112,14 +112,15 @@ public final class ActiveConnectionsRegistry {
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить все активные подключения пользователя по loginId.
|
||||
* Получить все активные подключения пользователя по login.
|
||||
*/
|
||||
public Set<ConnectionContext> getByLoginId(long loginId) {
|
||||
Set<ConnectionContext> set = byLoginId.get(loginId);
|
||||
public Set<ConnectionContext> getByLogin(String login) {
|
||||
if (login == null) return Set.of();
|
||||
Set<ConnectionContext> set = byLogin.get(login);
|
||||
if (set == null) {
|
||||
return Set.of();
|
||||
}
|
||||
// CopyOnWriteArraySet безопасно отдавать как есть
|
||||
return set;
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-6
@@ -77,16 +77,12 @@ public class ConnectionContext {
|
||||
this.activeSessionEntry = activeSessionEntry;
|
||||
}
|
||||
|
||||
// --- Удобные геттеры для логина ---
|
||||
// --- Удобный геттер для логина ---
|
||||
|
||||
public String getLogin() {
|
||||
return solanaUserEntry != null ? solanaUserEntry.getLogin() : null;
|
||||
}
|
||||
|
||||
public Long getLoginId() {
|
||||
return solanaUserEntry != null ? solanaUserEntry.getLoginId() : null;
|
||||
}
|
||||
|
||||
// --- sessionId / sessionPwd ---
|
||||
|
||||
public String getSessionId() {
|
||||
@@ -149,7 +145,6 @@ public class ConnectionContext {
|
||||
public String toString() {
|
||||
return "ConnectionContext{" +
|
||||
"login='" + getLogin() + '\'' +
|
||||
", loginId=" + getLoginId() +
|
||||
", sessionId=" + sessionId +
|
||||
", authenticationStatus=" + authenticationStatus +
|
||||
'}';
|
||||
|
||||
+7
-7
@@ -4,17 +4,17 @@ import server.logic.ws_protocol.JSON.entyties.Net_Request;
|
||||
|
||||
public final class Net_AddBlock_Request extends Net_Request {
|
||||
|
||||
private String login; // обязателен
|
||||
private long blockchainId; // обязателен
|
||||
private int globalNumber; // обязателен
|
||||
private String prevGlobalHash; // HEX(64) или "" для нулевого
|
||||
private String blockBytesB64; // байты FULL-блока (raw+sig+hash) в Base64
|
||||
private String login; // обязателен
|
||||
private String blockchainName; // обязателен
|
||||
private int globalNumber; // обязателен
|
||||
private String prevGlobalHash; // HEX(64) или "" для нулевого
|
||||
private String blockBytesB64; // байты FULL-блока (raw+sig+hash) в Base64
|
||||
|
||||
public String getLogin() { return login; }
|
||||
public void setLogin(String login) { this.login = login; }
|
||||
|
||||
public long getBlockchainId() { return blockchainId; }
|
||||
public void setBlockchainId(long blockchainId) { this.blockchainId = blockchainId; }
|
||||
public String getBlockchainName() { return blockchainName; }
|
||||
public void setBlockchainName(String blockchainName) { this.blockchainName = blockchainName; }
|
||||
|
||||
public int getGlobalNumber() { return globalNumber; }
|
||||
public void setGlobalNumber(int globalNumber) { this.globalNumber = globalNumber; }
|
||||
|
||||
+13
-48
@@ -12,8 +12,7 @@ import server.logic.ws_protocol.JSON.entyties.Net_Request;
|
||||
* "requestId": "test-add-1",
|
||||
* "payload": {
|
||||
* "login": "anya",
|
||||
* "loginId": 100211,
|
||||
* "bchId": 4222,
|
||||
* "blockchainName": "anya0001",
|
||||
* "loginKey": "base64-ed25519-public-key-login",
|
||||
* "deviceKey": "base64-ed25519-public-key-device",
|
||||
* "bchLimit": 1000000
|
||||
@@ -25,57 +24,23 @@ import server.logic.ws_protocol.JSON.entyties.Net_Request;
|
||||
public class Net_AddUser_Request extends Net_Request {
|
||||
|
||||
private String login;
|
||||
private long loginId;
|
||||
private long bchId;
|
||||
private String blockchainName;
|
||||
private String loginKey;
|
||||
private String deviceKey;
|
||||
private Integer bchLimit;
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
public String getLogin() { return login; }
|
||||
public void setLogin(String login) { this.login = login; }
|
||||
|
||||
public void setLogin(String login) {
|
||||
this.login = login;
|
||||
}
|
||||
public String getBlockchainName() { return blockchainName; }
|
||||
public void setBlockchainName(String blockchainName) { this.blockchainName = blockchainName; }
|
||||
|
||||
public long getLoginId() {
|
||||
return loginId;
|
||||
}
|
||||
public String getLoginKey() { return loginKey; }
|
||||
public void setLoginKey(String loginKey) { this.loginKey = loginKey; }
|
||||
|
||||
public void setLoginId(long loginId) {
|
||||
this.loginId = loginId;
|
||||
}
|
||||
public String getDeviceKey() { return deviceKey; }
|
||||
public void setDeviceKey(String deviceKey) { this.deviceKey = deviceKey; }
|
||||
|
||||
public long getBchId() {
|
||||
return bchId;
|
||||
}
|
||||
|
||||
public void setBchId(long bchId) {
|
||||
this.bchId = bchId;
|
||||
}
|
||||
|
||||
public String getLoginKey() {
|
||||
return loginKey;
|
||||
}
|
||||
|
||||
public void setLoginKey(String loginKey) {
|
||||
this.loginKey = loginKey;
|
||||
}
|
||||
|
||||
public String getDeviceKey() {
|
||||
return deviceKey;
|
||||
}
|
||||
|
||||
public void setDeviceKey(String deviceKey) {
|
||||
this.deviceKey = deviceKey;
|
||||
}
|
||||
|
||||
public Integer getBchLimit() {
|
||||
return bchLimit;
|
||||
}
|
||||
|
||||
public void setBchLimit(Integer bchLimit) {
|
||||
this.bchLimit = bchLimit;
|
||||
}
|
||||
}
|
||||
public Integer getBchLimit() { return bchLimit; }
|
||||
public void setBchLimit(Integer bchLimit) { this.bchLimit = bchLimit; }
|
||||
}
|
||||
+3
-6
@@ -63,7 +63,7 @@ public class Net_CloseActiveSession_Handler implements JsonMessageHandler {
|
||||
}
|
||||
|
||||
SolanaUserEntry user = ctx.getSolanaUser();
|
||||
long currentLoginId = user.getLoginId();
|
||||
String currentLogin = user.getLogin();
|
||||
|
||||
int authStatus = ctx.getAuthenticationStatus();
|
||||
if (authStatus != ConnectionContext.AUTH_STATUS_USER
|
||||
@@ -180,7 +180,7 @@ public class Net_CloseActiveSession_Handler implements JsonMessageHandler {
|
||||
);
|
||||
}
|
||||
|
||||
if (targetSession.getLoginId() != currentLoginId) {
|
||||
if (currentLogin == null || !currentLogin.equals(targetSession.getLogin())) {
|
||||
return NetExceptionResponseFactory.error(
|
||||
req,
|
||||
WireCodes.Status.UNVERIFIED,
|
||||
@@ -236,10 +236,7 @@ public class Net_CloseActiveSession_Handler implements JsonMessageHandler {
|
||||
if (isCurrentSession && ctxToClose == currentCtx) {
|
||||
// Это текущее подключение: закрываем после отправки ответа.
|
||||
new Thread(() -> {
|
||||
try {
|
||||
Thread.sleep(50); // небольшая пауза, чтобы ответ ушёл
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
try { Thread.sleep(50); } catch (InterruptedException ignored) {}
|
||||
WsConnectionUtils.closeConnection(
|
||||
ctxToClose,
|
||||
4000,
|
||||
|
||||
+9
-55
@@ -25,52 +25,14 @@ import java.sql.SQLException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* Шаг 2 авторизации: проверка подписи и создание сессии.
|
||||
*
|
||||
* Клиент присылает в payload:
|
||||
* - storagePwd (base64 от 32 байт)
|
||||
* - timeMs (long, мс с 1970-01-01)
|
||||
* - signatureB64 (подпись Ed25519 над строкой:
|
||||
* "AUTHORIFICATED:" + timeMs + authNonce)
|
||||
* - clientInfo (опционально, до 50 символов)
|
||||
*
|
||||
* authNonce клиент получил на шаге 1 (AuthChallenge) и сервер
|
||||
* сохранил его в ctx.authNonce.
|
||||
*
|
||||
* При успехе:
|
||||
* - создаётся запись ActiveSession в БД;
|
||||
* - генерируется sessionId (base64 от 32 случайных байт);
|
||||
* - генерируется sessionPwd (base64 от 32 случайных байт);
|
||||
* - sessionCreatedAtMs и lastAuthirificatedAtMs = текущее время;
|
||||
* - заполняются поля clientIp, clientInfoFromClient, clientInfoFromRequest, userLanguage;
|
||||
* - возвращается sessionId и sessionPwd в ответе.
|
||||
*
|
||||
* При ошибке авторификации (битые данные, подпись, время и т.п.) —
|
||||
* соединение закрывается через WsConnectionUtils.
|
||||
*/
|
||||
public class Net_CreateAuthSession__Handler implements JsonMessageHandler {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(Net_CreateAuthSession__Handler.class);
|
||||
|
||||
private static final SecureRandom RANDOM = new SecureRandom();
|
||||
|
||||
/** Допустимое расхождение времени клиента и сервера (мс). */
|
||||
public static final long ALLOWED_SKEW_MS = 30_000L;
|
||||
|
||||
/**
|
||||
* Общая проверка подписи Ed25519 над строкой:
|
||||
* "AUTHORIFICATED:" + timeMs + authNonce.
|
||||
*
|
||||
* Используется и в CreateAuthSession, и в CloseActiveSession (для статуса AUTH_IN_PROGRESS).
|
||||
*
|
||||
* @param user пользователь (используется deviceKey)
|
||||
* @param authNonce одноразовый nonce из шага 1
|
||||
* @param timeMs время на стороне клиента
|
||||
* @param signatureB64 подпись в base64
|
||||
* @return true — подпись корректна; false — подпись не проходит верификацию
|
||||
* @throws IllegalArgumentException при некорректном base64 ключа/подписи
|
||||
*/
|
||||
public static boolean verifyAuthorificatedSignature(
|
||||
SolanaUserEntry user,
|
||||
String authNonce,
|
||||
@@ -92,7 +54,6 @@ public class Net_CreateAuthSession__Handler implements JsonMessageHandler {
|
||||
public Net_Response handle(Net_Request baseReq, ConnectionContext ctx) throws Exception {
|
||||
Net_CreateAuthSession_Request req = (Net_CreateAuthSession_Request) baseReq;
|
||||
|
||||
// --- базовые проверки контекста шага 1 ---
|
||||
if (ctx == null
|
||||
|| ctx.getSolanaUser() == null
|
||||
|| ctx.getAuthNonce() == null
|
||||
@@ -109,15 +70,15 @@ public class Net_CreateAuthSession__Handler implements JsonMessageHandler {
|
||||
}
|
||||
|
||||
SolanaUserEntry user = ctx.getSolanaUser();
|
||||
Long loginId = user.getLoginId();
|
||||
if (loginId == null) {
|
||||
String login = user.getLogin();
|
||||
if (login == null || login.isBlank()) {
|
||||
Net_Response err = NetExceptionResponseFactory.error(
|
||||
req,
|
||||
WireCodes.Status.SERVER_DATA_ERROR,
|
||||
"NO_LOGIN_ID",
|
||||
"Для пользователя не задан loginId в БД"
|
||||
"NO_LOGIN",
|
||||
"Для пользователя не задан login в БД"
|
||||
);
|
||||
WsConnectionUtils.closeConnection(ctx, 4001, "Auth failed: no loginId");
|
||||
WsConnectionUtils.closeConnection(ctx, 4001, "Auth failed: no login");
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -160,13 +121,11 @@ public class Net_CreateAuthSession__Handler implements JsonMessageHandler {
|
||||
return err;
|
||||
}
|
||||
|
||||
// Короткая строка clientInfo от клиента (до 50 символов)
|
||||
String clientInfoFromClient = req.getClientInfo();
|
||||
if (clientInfoFromClient != null && clientInfoFromClient.length() > 50) {
|
||||
clientInfoFromClient = clientInfoFromClient.substring(0, 50);
|
||||
}
|
||||
|
||||
// --- выбираем публичный ключ pubkey1 ---
|
||||
String pubKeyB64 = user.getDeviceKey();
|
||||
if (pubKeyB64 == null || pubKeyB64.isBlank()) {
|
||||
Net_Response err = NetExceptionResponseFactory.error(
|
||||
@@ -179,10 +138,8 @@ public class Net_CreateAuthSession__Handler implements JsonMessageHandler {
|
||||
return err;
|
||||
}
|
||||
|
||||
// --- authNonce (challenge) мы сохранили в ctx.authNonce на шаге 1 ---
|
||||
String authNonce = ctx.getAuthNonce();
|
||||
|
||||
// --- проверяем подпись через общий метод ---
|
||||
boolean sigOk;
|
||||
try {
|
||||
sigOk = verifyAuthorificatedSignature(user, authNonce, timeMs, signatureB64);
|
||||
@@ -230,10 +187,7 @@ public class Net_CreateAuthSession__Handler implements JsonMessageHandler {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (clientIp == null) {
|
||||
clientIp = "";
|
||||
}
|
||||
if (clientIp == null) clientIp = "";
|
||||
|
||||
// --- создаём запись ActiveSession и сохраняем в БД ---
|
||||
ActiveSessionsDAO dao = ActiveSessionsDAO.getInstance();
|
||||
@@ -242,8 +196,8 @@ public class Net_CreateAuthSession__Handler implements JsonMessageHandler {
|
||||
try {
|
||||
activeSessionEntry = new ActiveSessionEntry(
|
||||
sessionId,
|
||||
loginId,
|
||||
newSessionPwd, // настоящий секрет сессии
|
||||
login,
|
||||
newSessionPwd,
|
||||
storagePwd,
|
||||
now,
|
||||
now,
|
||||
@@ -258,7 +212,7 @@ public class Net_CreateAuthSession__Handler implements JsonMessageHandler {
|
||||
|
||||
dao.insert(activeSessionEntry);
|
||||
} catch (SQLException e) {
|
||||
log.error("Ошибка БД при создании новой сессии для loginId={}", loginId, e);
|
||||
log.error("Ошибка БД при создании новой сессии для login={}", login, e);
|
||||
Net_Response err = NetExceptionResponseFactory.error(
|
||||
req,
|
||||
WireCodes.Status.SERVER_DATA_ERROR,
|
||||
|
||||
+3
-3
@@ -51,7 +51,7 @@ public class Net_ListSessions_Handler implements JsonMessageHandler {
|
||||
}
|
||||
|
||||
SolanaUserEntry user = ctx.getSolanaUser();
|
||||
long currentLoginId = user.getLoginId();
|
||||
String currentLogin = user.getLogin();
|
||||
|
||||
int authStatus = ctx.getAuthenticationStatus();
|
||||
if (authStatus != ConnectionContext.AUTH_STATUS_USER
|
||||
@@ -130,9 +130,9 @@ public class Net_ListSessions_Handler implements JsonMessageHandler {
|
||||
// 3) Тянем все активные сессии пользователя из БД
|
||||
List<ActiveSessionEntry> sessions;
|
||||
try {
|
||||
sessions = ActiveSessionsDAO.getInstance().getByLoginId(currentLoginId);
|
||||
sessions = ActiveSessionsDAO.getInstance().getByLogin(currentLogin);
|
||||
} catch (SQLException e) {
|
||||
log.error("Ошибка БД при получении списка сессий для loginId={}", currentLoginId, e);
|
||||
log.error("Ошибка БД при получении списка сессий для login={}", currentLogin, e);
|
||||
return NetExceptionResponseFactory.error(
|
||||
req,
|
||||
WireCodes.Status.SERVER_DATA_ERROR,
|
||||
|
||||
+6
-5
@@ -95,14 +95,15 @@ public class Net_RefreshSession_Handler implements JsonMessageHandler {
|
||||
);
|
||||
}
|
||||
|
||||
// --- вытаскиваем пользователя по loginId ---
|
||||
SolanaUserEntry solanaUserEntry = null;
|
||||
long loginId = session.getLoginId();
|
||||
// --- вытаскиваем пользователя по login из сессии ---
|
||||
SolanaUserEntry solanaUserEntry;
|
||||
String login = session.getLogin();
|
||||
|
||||
try {
|
||||
SolanaUsersDAO usersDao = SolanaUsersDAO.getInstance();
|
||||
solanaUserEntry = usersDao.getByLoginId(loginId);
|
||||
solanaUserEntry = usersDao.getByLogin(login);
|
||||
} catch (SQLException e) {
|
||||
log.error("Ошибка БД при поиске пользователя по loginId={} из сессии", loginId, e);
|
||||
log.error("Ошибка БД при поиске пользователя по login={} из сессии", login, e);
|
||||
return NetExceptionResponseFactory.error(
|
||||
req,
|
||||
WireCodes.Status.SERVER_DATA_ERROR,
|
||||
|
||||
+3
-3
@@ -4,11 +4,11 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public final class BlockchainLocks {
|
||||
private static final ConcurrentHashMap<Long, ReentrantLock> MAP = new ConcurrentHashMap<>();
|
||||
private static final ConcurrentHashMap<String, ReentrantLock> MAP = new ConcurrentHashMap<>();
|
||||
|
||||
private BlockchainLocks() {}
|
||||
|
||||
public static ReentrantLock lockFor(long blockchainId) {
|
||||
return MAP.computeIfAbsent(blockchainId, id -> new ReentrantLock(true)); // fair=true
|
||||
public static ReentrantLock lockFor(String blockchainName) {
|
||||
return MAP.computeIfAbsent(blockchainName, id -> new ReentrantLock(true)); // fair=true
|
||||
}
|
||||
}
|
||||
+28
-19
@@ -11,7 +11,6 @@ import shine.db.entities.SolanaUserEntry;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
@@ -28,9 +27,9 @@ public final class BlockchainStateService_new {
|
||||
|
||||
/** Результат атомарного addBlock */
|
||||
public static final class AddBlockResult {
|
||||
public final int lineIndex; // 0..7 (пока ставим 0)
|
||||
public final int httpStatus; // WireCodes.Status.*
|
||||
public final String reasonCode; // null если ok
|
||||
public final int lineIndex; // 0..7 (пока ставим 0)
|
||||
public final int httpStatus; // WireCodes.Status.*
|
||||
public final String reasonCode; // null если ok
|
||||
public final BlockchainStateEntry stateAfter; // состояние после (может быть null)
|
||||
|
||||
public AddBlockResult(int lineIndex, int httpStatus, String reasonCode, BlockchainStateEntry stateAfter) {
|
||||
@@ -69,7 +68,7 @@ public final class BlockchainStateService_new {
|
||||
*/
|
||||
public AddBlockResult addBlockAtomically(
|
||||
String login,
|
||||
long blockchainId,
|
||||
String blockchainName,
|
||||
int globalNumber,
|
||||
String prevGlobalHash,
|
||||
String blockBytesB64
|
||||
@@ -91,11 +90,21 @@ public final class BlockchainStateService_new {
|
||||
);
|
||||
}
|
||||
|
||||
if (login == null || login.isBlank()) {
|
||||
return new AddBlockResult(lineIndex, WireCodes.Status.BAD_REQUEST, "empty_login", null);
|
||||
}
|
||||
if (blockchainName == null || blockchainName.isBlank()) {
|
||||
return new AddBlockResult(lineIndex, WireCodes.Status.BAD_REQUEST, "empty_blockchain_name", null);
|
||||
}
|
||||
if (blockBytes == null || blockBytes.length == 0) {
|
||||
return new AddBlockResult(lineIndex, WireCodes.Status.BAD_REQUEST, "empty_block_bytes", null);
|
||||
}
|
||||
|
||||
try (Connection c = db.getConnection()) {
|
||||
boolean oldAutoCommit = c.getAutoCommit();
|
||||
c.setAutoCommit(false);
|
||||
try {
|
||||
// 1) получаем loginId по login
|
||||
// 1) получаем пользователя по login (если надо валидировать существование)
|
||||
SolanaUserEntry u = solanaUsersDAO.getByLogin(c, login);
|
||||
if (u == null) {
|
||||
c.rollback();
|
||||
@@ -106,13 +115,12 @@ public final class BlockchainStateService_new {
|
||||
null
|
||||
);
|
||||
}
|
||||
long loginId = u.getLoginId();
|
||||
|
||||
// 2) вставляем блок в blocks
|
||||
insertBlockRow(c, loginId, blockchainId, globalNumber, prevGlobalHash, blockBytes, lineIndex);
|
||||
insertBlockRow(c, login, blockchainName, globalNumber, prevGlobalHash, blockBytes, lineIndex);
|
||||
|
||||
// 3) обновляем агрегатное состояние blockchain_state
|
||||
BlockchainStateEntry st = stateDAO.getByBlockchainId(c, blockchainId);
|
||||
// 3) обновляем агрегатное состояние blockchain_state (по blockchainName)
|
||||
BlockchainStateEntry st = stateDAO.getByBlockchainName(c, blockchainName);
|
||||
if (st == null) {
|
||||
c.rollback();
|
||||
return new AddBlockResult(
|
||||
@@ -124,7 +132,6 @@ public final class BlockchainStateService_new {
|
||||
}
|
||||
|
||||
// MVP: обновляем “последний глобальный номер”.
|
||||
// Хэш тут сейчас оставлен как заглушка — лучше поставить фактический хэш нового блока.
|
||||
st.setLastGlobalNumber(globalNumber);
|
||||
st.setLastGlobalHash(nn(prevGlobalHash)); // TODO: заменить на hash нового блока
|
||||
st.setUpdatedAtMs(System.currentTimeMillis());
|
||||
@@ -158,8 +165,8 @@ public final class BlockchainStateService_new {
|
||||
|
||||
private void insertBlockRow(
|
||||
Connection c,
|
||||
long loginId,
|
||||
long blockchainId,
|
||||
String login,
|
||||
String blockchainName,
|
||||
int globalNumber,
|
||||
String prevGlobalHash,
|
||||
byte[] blockBytes,
|
||||
@@ -167,8 +174,9 @@ public final class BlockchainStateService_new {
|
||||
) throws SQLException {
|
||||
|
||||
BlockEntry e = new BlockEntry();
|
||||
e.setLoginId(loginId);
|
||||
e.setBlockchainId(blockchainId);
|
||||
|
||||
e.setLogin(login);
|
||||
e.setBchName(blockchainName);
|
||||
|
||||
e.setBlockGlobalNumber(globalNumber);
|
||||
e.setBlockGlobalPreHashe(nn(prevGlobalHash));
|
||||
@@ -182,10 +190,11 @@ public final class BlockchainStateService_new {
|
||||
|
||||
e.setBlockByte(blockBytes);
|
||||
|
||||
e.setToLoginId(0);
|
||||
e.setToBlockchainId(0);
|
||||
e.setToBlockGlobalNumber(0);
|
||||
e.setToBlockHashe("");
|
||||
// NEW: nullable ссылки (не забиваем фейковыми нулями)
|
||||
e.setToLogin(null);
|
||||
e.setToBchName(null);
|
||||
e.setToBlockGlobalNumber(null);
|
||||
e.setToBlockHashe(null);
|
||||
|
||||
blocksDAO.upsert(c, e);
|
||||
}
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@ public final class Net_AddBlock_new_Handler implements JsonMessageHandler {
|
||||
|
||||
var r = BlockchainStateService_new.getInstance().addBlockAtomically(
|
||||
req.getLogin(),
|
||||
req.getBlockchainId(),
|
||||
req.getBlockchainName(),
|
||||
req.getGlobalNumber(),
|
||||
req.getPrevGlobalHash(),
|
||||
req.getBlockBytesB64()
|
||||
|
||||
+6
-8
@@ -27,16 +27,15 @@ public class Net_AddUser_Handler implements JsonMessageHandler {
|
||||
Net_AddUser_Request req = (Net_AddUser_Request) baseRequest;
|
||||
|
||||
if (req.getLogin() == null || req.getLogin().isBlank()
|
||||
|| req.getBlockchainName() == null || req.getBlockchainName().isBlank()
|
||||
|| req.getLoginKey() == null || req.getLoginKey().isBlank()
|
||||
|| req.getDeviceKey() == null || req.getDeviceKey().isBlank()
|
||||
|| req.getLoginId() <= 0
|
||||
|| req.getBchId() <= 0) {
|
||||
|| req.getDeviceKey() == null || req.getDeviceKey().isBlank()) {
|
||||
|
||||
return NetExceptionResponseFactory.error(
|
||||
req,
|
||||
WireCodes.Status.BAD_REQUEST,
|
||||
"BAD_FIELDS",
|
||||
"Некорректные поля: login/loginId/bchId/loginKey/deviceKey"
|
||||
"Некорректные поля: login/blockchainName/loginKey/deviceKey"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -47,9 +46,8 @@ public class Net_AddUser_Handler implements JsonMessageHandler {
|
||||
SolanaUsersDAO dao = SolanaUsersDAO.getInstance();
|
||||
|
||||
SolanaUserEntry user = new SolanaUserEntry(
|
||||
req.getLoginId(),
|
||||
req.getLogin(),
|
||||
req.getBchId(),
|
||||
req.getBlockchainName(),
|
||||
req.getLoginKey(),
|
||||
req.getDeviceKey(),
|
||||
limit
|
||||
@@ -62,8 +60,8 @@ public class Net_AddUser_Handler implements JsonMessageHandler {
|
||||
resp.setRequestId(req.getRequestId());
|
||||
resp.setStatus(WireCodes.Status.OK);
|
||||
|
||||
log.info("✅ AddUser ok: login={}, loginId={}, bchId={}, limit={}",
|
||||
req.getLogin(), req.getLoginId(), req.getBchId(), limit);
|
||||
log.info("✅ AddUser ok: login={}, blockchainName={}, limit={}",
|
||||
req.getLogin(), req.getBlockchainName(), limit);
|
||||
|
||||
return resp;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user