back32hash 5591: готово к тестам

Вроде как всё готово к тестам.
This commit is contained in:
AidarKC
2026-10-02 01:32:37 +03:00
parent e101e5c5f4
commit de8700f8e0
51 changed files with 337 additions and 18091 deletions
@@ -26,7 +26,7 @@ import java.time.Duration;
import java.util.*;
/**
* Discovers individual App=test5590 DataItems and imports the exact signed ANS-104 bytes.
* Discovers individual App=test5591 DataItems and imports the exact signed ANS-104 bytes.
* The importer is transport-agnostic: a DataItem may have reached Arweave through Turbo
* or inside a direct server-created ANS-104 bundle.
*/
@@ -1,6 +1,7 @@
package server.archive;
import blockchain.Ans104DataItem;
import blockchain.ShineProtocolTags;
import utils.config.AppConfig;
import java.nio.file.Path;
@@ -45,9 +46,9 @@ public record ArweaveBlocksConfig(
}
}
public static final String TEST_TAG_NAME = "App";
public static final String TEST_TAG_VALUE = "test5590";
public static final String CHANNEL_TAG_NAME = "c_test5590";
public static final String TEST_TAG_NAME = ShineProtocolTags.APP_TAG_NAME;
public static final String TEST_TAG_VALUE = ShineProtocolTags.APP_TAG_VALUE;
public static final String CHANNEL_TAG_NAME = ShineProtocolTags.CHANNEL_TAG_NAME;
public static ArweaveBlocksConfig load() {
AppConfig c = AppConfig.getInstance();
@@ -29,7 +29,7 @@ final class Ans104BundleTest {
byte[] privateKey = Ed25519Util.generatePrivateKeyFromString("bundle-" + payload);
byte[] owner = Ed25519Util.derivePublicKey(privateKey);
byte[] data = payload.getBytes(StandardCharsets.UTF_8);
List<Ans104DataItem.Tag> tags = List.of(new Ans104DataItem.Tag("App", "test5590"));
List<Ans104DataItem.Tag> tags = List.of(new Ans104DataItem.Tag(blockchain.ShineProtocolTags.APP_TAG_NAME, blockchain.ShineProtocolTags.APP_TAG_VALUE));
byte[] signature = Ed25519Util.sign(Ans104DataItem.buildSigningMessage(owner, tags, data), privateKey);
return Ans104DataItem.serializeEd25519(signature, owner, tags, data);
}
@@ -17,6 +17,7 @@ import java.util.Objects;
* SHiNE Frame v1 (all integer fields BigEndian):
* [2] frameCode = 0x0001
* [32] prevHash32 = SHA-256(Frame v1 bytes of previous SHiNE block)
* [32] back32Hash32 = SHA-256(Frame v1 bytes of block N-32), or zero for N<32
* [4] blockSize = exact Frame v1 byte length
* [4] blockNumber
* [8] timestamp unix seconds
@@ -33,10 +34,11 @@ public final class BchBlockEntry {
public static final int FRAME_CODE_V1 = 0x0001;
public static final int MAX_BLOCK_FULL_BYTES = Ans104DataItem.MAX_DATA_ITEM_BYTES;
public static final long MAX_FUTURE_SECONDS = 60;
public static final int FRAME_HEADER_SIZE = 2 + 32 + 4 + 4 + 8 + 2 + 2 + 2;
public static final int FRAME_HEADER_SIZE = 2 + 32 + 32 + 4 + 4 + 8 + 2 + 2 + 2;
public final int frameCode;
public final byte[] prevHash32;
public final byte[] back32Hash32;
public final int blockSize;
public final int blockNumber;
public final long timestamp;
@@ -66,6 +68,8 @@ public final class BchBlockEntry {
}
this.prevHash32 = new byte[32];
bb.get(prevHash32);
this.back32Hash32 = new byte[32];
bb.get(back32Hash32);
this.blockSize = bb.getInt();
if (blockSize != frameBytes.length || blockSize < FRAME_HEADER_SIZE) {
throw new IllegalArgumentException("SHiNE blockSize mismatch: declared=" + blockSize + " actual=" + frameBytes.length);
@@ -0,0 +1,17 @@
package blockchain;
/**
* Канонические ANS-104 tag names/value текущего SHiNE transport namespace на сервере.
*
* При смене тестовой эпохи namespace меняется отдельно в трёх рабочих местах:
* сервер, UI и актуальный Arweave Viewer.
*/
public final class ShineProtocolTags {
private ShineProtocolTags() {}
public static final String NAMESPACE = "test5591";
public static final String APP_TAG_NAME = "App";
public static final String APP_TAG_VALUE = NAMESPACE;
public static final String CHANNEL_TAG_NAME = "c";
public static final String TYPE_TAG_NAME = "t";
}
@@ -15,8 +15,8 @@ final class Ans104DataItemTest {
byte[] owner = Ed25519Util.derivePublicKey(privateKey);
byte[] data = "frame-v1-test".getBytes(StandardCharsets.UTF_8);
List<Ans104DataItem.Tag> tags = List.of(
new Ans104DataItem.Tag("App", "test5590"),
new Ans104DataItem.Tag("c_test5590", "books")
new Ans104DataItem.Tag(ShineProtocolTags.APP_TAG_NAME, ShineProtocolTags.APP_TAG_VALUE),
new Ans104DataItem.Tag(ShineProtocolTags.CHANNEL_TAG_NAME, "books")
);
byte[] message = Ans104DataItem.buildSigningMessage(owner, tags, data);
@@ -27,8 +27,8 @@ final class Ans104DataItemTest {
assertEquals(2, parsed.signatureType());
assertArrayEquals(owner, parsed.owner32());
assertArrayEquals(data, parsed.data());
assertTrue(parsed.hasTag("App", "test5590"));
assertEquals("books", parsed.tagValue("c_test5590"));
assertTrue(parsed.hasTag(ShineProtocolTags.APP_TAG_NAME, ShineProtocolTags.APP_TAG_VALUE));
assertEquals("books", parsed.tagValue(ShineProtocolTags.CHANNEL_TAG_NAME));
assertTrue(parsed.verifySignature());
assertEquals(32, parsed.id32().length);
}
@@ -38,7 +38,7 @@ final class Ans104DataItemTest {
byte[] privateKey = Ed25519Util.generatePrivateKeyFromString("shine-ans104-mutation-key");
byte[] owner = Ed25519Util.derivePublicKey(privateKey);
byte[] data = "original".getBytes(StandardCharsets.UTF_8);
List<Ans104DataItem.Tag> tags = List.of(new Ans104DataItem.Tag("App", "test5590"));
List<Ans104DataItem.Tag> tags = List.of(new Ans104DataItem.Tag(ShineProtocolTags.APP_TAG_NAME, ShineProtocolTags.APP_TAG_VALUE));
byte[] signature = Ed25519Util.sign(Ans104DataItem.buildSigningMessage(owner, tags, data), privateKey);
byte[] raw = Ans104DataItem.serializeEd25519(signature, owner, tags, data);
raw[raw.length - 1] ^= 1;
@@ -50,8 +50,8 @@ final class Ans104DataItemTest {
for (int i = 0; i < owner.length; i++) owner[i] = (byte) i;
byte[] data = "frame-v1-test".getBytes(StandardCharsets.UTF_8);
List<Ans104DataItem.Tag> tags = List.of(
new Ans104DataItem.Tag("App", "test5590"),
new Ans104DataItem.Tag("c_test5590", "books")
new Ans104DataItem.Tag(ShineProtocolTags.APP_TAG_NAME, ShineProtocolTags.APP_TAG_VALUE),
new Ans104DataItem.Tag(ShineProtocolTags.CHANNEL_TAG_NAME, "books")
);
byte[] actual = Ans104DataItem.buildSigningMessage(owner, tags, data);
@@ -3,6 +3,7 @@ package server.logic.ws_protocol.JSON.handlers.blockchain;
import blockchain.BchBlockEntry;
import blockchain.BchCryptoVerifier;
import blockchain.MsgSubType;
import blockchain.ShineProtocolTags;
import blockchain.body.BodyHasLine;
import blockchain.body.BodyHasTarget;
import blockchain.body.ConnectionBody;
@@ -203,6 +204,8 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
case "bad_block_number" -> "Некорректный номер блока";
case "req_global_mismatch" -> "Номер блока в запросе не совпадает с номером в блоке";
case "bad_prev_hash" -> "Некорректный prevHash (цепочка не совпадает)";
case "bad_back32_hash" -> "Некорректный back32Hash (резервная ссылка N-32 не совпадает)";
case "db_error_back32_check" -> "Ошибка БД при проверке back32Hash";
case "bad_blockchain_key_len" -> "Некорректный ключ блокчейна в состоянии (ожидалось 32 байта)";
case "signature_verify_failed" -> "Ошибка проверки подписи блока";
case "bad_signature" -> "Некорректная подпись блока";
@@ -316,13 +319,13 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
return new AddBlockResult(WireCodes.Status.BAD_REQUEST, "bad_block_format", serverLastNum, serverLastHashHex);
}
if (!block.getDataItem().hasTag("App", "test5590")) {
if (!block.getDataItem().hasTag(ShineProtocolTags.APP_TAG_NAME, ShineProtocolTags.APP_TAG_VALUE)) {
return new AddBlockResult(WireCodes.Status.BAD_REQUEST, "bad_app_tag", serverLastNum, serverLastHashHex);
}
// Signed secondary index for the fixed SHiNE streams.
String expectedTypeTag = expectedTypeTag(block);
String actualTypeTag = block.getDataItem().tagValue("t");
String actualTypeTag = block.getDataItem().tagValue(ShineProtocolTags.TYPE_TAG_NAME);
if (expectedTypeTag != null) {
if (!expectedTypeTag.equals(actualTypeTag)) {
return new AddBlockResult(WireCodes.Status.BAD_REQUEST, "bad_type_tag", serverLastNum, serverLastHashHex);
@@ -472,10 +475,10 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
channelMetaUpdateEntry.setMetaUpdatedAtMs(block.timestamp * 1000L);
}
// Channel DataItems are indexed by a signed canonical channel slug tag: c_test5590=<slug>.
// Channel DataItems are indexed by a signed canonical channel slug tag: c=<slug>.
try {
String expectedChannelSlug = expectedChannelSlug(blockchainName, block, channelNameStateEntry);
String actualChannelSlug = block.getDataItem().tagValue("c_test5590");
String actualChannelSlug = block.getDataItem().tagValue(ShineProtocolTags.CHANNEL_TAG_NAME);
if (expectedChannelSlug != null) {
if (!expectedChannelSlug.equals(actualChannelSlug)) {
return new AddBlockResult(WireCodes.Status.BAD_REQUEST, "bad_channel_tag", serverLastNum, serverLastHashHex);
@@ -551,6 +554,29 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
return new AddBlockResult(WireCodes.Status.BAD_REQUEST, "bad_prev_hash", serverLastNum, serverLastHashHex);
}
// 4.4) резервная глобальная ссылка: N<32 -> ZERO32, иначе hash блока N-32.
try {
byte[] expectedBack32 = new byte[32];
if (block.blockNumber >= 32) {
byte[] storedBack32 = blocksDAO.getHashByNumber(blockchainName, block.blockNumber - 32);
if (storedBack32 == null || storedBack32.length != 32) {
log.error("AddBlock: missing N-32 block for back32 validation (blockchainName={}, blockNumber={}, target={})",
blockchainName, block.blockNumber, block.blockNumber - 32);
return new AddBlockResult(WireCodes.Status.INTERNAL_ERROR, "db_error_back32_check", serverLastNum, serverLastHashHex);
}
expectedBack32 = storedBack32;
}
if (!Arrays.equals(block.back32Hash32, expectedBack32)) {
log.warn("AddBlock: bad_back32_hash (login={}, blockchainName={}, blockNumber={}, clientBack32={}, expectedBack32={})",
login, blockchainName, block.blockNumber, toHex(block.back32Hash32), toHex(expectedBack32));
return new AddBlockResult(WireCodes.Status.BAD_REQUEST, "bad_back32_hash", serverLastNum, serverLastHashHex);
}
} catch (Exception e) {
log.error("AddBlock: db_error_back32_check (login={}, blockchainName={}, blockNumber={})",
login, blockchainName, block.blockNumber, e);
return new AddBlockResult(WireCodes.Status.INTERNAL_ERROR, "db_error_back32_check", serverLastNum, serverLastHashHex);
}
// 5) pubKey
final byte[] pubKey32 = st.getBlockchainKeyBytes();
if (pubKey32 == null || pubKey32.length != 32) {
@@ -3,6 +3,7 @@ package server.logic.ws_protocol.JSON.handlers.keyRotation;
import blockchain.BchBlockEntry;
import blockchain.BchCryptoVerifier;
import blockchain.MsgSubType;
import blockchain.ShineProtocolTags;
import blockchain.body.ForkBody;
import server.logic.ws_protocol.Base64Ws;
import server.logic.ws_protocol.JSON.ConnectionContext;
@@ -98,17 +99,28 @@ public final class Net_KeyRotationAddBlock_Handler implements JsonMessageHandler
if (block.blockNumber != req.getBlockNumber()) {
return NetExceptionResponseFactory.error(req, 400, "KEY_ROTATION_BLOCK_NUMBER_MISMATCH", "blockNumber запроса не совпадает с блоком");
}
if (!block.getDataItem().hasTag("App", "test5590")) {
return NetExceptionResponseFactory.error(req, 400, "KEY_ROTATION_BAD_APP_TAG", "Отсутствует App=test5590");
if (!block.getDataItem().hasTag(ShineProtocolTags.APP_TAG_NAME, ShineProtocolTags.APP_TAG_VALUE)) {
return NetExceptionResponseFactory.error(req, 400, "KEY_ROTATION_BAD_APP_TAG", "Отсутствует App=" + ShineProtocolTags.APP_TAG_VALUE);
}
String expectedTypeTag = expectedTypeTag(block);
String actualTypeTag = block.getDataItem().tagValue("t");
String actualTypeTag = block.getDataItem().tagValue(ShineProtocolTags.TYPE_TAG_NAME);
if (expectedTypeTag != null ? !expectedTypeTag.equals(actualTypeTag) : actualTypeTag != null) {
return NetExceptionResponseFactory.error(req, 400, "KEY_ROTATION_BAD_TYPE_TAG", "Некорректный t-тег candidate DataItem");
}
if (!requestPrevHashMatches(req.getPrevBlockHash(), block.prevHash32)) {
return NetExceptionResponseFactory.error(req, 400, "KEY_ROTATION_PREV_HASH_MISMATCH", "prevBlockHash запроса не совпадает с блоком");
}
byte[] expectedBack32 = new byte[32];
if (block.blockNumber >= 32) {
KeyRotationCandidateBlockEntry back = candidates.getByNumber(c, session.getId(), block.blockNumber - 32);
if (back == null || back.getBlockHash() == null || back.getBlockHash().length != 32) {
return NetExceptionResponseFactory.error(req, 409, "KEY_ROTATION_BACK32_SOURCE_MISSING", "Нет candidate-блока N-32 для проверки back32Hash");
}
expectedBack32 = back.getBlockHash();
}
if (!Arrays.equals(block.back32Hash32, expectedBack32)) {
return NetExceptionResponseFactory.error(req, 400, "KEY_ROTATION_BACK32_HASH_MISMATCH", "back32Hash candidate-блока не совпадает с N-32");
}
byte[] newBlockchainKey;
try {
@@ -3,6 +3,7 @@ package server.logic.ws_protocol.JSON.handlers.keyRotation;
import blockchain.BchBlockEntry;
import blockchain.BchCryptoVerifier;
import blockchain.MsgSubType;
import blockchain.ShineProtocolTags;
import blockchain.body.ForkBody;
import server.logic.ws_protocol.JSON.ConnectionContext;
import server.logic.ws_protocol.JSON.entyties.Net_Request;
@@ -147,9 +148,9 @@ public final class Net_KeyRotationFinishChain_Handler implements JsonMessageHand
if (candidate.blockNumber != i) return "KEY_ROTATION_BLOCK_NUMBER_MISMATCH";
if (!Arrays.equals(candidate.getHash32(), stored.getBlockHash())) return "KEY_ROTATION_STORED_HASH_MISMATCH";
if (!Arrays.equals(candidate.getDataItemId32(), stored.getDataItemId())) return "KEY_ROTATION_STORED_DATAITEM_MISMATCH";
if (!candidate.getDataItem().hasTag("App", "test5590")) return "KEY_ROTATION_BAD_APP_TAG";
if (!candidate.getDataItem().hasTag(ShineProtocolTags.APP_TAG_NAME, ShineProtocolTags.APP_TAG_VALUE)) return "KEY_ROTATION_BAD_APP_TAG";
String expectedTypeTag = expectedTypeTag(candidate);
String actualTypeTag = candidate.getDataItem().tagValue("t");
String actualTypeTag = candidate.getDataItem().tagValue(ShineProtocolTags.TYPE_TAG_NAME);
if (expectedTypeTag != null ? !expectedTypeTag.equals(actualTypeTag) : actualTypeTag != null) return "KEY_ROTATION_BAD_TYPE_TAG";
if (!BchCryptoVerifier.verifyBlock(candidate, newBlockchainKey)) return "KEY_ROTATION_BAD_SIGNATURE";
@@ -160,6 +161,14 @@ public final class Net_KeyRotationFinishChain_Handler implements JsonMessageHand
return "KEY_ROTATION_CHAIN_HASH_MISMATCH";
}
}
if (i < 32) {
if (!isZero32(candidate.back32Hash32)) return "KEY_ROTATION_BACK32_HASH_MISMATCH";
} else {
byte[] expectedBack32 = entries.get(i - 32).getBlockHash();
if (expectedBack32 == null || !Arrays.equals(candidate.back32Hash32, expectedBack32)) {
return "KEY_ROTATION_BACK32_HASH_MISMATCH";
}
}
String semanticError = validateAgainstRotation(c, session, candidate);
if (semanticError != null) return semanticError;
@@ -137,7 +137,7 @@ test.freeAvatar.walletJwkPath=
# ============================================================
# Arweave per-user-block transport (ANS-104)
# Test namespace: each user DataItem is signed with App=test5590.
# Test namespace: each user DataItem is signed with App=test5591.
# Channel DataItems additionally contain c=<canonical-channel-slug>.
# ============================================================
arweave.blocks.publish.enabled=false
@@ -2,6 +2,7 @@ package test.it.blockchain;
import blockchain.Ans104DataItem;
import blockchain.BchBlockEntry;
import blockchain.ShineProtocolTags;
import blockchain.body.*;
import test.it.utils.TestConfig;
import test.it.utils.TestIds;
@@ -68,6 +69,7 @@ public final class AddBlockSender {
int blockNumber = state.nextBlockNumber();
byte[] prevHash32 = state.prevHash32ForNext();
byte[] back32Hash32 = state.back32Hash32ForNext();
long tsSec = System.currentTimeMillis() / 1000L;
short type = typeOf(body);
@@ -76,15 +78,15 @@ public final class AddBlockSender {
byte[] bodyBytes = body.toBytes();
byte[] frame = buildFrame(prevHash32, blockNumber, tsSec, type, subType, version, bodyBytes);
byte[] frame = buildFrame(prevHash32, back32Hash32, blockNumber, tsSec, type, subType, version, bodyBytes);
byte[] owner32 = utils.crypto.Ed25519Util.derivePublicKey(loginPrivKey);
List<Ans104DataItem.Tag> tags = new ArrayList<>();
tags.add(new Ans104DataItem.Tag("App", "test5590"));
tags.add(new Ans104DataItem.Tag(ShineProtocolTags.APP_TAG_NAME, ShineProtocolTags.APP_TAG_VALUE));
String typeTag = typeTagFor(type);
if (typeTag != null) tags.add(new Ans104DataItem.Tag("t", typeTag));
if (typeTag != null) tags.add(new Ans104DataItem.Tag(ShineProtocolTags.TYPE_TAG_NAME, typeTag));
String channelSlug = channelSlugFor(body);
if (channelSlug != null) tags.add(new Ans104DataItem.Tag("c_test5590", channelSlug));
if (channelSlug != null) tags.add(new Ans104DataItem.Tag(ShineProtocolTags.CHANNEL_TAG_NAME, channelSlug));
byte[] signingMessage = Ans104DataItem.buildSigningMessage(owner32, tags, frame);
byte[] signature64 = utils.crypto.Ed25519Util.sign(signingMessage, loginPrivKey);
@@ -232,6 +234,7 @@ public final class AddBlockSender {
}
private static byte[] buildFrame(byte[] prevHash32,
byte[] back32Hash32,
int blockNumber,
long tsSec,
short type,
@@ -242,12 +245,16 @@ public final class AddBlockSender {
if (prevHash32 == null || prevHash32.length != 32) {
throw new IllegalArgumentException("prevHash32 must be 32 bytes");
}
if (back32Hash32 == null || back32Hash32.length != 32) {
throw new IllegalArgumentException("back32Hash32 must be 32 bytes");
}
int bodyLen = bodyBytes == null ? 0 : bodyBytes.length;
int blockSize = BchBlockEntry.FRAME_HEADER_SIZE + bodyLen;
java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocate(blockSize).order(java.nio.ByteOrder.BIG_ENDIAN);
bb.putShort((short) BchBlockEntry.FRAME_CODE_V1);
bb.put(prevHash32);
bb.put(back32Hash32);
bb.putInt(blockSize);
bb.putInt(blockNumber);
bb.putLong(tsSec);
@@ -123,10 +123,18 @@ public final class ChainState {
}
public byte[] prevHash32ForNext() {
if (lastBlockNumber < 0) return ZERO32;
if (lastBlockNumber < 0) return ZERO32.clone();
return hexToBytes32(lastBlockHashHex);
}
public byte[] back32Hash32ForNext() {
int next = nextBlockNumber();
if (next < 32) return ZERO32.clone();
byte[] h = hash32ByNumber.get(next - 32);
if (h == null || h.length != 32) throw new IllegalStateException("Missing hash for block " + (next - 32));
return h.clone();
}
public byte[] headerHash32() {
return headerHash32 == null ? null : headerHash32.clone();
}