Промежуточная версия и ТУДУ на чём остановился
This commit is contained in:
AidarKC
2025-12-16 17:56:36 +03:00
parent 19c4fd6cd1
commit ab44cc5282
30 changed files with 2511 additions and 3 deletions
@@ -0,0 +1,19 @@
package blockchain.body;
/**
* Общий интерфейс для всех тел (body) блоков.
*.
* Каждый тип тела реализует:
* - check() — проверку корректности данных
* - toBytes() — опциональную сериализацию обратно в байты
*/
public interface BodyRecord {
/** Проверить корректность содержимого. */
BodyRecord check();
/** (опционально) Сериализация тела обратно в байты. */
default byte[] toBytes() {
throw new UnsupportedOperationException("toBytes() не реализован");
}
}
@@ -0,0 +1,36 @@
package blockchain.body;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
/**
* BodyRecordParser_new — общий фабричный парсер body для нового формата.
*
* Правило совместимости (строгое):
* - если (type, version) неизвестны → кидаем IllegalArgumentException
*/
public final class BodyRecordParser_new {
private BodyRecordParser_new() {}
public static BodyRecord_new parse(byte[] bodyBytes) {
if (bodyBytes == null) throw new IllegalArgumentException("bodyBytes == null");
if (bodyBytes.length < 4) throw new IllegalArgumentException("bodyBytes too short (<4)");
ByteBuffer bb = ByteBuffer.wrap(bodyBytes).order(ByteOrder.BIG_ENDIAN);
short type = bb.getShort();
short ver = bb.getShort();
// Строгое сопоставление type+version → класс
int key = ((type & 0xFFFF) << 16) | (ver & 0xFFFF);
return switch (key) {
case 0x0000_0001 -> new HeaderBody_new(bodyBytes); // type=0, ver=1
case 0x0001_0001 -> new TextBody_new(bodyBytes); // type=1, ver=1
default -> throw new IllegalArgumentException(String.format(
"Unknown body type/version: type=%d ver=%d (key=0x%08X)",
(type & 0xFFFF), (ver & 0xFFFF), key
));
};
}
}
@@ -0,0 +1,38 @@
package blockchain.body;
/**
* BodyRecord_new — общий контракт для всех типов body (тела блока).
*
* Идея:
* - На каждый тип body (Header, Text, File, ...) — отдельный класс.
* - Десериализация из байтов делается КОНСТРУКТОРОМ:
* new XxxBody_new(byte[] bodyBytes)
* (конструктор обязан распарсить байты или кинуть IllegalArgumentException).
*
* - Валидация делается методом check().
* check() должен:
* - вернуть this, если всё корректно
* - кинуть IllegalArgumentException, если данные некорректны
*
* - Сериализация обратно в байты делается методом toBytes().
*
* - type() и version() — это идентификаторы формата body.
* Они должны быть константами для класса (например TYPE=1, VERSION=1).
*/
public interface BodyRecord_new {
/** Код типа записи (совпадает с recordType в BchBlockEntry). */
short type();
/** Версия формата записи (совпадает с recordTypeVersion в BchBlockEntry). */
short version();
/** Проверить корректность содержимого и вернуть этот объект (или кинуть исключение). */
BodyRecord_new check();
/**
* Сериализовать тело записи в байты (ровно то, что кладётся в block.body).
* Важно: НЕ включает общий заголовок блока (recordNumber/timestamp/type/version).
*/
byte[] toBytes();
}
@@ -0,0 +1,191 @@
package blockchain.body;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Objects;
/**
* ============================================================================
* HeaderBody — тело записи типа 0 (заглавие блокчейна)
* ============================================================================
*.
* 🧩 Назначение:
* Первый блок каждой пользовательской цепочки (.bch) — это "заголовок".
* Он хранит базовую информацию о владельце, версии и публичном ключе.
*.
* Этот блок всегда имеет:
* • recordType = 0
* • recordNumber = 0
* • recordTypeVersion = 1
*.
* ----------------------------------------------------------------------------
* 🔹 Формат body (без общих 20 байт заголовка блока BchBlock)
*.
* | Смещение | Размер | Поле | Формат | Описание |
* |-----------|--------|--------------------|---------|-----------|
* | 0x00 | 8 | tag | ASCII | Статическая сигнатура "SHiNE001" |
* | 0x08 | 8 | blockchainId | long BE | Уникальный идентификатор цепочки |
* | 0x10 | 1 | userLoginLength=N | uint8 | Длина логина пользователя |
* | 0x11 | N | userLogin | UTF-8 | Логин пользователя |
* | 0x11+N | 4 | blockchainType | int BE | Зарезервировано (всегда 0) |
* | 0x15+N | 4 | blockchainNumber | int BE | Зарезервировано (всегда 0) |
* | 0x19+N | 2 | versionUserBch | short BE| Версия формата (всегда 1) |
* | 0x1B+N | 8 | prevUserBchId | long BE | Зарезервировано (всегда 0) |
* | 0x23+N | 32 | publicKey32 | raw | Публичный ключ (Ed25519, 32 байта) |
*.
* ----------------------------------------------------------------------------
* 💡 Пример структуры в байтах:
*.
* 0000: 53 48 69 4E 45 30 30 31 "SHiNE001"
* 0008: 00 00 00 00 01 23 45 67 blockchainId
* 0010: 05 userLoginLength = 5
* 0011: 41 69 64 61 72 userLogin = "Aidar"
* 0016: 00 00 00 00 blockchainType = 0
* 001A: 00 00 00 00 blockchainNumber = 0
* 001E: 00 01 versionUserBch = 1
* 0020: 00 00 00 00 00 00 00 00 prevUserBchId = 0
* 0028: [32 байта публичного ключа]
*.
* ----------------------------------------------------------------------------
* 📘 Замечания:
* • Поля blockchainType, blockchainNumber, versionUserBch, prevUserBchId
* зарезервированы для будущего расширения формата.
* • На данный момент все они принимают фиксированные значения:
* blockchainType = 0
* blockchainNumber = 0
* versionUserBch = 1
* prevUserBchId = 0
*.
* ============================================================================
*/
public final class HeaderBody implements BodyRecord {
public static final short TYPE = 0;
public static final String TAG = "SHiNE001";
public static final int PUBKEY_LEN = 32;
public final String tag; // всегда "SHiNE001"
public final long blockchainId;
public final String userLogin; // UTF-8
public final int blockchainType; // пока 0
public final int blockchainNumber; // пока 0
public final short versionUserBch; // пока 1
public final long prevUserBchId; // пока 0
public final byte[] publicKey32; // 32 байта
// ------------------------------------------------------------
// Конструктор №1 — из массива байт (для парсинга существующего блока)
// ------------------------------------------------------------
public HeaderBody(byte[] body) {
Objects.requireNonNull(body, "body == null");
if (body.length < 8 + 8 + 1 + 2 + 4 + 4 + 8 + 32)
throw new IllegalArgumentException("HeaderBody слишком короткое");
ByteBuffer buf = ByteBuffer.wrap(body).order(ByteOrder.BIG_ENDIAN);
// [8] тег
byte[] tagBytes = new byte[8];
buf.get(tagBytes);
String tag = new String(tagBytes, StandardCharsets.US_ASCII);
if (!TAG.equals(tag))
throw new IllegalArgumentException("Неверный тег: " + tag);
this.tag = tag;
// [8] blockchainId
this.blockchainId = buf.getLong();
// [1] длина логина
int loginLen = Byte.toUnsignedInt(buf.get());
if (loginLen == 0 || buf.remaining() < loginLen + 4 + 4 + 2 + 8 + 32)
throw new IllegalArgumentException("Некорректная длина логина");
// [N] логин
byte[] loginBytes = new byte[loginLen];
buf.get(loginBytes);
this.userLogin = new String(loginBytes, StandardCharsets.UTF_8);
// Остальные поля
this.blockchainType = buf.getInt();
this.blockchainNumber = buf.getInt();
this.versionUserBch = buf.getShort();
this.prevUserBchId = buf.getLong();
this.publicKey32 = new byte[PUBKEY_LEN];
buf.get(this.publicKey32);
}
// ------------------------------------------------------------
// Конструктор №2 — из параметров (для создания нового заголовка)
// ------------------------------------------------------------
public HeaderBody(long blockchainId, String userLogin,
int blockchainType, int blockchainNumber,
short versionUserBch, long prevUserBchId,
byte[] publicKey32) {
Objects.requireNonNull(userLogin, "userLogin == null");
Objects.requireNonNull(publicKey32, "publicKey32 == null");
if (publicKey32.length != PUBKEY_LEN)
throw new IllegalArgumentException("Публичный ключ должен состоять из 32 байт");
this.tag = TAG;
this.blockchainId = blockchainId;
this.userLogin = userLogin;
this.blockchainType = blockchainType;
this.blockchainNumber = blockchainNumber;
this.versionUserBch = versionUserBch;
this.prevUserBchId = prevUserBchId;
this.publicKey32 = Arrays.copyOf(publicKey32, PUBKEY_LEN);
}
// ------------------------------------------------------------
// Проверка и сериализация
// ------------------------------------------------------------
@Override
public HeaderBody check() {
if (userLogin == null || userLogin.isBlank())
throw new IllegalArgumentException("Логин не может быть пустым");
if (!userLogin.matches("^[A-Za-z0-9_]+$"))
throw new IllegalArgumentException("Логин может содержать только латиницу, цифры и _");
if (publicKey32 == null || publicKey32.length != PUBKEY_LEN)
throw new IllegalArgumentException("Публичный ключ должен быть 32 байта");
return this;
}
@Override
public byte[] toBytes() {
byte[] loginUtf8 = userLogin.getBytes(StandardCharsets.UTF_8);
if (loginUtf8.length > 255)
throw new IllegalArgumentException("Логин слишком длинный (>255 байт)");
int cap = 8 + 8 + 1 + loginUtf8.length + 4 + 4 + 2 + 8 + 32;
ByteBuffer buf = ByteBuffer.allocate(cap).order(ByteOrder.BIG_ENDIAN);
buf.put(TAG.getBytes(StandardCharsets.US_ASCII)); // [8]
buf.putLong(blockchainId); // [8]
buf.put((byte) loginUtf8.length); // [1]
buf.put(loginUtf8); // [N]
buf.putInt(blockchainType); // [4]
buf.putInt(blockchainNumber); // [4]
buf.putShort(versionUserBch); // [2]
buf.putLong(prevUserBchId); // [8]
buf.put(publicKey32); // [32]
return buf.array();
}
@Override
public String toString() {
return "HeaderBody{" +
"id=" + blockchainId +
", login='" + userLogin + '\'' +
", type=" + blockchainType +
", num=" + blockchainNumber +
", ver=" + versionUserBch +
", prev=" + prevUserBchId +
", pubkey32=" + Arrays.toString(Arrays.copyOf(publicKey32, 4)) + "..." +
'}';
}
}
@@ -0,0 +1,155 @@
package blockchain.body;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Objects;
/**
* HeaderBody_new — type=0, version=1.
*
* Полный bodyBytes:
* [2] type=0
* [2] version=1
* [payload...]
*
* Payload (как у текущего HeaderBody):
* [8] tag ASCII "SHiNE001"
* [8] blockchainId (long BE)
* [1] loginLength=N (uint8)
* [N] userLogin UTF-8
* [4] blockchainType (int BE) (резерв)
* [4] blockchainNumber (int BE) (резерв)
* [2] versionUserBch (short BE) (резерв)
* [8] prevUserBchId (long BE) (резерв)
* [32] publicKey32 (raw)
*/
public final class HeaderBody_new implements BodyRecord_new {
public static final short TYPE = 0;
public static final short VER = 1;
public static final String TAG = "SHiNE001";
public static final int PUBKEY_LEN = 32;
public final String tag; // "SHiNE001"
public final long blockchainId;
public final String userLogin;
public final int blockchainType;
public final int blockchainNumber;
public final short versionUserBch;
public final long prevUserBchId;
public final byte[] publicKey32;
/**
* Десериализация из полного bodyBytes (ВКЛЮЧАЯ первые 4 байта type/version).
*/
public HeaderBody_new(byte[] bodyBytes) {
Objects.requireNonNull(bodyBytes, "bodyBytes == null");
if (bodyBytes.length < 4) throw new IllegalArgumentException("HeaderBody_new too short");
ByteBuffer bb = ByteBuffer.wrap(bodyBytes).order(ByteOrder.BIG_ENDIAN);
short type = bb.getShort();
short ver = bb.getShort();
if (type != TYPE || ver != VER)
throw new IllegalArgumentException("Not HeaderBody_new: type=" + type + " ver=" + ver);
// Теперь bb стоит на payload
if (bb.remaining() < 8 + 8 + 1 + 4 + 4 + 2 + 8 + 32)
throw new IllegalArgumentException("Header payload too short");
byte[] tagBytes = new byte[8];
bb.get(tagBytes);
String t = new String(tagBytes, StandardCharsets.US_ASCII);
if (!TAG.equals(t)) throw new IllegalArgumentException("Bad tag: " + t);
this.tag = t;
this.blockchainId = bb.getLong();
int loginLen = Byte.toUnsignedInt(bb.get());
if (loginLen <= 0 || bb.remaining() < loginLen + 4 + 4 + 2 + 8 + 32)
throw new IllegalArgumentException("Bad login length");
byte[] loginBytes = new byte[loginLen];
bb.get(loginBytes);
this.userLogin = new String(loginBytes, StandardCharsets.UTF_8);
this.blockchainType = bb.getInt();
this.blockchainNumber = bb.getInt();
this.versionUserBch = bb.getShort();
this.prevUserBchId = bb.getLong();
this.publicKey32 = new byte[PUBKEY_LEN];
bb.get(this.publicKey32);
}
/**
* Создание “вручную” (для генерации первого блока).
*/
public HeaderBody_new(long blockchainId,
String userLogin,
int blockchainType,
int blockchainNumber,
short versionUserBch,
long prevUserBchId,
byte[] publicKey32) {
Objects.requireNonNull(userLogin, "userLogin == null");
Objects.requireNonNull(publicKey32, "publicKey32 == null");
if (publicKey32.length != PUBKEY_LEN)
throw new IllegalArgumentException("publicKey32 must be 32 bytes");
this.tag = TAG;
this.blockchainId = blockchainId;
this.userLogin = userLogin;
this.blockchainType = blockchainType;
this.blockchainNumber = blockchainNumber;
this.versionUserBch = versionUserBch;
this.prevUserBchId = prevUserBchId;
this.publicKey32 = Arrays.copyOf(publicKey32, PUBKEY_LEN);
}
@Override public short type() { return TYPE; }
@Override public short version() { return VER; }
@Override
public HeaderBody_new check() {
if (userLogin == null || userLogin.isBlank())
throw new IllegalArgumentException("Login is blank");
if (!userLogin.matches("^[A-Za-z0-9_]+$"))
throw new IllegalArgumentException("Login must match ^[A-Za-z0-9_]+$");
if (publicKey32 == null || publicKey32.length != PUBKEY_LEN)
throw new IllegalArgumentException("publicKey32 must be 32 bytes");
return this;
}
@Override
public byte[] toBytes() {
byte[] loginUtf8 = userLogin.getBytes(StandardCharsets.UTF_8);
if (loginUtf8.length > 255)
throw new IllegalArgumentException("Login too long (>255 bytes)");
int payloadCap = 8 + 8 + 1 + loginUtf8.length + 4 + 4 + 2 + 8 + 32;
int cap = 4 + payloadCap;
ByteBuffer bb = ByteBuffer.allocate(cap).order(ByteOrder.BIG_ENDIAN);
// [type/version]
bb.putShort(TYPE);
bb.putShort(VER);
// payload
bb.put(TAG.getBytes(StandardCharsets.US_ASCII)); // [8]
bb.putLong(blockchainId); // [8]
bb.put((byte) loginUtf8.length); // [1]
bb.put(loginUtf8); // [N]
bb.putInt(blockchainType); // [4]
bb.putInt(blockchainNumber); // [4]
bb.putShort(versionUserBch); // [2]
bb.putLong(prevUserBchId); // [8]
bb.put(publicKey32); // [32]
return bb.array();
}
}
@@ -0,0 +1,77 @@
package blockchain.body;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
/**
* TextBody — тело записи типа 1 (простое текстовое сообщение).
*.
* Формат body:
* [N] message (UTF-8)
*.
* Тело полностью состоит из UTF-8-строки без каких-либо метаданных.
*/
public final class TextBody implements BodyRecord {
public static final short TYPE = 1;
public final String message;
// ------------------------------------------------------------
// Конструктор №1 — из массива байт (для парсинга)
// ------------------------------------------------------------
public TextBody(byte[] body) {
Objects.requireNonNull(body, "body == null");
if (body.length == 0)
throw new IllegalArgumentException("Тело текстового сообщения пустое");
// строгая проверка валидности UTF-8
var decoder = StandardCharsets.UTF_8
.newDecoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);
try {
var chars = decoder.decode(ByteBuffer.wrap(body));
this.message = chars.toString();
} catch (CharacterCodingException e) {
throw new IllegalArgumentException("Тело не является корректным UTF-8", e);
}
}
// ------------------------------------------------------------
// Конструктор №2 — из строки (для создания нового сообщения)
// ------------------------------------------------------------
public TextBody(String message) {
Objects.requireNonNull(message, "message == null");
if (message.isBlank())
throw new IllegalArgumentException("Текст сообщения не может быть пустым");
this.message = message;
}
// ------------------------------------------------------------
// Проверка и сериализация
// ------------------------------------------------------------
@Override
public TextBody check() {
if (message == null || message.isBlank())
throw new IllegalArgumentException("Текст сообщения не может быть пустым");
return this;
}
@Override
public byte[] toBytes() {
return message.getBytes(StandardCharsets.UTF_8);
}
@Override
public String toString() {
return "TextBody{" +
"len=" + message.length() +
", msg='" + (message.length() > 60 ? message.substring(0, 57) + "..." : message) + '\'' +
'}';
}
}
@@ -0,0 +1,89 @@
package blockchain.body;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
/**
* TextBody_new — type=1, version=1.
*
* Полный bodyBytes:
* [2] type=1
* [2] version=1
* [payload...]
*
* Payload:
* UTF-8 bytes (N>0)
*/
public final class TextBody_new implements BodyRecord_new {
public static final short TYPE = 1;
public static final short VER = 1;
public final String message;
/** Десериализация из полного bodyBytes (включая type/version). */
public TextBody_new(byte[] bodyBytes) {
Objects.requireNonNull(bodyBytes, "bodyBytes == null");
if (bodyBytes.length < 5) // минимум: 4 байта type/ver + 1 байт текста
throw new IllegalArgumentException("TextBody_new too short");
ByteBuffer bb = ByteBuffer.wrap(bodyBytes).order(ByteOrder.BIG_ENDIAN);
short type = bb.getShort();
short ver = bb.getShort();
if (type != TYPE || ver != VER)
throw new IllegalArgumentException("Not TextBody_new: type=" + type + " ver=" + ver);
byte[] payload = new byte[bb.remaining()];
bb.get(payload);
// строгая проверка UTF-8
var decoder = StandardCharsets.UTF_8
.newDecoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);
try {
this.message = decoder.decode(ByteBuffer.wrap(payload)).toString();
} catch (CharacterCodingException e) {
throw new IllegalArgumentException("Text payload is not valid UTF-8", e);
}
if (this.message.isBlank())
throw new IllegalArgumentException("Text message is blank");
}
/** Создание из строки. */
public TextBody_new(String message) {
Objects.requireNonNull(message, "message == null");
if (message.isBlank())
throw new IllegalArgumentException("message is blank");
this.message = message;
}
@Override public short type() { return TYPE; }
@Override public short version() { return VER; }
@Override
public TextBody_new check() {
if (message == null || message.isBlank())
throw new IllegalArgumentException("Text message is blank");
return this;
}
@Override
public byte[] toBytes() {
byte[] msg = message.getBytes(StandardCharsets.UTF_8);
if (msg.length == 0)
throw new IllegalArgumentException("Text payload is empty");
ByteBuffer bb = ByteBuffer.allocate(4 + msg.length).order(ByteOrder.BIG_ENDIAN);
bb.putShort(TYPE);
bb.putShort(VER);
bb.put(msg);
return bb.array();
}
}