Дорабатываю добавление блоков. Промежуточный комит
This commit is contained in:
AidarKC 2025-12-25 14:15:26 +03:00
parent bead78b372
commit e1b2c62231
7 changed files with 2 additions and 370 deletions

View File

@ -1,62 +0,0 @@
//package utils.blockchain;
//
//import com.fasterxml.jackson.annotation.JsonCreator;
//import com.fasterxml.jackson.annotation.JsonProperty;
//import java.util.Base64;
//
///**
// * BchInfoEntry данные об одной цепочке блокчейна.
// * Используется менеджером BchInfoManager.
// */
//public final class BchInfoEntry {
//
// @JsonProperty("blockchainId")
// public final long blockchainId;
//
// @JsonProperty("userLogin")
// public final String userLogin;
//
// @JsonProperty("publicKeyBase64")
// public final String publicKeyBase64;
//
// @JsonProperty("blockchainSizeLimit")
// public final int blockchainSizeLimit;
//
// @JsonProperty("blockchainSize")
// public final int blockchainSize;
//
// @JsonProperty("lastBlockNumber")
// public final int lastBlockNumber;
//
// @JsonProperty("lastBlockHash")
// public final String lastBlockHash;
//
// @JsonCreator
// public BchInfoEntry(
// @JsonProperty("blockchainId") long blockchainId,
// @JsonProperty("userLogin") String userLogin,
// @JsonProperty("publicKeyBase64") String publicKeyBase64,
// @JsonProperty("blockchainSizeLimit") int blockchainSizeLimit,
// @JsonProperty("blockchainSize") int blockchainSize,
// @JsonProperty("lastBlockNumber") int lastBlockNumber,
// @JsonProperty("lastBlockHash") String lastBlockHash
// ) {
// this.blockchainId = blockchainId;
// this.userLogin = userLogin == null ? "" : userLogin;
// this.publicKeyBase64 = publicKeyBase64;
// this.blockchainSizeLimit = blockchainSizeLimit;
// this.blockchainSize = blockchainSize;
// this.lastBlockNumber = lastBlockNumber;
// this.lastBlockHash = lastBlockHash == null ? "" : lastBlockHash;
// }
//
// /** Публичный ключ в бинарном виде (32 байта) или null, если битый. */
// public byte[] getPublicKey32() {
// try {
// byte[] raw = Base64.getDecoder().decode(publicKeyBase64);
// return (raw != null && raw.length == 32) ? raw : null;
// } catch (IllegalArgumentException e) {
// return null;
// }
// }
//}

View File

@ -1,178 +0,0 @@
//package utils.blockchain;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.io.IOException;
//import java.nio.file.*;
//import java.util.Base64;
//import java.util.LinkedHashMap;
//import java.util.Map;
//
///**
// * BchInfoManager Singleton.
// *.
// * Держит в памяти информацию обо всех блокчейнах.
// * Сейчас читает и пишет JSON на диск (data/blockchain_info.json).
// * В будущем можно заменить на SQL без изменений в остальном коде.
// */
//public final class BchInfoManager {
//
// private static final Logger log = LoggerFactory.getLogger(BchInfoManager.class);
//
// private static final String FILE_NAME = "blockchain_info.json";
// private static final Path DATA_DIR = Paths.get("data");
// private static final ObjectMapper MAPPER = new ObjectMapper();
//
// private static BchInfoManager instance;
//
// /** blockchainId → запись о цепочке */
// private final Map<Long, BchInfoEntry> records = new LinkedHashMap<>();
// private final Path path = DATA_DIR.resolve(FILE_NAME);
//
// private BchInfoManager() {
// ensureDataDir();
// load();
// }
//
// public static synchronized BchInfoManager getInstance() {
// if (instance == null) instance = new BchInfoManager();
// return instance;
// }
//
// // ========== API ==========
//
// /** Создать новую цепочку (после первого HEADER-блока). */
// public synchronized void addBlockchain(long blockchainId,
// String userLogin,
// byte[] publicKey32,
// int blockchainSizeLimit) {
// if (publicKey32 == null || publicKey32.length != 32)
// throw new IllegalArgumentException("publicKey32 must be 32 bytes");
// if (records.containsKey(blockchainId))
// throw new IllegalArgumentException("blockchain already exists: " + blockchainId);
//
// BchInfoEntry entry = new BchInfoEntry(
// blockchainId,
// userLogin,
// Base64.getEncoder().encodeToString(publicKey32),
// blockchainSizeLimit,
// 0, 0, ""
// );
//
// records.put(blockchainId, entry);
// log.info("Добавлен блокчейн id={} login='{}' (лимит {})", blockchainId, userLogin, blockchainSizeLimit);
// save();
// }
//
// /** Обновить состояние после добавления нового блока. */
// public synchronized void updateBlockchainState(long blockchainId,
// int lastBlockNumber,
// String lastBlockHash,
// int blockchainSize) {
// BchInfoEntry prev = getEntryOrThrow(blockchainId);
//
// BchInfoEntry updated = new BchInfoEntry(
// prev.blockchainId,
// prev.userLogin,
// prev.publicKeyBase64,
// prev.blockchainSizeLimit,
// blockchainSize,
// lastBlockNumber,
// lastBlockHash
// );
//
// records.put(blockchainId, updated);
// log.info("Обновлено состояние id={} lastNum={} hash={} size={}",
// blockchainId, lastBlockNumber, lastBlockHash, blockchainSize);
// save();
// }
//
// /** Получить полную информацию по blockchainId. */
// public synchronized BchInfoEntry getBchInfo(long blockchainId) {
// return records.get(blockchainId);
// }
//
// /** Быстро проверить существование цепочки. */
// public synchronized boolean exists(long blockchainId) {
// return records.containsKey(blockchainId);
// }
//
// /** id → userLogin (для поиска пользователей). */
// public synchronized Map<Long, String> getAllLoginsSnapshot() {
// Map<Long, String> copy = new LinkedHashMap<>(records.size());
// for (var e : records.entrySet()) {
// copy.put(e.getKey(), e.getValue().userLogin);
// }
// return copy;
// }
//
// // ========== private ==========
//
// private BchInfoEntry getEntryOrThrow(long blockchainId) {
// BchInfoEntry e = records.get(blockchainId);
// if (e == null) throw new IllegalStateException("Блокчейн с id=" + blockchainId + " не найден.");
// return e;
// }
//
// private void ensureDataDir() {
// try {
// if (!Files.exists(DATA_DIR)) {
// Files.createDirectories(DATA_DIR);
// log.info("Создана директория данных: {}", DATA_DIR);
// }
// } catch (IOException e) {
// throw new IllegalStateException("Не удалось создать директорию хранения: " + DATA_DIR, e);
// }
// }
//
// private synchronized void load() {
// if (!Files.exists(path)) {
// save();
// log.info("Создан JSON-хранилище: {}", path);
// return;
// }
// try {
// byte[] json = Files.readAllBytes(path);
// if (json.length == 0) return;
//
// Map<String, BchInfoEntry> map = MAPPER.readValue(
// json,
// MAPPER.getTypeFactory().constructMapType(Map.class, String.class, BchInfoEntry.class)
// );
//
// records.clear();
// for (var e : map.entrySet()) {
// try {
// long id = Long.parseLong(e.getKey());
// records.put(id, e.getValue());
// } catch (NumberFormatException nfe) {
// log.warn("Пропущен некорректный ключ '{}' в JSON", e.getKey());
// }
// }
// log.info("Загружено {} записей из {}", records.size(), path);
// } catch (IOException e) {
// log.error("Ошибка загрузки {}", path, e);
// }
// }
//
// /** Атомарная запись JSON через .tmp + ATOMIC_MOVE */
// private synchronized void save() {
// try {
// Map<String, BchInfoEntry> map = new LinkedHashMap<>();
// for (var e : records.entrySet())
// map.put(String.valueOf(e.getKey()), e.getValue());
//
// byte[] json = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsBytes(map);
//
// Path tmp = path.resolveSibling(FILE_NAME + ".tmp");
// Files.write(tmp, json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
// Files.move(tmp, path, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
//
// log.debug("Сохранено {} записей в {}", records.size(), path);
// } catch (IOException e) {
// log.error("Ошибка сохранения {}", path, e);
// }
// }
//}

View File

@ -1,41 +0,0 @@
# utils.blockchain
Хранит состояние всех цепочек и их владельцев (логин, ключ, хэш, размер).
---
## Классы
### `BchInfoManager`
Singleton-менеджер всех известных цепочек.
Хранит карту `blockchainId → BchInfoEntry` в файле `data/blockchain_info.json`.
Обновляется после каждого добавления блока.
Главные методы:
- `getInstance()` — получить менеджер.
- `addBlockchain(id, login, key32, limit)` — создать новую цепочку (первый блок).
- `updateBlockchainState(id, num, hash, size)` — обновить состояние после блока.
- `getBchInfo(id)` — получить всю запись (`BchInfoEntry`) по цепочке.
- `getAllLoginsSnapshot()` — карта `id → login` для поиска.
---
### `BchInfoEntry`
Сущность одной цепочки:
- `blockchainId`
- `userLogin`
- `publicKeyBase64`
- `blockchainSizeLimit`
- `blockchainSize`
- `lastBlockNumber`
- `lastBlockHash`
Метод:
- `getPublicKey32()` — вернуть ключ в бинарном виде.
---
## Итого
- Менеджер хранит метаданные всех цепочек.
- Сущность описывает одну цепочку.
- Сейчас всё лежит в JSON, позже можно заменить на SQL без изменений кода.

View File

@ -1,8 +0,0 @@
В будущем всё это уйдёт в SQL.
TODO И проработать вот эту проблему
есть вариант тто при врнезапном жёстком завершении приложения, может дописаться в конец файла только половина записи и это будет жёсткой ошибкой

View File

@ -1,70 +0,0 @@
//package utils.search;
//
//
//import utils.blockchain.BchInfoManager;
//
//import java.nio.charset.StandardCharsets;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Locale;
//import java.util.Map;
//
///**
// * UserSearchService поиск первых 5 пользователей по подстроке логина (без учёта регистра).
// */
//public final class UserSearchService {
//
// private static final UserSearchService INSTANCE = new UserSearchService();
// private UserSearchService() {}
// public static UserSearchService getInstance() { return INSTANCE; }
//
// /** Результат одной пары: id + исходный login (с родным регистром). */
// public static final class Pair {
// public final long blockchainId;
// public final String userLogin;
// public Pair(long blockchainId, String userLogin) {
// this.blockchainId = blockchainId;
// this.userLogin = userLogin;
// }
// }
//
// /**
// * Найти первые до 5 логинов, содержащих подстроку (case-insensitive).
// */
// public List<Pair> searchFirst5(String query) {
// String q = (query == null ? "" : query).toLowerCase(Locale.ROOT).trim();
// List<Pair> out = new ArrayList<>(5);
// if (q.isEmpty()) return out;
//
// // берём снапшот idlogin
// Map<Long, String> all = BchInfoManager.getInstance().getAllLoginsSnapshot();
//
// for (var e : all.entrySet()) {
// if (out.size() >= 5) break;
// String login = e.getValue() == null ? "" : e.getValue();
// if (login.toLowerCase(Locale.ROOT).contains(q)) {
// out.add(new Pair(e.getKey(), login));
// }
// }
// return out;
// }
//
// // Упаковка пары в байтовый формат ответа: [8] id + [1] L + [L] login UTF-8 (L<=255)
// public static byte[] packPair(Pair p) {
// byte[] loginUtf8 = (p.userLogin == null ? "" : p.userLogin).getBytes(StandardCharsets.UTF_8);
// int L = Math.min(loginUtf8.length, 255);
// byte[] b = new byte[8 + 1 + L];
// // beLong
// b[0]=(byte)((p.blockchainId>>>56)&0xFF);
// b[1]=(byte)((p.blockchainId>>>48)&0xFF);
// b[2]=(byte)((p.blockchainId>>>40)&0xFF);
// b[3]=(byte)((p.blockchainId>>>32)&0xFF);
// b[4]=(byte)((p.blockchainId>>>24)&0xFF);
// b[5]=(byte)((p.blockchainId>>>16)&0xFF);
// b[6]=(byte)((p.blockchainId>>>8 )&0xFF);
// b[7]=(byte)((p.blockchainId )&0xFF);
// b[8]=(byte)L;
// System.arraycopy(loginUtf8, 0, b, 9, L);
// return b;
// }
//}

View File

@ -91,7 +91,7 @@ public final class BlockchainStateDAO {
line6_last_number, line6_last_hash, line6_last_number, line6_last_hash,
line7_last_number, line7_last_hash line7_last_number, line7_last_hash
) VALUES ( ) VALUES (
?,?,?,?,?,?,?,?,?, ?,?,?,?,?,?,?,?,
?,?, ?,?,
?,?, ?,?,
?,?, ?,?,
@ -136,7 +136,6 @@ public final class BlockchainStateDAO {
ps.setString(i++, nn(e.getLogin())); ps.setString(i++, nn(e.getLogin()));
ps.setString(i++, nn(e.getPublicKeyBase64())); ps.setString(i++, nn(e.getPublicKeyBase64()));
ps.setInt(i++, e.getSizeLimit()); ps.setInt(i++, e.getSizeLimit());
ps.setInt(i++, e.getSizeBytes());
ps.setLong(i++, e.getFileSizeBytes()); ps.setLong(i++, e.getFileSizeBytes());
ps.setInt(i++, e.getLastGlobalNumber()); ps.setInt(i++, e.getLastGlobalNumber());
ps.setString(i++, nn(e.getLastGlobalHash())); ps.setString(i++, nn(e.getLastGlobalHash()));
@ -159,7 +158,6 @@ public final class BlockchainStateDAO {
e.setPublicKeyBase64(rs.getString("public_key_base64")); e.setPublicKeyBase64(rs.getString("public_key_base64"));
e.setSizeLimit(rs.getInt("size_limit")); e.setSizeLimit(rs.getInt("size_limit"));
e.setSizeBytes(rs.getInt("size_bytes"));
e.setFileSizeBytes(rs.getLong("file_size_bytes")); e.setFileSizeBytes(rs.getLong("file_size_bytes"));
e.setLastGlobalNumber(rs.getInt("last_global_number")); e.setLastGlobalNumber(rs.getInt("last_global_number"));

View File

@ -14,9 +14,7 @@ public final class BlockchainStateEntry {
private String publicKeyBase64; private String publicKeyBase64;
private int sizeLimit; private int sizeLimit;
private int sizeBytes; /** Размер файла блокчейна в байтах (то, что будем сверять/чинить при старте). */
/** NEW: размер файла блокчейна в байтах (то, что будем сверять/чинить при старте). */
private long fileSizeBytes; private long fileSizeBytes;
private int lastGlobalNumber; private int lastGlobalNumber;
@ -39,7 +37,6 @@ public final class BlockchainStateEntry {
String login, String login,
String publicKeyBase64, String publicKeyBase64,
int sizeLimit, int sizeLimit,
int sizeBytes,
long fileSizeBytes, long fileSizeBytes,
int lastGlobalNumber, int lastGlobalNumber,
String lastGlobalHash, String lastGlobalHash,
@ -50,7 +47,6 @@ public final class BlockchainStateEntry {
this.login = login; this.login = login;
this.publicKeyBase64 = publicKeyBase64; this.publicKeyBase64 = publicKeyBase64;
this.sizeLimit = sizeLimit; this.sizeLimit = sizeLimit;
this.sizeBytes = sizeBytes;
this.fileSizeBytes = fileSizeBytes; this.fileSizeBytes = fileSizeBytes;
this.lastGlobalNumber = lastGlobalNumber; this.lastGlobalNumber = lastGlobalNumber;
this.lastGlobalHash = lastGlobalHash == null ? "" : lastGlobalHash; this.lastGlobalHash = lastGlobalHash == null ? "" : lastGlobalHash;
@ -81,9 +77,6 @@ public final class BlockchainStateEntry {
public int getSizeLimit() { return sizeLimit; } public int getSizeLimit() { return sizeLimit; }
public void setSizeLimit(int sizeLimit) { this.sizeLimit = sizeLimit; } public void setSizeLimit(int sizeLimit) { this.sizeLimit = sizeLimit; }
public int getSizeBytes() { return sizeBytes; }
public void setSizeBytes(int sizeBytes) { this.sizeBytes = sizeBytes; }
public long getFileSizeBytes() { return fileSizeBytes; } public long getFileSizeBytes() { return fileSizeBytes; }
public void setFileSizeBytes(long fileSizeBytes) { this.fileSizeBytes = fileSizeBytes; } public void setFileSizeBytes(long fileSizeBytes) { this.fileSizeBytes = fileSizeBytes; }