Переписал код кучи классов перешёл на новый надеюсь теперь подходящий формат блоков

и тесты переделал.

Но пока остались баги и тесты не проходят (в частности пользователи не создаются - ошибка в бд)
This commit is contained in:
AidarKC
2026-01-13 16:18:38 +03:00
parent b7025dde59
commit e9e05c1192
23 changed files with 1516 additions and 2379 deletions
@@ -1,93 +1,62 @@
// =======================
// shine/db/entities/BlockEntry.java (ИЗМЕНЁННАЯ под новый blocks формат)
// =======================
package shine.db.entities;
/**
* Запись блока (таблица blocks).
* Запись блока (таблица blocks) — обновлённая модель под новый формат.
*
* Храним:
* - login, bch_name (как было в проекте, чтобы не ломать общую БД)
* - block_number (глобальный номер в этой цепочке)
* - block_bytes (полный блок: preimage + signature)
* - block_hash (32 байта вычисленный SHA-256(preimage))
* - block_signature (64 байта)
*
* Опционально:
* - prev_line_number / prev_line_hash / this_line_number
*
* Плюс поля индексации (как раньше было удобно):
* - msg_type / msg_sub_type
* - to_* (если есть target)
* - edited_by_block_number (для TEXT_EDIT)
*/
public class BlockEntry {
private String login;
private String bchName;
private int blockGlobalNumber;
private byte[] blockGlobalPreHashe;
private int blockNumber;
private int blockLineIndex;
private int blockLineNumber;
private byte[] blockLinePreHashe;
private int msgType;
private int msgSubType;
private int msgType;
private int msgSubType;
private byte[] blockByte;
private byte[] blockBytes;
private String toLogin;
private String toBchName;
private Integer toBlockGlobalNumber;
private byte[] toBlockHashe;
private Integer toBlockNumber;
private byte[] toBlockHash;
// новое
private byte[] blockHash;
private byte[] blockSignature;
private Integer editedByBlockGlobalNumber;
private Integer editedByBlockNumber;
private Integer prevLineNumber;
private byte[] prevLineHash;
private Integer thisLineNumber;
public BlockEntry() {}
public BlockEntry(String login,
String bchName,
int blockGlobalNumber,
byte[] blockGlobalPreHashe,
int blockLineIndex,
int blockLineNumber,
byte[] blockLinePreHashe,
int msgType,
int msgSubType,
byte[] blockByte,
String toLogin,
String toBchName,
Integer toBlockGlobalNumber,
byte[] toBlockHashe,
byte[] blockHash,
byte[] blockSignature,
Integer editedByBlockGlobalNumber) {
this.login = login;
this.bchName = bchName;
this.blockGlobalNumber = blockGlobalNumber;
this.blockGlobalPreHashe = blockGlobalPreHashe;
this.blockLineIndex = blockLineIndex;
this.blockLineNumber = blockLineNumber;
this.blockLinePreHashe = blockLinePreHashe;
this.msgType = msgType;
this.msgSubType = msgSubType;
this.blockByte = blockByte;
this.toLogin = toLogin;
this.toBchName = toBchName;
this.toBlockGlobalNumber = toBlockGlobalNumber;
this.toBlockHashe = toBlockHashe;
this.blockHash = blockHash;
this.blockSignature = blockSignature;
this.editedByBlockGlobalNumber = editedByBlockGlobalNumber;
}
public String getLogin() { return login; }
public void setLogin(String login) { this.login = login; }
public String getBchName() { return bchName; }
public void setBchName(String bchName) { this.bchName = bchName; }
public int getBlockGlobalNumber() { return blockGlobalNumber; }
public void setBlockGlobalNumber(int blockGlobalNumber) { this.blockGlobalNumber = blockGlobalNumber; }
public byte[] getBlockGlobalPreHashe() { return blockGlobalPreHashe; }
public void setBlockGlobalPreHashe(byte[] blockGlobalPreHashe) { this.blockGlobalPreHashe = blockGlobalPreHashe; }
public int getBlockLineIndex() { return blockLineIndex; }
public void setBlockLineIndex(int blockLineIndex) { this.blockLineIndex = blockLineIndex; }
public int getBlockLineNumber() { return blockLineNumber; }
public void setBlockLineNumber(int blockLineNumber) { this.blockLineNumber = blockLineNumber; }
public byte[] getBlockLinePreHashe() { return blockLinePreHashe; }
public void setBlockLinePreHashe(byte[] blockLinePreHashe) { this.blockLinePreHashe = blockLinePreHashe; }
public int getBlockNumber() { return blockNumber; }
public void setBlockNumber(int blockNumber) { this.blockNumber = blockNumber; }
public int getMsgType() { return msgType; }
public void setMsgType(int msgType) { this.msgType = msgType; }
@@ -95,8 +64,8 @@ public class BlockEntry {
public int getMsgSubType() { return msgSubType; }
public void setMsgSubType(int msgSubType) { this.msgSubType = msgSubType; }
public byte[] getBlockByte() { return blockByte; }
public void setBlockByte(byte[] blockByte) { this.blockByte = blockByte; }
public byte[] getBlockBytes() { return blockBytes; }
public void setBlockBytes(byte[] blockBytes) { this.blockBytes = blockBytes; }
public String getToLogin() { return toLogin; }
public void setToLogin(String toLogin) { this.toLogin = toLogin; }
@@ -104,11 +73,11 @@ public class BlockEntry {
public String getToBchName() { return toBchName; }
public void setToBchName(String toBchName) { this.toBchName = toBchName; }
public Integer getToBlockGlobalNumber() { return toBlockGlobalNumber; }
public void setToBlockGlobalNumber(Integer toBlockGlobalNumber) { this.toBlockGlobalNumber = toBlockGlobalNumber; }
public Integer getToBlockNumber() { return toBlockNumber; }
public void setToBlockNumber(Integer toBlockNumber) { this.toBlockNumber = toBlockNumber; }
public byte[] getToBlockHashe() { return toBlockHashe; }
public void setToBlockHashe(byte[] toBlockHashe) { this.toBlockHashe = toBlockHashe; }
public byte[] getToBlockHash() { return toBlockHash; }
public void setToBlockHash(byte[] toBlockHash) { this.toBlockHash = toBlockHash; }
public byte[] getBlockHash() { return blockHash; }
public void setBlockHash(byte[] blockHash) { this.blockHash = blockHash; }
@@ -116,6 +85,15 @@ public class BlockEntry {
public byte[] getBlockSignature() { return blockSignature; }
public void setBlockSignature(byte[] blockSignature) { this.blockSignature = blockSignature; }
public Integer getEditedByBlockGlobalNumber() { return editedByBlockGlobalNumber; }
public void setEditedByBlockGlobalNumber(Integer editedByBlockGlobalNumber) { this.editedByBlockGlobalNumber = editedByBlockGlobalNumber; }
public Integer getEditedByBlockNumber() { return editedByBlockNumber; }
public void setEditedByBlockNumber(Integer editedByBlockNumber) { this.editedByBlockNumber = editedByBlockNumber; }
public Integer getPrevLineNumber() { return prevLineNumber; }
public void setPrevLineNumber(Integer prevLineNumber) { this.prevLineNumber = prevLineNumber; }
public byte[] getPrevLineHash() { return prevLineHash; }
public void setPrevLineHash(byte[] prevLineHash) { this.prevLineHash = prevLineHash; }
public Integer getThisLineNumber() { return thisLineNumber; }
public void setThisLineNumber(Integer thisLineNumber) { this.thisLineNumber = thisLineNumber; }
}
@@ -1,72 +1,38 @@
// =======================
// BlockchainStateEntry.java (НОВАЯ ВЕРСИЯ)
// shine/db/entities/BlockchainStateEntry.java (ИЗМЕНЁННАЯ: убраны line0..7, переименовано last_block_*)
// =======================
package shine.db.entities;
import java.util.Arrays;
import java.util.Base64;
/**
* Агрегатная сущность текущего состояния блокчейна.
* 1 строка = 1 blockchain_name, плюс состояние линий 0..7.
*
* ВАЖНО:
* - hash-поля теперь храним как byte[] и допускаем NULL:
* * NULL = "ещё не было ни одного блока" (genesis и т.п.)
* * не подменяем на new byte[0], чтобы не терять смысл
* - Убраны все поля линий line0..7 (они больше не нужны).
* - Оставляем:
* last_block_number
* last_block_hash
*
* Остальные поля (login, blockchain_key, лимиты) оставлены как в проекте,
* потому что серверу они реально нужны (ключ подписи/лимит файла).
*/
public final class BlockchainStateEntry {
private String blockchainName;
private String login;
private String blockchainKey;
private String blockchainKey; // Base64(32)
private long sizeLimit;
private long fileSizeBytes;
private int lastGlobalNumber;
private byte[] lastGlobalHash; // nullable
private final int[] lastLineNumbers = new int[8];
private final byte[][] lastLineHashes = new byte[8][]; // nullable elements
private int lastBlockNumber; // было last_global_number
private byte[] lastBlockHash; // было last_global_hash (nullable)
private long updatedAtMs;
public BlockchainStateEntry() {
// hashes остаются null по умолчанию (genesis)
}
public BlockchainStateEntry(String blockchainName,
String login,
String blockchainKey,
long sizeLimit,
long fileSizeBytes,
int lastGlobalNumber,
byte[] lastGlobalHash,
int[] lastLineNumbers,
byte[][] lastLineHashes,
long updatedAtMs) {
this.blockchainName = blockchainName;
this.login = login;
this.blockchainKey = blockchainKey;
this.sizeLimit = sizeLimit;
this.fileSizeBytes = fileSizeBytes;
this.lastGlobalNumber = lastGlobalNumber;
this.lastGlobalHash = lastGlobalHash;
if (lastLineNumbers != null) {
if (lastLineNumbers.length != 8) throw new IllegalArgumentException("lastLineNumbers must be len=8");
System.arraycopy(lastLineNumbers, 0, this.lastLineNumbers, 0, 8);
}
if (lastLineHashes != null) {
if (lastLineHashes.length != 8) throw new IllegalArgumentException("lastLineHashes must be len=8");
System.arraycopy(lastLineHashes, 0, this.lastLineHashes, 0, 8);
}
this.updatedAtMs = updatedAtMs;
}
public BlockchainStateEntry() {}
public String getBlockchainName() { return blockchainName; }
public void setBlockchainName(String blockchainName) { this.blockchainName = blockchainName; }
@@ -95,42 +61,12 @@ public final class BlockchainStateEntry {
public long getFileSizeBytes() { return fileSizeBytes; }
public void setFileSizeBytes(long fileSizeBytes) { this.fileSizeBytes = fileSizeBytes; }
public int getLastGlobalNumber() { return lastGlobalNumber; }
public void setLastGlobalNumber(int lastGlobalNumber) { this.lastGlobalNumber = lastGlobalNumber; }
public int getLastBlockNumber() { return lastBlockNumber; }
public void setLastBlockNumber(int lastBlockNumber) { this.lastBlockNumber = lastBlockNumber; }
public byte[] getLastGlobalHash() { return lastGlobalHash; }
public void setLastGlobalHash(byte[] lastGlobalHash) { this.lastGlobalHash = lastGlobalHash; }
public int getLastLineNumber(int line) {
checkLine(line);
return lastLineNumbers[line];
}
public void setLastLineNumber(int line, int value) {
checkLine(line);
lastLineNumbers[line] = value;
}
public byte[] getLastLineHash(int line) {
checkLine(line);
return lastLineHashes[line];
}
public void setLastLineHash(int line, byte[] value) {
checkLine(line);
lastLineHashes[line] = value;
}
public int[] getLastLineNumbersCopy() {
return Arrays.copyOf(lastLineNumbers, 8);
}
public byte[][] getLastLineHashesCopy() {
return Arrays.copyOf(lastLineHashes, 8);
}
public byte[] getLastBlockHash() { return lastBlockHash; }
public void setLastBlockHash(byte[] lastBlockHash) { this.lastBlockHash = lastBlockHash; }
public long getUpdatedAtMs() { return updatedAtMs; }
public void setUpdatedAtMs(long updatedAtMs) { this.updatedAtMs = updatedAtMs; }
private static void checkLine(int line) {
if (line < 0 || line > 7) throw new IllegalArgumentException("line must be 0..7");
}
}