Новый протокол Solana PDA 1.2

This commit is contained in:
AidarKC
2026-09-26 11:01:14 +03:00
parent 6e5b57fd7c
commit c997057a37
47 changed files with 2392 additions and 4480 deletions
+7 -15
View File
@@ -18,15 +18,11 @@ SHiNE-server — серверная часть мессенджера SHiNE: Web
## Настройка сервера в Solana (Solana PDA)
Серверный аккаунт SHiNE регистрируется в Solana в виде `user_pda` с флагом `is_server=true`.
В PDA хранятся:
Серверный аккаунт SHiNE регистрируется как обычный `user_pda` формата 1.2 с присутствующим `ServerProfileBlock`. Отдельного `is_server` в PDA нет: наличие server profile означает, что аккаунт объявляет серверный endpoint.
- **адрес сервера** (URL WebSocket/HTTPS, например `https://shineup.me/ws`);
- **список серверов синхронизации** (`sync_servers`) — логины SHiNE-аккаунтов серверов-партнёров,
с которыми синхронизируются пользовательские блокчейны;
- **корневой ключ** сервера (`root_key`).
PDA 1.2 сейчас допускает **один** адрес сервера (`address_format_type + address_format_version + address`). Бинарный блок использует count/array, чтобы будущая версия протокола могла увеличить лимит без смены структуры блока. Прямой список `sync_servers` удалён: пользовательские blockchain синхронизируются через Arweave.
Клиенты читают PDA напрямую из Solana, чтобы узнать адрес сервера и при необходимости подключиться.
Клиенты читают server PDA из Solana, получают опубликованный endpoint и подключаются к нему. Межсерверная доставка DM через access-server routing остаётся отдельным механизмом.
**Управление серверной PDA выполняется через Web-панель администратора:**
@@ -36,10 +32,9 @@ shine-UI/server-ui.html
Страницы:
- `shine-UI/server-ui/create-server-pda.html` — первичная регистрация серверного аккаунта;
- `shine-UI/server-ui/update-server-pda.html` — обновление адреса или списка sync_servers.
- `shine-UI/server-ui/update-server-pda.html` — обновление адреса сервера.
Для регистрации нужен полный keyBundle (root + device + blockchain).
Для обновления — только root + device (blockchain-ключ не нужен).
PDA 1.2 использует три постоянные роли ключей: `root` (cold recovery), `blockchain` (обычный authority и подпись блоков) и `client` (клиент/кошелёк). Обычный update авторизуется активным последним blockchain key; root используется для recovery и может менять root.
Актуальные адреса программ Solana (devnet):
- `shine_users`: `SHiNEPr1APdAgNBteUyBXcNovaHctpSjUu8oH2ZJdN6`
@@ -47,12 +42,9 @@ shine-UI/server-ui.html
Подробнее: `docs/Инициализация_Solana_регистрации/README.md`
## Синхронизация с партнёрскими серверами
## Синхронизация пользовательских blockchain
Сервер должен синхронизировать блоки пользовательских блокчейнов с
серверами-партнёрами из `sync_servers`. DM между партнёрами не реплицируются:
они доставляются только на первый access-сервер получателя.
Детали: `docs/Blockchain/sync-between-servers.md`
Прямой peer-to-peer blockchain sync через `sync_servers` отключён. Сервер восстанавливает и синхронизирует пользовательские записи через Arweave. Код старого peer-sync пока может оставаться как inert legacy, но не должен включаться в runtime.
## Деплой
@@ -23,6 +23,46 @@ public final class KeyEncodingUtil {
private KeyEncodingUtil() {}
public static String encodeBase58(byte[] input) {
if (input == null || input.length == 0) {
return "";
}
byte[] copy = java.util.Arrays.copyOf(input, input.length);
int zeros = 0;
while (zeros < copy.length && copy[zeros] == 0) {
zeros++;
}
byte[] encoded = new byte[copy.length * 2];
int outputStart = encoded.length;
int inputStart = zeros;
while (inputStart < copy.length) {
int remainder = divmod58(copy, inputStart);
if (copy[inputStart] == 0) {
inputStart++;
}
encoded[--outputStart] = (byte) BASE58_ALPHABET.charAt(remainder);
}
while (outputStart < encoded.length && encoded[outputStart] == (byte) BASE58_ALPHABET.charAt(0)) {
outputStart++;
}
while (--zeros >= 0) {
encoded[--outputStart] = (byte) BASE58_ALPHABET.charAt(0);
}
return new String(encoded, outputStart, encoded.length - outputStart, java.nio.charset.StandardCharsets.US_ASCII);
}
public static String base58KeyToBase64_32(String rawKey) {
if (rawKey == null) {
return null;
}
String value = rawKey.trim();
if (value.isEmpty()) {
return value;
}
byte[] decoded = tryDecodeBase58_32(value);
return decoded == null ? value : Base64.getEncoder().encodeToString(decoded);
}
public static String normalizeKeyToBase64_32(String rawKey) {
if (rawKey == null) {
return null;
@@ -102,6 +142,17 @@ public final class KeyEncodingUtil {
return result;
}
private static int divmod58(byte[] number, int startAt) {
int remainder = 0;
for (int i = startAt; i < number.length; i++) {
int digit256 = number[i] & 0xFF;
int temp = remainder * 256 + digit256;
number[i] = (byte) (temp / 58);
remainder = temp % 58;
}
return remainder;
}
private static int divmod256(byte[] number58, int startAt) {
int remainder = 0;
for (int i = startAt; i < number58.length; i++) {
@@ -8,7 +8,6 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Base64;
/**
* Доступ к актуальной PostgreSQL-таблице пользователей, поддерживаемой sync-модулем.
@@ -61,28 +60,35 @@ public final class SolanaUserPdaCurrentDAO {
}
}
/** Resolve a current user by the 32-byte Ed25519 blockchain key used as ANS-104 owner. */
/** Resolve a user/fork by the 32-byte Ed25519 blockchain key used as ANS-104 owner. */
public SolanaUserPdaCurrentEntry getByBlockchainKey(byte[] owner32) throws SQLException {
if (owner32 == null || owner32.length != 32) return null;
String wanted = Base64.getEncoder().encodeToString(owner32);
String wanted = KeyEncodingUtil.encodeBase58(owner32);
String sql = """
SELECT login, blockchain_name, blockchain_key, paid_limit_bytes
FROM solana_user_pda_current
SELECT
u.login,
u.login || '-' || LPAD(((fork_item.value ->> 'forkIndex')::integer + 1)::text, 3, '0') AS blockchain_name,
fork_item.value ->> 'blockchainKey' AS blockchain_key,
(fork_item.value ->> 'paidLimitBytes')::bigint AS paid_limit_bytes
FROM solana_user_pda_current u
CROSS JOIN LATERAL jsonb_array_elements(COALESCE(NULLIF(u.blockchain_forks_json, ''), '[]')::jsonb) AS fork_item(value)
WHERE fork_item.value ->> 'blockchainKey' = ?
LIMIT 1
""";
try (Connection c = db.getConnection(); PreparedStatement ps = c.prepareStatement(sql); ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
SolanaUserPdaCurrentEntry e = mapRow(rs);
if (wanted.equals(e.getBlockchainKey())) return e;
try (Connection c = db.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
ps.setString(1, wanted);
try (ResultSet rs = ps.executeQuery()) {
if (!rs.next()) return null;
return mapRow(rs);
}
}
return null;
}
private SolanaUserPdaCurrentEntry mapRow(ResultSet rs) throws SQLException {
SolanaUserPdaCurrentEntry entry = new SolanaUserPdaCurrentEntry();
entry.setLogin(rs.getString("login"));
entry.setBlockchainName(rs.getString("blockchain_name"));
entry.setBlockchainKey(KeyEncodingUtil.normalizeKeyToBase64_32(rs.getString("blockchain_key")));
entry.setBlockchainKey(KeyEncodingUtil.base58KeyToBase64_32(rs.getString("blockchain_key")));
entry.setPaidLimitBytes(rs.getLong("paid_limit_bytes"));
return entry;
}
@@ -1,11 +1,8 @@
-- Turbo/direct-Arweave transport no longer stores root transaction IDs for user DataItems.
ALTER TABLE blocks
DROP COLUMN IF EXISTS arweave_root_tx_id;
ALTER TABLE arweave_block_import_queue
DROP COLUMN IF EXISTS root_tx_id;
-- PDA 1.2: keep the complete append-only blockchain-key/fork history alongside the active compatibility columns.
ALTER TABLE solana_user_pda_current
ADD COLUMN IF NOT EXISTS blockchain_forks_json TEXT NOT NULL DEFAULT '[]';
UPDATE db_schema_version
SET schema_version=25,
updated_at_ms=CAST(EXTRACT(EPOCH FROM clock_timestamp())*1000 AS BIGINT)
WHERE id=1;
SET schema_version = 25,
updated_at_ms = CAST(EXTRACT(EPOCH FROM clock_timestamp()) * 1000 AS BIGINT)
WHERE id = 1;
@@ -84,6 +84,7 @@ CREATE TABLE IF NOT EXISTS solana_user_pda_current (
blockchain_name TEXT NOT NULL,
blockchain_key TEXT NOT NULL,
paid_limit_bytes BIGINT NOT NULL,
blockchain_forks_json TEXT NOT NULL DEFAULT '[]',
used_bytes BIGINT NOT NULL,
last_block_number INTEGER NOT NULL,
last_block_hash TEXT NOT NULL,
@@ -500,6 +501,7 @@ CREATE TABLE IF NOT EXISTS blocks (
data_item_id BYTEA NOT NULL,
arweave_publish_pending BOOLEAN NOT NULL DEFAULT FALSE,
arweave_published_at_ms BIGINT,
arweave_root_tx_id TEXT,
edited_by_block_number INTEGER CHECK (edited_by_block_number IS NULL OR edited_by_block_number >= 0),
line_code INTEGER CHECK (line_code IS NULL OR line_code >= 0),
prev_line_number INTEGER CHECK (prev_line_number IS NULL OR prev_line_number >= 0),
@@ -534,6 +536,7 @@ VALUES (1, 0, 0) ON CONFLICT (id) DO NOTHING;
CREATE TABLE IF NOT EXISTS arweave_block_import_queue (
data_item_id BYTEA PRIMARY KEY,
root_tx_id TEXT NOT NULL,
block_height BIGINT NOT NULL,
raw_data_item BYTEA NOT NULL,
status TEXT NOT NULL,
@@ -2079,7 +2082,7 @@ CREATE INDEX IF NOT EXISTS idx_solana_user_pda_current_archive_pending
WHERE archive_head_tx_id <> '';
INSERT INTO db_schema_version(id,schema_version,updated_at_ms)
VALUES(1,25,CAST(EXTRACT(EPOCH FROM clock_timestamp())*1000 AS BIGINT))
VALUES(1,24,CAST(EXTRACT(EPOCH FROM clock_timestamp())*1000 AS BIGINT))
ON CONFLICT(id) DO UPDATE SET schema_version=EXCLUDED.schema_version, updated_at_ms=EXCLUDED.updated_at_ms;
COMMIT;
@@ -28,7 +28,6 @@ public final class SolanaUserPdaImportService {
private static final HttpClient HTTP = HttpClient.newHttpClient();
private static final String MAGIC = "SHiNE";
private static final int MAX_EFFECTIVE_ACCESS_SERVERS = 1;
private static final int ARCHIVE_HEAD_PAYLOAD_BYTES = 64;
private SolanaUserPdaImportService() {}
@@ -55,8 +54,7 @@ public final class SolanaUserPdaImportService {
}
/**
* Чтение server PDA по логину сервера. Используется сервером при старте,
* чтобы получить актуальный server_address и список sync_servers.
* Чтение server PDA 1.2 по логину сервера. В 1.2 разрешён ровно один endpoint.
*/
public static ParsedServerProfile fetchServerProfileByLogin(String loginRaw) throws Exception {
String login = normalizeLogin(loginRaw);
@@ -156,9 +154,16 @@ public final class SolanaUserPdaImportService {
}
private static ParsedSolanaUser parseUserPda(byte[] raw) {
if (raw == null || raw.length < 128) return null;
if (raw == null || raw.length < 9) return null;
if (!MAGIC.equals(new String(raw, 0, 5, StandardCharsets.UTF_8))) return null;
int major = u8(raw, 5);
int minor = u8(raw, 6);
if (major != 1 || minor != 2) return null;
return parseUserPdaV12(raw);
}
private static ParsedSolanaUser parseUserPdaV12(byte[] raw) {
if (raw == null || raw.length < 128) return null;
int recordLen = u16le(raw, 7);
if (recordLen < 73 || recordLen > raw.length) return null;
@@ -167,228 +172,166 @@ public final class SolanaUserPdaImportService {
c += 8; // updated_at_ms
c += 4; // record_number
c += 32; // prev_record_hash
if (c >= recordLen) return null;
int loginLen = u8(raw, c++);
if (loginLen <= 0 || c + loginLen > recordLen) return null;
String login = new String(raw, c, loginLen, StandardCharsets.UTF_8);
c += loginLen;
if (c >= recordLen) return null;
int blocksCount = u8(raw, c++);
String blockchainName = null;
byte[] blockchainKey32 = null;
byte[] clientKey32 = null;
long paidLimitBytes = 0L;
List<ParsedSessionRecord> sessions = new ArrayList<>();
int forkCount = 0;
List<String> accessServers = new ArrayList<>();
for (int i = 0; i < blocksCount; i++) {
if (c + 2 > recordLen) return null;
int blockType = u8(raw, c++);
int blockVer = u8(raw, c++);
if (blockVer != 0) return null;
if (blockType == 0) {
c += 32; // recovery_key
} else if (blockType == 1) {
c += 32;
} else if (blockType == 2) {
if (blockType == 1) {
if (c + 32 > recordLen) return null;
c += 32; // root_key
continue;
}
if (blockType == 2) {
if (c + 32 > recordLen) return null;
clientKey32 = slice(raw, c, 32);
c += 32;
} else if (blockType == 3) {
int count = u8(raw, c++);
for (int j = 0; j < count; j++) {
c += 1; // blockchain_type
int bchLen = u8(raw, c++);
blockchainName = new String(raw, c, bchLen, StandardCharsets.UTF_8);
c += bchLen;
blockchainKey32 = slice(raw, c, 32);
c += 32;
paidLimitBytes = u64le(raw, c);
c += 8;
c += 8; // used_bytes
c += 4; // last_block_number
c += 32; // last_block_hash
c += 64; // last_block_signature
int arweavePresent = u8(raw, c++);
if (arweavePresent == 1) {
int arLen = u8(raw, c++);
c += arLen;
} else if (arweavePresent != 0) {
return null;
}
}
} else if (blockType == 30) {
int isServer = u8(raw, c++);
if (isServer == 1) {
c += 1; // address_format_type
c += 1; // address_format_version
int addrLen = u8(raw, c++);
c += addrLen;
int syncCount = u8(raw, c++);
for (int j = 0; j < syncCount; j++) {
int n = u8(raw, c++);
c += n;
}
} else if (isServer != 0) {
return null;
}
} else if (blockType == 40) {
int accessCount = u8(raw, c++);
for (int j = 0; j < accessCount; j++) {
int n = u8(raw, c++);
String accessServerLogin = new String(raw, c, n, StandardCharsets.UTF_8);
c += n;
String normalizedAccessServerLogin = normalizeLogin(accessServerLogin);
if (normalizedAccessServerLogin == null || accessServers.contains(normalizedAccessServerLogin)) {
continue;
}
if (accessServers.size() < MAX_EFFECTIVE_ACCESS_SERVERS) {
accessServers.add(normalizedAccessServerLogin);
}
}
} else if (blockType == 50) {
int sessionsMode = u8(raw, c++);
if (sessionsMode != 1 && sessionsMode != 10) return null;
int sessionsCount = u8(raw, c++);
if (sessionsCount > 64) return null;
for (int j = 0; j < sessionsCount; j++) {
int sessionType = u8(raw, c++);
int sessionVersion = u8(raw, c++);
int n = u8(raw, c++);
String sessionName = new String(raw, c, n, StandardCharsets.UTF_8);
c += n;
byte[] sessionPubKey32 = slice(raw, c, 32);
c += 32;
sessions.add(new ParsedSessionRecord(
sessionType,
sessionVersion,
sessionName,
sessionPubKey32
));
}
} else if (blockType == 70) {
c += 1;
} else if (blockType == 100) {
c += ARCHIVE_HEAD_PAYLOAD_BYTES;
} else {
return null;
continue;
}
if (c > recordLen) return null;
if (c + 2 > recordLen) return null;
int payloadLen = u16le(raw, c);
c += 2;
int payloadEnd = c + payloadLen;
if (payloadEnd < c || payloadEnd > recordLen) return null;
if (blockType == 3) {
if (c + 2 > payloadEnd) return null;
forkCount = u16le(raw, c);
c += 2;
if (forkCount <= 0) return null;
for (int j = 0; j < forkCount; j++) {
if (c + 44 > payloadEnd) return null;
blockchainKey32 = slice(raw, c, 32);
c += 32;
c += 8; // fork created_at_ms
paidLimitBytes = u32le(raw, c);
c += 4;
}
if (c != payloadEnd) return null;
} else if (blockType == 30) {
// Server profile is not needed for auth parsing; validate/skip its payload.
c = payloadEnd;
} else if (blockType == 40) {
if (c >= payloadEnd) return null;
int accessCount = u8(raw, c++);
if (accessCount > 1) return null;
for (int j = 0; j < accessCount; j++) {
if (c >= payloadEnd) return null;
int n = u8(raw, c++);
if (c + n > payloadEnd) return null;
String accessServerLogin = new String(raw, c, n, StandardCharsets.UTF_8);
c += n;
String normalized = normalizeLogin(accessServerLogin);
if (normalized != null && !accessServers.contains(normalized)
&& accessServers.size() < MAX_EFFECTIVE_ACCESS_SERVERS) {
accessServers.add(normalized);
}
}
if (c != payloadEnd) return null;
} else {
// Unknown variable PDA 1.2 block: forward-compatible skip by payload_len.
c = payloadEnd;
}
c = payloadEnd;
}
if (blockchainName == null || blockchainKey32 == null || clientKey32 == null) return null;
if (blockchainKey32 == null || clientKey32 == null || forkCount <= 0) return null;
String blockchainName = login + "-" + String.format(Locale.ROOT, "%03d", forkCount);
return new ParsedSolanaUser(
login,
blockchainName,
Base64.getEncoder().encodeToString(blockchainKey32),
Base64.getEncoder().encodeToString(clientKey32),
paidLimitBytes,
sessions,
List.of(),
accessServers
);
}
private static ParsedServerProfile parseServerProfile(byte[] raw) {
if (raw == null || raw.length < 128) return null;
if (!MAGIC.equals(new String(raw, 0, 5, StandardCharsets.UTF_8))) return null;
private static ParsedServerProfile parseServerProfile(byte[] raw) {
if (raw == null || raw.length < 9) return null;
if (!MAGIC.equals(new String(raw, 0, 5, StandardCharsets.UTF_8))) return null;
int major = u8(raw, 5);
int minor = u8(raw, 6);
if (major != 1 || minor != 2) return null;
return parseServerProfileV12(raw);
}
private static ParsedServerProfile parseServerProfileV12(byte[] raw) {
if (raw == null || raw.length < 128) return null;
int recordLen = u16le(raw, 7);
if (recordLen < 73 || recordLen > raw.length) return null;
int c = 9;
c += 8; // created_at_ms
c += 8; // updated_at_ms
c += 4; // record_number
c += 32; // prev_record_hash
int c = 9 + 8 + 8 + 4 + 32;
if (c >= recordLen) return null;
int loginLen = u8(raw, c++);
if (loginLen <= 0 || c + loginLen > recordLen) return null;
String login = new String(raw, c, loginLen, StandardCharsets.UTF_8);
c += loginLen;
if (c >= recordLen) return null;
int blocksCount = u8(raw, c++);
boolean isServer = false;
String serverAddress = "";
List<String> syncServers = new ArrayList<>();
for (int i = 0; i < blocksCount; i++) {
if (c + 2 > recordLen) return null;
int blockType = u8(raw, c++);
int blockVer = u8(raw, c++);
if (blockVer != 0) return null;
if (blockType == 0 || blockType == 1 || blockType == 2) {
if (blockType == 1 || blockType == 2) {
if (c + 32 > recordLen) return null;
c += 32;
} else if (blockType == 3) {
int count = u8(raw, c++);
for (int j = 0; j < count; j++) {
c += 1; // blockchain_type
int bchLen = u8(raw, c++);
c += bchLen;
c += 32; // blockchain pubkey
c += 8; // paid_limit_bytes
c += 8; // used_bytes
c += 4; // last_block_number
c += 32; // last_block_hash
c += 64; // last_block_signature
int arweavePresent = u8(raw, c++);
if (arweavePresent == 1) {
int arLen = u8(raw, c++);
c += arLen;
} else if (arweavePresent != 0) {
return null;
}
}
} else if (blockType == 30) {
int isServerValue = u8(raw, c++);
if (isServerValue == 1) {
isServer = true;
continue;
}
if (c + 2 > recordLen) return null;
int payloadLen = u16le(raw, c);
c += 2;
int payloadEnd = c + payloadLen;
if (payloadEnd < c || payloadEnd > recordLen) return null;
if (blockType == 30) {
if (c >= payloadEnd) return null;
int addressCount = u8(raw, c++);
if (addressCount != 1) return null;
for (int j = 0; j < addressCount; j++) {
if (c + 3 > payloadEnd) return null;
c += 1; // address_format_type
c += 1; // address_format_version
int addrLen = u8(raw, c++);
serverAddress = new String(raw, c, addrLen, StandardCharsets.UTF_8);
c += addrLen;
int syncCount = u8(raw, c++);
for (int j = 0; j < syncCount; j++) {
int n = u8(raw, c++);
String syncLogin = new String(raw, c, n, StandardCharsets.UTF_8);
c += n;
syncServers.add(normalizeLogin(syncLogin));
int n = u8(raw, c++);
if (c + n > payloadEnd) return null;
String address = new String(raw, c, n, StandardCharsets.UTF_8);
c += n;
if (!isServer) {
isServer = true;
serverAddress = address;
}
} else if (isServerValue != 0) {
return null;
}
} else if (blockType == 40) {
int accessCount = u8(raw, c++);
for (int j = 0; j < accessCount; j++) {
int n = u8(raw, c++);
c += n;
}
} else if (blockType == 50) {
int sessionsMode = u8(raw, c++);
if (sessionsMode != 1 && sessionsMode != 10) return null;
int sessionsCount = u8(raw, c++);
if (sessionsCount > 64) return null;
for (int j = 0; j < sessionsCount; j++) {
c += 1; // session_type
c += 1; // session_version
int n = u8(raw, c++);
c += n;
c += 32;
}
} else if (blockType == 70) {
c += 1;
} else if (blockType == 100) {
c += ARCHIVE_HEAD_PAYLOAD_BYTES;
} else {
return null;
if (c != payloadEnd) return null;
}
if (c > recordLen) return null;
c = payloadEnd;
}
return new ParsedServerProfile(login, isServer, serverAddress, syncServers);
return new ParsedServerProfile(login, isServer, serverAddress, List.of());
}
private static String normalizeLogin(String login) {
if (login == null) return null;
String s = login.trim();
@@ -400,6 +343,12 @@ public final class SolanaUserPdaImportService {
private static int u16le(byte[] b, int o) {
return (b[o] & 0xFF) | ((b[o + 1] & 0xFF) << 8);
}
private static long u32le(byte[] b, int o) {
return ((long) b[o] & 0xFFL)
| (((long) b[o + 1] & 0xFFL) << 8)
| (((long) b[o + 2] & 0xFFL) << 16)
| (((long) b[o + 3] & 0xFFL) << 24);
}
private static long u64le(byte[] b, int o) {
long out = 0L;
for (int i = 0; i < 8; i++) out |= ((long) (b[o + i] & 0xFF)) << (8 * i);
@@ -63,7 +63,8 @@ public final class AddBlockSyncService {
}
private static boolean isEnabled() {
return AppConfig.getInstance().getBoolean("blockchain.sync.enabled", true);
// Legacy peer replication is disabled in PDA 1.2; Arweave is the synchronization source.
return false;
}
private void replicate(String blockchainName, int blockNumber) throws Exception {
@@ -415,9 +415,12 @@ public final class SolanaUsersSyncService
List<ParsedTxEnvelope> envelopes =
new ArrayList<>();
Set<String> updatePdaAddresses =
Set<String> exactFetchPdaAddresses =
new LinkedHashSet<>();
Map<String, String> latestRelevantSignatureByPda =
new LinkedHashMap<>();
for (SolanaRpcClient.SignatureRecord signatureRecord : chronologicalSignatures) {
JsonNode transaction =
@@ -436,20 +439,14 @@ public final class SolanaUsersSyncService
);
if (envelope.parsedInstruction() != null
&& envelope.parsedInstruction().kind() == ShineUsersCodec.TxKind.UPDATE_USER_PDA
&& envelope.parsedInstruction().affectedPdaAddress() != null) {
updatePdaAddresses.add(
envelope.parsedInstruction()
.affectedPdaAddress()
);
&& envelope.parsedInstruction().affectedPdaAddress() != null
&& envelope.parsedInstruction().relevant()) {
String changedPda = envelope.parsedInstruction().affectedPdaAddress();
latestRelevantSignatureByPda.put(changedPda, envelope.signatureRecord().signature());
exactFetchPdaAddresses.add(changedPda);
}
}
Map<String, ShineUsersCodec.UserPdaSnapshot> currentSnapshots =
storage.getCurrentSnapshots(
updatePdaAddresses
);
List<PostgresStorageRepository.TxHistoryEntry> txEntries =
new ArrayList<>();
@@ -492,71 +489,16 @@ public final class SolanaUsersSyncService
parsedInstruction.economyConfigState();
}
if (parsedInstruction.relevant()
&& parsedInstruction.userPdaMutation() != null) {
if (parsedInstruction.relevant()) {
relevant = true;
affectedPdaAddress = parsedInstruction.affectedPdaAddress();
affectedLogin = parsedInstruction.affectedLogin();
ShineUsersCodec.UserPdaSnapshot snapshot;
lastRelevantSignature = envelope.signatureRecord().signature();
lastRelevantSlot = envelope.signatureRecord().slot();
if (parsedInstruction.kind() == ShineUsersCodec.TxKind.CREATE_USER_PDA) {
if (economyState == null) {
economyState =
ShineUsersCodec.EconomyConfigState.initial();
log.warn(
"Economy config state was absent while processing create tx {}. Falling back to initial constants.",
envelope.signatureRecord().signature()
);
}
snapshot =
ShineUsersCodec.buildCreateSnapshot(
parsedInstruction.userPdaMutation(),
economyState,
envelope.signatureRecord().signature(),
envelope.signatureRecord().slot()
);
} else {
ShineUsersCodec.UserPdaSnapshot previous =
currentSnapshots.get(
affectedPdaAddress
);
if (previous == null) {
throw new IllegalStateException(
"Missing previous snapshot for update PDA " +
affectedPdaAddress
);
}
snapshot =
ShineUsersCodec.buildUpdateSnapshot(
parsedInstruction.userPdaMutation(),
previous,
envelope.signatureRecord().signature(),
envelope.signatureRecord().slot()
);
}
currentSnapshots.put(
snapshot.pdaAddress(),
snapshot
);
snapshotsToPersist.add(
snapshot
);
lastRelevantSignature =
envelope.signatureRecord().signature();
lastRelevantSlot =
envelope.signatureRecord().slot();
}
}
@@ -579,6 +521,25 @@ public final class SolanaUsersSyncService
fetchResult.signatures()
.get(0);
// PDA 1.2 содержит поля, вычисляемые программой и подписанные как единый документ.
// Поэтому для нового формата не реконструируем байты из instruction args, а читаем
// фактическое текущее состояние затронутых PDA из Solana.
if (!exactFetchPdaAddresses.isEmpty()) {
SolanaRpcClient.AccountBatchResult exactAccounts =
rpcClient.getCurrentAccounts(exactFetchPdaAddresses, newestSeen.slot());
for (ProgramAccountUpdate account : exactAccounts.updates()) {
ShineUsersCodec.UserPdaSnapshot snapshot =
ShineUsersCodec.parseUserPdaAccount(
account.address(),
account.slot(),
account.dataBase64(),
latestRelevantSignatureByPda.getOrDefault(account.address(), "")
);
snapshotsToPersist.removeIf(existing -> existing.pdaAddress().equals(snapshot.pdaAddress()));
snapshotsToPersist.add(snapshot);
}
}
PostgresStorageRepository.SyncStateSnapshot newState =
new PostgresStorageRepository.SyncStateSnapshot(
"READY",
@@ -462,7 +462,7 @@ public final class PostgresStorageRepository
"INSERT INTO solana_user_pda_current (" +
"pda_address, login, normalized_login, record_number, slot, last_tx_signature, " +
"recovery_key, root_key, client_key, blockchain_name, " +
"blockchain_key, paid_limit_bytes, used_bytes, " +
"blockchain_key, paid_limit_bytes, blockchain_forks_json, used_bytes, " +
"last_block_number, last_block_hash, last_block_signature, " +
"arweave_tx_id, archive_head_tx_id, archive_head_hash, is_server, address_format_type, " +
"address_format_version, server_address, sync_servers_json, " +
@@ -470,7 +470,7 @@ public final class PostgresStorageRepository
"trusted_count, created_at_ms, updated_at_ms, " +
"prev_record_hash, record_signature, raw_data_base64, " +
"first_seen_at_ms, last_synced_at_ms" +
") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) " +
") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) " +
"ON CONFLICT (pda_address) DO UPDATE SET " +
"login = EXCLUDED.login, " +
"normalized_login = EXCLUDED.normalized_login, " +
@@ -483,6 +483,7 @@ public final class PostgresStorageRepository
"blockchain_name = EXCLUDED.blockchain_name, " +
"blockchain_key = EXCLUDED.blockchain_key, " +
"paid_limit_bytes = EXCLUDED.paid_limit_bytes, " +
"blockchain_forks_json = EXCLUDED.blockchain_forks_json, " +
"used_bytes = EXCLUDED.used_bytes, " +
"last_block_number = EXCLUDED.last_block_number, " +
"last_block_hash = EXCLUDED.last_block_hash, " +
@@ -595,6 +596,7 @@ public final class PostgresStorageRepository
statement.setString(i++, snapshot.blockchainName());
statement.setString(i++, snapshot.blockchainKey());
statement.setLong(i++, snapshot.paidLimitBytes());
statement.setString(i++, writeJson(snapshot.blockchainForks()));
statement.setLong(i++, snapshot.usedBytes());
statement.setInt(i++, snapshot.lastBlockNumber());
statement.setString(i++, snapshot.lastBlockHash());
@@ -652,6 +654,17 @@ public final class PostgresStorageRepository
)
);
String forksJson = resultSet.getString("blockchain_forks_json");
List<ShineUsersCodec.BlockchainForkSnapshot> blockchainForks =
forksJson == null || forksJson.isBlank()
? List.of()
: Arrays.asList(
mapper.readValue(
forksJson,
ShineUsersCodec.BlockchainForkSnapshot[].class
)
);
return new ShineUsersCodec.UserPdaSnapshot(
resultSet.getString("pda_address"),
resultSet.getString("login"),
@@ -684,7 +697,8 @@ public final class PostgresStorageRepository
resultSet.getLong("updated_at_ms"),
resultSet.getString("prev_record_hash"),
resultSet.getString("record_signature"),
resultSet.getString("raw_data_base64")
resultSet.getString("raw_data_base64"),
List.copyOf(blockchainForks)
);
}
@@ -836,6 +850,7 @@ public final class PostgresStorageRepository
"blockchain_name TEXT NOT NULL, " +
"blockchain_key TEXT NOT NULL, " +
"paid_limit_bytes BIGINT NOT NULL, " +
"blockchain_forks_json TEXT NOT NULL DEFAULT '[]', " +
"used_bytes BIGINT NOT NULL, " +
"last_block_number INTEGER NOT NULL, " +
"last_block_hash TEXT NOT NULL, " +
@@ -872,6 +887,7 @@ public final class PostgresStorageRepository
" OR normalized_login <> LOWER(BTRIM(login))"
);
statement.executeUpdate("ALTER TABLE solana_user_pda_current ADD COLUMN IF NOT EXISTS blockchain_forks_json TEXT NOT NULL DEFAULT '[]'");
statement.executeUpdate("ALTER TABLE solana_user_pda_current ADD COLUMN IF NOT EXISTS archive_head_tx_id TEXT NOT NULL DEFAULT ''");
statement.executeUpdate("ALTER TABLE solana_user_pda_current ADD COLUMN IF NOT EXISTS archive_head_hash TEXT NOT NULL DEFAULT ''");
@@ -74,7 +74,8 @@ public final class PeriodicBlockchainSyncService {
}
private static boolean isEnabled() {
return AppConfig.getInstance().getBoolean("blockchain.sync.enabled", true);
// PDA 1.2 удаляет sync_servers: пользовательские блокчейны синхронизируются через Arweave.
return false;
}
private static void runCycleSafe() {
@@ -22,54 +22,11 @@ public final class SyncServersBootstrapService {
private SyncServersBootstrapService() {}
public static void refreshFromSolanaOrLog() {
String serverLogin = normalize(AppConfig.getInstance().getParam(CONFIG_KEY));
if (serverLogin == null) {
log.warn("Sync bootstrap skipped: параметр {} не задан", CONFIG_KEY);
return;
}
try {
SolanaUserPdaImportService.ParsedServerProfile own =
SolanaUserPdaImportService.fetchServerProfileByLogin(serverLogin);
if (own == null) {
log.warn("Sync bootstrap skipped: server PDA не найдена для login={}", serverLogin);
return;
}
if (!own.isServer()) {
log.warn("Sync bootstrap skipped: PDA login={} не помечена как server", serverLogin);
return;
}
List<SyncServerEntry> entries = new ArrayList<>();
long now = System.currentTimeMillis();
for (String partnerLogin : own.syncServers()) {
String normalizedPartnerLogin = normalize(partnerLogin);
if (normalizedPartnerLogin == null) continue;
SolanaUserPdaImportService.ParsedServerProfile partner =
SolanaUserPdaImportService.fetchServerProfileByLogin(normalizedPartnerLogin);
if (partner == null) {
log.warn("Sync bootstrap: partner PDA не найдена для login={}", normalizedPartnerLogin);
continue;
}
if (!partner.isServer()) {
log.warn("Sync bootstrap: partner login={} не является server PDA", normalizedPartnerLogin);
continue;
}
String serverAddress = safe(partner.serverAddress());
if (serverAddress.isBlank()) {
log.warn("Sync bootstrap: у partner login={} пустой server_address", normalizedPartnerLogin);
continue;
}
entries.add(new SyncServerEntry(normalizedPartnerLogin, serverAddress, now));
}
SyncServersDAO.getInstance().replaceAll(entries);
log.info("Sync bootstrap: сохранено {} серверов синхронизации для login={}", entries.size(), serverLogin);
SyncServersDAO.getInstance().replaceAll(List.of());
log.info("Legacy sync_servers disabled: blockchain synchronization uses Arweave");
} catch (Exception e) {
log.error("Sync bootstrap failed while loading server PDA and sync_servers from Solana", e);
log.warn("Failed to clear legacy sync_servers table", e);
}
}
@@ -28,11 +28,11 @@ solana.users.sync.pollIntervalSeconds=300
sync.importUserProfileFromPartner.enabled=false
# ------------------------------------------------------------
# Прямая межсерверная синхронизация пользовательских блокчейнов
# через sync_servers: ListBlockchainHeads/GetBlockchainBlock -> локальный AddBlock.
# Для проверки режима Solana + Arweave без прямых связей серверов можно выключить.
# Legacy peer-to-peer blockchain sync. В PDA 1.2 sync_servers удалён,
# пользовательские блокчейны синхронизируются через Arweave.
# Параметр оставлен временно для совместимости конфигураций и не активирует legacy sync.
# ------------------------------------------------------------
blockchain.sync.enabled=true
blockchain.sync.enabled=false
# ------------------------------------------------------------
# Server public info
@@ -138,14 +138,11 @@ test.freeAvatar.walletJwkPath=
# ============================================================
# Arweave per-user-block transport (ANS-104)
# Test namespace: each user DataItem is signed with App=test5590.
# Channel DataItems additionally contain c_test5590=<canonical-channel-slug>.
# publish.mode: turbo | arweave | none
# Channel DataItems additionally contain c=<canonical-channel-slug>.
# ============================================================
arweave.blocks.publish.mode=none
arweave.blocks.publish.enabled=false
arweave.blocks.publish.intervalMinutes=15
arweave.blocks.publish.maxItems=10000
# Direct Arweave L1 fallback: server combines user DataItems into one standard ANS-104 bundle.
arweave.blocks.publish.maxBundleBytes=134217728
arweave.blocks.publish.gateway=https://arweave.net
arweave.blocks.publish.walletJwkPath=
@@ -153,18 +150,10 @@ arweave.blocks.publish.minConfirmations=0
arweave.blocks.publish.confirmPollSeconds=30
arweave.blocks.publish.confirmTimeoutMinutes=180
# Turbo: uploads each already user-signed DataItem separately, without re-signing it.
# paidByAddress is the public Turbo payer address. If it is empty and turbo.walletJwkPath is set,
# the Arweave payer address is derived locally from that JWK. The private key is never sent to Turbo.
# For paid uploads of someone else's signed DataItem, Turbo Credit Share Approval must exist for its signer.
arweave.blocks.publish.turbo.uploadUrl=https://turbo.ardrive.io/tx
arweave.blocks.publish.turbo.paidByAddress=
arweave.blocks.publish.turbo.walletJwkPath=
arweave.blocks.sync.enabled=false
arweave.blocks.sync.intervalMinutes=15
arweave.blocks.sync.gateway=https://turbo-gateway.com
arweave.blocks.sync.pageSize=100
arweave.blocks.sync.queueBatchSize=10000
arweave.blocks.sync.startBlockHeight=0
arweave.blocks.sync.maxDataItemBytes=8388608
arweave.blocks.sync.maxRootBundleBytes=268435456