From b6b50557a780a0722b56cf81d206d9e70f6b9972cd8b8dbd454b093ec54056e9 Mon Sep 17 00:00:00 2001 From: AidarKC Date: Mon, 29 Dec 2025 15:31:20 +0300 Subject: [PATCH] =?UTF-8?q?29=2012=2025=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20toString=20=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=BB?= =?UTF-8?q?=D0=B0=D1=81=D1=81=D0=BE=D0=B2=20Body?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/blockchain/BchBlockEntry.java | 60 ++++++++++++++++++- .../main/java/blockchain/body/HeaderBody.java | 12 ++++ .../java/blockchain/body/ReactionBody.java | 35 ++++++----- .../main/java/blockchain/body/TextBody.java | 15 +++++ 4 files changed, 106 insertions(+), 16 deletions(-) diff --git a/shine-server-blockchain/src/main/java/blockchain/BchBlockEntry.java b/shine-server-blockchain/src/main/java/blockchain/BchBlockEntry.java index ecec648..dbe629d 100644 --- a/shine-server-blockchain/src/main/java/blockchain/BchBlockEntry.java +++ b/shine-server-blockchain/src/main/java/blockchain/BchBlockEntry.java @@ -5,6 +5,7 @@ import blockchain.body.BodyRecordParser; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import java.time.Instant; import java.util.Arrays; import java.util.Objects; @@ -84,7 +85,7 @@ public final class BchBlockEntry { if (this.lineIndex != expectedLine) { throw new IllegalArgumentException( "Body is in wrong lineIndex: expected=" + expectedLine + " actual=" + this.lineIndex + - " (type=" + this.body.type() + " ver=" + this.body.version() + ")" + " (type=" + this.body.type() + " ver=" + this.body.version() + ")" ); } @@ -132,7 +133,7 @@ public final class BchBlockEntry { if (this.lineIndex != expectedLine) { throw new IllegalArgumentException( "Body is in wrong lineIndex: expected=" + expectedLine + " actual=" + this.lineIndex + - " (type=" + this.body.type() + " ver=" + this.body.version() + ")" + " (type=" + this.body.type() + " ver=" + this.body.version() + ")" ); } @@ -175,4 +176,59 @@ public final class BchBlockEntry { public byte[] toBytes() { return Arrays.copyOf(fullBytes, fullBytes.length); } + + @Override + public String toString() { + String timeIso; + try { + timeIso = Instant.ofEpochSecond(timestamp).toString(); + } catch (Exception e) { + timeIso = "некорректныйTimestamp"; + } + + return "BchBlockEntry{" + + "RAW{" + + "recordSize=" + recordSize + + ", recordNumber=" + recordNumber + + ", timestamp=" + timestamp + " (" + timeIso + ")" + + ", lineIndex=" + lineIndex + + ", lineNumber=" + lineNumber + + ", bodyLen=" + (bodyBytes == null ? -1 : bodyBytes.length) + + ", bodyType=" + (body == null ? "?" : (body.type() & 0xFFFF)) + + ", bodyVer=" + (body == null ? "?" : (body.version() & 0xFFFF)) + + "}" + + ", TAIL{" + + "signature64(hex)=" + toHex(signature64) + + ", hash32(hex)=" + toHex(hash32) + + "}" + + ", FULL{" + + "fullLen=" + (fullBytes == null ? -1 : fullBytes.length) + + ", rawLen=" + recordSize + + "}" + + ", body=" + (body == null ? "null" : body.toString()) + + ", bodyBytesPreview(hex32)=" + toHexPreview(bodyBytes, 32) + + "}"; + } + + private static String toHex(byte[] bytes) { + if (bytes == null) return "null"; + char[] HEX = "0123456789abcdef".toCharArray(); + char[] out = new char[bytes.length * 2]; + for (int i = 0; i < bytes.length; i++) { + int v = bytes[i] & 0xFF; + out[i * 2] = HEX[v >>> 4]; + out[i * 2 + 1] = HEX[v & 0x0F]; + } + return new String(out); + } + + private static String toHexPreview(byte[] bytes, int maxBytes) { + if (bytes == null) return "null"; + if (maxBytes <= 0) return ""; + int n = Math.min(bytes.length, maxBytes); + byte[] cut = Arrays.copyOf(bytes, n); + String hex = toHex(cut); + if (bytes.length > n) hex += "…(+" + (bytes.length - n) + " байт)"; + return hex; + } } \ No newline at end of file diff --git a/shine-server-blockchain/src/main/java/blockchain/body/HeaderBody.java b/shine-server-blockchain/src/main/java/blockchain/body/HeaderBody.java index e6ae7f2..4c54084 100644 --- a/shine-server-blockchain/src/main/java/blockchain/body/HeaderBody.java +++ b/shine-server-blockchain/src/main/java/blockchain/body/HeaderBody.java @@ -100,4 +100,16 @@ public final class HeaderBody implements BodyRecord { return bb.array(); } + + @Override + public String toString() { + return """ + HeaderBody { + тип записи : HEADER (type=0, ver=1) + ожидаемая линия : 0 (genesis) + тег формата : "%s" + login владельца : "%s" + } + """.formatted(tag, login); + } } \ No newline at end of file diff --git a/shine-server-blockchain/src/main/java/blockchain/body/ReactionBody.java b/shine-server-blockchain/src/main/java/blockchain/body/ReactionBody.java index 1518f63..d8595a5 100644 --- a/shine-server-blockchain/src/main/java/blockchain/body/ReactionBody.java +++ b/shine-server-blockchain/src/main/java/blockchain/body/ReactionBody.java @@ -34,7 +34,6 @@ public final class ReactionBody implements BodyRecord { public final int toBlockGlobalNumber; public final byte[] toBlockHash32; - /** Десериализация из полного bodyBytes (включая type/version). */ public ReactionBody(byte[] bodyBytes) { Objects.requireNonNull(bodyBytes, "bodyBytes == null"); if (bodyBytes.length < 4 + 4 + 1 + 1 + 4 + 32) { @@ -88,16 +87,10 @@ public final class ReactionBody implements BodyRecord { public ReactionBody check() { if (toBlockchainName == null || toBlockchainName.isBlank()) throw new IllegalArgumentException("toBlockchainName is blank"); - byte[] nameBytes = toBlockchainName.getBytes(StandardCharsets.UTF_8); - if (nameBytes.length == 0 || nameBytes.length > 255) - throw new IllegalArgumentException("toBlockchainName utf8 len must be 1..255"); - if (toBlockGlobalNumber < 0) throw new IllegalArgumentException("toBlockGlobalNumber < 0"); - if (toBlockHash32 == null || toBlockHash32.length != 32) throw new IllegalArgumentException("toBlockHash32 invalid"); - return this; } @@ -121,16 +114,30 @@ public final class ReactionBody implements BodyRecord { return bb.array(); } - /** Для записи в БД (toBlockHashe TEXT) удобно хранить hex. */ - public String toBlockHashHex() { - return toHex(toBlockHash32); + @Override + public String toString() { + return """ + ReactionBody { + тип записи : REACTION (type=2, ver=1) + ожидаемая линия : 2 + код реакции : %d + целевой блокчейн : "%s" + globalNumber цели : %d + hash цели (hex) : %s + } + """.formatted( + reactionCode, + toBlockchainName, + toBlockGlobalNumber, + toBlockHashHex() + ); } - private static String toHex(byte[] bytes) { + public String toBlockHashHex() { char[] HEX = "0123456789abcdef".toCharArray(); - char[] out = new char[bytes.length * 2]; - for (int i = 0; i < bytes.length; i++) { - int v = bytes[i] & 0xFF; + char[] out = new char[64]; + for (int i = 0; i < 32; i++) { + int v = toBlockHash32[i] & 0xFF; out[i * 2] = HEX[v >>> 4]; out[i * 2 + 1] = HEX[v & 0x0F]; } diff --git a/shine-server-blockchain/src/main/java/blockchain/body/TextBody.java b/shine-server-blockchain/src/main/java/blockchain/body/TextBody.java index 41529fb..1962a74 100644 --- a/shine-server-blockchain/src/main/java/blockchain/body/TextBody.java +++ b/shine-server-blockchain/src/main/java/blockchain/body/TextBody.java @@ -88,4 +88,19 @@ public final class TextBody implements BodyRecord { bb.put(msg); return bb.array(); } + + @Override + public String toString() { + return """ + TextBody { + тип записи : TEXT (type=1, ver=1) + ожидаемая линия : 1 + длина сообщения : %d байт + текст сообщения : "%s" + } + """.formatted( + message.getBytes(StandardCharsets.UTF_8).length, + message + ); + } } \ No newline at end of file