24 12 25
Дорабатываю добавление блоков.
This commit is contained in:
parent
4e14f300f9
commit
bead78b372
@ -1,5 +1,8 @@
|
|||||||
package blockchain;
|
package blockchain;
|
||||||
|
|
||||||
|
import blockchain.body.BodyRecord;
|
||||||
|
import blockchain.body.BodyRecordParser;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -36,6 +39,9 @@ public final class BchBlockEntry {
|
|||||||
public final int lineNumber;
|
public final int lineNumber;
|
||||||
public final byte[] bodyBytes;
|
public final byte[] bodyBytes;
|
||||||
|
|
||||||
|
/** Распарсенное тело (создаётся сразу при парсинге блока). */
|
||||||
|
public final BodyRecord body;
|
||||||
|
|
||||||
// --- TAIL ---
|
// --- TAIL ---
|
||||||
private final byte[] signature64;
|
private final byte[] signature64;
|
||||||
private final byte[] hash32;
|
private final byte[] hash32;
|
||||||
@ -70,6 +76,9 @@ public final class BchBlockEntry {
|
|||||||
this.bodyBytes = new byte[bodyLen];
|
this.bodyBytes = new byte[bodyLen];
|
||||||
bb.get(this.bodyBytes);
|
bb.get(this.bodyBytes);
|
||||||
|
|
||||||
|
// ✅ Сразу парсим BodyRecord (и если неизвестный type/version — тут же упадём)
|
||||||
|
this.body = BodyRecordParser.parse(this.bodyBytes);
|
||||||
|
|
||||||
this.signature64 = new byte[SIGNATURE_LEN];
|
this.signature64 = new byte[SIGNATURE_LEN];
|
||||||
bb.get(this.signature64);
|
bb.get(this.signature64);
|
||||||
|
|
||||||
@ -105,6 +114,10 @@ public final class BchBlockEntry {
|
|||||||
this.lineIndex = lineIndex;
|
this.lineIndex = lineIndex;
|
||||||
this.lineNumber = lineNumber;
|
this.lineNumber = lineNumber;
|
||||||
this.bodyBytes = Arrays.copyOf(bodyBytes, bodyBytes.length);
|
this.bodyBytes = Arrays.copyOf(bodyBytes, bodyBytes.length);
|
||||||
|
|
||||||
|
// ✅ И при сборке — тоже сразу парсим body (чтобы объект был цельным)
|
||||||
|
this.body = BodyRecordParser.parse(this.bodyBytes);
|
||||||
|
|
||||||
this.signature64 = Arrays.copyOf(signature64, SIGNATURE_LEN);
|
this.signature64 = Arrays.copyOf(signature64, SIGNATURE_LEN);
|
||||||
this.hash32 = Arrays.copyOf(hash32, HASH_LEN);
|
this.hash32 = Arrays.copyOf(hash32, HASH_LEN);
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package server.logic.ws_protocol.JSON.handlers.blockchain;
|
package server.logic.ws_protocol.JSON.handlers.blockchain;
|
||||||
|
|
||||||
|
import blockchain.BchBlockEntry;
|
||||||
import shine.db.SqliteDbController;
|
import shine.db.SqliteDbController;
|
||||||
import shine.db.dao.BlockchainStateDAO;
|
import shine.db.dao.BlockchainStateDAO;
|
||||||
import shine.db.dao.BlocksDAO;
|
import shine.db.dao.BlocksDAO;
|
||||||
@ -31,9 +32,8 @@ public final class BlockchainWriter {
|
|||||||
public void appendBlockAndState(
|
public void appendBlockAndState(
|
||||||
String login,
|
String login,
|
||||||
String blockchainName,
|
String blockchainName,
|
||||||
int globalNumber,
|
|
||||||
String prevGlobalHashHex,
|
String prevGlobalHashHex,
|
||||||
byte[] blockBytes,
|
BchBlockEntry block,
|
||||||
BlockchainStateEntry stOrNull,
|
BlockchainStateEntry stOrNull,
|
||||||
String newHashHex
|
String newHashHex
|
||||||
) throws SQLException {
|
) throws SQLException {
|
||||||
@ -45,10 +45,10 @@ public final class BlockchainWriter {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// 1) блок
|
// 1) блок
|
||||||
insertBlockRow(c, login, blockchainName, globalNumber, prevGlobalHashHex, blockBytes);
|
insertBlockRow(c, login, blockchainName, prevGlobalHashHex, block);
|
||||||
|
|
||||||
// 2) state
|
// 2) state
|
||||||
appendState(c, blockchainName, globalNumber, stOrNull, newHashHex);
|
appendState(c, blockchainName, block.recordNumber, stOrNull, newHashHex);
|
||||||
|
|
||||||
// 3) commit
|
// 3) commit
|
||||||
c.commit();
|
c.commit();
|
||||||
@ -105,9 +105,8 @@ public final class BlockchainWriter {
|
|||||||
Connection c,
|
Connection c,
|
||||||
String login,
|
String login,
|
||||||
String blockchainName,
|
String blockchainName,
|
||||||
int globalNumber,
|
|
||||||
String prevGlobalHashHex,
|
String prevGlobalHashHex,
|
||||||
byte[] blockBytes
|
BchBlockEntry block
|
||||||
) throws SQLException {
|
) throws SQLException {
|
||||||
|
|
||||||
BlockEntry e = new BlockEntry();
|
BlockEntry e = new BlockEntry();
|
||||||
@ -117,19 +116,20 @@ public final class BlockchainWriter {
|
|||||||
e.setBchName(blockchainName);
|
e.setBchName(blockchainName);
|
||||||
|
|
||||||
// Глобальная нумерация
|
// Глобальная нумерация
|
||||||
e.setBlockGlobalNumber(globalNumber);
|
e.setBlockGlobalNumber(block.recordNumber);
|
||||||
e.setBlockGlobalPreHashe(prevGlobalHashHex);
|
e.setBlockGlobalPreHashe(prevGlobalHashHex);
|
||||||
|
|
||||||
// Линии пока не используются: lineIndex=0, lineNumber=globalNumber
|
// Линии пока не используются: lineIndex=0, lineNumber=globalNumber
|
||||||
e.setBlockLineIndex(0);
|
e.setBlockLineIndex(0);
|
||||||
e.setBlockLineNumber(globalNumber);
|
e.setBlockLineNumber(block.recordNumber);
|
||||||
e.setBlockLinePreHashe(prevGlobalHashHex);
|
e.setBlockLinePreHashe(prevGlobalHashHex);
|
||||||
|
|
||||||
// msgType у тебя пока 0 (при желании позже можно ставить по Body/type)
|
// msgType у тебя пока 0 (при желании позже можно ставить по Body/type)
|
||||||
e.setMsgType(0);
|
// ✅ Теперь сохраняем тип блока
|
||||||
|
e.setMsgType(block.body.type());
|
||||||
|
|
||||||
// Сырые байты полного блока
|
// Сырые байты полного блока
|
||||||
e.setBlockByte(blockBytes);
|
e.setBlockByte(block.toBytes());
|
||||||
|
|
||||||
// Поля "кому" (для сообщений/трансферов) пока пустые
|
// Поля "кому" (для сообщений/трансферов) пока пустые
|
||||||
e.setToLogin(null);
|
e.setToLogin(null);
|
||||||
|
|||||||
@ -2,7 +2,6 @@ package server.logic.ws_protocol.JSON.handlers.blockchain;
|
|||||||
|
|
||||||
import blockchain.BchBlockEntry;
|
import blockchain.BchBlockEntry;
|
||||||
import blockchain.BchCryptoVerifier;
|
import blockchain.BchCryptoVerifier;
|
||||||
import blockchain.body.BodyRecordParser;
|
|
||||||
import server.logic.ws_protocol.JSON.ConnectionContext;
|
import server.logic.ws_protocol.JSON.ConnectionContext;
|
||||||
import server.logic.ws_protocol.JSON.entyties.Net_Request;
|
import server.logic.ws_protocol.JSON.entyties.Net_Request;
|
||||||
import server.logic.ws_protocol.JSON.entyties.Net_Response;
|
import server.logic.ws_protocol.JSON.entyties.Net_Response;
|
||||||
@ -124,9 +123,9 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
return new AddBlockResult(WireCodes.Status.BAD_REQUEST, "bad_block_format", 0, "");
|
return new AddBlockResult(WireCodes.Status.BAD_REQUEST, "bad_block_format", 0, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5) Парсим и валидируем body (type/version + содержимое)
|
// 5) Валидируем body (type/version + содержимое) — теперь body уже распарсен внутри BchBlockEntry
|
||||||
try {
|
try {
|
||||||
BodyRecordParser.parse(block.bodyBytes).check();
|
block.body.check();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return new AddBlockResult(WireCodes.Status.BAD_REQUEST, "bad_block_body", 0, "");
|
return new AddBlockResult(WireCodes.Status.BAD_REQUEST, "bad_block_body", 0, "");
|
||||||
}
|
}
|
||||||
@ -221,9 +220,8 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
dbWriter.appendBlockAndState(
|
dbWriter.appendBlockAndState(
|
||||||
login,
|
login,
|
||||||
blockchainName,
|
blockchainName,
|
||||||
globalNumber,
|
|
||||||
nn(prevGlobalHashHex),
|
nn(prevGlobalHashHex),
|
||||||
blockBytes,
|
block, // ✅ передаём целиком объект блока
|
||||||
st,
|
st,
|
||||||
newHashHex
|
newHashHex
|
||||||
);
|
);
|
||||||
|
|||||||
@ -20,9 +20,9 @@ public final class InboundMessageProcessor {
|
|||||||
private static final Map<Integer, MessageHandler> HANDLERS = Map.of(
|
private static final Map<Integer, MessageHandler> HANDLERS = Map.of(
|
||||||
WireCodes.Op.PING, new PingHandler(),
|
WireCodes.Op.PING, new PingHandler(),
|
||||||
// WireCodes.Op.ADD_BLOCK, new AddBlockHandler(),
|
// WireCodes.Op.ADD_BLOCK, new AddBlockHandler(),
|
||||||
WireCodes.Op.GET_BLOCKCHAIN,new GetBlockchainHandler(),
|
WireCodes.Op.GET_BLOCKCHAIN,new GetBlockchainHandler()
|
||||||
WireCodes.Op.SEARCH_USERS, new SearchUsersHandler(),
|
// WireCodes.Op.SEARCH_USERS, new SearchUsersHandler(),
|
||||||
WireCodes.Op.GET_LAST_BLOCK_INFO,new GetLastBlockInfoHandler()
|
// WireCodes.Op.GET_LAST_BLOCK_INFO,new GetLastBlockInfoHandler()
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@ -1,66 +1,66 @@
|
|||||||
package server.logic.ws_protocol.binary.handlers;
|
//package server.logic.ws_protocol.binary.handlers;
|
||||||
|
//
|
||||||
import org.slf4j.Logger;
|
//import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
//import org.slf4j.LoggerFactory;
|
||||||
import server.logic.ws_protocol.WireCodes;
|
//import server.logic.ws_protocol.WireCodes;
|
||||||
import utils.blockchain.BchInfoEntry;
|
//import utils.blockchain.BchInfoEntry;
|
||||||
import utils.blockchain.BchInfoManager;
|
//import utils.blockchain.BchInfoManager;
|
||||||
|
//
|
||||||
import java.nio.ByteBuffer;
|
//import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
//import java.nio.ByteOrder;
|
||||||
import java.util.Arrays;
|
//import java.util.Arrays;
|
||||||
|
//
|
||||||
/**
|
///**
|
||||||
* Возврат информации о последнем блоке цепочки (GET_LAST_BLOCK_INFO).
|
// * Возврат информации о последнем блоке цепочки (GET_LAST_BLOCK_INFO).
|
||||||
*/
|
// */
|
||||||
public class GetLastBlockInfoHandler implements MessageHandler {
|
//public class GetLastBlockInfoHandler implements MessageHandler {
|
||||||
private static final Logger log = LoggerFactory.getLogger(GetLastBlockInfoHandler.class);
|
// private static final Logger log = LoggerFactory.getLogger(GetLastBlockInfoHandler.class);
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public byte[] handle(byte[] msg) {
|
// public byte[] handle(byte[] msg) {
|
||||||
try {
|
// try {
|
||||||
if (msg.length < 12)
|
// if (msg.length < 12)
|
||||||
return intTo4Bytes(WireCodes.Status.BAD_REQUEST);
|
// return intTo4Bytes(WireCodes.Status.BAD_REQUEST);
|
||||||
|
//
|
||||||
long blockchainId = ByteBuffer.wrap(msg, 4, 8)
|
// long blockchainId = ByteBuffer.wrap(msg, 4, 8)
|
||||||
.order(ByteOrder.BIG_ENDIAN)
|
// .order(ByteOrder.BIG_ENDIAN)
|
||||||
.getLong();
|
// .getLong();
|
||||||
|
//
|
||||||
BchInfoManager mgr = BchInfoManager.getInstance();
|
// BchInfoManager mgr = BchInfoManager.getInstance();
|
||||||
BchInfoEntry entry = mgr.getBchInfo(blockchainId);
|
// BchInfoEntry entry = mgr.getBchInfo(blockchainId);
|
||||||
if (entry == null)
|
// if (entry == null)
|
||||||
return intTo4Bytes(WireCodes.Status.CHAIN_NOT_FOUND);
|
// return intTo4Bytes(WireCodes.Status.CHAIN_NOT_FOUND);
|
||||||
|
//
|
||||||
int lastNum = entry.lastBlockNumber;
|
// int lastNum = entry.lastBlockNumber;
|
||||||
byte[] hash = hexToBytes(entry.lastBlockHash);
|
// byte[] hash = hexToBytes(entry.lastBlockHash);
|
||||||
|
//
|
||||||
ByteBuffer out = ByteBuffer.allocate(4 + 4 + 32).order(ByteOrder.BIG_ENDIAN);
|
// ByteBuffer out = ByteBuffer.allocate(4 + 4 + 32).order(ByteOrder.BIG_ENDIAN);
|
||||||
out.putInt(WireCodes.Status.OK);
|
// out.putInt(WireCodes.Status.OK);
|
||||||
out.putInt(lastNum);
|
// out.putInt(lastNum);
|
||||||
out.put(hash);
|
// out.put(hash);
|
||||||
return out.array();
|
// return out.array();
|
||||||
|
//
|
||||||
} catch (Exception e) {
|
// } catch (Exception e) {
|
||||||
log.error("GET_LAST_BLOCK_INFO: ошибка", e);
|
// log.error("GET_LAST_BLOCK_INFO: ошибка", e);
|
||||||
return intTo4Bytes(WireCodes.Status.INTERNAL_ERROR);
|
// return intTo4Bytes(WireCodes.Status.INTERNAL_ERROR);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
private static byte[] intTo4Bytes(int code) {
|
// private static byte[] intTo4Bytes(int code) {
|
||||||
return ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN).putInt(code).array();
|
// return ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN).putInt(code).array();
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
private static byte[] hexToBytes(String hex) {
|
// private static byte[] hexToBytes(String hex) {
|
||||||
if (hex == null || hex.isEmpty()) return new byte[32];
|
// if (hex == null || hex.isEmpty()) return new byte[32];
|
||||||
int len = hex.length();
|
// int len = hex.length();
|
||||||
byte[] out = new byte[len / 2];
|
// byte[] out = new byte[len / 2];
|
||||||
for (int i = 0; i < len; i += 2)
|
// for (int i = 0; i < len; i += 2)
|
||||||
out[i / 2] = (byte) Integer.parseInt(hex.substring(i, i + 2), 16);
|
// out[i / 2] = (byte) Integer.parseInt(hex.substring(i, i + 2), 16);
|
||||||
if (out.length < 32) { // добиваем нулями
|
// if (out.length < 32) { // добиваем нулями
|
||||||
byte[] full = new byte[32];
|
// byte[] full = new byte[32];
|
||||||
System.arraycopy(out, 0, full, 32 - out.length, out.length);
|
// System.arraycopy(out, 0, full, 32 - out.length, out.length);
|
||||||
return full;
|
// return full;
|
||||||
}
|
// }
|
||||||
return Arrays.copyOf(out, 32);
|
// return Arrays.copyOf(out, 32);
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|||||||
@ -1,59 +1,59 @@
|
|||||||
package server.logic.ws_protocol.binary.handlers;
|
//package server.logic.ws_protocol.binary.handlers;
|
||||||
|
//
|
||||||
import org.slf4j.Logger;
|
//import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
//import org.slf4j.LoggerFactory;
|
||||||
import server.logic.ws_protocol.WireCodes;
|
//import server.logic.ws_protocol.WireCodes;
|
||||||
import utils.search.UserSearchService;
|
//import utils.search.UserSearchService;
|
||||||
|
//
|
||||||
import java.nio.ByteBuffer;
|
//import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
//import java.nio.ByteOrder;
|
||||||
import java.nio.charset.StandardCharsets;
|
//import java.nio.charset.StandardCharsets;
|
||||||
import java.util.List;
|
//import java.util.List;
|
||||||
|
//
|
||||||
/**
|
///**
|
||||||
* Поиск пользователей по логину (SEARCH_USERS).
|
// * Поиск пользователей по логину (SEARCH_USERS).
|
||||||
*/
|
// */
|
||||||
public class SearchUsersHandler implements MessageHandler {
|
//public class SearchUsersHandler implements MessageHandler {
|
||||||
private static final Logger log = LoggerFactory.getLogger(SearchUsersHandler.class);
|
// private static final Logger log = LoggerFactory.getLogger(SearchUsersHandler.class);
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public byte[] handle(byte[] msg) {
|
// public byte[] handle(byte[] msg) {
|
||||||
try {
|
// try {
|
||||||
if (msg.length < 8)
|
// if (msg.length < 8)
|
||||||
return intTo4Bytes(WireCodes.Status.BAD_REQUEST);
|
// return intTo4Bytes(WireCodes.Status.BAD_REQUEST);
|
||||||
|
//
|
||||||
int N = ByteBuffer.wrap(msg, 4, 4).order(ByteOrder.BIG_ENDIAN).getInt();
|
// int N = ByteBuffer.wrap(msg, 4, 4).order(ByteOrder.BIG_ENDIAN).getInt();
|
||||||
if (N < 0 || msg.length < 8 + N)
|
// if (N < 0 || msg.length < 8 + N)
|
||||||
return intTo4Bytes(WireCodes.Status.BAD_REQUEST);
|
// return intTo4Bytes(WireCodes.Status.BAD_REQUEST);
|
||||||
|
//
|
||||||
String query = new String(msg, 8, N, StandardCharsets.UTF_8);
|
// String query = new String(msg, 8, N, StandardCharsets.UTF_8);
|
||||||
List<UserSearchService.Pair> found = UserSearchService.getInstance().searchFirst5(query);
|
// List<UserSearchService.Pair> found = UserSearchService.getInstance().searchFirst5(query);
|
||||||
return pack(found);
|
// return pack(found);
|
||||||
|
//
|
||||||
} catch (Exception e) {
|
// } catch (Exception e) {
|
||||||
log.error("SEARCH_USERS: ошибка", e);
|
// log.error("SEARCH_USERS: ошибка", e);
|
||||||
return intTo4Bytes(WireCodes.Status.INTERNAL_ERROR);
|
// return intTo4Bytes(WireCodes.Status.INTERNAL_ERROR);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
private static byte[] pack(List<UserSearchService.Pair> pairs) {
|
// private static byte[] pack(List<UserSearchService.Pair> pairs) {
|
||||||
if (pairs == null) pairs = List.of();
|
// if (pairs == null) pairs = List.of();
|
||||||
int total = 8;
|
// int total = 8;
|
||||||
var chunks = new java.util.ArrayList<byte[]>();
|
// var chunks = new java.util.ArrayList<byte[]>();
|
||||||
for (var p : pairs) {
|
// for (var p : pairs) {
|
||||||
byte[] packed = UserSearchService.packPair(p);
|
// byte[] packed = UserSearchService.packPair(p);
|
||||||
chunks.add(packed);
|
// chunks.add(packed);
|
||||||
total += packed.length;
|
// total += packed.length;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
ByteBuffer out = ByteBuffer.allocate(total).order(ByteOrder.BIG_ENDIAN);
|
// ByteBuffer out = ByteBuffer.allocate(total).order(ByteOrder.BIG_ENDIAN);
|
||||||
out.putInt(WireCodes.Status.OK);
|
// out.putInt(WireCodes.Status.OK);
|
||||||
out.putInt(pairs.size());
|
// out.putInt(pairs.size());
|
||||||
for (var c : chunks) out.put(c);
|
// for (var c : chunks) out.put(c);
|
||||||
return out.array();
|
// return out.array();
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
private static byte[] intTo4Bytes(int code) {
|
// private static byte[] intTo4Bytes(int code) {
|
||||||
return ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN).putInt(code).array();
|
// return ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN).putInt(code).array();
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user