SHA256
Compare commits
120
Commits
svyazi
..
bd52a23bec
| Author | SHA256 | Date | |
|---|---|---|---|
|
|
bd52a23bec | ||
|
|
e84ab41fe6 | ||
|
|
41d5e79f83 | ||
|
|
c13734cbc9 | ||
|
|
e78402c860 | ||
|
|
2ce80eeaef | ||
|
|
551bdc2d65 | ||
|
|
f507bdb343 | ||
|
|
395aa62bda | ||
|
|
d2b2876a79 | ||
|
|
8bb7d39c76 | ||
|
|
e296217f21 | ||
|
|
c0257f4be2 | ||
|
|
7b88b61b22 | ||
|
|
ab61787ee7 | ||
|
|
219a65ae27 | ||
|
|
94d4665185 | ||
|
|
ce97171d5a | ||
|
|
a22f590395 | ||
|
|
a520c5c5e1 | ||
|
|
cab26bbf41 | ||
|
|
6c8159c5f7 | ||
|
|
df0e9cef9b | ||
|
|
7a6ab3b35a | ||
|
|
aa5addb5d9 | ||
|
|
b67d06ce43 | ||
|
|
8d9a580f50 | ||
|
|
45eb77f431 | ||
|
|
2be6fe3ff9 | ||
|
|
a380900ec8 | ||
|
|
bca788c5e6 | ||
|
|
e77aaa3675 | ||
|
|
d25894149b | ||
|
|
68eec2b28b | ||
|
|
694aaf1f0e | ||
|
|
5caa4ce0ae | ||
|
|
af5b26c934 | ||
|
|
6997bc7600 | ||
|
|
9eedbf60a8 | ||
|
|
6294646922 | ||
|
|
42797b1a43 | ||
|
|
8c3751aba0 | ||
|
|
f40353725e | ||
|
|
3f0d71f788 | ||
|
|
f2ba7ce630 | ||
|
|
edc3d50136 | ||
|
|
a7a600035a | ||
|
|
3f05806563 | ||
|
|
2fb19ee66d | ||
|
|
d91ca5fbb3 | ||
|
|
7173f33d5f | ||
|
|
b3f9e1fe21 | ||
|
|
401ba918ad | ||
|
|
82d5a768f8 | ||
|
|
835109c81a | ||
|
|
d5f2c12fe7 | ||
|
|
11f169b080 | ||
|
|
96d23b16fc | ||
|
|
26db18e250 | ||
|
|
8fee7e1a9b | ||
|
|
e7cf98889b | ||
|
|
3fdff2a92b | ||
|
|
4d5a485933 | ||
|
|
b9cbbb857b | ||
|
|
56de0f2c74 | ||
|
|
68fe0532ce | ||
|
|
3edde418a2 | ||
|
|
923f281bf6 | ||
|
|
9ea276ba77 | ||
|
|
cfb0f9b734 | ||
|
|
993930eb82 | ||
|
|
17d6cb5be0 | ||
|
|
bb725ee451 | ||
|
|
b5ce7b0fa0 | ||
|
|
58de6a025d | ||
|
|
e6937cb49c | ||
|
|
7ac9e5a806 | ||
|
|
89630878af | ||
|
|
8b2012fad3 | ||
|
|
07ddaf5e4c | ||
|
|
4d10096ba2 | ||
|
|
5e9699613d | ||
|
|
b56a3a795f | ||
|
|
70fac068d0 | ||
|
|
d138bd9464 | ||
|
|
e14e3dfa21 | ||
|
|
30541ae4af | ||
|
|
74bace639c | ||
|
|
dda21a54ff | ||
|
|
d8f42c769c | ||
|
|
7b8e178723 | ||
|
|
b1c42efa82 | ||
|
|
acd5d4406d | ||
|
|
83548a859f | ||
|
|
79598fffa6 | ||
|
|
72e62127b3 | ||
|
|
67010a57f2 | ||
|
|
daa221a158 | ||
|
|
aca1e3054e | ||
|
|
6b5734d535 | ||
|
|
de8700f8e0 | ||
|
|
e101e5c5f4 | ||
|
|
8d22602761 | ||
|
|
f452ce1f1e | ||
|
|
3281da7f5c | ||
|
|
08c7fd7676 | ||
|
|
2e62616b46 | ||
|
|
9be4c4d3a5 | ||
|
|
f5698771e4 | ||
|
|
151a2c1754 | ||
|
|
13693a0a53 | ||
|
|
451280edd2 | ||
|
|
c997057a37 | ||
|
|
f8900e531a | ||
|
|
6e5b57fd7c | ||
|
|
408e474130 | ||
|
|
ef707ec217 | ||
|
|
8b12760dde | ||
|
|
cae64639bd | ||
|
|
be696d116b |
+12
@@ -78,6 +78,8 @@ shine-solana/shine/scripts/**/TEMP_*.md
|
||||
|
||||
# Локальные артефакты и внешние материалы ESP32-подпроекта
|
||||
ESP32/esp32-config-tool/
|
||||
ESP32/local-sketches/
|
||||
ESP32/esp32/ESP32-S3-Touch-AMOLED-2.16/main-device/test_sketches/breath_dual_mode_monitor/
|
||||
ESP32/**/.git/
|
||||
ESP32/**/.idea/
|
||||
ESP32-wallet/.idea/
|
||||
@@ -114,3 +116,13 @@ SHiNE-bundle-*.zip
|
||||
# Рабочие бэкапы/превью-ассеты UI — не для репозитория
|
||||
*.bak.png
|
||||
shine-UI/assets/navbar_preview.png
|
||||
|
||||
# Private local configuration: keep on disk, never push to GitHub
|
||||
/SHiNE-server/src/main/resources/application.properties
|
||||
/application.properties
|
||||
/deploy/backup/scheme/
|
||||
/shine-solana/shine/create-SHiNE-DAO/dao.config.mainnet.env
|
||||
**/.env
|
||||
**/.env.*
|
||||
!**/.env.example
|
||||
!**/.env.sample
|
||||
|
||||
@@ -43,8 +43,9 @@
|
||||
- Этот файл считать основной справкой (single source of truth) по деплою и первичной инициализации Solana-регистрации в текущем проекте.
|
||||
- Актуальная архитектурная справка по устройству Solana-программ, PDA-счетам, ролям DAO и движению средств находится в:
|
||||
- `docs/Solana_Architecture/README.md`
|
||||
- Документ формата пользовательской PDA-записи `shine_users` находится в:
|
||||
- `shine-solana/shine/doc/formats/shine-user-pda-format-v.1.0.md`
|
||||
- Документы формата пользовательской PDA-записи `shine_users`:
|
||||
- текущий: `shine-solana/shine/doc/formats/shine-user-pda-format-v.1.2.md`;
|
||||
- legacy: `shine-solana/shine/doc/formats/shine-user-pda-format-v.1.0.md`.
|
||||
- Актуальная документация по серверному модулю синхронизации Solana users находится в:
|
||||
- `docs/Solana/SOLANA_USERS_SYNC_MODULE_DESIGN.md`
|
||||
- При любом изменении логики серверной синхронизации `shine_users`, её таблиц PostgreSQL, checkpoint-механизма, startup/lifecycle или deploy-настроек обязательно обновлять:
|
||||
@@ -137,11 +138,8 @@
|
||||
- с какого сценария продолжать разработку;
|
||||
- из какого коммита брать последнюю полную реализацию.
|
||||
|
||||
## Коммуникация по новым задачам (обязательно)
|
||||
- При получении нового задания сначала кратко пересказать задачу своими словами.
|
||||
- До начала реализации задать недостающие уточняющие вопросы (если они есть).
|
||||
## Коммуникация по новым задачам
|
||||
- При получении нового задания кратко подтвердить понимание задачи, если это помогает синхронизироваться.
|
||||
- До начала реализации задавать уточняющие вопросы только когда без ответа есть риск сделать не то или затронуть опасную область.
|
||||
- Если задача понятна и не требует отдельного подтверждения по другим правилам проекта, переходить к реализации сразу.
|
||||
- Если есть уместные идеи/улучшения — кратко предложить их; если полезных идей нет, ничего дополнительно не предлагать.
|
||||
- Добавлять краткую оценку фичи (насколько это полезно/удачно по мнению исполнителя).
|
||||
- После этого обязательно запросить подтверждение от пользователя, что задача понята верно, и только после подтверждения переходить к реализации.
|
||||
- Если вопросов нет, явно написать в формате: «Я всё понял, начинаю делать?» и ждать подтверждения.
|
||||
- Без подтверждения пользователя реализацию не начинать.
|
||||
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
MODE="${1:-potentiometer-adc-test}"
|
||||
PORT="${PORT:-}"
|
||||
|
||||
case "$MODE" in
|
||||
potentiometer-adc-test)
|
||||
SKETCH_DIR="$ROOT_DIR/test_sketches/potentiometer_adc_test"
|
||||
BUILD_DIR="$ROOT_DIR/.arduino-build/build-potentiometer-adc-test"
|
||||
;;
|
||||
buzzer-gpio6-test)
|
||||
SKETCH_DIR="$ROOT_DIR/test_sketches/buzzer_gpio6_test"
|
||||
BUILD_DIR="$ROOT_DIR/.arduino-build/build-buzzer-gpio6-test"
|
||||
;;
|
||||
*)
|
||||
echo "Unknown mode: $MODE" >&2
|
||||
echo "Known modes: potentiometer-adc-test, buzzer-gpio6-test" >&2
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
|
||||
if [[ -z "$PORT" ]]; then
|
||||
PORT="$(arduino-cli board list | awk '/ttyACM|ttyUSB/ {print $1; exit}')"
|
||||
fi
|
||||
|
||||
if [[ -z "$PORT" ]]; then
|
||||
echo "No serial port found. Set PORT=/dev/ttyACM0 explicitly." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
FQBN="esp32:esp32:esp32s3:USBMode=hwcdc,CDCOnBoot=cdc,UploadSpeed=921600,CPUFreq=240,FlashMode=dio,FlashSize=16M,PartitionScheme=app3M_fat9M_16MB,PSRAM=opi"
|
||||
|
||||
echo "== Mode: $MODE"
|
||||
echo "== Sketch: $SKETCH_DIR"
|
||||
echo "== Port: $PORT"
|
||||
echo "== FQBN: $FQBN"
|
||||
|
||||
arduino-cli compile \
|
||||
--fqbn "$FQBN" \
|
||||
--build-path "$BUILD_DIR" \
|
||||
"$SKETCH_DIR"
|
||||
|
||||
arduino-cli upload \
|
||||
--fqbn "$FQBN" \
|
||||
-p "$PORT" \
|
||||
--input-dir "$BUILD_DIR" \
|
||||
"$SKETCH_DIR"
|
||||
|
||||
echo
|
||||
echo "== Done."
|
||||
@@ -0,0 +1,3 @@
|
||||
# AGENTS
|
||||
|
||||
This folder contains a temporary GPIO6 buzzer diagnostic for ESP32-S3-LCD-2. Keep screen/serial strings in English.
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
// Buzzer module wiring: GND -> GND, VCC -> 3V3, I/O -> P1 IO6 / GPIO6.
|
||||
static constexpr uint8_t PIN_BUZZER = 6;
|
||||
static constexpr uint32_t STEP_MS = 1800;
|
||||
static uint8_t gStep = 0;
|
||||
static uint32_t gChangedMs = 0;
|
||||
|
||||
static void applyStep(uint8_t step) {
|
||||
ledcDetach(PIN_BUZZER);
|
||||
pinMode(PIN_BUZZER, OUTPUT);
|
||||
digitalWrite(PIN_BUZZER, LOW);
|
||||
delay(80);
|
||||
switch (step) {
|
||||
case 0:
|
||||
pinMode(PIN_BUZZER, OUTPUT);
|
||||
digitalWrite(PIN_BUZZER, HIGH);
|
||||
Serial.println("TEST 1: GPIO6 HIGH - active-high module should sound");
|
||||
break;
|
||||
case 1:
|
||||
pinMode(PIN_BUZZER, OUTPUT);
|
||||
digitalWrite(PIN_BUZZER, LOW);
|
||||
Serial.println("TEST 2: GPIO6 LOW - active-low module should sound");
|
||||
break;
|
||||
case 2:
|
||||
ledcAttach(PIN_BUZZER, 1800, 8);
|
||||
ledcWriteTone(PIN_BUZZER, 1800);
|
||||
ledcWrite(PIN_BUZZER, 180);
|
||||
Serial.println("TEST 3: GPIO6 PWM 1800 Hz - passive module should sound");
|
||||
break;
|
||||
default:
|
||||
pinMode(PIN_BUZZER, OUTPUT);
|
||||
digitalWrite(PIN_BUZZER, LOW);
|
||||
Serial.println("SILENCE");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.println("GPIO6 BUZZER TEST: HIGH, LOW, PWM, SILENCE. Each step lasts 1.8 seconds.");
|
||||
applyStep(gStep);
|
||||
gChangedMs = millis();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (millis() - gChangedMs < STEP_MS) return;
|
||||
gStep = (gStep + 1) % 4;
|
||||
applyStep(gStep);
|
||||
gChangedMs = millis();
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
# AGENTS
|
||||
|
||||
## Назначение папки
|
||||
|
||||
Эта папка содержит отдельный тестовый Arduino-скетч `potentiometer_adc_test.ino` для Waveshare `ESP32-S3-LCD-2`.
|
||||
|
||||
Скетч проверяет переменный резистор на обычных GPIO/ADC, без использования USB `D+ / D-`.
|
||||
|
||||
## Обязательная синхронизация
|
||||
|
||||
При любом изменении `potentiometer_adc_test.ino` обязательно обновлять рядом лежащий `README.md`.
|
||||
|
||||
Если меняются экранные тексты, режимы, пины подключения, правила расчёта, touch-кнопки или serial-команды, описание в `README.md` должно быть обновлено в том же наборе изменений.
|
||||
|
||||
## Язык экрана
|
||||
|
||||
Экранные строки в скетче оставлять на английском языке.
|
||||
@@ -0,0 +1,73 @@
|
||||
# Potentiometer ADC Test for ESP32-S3-LCD-2
|
||||
|
||||
Тестовый скетч для Waveshare `ESP32-S3-LCD-2`.
|
||||
|
||||
Назначение: проверить переменный резистор `RP 6045-B50K` через обычные GPIO/ADC, без использования USB `D+ / D-`.
|
||||
|
||||
## Пины
|
||||
|
||||
Используются пины с разъёма `P1` платы:
|
||||
|
||||
- `GPIO2 / IO2` — ADC-вход, сюда подключается ползунок потенциометра.
|
||||
- `GPIO6 / IO6` — DRIVE-выход для грубого 2-проводного режима.
|
||||
- `3V3` — питание потенциометра для 3-проводного режима, находится на разъёме `P2`.
|
||||
- `GND` — земля.
|
||||
|
||||
Не подавать `5V` на `GPIO2` или `GPIO6`: GPIO ESP32-S3 не являются 5V tolerant.
|
||||
|
||||
## Режимы
|
||||
|
||||
Переключение:
|
||||
|
||||
- touch-кнопка `3-WIRE`;
|
||||
- touch-кнопка `2-WIRE`;
|
||||
- серийная команда `m`;
|
||||
- короткое нажатие `BOOT / GPIO0`.
|
||||
|
||||
Сброс min/max:
|
||||
|
||||
- touch-кнопка `RESET`;
|
||||
- серийная команда `r`.
|
||||
|
||||
## 3-Wire Mode
|
||||
|
||||
Правильное подключение:
|
||||
|
||||
- один край потенциометра -> `3V3`;
|
||||
- второй край потенциометра -> `GND`;
|
||||
- средний ползунок -> `GPIO2 / IO2`.
|
||||
|
||||
Скетч показывает напряжение на ползунке, raw ADC, абсолютное положение от `0..3.3V`, калиброванное положение между найденными min/max и примерное сопротивление участка `0..50 kΩ`.
|
||||
|
||||
## 2-Wire Mode
|
||||
|
||||
Грубое подключение:
|
||||
|
||||
- один край потенциометра -> `GPIO6 / IO6`;
|
||||
- средний ползунок -> `GPIO2 / IO2`;
|
||||
- второй край не подключать.
|
||||
|
||||
`GPIO6` выдаёт `HIGH`, `GPIO2` читается как ADC с внутренней подтяжкой вниз. Такой режим показывает движение ползунка, но не даёт точного сопротивления: внутренняя подтяжка ESP32 имеет большой разброс.
|
||||
|
||||
## Экранные данные
|
||||
|
||||
На экране выводятся:
|
||||
|
||||
- режим и схема пинов;
|
||||
- raw ADC и millivolts;
|
||||
- абсолютный процент от `0..3300 mV`;
|
||||
- калиброванный процент по найденному min/max;
|
||||
- min/max/span;
|
||||
- EMA-сглаживание;
|
||||
- примерная оценка сопротивления;
|
||||
- статус `OPEN/LOW/MID/HIGH/SATURATED/NO SPAN`;
|
||||
- координаты последнего touch;
|
||||
- serial-подсказка.
|
||||
|
||||
## Сборка
|
||||
|
||||
Из папки `main-device`:
|
||||
|
||||
```bash
|
||||
./burn.sh potentiometer-adc-test
|
||||
```
|
||||
+348
@@ -0,0 +1,348 @@
|
||||
#include <Arduino.h>
|
||||
#include <Arduino_GFX_Library.h>
|
||||
#include <Wire.h>
|
||||
#include <bsp_cst816.h>
|
||||
|
||||
#define PIN_LCD_SCLK 39
|
||||
#define PIN_LCD_MOSI 38
|
||||
#define PIN_LCD_MISO 40
|
||||
#define PIN_LCD_DC 42
|
||||
#define PIN_LCD_RST -1
|
||||
#define PIN_LCD_CS 45
|
||||
#define PIN_LCD_BL 1
|
||||
|
||||
#define PIN_TP_SDA 48
|
||||
#define PIN_TP_SCL 47
|
||||
|
||||
#define PIN_ADC_WIPER 2
|
||||
#define PIN_DRIVE_2WIRE 6
|
||||
#define PIN_BOOT_BUTTON 0
|
||||
|
||||
static const uint16_t DISP_W = 320;
|
||||
static const uint16_t DISP_H = 240;
|
||||
static const uint32_t ADC_FULL_MV = 3300;
|
||||
static const float POT_OHMS = 50000.0f;
|
||||
static const float ROUGH_PULLDOWN_OHMS = 45000.0f;
|
||||
|
||||
static const uint16_t C_BG = 0x0841;
|
||||
static const uint16_t C_PANEL = 0x18E3;
|
||||
static const uint16_t C_CARD = 0x2126;
|
||||
static const uint16_t C_TEXT = 0xF7BE;
|
||||
static const uint16_t C_MUTE = 0x8C71;
|
||||
static const uint16_t C_ACCENT = 0x05DF;
|
||||
static const uint16_t C_OK = 0x47E8;
|
||||
static const uint16_t C_WARN = 0xFD20;
|
||||
static const uint16_t C_BAD = 0xF986;
|
||||
static const uint16_t C_BORDER = 0x4208;
|
||||
|
||||
struct Rect {
|
||||
int16_t x;
|
||||
int16_t y;
|
||||
int16_t w;
|
||||
int16_t h;
|
||||
};
|
||||
|
||||
enum class Mode : uint8_t {
|
||||
ThreeWire,
|
||||
TwoWire,
|
||||
};
|
||||
|
||||
struct Sample {
|
||||
uint16_t raw;
|
||||
uint32_t mv;
|
||||
uint16_t rawMinBurst;
|
||||
uint16_t rawMaxBurst;
|
||||
};
|
||||
|
||||
Arduino_DataBus *bus = new Arduino_ESP32SPI(
|
||||
PIN_LCD_DC, PIN_LCD_CS, PIN_LCD_SCLK, PIN_LCD_MOSI, PIN_LCD_MISO);
|
||||
Arduino_GFX *gfx = new Arduino_ST7789(
|
||||
bus, PIN_LCD_RST, 1, true, 240, 320);
|
||||
|
||||
static const Rect BTN_3WIRE = {10, 38, 94, 30};
|
||||
static const Rect BTN_2WIRE = {113, 38, 94, 30};
|
||||
static const Rect BTN_RESET = {216, 38, 94, 30};
|
||||
|
||||
static Mode gMode = Mode::ThreeWire;
|
||||
static uint32_t gLastUiMs = 0;
|
||||
static uint32_t gLastSerialMs = 0;
|
||||
static uint32_t gMinMv = 0xFFFFFFFF;
|
||||
static uint32_t gMaxMv = 0;
|
||||
static float gEmaMv = 0.0f;
|
||||
static bool gHasEma = false;
|
||||
static bool gTouchOk = false;
|
||||
static uint16_t gLastTouchX = 0;
|
||||
static uint16_t gLastTouchY = 0;
|
||||
static bool gBootWasDown = false;
|
||||
static uint32_t gBootLastChangeMs = 0;
|
||||
|
||||
static void drawText(int16_t x, int16_t y, const char *text, uint16_t color, uint8_t size = 1) {
|
||||
gfx->setTextSize(size);
|
||||
gfx->setTextColor(color);
|
||||
gfx->setCursor(x, y);
|
||||
gfx->print(text);
|
||||
}
|
||||
|
||||
static void drawButton(const Rect &r, const char *label, bool active) {
|
||||
uint16_t fill = active ? C_ACCENT : C_CARD;
|
||||
uint16_t text = active ? C_BG : C_TEXT;
|
||||
gfx->fillRoundRect(r.x, r.y, r.w, r.h, 6, fill);
|
||||
gfx->drawRoundRect(r.x, r.y, r.w, r.h, 6, active ? C_ACCENT : C_BORDER);
|
||||
int16_t tx = r.x + 10;
|
||||
int16_t ty = r.y + 10;
|
||||
drawText(tx, ty, label, text, 1);
|
||||
}
|
||||
|
||||
static bool contains(const Rect &r, uint16_t x, uint16_t y) {
|
||||
return x >= r.x && x < r.x + r.w && y >= r.y && y < r.y + r.h;
|
||||
}
|
||||
|
||||
static const char *modeName() {
|
||||
return gMode == Mode::ThreeWire ? "3-WIRE" : "2-WIRE";
|
||||
}
|
||||
|
||||
static void resetRange() {
|
||||
gMinMv = 0xFFFFFFFF;
|
||||
gMaxMv = 0;
|
||||
gHasEma = false;
|
||||
}
|
||||
|
||||
static void configurePins() {
|
||||
pinMode(PIN_ADC_WIPER, INPUT);
|
||||
analogReadResolution(12);
|
||||
analogSetPinAttenuation(PIN_ADC_WIPER, ADC_11db);
|
||||
|
||||
if (gMode == Mode::TwoWire) {
|
||||
pinMode(PIN_DRIVE_2WIRE, OUTPUT);
|
||||
digitalWrite(PIN_DRIVE_2WIRE, HIGH);
|
||||
pinMode(PIN_ADC_WIPER, INPUT_PULLDOWN);
|
||||
} else {
|
||||
pinMode(PIN_DRIVE_2WIRE, INPUT);
|
||||
pinMode(PIN_ADC_WIPER, INPUT);
|
||||
}
|
||||
}
|
||||
|
||||
static void setMode(Mode mode) {
|
||||
if (gMode == mode) return;
|
||||
gMode = mode;
|
||||
configurePins();
|
||||
resetRange();
|
||||
}
|
||||
|
||||
static Sample readSample() {
|
||||
const uint8_t count = 32;
|
||||
uint32_t rawSum = 0;
|
||||
uint32_t mvSum = 0;
|
||||
uint16_t rawMin = 4095;
|
||||
uint16_t rawMax = 0;
|
||||
|
||||
for (uint8_t i = 0; i < count; i++) {
|
||||
uint16_t raw = analogRead(PIN_ADC_WIPER);
|
||||
uint32_t mv = analogReadMilliVolts(PIN_ADC_WIPER);
|
||||
rawSum += raw;
|
||||
mvSum += mv;
|
||||
if (raw < rawMin) rawMin = raw;
|
||||
if (raw > rawMax) rawMax = raw;
|
||||
delayMicroseconds(250);
|
||||
}
|
||||
|
||||
Sample s;
|
||||
s.raw = rawSum / count;
|
||||
s.mv = mvSum / count;
|
||||
s.rawMinBurst = rawMin;
|
||||
s.rawMaxBurst = rawMax;
|
||||
return s;
|
||||
}
|
||||
|
||||
static uint8_t percentFromMv(uint32_t mv) {
|
||||
if (mv >= ADC_FULL_MV) return 100;
|
||||
return (uint8_t)((mv * 100UL + ADC_FULL_MV / 2) / ADC_FULL_MV);
|
||||
}
|
||||
|
||||
static uint8_t calibratedPercent(uint32_t mv, uint32_t span) {
|
||||
if (span < 10 || gMinMv == 0xFFFFFFFF || mv <= gMinMv) return 0;
|
||||
uint32_t pct = ((mv - gMinMv) * 100UL + span / 2) / span;
|
||||
return (uint8_t)min<uint32_t>(pct, 100);
|
||||
}
|
||||
|
||||
static const char *statusText(uint32_t mv, uint32_t span, uint8_t pctAbs) {
|
||||
if (mv < 30) return "OPEN/GND";
|
||||
if (mv > 3250) return "SATURATED";
|
||||
if (span < 20) return "NO SPAN";
|
||||
if (pctAbs < 15) return "LOW";
|
||||
if (pctAbs > 85) return "HIGH";
|
||||
return "MID";
|
||||
}
|
||||
|
||||
static float estimateOhms(uint32_t mv) {
|
||||
if (gMode == Mode::ThreeWire) {
|
||||
return POT_OHMS * ((float)percentFromMv(mv) / 100.0f);
|
||||
}
|
||||
if (mv < 5) return -1.0f;
|
||||
if (mv >= ADC_FULL_MV) return 0.0f;
|
||||
return ROUGH_PULLDOWN_OHMS * ((float)ADC_FULL_MV / (float)mv - 1.0f);
|
||||
}
|
||||
|
||||
static void drawStaticUi() {
|
||||
gfx->fillScreen(C_BG);
|
||||
drawText(10, 8, "POT ADC TEST", C_TEXT, 2);
|
||||
drawText(210, 12, "ESP32-S3-LCD-2", C_MUTE, 1);
|
||||
drawButton(BTN_3WIRE, "3-WIRE", gMode == Mode::ThreeWire);
|
||||
drawButton(BTN_2WIRE, "2-WIRE", gMode == Mode::TwoWire);
|
||||
drawButton(BTN_RESET, "RESET", false);
|
||||
|
||||
gfx->drawRoundRect(8, 76, 304, 64, 8, C_BORDER);
|
||||
gfx->drawRoundRect(8, 148, 304, 54, 8, C_BORDER);
|
||||
gfx->drawRoundRect(8, 208, 304, 24, 6, C_BORDER);
|
||||
}
|
||||
|
||||
static void drawBar(uint8_t pct, uint16_t color) {
|
||||
int x = 18;
|
||||
int y = 123;
|
||||
int w = 284;
|
||||
int h = 10;
|
||||
gfx->fillRect(x, y, w, h, C_BORDER);
|
||||
gfx->fillRect(x, y, (w * pct) / 100, h, color);
|
||||
}
|
||||
|
||||
static void updateUi() {
|
||||
Sample s = readSample();
|
||||
if (s.mv < gMinMv) gMinMv = s.mv;
|
||||
if (s.mv > gMaxMv) gMaxMv = s.mv;
|
||||
uint32_t span = (gMaxMv > gMinMv) ? (gMaxMv - gMinMv) : 0;
|
||||
|
||||
if (!gHasEma) {
|
||||
gEmaMv = (float)s.mv;
|
||||
gHasEma = true;
|
||||
} else {
|
||||
gEmaMv = gEmaMv * 0.85f + (float)s.mv * 0.15f;
|
||||
}
|
||||
|
||||
uint8_t pctAbs = percentFromMv(s.mv);
|
||||
uint8_t pctCal = calibratedPercent(s.mv, span);
|
||||
float ohms = estimateOhms(s.mv);
|
||||
const char *status = statusText(s.mv, span, pctAbs);
|
||||
uint16_t statusColor = strcmp(status, "MID") == 0 ? C_OK :
|
||||
strcmp(status, "NO SPAN") == 0 ? C_WARN :
|
||||
strcmp(status, "SATURATED") == 0 ? C_BAD : C_ACCENT;
|
||||
|
||||
char line[96];
|
||||
gfx->fillRect(9, 77, 302, 62, C_BG);
|
||||
snprintf(line, sizeof(line), "%s ADC=IO2 DRIVE=IO6", modeName());
|
||||
drawText(16, 84, line, C_ACCENT, 1);
|
||||
snprintf(line, sizeof(line), "raw:%4u mv:%4lu ema:%4lu",
|
||||
s.raw, (unsigned long)s.mv, (unsigned long)(gEmaMv + 0.5f));
|
||||
drawText(16, 101, line, C_TEXT, 1);
|
||||
snprintf(line, sizeof(line), "abs:%3u%% cal:%3u%% jitter:%u",
|
||||
pctAbs, pctCal, (unsigned)(s.rawMaxBurst - s.rawMinBurst));
|
||||
drawText(16, 115, line, C_TEXT, 1);
|
||||
drawBar(gMode == Mode::ThreeWire ? pctAbs : pctCal, statusColor);
|
||||
|
||||
gfx->fillRect(9, 149, 302, 52, C_BG);
|
||||
snprintf(line, sizeof(line), "min:%4lu max:%4lu span:%4lu mV",
|
||||
(unsigned long)gMinMv, (unsigned long)gMaxMv, (unsigned long)span);
|
||||
drawText(16, 156, line, C_TEXT, 1);
|
||||
if (ohms < 0) {
|
||||
snprintf(line, sizeof(line), "ohm: open/too high status:%s", status);
|
||||
} else {
|
||||
snprintf(line, sizeof(line), "ohm:%6.1fk%s status:%s",
|
||||
ohms / 1000.0f, gMode == Mode::TwoWire ? " rough" : "", status);
|
||||
}
|
||||
drawText(16, 172, line, statusColor, 1);
|
||||
snprintf(line, sizeof(line), "touch:%s x:%u y:%u serial:m/r",
|
||||
gTouchOk ? "ok" : "off", gLastTouchX, gLastTouchY);
|
||||
drawText(16, 188, line, C_MUTE, 1);
|
||||
|
||||
gfx->fillRect(9, 209, 302, 22, C_BG);
|
||||
if (gMode == Mode::ThreeWire) {
|
||||
drawText(16, 216, "Wire: 3V3-edge GND-edge IO2-wiper", C_MUTE, 1);
|
||||
} else {
|
||||
drawText(16, 216, "Wire: IO6-edge IO2-wiper third pin open", C_MUTE, 1);
|
||||
}
|
||||
|
||||
Serial.printf("mode=%s raw=%u mv=%lu ema=%.1f abs=%u cal=%u min=%lu max=%lu span=%lu jitter=%u status=%s\n",
|
||||
modeName(), s.raw, (unsigned long)s.mv, gEmaMv, pctAbs, pctCal,
|
||||
(unsigned long)gMinMv, (unsigned long)gMaxMv, (unsigned long)span,
|
||||
(unsigned)(s.rawMaxBurst - s.rawMinBurst), status);
|
||||
}
|
||||
|
||||
static void handleTouch() {
|
||||
bsp_touch_read();
|
||||
uint16_t x = 0;
|
||||
uint16_t y = 0;
|
||||
if (!bsp_touch_get_coordinates(&x, &y)) return;
|
||||
gLastTouchX = x;
|
||||
gLastTouchY = y;
|
||||
|
||||
if (contains(BTN_3WIRE, x, y)) {
|
||||
setMode(Mode::ThreeWire);
|
||||
drawStaticUi();
|
||||
} else if (contains(BTN_2WIRE, x, y)) {
|
||||
setMode(Mode::TwoWire);
|
||||
drawStaticUi();
|
||||
} else if (contains(BTN_RESET, x, y)) {
|
||||
resetRange();
|
||||
}
|
||||
}
|
||||
|
||||
static void handleBootButton() {
|
||||
bool down = digitalRead(PIN_BOOT_BUTTON) == LOW;
|
||||
uint32_t now = millis();
|
||||
if (down != gBootWasDown && now - gBootLastChangeMs > 40) {
|
||||
gBootWasDown = down;
|
||||
gBootLastChangeMs = now;
|
||||
if (!down) {
|
||||
setMode(gMode == Mode::ThreeWire ? Mode::TwoWire : Mode::ThreeWire);
|
||||
drawStaticUi();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void handleSerial() {
|
||||
while (Serial.available() > 0) {
|
||||
char c = (char)Serial.read();
|
||||
if (c == 'm' || c == 'M') {
|
||||
setMode(gMode == Mode::ThreeWire ? Mode::TwoWire : Mode::ThreeWire);
|
||||
drawStaticUi();
|
||||
} else if (c == 'r' || c == 'R') {
|
||||
resetRange();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(100);
|
||||
Serial.println("ESP32-S3-LCD-2 potentiometer ADC test");
|
||||
Serial.println("Commands: m=switch mode, r=reset min/max");
|
||||
|
||||
pinMode(PIN_BOOT_BUTTON, INPUT_PULLUP);
|
||||
configurePins();
|
||||
|
||||
if (!gfx->begin()) {
|
||||
Serial.println("gfx->begin() failed");
|
||||
}
|
||||
pinMode(PIN_LCD_BL, OUTPUT);
|
||||
digitalWrite(PIN_LCD_BL, HIGH);
|
||||
|
||||
Wire.begin(PIN_TP_SDA, PIN_TP_SCL);
|
||||
gTouchOk = bsp_touch_init(&Wire, 1, DISP_W, DISP_H);
|
||||
|
||||
drawStaticUi();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
handleSerial();
|
||||
handleBootButton();
|
||||
if (gTouchOk) handleTouch();
|
||||
|
||||
uint32_t now = millis();
|
||||
if (now - gLastUiMs >= 250) {
|
||||
updateUi();
|
||||
gLastUiMs = now;
|
||||
}
|
||||
if (now - gLastSerialMs >= 5000) {
|
||||
Serial.println("Hint: m switches 3-WIRE/2-WIRE, r resets min/max. Never feed 5V into IO2/IO6.");
|
||||
gLastSerialMs = now;
|
||||
}
|
||||
}
|
||||
@@ -47,24 +47,20 @@ case "${MODE}" in
|
||||
lvgl-official-based-test) SKETCH_DIR="${ROOT_DIR}/test_sketches/lvgl_official_based_test" ;;
|
||||
lvgl-subserver-touch-test) SKETCH_DIR="${ROOT_DIR}/test_sketches/lvgl_subserver_touch_test" ;;
|
||||
lvgl-russian-font-test) SKETCH_DIR="${ROOT_DIR}/test_sketches/lvgl_russian_font_test" ;;
|
||||
usb-contact-test) SKETCH_DIR="${ROOT_DIR}/test_sketches/usb_contact_sensor_test" ;;
|
||||
usb-contact-debug-test) SKETCH_DIR="${ROOT_DIR}/test_sketches/usb_contact_sensor_debug_test" ;;
|
||||
usb-contact-resistance-test) SKETCH_DIR="${ROOT_DIR}/test_sketches/usb_contact_resistance_test" ;;
|
||||
usb-contact-screen-start-stub) SKETCH_DIR="${ROOT_DIR}/test_sketches/usb_contact_screen_start_stub" ;;
|
||||
lvgl-nav-minimal-test) SKETCH_DIR="${ROOT_DIR}/shine_homeserver_main" ;;
|
||||
*)
|
||||
echo "Unknown mode: ${MODE}" >&2
|
||||
echo "Use one of: hello, widgets, audio, simple, argon2, homeserver-ui, shine-homeserver-main, shine-homeserver-ui-main, legacy-homeserver-ui, text-test, gfx-text-test, gfx-layout-test, lvgl-basic-test, lvgl-interaction-test, lvgl-touch-debug-test, lvgl-official-based-test, lvgl-subserver-touch-test, lvgl-russian-font-test" >&2
|
||||
echo "Use one of: hello, widgets, audio, simple, argon2, homeserver-ui, shine-homeserver-main, shine-homeserver-ui-main, legacy-homeserver-ui, text-test, gfx-text-test, gfx-layout-test, lvgl-basic-test, lvgl-interaction-test, lvgl-touch-debug-test, lvgl-official-based-test, lvgl-subserver-touch-test, lvgl-russian-font-test, usb-contact-test, usb-contact-debug-test, usb-contact-resistance-test, usb-contact-screen-start-stub" >&2
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
|
||||
if [[ -z "${PORT}" ]]; then
|
||||
if ! PORT="$(detect_port)"; then
|
||||
echo "Failed to auto-detect ESP32 port. Set PORT=/dev/ttyACM0 ./burn.sh ${MODE}" >&2
|
||||
exit 3
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "== Mode: ${MODE}"
|
||||
echo "== Sketch: ${SKETCH_DIR}"
|
||||
echo "== Port: ${PORT}"
|
||||
echo "== FQBN: ${FQBN}"
|
||||
|
||||
mkdir -p "${BUILD_DIR}" "${OUT_DIR}"
|
||||
@@ -78,6 +74,7 @@ compile_args=(
|
||||
--library "${DEMO_BASE}/libraries/XPowersLib"
|
||||
--library "${DEMO_BASE}/libraries/lvgl"
|
||||
--library "${DEMO_BASE}/libraries/Mylibrary"
|
||||
--library "${ROOT_DIR}/simple_av_test"
|
||||
"${SKETCH_DIR}"
|
||||
)
|
||||
|
||||
@@ -87,6 +84,20 @@ if ! arduino-cli compile "${compile_args[@]}"; then
|
||||
arduino-cli compile --clean "${compile_args[@]}"
|
||||
fi
|
||||
|
||||
if [[ "${NO_UPLOAD:-0}" == "1" ]]; then
|
||||
echo "== Done: compiled without upload (NO_UPLOAD=1)."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [[ -z "${PORT}" ]]; then
|
||||
if ! PORT="$(detect_port)"; then
|
||||
echo "Failed to auto-detect ESP32 port. Set PORT=/dev/ttyACM0 ./burn.sh ${MODE}" >&2
|
||||
exit 3
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "== Port: ${PORT}"
|
||||
|
||||
arduino-cli upload \
|
||||
-p "${PORT}" \
|
||||
--fqbn "${FQBN}" \
|
||||
|
||||
@@ -15,6 +15,9 @@
|
||||
- `lvgl_official_based_test/` - минимальный наш экран поверх максимально близкой к официальному `05_LVGL_Widgets` инициализации
|
||||
- `lvgl_subserver_touch_test/` - старый гибридный тест: `LVGL`-экран с инициализацией дисплея и чтением touch из старого `shine_homeserver_ui`; подтверждён на реальном устройстве
|
||||
- `lvgl_russian_font_test/` - тест кастомного кириллического `LVGL`-шрифта с русскими кнопками, длинными строками и рабочим touch
|
||||
- `usb_contact_sensor_test/` - тест переключения нативных USB D-/D+ (`GPIO19/20`) в режим простого замыкаемого контакта после нажатия Start; после перезагрузки снова обычный USB
|
||||
- `usb_contact_sensor_debug_test/` - debug-версия теста замыкания контакта: экранная кнопка проверяет touch, а переход в sensor mode выполняется физической кнопкой BOOT
|
||||
- `usb_contact_resistance_test/` - тест измерения сопротивления между USB D-/D+ через ADC на GPIO20; рассчитан на внешний известный резистор 10 kΩ с D+ на GND
|
||||
- `lvgl_nav_minimal_test/` - старое тестовое имя, этот скетч перенесён в `shine_homeserver_main/` и теперь является основным
|
||||
|
||||
## Запуск
|
||||
@@ -29,3 +32,6 @@
|
||||
- `./burn.sh lvgl-official-based-test`
|
||||
- `./burn.sh lvgl-subserver-touch-test`
|
||||
- `./burn.sh lvgl-russian-font-test`
|
||||
- `./burn.sh usb-contact-test`
|
||||
- `./burn.sh usb-contact-debug-test`
|
||||
- `./burn.sh usb-contact-resistance-test`
|
||||
|
||||
+295
@@ -0,0 +1,295 @@
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
#include <lvgl.h>
|
||||
#include <Arduino_GFX_Library.h>
|
||||
#include "pin_config.h"
|
||||
#include "TouchDrvCSTXXX.hpp"
|
||||
|
||||
#define EXAMPLE_LVGL_TICK_PERIOD_MS 2
|
||||
|
||||
// ESP32-S3 native USB data pins.
|
||||
static constexpr int kUsbDmPin = 19; // USB D-
|
||||
static constexpr int kUsbDpPin = 20; // USB D+, ADC input in resistance mode.
|
||||
static constexpr uint32_t kUiRefreshMs = 120;
|
||||
static constexpr uint32_t kAdcSamples = 32;
|
||||
|
||||
// For a stable ohm reading connect a known resistor from D+ / GPIO20 to GND.
|
||||
// The measured resistance must be connected between D- / GPIO19 and D+ / GPIO20.
|
||||
static constexpr float kKnownPulldownOhms = 10000.0f;
|
||||
static constexpr float kSourceMillivolts = 3300.0f;
|
||||
static constexpr float kOpenThresholdMillivolts = 25.0f;
|
||||
|
||||
static lv_disp_draw_buf_t gDrawBuf;
|
||||
static lv_color_t *gBuf1 = nullptr;
|
||||
static lv_color_t *gBuf2 = nullptr;
|
||||
static lv_obj_t *gModeLabel = nullptr;
|
||||
static lv_obj_t *gResistanceLabel = nullptr;
|
||||
static lv_obj_t *gRawLabel = nullptr;
|
||||
static lv_obj_t *gHintLabel = nullptr;
|
||||
static lv_obj_t *gActionButton = nullptr;
|
||||
static lv_obj_t *gActionButtonLabel = nullptr;
|
||||
|
||||
static TouchDrvCST92xx gTouch;
|
||||
static int16_t gTouchX[5];
|
||||
static int16_t gTouchY[5];
|
||||
|
||||
static bool gSensorMode = false;
|
||||
static uint32_t gLastUiRefreshMs = 0;
|
||||
|
||||
Arduino_DataBus *gBus = new Arduino_ESP32QSPI(
|
||||
LCD_CS, LCD_SCLK, LCD_SDIO0, LCD_SDIO1, LCD_SDIO2, LCD_SDIO3);
|
||||
Arduino_CO5300 *gfx = new Arduino_CO5300(
|
||||
gBus, LCD_RESET, 0, LCD_WIDTH, LCD_HEIGHT, 0, 0, 0, 0);
|
||||
|
||||
static void lvglFlushCb(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *colorP) {
|
||||
uint32_t w = area->x2 - area->x1 + 1;
|
||||
uint32_t h = area->y2 - area->y1 + 1;
|
||||
#if (LV_COLOR_16_SWAP != 0)
|
||||
gfx->draw16bitBeRGBBitmap(area->x1, area->y1, (uint16_t *)&colorP->full, w, h);
|
||||
#else
|
||||
gfx->draw16bitRGBBitmap(area->x1, area->y1, (uint16_t *)&colorP->full, w, h);
|
||||
#endif
|
||||
lv_disp_flush_ready(disp);
|
||||
}
|
||||
|
||||
static void lvglTick(void *arg) {
|
||||
LV_UNUSED(arg);
|
||||
lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
|
||||
}
|
||||
|
||||
static void lvglTouchRead(lv_indev_drv_t *indevDrv, lv_indev_data_t *data) {
|
||||
LV_UNUSED(indevDrv);
|
||||
uint8_t touched = gTouch.getPoint(gTouchX, gTouchY, gTouch.getSupportTouchPoint());
|
||||
if (touched > 0) {
|
||||
data->state = LV_INDEV_STATE_PR;
|
||||
data->point.x = gTouchX[0];
|
||||
data->point.y = gTouchY[0];
|
||||
return;
|
||||
}
|
||||
data->state = LV_INDEV_STATE_REL;
|
||||
}
|
||||
|
||||
static uint32_t readAverageRaw() {
|
||||
uint32_t sum = 0;
|
||||
for (uint32_t i = 0; i < kAdcSamples; i++) {
|
||||
sum += analogRead(kUsbDpPin);
|
||||
delayMicroseconds(250);
|
||||
}
|
||||
return sum / kAdcSamples;
|
||||
}
|
||||
|
||||
static uint32_t readAverageMillivolts() {
|
||||
uint32_t sum = 0;
|
||||
for (uint32_t i = 0; i < kAdcSamples; i++) {
|
||||
sum += analogReadMilliVolts(kUsbDpPin);
|
||||
delayMicroseconds(250);
|
||||
}
|
||||
return sum / kAdcSamples;
|
||||
}
|
||||
|
||||
static void setResistanceText(float ohms) {
|
||||
if (ohms >= 1000000.0f) {
|
||||
String text = String(ohms / 1000000.0f, 2) + " Mohm";
|
||||
lv_label_set_text(gResistanceLabel, text.c_str());
|
||||
} else if (ohms >= 1000.0f) {
|
||||
String text = String(ohms / 1000.0f, 2) + " kohm";
|
||||
lv_label_set_text(gResistanceLabel, text.c_str());
|
||||
} else {
|
||||
String text = String(ohms, 0) + " ohm";
|
||||
lv_label_set_text(gResistanceLabel, text.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
static void updateResistanceUi() {
|
||||
if (!gSensorMode) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t now = millis();
|
||||
if (now - gLastUiRefreshMs < kUiRefreshMs) {
|
||||
return;
|
||||
}
|
||||
gLastUiRefreshMs = now;
|
||||
|
||||
uint32_t raw = readAverageRaw();
|
||||
uint32_t millivolts = readAverageMillivolts();
|
||||
|
||||
if (millivolts <= kOpenThresholdMillivolts) {
|
||||
lv_label_set_text(gResistanceLabel, "OPEN");
|
||||
lv_obj_set_style_text_color(gResistanceLabel, lv_color_hex(0xFFD166), 0);
|
||||
} else if (millivolts >= (uint32_t)(kSourceMillivolts - 80.0f)) {
|
||||
lv_label_set_text(gResistanceLabel, "SHORT / LOW");
|
||||
lv_obj_set_style_text_color(gResistanceLabel, lv_color_hex(0x7CFF8A), 0);
|
||||
} else {
|
||||
float rx = kKnownPulldownOhms * ((kSourceMillivolts / (float)millivolts) - 1.0f);
|
||||
lv_obj_set_style_text_color(gResistanceLabel, lv_color_hex(0x7CFF8A), 0);
|
||||
setResistanceText(rx);
|
||||
}
|
||||
|
||||
String rawText = "ADC raw: " + String(raw);
|
||||
rawText += "\nD+ voltage: " + String(millivolts) + " mV";
|
||||
rawText += "\nKnown pulldown: 10 kohm to GND";
|
||||
lv_label_set_text(gRawLabel, rawText.c_str());
|
||||
}
|
||||
|
||||
static void enterSensorMode() {
|
||||
Serial.flush();
|
||||
delay(30);
|
||||
|
||||
pinMode(kUsbDmPin, OUTPUT);
|
||||
digitalWrite(kUsbDmPin, HIGH);
|
||||
|
||||
pinMode(kUsbDpPin, INPUT);
|
||||
analogReadResolution(12);
|
||||
analogSetPinAttenuation(kUsbDpPin, ADC_11db);
|
||||
|
||||
gSensorMode = true;
|
||||
gLastUiRefreshMs = 0;
|
||||
|
||||
lv_label_set_text(gModeLabel, "Resistance mode");
|
||||
lv_label_set_text(gHintLabel, "D- is 3.3V source. D+ is ADC input.\nUse 10 kohm from D+ to GND for accurate readings.");
|
||||
lv_label_set_text(gActionButtonLabel, "Restart to USB");
|
||||
updateResistanceUi();
|
||||
}
|
||||
|
||||
static void actionButtonEventCb(lv_event_t *event) {
|
||||
if (lv_event_get_code(event) != LV_EVENT_CLICKED) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!gSensorMode) {
|
||||
enterSensorMode();
|
||||
return;
|
||||
}
|
||||
|
||||
lv_label_set_text(gModeLabel, "Restarting...");
|
||||
lv_label_set_text(gHintLabel, "USB mode will return after reboot.");
|
||||
lv_timer_handler();
|
||||
delay(150);
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
static void createUi() {
|
||||
lv_obj_t *screen = lv_scr_act();
|
||||
lv_obj_set_style_bg_color(screen, lv_color_hex(0x0B1320), 0);
|
||||
lv_obj_set_style_bg_opa(screen, LV_OPA_COVER, 0);
|
||||
|
||||
lv_obj_t *title = lv_label_create(screen);
|
||||
lv_label_set_text(title, "USB RESISTANCE TEST");
|
||||
lv_obj_set_style_text_color(title, lv_color_hex(0xFFFFFF), 0);
|
||||
lv_obj_set_style_text_font(title, &lv_font_montserrat_22, 0);
|
||||
lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 24);
|
||||
|
||||
gModeLabel = lv_label_create(screen);
|
||||
lv_label_set_text(gModeLabel, "USB mode");
|
||||
lv_obj_set_style_text_color(gModeLabel, lv_color_hex(0xB8C8DC), 0);
|
||||
lv_obj_set_style_text_font(gModeLabel, &lv_font_montserrat_18, 0);
|
||||
lv_obj_align(gModeLabel, LV_ALIGN_TOP_MID, 0, 68);
|
||||
|
||||
gResistanceLabel = lv_label_create(screen);
|
||||
lv_label_set_text(gResistanceLabel, "READY");
|
||||
lv_obj_set_style_text_color(gResistanceLabel, lv_color_hex(0x72D6FF), 0);
|
||||
lv_obj_set_style_text_font(gResistanceLabel, &lv_font_montserrat_40, 0);
|
||||
lv_obj_align(gResistanceLabel, LV_ALIGN_CENTER, 0, -72);
|
||||
|
||||
gRawLabel = lv_label_create(screen);
|
||||
lv_label_set_text(gRawLabel, "ADC raw: -\nD+ voltage: -\nKnown pulldown: 10 kohm to GND");
|
||||
lv_obj_set_width(gRawLabel, 420);
|
||||
lv_label_set_long_mode(gRawLabel, LV_LABEL_LONG_WRAP);
|
||||
lv_obj_set_style_text_align(gRawLabel, LV_TEXT_ALIGN_CENTER, 0);
|
||||
lv_obj_set_style_text_color(gRawLabel, lv_color_hex(0xD5DCE5), 0);
|
||||
lv_obj_set_style_text_font(gRawLabel, &lv_font_montserrat_16, 0);
|
||||
lv_obj_align(gRawLabel, LV_ALIGN_CENTER, 0, -8);
|
||||
|
||||
gHintLabel = lv_label_create(screen);
|
||||
lv_label_set_text(gHintLabel, "USB is normal now.\nTap Start to measure resistance between D- and D+.");
|
||||
lv_obj_set_width(gHintLabel, 420);
|
||||
lv_label_set_long_mode(gHintLabel, LV_LABEL_LONG_WRAP);
|
||||
lv_obj_set_style_text_align(gHintLabel, LV_TEXT_ALIGN_CENTER, 0);
|
||||
lv_obj_set_style_text_color(gHintLabel, lv_color_hex(0xAAB7C6), 0);
|
||||
lv_obj_set_style_text_font(gHintLabel, &lv_font_montserrat_14, 0);
|
||||
lv_obj_align(gHintLabel, LV_ALIGN_CENTER, 0, 72);
|
||||
|
||||
gActionButton = lv_btn_create(screen);
|
||||
lv_obj_set_size(gActionButton, 260, 72);
|
||||
lv_obj_align(gActionButton, LV_ALIGN_BOTTOM_MID, 0, -42);
|
||||
lv_obj_set_style_radius(gActionButton, 18, 0);
|
||||
lv_obj_set_style_bg_color(gActionButton, lv_color_hex(0x2A9D8F), 0);
|
||||
lv_obj_set_style_border_width(gActionButton, 0, 0);
|
||||
lv_obj_add_event_cb(gActionButton, actionButtonEventCb, LV_EVENT_CLICKED, nullptr);
|
||||
|
||||
gActionButtonLabel = lv_label_create(gActionButton);
|
||||
lv_label_set_text(gActionButtonLabel, "Start");
|
||||
lv_obj_set_style_text_color(gActionButtonLabel, lv_color_hex(0xFFFFFF), 0);
|
||||
lv_obj_set_style_text_font(gActionButtonLabel, &lv_font_montserrat_22, 0);
|
||||
lv_obj_center(gActionButtonLabel);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
Wire.begin(IIC_SDA, IIC_SCL);
|
||||
|
||||
pinMode(TP_RST, OUTPUT);
|
||||
pinMode(TP_INT, INPUT);
|
||||
digitalWrite(TP_RST, LOW);
|
||||
delay(30);
|
||||
digitalWrite(TP_RST, HIGH);
|
||||
delay(50);
|
||||
delay(1000);
|
||||
|
||||
gTouch.setPins(TP_RST, TP_INT);
|
||||
bool touchOk = gTouch.begin(Wire, 0x5A, IIC_SDA, IIC_SCL);
|
||||
if (!touchOk) {
|
||||
Serial.println("Touch init failed");
|
||||
} else {
|
||||
gTouch.sleep();
|
||||
gTouch.reset();
|
||||
gTouch.setMaxCoordinates(480, 480);
|
||||
gTouch.setSwapXY(true);
|
||||
gTouch.setMirrorXY(true, false);
|
||||
}
|
||||
|
||||
gfx->begin();
|
||||
gBus->writeC8D8(0x36, 0xA0);
|
||||
gfx->setBrightness(220);
|
||||
gfx->fillScreen(0x0000);
|
||||
|
||||
lv_init();
|
||||
|
||||
uint32_t screenWidth = gfx->width();
|
||||
uint32_t screenHeight = gfx->height();
|
||||
gBuf1 = (lv_color_t *)heap_caps_malloc(screenWidth * screenHeight / 4 * sizeof(lv_color_t), MALLOC_CAP_DMA);
|
||||
gBuf2 = (lv_color_t *)heap_caps_malloc(screenWidth * screenHeight / 4 * sizeof(lv_color_t), MALLOC_CAP_DMA);
|
||||
lv_disp_draw_buf_init(&gDrawBuf, gBuf1, gBuf2, screenWidth * screenHeight / 4);
|
||||
|
||||
static lv_disp_drv_t dispDrv;
|
||||
lv_disp_drv_init(&dispDrv);
|
||||
dispDrv.hor_res = screenWidth;
|
||||
dispDrv.ver_res = screenHeight;
|
||||
dispDrv.flush_cb = lvglFlushCb;
|
||||
dispDrv.draw_buf = &gDrawBuf;
|
||||
lv_disp_drv_register(&dispDrv);
|
||||
|
||||
static lv_indev_drv_t indevDrv;
|
||||
lv_indev_drv_init(&indevDrv);
|
||||
indevDrv.type = LV_INDEV_TYPE_POINTER;
|
||||
indevDrv.read_cb = lvglTouchRead;
|
||||
lv_indev_drv_register(&indevDrv);
|
||||
|
||||
const esp_timer_create_args_t lvglTickTimerArgs = {
|
||||
.callback = &lvglTick,
|
||||
.name = "lvgl_tick"};
|
||||
esp_timer_handle_t lvglTickTimer = nullptr;
|
||||
esp_timer_create(&lvglTickTimerArgs, &lvglTickTimer);
|
||||
esp_timer_start_periodic(lvglTickTimer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000);
|
||||
|
||||
createUi();
|
||||
Serial.println("USB resistance test ready; native USB remains active until Start is tapped.");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
updateResistanceUi();
|
||||
lv_timer_handler();
|
||||
delay(5);
|
||||
}
|
||||
+600
@@ -0,0 +1,600 @@
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
#include <math.h>
|
||||
#include <Preferences.h>
|
||||
#include <Arduino_GFX_Library.h>
|
||||
#include <TouchDrvCSTXXX.hpp>
|
||||
#include "ESP_I2S.h"
|
||||
#include "es8311.h"
|
||||
|
||||
#define PIN_LCD_CS 12
|
||||
#define PIN_LCD_SCLK 38
|
||||
#define PIN_LCD_D0 4
|
||||
#define PIN_LCD_D1 5
|
||||
#define PIN_LCD_D2 6
|
||||
#define PIN_LCD_D3 7
|
||||
#define PIN_LCD_RST 2
|
||||
#define PIN_I2C_SDA 15
|
||||
#define PIN_I2C_SCL 14
|
||||
#define PIN_TP_INT 11
|
||||
#define PIN_USER_BTN 0
|
||||
#define PIN_PA 46
|
||||
#define PIN_I2S_BCLK 9
|
||||
#define PIN_I2S_LRCK 45
|
||||
#define PIN_I2S_DOUT 8
|
||||
#define PIN_I2S_DIN 10
|
||||
#define PIN_I2S_MCLK 42
|
||||
|
||||
#define DISP_W 480
|
||||
#define DISP_H 480
|
||||
|
||||
static constexpr int kUsbDmPin = 19;
|
||||
static constexpr int kUsbDpPin = 20;
|
||||
static constexpr uint32_t kContactDebounceMs = 80;
|
||||
static constexpr uint32_t kButtonDebounceMs = 45;
|
||||
static constexpr uint32_t kUiPeriodMs = 200;
|
||||
static constexpr uint32_t kSampleRate = 16000;
|
||||
static constexpr uint32_t kMinCycleMs = 1500;
|
||||
static constexpr uint32_t kPauseAfterMs = 60000;
|
||||
|
||||
#define C_BG 0x0841u
|
||||
#define C_PANEL 0x1082u
|
||||
#define C_CARD 0x18C3u
|
||||
#define C_BORDER 0x39C7u
|
||||
#define C_TEXT 0xFFFFu
|
||||
#define C_MUTE 0xBDF7u
|
||||
#define C_ACCENT 0x5D5Fu
|
||||
#define C_OK 0x3666u
|
||||
#define C_BAD 0xD145u
|
||||
#define C_BUTTON 0x2145u
|
||||
|
||||
Arduino_DataBus *gBus = new Arduino_ESP32QSPI(
|
||||
PIN_LCD_CS, PIN_LCD_SCLK, PIN_LCD_D0, PIN_LCD_D1, PIN_LCD_D2, PIN_LCD_D3);
|
||||
Arduino_CO5300 *gfx = new Arduino_CO5300(
|
||||
gBus, PIN_LCD_RST, 0, DISP_W, DISP_H, 0, 0, 0, 0);
|
||||
|
||||
TouchDrvCST92xx gTouch;
|
||||
I2SClass gI2s;
|
||||
Preferences gPrefs;
|
||||
|
||||
struct Rect {
|
||||
int16_t x;
|
||||
int16_t y;
|
||||
int16_t w;
|
||||
int16_t h;
|
||||
};
|
||||
|
||||
static const Rect kStartButton = {96, 348, 288, 78};
|
||||
static const Rect kLimitSlider = {42, 132, 396, 44};
|
||||
static const Rect kTickSlider = {42, 246, 396, 44};
|
||||
static const Rect kAlertSlider = {42, 360, 396, 44};
|
||||
static const uint16_t kLimitsSec[] = {5, 7, 10, 15, 20};
|
||||
static const int16_t kTickAmpByVolume[] = {0, 40, 80, 140, 220, 320, 440, 580, 720, 800, 1600};
|
||||
static const int16_t kAlertAmpByVolume[] = {0, 150, 300, 500, 800, 12000, 12000, 12000, 12000, 12000, 12000};
|
||||
|
||||
static bool gStarted = false;
|
||||
static bool gSettingsOpen = false;
|
||||
static bool gAudioReady = false;
|
||||
static bool gStableClosed = false;
|
||||
static bool gLastRawClosed = false;
|
||||
static bool gStaticDrawn = false;
|
||||
static bool gNeedRedraw = true;
|
||||
static bool gNeedStaticRedraw = true;
|
||||
static bool gLastDrawnClosed = false;
|
||||
static bool gLastDrawnPaused = false;
|
||||
static bool gTouchDown = false;
|
||||
static bool gUserButtonDown = false;
|
||||
static int16_t gTouchStartX = 0;
|
||||
static int16_t gTouchStartY = 0;
|
||||
static int16_t gTouchLastX = 0;
|
||||
static int16_t gTouchLastY = 0;
|
||||
static uint32_t gLastRawChangeMs = 0;
|
||||
static uint32_t gStartedMs = 0;
|
||||
static uint32_t gLastCycleStartMs = 0;
|
||||
static uint32_t gLastUiMs = 0;
|
||||
static uint32_t gUserButtonChangeMs = 0;
|
||||
static uint32_t gCycleMs[5] = {};
|
||||
static uint8_t gCycleCount = 0;
|
||||
static uint8_t gCycleNext = 0;
|
||||
static uint8_t gLimitIndex = 2;
|
||||
static uint8_t gTickVolume = 3;
|
||||
static uint8_t gAlertVolume = 10;
|
||||
static uint8_t gShortStreak = 0;
|
||||
static char gLastTopLine[48] = "";
|
||||
static char gLastStreakLine[48] = "";
|
||||
|
||||
static void drawPanel(int x, int y, int w, int h, uint16_t fill, uint16_t border, int radius = 10) {
|
||||
gfx->fillRoundRect(x, y, w, h, radius, fill);
|
||||
gfx->drawRoundRect(x, y, w, h, radius, border);
|
||||
}
|
||||
|
||||
static void drawText(int x, int y, const char *text, uint16_t color, uint8_t size = 2) {
|
||||
gfx->setFont();
|
||||
gfx->setTextSize(size);
|
||||
gfx->setTextColor(color);
|
||||
gfx->setCursor(x, y);
|
||||
gfx->print(text);
|
||||
}
|
||||
|
||||
static void drawCenteredText(int x, int y, int w, const char *text, uint16_t color, uint8_t size = 2) {
|
||||
int textW = strlen(text) * 6 * size;
|
||||
int textX = x + (w - textW) / 2;
|
||||
if (textX < x + 8) {
|
||||
textX = x + 8;
|
||||
}
|
||||
drawText(textX, y, text, color, size);
|
||||
}
|
||||
|
||||
static void drawButton(const Rect &rect, const char *label, uint8_t size = 2) {
|
||||
drawPanel(rect.x, rect.y, rect.w, rect.h, C_BUTTON, C_BORDER, 12);
|
||||
drawCenteredText(rect.x, rect.y + 18, rect.w, label, C_TEXT, size);
|
||||
}
|
||||
|
||||
static bool contains(const Rect &rect, int16_t x, int16_t y) {
|
||||
return x >= rect.x && x < rect.x + rect.w && y >= rect.y && y < rect.y + rect.h;
|
||||
}
|
||||
|
||||
static void clearArea(int x, int y, int w, int h, uint16_t fill) {
|
||||
gfx->fillRect(x, y, w, h, fill);
|
||||
}
|
||||
|
||||
static void formatSec(char *out, size_t outSize, uint32_t ms) {
|
||||
snprintf(out, outSize, "%lu.%01lus", (unsigned long)(ms / 1000), (unsigned long)((ms % 1000) / 100));
|
||||
}
|
||||
|
||||
static void formatClock(char *out, size_t outSize, uint32_t ms) {
|
||||
uint32_t sec = ms / 1000;
|
||||
snprintf(out, outSize, "%02lu:%02lu", (unsigned long)(sec / 60), (unsigned long)(sec % 60));
|
||||
}
|
||||
|
||||
static uint32_t averageCycleMs() {
|
||||
if (gCycleCount == 0) {
|
||||
return 0;
|
||||
}
|
||||
uint32_t sum = 0;
|
||||
for (uint8_t i = 0; i < gCycleCount; i++) {
|
||||
sum += gCycleMs[i];
|
||||
}
|
||||
return sum / gCycleCount;
|
||||
}
|
||||
|
||||
static void resetCachedUiText() {
|
||||
gLastTopLine[0] = '\0';
|
||||
gLastStreakLine[0] = '\0';
|
||||
}
|
||||
|
||||
static void loadSettings() {
|
||||
gPrefs.begin("breath-mon", false);
|
||||
gLimitIndex = gPrefs.getUChar("limit", gLimitIndex);
|
||||
gTickVolume = gPrefs.getUChar("tick", gTickVolume);
|
||||
gAlertVolume = gPrefs.getUChar("alert", gAlertVolume);
|
||||
if (gLimitIndex > 4) {
|
||||
gLimitIndex = 2;
|
||||
}
|
||||
if (gTickVolume > 10) {
|
||||
gTickVolume = 3;
|
||||
}
|
||||
if (gAlertVolume > 10) {
|
||||
gAlertVolume = 10;
|
||||
}
|
||||
}
|
||||
|
||||
static void saveSettings() {
|
||||
gPrefs.putUChar("limit", gLimitIndex);
|
||||
gPrefs.putUChar("tick", gTickVolume);
|
||||
gPrefs.putUChar("alert", gAlertVolume);
|
||||
}
|
||||
|
||||
static bool isAutoPaused() {
|
||||
return gStarted && gLastCycleStartMs != 0 && (millis() - gLastCycleStartMs) > kPauseAfterMs;
|
||||
}
|
||||
|
||||
static esp_err_t codecInit() {
|
||||
es8311_handle_t esHandle = es8311_create(0, ES8311_ADDRRES_0);
|
||||
if (!esHandle) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
const es8311_clock_config_t clk = {
|
||||
.mclk_inverted = false,
|
||||
.sclk_inverted = false,
|
||||
.mclk_from_mclk_pin = true,
|
||||
.mclk_frequency = (int)kSampleRate * 256,
|
||||
.sample_frequency = (int)kSampleRate,
|
||||
};
|
||||
if (es8311_init(esHandle, &clk, ES8311_RESOLUTION_16, ES8311_RESOLUTION_16) != ESP_OK) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
es8311_voice_volume_set(esHandle, 100, nullptr);
|
||||
es8311_voice_mute(esHandle, false);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void initAudio() {
|
||||
pinMode(PIN_PA, OUTPUT);
|
||||
digitalWrite(PIN_PA, HIGH);
|
||||
gI2s.setPins(PIN_I2S_BCLK, PIN_I2S_LRCK, PIN_I2S_DOUT, PIN_I2S_DIN, PIN_I2S_MCLK);
|
||||
if (!gI2s.begin(I2S_MODE_STD, kSampleRate, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO, I2S_STD_SLOT_BOTH)) {
|
||||
Serial.println("I2S init failed");
|
||||
return;
|
||||
}
|
||||
gAudioReady = (codecInit() == ESP_OK);
|
||||
Serial.println(gAudioReady ? "Audio init OK" : "Audio codec init failed");
|
||||
}
|
||||
|
||||
static void playBeep(uint16_t freq, uint16_t samples, int16_t amp) {
|
||||
if (!gAudioReady || amp <= 0) {
|
||||
return;
|
||||
}
|
||||
static int16_t toneBuf[1600 * 2];
|
||||
if (samples > 1600) {
|
||||
samples = 1600;
|
||||
}
|
||||
for (int i = 0; i < samples; i++) {
|
||||
float s = sinf(2.0f * 3.1415926f * (float)freq * ((float)i / (float)kSampleRate));
|
||||
int16_t v = (int16_t)(s * amp);
|
||||
toneBuf[i * 2] = v;
|
||||
toneBuf[i * 2 + 1] = v;
|
||||
}
|
||||
gI2s.write((uint8_t *)toneBuf, samples * 2 * sizeof(int16_t));
|
||||
}
|
||||
|
||||
static void playCycleTick() {
|
||||
playBeep(880, 480, kTickAmpByVolume[gTickVolume]);
|
||||
}
|
||||
|
||||
static void playAlertBeep() {
|
||||
playBeep(1200, 1600, kAlertAmpByVolume[gAlertVolume]);
|
||||
}
|
||||
|
||||
static void alertIfNeeded() {
|
||||
if (gShortStreak == 0) {
|
||||
return;
|
||||
}
|
||||
uint8_t count = min<uint8_t>(gShortStreak, 5);
|
||||
for (uint8_t i = 0; i < count; i++) {
|
||||
playAlertBeep();
|
||||
delay(80);
|
||||
}
|
||||
}
|
||||
|
||||
static void addCycle(uint32_t cycleMs) {
|
||||
gCycleMs[gCycleNext] = cycleMs;
|
||||
gCycleNext = (gCycleNext + 1) % 5;
|
||||
if (gCycleCount < 5) {
|
||||
gCycleCount++;
|
||||
}
|
||||
|
||||
uint32_t avgMs = averageCycleMs();
|
||||
if (avgMs > 0 && avgMs < (uint32_t)kLimitsSec[gLimitIndex] * 1000UL) {
|
||||
if (gShortStreak < 5) {
|
||||
gShortStreak++;
|
||||
}
|
||||
} else {
|
||||
gShortStreak = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void drawReadyScreen() {
|
||||
gfx->fillScreen(C_BG);
|
||||
drawPanel(12, 12, 456, 456, C_PANEL, C_BORDER, 16);
|
||||
drawText(28, 42, "BREATH MONITOR", C_TEXT, 3);
|
||||
drawText(28, 78, "USB contact sensor", C_MUTE, 2);
|
||||
drawPanel(34, 132, 412, 160, C_CARD, C_BORDER, 14);
|
||||
drawCenteredText(34, 166, 412, "READY", C_ACCENT, 4);
|
||||
drawCenteredText(34, 222, 412, "Closed = exhale", C_TEXT, 2);
|
||||
drawCenteredText(34, 252, 412, "Open = inhale", C_MUTE, 2);
|
||||
drawButton(kStartButton, "START", 3);
|
||||
}
|
||||
|
||||
static void drawSlider(const Rect &rect, const char *title, const char *value, uint8_t pos, uint8_t maxPos) {
|
||||
drawText(rect.x, rect.y - 28, title, C_TEXT, 2);
|
||||
drawText(rect.x + 278, rect.y - 28, value, C_ACCENT, 2);
|
||||
drawPanel(rect.x, rect.y, rect.w, rect.h, C_BUTTON, C_BORDER, 12);
|
||||
int trackX = rect.x + 22;
|
||||
int trackW = rect.w - 44;
|
||||
int trackY = rect.y + rect.h / 2 - 3;
|
||||
gfx->fillRect(trackX, trackY, trackW, 6, C_BORDER);
|
||||
int knobX = trackX + (trackW * pos) / max<uint8_t>(1, maxPos);
|
||||
gfx->fillRoundRect(knobX - 10, rect.y + 8, 20, rect.h - 16, 8, C_ACCENT);
|
||||
}
|
||||
|
||||
static void drawSettingsScreen() {
|
||||
char value[24];
|
||||
gfx->fillScreen(C_BG);
|
||||
drawPanel(12, 12, 456, 456, C_PANEL, C_BORDER, 16);
|
||||
drawText(28, 36, "SETTINGS", C_TEXT, 3);
|
||||
drawText(28, 72, "Press top button to close", C_MUTE, 2);
|
||||
|
||||
snprintf(value, sizeof(value), "%us", kLimitsSec[gLimitIndex]);
|
||||
drawSlider(kLimitSlider, "Limit", value, gLimitIndex, 4);
|
||||
snprintf(value, sizeof(value), "%u/10", gTickVolume);
|
||||
drawSlider(kTickSlider, "Cycle tick volume", value, gTickVolume, 10);
|
||||
snprintf(value, sizeof(value), "%u/10", gAlertVolume);
|
||||
drawSlider(kAlertSlider, "Alert volume", value, gAlertVolume, 10);
|
||||
|
||||
gStaticDrawn = false;
|
||||
gNeedStaticRedraw = true;
|
||||
}
|
||||
|
||||
static void drawMonitorStatic() {
|
||||
gfx->fillScreen(C_BG);
|
||||
drawPanel(12, 12, 456, 456, C_PANEL, C_BORDER, 16);
|
||||
drawText(28, 32, "BREATH MONITOR", C_TEXT, 2);
|
||||
drawPanel(28, 96, 424, 104, C_CARD, C_BORDER, 14);
|
||||
drawPanel(28, 216, 200, 72, C_CARD, C_BORDER, 10);
|
||||
drawText(42, 236, "Current", C_MUTE, 1);
|
||||
drawPanel(252, 216, 200, 72, C_CARD, C_BORDER, 10);
|
||||
drawText(266, 236, "Average 5", C_MUTE, 1);
|
||||
drawPanel(28, 304, 200, 64, C_CARD, C_BORDER, 10);
|
||||
drawText(42, 322, "From start", C_MUTE, 1);
|
||||
drawPanel(252, 304, 200, 64, C_CARD, C_BORDER, 10);
|
||||
drawText(266, 322, "Limit", C_MUTE, 1);
|
||||
gStaticDrawn = true;
|
||||
gLastDrawnClosed = !gStableClosed;
|
||||
gLastDrawnPaused = !isAutoPaused();
|
||||
resetCachedUiText();
|
||||
}
|
||||
|
||||
static void updateMonitorValues(bool force) {
|
||||
char line[48];
|
||||
char value[16];
|
||||
uint32_t now = millis();
|
||||
uint32_t runMs = now - gStartedMs;
|
||||
uint32_t currentMs = (gLastCycleStartMs == 0) ? 0 : now - gLastCycleStartMs;
|
||||
bool paused = isAutoPaused();
|
||||
if (paused) {
|
||||
currentMs = now - gLastCycleStartMs - kPauseAfterMs;
|
||||
}
|
||||
uint32_t avgMs = averageCycleMs();
|
||||
|
||||
if (force || gLastDrawnClosed != gStableClosed || gLastDrawnPaused != paused) {
|
||||
clearArea(42, 118, 396, 68, C_CARD);
|
||||
if (paused) {
|
||||
drawCenteredText(28, 122, 424, "PAUSE", C_BAD, 4);
|
||||
drawCenteredText(28, 172, 424, "no cycle for 60s", C_MUTE, 2);
|
||||
} else {
|
||||
drawCenteredText(28, 122, 424, gStableClosed ? "EXHALE" : "INHALE",
|
||||
gStableClosed ? C_OK : C_ACCENT, 4);
|
||||
drawCenteredText(28, 172, 424, gStableClosed ? "contact closed" : "contact open", C_MUTE, 2);
|
||||
}
|
||||
gLastDrawnClosed = gStableClosed;
|
||||
gLastDrawnPaused = paused;
|
||||
}
|
||||
|
||||
snprintf(line, sizeof(line), "Limit: %us top button: settings", kLimitsSec[gLimitIndex]);
|
||||
if (strcmp(line, gLastTopLine) != 0) {
|
||||
clearArea(28, 58, 424, 28, C_PANEL);
|
||||
drawText(28, 60, line, C_MUTE, 2);
|
||||
strncpy(gLastTopLine, line, sizeof(gLastTopLine) - 1);
|
||||
gLastTopLine[sizeof(gLastTopLine) - 1] = '\0';
|
||||
}
|
||||
|
||||
clearArea(42, 254, 160, 28, C_CARD);
|
||||
formatSec(value, sizeof(value), currentMs);
|
||||
drawText(42, 256, value, C_TEXT, 3);
|
||||
|
||||
clearArea(266, 254, 160, 28, C_CARD);
|
||||
if (avgMs > 0) {
|
||||
formatSec(value, sizeof(value), avgMs);
|
||||
} else {
|
||||
snprintf(value, sizeof(value), "--");
|
||||
}
|
||||
drawText(266, 256, value, C_TEXT, 3);
|
||||
|
||||
clearArea(42, 340, 160, 24, C_CARD);
|
||||
formatClock(value, sizeof(value), runMs);
|
||||
drawText(42, 342, value, C_TEXT, 2);
|
||||
|
||||
clearArea(266, 340, 160, 24, C_CARD);
|
||||
snprintf(value, sizeof(value), "%us", kLimitsSec[gLimitIndex]);
|
||||
drawText(266, 342, value, C_TEXT, 2);
|
||||
|
||||
snprintf(line, sizeof(line), "Short avg streak: %u", gShortStreak);
|
||||
if (strcmp(line, gLastStreakLine) != 0) {
|
||||
clearArea(28, 398, 280, 28, C_PANEL);
|
||||
drawText(32, 402, line, gShortStreak > 0 ? C_BAD : C_MUTE, 2);
|
||||
strncpy(gLastStreakLine, line, sizeof(gLastStreakLine) - 1);
|
||||
gLastStreakLine[sizeof(gLastStreakLine) - 1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
static void redrawScreen() {
|
||||
if (gSettingsOpen) {
|
||||
drawSettingsScreen();
|
||||
} else if (gStarted) {
|
||||
if (gNeedStaticRedraw || !gStaticDrawn) {
|
||||
drawMonitorStatic();
|
||||
gNeedStaticRedraw = false;
|
||||
}
|
||||
updateMonitorValues(true);
|
||||
} else {
|
||||
drawReadyScreen();
|
||||
}
|
||||
gNeedRedraw = false;
|
||||
gLastUiMs = millis();
|
||||
}
|
||||
|
||||
static void enterSensorMode() {
|
||||
Serial.flush();
|
||||
delay(30);
|
||||
pinMode(kUsbDmPin, OUTPUT);
|
||||
digitalWrite(kUsbDmPin, LOW);
|
||||
pinMode(kUsbDpPin, INPUT_PULLUP);
|
||||
|
||||
uint32_t now = millis();
|
||||
gStarted = true;
|
||||
gStartedMs = now;
|
||||
gLastCycleStartMs = 0;
|
||||
gLastRawClosed = (digitalRead(kUsbDpPin) == LOW);
|
||||
gStableClosed = gLastRawClosed;
|
||||
gLastRawChangeMs = now;
|
||||
if (gStableClosed) {
|
||||
gLastCycleStartMs = now;
|
||||
playCycleTick();
|
||||
}
|
||||
gNeedStaticRedraw = true;
|
||||
gNeedRedraw = true;
|
||||
}
|
||||
|
||||
static void toggleSettings() {
|
||||
gSettingsOpen = !gSettingsOpen;
|
||||
gNeedStaticRedraw = true;
|
||||
gNeedRedraw = true;
|
||||
resetCachedUiText();
|
||||
}
|
||||
|
||||
static void handleStableContact(bool closed) {
|
||||
uint32_t now = millis();
|
||||
gStableClosed = closed;
|
||||
if (closed) {
|
||||
if (gLastCycleStartMs != 0) {
|
||||
uint32_t cycleMs = now - gLastCycleStartMs;
|
||||
if (cycleMs >= kPauseAfterMs) {
|
||||
gShortStreak = 0;
|
||||
gLastCycleStartMs = now;
|
||||
playCycleTick();
|
||||
} else if (cycleMs >= kMinCycleMs) {
|
||||
addCycle(cycleMs);
|
||||
gLastCycleStartMs = now;
|
||||
playCycleTick();
|
||||
alertIfNeeded();
|
||||
}
|
||||
} else {
|
||||
gLastCycleStartMs = now;
|
||||
playCycleTick();
|
||||
}
|
||||
}
|
||||
gNeedRedraw = !gSettingsOpen;
|
||||
}
|
||||
|
||||
static void pollContact() {
|
||||
if (!gStarted) {
|
||||
return;
|
||||
}
|
||||
bool rawClosed = (digitalRead(kUsbDpPin) == LOW);
|
||||
uint32_t now = millis();
|
||||
if (rawClosed != gLastRawClosed) {
|
||||
gLastRawClosed = rawClosed;
|
||||
gLastRawChangeMs = now;
|
||||
}
|
||||
if ((now - gLastRawChangeMs) >= kContactDebounceMs && gStableClosed != gLastRawClosed) {
|
||||
handleStableContact(gLastRawClosed);
|
||||
}
|
||||
}
|
||||
|
||||
static void pollUserButton() {
|
||||
bool down = digitalRead(PIN_USER_BTN) == LOW;
|
||||
uint32_t now = millis();
|
||||
if (down != gUserButtonDown && (now - gUserButtonChangeMs) >= kButtonDebounceMs) {
|
||||
gUserButtonDown = down;
|
||||
gUserButtonChangeMs = now;
|
||||
if (down) {
|
||||
toggleSettings();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t sliderValueFromX(const Rect &rect, int16_t x, uint8_t maxValue) {
|
||||
int16_t left = rect.x + 22;
|
||||
int16_t right = rect.x + rect.w - 22;
|
||||
if (x < left) x = left;
|
||||
if (x > right) x = right;
|
||||
return (uint8_t)(((uint32_t)(x - left) * maxValue + (right - left) / 2) / (right - left));
|
||||
}
|
||||
|
||||
static void handleSettingsTouch(int16_t x, int16_t y) {
|
||||
if (contains(kLimitSlider, x, y)) {
|
||||
uint8_t next = sliderValueFromX(kLimitSlider, x, 4);
|
||||
if (next == gLimitIndex) {
|
||||
return;
|
||||
}
|
||||
gLimitIndex = next;
|
||||
} else if (contains(kTickSlider, x, y)) {
|
||||
uint8_t next = sliderValueFromX(kTickSlider, x, 10);
|
||||
if (next == gTickVolume) {
|
||||
return;
|
||||
}
|
||||
gTickVolume = next;
|
||||
playCycleTick();
|
||||
} else if (contains(kAlertSlider, x, y)) {
|
||||
uint8_t next = sliderValueFromX(kAlertSlider, x, 10);
|
||||
if (next == gAlertVolume) {
|
||||
return;
|
||||
}
|
||||
gAlertVolume = next;
|
||||
playAlertBeep();
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
saveSettings();
|
||||
gNeedRedraw = true;
|
||||
}
|
||||
|
||||
static void pollTouch() {
|
||||
int16_t x = 0;
|
||||
int16_t y = 0;
|
||||
bool touching = gTouch.getPoint(&x, &y, 1) > 0;
|
||||
if (touching && !gTouchDown) {
|
||||
gTouchDown = true;
|
||||
gTouchStartX = x;
|
||||
gTouchStartY = y;
|
||||
gTouchLastX = x;
|
||||
gTouchLastY = y;
|
||||
return;
|
||||
}
|
||||
if (touching && gTouchDown) {
|
||||
gTouchLastX = x;
|
||||
gTouchLastY = y;
|
||||
return;
|
||||
}
|
||||
if (!touching && gTouchDown) {
|
||||
gTouchDown = false;
|
||||
int dx = abs(gTouchLastX - gTouchStartX);
|
||||
int dy = abs(gTouchLastY - gTouchStartY);
|
||||
if (dx >= 14 || dy >= 14) {
|
||||
return;
|
||||
}
|
||||
if (gSettingsOpen) {
|
||||
handleSettingsTouch(gTouchLastX, gTouchLastY);
|
||||
} else if (!gStarted && contains(kStartButton, gTouchLastX, gTouchLastY)) {
|
||||
enterSensorMode();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Wire.begin(PIN_I2C_SDA, PIN_I2C_SCL);
|
||||
pinMode(PIN_USER_BTN, INPUT_PULLUP);
|
||||
loadSettings();
|
||||
initAudio();
|
||||
|
||||
gfx->begin();
|
||||
gBus->writeC8D8(0x36, 0xA0);
|
||||
gfx->setBrightness(220);
|
||||
gfx->fillScreen(C_BG);
|
||||
|
||||
gTouch.setPins(PIN_TP_INT, -1);
|
||||
bool touchOk = gTouch.begin(Wire, CST92XX_SLAVE_ADDRESS, PIN_I2C_SDA, PIN_I2C_SCL);
|
||||
gTouch.setMaxCoordinates(DISP_W, DISP_H);
|
||||
gTouch.setSwapXY(true);
|
||||
gTouch.setMirrorXY(true, false);
|
||||
|
||||
Serial.println("Breath monitor ready.");
|
||||
Serial.println(touchOk ? "Touch init OK" : "Touch init failed");
|
||||
gNeedRedraw = true;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
pollUserButton();
|
||||
pollTouch();
|
||||
pollContact();
|
||||
if (gStarted && !gSettingsOpen && millis() - gLastUiMs >= kUiPeriodMs) {
|
||||
updateMonitorValues(false);
|
||||
gLastUiMs = millis();
|
||||
}
|
||||
if (gNeedRedraw) {
|
||||
redrawScreen();
|
||||
}
|
||||
delay(10);
|
||||
}
|
||||
+407
@@ -0,0 +1,407 @@
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
#include <lvgl.h>
|
||||
#include <Arduino_GFX_Library.h>
|
||||
#include "pin_config.h"
|
||||
#include "TouchDrvCSTXXX.hpp"
|
||||
|
||||
#define EXAMPLE_LVGL_TICK_PERIOD_MS 2
|
||||
|
||||
// ESP32-S3 native USB data pins.
|
||||
static constexpr int kUsbDmPin = 19; // USB D-
|
||||
static constexpr int kUsbDpPin = 20; // USB D+
|
||||
static constexpr int kUserButtonPin = 0; // BOOT/user button, active LOW.
|
||||
static constexpr uint32_t kDebounceMs = 20;
|
||||
static constexpr uint32_t kEnterDelayMs = 250;
|
||||
static constexpr uint32_t kButtonDebounceMs = 35;
|
||||
|
||||
static lv_disp_draw_buf_t gDrawBuf;
|
||||
static lv_color_t *gBuf1 = nullptr;
|
||||
static lv_color_t *gBuf2 = nullptr;
|
||||
|
||||
static lv_obj_t *gRawLabel = nullptr;
|
||||
static lv_obj_t *gEventLabel = nullptr;
|
||||
static lv_obj_t *gModeLabel = nullptr;
|
||||
static lv_obj_t *gStateLabel = nullptr;
|
||||
static lv_obj_t *gActionButton = nullptr;
|
||||
static lv_obj_t *gActionButtonLabel = nullptr;
|
||||
static lv_obj_t *gMarker = nullptr;
|
||||
|
||||
static TouchDrvCST92xx gTouch;
|
||||
static int16_t gTouchX[5];
|
||||
static int16_t gTouchY[5];
|
||||
|
||||
static volatile bool gTouchPressed = false;
|
||||
static volatile int16_t gLastTouchX = -1;
|
||||
static volatile int16_t gLastTouchY = -1;
|
||||
static volatile uint32_t gRawTouchCount = 0;
|
||||
static uint32_t gButtonPressCount = 0;
|
||||
static uint32_t gButtonClickCount = 0;
|
||||
static bool gLastButtonRawPressed = false;
|
||||
static bool gButtonStablePressed = false;
|
||||
static uint32_t gLastButtonRawChangeMs = 0;
|
||||
|
||||
static bool gSensorMode = false;
|
||||
static bool gEnterPending = false;
|
||||
static uint32_t gEnterAtMs = 0;
|
||||
static bool gStableClosed = false;
|
||||
static bool gLastRawClosed = false;
|
||||
static uint32_t gLastRawChangeMs = 0;
|
||||
static uint32_t gLastUiRefreshMs = 0;
|
||||
|
||||
Arduino_DataBus *gBus = new Arduino_ESP32QSPI(
|
||||
LCD_CS, LCD_SCLK, LCD_SDIO0, LCD_SDIO1, LCD_SDIO2, LCD_SDIO3);
|
||||
Arduino_CO5300 *gfx = new Arduino_CO5300(
|
||||
gBus, LCD_RESET, 0, LCD_WIDTH, LCD_HEIGHT, 0, 0, 0, 0);
|
||||
|
||||
static void lvglFlushCb(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *colorP) {
|
||||
uint32_t w = area->x2 - area->x1 + 1;
|
||||
uint32_t h = area->y2 - area->y1 + 1;
|
||||
#if (LV_COLOR_16_SWAP != 0)
|
||||
gfx->draw16bitBeRGBBitmap(area->x1, area->y1, (uint16_t *)&colorP->full, w, h);
|
||||
#else
|
||||
gfx->draw16bitRGBBitmap(area->x1, area->y1, (uint16_t *)&colorP->full, w, h);
|
||||
#endif
|
||||
lv_disp_flush_ready(disp);
|
||||
}
|
||||
|
||||
static void lvglTick(void *arg) {
|
||||
LV_UNUSED(arg);
|
||||
lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
|
||||
}
|
||||
|
||||
static void lvglTouchRead(lv_indev_drv_t *indevDrv, lv_indev_data_t *data) {
|
||||
LV_UNUSED(indevDrv);
|
||||
uint8_t touched = gTouch.getPoint(gTouchX, gTouchY, gTouch.getSupportTouchPoint());
|
||||
|
||||
if (touched > 0) {
|
||||
gTouchPressed = true;
|
||||
gLastTouchX = gTouchX[0];
|
||||
gLastTouchY = gTouchY[0];
|
||||
gRawTouchCount++;
|
||||
|
||||
data->state = LV_INDEV_STATE_PR;
|
||||
data->point.x = gTouchX[0];
|
||||
data->point.y = gTouchY[0];
|
||||
return;
|
||||
}
|
||||
|
||||
gTouchPressed = false;
|
||||
data->state = LV_INDEV_STATE_REL;
|
||||
}
|
||||
|
||||
static void setStateUi(bool closed) {
|
||||
if (closed) {
|
||||
lv_label_set_text(gStateLabel, "CLOSED");
|
||||
lv_obj_set_style_text_color(gStateLabel, lv_color_hex(0x6DFF8B), 0);
|
||||
} else {
|
||||
lv_label_set_text(gStateLabel, "OPEN");
|
||||
lv_obj_set_style_text_color(gStateLabel, lv_color_hex(0xFFD166), 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void updateTouchDiagnostics() {
|
||||
uint32_t now = millis();
|
||||
if (now - gLastUiRefreshMs < 40) return;
|
||||
gLastUiRefreshMs = now;
|
||||
|
||||
String raw = gTouchPressed ? "RAW: PRESSED" : "RAW: released";
|
||||
raw += "\nx=" + String((int)gLastTouchX) + " y=" + String((int)gLastTouchY);
|
||||
raw += " n=" + String((uint32_t)gRawTouchCount);
|
||||
lv_label_set_text(gRawLabel, raw.c_str());
|
||||
|
||||
if (gTouchPressed && gLastTouchX >= 0 && gLastTouchY >= 0) {
|
||||
int markerX = gLastTouchX - 8;
|
||||
int markerY = gLastTouchY - 8;
|
||||
if (markerX < 0) markerX = 0;
|
||||
if (markerY < 0) markerY = 0;
|
||||
lv_obj_set_pos(gMarker, markerX, markerY);
|
||||
lv_obj_clear_flag(gMarker, LV_OBJ_FLAG_HIDDEN);
|
||||
} else {
|
||||
lv_obj_add_flag(gMarker, LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
}
|
||||
|
||||
static void actuallyEnterSensorMode() {
|
||||
gEnterPending = false;
|
||||
|
||||
// Make the visual transition happen BEFORE the USB data pins are repurposed.
|
||||
lv_label_set_text(gModeLabel, "SENSOR MODE");
|
||||
lv_label_set_text(gActionButtonLabel, "Restart to USB");
|
||||
lv_obj_set_style_bg_color(gActionButton, lv_color_hex(0xA33D4F), LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_bg_color(gActionButton, lv_color_hex(0xC94F65), LV_PART_MAIN | LV_STATE_PRESSED);
|
||||
lv_label_set_text(gEventLabel, "GPIO19=D- LOW GPIO20=D+ INPUT_PULLUP");
|
||||
lv_label_set_text(gStateLabel, "SWITCHING...");
|
||||
lv_obj_set_style_text_color(gStateLabel, lv_color_hex(0x72D6FF), 0);
|
||||
lv_refr_now(nullptr);
|
||||
delay(60);
|
||||
|
||||
// Intentionally repurpose only USB D-/D+. These are not LCD or touch pins.
|
||||
// Do NOT call Serial.flush() here: USB CDC may already be disappearing.
|
||||
pinMode(kUsbDmPin, OUTPUT);
|
||||
digitalWrite(kUsbDmPin, LOW);
|
||||
pinMode(kUsbDpPin, INPUT_PULLUP);
|
||||
|
||||
gSensorMode = true;
|
||||
gLastRawClosed = (digitalRead(kUsbDpPin) == LOW);
|
||||
gStableClosed = gLastRawClosed;
|
||||
gLastRawChangeMs = millis();
|
||||
setStateUi(gStableClosed);
|
||||
}
|
||||
|
||||
static void actionButtonEventCb(lv_event_t *event) {
|
||||
lv_event_code_t code = lv_event_get_code(event);
|
||||
|
||||
if (code == LV_EVENT_PRESSED) {
|
||||
gButtonPressCount++;
|
||||
String text = "BUTTON PRESSED #" + String(gButtonPressCount);
|
||||
lv_label_set_text(gEventLabel, text.c_str());
|
||||
|
||||
if (!gSensorMode && !gEnterPending) {
|
||||
lv_label_set_text(gActionButtonLabel, "Starting...");
|
||||
lv_label_set_text(gStateLabel, "TOUCH OK");
|
||||
lv_obj_set_style_text_color(gStateLabel, lv_color_hex(0x7CFF8A), 0);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (code != LV_EVENT_CLICKED) return;
|
||||
|
||||
gButtonClickCount++;
|
||||
|
||||
if (!gSensorMode) {
|
||||
lv_label_set_text(gModeLabel, "USB MODE");
|
||||
lv_label_set_text(gActionButtonLabel, "TOUCH OK");
|
||||
lv_label_set_text(gStateLabel, "PRESS BOOT");
|
||||
lv_obj_set_style_text_color(gStateLabel, lv_color_hex(0xFFD166), 0);
|
||||
String text = "TOUCH CLICK OK #" + String(gButtonClickCount) + " use BOOT button";
|
||||
lv_label_set_text(gEventLabel, text.c_str());
|
||||
lv_refr_now(nullptr);
|
||||
return;
|
||||
}
|
||||
|
||||
lv_label_set_text(gModeLabel, "RESTARTING TO USB...");
|
||||
lv_label_set_text(gActionButtonLabel, "Restarting...");
|
||||
lv_label_set_text(gStateLabel, "USB NEXT BOOT");
|
||||
lv_refr_now(nullptr);
|
||||
delay(180);
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
static void requestEnterSensorMode(const char *source) {
|
||||
if (gSensorMode || gEnterPending) {
|
||||
return;
|
||||
}
|
||||
|
||||
gEnterPending = true;
|
||||
gEnterAtMs = millis() + kEnterDelayMs;
|
||||
lv_label_set_text(gModeLabel, "START RECEIVED");
|
||||
lv_label_set_text(gActionButtonLabel, "Starting sensor...");
|
||||
String text = String(source) + " OK waiting 250 ms";
|
||||
lv_label_set_text(gEventLabel, text.c_str());
|
||||
lv_label_set_text(gStateLabel, "STARTING...");
|
||||
lv_obj_set_style_text_color(gStateLabel, lv_color_hex(0x7CFF8A), 0);
|
||||
lv_refr_now(nullptr);
|
||||
}
|
||||
|
||||
static void pollUserButton() {
|
||||
bool rawPressed = (digitalRead(kUserButtonPin) == LOW);
|
||||
uint32_t now = millis();
|
||||
|
||||
if (rawPressed != gLastButtonRawPressed) {
|
||||
gLastButtonRawPressed = rawPressed;
|
||||
gLastButtonRawChangeMs = now;
|
||||
}
|
||||
|
||||
if ((now - gLastButtonRawChangeMs) < kButtonDebounceMs || rawPressed == gButtonStablePressed) {
|
||||
return;
|
||||
}
|
||||
|
||||
gButtonStablePressed = rawPressed;
|
||||
if (!gButtonStablePressed) {
|
||||
return;
|
||||
}
|
||||
|
||||
gButtonClickCount++;
|
||||
if (!gSensorMode) {
|
||||
requestEnterSensorMode("BOOT BUTTON");
|
||||
return;
|
||||
}
|
||||
|
||||
lv_label_set_text(gModeLabel, "RESTARTING TO USB...");
|
||||
lv_label_set_text(gActionButtonLabel, "Restarting...");
|
||||
lv_label_set_text(gStateLabel, "USB NEXT BOOT");
|
||||
lv_refr_now(nullptr);
|
||||
delay(180);
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
static void createUi() {
|
||||
lv_obj_t *screen = lv_scr_act();
|
||||
lv_obj_set_style_bg_color(screen, lv_color_hex(0x000000), 0);
|
||||
lv_obj_set_style_bg_opa(screen, LV_OPA_COVER, 0);
|
||||
|
||||
lv_obj_t *title = lv_label_create(screen);
|
||||
lv_label_set_text(title, "USB CONTACT DEBUG");
|
||||
lv_obj_set_style_text_font(title, &lv_font_montserrat_22, 0);
|
||||
lv_obj_set_style_text_color(title, lv_color_hex(0xFFFFFF), 0);
|
||||
lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 10);
|
||||
|
||||
gRawLabel = lv_label_create(screen);
|
||||
lv_label_set_text(gRawLabel, "RAW: released\nx=-1 y=-1 n=0");
|
||||
lv_obj_set_style_text_font(gRawLabel, &lv_font_montserrat_16, 0);
|
||||
lv_obj_set_style_text_color(gRawLabel, lv_color_hex(0x9EE493), 0);
|
||||
lv_obj_align(gRawLabel, LV_ALIGN_TOP_LEFT, 14, 48);
|
||||
|
||||
gModeLabel = lv_label_create(screen);
|
||||
lv_label_set_text(gModeLabel, "USB MODE");
|
||||
lv_obj_set_style_text_font(gModeLabel, &lv_font_montserrat_18, 0);
|
||||
lv_obj_set_style_text_color(gModeLabel, lv_color_hex(0x72D6FF), 0);
|
||||
lv_obj_align(gModeLabel, LV_ALIGN_TOP_RIGHT, -14, 50);
|
||||
|
||||
gEventLabel = lv_label_create(screen);
|
||||
lv_label_set_text(gEventLabel, "Button events: waiting");
|
||||
lv_obj_set_width(gEventLabel, 440);
|
||||
lv_label_set_long_mode(gEventLabel, LV_LABEL_LONG_WRAP);
|
||||
lv_obj_set_style_text_align(gEventLabel, LV_TEXT_ALIGN_CENTER, 0);
|
||||
lv_obj_set_style_text_font(gEventLabel, &lv_font_montserrat_16, 0);
|
||||
lv_obj_set_style_text_color(gEventLabel, lv_color_hex(0xFFD166), 0);
|
||||
lv_obj_align(gEventLabel, LV_ALIGN_TOP_MID, 0, 102);
|
||||
|
||||
gStateLabel = lv_label_create(screen);
|
||||
lv_label_set_text(gStateLabel, "READY");
|
||||
lv_obj_set_style_text_font(gStateLabel, &lv_font_montserrat_40, 0);
|
||||
lv_obj_set_style_text_color(gStateLabel, lv_color_hex(0x72D6FF), 0);
|
||||
lv_obj_align(gStateLabel, LV_ALIGN_CENTER, 0, -52);
|
||||
|
||||
// Put the button in almost the same position/size as the known-working touch debug sketch.
|
||||
gActionButton = lv_btn_create(screen);
|
||||
lv_obj_set_size(gActionButton, 360, 110);
|
||||
lv_obj_align(gActionButton, LV_ALIGN_CENTER, 0, 52);
|
||||
lv_obj_set_style_radius(gActionButton, 18, 0);
|
||||
lv_obj_set_style_bg_color(gActionButton, lv_color_hex(0x2A9D8F), LV_PART_MAIN | LV_STATE_DEFAULT);
|
||||
lv_obj_set_style_bg_color(gActionButton, lv_color_hex(0x47C7B6), LV_PART_MAIN | LV_STATE_PRESSED);
|
||||
lv_obj_set_style_border_width(gActionButton, 3, 0);
|
||||
lv_obj_set_style_border_color(gActionButton, lv_color_hex(0xBDE0FE), 0);
|
||||
lv_obj_add_event_cb(gActionButton, actionButtonEventCb, LV_EVENT_ALL, nullptr);
|
||||
|
||||
gActionButtonLabel = lv_label_create(gActionButton);
|
||||
lv_label_set_text(gActionButtonLabel, "START");
|
||||
lv_obj_set_style_text_font(gActionButtonLabel, &lv_font_montserrat_28, 0);
|
||||
lv_obj_set_style_text_color(gActionButtonLabel, lv_color_hex(0xFFFFFF), 0);
|
||||
lv_obj_center(gActionButtonLabel);
|
||||
|
||||
lv_obj_t *hint = lv_label_create(screen);
|
||||
lv_label_set_text(hint, "Press BOOT button to START. Touch button only checks touch UI.");
|
||||
lv_obj_set_width(hint, 440);
|
||||
lv_label_set_long_mode(hint, LV_LABEL_LONG_WRAP);
|
||||
lv_obj_set_style_text_align(hint, LV_TEXT_ALIGN_CENTER, 0);
|
||||
lv_obj_set_style_text_font(hint, &lv_font_montserrat_14, 0);
|
||||
lv_obj_set_style_text_color(hint, lv_color_hex(0xD9D9D9), 0);
|
||||
lv_obj_align(hint, LV_ALIGN_BOTTOM_MID, 0, -18);
|
||||
|
||||
gMarker = lv_obj_create(screen);
|
||||
lv_obj_set_size(gMarker, 16, 16);
|
||||
lv_obj_set_style_radius(gMarker, LV_RADIUS_CIRCLE, 0);
|
||||
lv_obj_set_style_bg_color(gMarker, lv_color_hex(0xFF4D6D), 0);
|
||||
lv_obj_set_style_border_width(gMarker, 2, 0);
|
||||
lv_obj_set_style_border_color(gMarker, lv_color_hex(0xFFFFFF), 0);
|
||||
lv_obj_add_flag(gMarker, LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
|
||||
static void pollContact() {
|
||||
if (!gSensorMode) return;
|
||||
|
||||
bool rawClosed = (digitalRead(kUsbDpPin) == LOW);
|
||||
uint32_t now = millis();
|
||||
|
||||
if (rawClosed != gLastRawClosed) {
|
||||
gLastRawClosed = rawClosed;
|
||||
gLastRawChangeMs = now;
|
||||
}
|
||||
|
||||
if ((now - gLastRawChangeMs) >= kDebounceMs && gStableClosed != gLastRawClosed) {
|
||||
gStableClosed = gLastRawClosed;
|
||||
setStateUi(gStableClosed);
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
pinMode(kUserButtonPin, INPUT_PULLUP);
|
||||
|
||||
Wire.begin(IIC_SDA, IIC_SCL);
|
||||
|
||||
pinMode(TP_RST, OUTPUT);
|
||||
pinMode(TP_INT, INPUT);
|
||||
digitalWrite(TP_RST, LOW);
|
||||
delay(30);
|
||||
digitalWrite(TP_RST, HIGH);
|
||||
delay(50);
|
||||
delay(1000);
|
||||
|
||||
gTouch.setPins(TP_RST, TP_INT);
|
||||
bool touchOk = gTouch.begin(Wire, 0x5A, IIC_SDA, IIC_SCL);
|
||||
if (!touchOk) {
|
||||
Serial.println("Touch init failed");
|
||||
} else {
|
||||
Serial.print("Touch model: ");
|
||||
Serial.println(gTouch.getModelName());
|
||||
gTouch.sleep();
|
||||
gTouch.reset();
|
||||
gTouch.setMaxCoordinates(480, 480);
|
||||
gTouch.setSwapXY(true);
|
||||
gTouch.setMirrorXY(true, false);
|
||||
}
|
||||
|
||||
gfx->begin();
|
||||
gBus->writeC8D8(0x36, 0xA0);
|
||||
gfx->setBrightness(220);
|
||||
gfx->fillScreen(0x0000);
|
||||
|
||||
lv_init();
|
||||
|
||||
uint32_t screenWidth = gfx->width();
|
||||
uint32_t screenHeight = gfx->height();
|
||||
gBuf1 = (lv_color_t *)heap_caps_malloc(screenWidth * screenHeight / 4 * sizeof(lv_color_t), MALLOC_CAP_DMA);
|
||||
gBuf2 = (lv_color_t *)heap_caps_malloc(screenWidth * screenHeight / 4 * sizeof(lv_color_t), MALLOC_CAP_DMA);
|
||||
lv_disp_draw_buf_init(&gDrawBuf, gBuf1, gBuf2, screenWidth * screenHeight / 4);
|
||||
|
||||
static lv_disp_drv_t dispDrv;
|
||||
lv_disp_drv_init(&dispDrv);
|
||||
dispDrv.hor_res = screenWidth;
|
||||
dispDrv.ver_res = screenHeight;
|
||||
dispDrv.flush_cb = lvglFlushCb;
|
||||
dispDrv.draw_buf = &gDrawBuf;
|
||||
lv_disp_drv_register(&dispDrv);
|
||||
|
||||
static lv_indev_drv_t indevDrv;
|
||||
lv_indev_drv_init(&indevDrv);
|
||||
indevDrv.type = LV_INDEV_TYPE_POINTER;
|
||||
indevDrv.read_cb = lvglTouchRead;
|
||||
lv_indev_drv_register(&indevDrv);
|
||||
|
||||
const esp_timer_create_args_t lvglTickTimerArgs = {
|
||||
.callback = &lvglTick,
|
||||
.name = "lvgl_tick"};
|
||||
esp_timer_handle_t lvglTickTimer = nullptr;
|
||||
esp_timer_create(&lvglTickTimerArgs, &lvglTickTimer);
|
||||
esp_timer_start_periodic(lvglTickTimer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000);
|
||||
|
||||
createUi();
|
||||
Serial.println("USB contact sensor DEBUG ready. USB pins remain untouched until START click.");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
updateTouchDiagnostics();
|
||||
pollUserButton();
|
||||
pollContact();
|
||||
|
||||
if (gEnterPending && (int32_t)(millis() - gEnterAtMs) >= 0) {
|
||||
actuallyEnterSensorMode();
|
||||
}
|
||||
|
||||
lv_timer_handler();
|
||||
delay(5);
|
||||
}
|
||||
+250
@@ -0,0 +1,250 @@
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
#include <lvgl.h>
|
||||
#include <Arduino_GFX_Library.h>
|
||||
#include "pin_config.h"
|
||||
#include "TouchDrvCSTXXX.hpp"
|
||||
|
||||
#define EXAMPLE_LVGL_TICK_PERIOD_MS 2
|
||||
|
||||
// ESP32-S3 native USB pins.
|
||||
static constexpr int kUsbDmPin = 19; // USB D-
|
||||
static constexpr int kUsbDpPin = 20; // USB D+
|
||||
static constexpr uint32_t kDebounceMs = 20;
|
||||
|
||||
static lv_disp_draw_buf_t gDrawBuf;
|
||||
static lv_color_t *gBuf1 = nullptr;
|
||||
static lv_color_t *gBuf2 = nullptr;
|
||||
static lv_obj_t *gModeLabel = nullptr;
|
||||
static lv_obj_t *gStateLabel = nullptr;
|
||||
static lv_obj_t *gHintLabel = nullptr;
|
||||
static lv_obj_t *gActionButton = nullptr;
|
||||
static lv_obj_t *gActionButtonLabel = nullptr;
|
||||
|
||||
static TouchDrvCST92xx gTouch;
|
||||
static int16_t gTouchX[5];
|
||||
static int16_t gTouchY[5];
|
||||
|
||||
static bool gSensorMode = false;
|
||||
static bool gStableClosed = false;
|
||||
static bool gLastRawClosed = false;
|
||||
static uint32_t gLastRawChangeMs = 0;
|
||||
|
||||
Arduino_DataBus *gBus = new Arduino_ESP32QSPI(
|
||||
LCD_CS, LCD_SCLK, LCD_SDIO0, LCD_SDIO1, LCD_SDIO2, LCD_SDIO3);
|
||||
Arduino_CO5300 *gfx = new Arduino_CO5300(
|
||||
gBus, LCD_RESET, 0, LCD_WIDTH, LCD_HEIGHT, 0, 0, 0, 0);
|
||||
|
||||
static void lvglFlushCb(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *colorP) {
|
||||
uint32_t w = area->x2 - area->x1 + 1;
|
||||
uint32_t h = area->y2 - area->y1 + 1;
|
||||
#if (LV_COLOR_16_SWAP != 0)
|
||||
gfx->draw16bitBeRGBBitmap(area->x1, area->y1, (uint16_t *)&colorP->full, w, h);
|
||||
#else
|
||||
gfx->draw16bitRGBBitmap(area->x1, area->y1, (uint16_t *)&colorP->full, w, h);
|
||||
#endif
|
||||
lv_disp_flush_ready(disp);
|
||||
}
|
||||
|
||||
static void lvglTick(void *arg) {
|
||||
LV_UNUSED(arg);
|
||||
lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
|
||||
}
|
||||
|
||||
static void lvglTouchRead(lv_indev_drv_t *indevDrv, lv_indev_data_t *data) {
|
||||
LV_UNUSED(indevDrv);
|
||||
uint8_t touched = gTouch.getPoint(gTouchX, gTouchY, gTouch.getSupportTouchPoint());
|
||||
if (touched > 0) {
|
||||
data->state = LV_INDEV_STATE_PR;
|
||||
data->point.x = gTouchX[0];
|
||||
data->point.y = gTouchY[0];
|
||||
return;
|
||||
}
|
||||
data->state = LV_INDEV_STATE_REL;
|
||||
}
|
||||
|
||||
static void setStateUi(bool closed) {
|
||||
if (closed) {
|
||||
lv_label_set_text(gStateLabel, "CLOSED");
|
||||
lv_obj_set_style_text_color(gStateLabel, lv_color_hex(0x7CFF8A), 0);
|
||||
} else {
|
||||
lv_label_set_text(gStateLabel, "OPEN");
|
||||
lv_obj_set_style_text_color(gStateLabel, lv_color_hex(0xFFCC66), 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void enterSensorMode() {
|
||||
// From this moment the native USB data pair is no longer USB.
|
||||
// D- is held LOW, D+ is read with the ESP32-S3 internal pull-up.
|
||||
// A passive switch between D- and D+ therefore reads LOW when closed.
|
||||
Serial.flush();
|
||||
delay(30);
|
||||
|
||||
pinMode(kUsbDmPin, OUTPUT);
|
||||
digitalWrite(kUsbDmPin, LOW);
|
||||
pinMode(kUsbDpPin, INPUT_PULLUP);
|
||||
|
||||
gSensorMode = true;
|
||||
gLastRawClosed = (digitalRead(kUsbDpPin) == LOW);
|
||||
gStableClosed = gLastRawClosed;
|
||||
gLastRawChangeMs = millis();
|
||||
|
||||
lv_label_set_text(gModeLabel, "Sensor mode");
|
||||
lv_label_set_text(gHintLabel, "USB D- / D+ are now the contact input.\nDo not connect this port to a computer.");
|
||||
lv_label_set_text(gActionButtonLabel, "Restart to USB");
|
||||
setStateUi(gStableClosed);
|
||||
}
|
||||
|
||||
static void actionButtonEventCb(lv_event_t *event) {
|
||||
if (lv_event_get_code(event) != LV_EVENT_CLICKED) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!gSensorMode) {
|
||||
enterSensorMode();
|
||||
return;
|
||||
}
|
||||
|
||||
lv_label_set_text(gModeLabel, "Restarting...");
|
||||
lv_label_set_text(gHintLabel, "USB mode will return after reboot.");
|
||||
lv_timer_handler();
|
||||
delay(150);
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
static void createUi() {
|
||||
lv_obj_t *screen = lv_scr_act();
|
||||
lv_obj_set_style_bg_color(screen, lv_color_hex(0x0B1320), 0);
|
||||
lv_obj_set_style_bg_opa(screen, LV_OPA_COVER, 0);
|
||||
|
||||
lv_obj_t *title = lv_label_create(screen);
|
||||
lv_label_set_text(title, "USB CONTACT TEST");
|
||||
lv_obj_set_style_text_color(title, lv_color_hex(0xFFFFFF), 0);
|
||||
lv_obj_set_style_text_font(title, &lv_font_montserrat_22, 0);
|
||||
lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 26);
|
||||
|
||||
gModeLabel = lv_label_create(screen);
|
||||
lv_label_set_text(gModeLabel, "USB mode");
|
||||
lv_obj_set_style_text_color(gModeLabel, lv_color_hex(0xB8C8DC), 0);
|
||||
lv_obj_set_style_text_font(gModeLabel, &lv_font_montserrat_18, 0);
|
||||
lv_obj_align(gModeLabel, LV_ALIGN_TOP_MID, 0, 72);
|
||||
|
||||
gStateLabel = lv_label_create(screen);
|
||||
lv_label_set_text(gStateLabel, "READY");
|
||||
lv_obj_set_style_text_color(gStateLabel, lv_color_hex(0x72D6FF), 0);
|
||||
lv_obj_set_style_text_font(gStateLabel, &lv_font_montserrat_40, 0);
|
||||
lv_obj_align(gStateLabel, LV_ALIGN_CENTER, 0, -56);
|
||||
|
||||
gHintLabel = lv_label_create(screen);
|
||||
lv_label_set_text(gHintLabel, "USB is normal now.\nTap Start to use D- / D+ as a contact sensor.");
|
||||
lv_obj_set_width(gHintLabel, 390);
|
||||
lv_label_set_long_mode(gHintLabel, LV_LABEL_LONG_WRAP);
|
||||
lv_obj_set_style_text_align(gHintLabel, LV_TEXT_ALIGN_CENTER, 0);
|
||||
lv_obj_set_style_text_color(gHintLabel, lv_color_hex(0xD5DCE5), 0);
|
||||
lv_obj_set_style_text_font(gHintLabel, &lv_font_montserrat_16, 0);
|
||||
lv_obj_align(gHintLabel, LV_ALIGN_CENTER, 0, 18);
|
||||
|
||||
gActionButton = lv_btn_create(screen);
|
||||
lv_obj_set_size(gActionButton, 260, 72);
|
||||
lv_obj_align(gActionButton, LV_ALIGN_BOTTOM_MID, 0, -48);
|
||||
lv_obj_set_style_radius(gActionButton, 18, 0);
|
||||
lv_obj_set_style_bg_color(gActionButton, lv_color_hex(0x2A9D8F), 0);
|
||||
lv_obj_set_style_border_width(gActionButton, 0, 0);
|
||||
lv_obj_add_event_cb(gActionButton, actionButtonEventCb, LV_EVENT_CLICKED, nullptr);
|
||||
|
||||
gActionButtonLabel = lv_label_create(gActionButton);
|
||||
lv_label_set_text(gActionButtonLabel, "Start");
|
||||
lv_obj_set_style_text_color(gActionButtonLabel, lv_color_hex(0xFFFFFF), 0);
|
||||
lv_obj_set_style_text_font(gActionButtonLabel, &lv_font_montserrat_22, 0);
|
||||
lv_obj_center(gActionButtonLabel);
|
||||
}
|
||||
|
||||
static void pollContact() {
|
||||
if (!gSensorMode) {
|
||||
return;
|
||||
}
|
||||
|
||||
bool rawClosed = (digitalRead(kUsbDpPin) == LOW);
|
||||
uint32_t now = millis();
|
||||
|
||||
if (rawClosed != gLastRawClosed) {
|
||||
gLastRawClosed = rawClosed;
|
||||
gLastRawChangeMs = now;
|
||||
}
|
||||
|
||||
if ((now - gLastRawChangeMs) >= kDebounceMs && gStableClosed != gLastRawClosed) {
|
||||
gStableClosed = gLastRawClosed;
|
||||
setStateUi(gStableClosed);
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
// With the project's FQBN this is the normal ESP32-S3 native USB serial mode.
|
||||
// No sensor mode is persisted: every reset starts here with normal USB again.
|
||||
Serial.begin(115200);
|
||||
|
||||
Wire.begin(IIC_SDA, IIC_SCL);
|
||||
|
||||
pinMode(TP_RST, OUTPUT);
|
||||
pinMode(TP_INT, INPUT);
|
||||
digitalWrite(TP_RST, LOW);
|
||||
delay(30);
|
||||
digitalWrite(TP_RST, HIGH);
|
||||
delay(50);
|
||||
delay(1000);
|
||||
|
||||
gTouch.setPins(TP_RST, TP_INT);
|
||||
bool touchOk = gTouch.begin(Wire, 0x5A, IIC_SDA, IIC_SCL);
|
||||
if (!touchOk) {
|
||||
Serial.println("Touch init failed");
|
||||
} else {
|
||||
gTouch.sleep();
|
||||
gTouch.reset();
|
||||
gTouch.setMaxCoordinates(480, 480);
|
||||
gTouch.setSwapXY(true);
|
||||
gTouch.setMirrorXY(true, false);
|
||||
}
|
||||
|
||||
gfx->begin();
|
||||
gBus->writeC8D8(0x36, 0xA0);
|
||||
gfx->setBrightness(220);
|
||||
gfx->fillScreen(0x0000);
|
||||
|
||||
lv_init();
|
||||
|
||||
uint32_t screenWidth = gfx->width();
|
||||
uint32_t screenHeight = gfx->height();
|
||||
gBuf1 = (lv_color_t *)heap_caps_malloc(screenWidth * screenHeight / 4 * sizeof(lv_color_t), MALLOC_CAP_DMA);
|
||||
gBuf2 = (lv_color_t *)heap_caps_malloc(screenWidth * screenHeight / 4 * sizeof(lv_color_t), MALLOC_CAP_DMA);
|
||||
lv_disp_draw_buf_init(&gDrawBuf, gBuf1, gBuf2, screenWidth * screenHeight / 4);
|
||||
|
||||
static lv_disp_drv_t dispDrv;
|
||||
lv_disp_drv_init(&dispDrv);
|
||||
dispDrv.hor_res = screenWidth;
|
||||
dispDrv.ver_res = screenHeight;
|
||||
dispDrv.flush_cb = lvglFlushCb;
|
||||
dispDrv.draw_buf = &gDrawBuf;
|
||||
lv_disp_drv_register(&dispDrv);
|
||||
|
||||
static lv_indev_drv_t indevDrv;
|
||||
lv_indev_drv_init(&indevDrv);
|
||||
indevDrv.type = LV_INDEV_TYPE_POINTER;
|
||||
indevDrv.read_cb = lvglTouchRead;
|
||||
lv_indev_drv_register(&indevDrv);
|
||||
|
||||
const esp_timer_create_args_t lvglTickTimerArgs = {
|
||||
.callback = &lvglTick,
|
||||
.name = "lvgl_tick"};
|
||||
esp_timer_handle_t lvglTickTimer = nullptr;
|
||||
esp_timer_create(&lvglTickTimerArgs, &lvglTickTimer);
|
||||
esp_timer_start_periodic(lvglTickTimer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000);
|
||||
|
||||
createUi();
|
||||
Serial.println("USB contact sensor test ready; native USB remains active until Start is tapped.");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
pollContact();
|
||||
lv_timer_handler();
|
||||
delay(5);
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
# USB contact sensor test
|
||||
|
||||
Diagnostic sketches:
|
||||
|
||||
- `main-device/test_sketches/usb_contact_sensor_test/usb_contact_sensor_test.ino`
|
||||
- `main-device/test_sketches/usb_contact_sensor_debug_test/usb_contact_sensor_debug_test.ino`
|
||||
- `main-device/test_sketches/breath_dual_mode_monitor/breath_dual_mode_monitor.ino`
|
||||
- `main-device/test_sketches/usb_contact_resistance_test/usb_contact_resistance_test.ino`
|
||||
- `main-device/test_sketches/usb_contact_screen_start_stub/usb_contact_screen_start_stub.ino`
|
||||
|
||||
## Purpose
|
||||
|
||||
Use the ESP32-S3 native USB data pair as a removable two-wire dry-contact input without making this mode persistent.
|
||||
|
||||
- `GPIO19` = native USB `D-`
|
||||
- `GPIO20` = native USB `D+`
|
||||
|
||||
## Boot behavior
|
||||
|
||||
Every normal reset starts in ordinary native USB mode. The sketch does not save sensor mode in NVS or any other persistent storage.
|
||||
|
||||
Initial screen:
|
||||
|
||||
- title: `USB CONTACT TEST`
|
||||
- mode: `USB mode`
|
||||
- state: `READY`
|
||||
- action button: `Start`
|
||||
|
||||
Until `Start` is pressed, the sketch does not reconfigure GPIO19/20.
|
||||
|
||||
## Sensor mode
|
||||
|
||||
After `Start`:
|
||||
|
||||
- GPIO19 is configured as `OUTPUT LOW`
|
||||
- GPIO20 is configured as `INPUT_PULLUP`
|
||||
- a passive switch/contact is connected between USB `D-` and `D+`
|
||||
- open contact => GPIO20 reads HIGH => UI shows `OPEN`
|
||||
- closed contact => GPIO20 reads LOW => UI shows `CLOSED`
|
||||
- contact changes use a 20 ms debounce
|
||||
|
||||
Native USB data is intentionally unavailable while sensor mode owns GPIO19/20. The UI warns not to connect the port to a computer in this state.
|
||||
|
||||
The action button changes to `Restart to USB`. Pressing it calls `ESP.restart()`; after reboot normal USB mode is restored.
|
||||
|
||||
A physical reset/power cycle has the same effect. The ESP32-S3 ROM download mode remains available through the board's BOOT procedure if recovery is needed.
|
||||
|
||||
## Debug version
|
||||
|
||||
The debug sketch starts in normal USB mode and shows raw touch diagnostics on screen. The on-screen `START` button is intentionally diagnostic only: it confirms touch/LVGL button events and does not reconfigure GPIO19/20.
|
||||
|
||||
Sensor mode starts from the physical BOOT/user button on GPIO0, active LOW. The button is debounced for 35 ms, then the UI shows `START RECEIVED` / `STARTING...`, waits 250 ms so the visual transition is rendered, and only then reconfigures USB D-/D+ as the contact input.
|
||||
|
||||
While in sensor mode, pressing the BOOT/user button again restarts the device and returns normal USB mode on the next boot.
|
||||
|
||||
## Resistance version
|
||||
|
||||
The `usb_contact_resistance_test` sketch starts in normal USB mode and does not reconfigure GPIO19/20 until `Start` is tapped.
|
||||
|
||||
Initial screen:
|
||||
|
||||
- title: `USB RESISTANCE TEST`
|
||||
- mode: `USB mode`
|
||||
- state: `READY`
|
||||
- action button: `Start`
|
||||
|
||||
After `Start`:
|
||||
|
||||
- GPIO19 / USB `D-` is configured as `OUTPUT HIGH`
|
||||
- GPIO20 / USB `D+` is configured as ADC input
|
||||
- the measured resistance is connected between `D-` and `D+`
|
||||
- a known external 10 kΩ pulldown resistor must be connected from `D+` to `GND` for meaningful ohm calculation
|
||||
- UI shows calculated resistance, raw ADC value, and measured `D+` millivolts
|
||||
- very low ADC voltage is shown as `OPEN`
|
||||
- near-source voltage is shown as `SHORT / LOW`
|
||||
|
||||
The calculation assumes a divider where the unknown resistance is the high-side resistor from 3.3 V to ADC and the known 10 kΩ resistor is the low-side resistor from ADC to GND:
|
||||
|
||||
`Rx = 10000 * (3300 / Vadc - 1)`
|
||||
|
||||
This test is intended to check whether the USB contact pair can be reused as an ADC-measured resistance input, for example before wiring a 10 kΩ potentiometer. Native USB data is unavailable while resistance mode owns GPIO19/20. Pressing `Restart to USB` calls `ESP.restart()`; after reboot normal USB mode is restored.
|
||||
|
||||
## GFX breath monitor version
|
||||
|
||||
The `usb_contact_screen_start_stub` sketch is a breath monitor firmware based on the production `Arduino_GFX` display path instead of LVGL.
|
||||
|
||||
Initial screen:
|
||||
|
||||
- title: `USB CONTACT`
|
||||
- title in the current version: `BREATH MONITOR`
|
||||
- state: `READY`
|
||||
- action button: `START`
|
||||
|
||||
After tapping `START`:
|
||||
|
||||
- GPIO19 is configured as `OUTPUT LOW`
|
||||
- GPIO20 is configured as `INPUT_PULLUP`
|
||||
- closed contact is treated as `EXHALE`
|
||||
- open contact is treated as `INHALE`
|
||||
- one breathing cycle is measured from one stable `EXHALE` start to the next stable `EXHALE` start
|
||||
- contact changes use an 80 ms debounce
|
||||
- UI shows current cycle duration, arithmetic average duration over the last 5 completed cycles, and elapsed time from start
|
||||
- the physical user/BOOT button opens and closes the settings screen
|
||||
- settings screen has three touch sliders: limit `5/7/10/15/20` seconds, quiet cycle tick volume `0..10`, and alert volume `0..10`
|
||||
- settings are persisted in NVS and restored after power off/on
|
||||
- quiet cycle tick volume uses a compressed quiet scale: previous low audible levels are moved near `9..10`
|
||||
- alert volume uses a gentler low range; level `5` equals the previous maximum alert level
|
||||
- the working screen uses a static frame and only updates value regions to avoid display flicker
|
||||
- the main screen header/status line and the bottom short-average streak line redraw only when their text changes
|
||||
- the `START` button is a flat rounded rectangle with a border; there is no pause button in this version
|
||||
- every new cycle start plays one quiet confirmation beep
|
||||
- if the arithmetic average of the last up to 5 completed cycles is shorter than the selected limit, the device plays warning beeps: 1 beep for the first short average in a row, 2 beeps for the second, up to 5 beeps
|
||||
- completed cycles shorter than 1.5 seconds are ignored as contact bounce
|
||||
- if the current cycle exceeds 60 seconds, the main screen shows `PAUSE`; that long cycle is skipped and the next `EXHALE` start begins a fresh cycle from zero
|
||||
- reset/power cycle restores ordinary native USB boot
|
||||
|
||||
This version is intended to monitor breathing cadence from a dry-contact USB D-/D+ sensor.
|
||||
|
||||
## Dual-mode GFX breath monitor version
|
||||
|
||||
The `breath_dual_mode_monitor` sketch reuses the production-style `Arduino_GFX` visual approach, audio path, settings sliders, and average-cycle alert behavior from the USB contact breath monitor. It supports two start modes on one firmware: USB contact sensor mode and external button mode.
|
||||
|
||||
Initial screen:
|
||||
|
||||
- title: `BREATH MONITOR`
|
||||
- state: `READY`
|
||||
- action button: `START USB SENSOR`
|
||||
- action button: `START MANUAL MONITOR`
|
||||
- action button: `START POT 3-WIRE`
|
||||
- action button: `START POT 2-WIRE`
|
||||
- action button: `START POT CAL`
|
||||
- action button: `START PIN DIAG`
|
||||
|
||||
After `START USB SENSOR`:
|
||||
|
||||
- GPIO19 / USB `D-` is configured as `OUTPUT LOW`
|
||||
- GPIO20 / USB `D+` is configured as `INPUT_PULLUP`
|
||||
- closed contact is treated as `EXHALE`
|
||||
- open contact is treated as `INHALE`
|
||||
- USB sensor cycles shorter than 3 seconds are ignored without resetting the current cycle timer
|
||||
- a short `KEY3 / GPIO18` press opens or closes settings in USB sensor mode
|
||||
- the bottom line shows the durations of the last 5 accepted USB sensor cycles
|
||||
|
||||
After `START MANUAL MONITOR`:
|
||||
|
||||
- `KEY3 / GPIO18` is the external `INHALE` button, active LOW
|
||||
- `BOOT / GPIO0` is the external `EXHALE` button, active LOW
|
||||
- do not hold `BOOT` while booting or resetting the board
|
||||
- the mode title is `MANUAL MONITOR`
|
||||
- accepted manual phases strictly alternate: `INHALE`, then `EXHALE`; a repeated current button or `EXHALE` before the first `INHALE` is ignored without a beep or timer reset
|
||||
- each accepted button press plays one quiet confirmation beep and starts a fresh `Phase time` timer for that inhale or exhale
|
||||
- the working screen does not show touch `INHALE`/`EXHALE` buttons
|
||||
- long-holding `KEY3 / INHALE` for about 850 ms opens settings; the long press still counts as an inhale press
|
||||
- while settings are open, the next `KEY3 / INHALE` press closes settings and also counts as an inhale press
|
||||
|
||||
After `START POT 3-WIRE`:
|
||||
|
||||
- `GPIO19 / USB D-` is configured as `OUTPUT HIGH` and works as the 3.3 V side of the potentiometer test
|
||||
- `GPIO20 / USB D+` is configured as ADC input and must be connected to the potentiometer wiper
|
||||
- `USB GND` must be connected to the other edge pin of the potentiometer
|
||||
- the screen shows position `0..100%`, a bar, ADC raw value, measured millivolts, and estimated `0..50 kΩ` resistance
|
||||
- if ADC saturates or reaches an unsafe high-voltage threshold, the firmware releases `D-` and `D+` to `INPUT`, beeps, and shows `USB/PIN FAULT`
|
||||
- this is only best-effort software protection; ESP32-S3 GPIO pins are not 5 V tolerant, so external wiring must still prevent 5 V from reaching `D+` or `D-`
|
||||
|
||||
After `START POT 2-WIRE`:
|
||||
|
||||
- `GPIO19 / USB D-` is configured as `OUTPUT HIGH`
|
||||
- `GPIO20 / USB D+` is configured as ADC input with internal pulldown
|
||||
- connect only two potentiometer pins between `D-` and `D+`: the wiper and one edge pin
|
||||
- the screen shows rough position, raw ADC, millivolts, and rough estimated resistance
|
||||
- this mode is intentionally approximate because the ESP32 internal pulldown value is not precise and can vary between boards
|
||||
- use `START POT 3-WIRE` for stable and more linear future breath sensing
|
||||
|
||||
After `START POT CAL`:
|
||||
|
||||
- `GPIO19 / USB D-` is configured as `OUTPUT HIGH`
|
||||
- `GPIO20 / USB D+` is configured as ADC input with internal pulldown
|
||||
- connect only two potentiometer pins between `D-` and `D+`: the wiper and one edge pin
|
||||
- the firmware tracks the observed minimum and maximum ADC millivolts
|
||||
- the screen shows a calibrated `0..100%` position within the observed range, current millivolts, raw ADC, min-max range, and state
|
||||
- `KEY3 / GPIO18` resets the learned min-max calibration
|
||||
- this mode is for relative position testing on weak or shifted USB-pin ranges; it does not report real resistance
|
||||
|
||||
After `START PIN DIAG`:
|
||||
|
||||
- the firmware runs two alternating USB pin drive/read checks
|
||||
- Test A drives `GPIO19 / USB D-` HIGH and reads `GPIO20 / USB D+` as ADC
|
||||
- Test B drives `GPIO20 / USB D+` HIGH and reads `GPIO19 / USB D-` as ADC
|
||||
- shorting `D-` and `D+` should make the ADC side rise if the pins are actually controllable as GPIO
|
||||
- if both tests stay near 0 mV, the USB cable/host, USB PHY, or board wiring is likely holding the lines and the USB data pins are not usable as an analog sensor in that setup
|
||||
|
||||
In both modes:
|
||||
|
||||
- one breathing cycle is measured from one `EXHALE` tap to the next `EXHALE` tap
|
||||
- inhale before any exhale does not create a cycle
|
||||
- button-mode cycles shorter than 1.5 seconds are ignored
|
||||
- USB sensor cycles shorter than 3 seconds are ignored
|
||||
- the UI shows current cycle time, arithmetic average over the last 5 completed cycles, elapsed session time, selected limit, long-pause count, short-average count, and current short-average streak
|
||||
- the lower numeric fields use a larger font than before
|
||||
- if the average of the last up to 5 completed cycles is shorter than the selected limit, warning beeps are played like in the USB contact breath monitor
|
||||
- if the current cycle reaches 60 seconds, 5 loud warning beeps are played, then after 5 seconds another 5 loud warning beeps are played, repeating until the user changes phase / exits the long cycle
|
||||
- in manual monitor mode, a single loud warning beep is also played at 35, 40, 45, 50, and 55 seconds of an unchanged inhale or exhale; the repeated five-beep alarm starts at 60 seconds
|
||||
- settings keep the same sliders: limit `5/7/10/15/20` seconds, quiet cycle tick volume `0..10`, and alert volume `0..10`
|
||||
@@ -0,0 +1,29 @@
|
||||
# SHiNE ANS-104 `test5590` patch
|
||||
|
||||
Этот архив содержит **только новые/изменённые файлы** плюс `DELETE_FILES.txt`.
|
||||
|
||||
## Что меняется
|
||||
|
||||
- Frame v1 без поддержки старого формата.
|
||||
- Один пользовательский block = готовый Ed25519 ANS-104 DataItem.
|
||||
- Test tag: `App=test5590`; channel tag: `c=<canonical slug>`.
|
||||
- SHiNE `prevHash` остаётся `SHA-256(FrameV1Bytes)` и не зависит от Arweave ID.
|
||||
- PostgreSQL — единственное локальное хранилище пользовательских blocks.
|
||||
- Старый custom SHINE-ARCHIVE и filesystem blockchain storage удаляются.
|
||||
- Каждые ~15 минут локальные pending DataItems собираются в standard ANS-104 bundle и идут через существующий native Arweave L1 transport.
|
||||
- Другие серверы находят `App=test5590`, импортируют и не публикуют повторно.
|
||||
- `ESP32/**` **не изменён**.
|
||||
|
||||
## Состав
|
||||
|
||||
- изменено/добавлено файлов: 46
|
||||
- удалить файлов: 41
|
||||
|
||||
Применение и проверки: `docs/Blockchain/CODEX_APPLY_ANS104_TEST5590_PATCH.md`.
|
||||
|
||||
## Проверки при сборке patch
|
||||
|
||||
- `node --check` для изменённых production JS: OK.
|
||||
- `diff -qr` каталога `ESP32` относительно исходного архива: без изменений.
|
||||
- Active Java refs на удалённые SHINE-ARCHIVE/file-recovery classes: отсутствуют.
|
||||
- `./gradlew testClasses --offline --no-daemon`: не запущен до компиляции, потому что wrapper требует скачать Gradle 8.14 с `services.gradle.org`, а рабочая среда не имеет внешнего сетевого доступа. Полный Gradle test обязателен после применения patch в dev-среде.
|
||||
@@ -52,113 +52,84 @@ function readStrU8(bytes, cursorRef) {
|
||||
|
||||
function parseServerFieldsFromUserPda(dataBytes) {
|
||||
const bytes = dataBytes instanceof Uint8Array ? dataBytes : new Uint8Array(dataBytes || []);
|
||||
if (bytes.length < 5) throw new Error('Некорректный формат PDA');
|
||||
if (bytes.length < 9) throw new Error('Некорректный формат PDA');
|
||||
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
||||
const cursorRef = { value: 0 };
|
||||
const readU16 = () => { if (cursorRef.value + 2 > bytes.length) throw new Error('Повреждённый формат PDA'); const v = view.getUint16(cursorRef.value, true); cursorRef.value += 2; return v; };
|
||||
const readU32 = () => { if (cursorRef.value + 4 > bytes.length) throw new Error('Повреждённый формат PDA'); const v = view.getUint32(cursorRef.value, true); cursorRef.value += 4; return v; };
|
||||
const readU64 = () => { if (cursorRef.value + 8 > bytes.length) throw new Error('Повреждённый формат PDA'); const v = view.getBigUint64(cursorRef.value, true); cursorRef.value += 8; return v; };
|
||||
const magic = new TextDecoder().decode(readBytes(bytes, cursorRef, 5));
|
||||
if (magic !== 'SHiNE') throw new Error('Некорректный формат PDA');
|
||||
cursorRef.value += 1; // format_major
|
||||
cursorRef.value += 1; // format_minor
|
||||
cursorRef.value += 2; // record_len
|
||||
const formatMajor = readU8(bytes, cursorRef);
|
||||
const formatMinor = readU8(bytes, cursorRef);
|
||||
if (formatMajor !== 1 || formatMinor !== 2) throw new Error(`Неподдерживаемый формат PDA ${formatMajor}.${formatMinor}`);
|
||||
const recordLen = readU16();
|
||||
if (recordLen < 73 || recordLen > bytes.length) throw new Error('Некорректный record_len PDA');
|
||||
cursorRef.value += 8; // created_at_ms
|
||||
cursorRef.value += 8; // updated_at_ms
|
||||
cursorRef.value += 4; // record_number
|
||||
cursorRef.value += 32; // prev_record_hash
|
||||
readStrU8(bytes, cursorRef); // login
|
||||
const login = readStrU8(bytes, cursorRef);
|
||||
const blocksCount = readU8(bytes, cursorRef);
|
||||
|
||||
let isServer = false;
|
||||
let serverAddress = '';
|
||||
let accessServers = [];
|
||||
let recoveryKey32 = null;
|
||||
let rootKey32 = null;
|
||||
let clientKey32 = null;
|
||||
let blockchainKey32 = null;
|
||||
let blockchainName = '';
|
||||
let homeserverSessions = [];
|
||||
let forkCount = 0;
|
||||
|
||||
for (let i = 0; i < blocksCount; i += 1) {
|
||||
const blockType = readU8(bytes, cursorRef);
|
||||
cursorRef.value += 1; // block_version
|
||||
|
||||
if (blockType === 0 || blockType === 1 || blockType === 2) {
|
||||
const blockVersion = readU8(bytes, cursorRef);
|
||||
if (blockVersion !== 0) throw new Error('Неподдерживаемая версия блока PDA');
|
||||
if (blockType === 1 || blockType === 2) {
|
||||
const key32 = readBytes(bytes, cursorRef, 32);
|
||||
if (blockType === 0) recoveryKey32 = key32;
|
||||
if (blockType === 1) rootKey32 = key32;
|
||||
if (blockType === 2) clientKey32 = key32;
|
||||
else clientKey32 = key32;
|
||||
continue;
|
||||
}
|
||||
|
||||
const payloadLen = readU16();
|
||||
const payloadEnd = cursorRef.value + payloadLen;
|
||||
if (payloadEnd > recordLen - 64) throw new Error('Повреждённый variable block PDA');
|
||||
|
||||
if (blockType === 3) {
|
||||
const count = readU8(bytes, cursorRef);
|
||||
for (let j = 0; j < count; j += 1) {
|
||||
cursorRef.value += 1;
|
||||
const currentBlockchainName = readStrU8(bytes, cursorRef);
|
||||
const currentBlockchainKey32 = readBytes(bytes, cursorRef, 32);
|
||||
if (!blockchainKey32) {
|
||||
blockchainKey32 = currentBlockchainKey32;
|
||||
blockchainName = currentBlockchainName;
|
||||
}
|
||||
cursorRef.value += 8 + 8 + 4 + 32 + 64;
|
||||
const arPresent = readU8(bytes, cursorRef);
|
||||
if (arPresent === 1) readStrU8(bytes, cursorRef);
|
||||
forkCount = readU16();
|
||||
if (forkCount < 1) throw new Error('BlockchainRegistry пуст');
|
||||
for (let j = 0; j < forkCount; j += 1) {
|
||||
blockchainKey32 = readBytes(bytes, cursorRef, 32);
|
||||
readU64(); // created_at_ms fork
|
||||
readU32(); // paid_limit_bytes
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (blockType === 30) {
|
||||
isServer = readU8(bytes, cursorRef) === 1;
|
||||
if (isServer) {
|
||||
cursorRef.value += 1; // address_format_type
|
||||
cursorRef.value += 1; // address_format_version
|
||||
serverAddress = readStrU8(bytes, cursorRef);
|
||||
const syncCount = readU8(bytes, cursorRef);
|
||||
for (let j = 0; j < syncCount; j += 1) readStrU8(bytes, cursorRef);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (blockType === 40) {
|
||||
} else if (blockType === 30) {
|
||||
const addressCount = readU8(bytes, cursorRef);
|
||||
if (addressCount !== 1) throw new Error('PDA 1.2 допускает ровно один адрес сервера');
|
||||
readU8(bytes, cursorRef); // address_format_type
|
||||
readU8(bytes, cursorRef); // address_format_version
|
||||
serverAddress = readStrU8(bytes, cursorRef);
|
||||
} else if (blockType === 40) {
|
||||
const accessCount = readU8(bytes, cursorRef);
|
||||
if (accessCount > 1) throw new Error('PDA 1.2 допускает максимум один access server');
|
||||
accessServers = [];
|
||||
for (let j = 0; j < accessCount; j += 1) accessServers.push(readStrU8(bytes, cursorRef));
|
||||
continue;
|
||||
}
|
||||
if (blockType === 50) {
|
||||
cursorRef.value += 1;
|
||||
const sessionsCount = readU8(bytes, cursorRef);
|
||||
for (let j = 0; j < sessionsCount; j += 1) {
|
||||
const sessionType = readU8(bytes, cursorRef);
|
||||
const sessionVersion = readU8(bytes, cursorRef);
|
||||
const sessionName = readStrU8(bytes, cursorRef);
|
||||
const sessionPubKey32 = readBytes(bytes, cursorRef, 32);
|
||||
if (sessionType === 100) {
|
||||
homeserverSessions.push({
|
||||
sessionType,
|
||||
sessionVersion,
|
||||
sessionName,
|
||||
sessionPubKeyBase58: new PublicKey(sessionPubKey32).toBase58(),
|
||||
sessionPubKeyB64: `ed25519/${btoa(String.fromCharCode(...sessionPubKey32))}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (blockType === 70) {
|
||||
cursorRef.value += 1;
|
||||
continue;
|
||||
}
|
||||
throw new Error(`Неизвестный блок PDA: ${blockType}`);
|
||||
cursorRef.value = payloadEnd;
|
||||
}
|
||||
|
||||
if (!rootKey32 || !clientKey32 || !blockchainKey32) throw new Error('PDA 1.2 не содержит обязательные ключи');
|
||||
return {
|
||||
isServer,
|
||||
isServer: Boolean(serverAddress),
|
||||
serverAddress: normalizeHostLike(serverAddress),
|
||||
accessServers: accessServers.map((value) => normalizeServerLogin(value)).filter(Boolean),
|
||||
publicKeys: {
|
||||
recoveryKeyBase58: recoveryKey32 ? new PublicKey(recoveryKey32).toBase58() : '',
|
||||
rootKeyBase58: rootKey32 ? new PublicKey(rootKey32).toBase58() : '',
|
||||
clientKeyBase58: clientKey32 ? new PublicKey(clientKey32).toBase58() : '',
|
||||
blockchainKeyBase58: blockchainKey32 ? new PublicKey(blockchainKey32).toBase58() : '',
|
||||
blockchainName,
|
||||
recoveryKeyBase58: '',
|
||||
rootKeyBase58: new PublicKey(rootKey32).toBase58(),
|
||||
clientKeyBase58: new PublicKey(clientKey32).toBase58(),
|
||||
blockchainKeyBase58: new PublicKey(blockchainKey32).toBase58(),
|
||||
blockchainName: `${normalizeServerLogin(login)}-${String(forkCount).padStart(3, '0')}`,
|
||||
},
|
||||
homeserverSessions,
|
||||
homeserverSessions: [],
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
+7
-15
@@ -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.
|
||||
|
||||
## Деплой
|
||||
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
package server.archive;
|
||||
|
||||
import blockchain.Ans104DataItem;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/** Minimal ANS-104 binary bundle writer/reader. */
|
||||
public final class Ans104Bundle {
|
||||
public record Entry(byte[] id32, byte[] rawDataItem) {}
|
||||
|
||||
private Ans104Bundle() {}
|
||||
|
||||
public static byte[] write(List<byte[]> rawItems) {
|
||||
if (rawItems == null || rawItems.isEmpty()) throw new IllegalArgumentException("ANS-104 bundle is empty");
|
||||
if (rawItems.size() > 1_000_000) throw new IllegalArgumentException("Too many ANS-104 items");
|
||||
|
||||
List<Ans104DataItem> parsed = new ArrayList<>(rawItems.size());
|
||||
long total = 32L + 64L * rawItems.size();
|
||||
for (byte[] raw : rawItems) {
|
||||
Ans104DataItem item = new Ans104DataItem(raw);
|
||||
if (!item.verifySignature()) throw new IllegalArgumentException("Invalid ANS-104 DataItem signature");
|
||||
parsed.add(item);
|
||||
total = Math.addExact(total, raw.length);
|
||||
}
|
||||
if (total > Integer.MAX_VALUE) throw new IllegalArgumentException("Bundle too large for in-memory publisher: " + total);
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream((int) total);
|
||||
out.writeBytes(uint256LE(rawItems.size()));
|
||||
for (int i = 0; i < rawItems.size(); i++) {
|
||||
out.writeBytes(uint256LE(rawItems.get(i).length));
|
||||
out.writeBytes(parsed.get(i).id32());
|
||||
}
|
||||
for (byte[] raw : rawItems) out.writeBytes(raw);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
public static List<Entry> read(byte[] bundle, int maxItems) {
|
||||
if (bundle == null || bundle.length < 32) throw new IllegalArgumentException("ANS-104 bundle too short");
|
||||
long countLong = readUint256AsLong(bundle, 0);
|
||||
if (countLong <= 0 || countLong > Math.max(1, maxItems)) {
|
||||
throw new IllegalArgumentException("Bad ANS-104 bundle item count: " + countLong);
|
||||
}
|
||||
int count = (int) countLong;
|
||||
long headerLong = 32L + 64L * count;
|
||||
if (headerLong > bundle.length) throw new IllegalArgumentException("ANS-104 bundle header truncated");
|
||||
int header = (int) headerLong;
|
||||
long[] sizes = new long[count];
|
||||
byte[][] ids = new byte[count][];
|
||||
int hp = 32;
|
||||
long payloadBytes = 0;
|
||||
for (int i = 0; i < count; i++) {
|
||||
sizes[i] = readUint256AsLong(bundle, hp); hp += 32;
|
||||
ids[i] = Arrays.copyOfRange(bundle, hp, hp + 32); hp += 32;
|
||||
if (sizes[i] <= 0 || sizes[i] > Integer.MAX_VALUE) throw new IllegalArgumentException("Bad DataItem size: " + sizes[i]);
|
||||
payloadBytes = Math.addExact(payloadBytes, sizes[i]);
|
||||
}
|
||||
if (headerLong + payloadBytes != bundle.length) {
|
||||
throw new IllegalArgumentException("ANS-104 bundle size mismatch: header=" + headerLong + " payload=" + payloadBytes + " actual=" + bundle.length);
|
||||
}
|
||||
List<Entry> out = new ArrayList<>(count);
|
||||
int p = header;
|
||||
for (int i = 0; i < count; i++) {
|
||||
int len = (int) sizes[i];
|
||||
byte[] raw = Arrays.copyOfRange(bundle, p, p + len);
|
||||
p += len;
|
||||
Ans104DataItem item = new Ans104DataItem(raw);
|
||||
if (!Arrays.equals(ids[i], item.id32())) throw new IllegalArgumentException("Bundle entry ID mismatch at index " + i);
|
||||
out.add(new Entry(ids[i], raw));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
public static byte[] find(byte[] bundle, byte[] wantedId32, int maxItems) {
|
||||
if (wantedId32 == null || wantedId32.length != 32) throw new IllegalArgumentException("wanted ID must be 32 bytes");
|
||||
for (Entry e : read(bundle, maxItems)) {
|
||||
if (Arrays.equals(e.id32(), wantedId32)) return e.rawDataItem();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** ANS-104 bundle integers are 32-byte unsigned little-endian values. */
|
||||
static byte[] uint256LE(long value) {
|
||||
if (value < 0) throw new IllegalArgumentException("negative uint256");
|
||||
byte[] out = new byte[32];
|
||||
long v = value;
|
||||
for (int i = 0; i < 8; i++) { out[i] = (byte) (v & 0xff); v >>>= 8; }
|
||||
return out;
|
||||
}
|
||||
|
||||
static long readUint256AsLong(byte[] data, int offset) {
|
||||
if (offset < 0 || offset + 32 > data.length) throw new IllegalArgumentException("uint256 truncated");
|
||||
for (int i = offset + 8; i < offset + 32; i++) {
|
||||
if (data[i] != 0) throw new IllegalArgumentException("uint256 exceeds signed long range");
|
||||
}
|
||||
long v = 0;
|
||||
for (int i = 7; i >= 0; i--) v = (v << 8) | (data[offset + i] & 0xffL);
|
||||
if (v < 0) throw new IllegalArgumentException("uint256 exceeds signed long range");
|
||||
return v;
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
package server.archive;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Base64;
|
||||
|
||||
/** Единые локальные имена больших архивных файлов. */
|
||||
public final class ArchiveFileNames {
|
||||
private static final DateTimeFormatter DATE = DateTimeFormatter.ofPattern("dd.MM.yy");
|
||||
|
||||
private ArchiveFileNames() {}
|
||||
|
||||
/**
|
||||
* Временное имя полностью собранного файла до появления Arweave TX ID.
|
||||
* Пример: archive01.00001.11.09.26.tmp.SHiNE-archive
|
||||
*/
|
||||
public static String pendingFileName(String login, long bigBlockNumber, long createdAtMs, ZoneId zone) {
|
||||
return baseName(login, bigBlockNumber, createdAtMs, zone) + ".tmp.SHiNE-archive";
|
||||
}
|
||||
|
||||
/**
|
||||
* Постоянное имя после успешной загрузки в Arweave.
|
||||
* Пример: archive01.00001.11.09.26.Xm32...kP9.SHiNE-archive
|
||||
*/
|
||||
public static String uploadedFileName(String login, long bigBlockNumber, long createdAtMs, ZoneId zone, byte[] txId) {
|
||||
if (txId == null || txId.length != 32) throw new IllegalArgumentException("Arweave TX ID должен быть 32 bytes");
|
||||
String tx = Base64.getUrlEncoder().withoutPadding().encodeToString(txId);
|
||||
return baseName(login, bigBlockNumber, createdAtMs, zone) + "." + tx + ".SHiNE-archive";
|
||||
}
|
||||
|
||||
public static Path pendingFilePath(Path archiveDir, String login, long bigBlockNumber, long createdAtMs, ZoneId zone) {
|
||||
return archiveDir.resolve(pendingFileName(login, bigBlockNumber, createdAtMs, zone));
|
||||
}
|
||||
|
||||
public static Path uploadedFilePath(Path archiveDir, String login, long bigBlockNumber, long createdAtMs, ZoneId zone, byte[] txId) {
|
||||
return archiveDir.resolve(uploadedFileName(login, bigBlockNumber, createdAtMs, zone, txId));
|
||||
}
|
||||
|
||||
private static String baseName(String login, long bigBlockNumber, long createdAtMs, ZoneId zone) {
|
||||
if (bigBlockNumber < 0 || bigBlockNumber > 0xffff_ffffL) {
|
||||
throw new IllegalArgumentException("Номер большого блока вне u32: " + bigBlockNumber);
|
||||
}
|
||||
String safeLogin = sanitize(login);
|
||||
String number = String.format("%05d", bigBlockNumber);
|
||||
String date = DATE.format(Instant.ofEpochMilli(createdAtMs).atZone(zone == null ? ZoneId.systemDefault() : zone));
|
||||
return safeLogin + "." + number + "." + date;
|
||||
}
|
||||
|
||||
static String sanitize(String login) {
|
||||
String s = login == null ? "server" : login.trim();
|
||||
if (s.isEmpty()) s = "server";
|
||||
return s.replaceAll("[^A-Za-z0-9._-]", "_");
|
||||
}
|
||||
}
|
||||
-37
@@ -1,37 +0,0 @@
|
||||
package server.archive;
|
||||
|
||||
import utils.config.AppConfig;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
|
||||
/** Настройки чтения архивов других доверенных SHiNE-серверов. Пустой whitelist = импорт выключен. */
|
||||
public record ArchiveImportConfig(
|
||||
Set<String> allowedPublishers,
|
||||
int intervalMinutes,
|
||||
String arweaveGateway,
|
||||
Path workDir
|
||||
) {
|
||||
public static ArchiveImportConfig load() {
|
||||
AppConfig c = AppConfig.getInstance();
|
||||
Set<String> approved = new LinkedHashSet<>();
|
||||
String raw = c.getParam("archive.import.allowedPublishers");
|
||||
if (raw != null) {
|
||||
for (String item : raw.split(",")) {
|
||||
String login = item.trim().toLowerCase(Locale.ROOT);
|
||||
if (!login.isEmpty()) approved.add(login);
|
||||
}
|
||||
}
|
||||
String gateway = c.getParam("archive.arweave.gateway");
|
||||
if (gateway == null || gateway.isBlank()) gateway = "https://arweave.net";
|
||||
String dir = c.getParam("archive.import.workDir");
|
||||
if (dir == null || dir.isBlank()) dir = "data/archive-import";
|
||||
return new ArchiveImportConfig(
|
||||
Set.copyOf(approved),
|
||||
Math.max(1, c.getInt("archive.import.intervalMinutes", 60)),
|
||||
gateway.trim(),
|
||||
Path.of(dir));
|
||||
}
|
||||
|
||||
public boolean enabled() { return !allowedPublishers.isEmpty(); }
|
||||
}
|
||||
-54
@@ -1,54 +0,0 @@
|
||||
package server.archive;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* Фоновая проверка уже синхронизированной локальной таблицы User PDA.
|
||||
* Solana отдельно здесь не опрашивается: первый запуск через 10 секунд после старта,
|
||||
* затем по умолчанию раз в час.
|
||||
*/
|
||||
public final class ArchiveImportScheduler {
|
||||
private static final Logger log = LoggerFactory.getLogger(ArchiveImportScheduler.class);
|
||||
private static ScheduledExecutorService executor;
|
||||
|
||||
private ArchiveImportScheduler() {}
|
||||
|
||||
public static synchronized void startOrLog() {
|
||||
if (executor != null) return;
|
||||
ArchiveImportConfig cfg = ArchiveImportConfig.load();
|
||||
if (!cfg.enabled()) {
|
||||
log.info("Archive importer выключен: archive.import.allowedPublishers пуст");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
ArchiveImportService service = new ArchiveImportService(cfg);
|
||||
executor = Executors.newSingleThreadScheduledExecutor(r -> {
|
||||
Thread t = new Thread(r, "archive-importer");
|
||||
t.setDaemon(true);
|
||||
return t;
|
||||
});
|
||||
executor.scheduleWithFixedDelay(
|
||||
() -> {
|
||||
try { service.pollOnce(); }
|
||||
catch (Exception e) { log.error("Archive importer cycle failed", e); }
|
||||
},
|
||||
10,
|
||||
Math.multiplyExact((long) cfg.intervalMinutes(), 60L),
|
||||
TimeUnit.SECONDS);
|
||||
log.info("Archive importer включён. approvedPublishers={} interval={}min",
|
||||
cfg.allowedPublishers(), cfg.intervalMinutes());
|
||||
} catch (Exception e) {
|
||||
log.error("Archive importer не запущен", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized void close() {
|
||||
if (executor != null) {
|
||||
executor.shutdownNow();
|
||||
executor = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
-229
@@ -1,229 +0,0 @@
|
||||
package server.archive;
|
||||
|
||||
import blockchain.BchBlockEntry;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import server.logic.ws_protocol.JSON.handlers.blockchain.Net_AddBlock_Handler;
|
||||
import server.logic.ws_protocol.JSON.handlers.blockchain.Net_AddBlock_Handler_utils.BlockchainLocks;
|
||||
import shine.db.archive.*;
|
||||
import shine.db.dao.*;
|
||||
import shine.db.entities.BlockchainStateEntry;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.*;
|
||||
import java.nio.file.*;
|
||||
import java.time.Duration;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/** Импорт archive head только от серверов из server-side whitelist. */
|
||||
public final class ArchiveImportService {
|
||||
private static final Logger log = LoggerFactory.getLogger(ArchiveImportService.class);
|
||||
private final ArchiveImportConfig cfg;
|
||||
private final ArchiveImportDAO dao = ArchiveImportDAO.getInstance();
|
||||
private final BlockchainStateDAO stateDao = BlockchainStateDAO.getInstance();
|
||||
private final BlocksDAO blocksDao = BlocksDAO.getInstance();
|
||||
private final ShineArchiveReader reader = new ShineArchiveReader();
|
||||
private final Net_AddBlock_Handler addBlockHandler = new Net_AddBlock_Handler();
|
||||
private final HttpClient http = HttpClient.newBuilder()
|
||||
.connectTimeout(Duration.ofSeconds(20))
|
||||
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||
.build();
|
||||
|
||||
public ArchiveImportService(ArchiveImportConfig cfg) throws IOException {
|
||||
this.cfg = Objects.requireNonNull(cfg);
|
||||
Files.createDirectories(cfg.workDir());
|
||||
}
|
||||
|
||||
/**
|
||||
* Никаких отдельных Solana RPC запросов здесь нет.
|
||||
* Читаем только solana_user_pda_current, которую уже обновляет обычный Solana users sync.
|
||||
*/
|
||||
public void pollOnce() throws Exception {
|
||||
if (!cfg.enabled()) return;
|
||||
for (ArchivePublisherHead head : dao.listPendingPublisherHeads(cfg.allowedPublishers())) {
|
||||
try {
|
||||
importHead(head);
|
||||
} catch (Exception e) {
|
||||
log.error("Archive import publisher={} tx={} failed", head.login(), head.archiveHeadTxId(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void importHead(ArchivePublisherHead publisher) throws Exception {
|
||||
String headTxId = publisher.archiveHeadTxId();
|
||||
byte[] headHash = hex(publisher.archiveHeadHash());
|
||||
|
||||
// Если процесс успел импортировать head, но упал до установки archive_imported=true,
|
||||
// следующий цикл просто завершает отметку без повторного скачивания всей истории.
|
||||
if (headTxId.equals(publisher.lastImportedArchiveTxId())) {
|
||||
if (!dao.markHeadImported(publisher.login(), headTxId)) {
|
||||
log.info("Archive head changed while finalizing import marker publisher={} oldHead={}",
|
||||
publisher.login(), headTxId);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Path headFile = download(headTxId);
|
||||
ShineArchiveReader.Document head;
|
||||
try {
|
||||
head = reader.readAndVerify(headFile, publisher.login(), headHash, publisher.rootKey());
|
||||
} finally {
|
||||
Files.deleteIfExists(headFile);
|
||||
}
|
||||
|
||||
// FULL reference table позволяет догнать все archive blocks, пропущенные пока сервер был выключен.
|
||||
List<ShineArchiveReader.Reference> previous = new ArrayList<>(head.references());
|
||||
previous.sort(Comparator.comparingLong(ShineArchiveReader.Reference::bigBlockNumber));
|
||||
|
||||
int startIndex = findResumeIndex(previous, publisher.lastImportedArchiveTxId(), headTxId);
|
||||
for (int i = startIndex; i < previous.size(); i++) {
|
||||
ShineArchiveReader.Reference ref = previous.get(i);
|
||||
importOne(publisher, ref.arweaveTxId(), ref.archiveHash());
|
||||
if (!dao.advanceImportCursor(publisher.login(), headTxId, ref.arweaveTxId())) {
|
||||
throw new IllegalStateException("Archive head changed during import for publisher " + publisher.login());
|
||||
}
|
||||
}
|
||||
|
||||
// Head уже скачан и проверен выше — второй раз его из Arweave не качаем.
|
||||
importDocument(publisher, headTxId, head);
|
||||
if (!dao.advanceImportCursor(publisher.login(), headTxId, headTxId)) {
|
||||
throw new IllegalStateException("Archive head changed during final import for publisher " + publisher.login());
|
||||
}
|
||||
if (!dao.markHeadImported(publisher.login(), headTxId)) {
|
||||
throw new IllegalStateException("Archive head changed before imported marker for publisher " + publisher.login());
|
||||
}
|
||||
|
||||
log.info("Archive head fully imported publisher={} bigBlock={} tx={}",
|
||||
publisher.login(), head.bigBlockNumber(), headTxId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает индекс первого ещё не импортированного previous big block.
|
||||
* Пустой cursor = новый сервер, импортируем всю FULL историю.
|
||||
*/
|
||||
private static int findResumeIndex(List<ShineArchiveReader.Reference> previous,
|
||||
String lastImportedTxId,
|
||||
String currentHeadTxId) {
|
||||
String last = lastImportedTxId == null ? "" : lastImportedTxId.trim();
|
||||
if (last.isEmpty()) return 0;
|
||||
if (last.equals(currentHeadTxId)) return previous.size();
|
||||
for (int i = 0; i < previous.size(); i++) {
|
||||
if (last.equals(previous.get(i).arweaveTxId())) return i + 1;
|
||||
}
|
||||
throw new IllegalStateException(
|
||||
"Последний импортированный archive TX не найден в FULL history текущего head: " + last);
|
||||
}
|
||||
|
||||
private void importOne(ArchivePublisherHead publisher, String txId, byte[] expectedHash) throws Exception {
|
||||
Path file = download(txId);
|
||||
try {
|
||||
ShineArchiveReader.Document doc = reader.readAndVerify(
|
||||
file, publisher.login(), expectedHash, publisher.rootKey());
|
||||
importDocument(publisher, txId, doc);
|
||||
} finally {
|
||||
Files.deleteIfExists(file);
|
||||
}
|
||||
}
|
||||
|
||||
private void importDocument(ArchivePublisherHead publisher, String txId,
|
||||
ShineArchiveReader.Document doc) throws Exception {
|
||||
for (ShineArchiveReader.Chunk chunk : doc.chunks()) {
|
||||
processChunk(publisher.login(), txId, doc, chunk);
|
||||
}
|
||||
log.info("Archive imported publisher={} bigBlock={} tx={} chunks={}",
|
||||
publisher.login(), doc.bigBlockNumber(), txId, doc.chunks().size());
|
||||
}
|
||||
|
||||
private void processChunk(String publisherLogin, String txId,
|
||||
ShineArchiveReader.Document doc,
|
||||
ShineArchiveReader.Chunk chunk) throws Exception {
|
||||
ReentrantLock lock = BlockchainLocks.lockFor(chunk.blockchainName());
|
||||
lock.lock();
|
||||
try {
|
||||
ensureState(chunk.blockchainName());
|
||||
BlockchainStateEntry state = stateDao.getByBlockchainName(chunk.blockchainName());
|
||||
int localLast = state == null ? -1 : state.getLastBlockNumber();
|
||||
long archiveLast = -1;
|
||||
|
||||
for (byte[] raw : chunk.rawRecords()) {
|
||||
BchBlockEntry block = new BchBlockEntry(raw);
|
||||
archiveLast = Integer.toUnsignedLong(block.blockNumber);
|
||||
|
||||
if (block.blockNumber <= localLast) {
|
||||
byte[] localHash = blocksDao.getHashByNumber(chunk.blockchainName(), block.blockNumber);
|
||||
if (!Arrays.equals(localHash, block.getHash32())) {
|
||||
throw new IllegalStateException(
|
||||
"Archive conflict " + chunk.blockchainName() + "#" + block.blockNumber);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (block.blockNumber != localLast + 1) {
|
||||
throw new IllegalStateException(
|
||||
"Archive gap " + chunk.blockchainName() + ": local=" + localLast +
|
||||
" next=" + block.blockNumber);
|
||||
}
|
||||
|
||||
var result = addBlockHandler.addBlockFromArchive(chunk.blockchainName(), raw);
|
||||
if (!result.ok()) {
|
||||
throw new IllegalStateException(
|
||||
"Archive AddBlock rejected " + chunk.blockchainName() + "#" +
|
||||
block.blockNumber + ": " + result.reasonCode());
|
||||
}
|
||||
localLast = block.blockNumber;
|
||||
}
|
||||
|
||||
if (archiveLast >= 0) {
|
||||
dao.upsertLocation(new ArchiveBlockchainLocation(
|
||||
chunk.blockchainName(), publisherLogin, txId, doc.archiveHash(), doc.bigBlockNumber(),
|
||||
chunk.offset(), chunk.size(), archiveLast, System.currentTimeMillis()));
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
private void ensureState(String blockchainName) throws Exception {
|
||||
if (stateDao.getByBlockchainName(blockchainName) != null) return;
|
||||
ArchiveImportDAO.BlockchainIdentity id = dao.getBlockchainIdentity(blockchainName);
|
||||
if (id == null) throw new IllegalStateException("Нет User PDA для blockchain " + blockchainName);
|
||||
|
||||
BlockchainStateEntry s = new BlockchainStateEntry();
|
||||
s.setBlockchainName(id.blockchainName());
|
||||
s.setLogin(id.login());
|
||||
s.setBlockchainKey(id.blockchainKey());
|
||||
s.setSizeLimit(id.sizeLimit());
|
||||
s.setFileSizeBytes(0);
|
||||
s.setLastBlockNumber(-1);
|
||||
s.setLastBlockHash(null);
|
||||
s.setUpdatedAtMs(System.currentTimeMillis());
|
||||
stateDao.insertIfMissing(s);
|
||||
}
|
||||
|
||||
private Path download(String txId) throws Exception {
|
||||
String gateway = cfg.arweaveGateway().replaceAll("/+$", "");
|
||||
Path target = Files.createTempFile(cfg.workDir(), "archive-", ".download");
|
||||
HttpRequest req = HttpRequest.newBuilder(URI.create(gateway + "/" + txId))
|
||||
.timeout(Duration.ofMinutes(10))
|
||||
.GET()
|
||||
.build();
|
||||
HttpResponse<Path> resp = http.send(req, HttpResponse.BodyHandlers.ofFile(target));
|
||||
if (resp.statusCode() < 200 || resp.statusCode() >= 300) {
|
||||
Files.deleteIfExists(target);
|
||||
throw new IOException("Arweave HTTP " + resp.statusCode() + " tx=" + txId);
|
||||
}
|
||||
return target;
|
||||
}
|
||||
|
||||
private static byte[] hex(String value) {
|
||||
String s = String.valueOf(value == null ? "" : value).trim();
|
||||
if (s.length() != 64) throw new IllegalArgumentException("archive_head_hash должен быть HEX64");
|
||||
byte[] out = new byte[32];
|
||||
for (int i = 0; i < 32; i++) {
|
||||
out[i] = (byte) Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
package server.archive;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import sync.util.Base58Util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Base64;
|
||||
|
||||
/** Читает Ed25519 seed32 из распространённых SHiNE/Solana форматов ключей. */
|
||||
final class ArchiveKeyLoader {
|
||||
private static final ObjectMapper JSON = new ObjectMapper();
|
||||
|
||||
private ArchiveKeyLoader() {}
|
||||
|
||||
static byte[] loadSeed32(Path path) throws IOException {
|
||||
if (path == null) throw new IllegalArgumentException("Путь ключа не задан");
|
||||
byte[] raw = Files.readAllBytes(path);
|
||||
if (raw.length == 32) return raw.clone();
|
||||
if (raw.length == 64) return first32(raw);
|
||||
|
||||
String text = new String(raw, StandardCharsets.UTF_8).trim();
|
||||
if (text.isEmpty()) throw new IOException("Пустой key file: " + path);
|
||||
|
||||
// Solana keypair JSON: [64 чисел].
|
||||
if (text.startsWith("[")) {
|
||||
JsonNode arr = JSON.readTree(text);
|
||||
if (!arr.isArray() || (arr.size() != 32 && arr.size() != 64)) {
|
||||
throw new IOException("Ожидался JSON keypair на 32/64 байта: " + path);
|
||||
}
|
||||
byte[] bytes = new byte[arr.size()];
|
||||
for (int i = 0; i < arr.size(); i++) {
|
||||
int v = arr.get(i).asInt(-1);
|
||||
if (v < 0 || v > 255) throw new IOException("Некорректный байт keypair: " + path);
|
||||
bytes[i] = (byte) v;
|
||||
}
|
||||
return bytes.length == 32 ? bytes : first32(bytes);
|
||||
}
|
||||
|
||||
// Base58 seed32 или Solana secret key 64 bytes.
|
||||
try {
|
||||
byte[] decoded = Base58Util.decode(text);
|
||||
if (decoded.length == 32) return decoded;
|
||||
if (decoded.length == 64) return first32(decoded);
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
// Пробуем остальные текстовые форматы ниже.
|
||||
}
|
||||
|
||||
// Base64 raw seed / keypair / Ed25519 PKCS8. В SHiNE PKCS8 seed находится в последних 32 байтах.
|
||||
try {
|
||||
byte[] decoded = Base64.getDecoder().decode(text.replaceAll("\\s+", ""));
|
||||
if (decoded.length == 32) return decoded;
|
||||
if (decoded.length == 64) return first32(decoded);
|
||||
if (decoded.length > 32) {
|
||||
byte[] seed = new byte[32];
|
||||
System.arraycopy(decoded, decoded.length - 32, seed, 0, 32);
|
||||
return seed;
|
||||
}
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
// Ниже выдадим понятную общую ошибку.
|
||||
}
|
||||
|
||||
throw new IOException("Не удалось распознать Ed25519 key file: " + path
|
||||
+ ". Поддерживаются raw seed32, raw/keypair64, Solana JSON, Base58 seed/keypair и Base64 PKCS8.");
|
||||
}
|
||||
|
||||
private static byte[] first32(byte[] bytes) {
|
||||
byte[] out = new byte[32];
|
||||
System.arraycopy(bytes, 0, out, 0, 32);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
-98
@@ -1,98 +0,0 @@
|
||||
package server.archive;
|
||||
|
||||
import utils.config.AppConfig;
|
||||
import utils.config.SolanaProgramsConfig;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.time.LocalTime;
|
||||
import java.time.ZoneId;
|
||||
|
||||
/** Настройки опционального архивного publisher-а. По умолчанию функция выключена. */
|
||||
public record ArchivePublisherConfig(
|
||||
boolean enabled,
|
||||
LocalTime publishTime,
|
||||
ZoneId publishZone,
|
||||
long maxFileBytes,
|
||||
Path workDir,
|
||||
String serverLogin,
|
||||
String arweaveGateway,
|
||||
Path arweaveWalletJwkPath,
|
||||
int arweaveMinConfirmations,
|
||||
int arweaveConfirmPollSeconds,
|
||||
int arweaveConfirmTimeoutMinutes,
|
||||
Path solanaRootKeyPath,
|
||||
Path solanaClientKeyPath,
|
||||
String solanaRpcUrl,
|
||||
String solanaUsersProgramId,
|
||||
String solanaPaymentsProgramId,
|
||||
int solanaConfirmPollSeconds,
|
||||
int solanaConfirmTimeoutMinutes,
|
||||
String solanaCommitment
|
||||
) {
|
||||
public static ArchivePublisherConfig load() {
|
||||
AppConfig c = AppConfig.getInstance();
|
||||
return new ArchivePublisherConfig(
|
||||
c.getBoolean("archive.publish.enabled", false),
|
||||
parseTime(orDefault(c.getParam("archive.publish.time"), "00:00")),
|
||||
parseZone(c.getParam("archive.publish.zoneId")),
|
||||
positiveLong(parseLong(c.getParam("archive.maxFileBytes"), 4_000_000_000L), "archive.maxFileBytes"),
|
||||
Path.of(orDefault(c.getParam("archive.workDir"), "data/archive")),
|
||||
requiredIfEnabled(c, "server.SHiNE.login"),
|
||||
orDefault(c.getParam("archive.arweave.gateway"), "https://arweave.net"),
|
||||
optionalPath(c.getParam("archive.arweave.walletJwkPath")),
|
||||
nonNegative(c.getInt("archive.arweave.minConfirmations", 1), "archive.arweave.minConfirmations"),
|
||||
positive(c.getInt("archive.arweave.confirmPollSeconds", 30), "archive.arweave.confirmPollSeconds"),
|
||||
positive(c.getInt("archive.arweave.confirmTimeoutMinutes", 180), "archive.arweave.confirmTimeoutMinutes"),
|
||||
optionalPath(c.getParam("archive.solana.rootKeyPath")),
|
||||
optionalPath(c.getParam("archive.solana.clientKeyPath")),
|
||||
firstNonBlank(c.getParam("solana.users.sync.rpcUrl"), c.getParam("solana.rpcUrl"), SolanaProgramsConfig.SOLANA_RPC_URL),
|
||||
firstNonBlank(c.getParam("solana.users.sync.programId"), SolanaProgramsConfig.SHINE_USERS_PROGRAM_ID),
|
||||
SolanaProgramsConfig.SHINE_PAYMENTS_PROGRAM_ID,
|
||||
positive(c.getInt("archive.solana.confirmPollSeconds", 5), "archive.solana.confirmPollSeconds"),
|
||||
positive(c.getInt("archive.solana.confirmTimeoutMinutes", 30), "archive.solana.confirmTimeoutMinutes"),
|
||||
orDefault(c.getParam("archive.solana.commitment"), "finalized")
|
||||
);
|
||||
}
|
||||
|
||||
public void validateForStart() {
|
||||
if (!enabled) return;
|
||||
if (serverLogin == null || serverLogin.isBlank()) throw new IllegalArgumentException("server.SHiNE.login обязателен для archive publisher");
|
||||
if (arweaveWalletJwkPath == null) throw new IllegalArgumentException("archive.arweave.walletJwkPath обязателен");
|
||||
if (solanaRootKeyPath == null) throw new IllegalArgumentException("archive.solana.rootKeyPath обязателен");
|
||||
if (solanaClientKeyPath == null) throw new IllegalArgumentException("archive.solana.clientKeyPath обязателен");
|
||||
if (solanaRpcUrl == null || solanaRpcUrl.isBlank()) throw new IllegalArgumentException("solana.users.sync.rpcUrl или solana.rpcUrl обязателен");
|
||||
if (maxFileBytes >= 0x1_0000_0000L) {
|
||||
throw new IllegalArgumentException("SHINE-ARCHIVE v1 требует archive.maxFileBytes < 4294967296");
|
||||
}
|
||||
}
|
||||
|
||||
private static String requiredIfEnabled(AppConfig c, String name) {
|
||||
String v = c.getParam(name);
|
||||
return v == null ? "" : v.trim();
|
||||
}
|
||||
private static Path optionalPath(String s) { return s == null || s.isBlank() ? null : Path.of(s.trim()); }
|
||||
private static String firstNonBlank(String... values) {
|
||||
if (values != null) for (String v : values) if (v != null && !v.isBlank()) return v.trim();
|
||||
return "";
|
||||
}
|
||||
private static String orDefault(String v, String d) { return v == null || v.isBlank() ? d : v.trim(); }
|
||||
private static long parseLong(String s, long d) { return s == null || s.isBlank() ? d : Long.parseLong(s.trim()); }
|
||||
private static LocalTime parseTime(String value) {
|
||||
try {
|
||||
return LocalTime.parse(value.trim());
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("archive.publish.time должен быть в формате HH:mm, например 00:00", e);
|
||||
}
|
||||
}
|
||||
private static ZoneId parseZone(String value) {
|
||||
if (value == null || value.isBlank()) return ZoneId.systemDefault();
|
||||
try {
|
||||
return ZoneId.of(value.trim());
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("Некорректный archive.publish.zoneId: " + value, e);
|
||||
}
|
||||
}
|
||||
private static int positive(int v, String n) { if (v <= 0) throw new IllegalArgumentException(n + " должен быть > 0"); return v; }
|
||||
private static int nonNegative(int v, String n) { if (v < 0) throw new IllegalArgumentException(n + " должен быть >= 0"); return v; }
|
||||
private static long positiveLong(long v, String n) { if (v <= 0) throw new IllegalArgumentException(n + " должен быть > 0"); return v; }
|
||||
}
|
||||
-90
@@ -1,90 +0,0 @@
|
||||
package server.archive;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/** Опциональный scheduler: один новый archive job в заданное локальное время каждый день. */
|
||||
public final class ArchivePublisherScheduler {
|
||||
private static final Logger log = LoggerFactory.getLogger(ArchivePublisherScheduler.class);
|
||||
private static final AtomicBoolean STARTED = new AtomicBoolean(false);
|
||||
private static ScheduledExecutorService executor;
|
||||
|
||||
private ArchivePublisherScheduler() {}
|
||||
|
||||
public static void startOrLog() {
|
||||
ArchivePublisherConfig cfg;
|
||||
try {
|
||||
cfg = ArchivePublisherConfig.load();
|
||||
} catch (Exception e) {
|
||||
log.error("Не удалось прочитать archive publisher config", e);
|
||||
return;
|
||||
}
|
||||
if (!cfg.enabled()) {
|
||||
log.info("Archive publisher выключен (archive.publish.enabled=false)");
|
||||
return;
|
||||
}
|
||||
if (!STARTED.compareAndSet(false, true)) return;
|
||||
|
||||
try {
|
||||
ArchivePublisherService service = new ArchivePublisherService(cfg);
|
||||
executor = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
|
||||
@Override public Thread newThread(Runnable r) {
|
||||
Thread t = new Thread(r, "shine-archive-publisher");
|
||||
t.setDaemon(true);
|
||||
return t;
|
||||
}
|
||||
});
|
||||
|
||||
// Незавершённую публикацию после рестарта продолжаем сразу, а новый snapshot
|
||||
// создаём только в очередное назначенное суточное время.
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
service.resumeUnfinishedJob();
|
||||
} catch (Exception e) {
|
||||
log.error("Не удалось продолжить незавершённый archive job", e);
|
||||
}
|
||||
});
|
||||
scheduleNextDaily(service, cfg);
|
||||
log.info("Archive publisher включён: login={} dir={} dailyAt={} zone={}",
|
||||
cfg.serverLogin(), cfg.workDir(), cfg.publishTime(), cfg.publishZone());
|
||||
} catch (Exception e) {
|
||||
STARTED.set(false);
|
||||
log.error("Archive publisher НЕ запущен из-за ошибки preflight", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void scheduleNextDaily(ArchivePublisherService service, ArchivePublisherConfig cfg) {
|
||||
ScheduledExecutorService e = executor;
|
||||
if (e == null || e.isShutdown()) return;
|
||||
|
||||
ZonedDateTime now = ZonedDateTime.now(cfg.publishZone());
|
||||
ZonedDateTime next = now.toLocalDate().atTime(cfg.publishTime()).atZone(cfg.publishZone());
|
||||
if (!next.isAfter(now)) next = next.plusDays(1);
|
||||
long delayMs = Math.max(1L, Duration.between(now, next).toMillis());
|
||||
|
||||
e.schedule(() -> {
|
||||
try {
|
||||
service.runCycle();
|
||||
} catch (Exception ex) {
|
||||
log.error("Archive publisher daily cycle failed; retry нового snapshot будет в следующее назначенное время", ex);
|
||||
} finally {
|
||||
scheduleNextDaily(service, cfg);
|
||||
}
|
||||
}, delayMs, TimeUnit.MILLISECONDS);
|
||||
|
||||
log.info("Следующая архивная публикация запланирована на {}", next);
|
||||
}
|
||||
|
||||
public static void close() {
|
||||
ScheduledExecutorService e = executor;
|
||||
if (e != null) e.shutdownNow();
|
||||
}
|
||||
}
|
||||
-341
@@ -1,341 +0,0 @@
|
||||
package server.archive;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import server.logic.ws_protocol.JSON.handlers.blockchain.Net_AddBlock_Handler_utils.BlockchainLocks;
|
||||
import shine.db.archive.ArchiveBigBlockRef;
|
||||
import shine.db.archive.ArchiveChainCursor;
|
||||
import shine.db.archive.ArchivePublishJob;
|
||||
import shine.db.archive.ArchivePublishJobChain;
|
||||
import shine.db.dao.ArchivePublicationDAO;
|
||||
import shine.db.dao.ArchiveImportDAO;
|
||||
import shine.db.dao.BlockchainStateDAO;
|
||||
import shine.db.dao.BlocksDAO;
|
||||
import shine.db.entities.BlockEntry;
|
||||
import shine.db.entities.BlockchainStateEntry;
|
||||
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/** Один crash-safe цикл публикации большого SHINE-ARCHIVE блока. */
|
||||
public final class ArchivePublisherService {
|
||||
private static final Logger log = LoggerFactory.getLogger(ArchivePublisherService.class);
|
||||
|
||||
private final ArchivePublisherConfig cfg;
|
||||
private final ArchivePublicationDAO archiveDao = ArchivePublicationDAO.getInstance();
|
||||
private final ArchiveImportDAO archiveImportDao = ArchiveImportDAO.getInstance();
|
||||
private final BlockchainStateDAO stateDao = BlockchainStateDAO.getInstance();
|
||||
private final BlocksDAO blocksDao = BlocksDAO.getInstance();
|
||||
private final ShineArchiveWriter writer = new ShineArchiveWriter();
|
||||
private final ArweaveArchiveService arweave;
|
||||
private final SolanaArchiveHeadWriter solana;
|
||||
private final byte[] archiveSigningSeed32;
|
||||
private final AtomicBoolean running = new AtomicBoolean(false);
|
||||
|
||||
public ArchivePublisherService(ArchivePublisherConfig cfg) throws Exception {
|
||||
this.cfg = Objects.requireNonNull(cfg);
|
||||
cfg.validateForStart();
|
||||
Files.createDirectories(cfg.workDir());
|
||||
this.archiveSigningSeed32 = ArchiveKeyLoader.loadSeed32(cfg.solanaRootKeyPath());
|
||||
this.arweave = new ArweaveArchiveService(cfg);
|
||||
this.solana = new SolanaArchiveHeadWriter(cfg);
|
||||
this.solana.validateKeysAgainstPda();
|
||||
backfillOwnLocationIndex();
|
||||
}
|
||||
|
||||
/**
|
||||
* После обновления старого publisher-сервера заполняет универсальный location index
|
||||
* из уже существующих finalized cursors, не дожидаясь следующего суточного archive block.
|
||||
*/
|
||||
private void backfillOwnLocationIndex() throws Exception {
|
||||
Map<Long, ArchiveBigBlockRef> finalized = new HashMap<>();
|
||||
for (ArchiveBigBlockRef ref : archiveDao.listFinalizedBigBlocks()) finalized.put(ref.bigBlockNumber(), ref);
|
||||
if (finalized.isEmpty()) return;
|
||||
long now = System.currentTimeMillis();
|
||||
int updated = 0;
|
||||
for (BlockchainStateEntry state : stateDao.listAll()) {
|
||||
if (state == null || state.getBlockchainName() == null || state.getBlockchainName().isBlank()) continue;
|
||||
ArchiveChainCursor cursor = archiveDao.getCursor(state.getBlockchainName());
|
||||
if (cursor == null) continue;
|
||||
ArchiveBigBlockRef ref = finalized.get(cursor.lastArchiveBigBlockNumber());
|
||||
if (ref == null || ref.arweaveTxId() == null || ref.arweaveTxId().length != 32) continue;
|
||||
archiveImportDao.upsertLocation(new shine.db.archive.ArchiveBlockchainLocation(
|
||||
cursor.blockchainName(), cfg.serverLogin(), b64url(ref.arweaveTxId()),
|
||||
cursor.lastArchiveBigBlockHash(), cursor.lastArchiveBigBlockNumber(),
|
||||
cursor.lastChunkOffset(), cursor.lastChunkSize(), cursor.lastArchivedSourceBlockNumber(), now));
|
||||
updated++;
|
||||
}
|
||||
if (updated > 0) log.info("Archive publisher: восстановлен location index для {} blockchain", updated);
|
||||
}
|
||||
|
||||
/** После рестарта продолжает только уже существующий незавершённый job; новый snapshot не создаёт. */
|
||||
public boolean resumeUnfinishedJob() throws Exception {
|
||||
if (!running.compareAndSet(false, true)) return false;
|
||||
try {
|
||||
ArchivePublishJob job = archiveDao.findUnfinishedJob();
|
||||
if (job == null) return false;
|
||||
log.info("Archive publisher: после рестарта продолжаем job id={} block={} status={}",
|
||||
job.id(), job.bigBlockNumber(), job.status());
|
||||
process(job.id());
|
||||
return true;
|
||||
} finally {
|
||||
running.set(false);
|
||||
}
|
||||
}
|
||||
|
||||
/** Запускает суточный цикл: продолжает незавершённый job либо создаёт новый frozen snapshot. */
|
||||
public boolean runCycle() throws Exception {
|
||||
if (!running.compareAndSet(false, true)) {
|
||||
log.info("Archive publisher: предыдущий цикл ещё работает, новый пропущен");
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
ArchivePublishJob job = archiveDao.findUnfinishedJob();
|
||||
if (job == null) {
|
||||
job = createFrozenJob();
|
||||
if (job == null) {
|
||||
log.info("Archive publisher: новых SHiNE-блоков нет");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
log.info("Archive publisher: продолжаем незавершённый job id={} block={} status={}",
|
||||
job.id(), job.bigBlockNumber(), job.status());
|
||||
}
|
||||
process(job.id());
|
||||
return true;
|
||||
} finally {
|
||||
running.set(false);
|
||||
}
|
||||
}
|
||||
|
||||
private ArchivePublishJob createFrozenJob() throws Exception {
|
||||
List<ArchivePublishJobChain> chains = new ArrayList<>();
|
||||
for (BlockchainStateEntry listed : stateDao.listAll()) {
|
||||
if (listed == null || listed.getBlockchainName() == null || listed.getBlockchainName().isBlank()) continue;
|
||||
String name = listed.getBlockchainName();
|
||||
ReentrantLock lock = BlockchainLocks.lockFor(name);
|
||||
lock.lock();
|
||||
try {
|
||||
BlockchainStateEntry state = stateDao.getByBlockchainName(name);
|
||||
if (state == null || state.getLastBlockNumber() < 0) continue;
|
||||
ArchiveChainCursor cursor = archiveDao.getCursor(name);
|
||||
long from;
|
||||
byte[] previousSourceHash;
|
||||
Long prevBig = null;
|
||||
byte[] prevBigHash = null;
|
||||
Long prevChunkOffset = null;
|
||||
Long prevChunkSize = null;
|
||||
|
||||
if (cursor == null) {
|
||||
from = 0L;
|
||||
previousSourceHash = new byte[32];
|
||||
} else {
|
||||
long cursorBlock = cursor.lastArchivedSourceBlockNumber();
|
||||
if (cursorBlock < 0 || cursorBlock > 0xffff_ffffL) {
|
||||
throw new IllegalStateException("Некорректный archive cursor для " + name + ": " + cursorBlock);
|
||||
}
|
||||
byte[] actualCursorHash = blocksDao.getHashByNumber(name, (int) cursorBlock);
|
||||
if (!Arrays.equals(actualCursorHash, cursor.lastArchivedSourceBlockHash())) {
|
||||
throw new IllegalStateException("Archive cursor hash не совпадает с локальной blockchain: " + name + "#" + cursorBlock);
|
||||
}
|
||||
from = cursorBlock + 1L;
|
||||
previousSourceHash = cursor.lastArchivedSourceBlockHash();
|
||||
prevBig = cursor.lastArchiveBigBlockNumber();
|
||||
prevBigHash = cursor.lastArchiveBigBlockHash();
|
||||
prevChunkOffset = cursor.lastChunkOffset();
|
||||
prevChunkSize = cursor.lastChunkSize();
|
||||
}
|
||||
|
||||
long to = Integer.toUnsignedLong(state.getLastBlockNumber());
|
||||
if (from > to) continue;
|
||||
if (from > 0xffff_ffffL || to > 0xffff_ffffL) throw new IllegalStateException("Source block number вне u32: " + name);
|
||||
byte[] lastHash = state.getLastBlockHash();
|
||||
if (lastHash == null || lastHash.length != 32) {
|
||||
lastHash = blocksDao.getHashByNumber(name, (int) to);
|
||||
}
|
||||
require32(lastHash, "last source block hash " + name);
|
||||
chains.add(new ArchivePublishJobChain(
|
||||
0L, name, from, to, previousSourceHash, lastHash,
|
||||
prevBig, prevBigHash, prevChunkOffset, prevChunkSize, null, null));
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
if (chains.isEmpty()) return null;
|
||||
chains.sort(Comparator.comparing(ArchivePublishJobChain::blockchainName));
|
||||
long bigBlock = archiveDao.nextBigBlockNumber();
|
||||
if (bigBlock > 0xffff_ffffL) throw new IllegalStateException("Закончился диапазон u32 big_block_number");
|
||||
long createdAt = System.currentTimeMillis();
|
||||
long id = archiveDao.createJobWithChains(bigBlock, createdAt, chains);
|
||||
log.info("Archive snapshot создан: job={} bigBlock={} chains={}", id, bigBlock, chains.size());
|
||||
return archiveDao.getJob(id);
|
||||
}
|
||||
|
||||
private void process(long jobId) throws Exception {
|
||||
while (true) {
|
||||
ArchivePublishJob job = archiveDao.getJob(jobId);
|
||||
if (job == null) throw new IllegalStateException("Archive job исчез: " + jobId);
|
||||
switch (job.status()) {
|
||||
case "SNAPSHOT_CREATED" -> buildFile(job);
|
||||
case "FILE_BUILT" -> uploadArweave(job);
|
||||
case "ARWEAVE_UPLOADED" -> waitArweave(job);
|
||||
case "ARWEAVE_CONFIRMED" -> submitSolana(job);
|
||||
case "SOLANA_SUBMITTED" -> waitSolana(job);
|
||||
case "SOLANA_FINALIZED" -> commitCursors(job);
|
||||
case "CURSORS_COMMITTED" -> {
|
||||
log.info("Archive job завершён: id={} bigBlock={} file={} tx={}", job.id(), job.bigBlockNumber(), job.localArchivePath(), b64url(job.arweaveTxId()));
|
||||
return;
|
||||
}
|
||||
case "FAILED" -> throw new IllegalStateException("Archive job помечен FAILED: " + job.errorText());
|
||||
default -> throw new IllegalStateException("Неизвестный archive job status: " + job.status());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void buildFile(ArchivePublishJob job) throws Exception {
|
||||
List<ArchivePublishJobChain> chains = archiveDao.listJobChains(job.id());
|
||||
if (chains.isEmpty()) throw new IllegalStateException("SNAPSHOT_CREATED job не содержит chains: " + job.id());
|
||||
Map<String,List<BlockEntry>> records = new LinkedHashMap<>();
|
||||
|
||||
for (ArchivePublishJobChain ch : chains) {
|
||||
ReentrantLock lock = BlockchainLocks.lockFor(ch.blockchainName());
|
||||
lock.lock();
|
||||
try {
|
||||
if (ch.fromSourceBlockNumber() > 0) {
|
||||
byte[] prev = blocksDao.getHashByNumber(ch.blockchainName(), (int)(ch.fromSourceBlockNumber() - 1));
|
||||
if (!Arrays.equals(prev, ch.previousSourceBlockHash())) {
|
||||
throw new IllegalStateException("Frozen previous source hash изменился: " + ch.blockchainName());
|
||||
}
|
||||
}
|
||||
List<BlockEntry> range = blocksDao.listRangeByNumber(
|
||||
ch.blockchainName(), (int)ch.fromSourceBlockNumber(), (int)ch.toSourceBlockNumber());
|
||||
if (range.isEmpty()) throw new IllegalStateException("Frozen range пуст: " + ch.blockchainName());
|
||||
byte[] last = range.get(range.size()-1).getBlockHash();
|
||||
if (!Arrays.equals(last, ch.lastSourceBlockHash())) {
|
||||
throw new IllegalStateException("Frozen last source hash изменился: " + ch.blockchainName());
|
||||
}
|
||||
records.put(ch.blockchainName(), range);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
List<ArchiveBigBlockRef> previous = archiveDao.listFinalizedBigBlocks();
|
||||
Path file = ArchiveFileNames.pendingFilePath(
|
||||
cfg.workDir(), cfg.serverLogin(), job.bigBlockNumber(), job.createdAtMs(), cfg.publishZone());
|
||||
ShineArchiveWriter.BuildResult result = writer.build(
|
||||
file, cfg.serverLogin(), job.bigBlockNumber(), job.createdAtMs(), previous,
|
||||
chains, records, archiveSigningSeed32, cfg.maxFileBytes());
|
||||
for (Map.Entry<String, ShineArchiveWriter.ChunkPosition> e : result.chunks().entrySet()) {
|
||||
archiveDao.updateNewChunkPosition(job.id(), e.getKey(), e.getValue().offset(), e.getValue().size());
|
||||
}
|
||||
archiveDao.markFileBuilt(job.id(), result.file().toString(), result.archiveHash());
|
||||
log.info("SHINE-ARCHIVE локально сохранён: block={} file={} bytes={} hash={}",
|
||||
job.bigBlockNumber(), result.file(), result.fileSize(), hex(result.archiveHash()));
|
||||
}
|
||||
|
||||
private void uploadArweave(ArchivePublishJob job) throws Exception {
|
||||
Path file = requireLocalFile(job);
|
||||
byte[] verified = ShineArchiveWriter.verifyLocalFile(file);
|
||||
if (!Arrays.equals(verified, job.archiveHash())) throw new IllegalStateException("Локальный archive hash отличается от job DB");
|
||||
ArweaveArchiveService.UploadResult upload = arweave.upload(file, cfg.serverLogin(), job.bigBlockNumber(), job.archiveHash());
|
||||
// Сначала надёжно фиксируем полученный TX ID в БД. Если процесс упадёт до rename,
|
||||
// recovery на следующем шаге переименует тот же временный файл без повторной загрузки.
|
||||
archiveDao.markArweaveUploaded(job.id(), upload.txIdBytes());
|
||||
Path uploadedFile = markLocalFileUploaded(job, file, upload.txIdBytes());
|
||||
log.info("SHINE-ARCHIVE загружен в Arweave: block={} tx={} file={}",
|
||||
job.bigBlockNumber(), upload.txId(), uploadedFile);
|
||||
}
|
||||
|
||||
private void waitArweave(ArchivePublishJob job) throws Exception {
|
||||
ensureUploadedLocalName(job);
|
||||
String tx = b64url(job.arweaveTxId());
|
||||
int confirmations = arweave.waitForConfirmations(tx);
|
||||
archiveDao.markArweaveConfirmed(job.id(), confirmations);
|
||||
log.info("Arweave archive закреплён: block={} tx={} confirmations={}", job.bigBlockNumber(), tx, confirmations);
|
||||
}
|
||||
|
||||
|
||||
/** После успешного Arweave upload временный файл получает реальный TX ID в имени и остаётся локально. */
|
||||
private Path markLocalFileUploaded(ArchivePublishJob job, Path currentFile, byte[] txId) throws Exception {
|
||||
require32(txId, "arweave tx id");
|
||||
Path target = ArchiveFileNames.uploadedFilePath(
|
||||
cfg.workDir(), cfg.serverLogin(), job.bigBlockNumber(), job.createdAtMs(), cfg.publishZone(), txId);
|
||||
Files.createDirectories(target.toAbsolutePath().getParent());
|
||||
if (!currentFile.equals(target)) {
|
||||
try {
|
||||
Files.move(currentFile, target, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (AtomicMoveNotSupportedException e) {
|
||||
Files.move(currentFile, target, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
}
|
||||
archiveDao.updateLocalArchivePath(job.id(), target.toString());
|
||||
return target;
|
||||
}
|
||||
|
||||
/** Crash recovery: TX уже сохранён, а временный файл ещё не успели переименовать. */
|
||||
private void ensureUploadedLocalName(ArchivePublishJob job) throws Exception {
|
||||
require32(job.arweaveTxId(), "job.arweave_tx_id");
|
||||
Path target = ArchiveFileNames.uploadedFilePath(
|
||||
cfg.workDir(), cfg.serverLogin(), job.bigBlockNumber(), job.createdAtMs(), cfg.publishZone(), job.arweaveTxId());
|
||||
if (Files.isRegularFile(target)) {
|
||||
if (!target.toString().equals(job.localArchivePath())) {
|
||||
archiveDao.updateLocalArchivePath(job.id(), target.toString());
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (job.localArchivePath() == null || job.localArchivePath().isBlank()) return;
|
||||
Path current = Path.of(job.localArchivePath());
|
||||
if (Files.isRegularFile(current)) {
|
||||
markLocalFileUploaded(job, current, job.arweaveTxId());
|
||||
}
|
||||
}
|
||||
|
||||
private void submitSolana(ArchivePublishJob job) throws Exception {
|
||||
require32(job.arweaveTxId(), "job.arweave_tx_id");
|
||||
require32(job.archiveHash(), "job.archive_hash");
|
||||
SolanaArchiveHeadWriter.SubmitResult submitted = solana.submitArchiveHead(job.arweaveTxId(), job.archiveHash());
|
||||
archiveDao.markSolanaSubmitted(job.id(), submitted.signature());
|
||||
}
|
||||
|
||||
private void waitSolana(ArchivePublishJob job) throws Exception {
|
||||
if (job.solanaSignature() == null || job.solanaSignature().isBlank()) throw new IllegalStateException("SOLANA_SUBMITTED без signature");
|
||||
solana.waitFinalized(job.solanaSignature());
|
||||
if (!solana.currentHeadMatches(job.arweaveTxId(), job.archiveHash())) {
|
||||
throw new IllegalStateException("После Solana finalized User PDA archive head не совпадает с job");
|
||||
}
|
||||
archiveDao.markSolanaFinalized(job.id());
|
||||
log.info("Solana archive head finalized: block={} signature={}", job.bigBlockNumber(), job.solanaSignature());
|
||||
}
|
||||
|
||||
private void commitCursors(ArchivePublishJob job) throws Exception {
|
||||
archiveDao.commitCursors(job.id(), job.archiveHash());
|
||||
|
||||
// Тот же универсальный индекс используется и для собственных опубликованных архивов.
|
||||
// Поэтому UI publisher-сервера получает ссылку даже если он не импортирует самого себя через whitelist.
|
||||
String txId = b64url(job.arweaveTxId());
|
||||
long now = System.currentTimeMillis();
|
||||
for (ArchivePublishJobChain ch : archiveDao.listJobChains(job.id())) {
|
||||
if (ch.newChunkOffset() == null || ch.newChunkSize() == null) continue;
|
||||
archiveImportDao.upsertLocation(new shine.db.archive.ArchiveBlockchainLocation(
|
||||
ch.blockchainName(), cfg.serverLogin(), txId, job.archiveHash(), job.bigBlockNumber(),
|
||||
ch.newChunkOffset(), ch.newChunkSize(), ch.toSourceBlockNumber(), now));
|
||||
}
|
||||
}
|
||||
|
||||
private static Path requireLocalFile(ArchivePublishJob job) {
|
||||
if (job.localArchivePath() == null || job.localArchivePath().isBlank()) throw new IllegalStateException("FILE_BUILT без local_archive_path");
|
||||
Path p = Path.of(job.localArchivePath());
|
||||
if (!Files.isRegularFile(p)) throw new IllegalStateException("Локальный archive-файл отсутствует: " + p);
|
||||
return p;
|
||||
}
|
||||
|
||||
private static void require32(byte[] v, String name) {
|
||||
if (v == null || v.length != 32) throw new IllegalStateException(name + " должен быть 32 bytes");
|
||||
}
|
||||
private static String b64url(byte[] v) { return v == null ? "" : Base64.getUrlEncoder().withoutPadding().encodeToString(v); }
|
||||
private static String hex(byte[] b){StringBuilder s=new StringBuilder();for(byte x:b)s.append(String.format("%02x",x));return s.toString();}
|
||||
}
|
||||
-259
@@ -1,259 +0,0 @@
|
||||
package server.archive;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.*;
|
||||
import java.security.spec.MGF1ParameterSpec;
|
||||
import java.security.spec.PSSParameterSpec;
|
||||
import java.security.spec.RSAPrivateCrtKeySpec;
|
||||
import java.time.Duration;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Публикация готовых локальных SHINE-ARCHIVE файлов в Arweave v2.
|
||||
* Поддерживает стандартный data Merkle tree и загрузку через /chunk.
|
||||
*/
|
||||
public final class ArweaveArchiveService {
|
||||
private static final Logger log = LoggerFactory.getLogger(ArweaveArchiveService.class);
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final Base64.Encoder B64 = Base64.getUrlEncoder().withoutPadding();
|
||||
private static final Base64.Decoder B64D = Base64.getUrlDecoder();
|
||||
|
||||
private final ArchivePublisherConfig cfg;
|
||||
private final HttpClient http;
|
||||
private volatile Wallet wallet;
|
||||
|
||||
public ArweaveArchiveService(ArchivePublisherConfig cfg) {
|
||||
this.cfg = Objects.requireNonNull(cfg);
|
||||
this.http = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(20)).build();
|
||||
}
|
||||
|
||||
public record UploadResult(String txId, byte[] txIdBytes, String gateway) {}
|
||||
|
||||
public UploadResult upload(Path archiveFile, String publisherLogin, long bigBlockNumber, byte[] archiveHash)
|
||||
throws Exception {
|
||||
long dataSize = Files.size(archiveFile);
|
||||
if (dataSize <= 0) throw new IllegalArgumentException("Пустой SHINE-ARCHIVE");
|
||||
Wallet w = wallet();
|
||||
String gateway = trimGateway(cfg.arweaveGateway());
|
||||
|
||||
ArweaveMerkle.Prepared merkle = ArweaveMerkle.prepare(archiveFile);
|
||||
String anchor = getRequiredText(gateway, "/tx_anchor");
|
||||
String reward = getRequiredText(gateway, "/price/" + dataSize);
|
||||
if (!reward.matches("^\\d+$")) throw new IOException("Arweave вернул некорректную цену: " + reward);
|
||||
verifyBalance(gateway, w.address(), reward);
|
||||
|
||||
List<List<byte[]>> tagList = new ArrayList<>();
|
||||
List<Map<String,String>> jsonTags = new ArrayList<>();
|
||||
addTag(jsonTags, tagList, "Content-Type", "application/octet-stream");
|
||||
addTag(jsonTags, tagList, "App-Name", "SHiNE");
|
||||
addTag(jsonTags, tagList, "SHiNE-Type", "archive");
|
||||
addTag(jsonTags, tagList, "SHiNE-Archive-Version", "1.0");
|
||||
addTag(jsonTags, tagList, "SHiNE-Publisher", publisherLogin);
|
||||
addTag(jsonTags, tagList, "SHiNE-Big-Block", Long.toUnsignedString(bigBlockNumber));
|
||||
addTag(jsonTags, tagList, "SHiNE-Archive-SHA256", hex(archiveHash));
|
||||
|
||||
byte[] signaturePayload = deepHash(List.of(
|
||||
utf8("2"),
|
||||
b64Decode(w.owner()),
|
||||
new byte[0],
|
||||
utf8("0"),
|
||||
utf8(reward),
|
||||
b64Decode(anchor),
|
||||
tagList,
|
||||
utf8(Long.toString(dataSize)),
|
||||
merkle.dataRoot()
|
||||
));
|
||||
byte[] rawSignature = signPayload(w.privateKey(), signaturePayload);
|
||||
byte[] txIdBytes = sha256(rawSignature);
|
||||
String txId = b64(txIdBytes);
|
||||
|
||||
Map<String,Object> tx = new LinkedHashMap<>();
|
||||
tx.put("format", 2);
|
||||
tx.put("id", txId);
|
||||
tx.put("last_tx", anchor);
|
||||
tx.put("owner", w.owner());
|
||||
tx.put("tags", jsonTags);
|
||||
tx.put("target", "");
|
||||
tx.put("quantity", "0");
|
||||
tx.put("data_root", b64(merkle.dataRoot()));
|
||||
tx.put("data_size", Long.toString(dataSize));
|
||||
tx.put("data", "");
|
||||
tx.put("reward", reward);
|
||||
tx.put("signature", b64(rawSignature));
|
||||
|
||||
postJsonExpect2xx(gateway + "/tx", tx, Duration.ofSeconds(60));
|
||||
log.info("Arweave tx header принят: tx={}, chunks={}, bytes={}", txId, merkle.chunks().size(), dataSize);
|
||||
|
||||
for (int i=0; i<merkle.chunks().size(); i++) {
|
||||
ArweaveMerkle.Chunk chunkMeta = merkle.chunks().get(i);
|
||||
ArweaveMerkle.Proof proof = merkle.proofs().get(i);
|
||||
byte[] chunk = ArweaveMerkle.readChunk(archiveFile, chunkMeta);
|
||||
Map<String,Object> body = new LinkedHashMap<>();
|
||||
body.put("data_root", b64(merkle.dataRoot()));
|
||||
body.put("data_size", Long.toString(dataSize));
|
||||
body.put("data_path", b64(proof.proof()));
|
||||
body.put("offset", Long.toString(proof.offset()));
|
||||
body.put("chunk", b64(chunk));
|
||||
postChunkWithRetry(gateway, body, i, merkle.chunks().size());
|
||||
}
|
||||
return new UploadResult(txId, txIdBytes, gateway);
|
||||
}
|
||||
|
||||
/** Ждёт включения tx в блок и заданного количества подтверждений. */
|
||||
public int waitForConfirmations(String txId) throws Exception {
|
||||
if (cfg.arweaveMinConfirmations() <= 0) return 0;
|
||||
String gateway = trimGateway(cfg.arweaveGateway());
|
||||
long deadline = System.currentTimeMillis() + cfg.arweaveConfirmTimeoutMinutes() * 60_000L;
|
||||
int last = 0;
|
||||
while (System.currentTimeMillis() < deadline) {
|
||||
HttpRequest req = HttpRequest.newBuilder(URI.create(gateway + "/tx/" + txId + "/status"))
|
||||
.timeout(Duration.ofSeconds(20)).GET().build();
|
||||
HttpResponse<String> resp = http.send(req, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
||||
if (resp.statusCode() == 200) {
|
||||
JsonNode json = MAPPER.readTree(resp.body());
|
||||
last = json.path("number_of_confirmations").asInt(0);
|
||||
if (last >= cfg.arweaveMinConfirmations()) return last;
|
||||
} else if (resp.statusCode() != 202 && resp.statusCode() != 404) {
|
||||
log.warn("Arweave status tx={} HTTP {}: {}", txId, resp.statusCode(), safe(resp.body()));
|
||||
}
|
||||
Thread.sleep(cfg.arweaveConfirmPollSeconds() * 1000L);
|
||||
}
|
||||
throw new IOException("Истёк timeout ожидания Arweave confirmations для " + txId + ", last=" + last);
|
||||
}
|
||||
|
||||
private void postChunkWithRetry(String gateway, Map<String,Object> body, int index, int total) throws Exception {
|
||||
Exception last = null;
|
||||
for (int attempt=1; attempt<=12; attempt++) {
|
||||
try {
|
||||
postJsonExpect2xx(gateway + "/chunk", body, Duration.ofSeconds(60));
|
||||
if ((index + 1) % 25 == 0 || index + 1 == total) {
|
||||
log.info("Arweave chunks: {}/{}", index + 1, total);
|
||||
}
|
||||
return;
|
||||
} catch (Exception e) {
|
||||
last = e;
|
||||
if (attempt == 12) break;
|
||||
log.warn("Arweave chunk {}/{} attempt {}/12: {}", index + 1, total, attempt, e.getMessage());
|
||||
Thread.sleep(5_000L);
|
||||
}
|
||||
}
|
||||
throw new IOException("Не удалось загрузить Arweave chunk " + (index + 1) + "/" + total, last);
|
||||
}
|
||||
|
||||
private void postJsonExpect2xx(String url, Object body, Duration timeout) throws Exception {
|
||||
String json = MAPPER.writeValueAsString(body);
|
||||
HttpRequest req = HttpRequest.newBuilder(URI.create(url))
|
||||
.timeout(timeout)
|
||||
.header("Content-Type", "application/json")
|
||||
.header("Accept", "application/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(json, StandardCharsets.UTF_8))
|
||||
.build();
|
||||
HttpResponse<String> resp = http.send(req, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
||||
if (resp.statusCode() < 200 || resp.statusCode() >= 300) {
|
||||
throw new IOException("HTTP " + resp.statusCode() + " " + safe(resp.body()));
|
||||
}
|
||||
}
|
||||
|
||||
private void verifyBalance(String gateway, String address, String reward) throws Exception {
|
||||
if (address == null || address.isBlank()) return;
|
||||
String balance = getRequiredText(gateway, "/wallet/" + address + "/balance");
|
||||
if (balance.matches("^\\d+$") && new BigInteger(balance).compareTo(new BigInteger(reward)) < 0) {
|
||||
throw new IllegalStateException("Недостаточно AR: balance=" + balance + ", reward=" + reward);
|
||||
}
|
||||
}
|
||||
|
||||
private String getRequiredText(String gateway, String path) throws Exception {
|
||||
HttpRequest req = HttpRequest.newBuilder(URI.create(gateway + path)).timeout(Duration.ofSeconds(20)).GET().build();
|
||||
HttpResponse<String> resp = http.send(req, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
||||
if (resp.statusCode() < 200 || resp.statusCode() >= 300) {
|
||||
throw new IOException("Arweave HTTP " + resp.statusCode() + " для " + path + ": " + safe(resp.body()));
|
||||
}
|
||||
String s = resp.body() == null ? "" : resp.body().trim();
|
||||
if (s.isBlank()) throw new IOException("Пустой Arweave ответ для " + path);
|
||||
return s;
|
||||
}
|
||||
|
||||
private Wallet wallet() throws Exception {
|
||||
Wallet local = wallet;
|
||||
if (local != null) return local;
|
||||
synchronized (this) {
|
||||
if (wallet != null) return wallet;
|
||||
JsonNode jwk = MAPPER.readTree(Files.readString(cfg.arweaveWalletJwkPath(), StandardCharsets.UTF_8));
|
||||
String owner = required(jwk, "n");
|
||||
PrivateKey pk = buildPrivateKey(jwk);
|
||||
String address = b64(sha256(b64Decode(owner)));
|
||||
wallet = new Wallet(owner, address, pk);
|
||||
return wallet;
|
||||
}
|
||||
}
|
||||
|
||||
private static PrivateKey buildPrivateKey(JsonNode jwk) throws GeneralSecurityException {
|
||||
RSAPrivateCrtKeySpec spec = new RSAPrivateCrtKeySpec(
|
||||
big(required(jwk,"n")), big(required(jwk,"e")), big(required(jwk,"d")),
|
||||
big(required(jwk,"p")), big(required(jwk,"q")), big(required(jwk,"dp")),
|
||||
big(required(jwk,"dq")), big(required(jwk,"qi")));
|
||||
return KeyFactory.getInstance("RSA").generatePrivate(spec);
|
||||
}
|
||||
|
||||
private static byte[] signPayload(PrivateKey key, byte[] payload) throws GeneralSecurityException {
|
||||
Signature signature = Signature.getInstance("RSASSA-PSS");
|
||||
signature.setParameter(new PSSParameterSpec("SHA-256", "MGF1", MGF1ParameterSpec.SHA256, 32, 1));
|
||||
signature.initSign(key);
|
||||
signature.update(payload);
|
||||
return signature.sign();
|
||||
}
|
||||
|
||||
private static byte[] deepHash(Object data) throws GeneralSecurityException {
|
||||
if (data instanceof List<?> list) {
|
||||
byte[] tag = concat(utf8("list"), utf8(Integer.toString(list.size())));
|
||||
return deepHashChunks(list, sha384(tag));
|
||||
}
|
||||
if (!(data instanceof byte[] bytes)) throw new IllegalArgumentException("deepHash: ожидается byte[]/List");
|
||||
byte[] tag = concat(utf8("blob"), utf8(Integer.toString(bytes.length)));
|
||||
return sha384(concat(sha384(tag), sha384(bytes)));
|
||||
}
|
||||
|
||||
private static byte[] deepHashChunks(List<?> list, byte[] acc) throws GeneralSecurityException {
|
||||
byte[] cur = acc;
|
||||
for (Object item : list) cur = sha384(concat(cur, deepHash(item)));
|
||||
return cur;
|
||||
}
|
||||
|
||||
private static void addTag(List<Map<String,String>> json, List<List<byte[]>> raw, String name, String value) {
|
||||
byte[] n=utf8(name), v=utf8(value);
|
||||
Map<String,String> item = new LinkedHashMap<>();
|
||||
item.put("name", b64(n)); item.put("value", b64(v));
|
||||
json.add(item); raw.add(List.of(n,v));
|
||||
}
|
||||
|
||||
private static String required(JsonNode node, String field) {
|
||||
String s = node == null ? "" : node.path(field).asText("").trim();
|
||||
if (s.isBlank()) throw new IllegalStateException("В Arweave JWK отсутствует " + field);
|
||||
return s;
|
||||
}
|
||||
private static BigInteger big(String s) { return new BigInteger(1, b64Decode(s)); }
|
||||
private static String trimGateway(String s) { return (s == null ? "https://arweave.net" : s.trim()).replaceAll("/+$", ""); }
|
||||
private static byte[] utf8(String s) { return String.valueOf(s == null ? "" : s).getBytes(StandardCharsets.UTF_8); }
|
||||
private static byte[] b64Decode(String s) { return B64D.decode(s); }
|
||||
private static String b64(byte[] b) { return B64.encodeToString(b); }
|
||||
private static byte[] sha256(byte[] b) throws GeneralSecurityException { return MessageDigest.getInstance("SHA-256").digest(b); }
|
||||
private static byte[] sha384(byte[] b) throws GeneralSecurityException { return MessageDigest.getInstance("SHA-384").digest(b); }
|
||||
private static String hex(byte[] b) { StringBuilder s=new StringBuilder(); for(byte x:b)s.append(String.format("%02x",x)); return s.toString(); }
|
||||
private static byte[] concat(byte[]... arrays) { int n=0; for(byte[] a:arrays)n=Math.addExact(n,a.length); byte[] o=new byte[n]; int p=0; for(byte[]a:arrays){System.arraycopy(a,0,o,p,a.length);p+=a.length;} return o; }
|
||||
private static String safe(String s) { s=String.valueOf(s==null?"":s).replace('\n',' ').replace('\r',' ').trim(); return s.length()<=250?s:s.substring(0,250)+"..."; }
|
||||
|
||||
private record Wallet(String owner, String address, PrivateKey privateKey) {}
|
||||
}
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
package server.archive;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import server.sync.ArweavePublishSignal;
|
||||
|
||||
import java.util.Base64;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* Publisher с двумя путями доставки:
|
||||
* 1) Turbo — немедленная неблокирующая попытка для только что принятого блока;
|
||||
* 2) плановый fixed-delay цикл — страховочная добивка только зависших pending-записей.
|
||||
*/
|
||||
public final class ArweaveBlockPublisherScheduler {
|
||||
private static final Logger log = LoggerFactory.getLogger(ArweaveBlockPublisherScheduler.class);
|
||||
private static final long RETRY_MIN_PENDING_AGE_MS = 60_000L;
|
||||
private static final AtomicBoolean STARTED = new AtomicBoolean(false);
|
||||
private static final AtomicBoolean IMMEDIATE_DRAIN_SCHEDULED = new AtomicBoolean(false);
|
||||
private static final ConcurrentLinkedQueue<ArweavePublishSignal.PendingRef> IMMEDIATE_QUEUE = new ConcurrentLinkedQueue<>();
|
||||
private static final ConcurrentHashMap<String, Long> PENDING_SINCE_MS = new ConcurrentHashMap<>();
|
||||
|
||||
private static ScheduledExecutorService executor;
|
||||
private static ArweaveBlockPublisherService service;
|
||||
private static ArweaveBlocksConfig config;
|
||||
|
||||
private ArweaveBlockPublisherScheduler() {}
|
||||
|
||||
public static void startOrLog() {
|
||||
ArweaveBlocksConfig cfg;
|
||||
try { cfg = ArweaveBlocksConfig.load(); cfg.validatePublisher(); }
|
||||
catch (Exception e) { log.error("Cannot read/validate Arweave block publisher config", e); return; }
|
||||
if (!cfg.publishEnabled()) { log.info("Arweave user-block publisher disabled (mode=none)"); return; }
|
||||
if (!STARTED.compareAndSet(false,true)) return;
|
||||
try {
|
||||
config = cfg;
|
||||
service = new ArweaveBlockPublisherService(cfg);
|
||||
executor = Executors.newSingleThreadScheduledExecutor(r -> {
|
||||
Thread t = new Thread(r, "shine-arweave-block-publisher"); t.setDaemon(true); return t;
|
||||
});
|
||||
|
||||
if (cfg.publishMode() == ArweaveBlocksConfig.PublishMode.TURBO) {
|
||||
ArweavePublishSignal.setListener(ArweaveBlockPublisherScheduler::enqueueImmediateTurboPublish);
|
||||
}
|
||||
|
||||
Runnable retryTask = () -> {
|
||||
try {
|
||||
if (config.publishMode() == ArweaveBlocksConfig.PublishMode.TURBO) {
|
||||
long nowMs = System.currentTimeMillis();
|
||||
service.runRetryCycle(
|
||||
id -> isOldEnoughForRetry(id, nowMs),
|
||||
id -> PENDING_SINCE_MS.remove(idKey(id))
|
||||
);
|
||||
} else {
|
||||
service.runCycle();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Arweave block publish retry cycle failed", e);
|
||||
}
|
||||
};
|
||||
long retryInitialDelayMinutes = cfg.publishMode() == ArweaveBlocksConfig.PublishMode.TURBO
|
||||
? cfg.publishIntervalMinutes()
|
||||
: 0L;
|
||||
executor.scheduleWithFixedDelay(retryTask, retryInitialDelayMinutes, cfg.publishIntervalMinutes(), TimeUnit.MINUTES);
|
||||
log.info("Arweave user-block publisher enabled: mode={} immediateTurbo={} retryInterval={}m retryMinAge={}s",
|
||||
cfg.publishMode(), cfg.publishMode() == ArweaveBlocksConfig.PublishMode.TURBO,
|
||||
cfg.publishIntervalMinutes(), RETRY_MIN_PENDING_AGE_MS / 1000L);
|
||||
} catch (Exception e) {
|
||||
ArweavePublishSignal.clearListener();
|
||||
STARTED.set(false);
|
||||
log.error("Arweave user-block publisher failed to start", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void enqueueImmediateTurboPublish(ArweavePublishSignal.PendingRef item) {
|
||||
if (!STARTED.get() || config == null || config.publishMode() != ArweaveBlocksConfig.PublishMode.TURBO) return;
|
||||
PENDING_SINCE_MS.putIfAbsent(idKey(item.dataItemId()), System.currentTimeMillis());
|
||||
IMMEDIATE_QUEUE.offer(item);
|
||||
scheduleImmediateDrain();
|
||||
}
|
||||
|
||||
private static boolean isOldEnoughForRetry(byte[] dataItemId, long nowMs) {
|
||||
Long pendingSince = PENDING_SINCE_MS.get(idKey(dataItemId));
|
||||
// Нет записи в памяти => pending пережил старт процесса и уже считается зависшим.
|
||||
return pendingSince == null || nowMs - pendingSince >= RETRY_MIN_PENDING_AGE_MS;
|
||||
}
|
||||
|
||||
private static String idKey(byte[] dataItemId) {
|
||||
return Base64.getUrlEncoder().withoutPadding().encodeToString(dataItemId);
|
||||
}
|
||||
|
||||
private static void scheduleImmediateDrain() {
|
||||
ScheduledExecutorService currentExecutor = executor;
|
||||
if (currentExecutor == null || currentExecutor.isShutdown()) return;
|
||||
if (!IMMEDIATE_DRAIN_SCHEDULED.compareAndSet(false, true)) return;
|
||||
try {
|
||||
currentExecutor.execute(ArweaveBlockPublisherScheduler::drainImmediateQueue);
|
||||
} catch (RuntimeException e) {
|
||||
IMMEDIATE_DRAIN_SCHEDULED.set(false);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private static void drainImmediateQueue() {
|
||||
try {
|
||||
ArweavePublishSignal.PendingRef item;
|
||||
while ((item = IMMEDIATE_QUEUE.poll()) != null) {
|
||||
try {
|
||||
boolean resolved = service.publishAcceptedNow(item);
|
||||
if (resolved) PENDING_SINCE_MS.remove(idKey(item.dataItemId()));
|
||||
} catch (Exception e) {
|
||||
// Turbo недоступен: не держим поток клиента и не молотим всю очередь одной и той же ошибкой.
|
||||
// Все элементы уже надёжно лежат в БД с pending=TRUE; плановый цикл подберёт их после min-age.
|
||||
log.warn("Immediate Turbo publish failed; pending blocks will be retried by the periodic cycle: {}", e.getMessage());
|
||||
IMMEDIATE_QUEUE.clear();
|
||||
break;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
IMMEDIATE_DRAIN_SCHEDULED.set(false);
|
||||
if (!IMMEDIATE_QUEUE.isEmpty()) scheduleImmediateDrain();
|
||||
}
|
||||
}
|
||||
|
||||
public static void close() {
|
||||
ArweavePublishSignal.clearListener();
|
||||
IMMEDIATE_QUEUE.clear();
|
||||
PENDING_SINCE_MS.clear();
|
||||
IMMEDIATE_DRAIN_SCHEDULED.set(false);
|
||||
ScheduledExecutorService e = executor;
|
||||
executor = null;
|
||||
service = null;
|
||||
config = null;
|
||||
STARTED.set(false);
|
||||
if (e != null) e.shutdownNow();
|
||||
}
|
||||
}
|
||||
+221
@@ -0,0 +1,221 @@
|
||||
package server.archive;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import shine.db.dao.BlocksDAO;
|
||||
import shine.db.dao.KeyRotationCandidateBlocksDAO;
|
||||
import shine.db.entities.BlockEntry;
|
||||
import shine.db.entities.KeyRotationCandidateBlockEntry;
|
||||
import server.sync.ArweavePublishSignal;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/** Publishes locally-created signed user DataItems through the configured transport. */
|
||||
public final class ArweaveBlockPublisherService {
|
||||
private static final Logger log = LoggerFactory.getLogger(ArweaveBlockPublisherService.class);
|
||||
|
||||
private final ArweaveBlocksConfig cfg;
|
||||
private final BlocksDAO blocksDAO = BlocksDAO.getInstance();
|
||||
private final KeyRotationCandidateBlocksDAO candidateBlocksDAO = KeyRotationCandidateBlocksDAO.getInstance();
|
||||
private final ArweaveL1Uploader arweaveUploader;
|
||||
private final TurboDataItemUploader turboUploader;
|
||||
|
||||
public ArweaveBlockPublisherService(ArweaveBlocksConfig cfg) {
|
||||
this.cfg = cfg;
|
||||
this.arweaveUploader = cfg.publishMode() == ArweaveBlocksConfig.PublishMode.ARWEAVE ? new ArweaveL1Uploader(cfg) : null;
|
||||
this.turboUploader = cfg.publishMode() == ArweaveBlocksConfig.PublishMode.TURBO ? new TurboDataItemUploader(cfg) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Мгновенная попытка для только что принятого DataItem. Вызывается только из фонового executor-а:
|
||||
* сетевой запрос AddBlock не ждёт результат Turbo. Если запись уже успел обработать другой цикл, это no-op.
|
||||
* При ошибке pending-флаг в БД не меняется, поэтому обычный runCycle() повторит публикацию позже.
|
||||
*/
|
||||
public boolean publishAcceptedNow(ArweavePublishSignal.PendingRef pending) throws Exception {
|
||||
if (cfg.publishMode() != ArweaveBlocksConfig.PublishMode.TURBO) return false;
|
||||
byte[] id = pending.dataItemId();
|
||||
if (pending.kind() == ArweavePublishSignal.Kind.KEY_ROTATION_CANDIDATE) {
|
||||
KeyRotationCandidateBlockEntry entry = candidateBlocksDAO.getPendingArweaveByDataItemId(id);
|
||||
if (entry == null) return true;
|
||||
byte[] raw = entry.getBlockBytes();
|
||||
if (raw == null || raw.length == 0) return false;
|
||||
TurboDataItemUploader.UploadResult result = turboUploader.upload(raw, id);
|
||||
candidateBlocksDAO.markArweavePublished(List.of(id), System.currentTimeMillis());
|
||||
log.debug("Turbo immediately published key-rotation DataItem {} chain={} block={}",
|
||||
result.dataItemId(), entry.getCandidateBlockchainName(), entry.getBlockNumber());
|
||||
return true;
|
||||
}
|
||||
|
||||
BlockEntry entry = blocksDAO.getPendingArweaveByDataItemId(id);
|
||||
if (entry == null) return true;
|
||||
byte[] raw = entry.getBlockBytes();
|
||||
if (raw == null || raw.length == 0) return false;
|
||||
TurboDataItemUploader.UploadResult result = turboUploader.upload(raw, id);
|
||||
blocksDAO.markArweavePublished(List.of(id), System.currentTimeMillis());
|
||||
log.debug("Turbo immediately published SHiNE DataItem {} chain={} block={}",
|
||||
result.dataItemId(), entry.getBchName(), entry.getBlockNumber());
|
||||
return true;
|
||||
}
|
||||
|
||||
public int runCycle() throws Exception {
|
||||
return runCycle(id -> true, id -> {});
|
||||
}
|
||||
|
||||
/** Плановый retry может отфильтровать слишком свежие pending-записи и получить callback успешных id. */
|
||||
public int runRetryCycle(Predicate<byte[]> eligible, Consumer<byte[]> onPublished) throws Exception {
|
||||
return runCycle(eligible == null ? id -> true : eligible, onPublished == null ? id -> {} : onPublished);
|
||||
}
|
||||
|
||||
private int runCycle(Predicate<byte[]> eligible, Consumer<byte[]> onPublished) throws Exception {
|
||||
if (cfg.publishMode() == ArweaveBlocksConfig.PublishMode.NONE) return 0;
|
||||
|
||||
int maxItems = cfg.publishMaxItems();
|
||||
int attempted = 0;
|
||||
int published = 0;
|
||||
|
||||
// Ротация приоритетна: пользователь уже заблокирован для обычной записи и ждёт завершения копирования.
|
||||
List<KeyRotationCandidateBlockEntry> rotationCandidates = candidateBlocksDAO.listPendingArweave(maxItems);
|
||||
rotationCandidates = rotationCandidates.stream().filter(e -> eligible.test(e.getDataItemId())).toList();
|
||||
if (!rotationCandidates.isEmpty()) {
|
||||
attempted += rotationCandidates.size();
|
||||
published += switch (cfg.publishMode()) {
|
||||
case TURBO -> publishTurboRotation(rotationCandidates, onPublished);
|
||||
case ARWEAVE -> publishDirectArweaveRotation(rotationCandidates);
|
||||
case NONE -> 0;
|
||||
};
|
||||
}
|
||||
|
||||
int remaining = Math.max(0, maxItems - attempted);
|
||||
if (remaining == 0) return published;
|
||||
|
||||
List<BlockEntry> candidates = blocksDAO.listPendingArweave(remaining);
|
||||
candidates = candidates.stream().filter(e -> eligible.test(e.getDataItemId())).toList();
|
||||
if (candidates.isEmpty()) return published;
|
||||
published += switch (cfg.publishMode()) {
|
||||
case TURBO -> publishTurbo(candidates, onPublished);
|
||||
case ARWEAVE -> publishDirectArweave(candidates);
|
||||
case NONE -> 0;
|
||||
};
|
||||
return published;
|
||||
}
|
||||
|
||||
private int publishTurboRotation(List<KeyRotationCandidateBlockEntry> candidates, Consumer<byte[]> onPublished) throws Exception {
|
||||
int published = 0;
|
||||
Exception firstFailure = null;
|
||||
for (KeyRotationCandidateBlockEntry e : candidates) {
|
||||
byte[] raw = e.getBlockBytes();
|
||||
byte[] id = e.getDataItemId();
|
||||
if (raw == null || raw.length == 0 || id == null || id.length != 32) continue;
|
||||
try {
|
||||
TurboDataItemUploader.UploadResult result = turboUploader.upload(raw, id);
|
||||
candidateBlocksDAO.markArweavePublished(List.of(id), System.currentTimeMillis());
|
||||
onPublished.accept(id);
|
||||
published++;
|
||||
log.debug("Turbo published key-rotation DataItem {} chain={} block={}",
|
||||
result.dataItemId(), e.getCandidateBlockchainName(), e.getBlockNumber());
|
||||
} catch (Exception ex) {
|
||||
if (firstFailure == null) firstFailure = ex;
|
||||
log.warn("Turbo key-rotation publish failed: chain={} block={} bytes={} error={}",
|
||||
e.getCandidateBlockchainName(), e.getBlockNumber(), raw.length, ex.getMessage());
|
||||
}
|
||||
}
|
||||
if (published > 0) log.info("Published {} SHiNE key-rotation DataItems through Turbo", published);
|
||||
if (published == 0 && firstFailure != null) throw firstFailure;
|
||||
return published;
|
||||
}
|
||||
|
||||
private int publishTurbo(List<BlockEntry> candidates, Consumer<byte[]> onPublished) throws Exception {
|
||||
int published = 0;
|
||||
Exception firstFailure = null;
|
||||
for (BlockEntry e : candidates) {
|
||||
byte[] raw = e.getBlockBytes();
|
||||
byte[] id = e.getDataItemId();
|
||||
if (raw == null || raw.length == 0 || id == null || id.length != 32) continue;
|
||||
try {
|
||||
TurboDataItemUploader.UploadResult result = turboUploader.upload(raw, id);
|
||||
blocksDAO.markArweavePublished(List.of(id), System.currentTimeMillis());
|
||||
onPublished.accept(id);
|
||||
published++;
|
||||
log.debug("Turbo published SHiNE DataItem {} chain={} block={}", result.dataItemId(), e.getBchName(), e.getBlockNumber());
|
||||
} catch (Exception ex) {
|
||||
if (firstFailure == null) firstFailure = ex;
|
||||
log.warn("Turbo publish failed: chain={} block={} bytes={} error={}",
|
||||
e.getBchName(), e.getBlockNumber(), raw.length, ex.getMessage());
|
||||
}
|
||||
}
|
||||
if (published > 0) log.info("Published {} SHiNE test DataItems through Turbo", published);
|
||||
if (published == 0 && firstFailure != null) throw firstFailure;
|
||||
return published;
|
||||
}
|
||||
|
||||
private int publishDirectArweaveRotation(List<KeyRotationCandidateBlockEntry> candidates) throws Exception {
|
||||
List<byte[]> items = new ArrayList<>();
|
||||
List<byte[]> ids = new ArrayList<>();
|
||||
long estimated = 32;
|
||||
for (KeyRotationCandidateBlockEntry e : candidates) {
|
||||
byte[] raw = e.getBlockBytes();
|
||||
byte[] id = e.getDataItemId();
|
||||
if (raw == null || raw.length == 0 || id == null || id.length != 32) continue;
|
||||
long next = estimated + 64L + raw.length;
|
||||
if (!items.isEmpty() && next > cfg.publishMaxBundleBytes()) break;
|
||||
if (next > cfg.publishMaxBundleBytes()) {
|
||||
log.error("One key-rotation DataItem exceeds arweave.blocks.publish.maxBundleBytes: chain={} block={} bytes={}",
|
||||
e.getCandidateBlockchainName(), e.getBlockNumber(), raw.length);
|
||||
continue;
|
||||
}
|
||||
items.add(raw);
|
||||
ids.add(id);
|
||||
estimated = next;
|
||||
}
|
||||
if (items.isEmpty()) return 0;
|
||||
|
||||
byte[] bundle = Ans104Bundle.write(items);
|
||||
List<ArweaveL1Uploader.Tag> rootTags = List.of(
|
||||
new ArweaveL1Uploader.Tag("Content-Type", "application/octet-stream"),
|
||||
new ArweaveL1Uploader.Tag("Bundle-Format", "binary"),
|
||||
new ArweaveL1Uploader.Tag("Bundle-Version", "2.0.0")
|
||||
);
|
||||
ArweaveL1Uploader.UploadResult result = arweaveUploader.upload(bundle, rootTags);
|
||||
candidateBlocksDAO.markArweavePublished(ids, System.currentTimeMillis());
|
||||
log.info("Published {} SHiNE key-rotation DataItems in direct Arweave root tx {} (bundle={} bytes)",
|
||||
items.size(), result.txId(), bundle.length);
|
||||
return items.size();
|
||||
}
|
||||
|
||||
private int publishDirectArweave(List<BlockEntry> candidates) throws Exception {
|
||||
List<byte[]> items = new ArrayList<>();
|
||||
List<byte[]> ids = new ArrayList<>();
|
||||
long estimated = 32;
|
||||
for (BlockEntry e : candidates) {
|
||||
byte[] raw = e.getBlockBytes();
|
||||
byte[] id = e.getDataItemId();
|
||||
if (raw == null || raw.length == 0 || id == null || id.length != 32) continue;
|
||||
long next = estimated + 64L + raw.length;
|
||||
if (!items.isEmpty() && next > cfg.publishMaxBundleBytes()) break;
|
||||
if (next > cfg.publishMaxBundleBytes()) {
|
||||
log.error("One DataItem exceeds arweave.blocks.publish.maxBundleBytes: chain={} block={} bytes={}",
|
||||
e.getBchName(), e.getBlockNumber(), raw.length);
|
||||
continue;
|
||||
}
|
||||
items.add(raw);
|
||||
ids.add(id);
|
||||
estimated = next;
|
||||
}
|
||||
if (items.isEmpty()) return 0;
|
||||
|
||||
byte[] bundle = Ans104Bundle.write(items);
|
||||
List<ArweaveL1Uploader.Tag> rootTags = List.of(
|
||||
new ArweaveL1Uploader.Tag("Content-Type", "application/octet-stream"),
|
||||
new ArweaveL1Uploader.Tag("Bundle-Format", "binary"),
|
||||
new ArweaveL1Uploader.Tag("Bundle-Version", "2.0.0")
|
||||
);
|
||||
ArweaveL1Uploader.UploadResult result = arweaveUploader.upload(bundle, rootTags);
|
||||
blocksDAO.markArweavePublished(ids, System.currentTimeMillis());
|
||||
log.info("Published {} SHiNE test DataItems in direct Arweave root tx {} (bundle={} bytes)",
|
||||
items.size(), result.txId(), bundle.length);
|
||||
return items.size();
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package server.archive;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public final class ArweaveBlockSyncScheduler {
|
||||
private static final Logger log = LoggerFactory.getLogger(ArweaveBlockSyncScheduler.class);
|
||||
private static final AtomicBoolean STARTED = new AtomicBoolean(false);
|
||||
private static ScheduledExecutorService executor;
|
||||
private ArweaveBlockSyncScheduler() {}
|
||||
|
||||
public static void startOrLog() {
|
||||
ArweaveBlocksConfig cfg;
|
||||
try { cfg = ArweaveBlocksConfig.load(); }
|
||||
catch (Exception e) { log.error("Cannot read Arweave block sync config", e); return; }
|
||||
if (!cfg.syncEnabled()) { log.info("Arweave user-block sync disabled"); return; }
|
||||
if (!STARTED.compareAndSet(false,true)) return;
|
||||
try {
|
||||
ArweaveBlockSyncService service = new ArweaveBlockSyncService(cfg);
|
||||
// Один поток намеренно сериализует полный discovery и быстрые retry уже найденных DataItem.
|
||||
executor = Executors.newSingleThreadScheduledExecutor(r -> { Thread t=new Thread(r,"shine-arweave-block-sync");t.setDaemon(true);return t; });
|
||||
Runnable task=()->{try{service.runCycle();}catch(Exception e){log.error("Arweave block sync cycle failed",e);}};
|
||||
Runnable retryTask=()->{try{service.runRetryCycle();}catch(Exception e){log.error("Arweave block fast retry failed",e);}};
|
||||
executor.scheduleWithFixedDelay(task,0,cfg.syncIntervalMinutes(),TimeUnit.MINUTES);
|
||||
executor.scheduleWithFixedDelay(retryTask,cfg.syncLocationRetrySeconds(),cfg.syncLocationRetrySeconds(),TimeUnit.SECONDS);
|
||||
log.info("Arweave user-block sync enabled: interval={}m locationRetry={}s gateway={} turboApi={}",
|
||||
cfg.syncIntervalMinutes(),cfg.syncLocationRetrySeconds(),cfg.syncGateway(),cfg.syncTurboApiBase());
|
||||
} catch (Exception e) { STARTED.set(false); log.error("Arweave user-block sync failed to start",e); }
|
||||
}
|
||||
public static void close(){ScheduledExecutorService e=executor;if(e!=null)e.shutdownNow();}
|
||||
}
|
||||
+791
@@ -0,0 +1,791 @@
|
||||
package server.archive;
|
||||
|
||||
import blockchain.Ans104DataItem;
|
||||
import blockchain.BchBlockEntry;
|
||||
import server.archive.registration.VerifiedFreeRegistrationImporter;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import server.logic.ws_protocol.JSON.handlers.blockchain.Net_AddBlock_Handler;
|
||||
import server.sync.BlockchainResyncGuard;
|
||||
import shine.db.DbController;
|
||||
import utils.config.AppConfig;
|
||||
import shine.db.dao.ArweaveBlockImportDAO;
|
||||
import shine.db.dao.BlockchainStateDAO;
|
||||
import shine.db.dao.BlocksDAO;
|
||||
import shine.db.dao.BlockchainForkSegmentsDAO;
|
||||
import shine.db.dao.CanonicalChainResolver;
|
||||
import shine.db.dao.ChannelReadStateDAO;
|
||||
import shine.db.dao.BlockchainResyncCleanupDAO;
|
||||
import shine.db.dao.SolanaUserPdaCurrentDAO;
|
||||
import shine.db.entities.BlockchainStateEntry;
|
||||
import shine.db.entities.SolanaUserPdaCurrentEntry;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.Duration;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Discovers individual App=test5592 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.
|
||||
*/
|
||||
public final class ArweaveBlockSyncService {
|
||||
private static final Logger log = LoggerFactory.getLogger(ArweaveBlockSyncService.class);
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final Base64.Decoder B64URL = Base64.getUrlDecoder();
|
||||
|
||||
private final ArweaveBlocksConfig cfg;
|
||||
private final HttpClient http = HttpClient.newBuilder()
|
||||
.connectTimeout(Duration.ofSeconds(20))
|
||||
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||
.build();
|
||||
private final VerifiedFreeRegistrationImporter registrationImporter = new VerifiedFreeRegistrationImporter(
|
||||
AppConfig.getInstance().getBoolean("freeRegistration.email.allowReuse", false));
|
||||
private final ArweaveBlockImportDAO importDAO = ArweaveBlockImportDAO.getInstance();
|
||||
private final BlocksDAO blocksDAO = BlocksDAO.getInstance();
|
||||
private final SolanaUserPdaCurrentDAO usersDAO = SolanaUserPdaCurrentDAO.getInstance();
|
||||
private final BlockchainStateDAO stateDAO = BlockchainStateDAO.getInstance();
|
||||
private final Net_AddBlock_Handler addBlock = new Net_AddBlock_Handler();
|
||||
private final BlockchainForkSegmentsDAO segmentsDAO = BlockchainForkSegmentsDAO.getInstance();
|
||||
private final CanonicalChainResolver canonical = CanonicalChainResolver.getInstance();
|
||||
private final BlockchainResyncCleanupDAO cleanupDAO = BlockchainResyncCleanupDAO.getInstance();
|
||||
|
||||
// DataItem уже виден через GraphQL, но его физические координаты внутри ANS-104 bundle
|
||||
// ещё не появились у индексатора. Полный discovery остаётся редким, а эти ID ретраятся часто.
|
||||
private final Map<String, PendingDownload> unresolvedDownloads = new LinkedHashMap<>();
|
||||
|
||||
public ArweaveBlockSyncService(ArweaveBlocksConfig cfg) { this.cfg = cfg; }
|
||||
|
||||
public void runCycle() throws Exception {
|
||||
recoverInterruptedCanonicalRebuilds();
|
||||
discover();
|
||||
discoverFreeRegistrations();
|
||||
drainQueue();
|
||||
}
|
||||
|
||||
/** Быстрый retry только для уже обнаруженных DataItem, без повторного полного GraphQL discovery. */
|
||||
public void runRetryCycle() throws Exception {
|
||||
int queued = retryPendingDownloads();
|
||||
if (queued > 0) drainQueue();
|
||||
}
|
||||
|
||||
private void discover() throws Exception {
|
||||
long stored = importDAO.getLastBlockHeight();
|
||||
long minHeight = Math.max(cfg.syncStartBlockHeight(), stored);
|
||||
String cursor = null;
|
||||
long maxHeight = minHeight;
|
||||
long lowestRetryHeight = Long.MAX_VALUE;
|
||||
int discovered = 0;
|
||||
|
||||
do {
|
||||
JsonNode response = graphQlPage(minHeight, cursor);
|
||||
JsonNode txs = response.path("data").path("transactions");
|
||||
if (response.has("errors")) throw new IOException("Arweave GraphQL errors: " + response.path("errors"));
|
||||
JsonNode edges = txs.path("edges");
|
||||
if (!edges.isArray()) throw new IOException("Arweave GraphQL returned no transactions.edges");
|
||||
|
||||
String nextCursor = null;
|
||||
for (JsonNode edge : edges) {
|
||||
nextCursor = edge.path("cursor").asText(null);
|
||||
JsonNode node = edge.path("node");
|
||||
String dataItemId = node.path("id").asText("").trim();
|
||||
long height = node.path("block").path("height").asLong(-1L);
|
||||
if (dataItemId.isBlank() || height < 0) continue;
|
||||
maxHeight = Math.max(maxHeight, height);
|
||||
|
||||
byte[] id32;
|
||||
try {
|
||||
id32 = B64URL.decode(dataItemId);
|
||||
if (id32.length != 32) throw new IllegalArgumentException("id length=" + id32.length);
|
||||
} catch (Exception e) {
|
||||
log.warn("Ignoring malformed Arweave DataItem id {}: {}", dataItemId, e.getMessage());
|
||||
continue;
|
||||
}
|
||||
if (blocksDAO.existsByDataItemId(id32) || importDAO.exists(id32)) {
|
||||
unresolvedDownloads.remove(dataItemId);
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
DownloadedDataItem downloaded = downloadSignedDataItem(dataItemId);
|
||||
if (isRegistrationItem(downloaded.rawDataItem())) {
|
||||
registrationImporter.importSigned(downloaded.rawDataItem(),
|
||||
VerifiedFreeRegistrationImporter.Provenance.ARWEAVE_CONFIRMED);
|
||||
unresolvedDownloads.remove(dataItemId);
|
||||
continue; // Не парсить регистрацию как BchBlockEntry.
|
||||
}
|
||||
unresolvedDownloads.remove(dataItemId);
|
||||
if (importDAO.enqueueIfMissing(id32, downloaded.rootTxId(), height, downloaded.rawDataItem(), System.currentTimeMillis())) discovered++;
|
||||
} catch (RetryableDataItemDownloadException e) {
|
||||
lowestRetryHeight = Math.min(lowestRetryHeight, height);
|
||||
unresolvedDownloads.put(dataItemId, new PendingDownload(dataItemId, id32, height));
|
||||
log.info("Signed DataItem {} visible at height {} but raw ANS-104 bytes are not ready yet; queued for fast retry: {}",
|
||||
dataItemId, height, e.getMessage());
|
||||
} catch (Exception e) {
|
||||
lowestRetryHeight = Math.min(lowestRetryHeight, height);
|
||||
log.warn("Cannot retrieve signed DataItem {} at height {} yet: {}", dataItemId, height, e.getMessage());
|
||||
}
|
||||
}
|
||||
boolean hasNext = txs.path("pageInfo").path("hasNextPage").asBoolean(false);
|
||||
cursor = hasNext ? nextCursor : null;
|
||||
if (hasNext && (cursor == null || cursor.isBlank())) throw new IOException("GraphQL hasNextPage without cursor");
|
||||
} while (cursor != null);
|
||||
|
||||
// Keep inclusive overlap. If a gateway has indexed GraphQL before offsets, do not advance past that item.
|
||||
long checkpoint = lowestRetryHeight == Long.MAX_VALUE ? maxHeight : Math.min(maxHeight, lowestRetryHeight);
|
||||
importDAO.setLastBlockHeight(checkpoint, System.currentTimeMillis());
|
||||
if (discovered > 0) {
|
||||
log.info("Arweave discovery queued {} new SHiNE test DataItems; checkpoint={}", discovered, checkpoint);
|
||||
}
|
||||
}
|
||||
|
||||
private int retryPendingDownloads() throws Exception {
|
||||
if (unresolvedDownloads.isEmpty()) return 0;
|
||||
|
||||
int queued = 0;
|
||||
int attempted = 0;
|
||||
List<PendingDownload> snapshot = new ArrayList<>(unresolvedDownloads.values());
|
||||
for (PendingDownload pending : snapshot) {
|
||||
if (attempted >= cfg.syncPageSize()) break;
|
||||
attempted++;
|
||||
|
||||
if (blocksDAO.existsByDataItemId(pending.id32()) || importDAO.exists(pending.id32())) {
|
||||
unresolvedDownloads.remove(pending.dataItemId());
|
||||
continue;
|
||||
}
|
||||
|
||||
DownloadedDataItem downloaded;
|
||||
try {
|
||||
downloaded = downloadSignedDataItem(pending.dataItemId());
|
||||
} catch (RetryableDataItemDownloadException e) {
|
||||
// Переносим в конец LinkedHashMap, чтобы первые 100 зависших ID не блокировали retry следующих.
|
||||
unresolvedDownloads.remove(pending.dataItemId());
|
||||
unresolvedDownloads.put(pending.dataItemId(), pending);
|
||||
log.debug("ANS-104 DataItem {} still waits for location/raw bytes: {}", pending.dataItemId(), e.getMessage());
|
||||
continue;
|
||||
} catch (Exception e) {
|
||||
// Не долбим явно плохой ответ каждые 30 секунд. Полный discovery попробует его снова позднее.
|
||||
unresolvedDownloads.remove(pending.dataItemId());
|
||||
log.warn("Fast retry stopped for signed DataItem {} until next discovery: {}", pending.dataItemId(), e.getMessage());
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isRegistrationItem(downloaded.rawDataItem())) {
|
||||
try {
|
||||
registrationImporter.importSigned(downloaded.rawDataItem(),
|
||||
VerifiedFreeRegistrationImporter.Provenance.ARWEAVE_CONFIRMED);
|
||||
unresolvedDownloads.remove(pending.dataItemId());
|
||||
} catch (Exception e) {
|
||||
unresolvedDownloads.remove(pending.dataItemId());
|
||||
unresolvedDownloads.put(pending.dataItemId(), pending);
|
||||
log.debug("Registration {} awaits namespace/DB sync or retry: {}", pending.dataItemId(), e.getMessage());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (importDAO.enqueueIfMissing(pending.id32(), downloaded.rootTxId(), pending.height(),
|
||||
downloaded.rawDataItem(), System.currentTimeMillis())) {
|
||||
queued++;
|
||||
}
|
||||
unresolvedDownloads.remove(pending.dataItemId());
|
||||
}
|
||||
|
||||
if (queued > 0) {
|
||||
log.info("Fast Arweave retry queued {} signed DataItems; {} still unresolved", queued, unresolvedDownloads.size());
|
||||
}
|
||||
return queued;
|
||||
}
|
||||
|
||||
private void drainQueue() throws Exception {
|
||||
int passes = 0;
|
||||
int importedTotal = 0;
|
||||
boolean progress;
|
||||
Set<String> replayedLogins = new HashSet<>();
|
||||
do {
|
||||
progress = false;
|
||||
|
||||
// Phase 1 is global: validate every pending DataItem and discover all signed segment
|
||||
// ancestry before any canonical/orphan decision is made.
|
||||
prepareAllSegmentMetadata();
|
||||
|
||||
// Phase 2 is deterministic logical replay order, never Arweave height/order.
|
||||
List<ArweaveBlockImportDAO.QueueItem> pending = importDAO.listPendingValidated(cfg.syncQueueBatchSize());
|
||||
if (pending.isEmpty()) break;
|
||||
for (ArweaveBlockImportDAO.QueueItem q : pending) {
|
||||
if (q.parsedLogin() == null || q.parsedForkNumber() == null || q.parsedBlockNumber() == null) continue;
|
||||
SolanaUserPdaCurrentEntry activeUser = usersDAO.getByLogin(q.parsedLogin());
|
||||
if (activeUser == null || activeUser.getBlockchainName() == null || activeUser.getBlockchainName().isBlank()) {
|
||||
importDAO.setPendingError(q.dataItemId(), "active_user_missing", System.currentTimeMillis());
|
||||
continue;
|
||||
}
|
||||
int activeFork = forkOf(activeUser.getBlockchainName());
|
||||
if (!isAncestryComplete(q.parsedLogin(), activeFork)) {
|
||||
importDAO.setPendingError(q.dataItemId(), "ancestry_incomplete", System.currentTimeMillis());
|
||||
continue;
|
||||
}
|
||||
|
||||
int canonicalFork;
|
||||
try (Connection cc = DbController.getInstance().getConnection()) {
|
||||
canonicalFork = canonical.resolveFork(cc, q.parsedLogin(), activeFork, q.parsedBlockNumber());
|
||||
}
|
||||
if (canonicalFork != q.parsedForkNumber()) {
|
||||
importDAO.reject(q.dataItemId(), "orphan_noncanonical", System.currentTimeMillis());
|
||||
progress = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
Ans104DataItem item;
|
||||
BchBlockEntry parsedBlock;
|
||||
try {
|
||||
item = new Ans104DataItem(q.rawDataItem());
|
||||
parsedBlock = new BchBlockEntry(q.rawDataItem());
|
||||
} catch (Exception e) {
|
||||
// Should be impossible after metadata validation, but never trust persisted bytes blindly.
|
||||
importDAO.reject(q.dataItemId(), "validated_item_reparse_failed: " + e.getMessage(), System.currentTimeMillis());
|
||||
progress = true;
|
||||
continue;
|
||||
}
|
||||
SolanaUserPdaCurrentEntry physicalUser = usersDAO.getByBlockchainKey(item.owner32());
|
||||
if (physicalUser == null || physicalUser.getBlockchainName() == null
|
||||
|| !q.parsedLogin().equalsIgnoreCase(physicalUser.getLogin())
|
||||
|| forkOf(physicalUser.getBlockchainName()) != q.parsedForkNumber()) {
|
||||
importDAO.reject(q.dataItemId(), "validated_owner_mapping_changed", System.currentTimeMillis());
|
||||
progress = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
ensureState(physicalUser, parsedBlock);
|
||||
Net_AddBlock_Handler.ArweaveImportResult result = addBlock.addBlockFromReplay(
|
||||
physicalUser.getBlockchainName(), q.rawDataItem());
|
||||
if (result.ok()) {
|
||||
importDAO.delete(q.dataItemId());
|
||||
importedTotal++;
|
||||
replayedLogins.add(q.parsedLogin());
|
||||
progress = true;
|
||||
rebuildIfLocalOrphansExist(q.parsedLogin(), activeFork);
|
||||
continue;
|
||||
}
|
||||
String reason = result.reasonCode();
|
||||
if ("bad_block_number".equals(reason) || "bad_prev_hash".equals(reason)
|
||||
|| "prev_line_block_not_found".equals(reason) || "channel_not_found".equals(reason)
|
||||
|| "chain_resync_in_progress".equals(reason)) {
|
||||
importDAO.setPendingError(q.dataItemId(), reason, System.currentTimeMillis());
|
||||
} else {
|
||||
importDAO.reject(q.dataItemId(), reason, System.currentTimeMillis());
|
||||
progress = true;
|
||||
}
|
||||
}
|
||||
passes++;
|
||||
} while (progress && passes < 1000);
|
||||
|
||||
// Replay deliberately suppresses live unread/push side effects. Recompute the server-owned
|
||||
// unread projection from final canonical channel contents and preserved signed read_count.
|
||||
if (!replayedLogins.isEmpty()) {
|
||||
try (Connection c = DbController.getInstance().getConnection()) {
|
||||
boolean ac = c.getAutoCommit(); c.setAutoCommit(false);
|
||||
try {
|
||||
for (String login : replayedLogins) {
|
||||
ChannelReadStateDAO.getInstance().recalculateUnreadForOwnedChannels(c, login, System.currentTimeMillis());
|
||||
}
|
||||
c.commit();
|
||||
} catch (Exception e) {
|
||||
c.rollback(); throw e;
|
||||
} finally { c.setAutoCommit(ac); }
|
||||
}
|
||||
}
|
||||
if (importedTotal > 0) log.info("Imported {} SHiNE test DataItems from Arweave", importedTotal);
|
||||
}
|
||||
|
||||
/**
|
||||
* Phase 1 of Arweave recovery: scan the complete persistent PENDING queue and register only
|
||||
* immutable segment ancestry metadata. No block is applied or rejected here.
|
||||
*/
|
||||
private void prepareAllSegmentMetadata() throws Exception {
|
||||
String sql = "SELECT data_item_id,raw_data_item FROM arweave_block_import_queue WHERE status=? AND metadata_validated=FALSE ORDER BY block_height DESC, first_seen_at_ms DESC";
|
||||
try (Connection c = DbController.getInstance().getConnection();
|
||||
PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
ps.setString(1, ArweaveBlockImportDAO.STATUS_PENDING);
|
||||
ps.setFetchSize(Math.max(100, cfg.syncQueueBatchSize()));
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
byte[] id = rs.getBytes(1);
|
||||
byte[] raw = rs.getBytes(2);
|
||||
if (raw == null || raw.length == 0) {
|
||||
importDAO.reject(id, "empty_data_item", System.currentTimeMillis());
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
// Metadata is trusted only after complete ANS-104 validation.
|
||||
Ans104DataItem item = new Ans104DataItem(raw);
|
||||
if (!Arrays.equals(item.id32(), id)) throw new IllegalArgumentException("data_item_id mismatch");
|
||||
if (!item.hasTag(ArweaveBlocksConfig.TEST_TAG_NAME, ArweaveBlocksConfig.TEST_TAG_VALUE))
|
||||
throw new IllegalArgumentException("bad App tag");
|
||||
if (!item.verifySignature()) throw new IllegalArgumentException("bad ANS-104 signature");
|
||||
BchBlockEntry b = new BchBlockEntry(raw);
|
||||
|
||||
SolanaUserPdaCurrentEntry owner = usersDAO.getByBlockchainKey(item.owner32());
|
||||
if (owner == null || owner.getBlockchainName() == null || owner.getLogin() == null) {
|
||||
importDAO.setPendingError(id, "unknown_owner", System.currentTimeMillis());
|
||||
continue;
|
||||
}
|
||||
int fork = forkOf(owner.getBlockchainName());
|
||||
|
||||
// Only signed structural anchor blocks can create/modify ancestry metadata.
|
||||
if (fork == 1 && b.blockNumber == 0) {
|
||||
if (!(b.body instanceof blockchain.body.HeaderBody))
|
||||
throw new IllegalArgumentException("fork1 block #0 is not HEADER");
|
||||
segmentsDAO.upsert(c, new BlockchainForkSegmentsDAO.Segment(
|
||||
owner.getLogin(), 1, null, -1, null, 0, System.currentTimeMillis()));
|
||||
} else if (b.body instanceof blockchain.body.ForkBody fb) {
|
||||
if (fork <= 1) throw new IllegalArgumentException("TECH_FORK cannot belong to fork1");
|
||||
if (b.blockNumber != fb.forkPointBlockNumber + 1)
|
||||
throw new IllegalArgumentException("TECH_FORK block number/fork point mismatch");
|
||||
SolanaUserPdaCurrentEntry parent = usersDAO.getByBlockchainKey(fb.parentBlockchainKey32);
|
||||
if (parent == null || parent.getBlockchainName() == null
|
||||
|| !owner.getLogin().equalsIgnoreCase(parent.getLogin()))
|
||||
throw new IllegalArgumentException("TECH_FORK parent owner mismatch");
|
||||
int parentFork = forkOf(parent.getBlockchainName());
|
||||
if (parentFork >= fork) throw new IllegalArgumentException("TECH_FORK parent fork is not older");
|
||||
segmentsDAO.upsert(c, new BlockchainForkSegmentsDAO.Segment(
|
||||
owner.getLogin(), fork, parentFork,
|
||||
fb.forkPointBlockNumber, fb.forkPointBlockHash32,
|
||||
fb.forkPointBlockNumber + 1, System.currentTimeMillis()));
|
||||
}
|
||||
|
||||
importDAO.markMetadataValidated(id, owner.getLogin(), fork, b.blockNumber, b.getHash32(), System.currentTimeMillis());
|
||||
} catch (Exception e) {
|
||||
importDAO.reject(id, "invalid_data_item: " + e.getMessage(), System.currentTimeMillis());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** True only when rotation ancestry is known all the way back to fork1. */
|
||||
private boolean isAncestryComplete(String login, int activeFork) throws Exception {
|
||||
int f = activeFork;
|
||||
Set<Integer> seen = new HashSet<>();
|
||||
while (true) {
|
||||
if (f < 1 || !seen.add(f)) return false;
|
||||
BlockchainForkSegmentsDAO.Segment seg = segmentsDAO.get(login, f);
|
||||
if (seg == null) return false;
|
||||
if (seg.parentForkNumber() == null) {
|
||||
return f == 1 && seg.firstPhysicalBlockNumber() == 0;
|
||||
}
|
||||
f = seg.parentForkNumber();
|
||||
}
|
||||
}
|
||||
|
||||
private void ensureState(SolanaUserPdaCurrentEntry user, BchBlockEntry firstSeen) throws Exception {
|
||||
int fork=forkOf(user.getBlockchainName()); BlockchainStateEntry s=new BlockchainStateEntry();
|
||||
s.setBlockchainName(user.getBlockchainName());s.setLogin(user.getLogin());s.setBlockchainKey(user.getBlockchainKey());s.setSizeLimit(user.getPaidLimitBytes()>0?user.getPaidLimitBytes():100_000L);s.setFileSizeBytes(0L);
|
||||
BlockchainForkSegmentsDAO.Segment seg=segmentsDAO.get(user.getLogin(),fork);
|
||||
BlockchainStateEntry existing=stateDAO.getByBlockchainName(user.getBlockchainName());
|
||||
if(existing!=null && (seg==null || seg.parentForkNumber()==null || existing.getLastBlockNumber()>=seg.forkPointBlockNumber()))return;
|
||||
if(seg!=null&&seg.parentForkNumber()!=null){
|
||||
s.setLastBlockNumber(seg.forkPointBlockNumber());s.setLastBlockHash(seg.forkPointBlockHash());
|
||||
try(var c=shine.db.DbController.getInstance().getConnection();var ps=c.prepareStatement("SELECT COALESCE(SUM(octet_length(block_bytes)),0) FROM blocks_store WHERE LOWER(login)=LOWER(?) AND block_number<=? AND fork_number=shine_canonical_fork(?,?,block_number)")){
|
||||
ps.setString(1,user.getLogin());ps.setInt(2,seg.forkPointBlockNumber());ps.setString(3,user.getLogin());ps.setInt(4,seg.parentForkNumber());try(var rs=ps.executeQuery()){if(rs.next())s.setFileSizeBytes(rs.getLong(1));}
|
||||
}
|
||||
} else{s.setLastBlockNumber(-1);s.setLastBlockHash(null);}
|
||||
s.setUpdatedAtMs(System.currentTimeMillis());stateDAO.insertIfMissing(s);
|
||||
if(existing!=null)stateDAO.upsert(s);
|
||||
}
|
||||
private static int forkOf(String n){int d=n.lastIndexOf('-');return Integer.parseInt(n.substring(d+1));}
|
||||
|
||||
/** Resume a durable canonical rebuild that was interrupted after staging/cleanup/replay. */
|
||||
private void recoverInterruptedCanonicalRebuilds() throws Exception {
|
||||
List<String> logins=new ArrayList<>();
|
||||
try(Connection c=DbController.getInstance().getConnection();
|
||||
PreparedStatement ps=c.prepareStatement("SELECT DISTINCT login FROM canonical_rebuild_staging ORDER BY login");
|
||||
ResultSet rs=ps.executeQuery()){
|
||||
while(rs.next())logins.add(rs.getString(1));
|
||||
}
|
||||
for(String login:logins){
|
||||
SolanaUserPdaCurrentEntry u=usersDAO.getByLogin(login);
|
||||
if(u==null||u.getBlockchainName()==null)continue;
|
||||
replayDurableCanonicalStaging(login,forkOf(u.getBlockchainName()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A server may learn a deep fork only through Arweave after already materializing the old branch.
|
||||
* If any local raw block is non-canonical under the current PDA fork, stage the canonical raw set
|
||||
* and rebuild projections exactly like local key rotation does.
|
||||
*/
|
||||
private void rebuildIfLocalOrphansExist(String login,int activeFork)throws Exception{
|
||||
if(!hasNonCanonicalLocalBlocks(login,activeFork))return;
|
||||
if(!hasGenericStaging(login))stageLocalCanonicalBlocks(login,activeFork);
|
||||
validateGenericStaging(login);
|
||||
replayDurableCanonicalStaging(login,activeFork);
|
||||
}
|
||||
|
||||
private boolean hasNonCanonicalLocalBlocks(String login,int activeFork)throws SQLException{
|
||||
String sql="SELECT 1 FROM blocks_store b WHERE LOWER(b.login)=LOWER(?) AND b.fork_number<>shine_canonical_fork(?,?,b.block_number) LIMIT 1";
|
||||
try(Connection c=DbController.getInstance().getConnection();PreparedStatement ps=c.prepareStatement(sql)){
|
||||
ps.setString(1,login);ps.setString(2,login);ps.setInt(3,activeFork);try(ResultSet rs=ps.executeQuery()){return rs.next();}
|
||||
}
|
||||
}
|
||||
private boolean hasGenericStaging(String login)throws SQLException{
|
||||
try(Connection c=DbController.getInstance().getConnection();PreparedStatement ps=c.prepareStatement("SELECT 1 FROM canonical_rebuild_staging WHERE LOWER(login)=LOWER(?) LIMIT 1")){
|
||||
ps.setString(1,login);try(ResultSet rs=ps.executeQuery()){return rs.next();}
|
||||
}
|
||||
}
|
||||
private void stageLocalCanonicalBlocks(String login,int activeFork)throws Exception{
|
||||
try(Connection c=DbController.getInstance().getConnection()){boolean ac=c.getAutoCommit();c.setAutoCommit(false);try{
|
||||
try(PreparedStatement d=c.prepareStatement("DELETE FROM canonical_rebuild_staging WHERE LOWER(login)=LOWER(?)")){d.setString(1,login);d.executeUpdate();}
|
||||
String q="""
|
||||
SELECT block_number,fork_number,block_bytes,block_hash
|
||||
FROM blocks_store b
|
||||
WHERE LOWER(login)=LOWER(?) AND fork_number=shine_canonical_fork(?,?,block_number)
|
||||
ORDER BY block_number ASC
|
||||
""";
|
||||
String ins="INSERT INTO canonical_rebuild_staging(login,logical_block_number,physical_fork_number,block_bytes,block_hash) VALUES(?,?,?,?,?)";
|
||||
try(PreparedStatement ps=c.prepareStatement(q);PreparedStatement out=c.prepareStatement(ins)){
|
||||
ps.setString(1,login);ps.setString(2,login);ps.setInt(3,activeFork);
|
||||
try(ResultSet rs=ps.executeQuery()){while(rs.next()){
|
||||
out.setString(1,login);out.setInt(2,rs.getInt(1));out.setInt(3,rs.getInt(2));out.setBytes(4,rs.getBytes(3));out.setBytes(5,rs.getBytes(4));out.addBatch();
|
||||
}}out.executeBatch();
|
||||
}
|
||||
c.commit();
|
||||
}catch(Exception e){c.rollback();throw e;}finally{c.setAutoCommit(ac);}}
|
||||
}
|
||||
private void validateGenericStaging(String login)throws Exception{
|
||||
int expected=0,last=-1;byte[] previous=null;
|
||||
try(Connection c=DbController.getInstance().getConnection();PreparedStatement ps=c.prepareStatement("SELECT logical_block_number,block_bytes,block_hash FROM canonical_rebuild_staging WHERE LOWER(login)=LOWER(?) ORDER BY logical_block_number ASC")){
|
||||
ps.setString(1,login);try(ResultSet rs=ps.executeQuery()){while(rs.next()){
|
||||
int n=rs.getInt(1);byte[] raw=rs.getBytes(2),hash=rs.getBytes(3);if(n!=expected)throw new IllegalStateException("canonical staging gap for "+login+": expected #"+expected+" got #"+n);
|
||||
BchBlockEntry b=new BchBlockEntry(raw);if(b.blockNumber!=n)throw new IllegalStateException("canonical staging block mismatch #"+n);
|
||||
if(n==0&&!(b.body instanceof blockchain.body.HeaderBody))throw new IllegalStateException("canonical staging must start HEADER #0");
|
||||
if(n>0&&(previous==null||!Arrays.equals(b.prevHash32,previous)))throw new IllegalStateException("canonical staging prevHash mismatch #"+n);
|
||||
if(hash==null||hash.length!=32||!Arrays.equals(hash,b.getHash32()))throw new IllegalStateException("canonical staging hash mismatch #"+n);
|
||||
previous=hash;last=n;expected++;
|
||||
}}
|
||||
}
|
||||
if(last<0)throw new IllegalStateException("canonical staging is empty for "+login);
|
||||
}
|
||||
private void replayDurableCanonicalStaging(String login,int activeFork)throws Exception{
|
||||
SolanaUserPdaCurrentEntry u=usersDAO.getByLogin(login);if(u==null)throw new IllegalStateException("user missing for canonical rebuild: "+login);
|
||||
String activeName=u.getBlockchainName();boolean guard=BlockchainResyncGuard.tryBegin(activeName);if(!guard)throw new IllegalStateException("canonical rebuild already active: "+login);
|
||||
try{
|
||||
validateGenericStaging(login);
|
||||
cleanupDAO.cleanupLoginForCanonicalReplay(login);
|
||||
long limit=u.getPaidLimitBytes()>0?u.getPaidLimitBytes():100_000L,cumulative=0;int currentFork=-1,prevNumber=-1;byte[] prevHash=null;
|
||||
try(Connection c=DbController.getInstance().getConnection();PreparedStatement ps=c.prepareStatement("SELECT logical_block_number,physical_fork_number,block_bytes,block_hash FROM canonical_rebuild_staging WHERE LOWER(login)=LOWER(?) ORDER BY logical_block_number ASC")){
|
||||
ps.setString(1,login);try(ResultSet rs=ps.executeQuery()){while(rs.next()){
|
||||
int n=rs.getInt(1),f=rs.getInt(2);byte[] raw=rs.getBytes(3),hash=rs.getBytes(4);BchBlockEntry b=new BchBlockEntry(raw);
|
||||
String bch=login+"-"+String.format("%03d",f);
|
||||
if(f!=currentFork){BlockchainStateEntry st=new BlockchainStateEntry();st.setBlockchainName(bch);st.setLogin(login);st.setBlockchainKey(Base64.getEncoder().encodeToString(b.getOwner32()));st.setSizeLimit(limit);st.setFileSizeBytes(cumulative);st.setLastBlockNumber(prevNumber);st.setLastBlockHash(prevHash);st.setUpdatedAtMs(System.currentTimeMillis());stateDAO.insertIfMissing(st);currentFork=f;}
|
||||
Net_AddBlock_Handler.ArweaveImportResult r=addBlock.addBlockFromReplay(bch,raw);if(!r.ok())throw new IllegalStateException("canonical Arweave rebuild rejected #"+n+": "+r.reasonCode());
|
||||
cumulative+=raw.length;prevNumber=n;prevHash=hash;
|
||||
}}
|
||||
}
|
||||
cleanupDAO.finalizeCanonicalReplay(login);
|
||||
try(Connection c=DbController.getInstance().getConnection();PreparedStatement ps=c.prepareStatement("DELETE FROM canonical_rebuild_staging WHERE LOWER(login)=LOWER(?)")){ps.setString(1,login);ps.executeUpdate();}
|
||||
}finally{BlockchainResyncGuard.end(activeName);}
|
||||
}
|
||||
|
||||
|
||||
/** Независимый GraphQL курсор для t=reg: не смешивается с блоковой очередью. */
|
||||
private void discoverFreeRegistrations() throws Exception {
|
||||
long stored;
|
||||
try (Connection c = DbController.getInstance().getConnection();
|
||||
PreparedStatement ps = c.prepareStatement(
|
||||
"SELECT last_block_height FROM free_registration_arweave_sync_state WHERE id=1");
|
||||
ResultSet rs = ps.executeQuery()) {
|
||||
stored = rs.next() ? rs.getLong(1) : 0;
|
||||
}
|
||||
long minHeight = Math.max(cfg.syncStartBlockHeight(), stored);
|
||||
long high = minHeight;
|
||||
long retryFrom = Long.MAX_VALUE;
|
||||
String cursor = null;
|
||||
int count = 0;
|
||||
do {
|
||||
JsonNode response = graphQlPage(minHeight, cursor, true);
|
||||
if (response.has("errors")) throw new IOException("Registration GraphQL errors: " + response.path("errors"));
|
||||
JsonNode txs = response.path("data").path("transactions");
|
||||
JsonNode edges = txs.path("edges");
|
||||
if (!edges.isArray()) throw new IOException("Registration GraphQL returned no edges");
|
||||
String next = null;
|
||||
for (JsonNode edge : edges) {
|
||||
next = edge.path("cursor").asText(null);
|
||||
JsonNode node = edge.path("node");
|
||||
String id = node.path("id").asText("").trim();
|
||||
long height = node.path("block").path("height").asLong(-1);
|
||||
if (id.isBlank() || height < 0) continue; // мгновенный Turbo-импорт не зависит от height
|
||||
high = Math.max(high, height);
|
||||
try {
|
||||
byte[] idBytes = B64URL.decode(id);
|
||||
if (idBytes.length != 32) throw new IllegalArgumentException("Malformed id");
|
||||
DownloadedDataItem item = downloadSignedDataItem(id);
|
||||
if (!isRegistrationItem(item.rawDataItem()))
|
||||
throw new IllegalArgumentException("GraphQL registration tag is not present in signed bytes");
|
||||
registrationImporter.importSigned(item.rawDataItem(),
|
||||
VerifiedFreeRegistrationImporter.Provenance.ARWEAVE_CONFIRMED);
|
||||
count++;
|
||||
} catch (RetryableDataItemDownloadException e) {
|
||||
retryFrom = Math.min(retryFrom, height);
|
||||
log.debug("Registration DataItem {} is not downloadable yet", id);
|
||||
} catch (IllegalArgumentException e) {
|
||||
log.warn("Rejecting invalid registration DataItem {}: {}", id, e.getMessage());
|
||||
} catch (Exception e) {
|
||||
retryFrom = Math.min(retryFrom, height);
|
||||
log.warn("Registration DataItem {} import deferred: {}", id, e.getMessage());
|
||||
}
|
||||
}
|
||||
cursor = txs.path("pageInfo").path("hasNextPage").asBoolean(false) ? next : null;
|
||||
if (txs.path("pageInfo").path("hasNextPage").asBoolean(false) && (cursor == null || cursor.isBlank()))
|
||||
throw new IOException("Registration GraphQL cursor missing");
|
||||
} while (cursor != null);
|
||||
long checkpoint = retryFrom == Long.MAX_VALUE ? high : Math.min(high, retryFrom);
|
||||
try (Connection c = DbController.getInstance().getConnection();
|
||||
PreparedStatement ps = c.prepareStatement("""
|
||||
UPDATE free_registration_arweave_sync_state
|
||||
SET last_block_height=?, updated_at_ms=? WHERE id=1
|
||||
""")) {
|
||||
ps.setLong(1, checkpoint); ps.setLong(2, System.currentTimeMillis()); ps.executeUpdate();
|
||||
}
|
||||
if (count > 0) log.info("Registration discovery verified {} DataItems; checkpoint={}", count, checkpoint);
|
||||
}
|
||||
|
||||
private static boolean isRegistrationItem(byte[] raw) {
|
||||
Ans104DataItem item = new Ans104DataItem(raw);
|
||||
return item.tags().contains(new Ans104DataItem.Tag("t", "reg"));
|
||||
}
|
||||
|
||||
private JsonNode graphQlPage(long minHeight, String cursor) throws Exception {
|
||||
return graphQlPage(minHeight, cursor, false);
|
||||
}
|
||||
|
||||
private JsonNode graphQlPage(long minHeight, String cursor, boolean registrationsOnly) throws Exception {
|
||||
String after = cursor == null ? "null" : "\"" + escapeGraphQl(cursor) + "\"";
|
||||
String tagFilter = "{name:\"" + ArweaveBlocksConfig.TEST_TAG_NAME + "\",values:[\""
|
||||
+ ArweaveBlocksConfig.TEST_TAG_VALUE + "\"]}" +
|
||||
(registrationsOnly ? ",{name:\"t\",values:[\"reg\"]}" : "");
|
||||
String query = "query { transactions(tags:[" + tagFilter + "], block:{min:" + minHeight + "}, first:"
|
||||
+ cfg.syncPageSize() + ", after:" + after
|
||||
+ ", sort:HEIGHT_ASC) { pageInfo { hasNextPage } edges { cursor node { id block { height } } } } }";
|
||||
String body = MAPPER.writeValueAsString(Map.of("query", query));
|
||||
HttpRequest req = HttpRequest.newBuilder(URI.create(trim(cfg.syncGateway()) + "/graphql"))
|
||||
.timeout(Duration.ofSeconds(60)).header("Content-Type","application/json").header("Accept","application/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(body, StandardCharsets.UTF_8)).build();
|
||||
HttpResponse<String> resp = http.send(req, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
||||
if (resp.statusCode() < 200 || resp.statusCode() >= 300) throw new IOException("GraphQL HTTP " + resp.statusCode());
|
||||
return MAPPER.readTree(resp.body());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gateway по /{dataItemId} обычно отдаёт только payload. Для импорта SHiNE нужен полный
|
||||
* подписанный ANS-104 DataItem, поэтому сначала резолвим rootTx + offset + size, а затем
|
||||
* читаем точный range из root L1 transaction.
|
||||
*/
|
||||
private DownloadedDataItem downloadSignedDataItem(String dataItemId) throws Exception {
|
||||
DataItemLocation location = resolveDataItemLocation(dataItemId);
|
||||
long endInclusive = location.rootOffset() + location.size() - 1;
|
||||
|
||||
HttpRequest req = HttpRequest.newBuilder(URI.create(trim(cfg.syncGateway()) + "/raw/" + location.rootTxId()))
|
||||
.timeout(Duration.ofMinutes(2))
|
||||
.header("Range", "bytes=" + location.rootOffset() + "-" + endInclusive)
|
||||
.header("Accept", "application/octet-stream")
|
||||
.GET().build();
|
||||
HttpResponse<InputStream> resp = http.send(req, HttpResponse.BodyHandlers.ofInputStream());
|
||||
try (InputStream in = resp.body()) {
|
||||
if (resp.statusCode() != 206) {
|
||||
if (isRetryableHttpStatus(resp.statusCode())) {
|
||||
throw new RetryableDataItemDownloadException(
|
||||
"root range is temporarily unavailable via " + location.source() + ": HTTP " + resp.statusCode());
|
||||
}
|
||||
throw new IOException("Gateway ignored root range for DataItem " + dataItemId + ": HTTP " + resp.statusCode());
|
||||
}
|
||||
byte[] bytes = readExactlyBounded(in, Math.toIntExact(location.size()));
|
||||
if (bytes.length != location.size()) {
|
||||
throw new RetryableDataItemDownloadException(
|
||||
"truncated DataItem range: expected=" + location.size() + " got=" + bytes.length);
|
||||
}
|
||||
Ans104DataItem parsed = new Ans104DataItem(bytes);
|
||||
byte[] expectedId = B64URL.decode(dataItemId);
|
||||
if (!Arrays.equals(parsed.id32(), expectedId)) {
|
||||
throw new IOException("Range returned another ANS-104 DataItem for " + dataItemId);
|
||||
}
|
||||
return new DownloadedDataItem(location.rootTxId(), bytes);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Координаты считаются только подсказкой транспорта: источник может ошибиться, поэтому после
|
||||
* range-read всё равно обязательно сверяется настоящий ANS-104 DataItem ID и затем подпись.
|
||||
*/
|
||||
private DataItemLocation resolveDataItemLocation(String dataItemId) throws Exception {
|
||||
List<String> misses = new ArrayList<>(3);
|
||||
|
||||
try {
|
||||
DataItemLocation location = getArIoIndexedLocation(dataItemId);
|
||||
if (location != null) return location;
|
||||
misses.add("ar-io=404");
|
||||
} catch (Exception e) {
|
||||
misses.add("ar-io=" + shortError(e));
|
||||
}
|
||||
|
||||
try {
|
||||
DataItemLocation location = getTurboLocation(dataItemId);
|
||||
if (location != null) return location;
|
||||
misses.add("turbo=404");
|
||||
} catch (Exception e) {
|
||||
misses.add("turbo=" + shortError(e));
|
||||
}
|
||||
|
||||
try {
|
||||
DataItemLocation location = getHeadLocation(dataItemId);
|
||||
if (location != null) return location;
|
||||
misses.add("head=no-location");
|
||||
} catch (Exception e) {
|
||||
misses.add("head=" + shortError(e));
|
||||
}
|
||||
|
||||
throw new RetryableDataItemDownloadException(
|
||||
"ANS-104 location not available yet (" + String.join(", ", misses) + ")");
|
||||
}
|
||||
|
||||
/** Самый дешёвый путь: локальный индекс AR.IO gateway. */
|
||||
private DataItemLocation getArIoIndexedLocation(String dataItemId) throws Exception {
|
||||
HttpRequest req = HttpRequest.newBuilder(URI.create(trim(cfg.syncGateway()) + "/ar-io/offsets/" + dataItemId))
|
||||
.timeout(Duration.ofSeconds(30)).header("Accept", "application/json").GET().build();
|
||||
HttpResponse<String> resp = http.send(req, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
||||
if (resp.statusCode() == 404) return null;
|
||||
if (resp.statusCode() < 200 || resp.statusCode() >= 300) {
|
||||
throw new IOException("HTTP " + resp.statusCode() + ": " + safe(resp.body()));
|
||||
}
|
||||
JsonNode offsets = MAPPER.readTree(resp.body());
|
||||
return validateLocation("ar-io", dataItemId,
|
||||
offsets.path("rootTxId").asText("").trim(),
|
||||
offsets.path("rootOffset").asLong(-1L),
|
||||
offsets.path("size").asLong(-1L));
|
||||
}
|
||||
|
||||
/** Turbo часто знает координаты раньше общего gateway index. */
|
||||
private DataItemLocation getTurboLocation(String dataItemId) throws Exception {
|
||||
HttpRequest req = HttpRequest.newBuilder(URI.create(trim(cfg.syncTurboApiBase()) + "/tx/" + dataItemId + "/offsets"))
|
||||
.timeout(Duration.ofSeconds(30)).header("Accept", "application/json").GET().build();
|
||||
HttpResponse<String> resp = http.send(req, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
||||
if (resp.statusCode() == 404) return null;
|
||||
if (resp.statusCode() < 200 || resp.statusCode() >= 300) {
|
||||
throw new IOException("HTTP " + resp.statusCode() + ": " + safe(resp.body()));
|
||||
}
|
||||
JsonNode offsets = MAPPER.readTree(resp.body());
|
||||
return validateLocation("turbo", dataItemId,
|
||||
offsets.path("rootBundleId").asText("").trim(),
|
||||
offsets.path("startOffsetInRootBundle").asLong(-1L),
|
||||
offsets.path("rawContentLength").asLong(-1L));
|
||||
}
|
||||
|
||||
/**
|
||||
* Последний fallback: успешный HEAD/GET AR.IO может вернуть координаты DataItem в headers.
|
||||
* Поддерживаем несколько имён headers, чтобы пережить различия версий gateway.
|
||||
*/
|
||||
private DataItemLocation getHeadLocation(String dataItemId) throws Exception {
|
||||
HttpRequest req = HttpRequest.newBuilder(URI.create(trim(cfg.syncGateway()) + "/" + dataItemId))
|
||||
.timeout(Duration.ofSeconds(45))
|
||||
.header("Accept", "application/octet-stream")
|
||||
.method("HEAD", HttpRequest.BodyPublishers.noBody())
|
||||
.build();
|
||||
HttpResponse<Void> resp = http.send(req, HttpResponse.BodyHandlers.discarding());
|
||||
if (resp.statusCode() == 404) return null;
|
||||
if (resp.statusCode() < 200 || resp.statusCode() >= 300) {
|
||||
throw new IOException("HTTP " + resp.statusCode());
|
||||
}
|
||||
|
||||
String rootTxId = firstHeader(resp, "X-AR-IO-Root-Transaction-Id");
|
||||
long offset = firstLongHeader(resp,
|
||||
"X-AR-IO-Root-Item-Offset",
|
||||
"X-AR-IO-Root-Data-Item-Offset",
|
||||
"X-AR-IO-Data-Item-Offset");
|
||||
long size = firstLongHeader(resp,
|
||||
"X-AR-IO-Root-Item-Size",
|
||||
"X-AR-IO-Data-Item-Size");
|
||||
if (rootTxId == null || offset < 0 || size <= 0) return null;
|
||||
return validateLocation("head", dataItemId, rootTxId, offset, size);
|
||||
}
|
||||
|
||||
private DataItemLocation validateLocation(String source, String dataItemId, String rootTxId, long rootOffset, long size)
|
||||
throws IOException {
|
||||
if (!isArweaveId(rootTxId) || rootOffset < 0 || size <= 0) {
|
||||
throw new IOException("bad location for " + dataItemId + ": rootTxId=" + rootTxId
|
||||
+ " offset=" + rootOffset + " size=" + size);
|
||||
}
|
||||
if (size > cfg.syncMaxDataItemBytes()) {
|
||||
throw new IOException("DataItem exceeds syncMaxDataItemBytes: " + size);
|
||||
}
|
||||
if (size > Integer.MAX_VALUE) throw new IOException("DataItem is too large for in-memory import: " + size);
|
||||
if (rootOffset > Long.MAX_VALUE - size) throw new IOException("DataItem offset overflow");
|
||||
return new DataItemLocation(rootTxId, rootOffset, size, source);
|
||||
}
|
||||
|
||||
private static String firstHeader(HttpResponse<?> response, String name) {
|
||||
return response.headers().firstValue(name).map(String::trim).filter(v -> !v.isEmpty()).orElse(null);
|
||||
}
|
||||
|
||||
private static long firstLongHeader(HttpResponse<?> response, String... names) {
|
||||
for (String name : names) {
|
||||
String value = firstHeader(response, name);
|
||||
if (value == null) continue;
|
||||
try { return Long.parseLong(value); }
|
||||
catch (NumberFormatException ignored) { }
|
||||
}
|
||||
return -1L;
|
||||
}
|
||||
|
||||
private static boolean isArweaveId(String value) {
|
||||
if (value == null || value.length() != 43) return false;
|
||||
try { return B64URL.decode(value).length == 32; }
|
||||
catch (IllegalArgumentException e) { return false; }
|
||||
}
|
||||
|
||||
private static boolean isRetryableHttpStatus(int status) {
|
||||
return status == 404 || status == 408 || status == 425 || status == 429 || status >= 500;
|
||||
}
|
||||
|
||||
private static String shortError(Exception e) {
|
||||
String message = e.getMessage();
|
||||
return safe(message == null || message.isBlank() ? e.getClass().getSimpleName() : message);
|
||||
}
|
||||
|
||||
private record PendingDownload(String dataItemId, byte[] id32, long height) {}
|
||||
private record DownloadedDataItem(String rootTxId, byte[] rawDataItem) {}
|
||||
private record DataItemLocation(String rootTxId, long rootOffset, long size, String source) {}
|
||||
|
||||
private static final class RetryableDataItemDownloadException extends IOException {
|
||||
private RetryableDataItemDownloadException(String message) { super(message); }
|
||||
}
|
||||
|
||||
private static byte[] readExactlyBounded(InputStream in, int expected) throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream(expected);
|
||||
byte[] buf = new byte[Math.min(64 * 1024, Math.max(1024, expected))];
|
||||
int remaining = expected;
|
||||
while (remaining > 0) {
|
||||
int n = in.read(buf, 0, Math.min(buf.length, remaining));
|
||||
if (n < 0) break;
|
||||
out.write(buf, 0, n);
|
||||
remaining -= n;
|
||||
}
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
private static String safe(String value) {
|
||||
String v = value == null ? "" : value.replace('\n',' ').replace('\r',' ').trim();
|
||||
return v.length() <= 500 ? v : v.substring(0, 500);
|
||||
}
|
||||
private static String trim(String s){return String.valueOf(s==null?"":s).trim().replaceAll("/+$","");}
|
||||
private static String escapeGraphQl(String s){return s.replace("\\","\\\\").replace("\"","\\\"");}
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
package server.archive;
|
||||
|
||||
import blockchain.Ans104DataItem;
|
||||
import blockchain.ShineProtocolTags;
|
||||
import utils.config.AppConfig;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Locale;
|
||||
|
||||
/** Configuration of per-user-block ANS-104 Arweave/Turbo transport. */
|
||||
public record ArweaveBlocksConfig(
|
||||
PublishMode publishMode,
|
||||
int publishIntervalMinutes,
|
||||
int publishMaxItems,
|
||||
long publishMaxBundleBytes,
|
||||
String publishGateway,
|
||||
Path walletJwkPath,
|
||||
int minConfirmations,
|
||||
int confirmPollSeconds,
|
||||
int confirmTimeoutMinutes,
|
||||
String turboUploadUrl,
|
||||
String turboPaidByAddress,
|
||||
Path turboWalletJwkPath,
|
||||
boolean syncEnabled,
|
||||
int syncIntervalMinutes,
|
||||
int syncPageSize,
|
||||
int syncQueueBatchSize,
|
||||
long syncStartBlockHeight,
|
||||
long syncMaxDataItemBytes,
|
||||
String syncGateway,
|
||||
String syncTurboApiBase,
|
||||
int syncLocationRetrySeconds
|
||||
) {
|
||||
public enum PublishMode {
|
||||
TURBO,
|
||||
ARWEAVE,
|
||||
NONE;
|
||||
|
||||
static PublishMode parse(String value) {
|
||||
String normalized = value == null ? "none" : value.trim().toLowerCase(Locale.ROOT);
|
||||
return switch (normalized) {
|
||||
case "turbo" -> TURBO;
|
||||
case "arweave" -> ARWEAVE;
|
||||
case "none", "" -> NONE;
|
||||
default -> throw new IllegalArgumentException(
|
||||
"arweave.blocks.publish.mode must be turbo, arweave or none; got: " + value);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
long maxDataItemBytes = parseLong(c.getParam("arweave.blocks.sync.maxDataItemBytes"), Ans104DataItem.MAX_DATA_ITEM_BYTES);
|
||||
if (maxDataItemBytes > Ans104DataItem.MAX_DATA_ITEM_BYTES) maxDataItemBytes = Ans104DataItem.MAX_DATA_ITEM_BYTES;
|
||||
return new ArweaveBlocksConfig(
|
||||
PublishMode.parse(c.getParam("arweave.blocks.publish.mode")),
|
||||
positive(c.getInt("arweave.blocks.publish.intervalMinutes", 15), "publish.intervalMinutes"),
|
||||
positive(c.getInt("arweave.blocks.publish.maxItems", 10_000), "publish.maxItems"),
|
||||
positiveLong(parseLong(c.getParam("arweave.blocks.publish.maxBundleBytes"), 128L * 1024 * 1024), "publish.maxBundleBytes"),
|
||||
orDefault(c.getParam("arweave.blocks.publish.gateway"), "https://arweave.net"),
|
||||
optionalPath(c.getParam("arweave.blocks.publish.walletJwkPath")),
|
||||
nonNegative(c.getInt("arweave.blocks.publish.minConfirmations", 0), "publish.minConfirmations"),
|
||||
positive(c.getInt("arweave.blocks.publish.confirmPollSeconds", 30), "publish.confirmPollSeconds"),
|
||||
positive(c.getInt("arweave.blocks.publish.confirmTimeoutMinutes", 180), "publish.confirmTimeoutMinutes"),
|
||||
orDefault(c.getParam("arweave.blocks.publish.turbo.uploadUrl"), "https://turbo.ardrive.io/tx"),
|
||||
blankToNull(c.getParam("arweave.blocks.publish.turbo.paidByAddress")),
|
||||
optionalPath(c.getParam("arweave.blocks.publish.turbo.walletJwkPath")),
|
||||
c.getBoolean("arweave.blocks.sync.enabled", false),
|
||||
positive(c.getInt("arweave.blocks.sync.intervalMinutes", 15), "sync.intervalMinutes"),
|
||||
clamp(c.getInt("arweave.blocks.sync.pageSize", 100), 1, 100),
|
||||
positive(c.getInt("arweave.blocks.sync.queueBatchSize", 10_000), "sync.queueBatchSize"),
|
||||
nonNegativeLong(parseLong(c.getParam("arweave.blocks.sync.startBlockHeight"), 0L), "sync.startBlockHeight"),
|
||||
positiveLong(maxDataItemBytes, "sync.maxDataItemBytes"),
|
||||
orDefault(c.getParam("arweave.blocks.sync.gateway"), "https://turbo-gateway.com"),
|
||||
orDefault(c.getParam("arweave.blocks.sync.turboApiBase"), "https://turbo.ardrive.io"),
|
||||
positive(c.getInt("arweave.blocks.sync.locationRetrySeconds", 30), "sync.locationRetrySeconds")
|
||||
);
|
||||
}
|
||||
|
||||
public boolean publishEnabled() { return publishMode != PublishMode.NONE; }
|
||||
|
||||
public void validatePublisher() {
|
||||
if (publishMode == PublishMode.ARWEAVE && walletJwkPath == null) {
|
||||
throw new IllegalArgumentException("arweave.blocks.publish.walletJwkPath is required for mode=arweave");
|
||||
}
|
||||
if (publishMode == PublishMode.TURBO && (turboUploadUrl == null || turboUploadUrl.isBlank())) {
|
||||
throw new IllegalArgumentException("arweave.blocks.publish.turbo.uploadUrl is required for mode=turbo");
|
||||
}
|
||||
}
|
||||
|
||||
private static String orDefault(String v, String d) { return v == null || v.isBlank() ? d : v.trim(); }
|
||||
private static String blankToNull(String v) { return v == null || v.isBlank() ? null : v.trim(); }
|
||||
private static Path optionalPath(String v) { return v == null || v.isBlank() ? null : Path.of(v.trim()); }
|
||||
private static long parseLong(String v, long d) { return v == null || v.isBlank() ? d : Long.parseLong(v.trim()); }
|
||||
private static int positive(int v,String n){if(v<=0)throw new IllegalArgumentException(n+" must be >0");return v;}
|
||||
private static int nonNegative(int v,String n){if(v<0)throw new IllegalArgumentException(n+" must be >=0");return v;}
|
||||
private static long positiveLong(long v,String n){if(v<=0)throw new IllegalArgumentException(n+" must be >0");return v;}
|
||||
private static long nonNegativeLong(long v,String n){if(v<0)throw new IllegalArgumentException(n+" must be >=0");return v;}
|
||||
private static int clamp(int v,int min,int max){return Math.max(min,Math.min(max,v));}
|
||||
}
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
package server.archive;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.security.*;
|
||||
import java.security.spec.MGF1ParameterSpec;
|
||||
import java.security.spec.PSSParameterSpec;
|
||||
import java.security.spec.RSAPrivateCrtKeySpec;
|
||||
import java.time.Duration;
|
||||
import java.util.*;
|
||||
|
||||
/** Existing native Arweave L1 transport adapted to upload in-memory ANS-104 bundles. */
|
||||
public final class ArweaveL1Uploader {
|
||||
private static final Logger log = LoggerFactory.getLogger(ArweaveL1Uploader.class);
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final Base64.Encoder B64 = Base64.getUrlEncoder().withoutPadding();
|
||||
private static final Base64.Decoder B64D = Base64.getUrlDecoder();
|
||||
|
||||
public record Tag(String name, String value) {}
|
||||
public record UploadResult(String txId, byte[] txIdBytes) {}
|
||||
|
||||
private final ArweaveBlocksConfig cfg;
|
||||
private final HttpClient http = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(20)).build();
|
||||
private volatile Wallet wallet;
|
||||
|
||||
public ArweaveL1Uploader(ArweaveBlocksConfig cfg) { this.cfg = Objects.requireNonNull(cfg); }
|
||||
|
||||
public UploadResult upload(byte[] data, List<Tag> tags) throws Exception {
|
||||
if (data == null || data.length == 0) throw new IllegalArgumentException("Arweave upload data is empty");
|
||||
Wallet w = wallet();
|
||||
String gateway = trimGateway(cfg.publishGateway());
|
||||
ArweaveMerkle.Prepared merkle = ArweaveMerkle.prepare(data);
|
||||
String anchor = getRequiredText(gateway, "/tx_anchor");
|
||||
String reward = getRequiredText(gateway, "/price/" + data.length);
|
||||
if (!reward.matches("^\\d+$")) throw new IOException("Bad Arweave price: " + reward);
|
||||
verifyBalance(gateway, w.address(), reward);
|
||||
|
||||
List<List<byte[]>> tagList = new ArrayList<>();
|
||||
List<Map<String,String>> jsonTags = new ArrayList<>();
|
||||
for (Tag tag : tags == null ? List.<Tag>of() : tags) addTag(jsonTags, tagList, tag.name(), tag.value());
|
||||
|
||||
byte[] signaturePayload = deepHash(List.of(
|
||||
utf8("2"), b64Decode(w.owner()), new byte[0], utf8("0"), utf8(reward),
|
||||
b64Decode(anchor), tagList, utf8(Integer.toString(data.length)), merkle.dataRoot()
|
||||
));
|
||||
byte[] rawSignature = signPayload(w.privateKey(), signaturePayload);
|
||||
byte[] txIdBytes = sha256(rawSignature);
|
||||
String txId = b64(txIdBytes);
|
||||
|
||||
Map<String,Object> tx = new LinkedHashMap<>();
|
||||
tx.put("format",2); tx.put("id",txId); tx.put("last_tx",anchor); tx.put("owner",w.owner());
|
||||
tx.put("tags",jsonTags); tx.put("target",""); tx.put("quantity","0");
|
||||
tx.put("data_root",b64(merkle.dataRoot())); tx.put("data_size",Integer.toString(data.length)); tx.put("data","");
|
||||
tx.put("reward",reward); tx.put("signature",b64(rawSignature));
|
||||
postJsonExpect2xx(gateway + "/tx", tx, Duration.ofSeconds(60));
|
||||
|
||||
for (int i=0;i<merkle.chunks().size();i++) {
|
||||
ArweaveMerkle.Chunk meta=merkle.chunks().get(i); ArweaveMerkle.Proof proof=merkle.proofs().get(i);
|
||||
Map<String,Object> body=new LinkedHashMap<>();
|
||||
body.put("data_root",b64(merkle.dataRoot())); body.put("data_size",Integer.toString(data.length));
|
||||
body.put("data_path",b64(proof.proof())); body.put("offset",Long.toString(proof.offset()));
|
||||
body.put("chunk",b64(ArweaveMerkle.readChunk(data,meta)));
|
||||
postChunkWithRetry(gateway,body,i,merkle.chunks().size());
|
||||
}
|
||||
if (cfg.minConfirmations() > 0) waitForConfirmations(txId);
|
||||
log.info("ANS-104 bundle uploaded to Arweave: tx={} bytes={} items-transported",txId,data.length);
|
||||
return new UploadResult(txId,txIdBytes);
|
||||
}
|
||||
|
||||
private int waitForConfirmations(String txId) throws Exception {
|
||||
String gateway=trimGateway(cfg.publishGateway());
|
||||
long deadline=System.currentTimeMillis()+cfg.confirmTimeoutMinutes()*60_000L; int last=0;
|
||||
while(System.currentTimeMillis()<deadline){
|
||||
HttpRequest req=HttpRequest.newBuilder(URI.create(gateway+"/tx/"+txId+"/status")).timeout(Duration.ofSeconds(20)).GET().build();
|
||||
HttpResponse<String> resp=http.send(req,HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
||||
if(resp.statusCode()==200){JsonNode j=MAPPER.readTree(resp.body());last=j.path("number_of_confirmations").asInt(0);if(last>=cfg.minConfirmations())return last;}
|
||||
else if(resp.statusCode()!=202&&resp.statusCode()!=404)log.warn("Arweave status tx={} HTTP {}",txId,resp.statusCode());
|
||||
Thread.sleep(cfg.confirmPollSeconds()*1000L);
|
||||
}
|
||||
throw new IOException("Arweave confirmation timeout for "+txId+", last="+last);
|
||||
}
|
||||
|
||||
private void postChunkWithRetry(String gateway,Map<String,Object> body,int index,int total)throws Exception{
|
||||
Exception last=null;
|
||||
for(int attempt=1;attempt<=12;attempt++)try{postJsonExpect2xx(gateway+"/chunk",body,Duration.ofSeconds(60));return;}catch(Exception e){last=e;if(attempt==12)break;Thread.sleep(5000L);}
|
||||
throw new IOException("Failed Arweave chunk "+(index+1)+"/"+total,last);
|
||||
}
|
||||
private void postJsonExpect2xx(String url,Object body,Duration timeout)throws Exception{
|
||||
HttpRequest req=HttpRequest.newBuilder(URI.create(url)).timeout(timeout).header("Content-Type","application/json").header("Accept","application/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(MAPPER.writeValueAsString(body),StandardCharsets.UTF_8)).build();
|
||||
HttpResponse<String> resp=http.send(req,HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
||||
if(resp.statusCode()<200||resp.statusCode()>=300)throw new IOException("HTTP "+resp.statusCode()+" "+safe(resp.body()));
|
||||
}
|
||||
private void verifyBalance(String gateway,String address,String reward)throws Exception{
|
||||
String balance=getRequiredText(gateway,"/wallet/"+address+"/balance");
|
||||
if(balance.matches("^\\d+$")&&new BigInteger(balance).compareTo(new BigInteger(reward))<0)throw new IllegalStateException("Not enough AR: balance="+balance+" reward="+reward);
|
||||
}
|
||||
private String getRequiredText(String gateway,String path)throws Exception{
|
||||
HttpRequest req=HttpRequest.newBuilder(URI.create(gateway+path)).timeout(Duration.ofSeconds(20)).GET().build();
|
||||
HttpResponse<String> resp=http.send(req,HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
||||
if(resp.statusCode()<200||resp.statusCode()>=300)throw new IOException("Arweave HTTP "+resp.statusCode()+" for "+path+": "+safe(resp.body()));
|
||||
String s=resp.body()==null?"":resp.body().trim();if(s.isBlank())throw new IOException("Empty Arweave response for "+path);return s;
|
||||
}
|
||||
private Wallet wallet()throws Exception{
|
||||
Wallet local=wallet;if(local!=null)return local;synchronized(this){if(wallet!=null)return wallet;
|
||||
JsonNode jwk=MAPPER.readTree(Files.readString(cfg.walletJwkPath(),StandardCharsets.UTF_8));String owner=required(jwk,"n");
|
||||
PrivateKey pk=buildPrivateKey(jwk);String address=b64(sha256(b64Decode(owner)));return wallet=new Wallet(owner,address,pk);}
|
||||
}
|
||||
private static PrivateKey buildPrivateKey(JsonNode jwk)throws GeneralSecurityException{
|
||||
RSAPrivateCrtKeySpec spec=new RSAPrivateCrtKeySpec(big(required(jwk,"n")),big(required(jwk,"e")),big(required(jwk,"d")),big(required(jwk,"p")),big(required(jwk,"q")),big(required(jwk,"dp")),big(required(jwk,"dq")),big(required(jwk,"qi")));
|
||||
return KeyFactory.getInstance("RSA").generatePrivate(spec);
|
||||
}
|
||||
private static byte[] signPayload(PrivateKey key,byte[] payload)throws GeneralSecurityException{
|
||||
Signature s=Signature.getInstance("RSASSA-PSS");s.setParameter(new PSSParameterSpec("SHA-256","MGF1",MGF1ParameterSpec.SHA256,32,1));s.initSign(key);s.update(payload);return s.sign();
|
||||
}
|
||||
private static byte[] deepHash(Object data)throws GeneralSecurityException{
|
||||
if(data instanceof List<?> list){byte[] acc=sha384(utf8("list"+list.size()));for(Object item:list)acc=sha384(concat(acc,deepHash(item)));return acc;}
|
||||
if(!(data instanceof byte[] bytes))throw new IllegalArgumentException("deepHash byte[]/List expected");return sha384(concat(sha384(utf8("blob"+bytes.length)),sha384(bytes)));
|
||||
}
|
||||
private static void addTag(List<Map<String,String>> json,List<List<byte[]>> raw,String name,String value){byte[]n=utf8(name),v=utf8(value);Map<String,String>m=new LinkedHashMap<>();m.put("name",b64(n));m.put("value",b64(v));json.add(m);raw.add(List.of(n,v));}
|
||||
private static String required(JsonNode n,String f){String s=n==null?"":n.path(f).asText("").trim();if(s.isBlank())throw new IllegalStateException("Arweave JWK missing "+f);return s;}
|
||||
private static BigInteger big(String s){return new BigInteger(1,b64Decode(s));}
|
||||
private static String trimGateway(String s){return(s==null?"https://arweave.net":s.trim()).replaceAll("/+$","");}
|
||||
private static byte[] utf8(String s){return String.valueOf(s==null?"":s).getBytes(StandardCharsets.UTF_8);}
|
||||
private static byte[] b64Decode(String s){return B64D.decode(s);}private static String b64(byte[]b){return B64.encodeToString(b);}
|
||||
private static byte[] sha256(byte[]b)throws GeneralSecurityException{return MessageDigest.getInstance("SHA-256").digest(b);}private static byte[] sha384(byte[]b)throws GeneralSecurityException{return MessageDigest.getInstance("SHA-384").digest(b);}
|
||||
private static byte[] concat(byte[]...a){int n=0;for(byte[]x:a)n=Math.addExact(n,x.length);byte[]o=new byte[n];int p=0;for(byte[]x:a){System.arraycopy(x,0,o,p,x.length);p+=x.length;}return o;}
|
||||
private static String safe(String s){s=String.valueOf(s==null?"":s).replace('\n',' ').replace('\r',' ').trim();return s.length()<=250?s:s.substring(0,250)+"...";}
|
||||
private record Wallet(String owner,String address,PrivateKey privateKey){}
|
||||
}
|
||||
@@ -29,6 +29,55 @@ final class ArweaveMerkle {
|
||||
|
||||
private ArweaveMerkle() {}
|
||||
|
||||
|
||||
static Prepared prepare(byte[] data) throws IOException {
|
||||
if (data == null || data.length == 0) throw new IOException("Arweave data is empty");
|
||||
List<Chunk> chunks = chunkBytes(data);
|
||||
List<Node> leaves = new ArrayList<>(chunks.size());
|
||||
for (Chunk c : chunks) {
|
||||
byte[] id = sha256(concat(sha256(c.dataHash()), sha256(note(c.maxByteRange()))));
|
||||
leaves.add(new Leaf(id, c.dataHash(), c.minByteRange(), c.maxByteRange()));
|
||||
}
|
||||
Node root = buildLayers(leaves);
|
||||
List<Proof> proofs = new ArrayList<>();
|
||||
resolveProofs(root, new byte[0], proofs);
|
||||
if (!chunks.isEmpty() && chunks.get(chunks.size() - 1).size() == 0) {
|
||||
chunks = new ArrayList<>(chunks);
|
||||
chunks.remove(chunks.size() - 1);
|
||||
proofs.remove(proofs.size() - 1);
|
||||
}
|
||||
if (chunks.size() != proofs.size()) throw new IOException("Arweave Merkle: chunks/proofs mismatch");
|
||||
return new Prepared(root.id(), List.copyOf(chunks), List.copyOf(proofs));
|
||||
}
|
||||
|
||||
private static List<Chunk> chunkBytes(byte[] data) {
|
||||
List<Chunk> out = new ArrayList<>();
|
||||
long remaining = data.length;
|
||||
long cursor = 0;
|
||||
int p = 0;
|
||||
while (remaining >= MAX_CHUNK_SIZE) {
|
||||
long chunkSize = MAX_CHUNK_SIZE;
|
||||
long next = remaining - MAX_CHUNK_SIZE;
|
||||
if (next > 0 && next < MIN_CHUNK_SIZE) chunkSize = (remaining + 1) / 2;
|
||||
byte[] chunk = Arrays.copyOfRange(data, p, p + (int) chunkSize);
|
||||
out.add(new Chunk(sha256(chunk), cursor, cursor + chunkSize));
|
||||
p += (int) chunkSize;
|
||||
cursor += chunkSize;
|
||||
remaining -= chunkSize;
|
||||
}
|
||||
byte[] rest = Arrays.copyOfRange(data, p, p + (int) remaining);
|
||||
out.add(new Chunk(sha256(rest), cursor, cursor + remaining));
|
||||
return out;
|
||||
}
|
||||
|
||||
static byte[] readChunk(byte[] data, Chunk c) throws IOException {
|
||||
long len = c.size();
|
||||
if (data == null || len < 0 || len > Integer.MAX_VALUE || c.minByteRange() < 0 || c.maxByteRange() > data.length) {
|
||||
throw new IOException("Invalid Arweave chunk range");
|
||||
}
|
||||
return Arrays.copyOfRange(data, (int) c.minByteRange(), (int) c.maxByteRange());
|
||||
}
|
||||
|
||||
static Prepared prepare(Path file) throws IOException {
|
||||
long fileSize = Files.size(file);
|
||||
List<Chunk> chunks = chunkFile(file, fileSize);
|
||||
|
||||
-101
@@ -1,101 +0,0 @@
|
||||
package server.archive;
|
||||
|
||||
import sync.util.Base58Util;
|
||||
import utils.crypto.Ed25519Util;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.*;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.*;
|
||||
|
||||
/** Строгий reader/verifier формата SHINE-ARCHIVE v1.0. */
|
||||
public final class ShineArchiveReader {
|
||||
public record Reference(long bigBlockNumber, byte[] archiveHash, String arweaveTxId) {}
|
||||
public record Chunk(long offset, long size, String blockchainName, List<byte[]> rawRecords,
|
||||
long previousBigBlockRef, long previousChunkOffset, long previousChunkSize) {}
|
||||
public record Document(long bigBlockNumber, long createdAtMs, String creatorLogin,
|
||||
List<Reference> references, List<Chunk> chunks, byte[] archiveHash) {}
|
||||
|
||||
public Document readAndVerify(Path file, String expectedPublisherLogin, byte[] expectedHash, String rootPublicKeyBase58) throws Exception {
|
||||
long fileSize = Files.size(file);
|
||||
if (fileSize < 13 + 38 + 1 + 32 + 64) throw new IOException("SHINE-ARCHIVE слишком короткий");
|
||||
if (fileSize >= 0x1_0000_0000L) throw new IOException("SHINE-ARCHIVE v1 >= 4 GiB не поддерживается");
|
||||
|
||||
byte[] computedHash = digestPrefix(file, fileSize - 96);
|
||||
try (RandomAccessFile in = new RandomAccessFile(file.toFile(), "r")) {
|
||||
byte[] magic = new byte[ShineArchiveWriter.MAGIC.length];
|
||||
in.readFully(magic);
|
||||
if (!Arrays.equals(magic, ShineArchiveWriter.MAGIC)) throw new IOException("Bad SHINE-ARCHIVE magic");
|
||||
int major=in.readUnsignedByte(), minor=in.readUnsignedByte();
|
||||
if (major != 1 || minor != 0) throw new IOException("Unsupported SHINE-ARCHIVE version " + major + "." + minor);
|
||||
long headerSize=u32(in); long bigBlock=u32(in); long createdAt=in.readLong();
|
||||
int creatorLen=in.readUnsignedByte(); int referencesMode=in.readUnsignedByte();
|
||||
long refsCount=u32(in); int refSize=in.readUnsignedShort(); long parentRef=u32(in);
|
||||
long chunksCount=u32(in); long recordsCount=u32(in);
|
||||
if (referencesMode != ShineArchiveWriter.REFERENCES_FULL || refSize != ShineArchiveWriter.REFERENCE_ENTRY_SIZE) {
|
||||
throw new IOException("Unsupported references table");
|
||||
}
|
||||
String creator=readUtf8(in,creatorLen);
|
||||
if (!creator.equalsIgnoreCase(String.valueOf(expectedPublisherLogin))) {
|
||||
throw new SecurityException("Archive creator " + creator + " != trusted publisher " + expectedPublisherLogin);
|
||||
}
|
||||
if (headerSize != in.getFilePointer()) throw new IOException("Некорректный header_size");
|
||||
if (refsCount > 10_000_000L || chunksCount > 10_000_000L) throw new IOException("Слишком много refs/chunks");
|
||||
|
||||
List<Reference> refs=new ArrayList<>((int)Math.min(refsCount, Integer.MAX_VALUE));
|
||||
for(long i=0;i<refsCount;i++) {
|
||||
long number=u32(in); byte[] hash=readFixed(in,32); byte[] tx=readFixed(in,32);
|
||||
refs.add(new Reference(number,hash,Base64.getUrlEncoder().withoutPadding().encodeToString(tx)));
|
||||
}
|
||||
if (refs.isEmpty() && parentRef != ShineArchiveWriter.NO_REFERENCE_U32) throw new IOException("Некорректный parent reference");
|
||||
if (!refs.isEmpty() && parentRef >= refs.size()) throw new IOException("parent_reference_index вне таблицы");
|
||||
|
||||
List<Chunk> chunks=new ArrayList<>((int)Math.min(chunksCount, Integer.MAX_VALUE));
|
||||
long parsedRecords=0;
|
||||
for(long ci=0;ci<chunksCount;ci++) {
|
||||
long chunkOffset=in.getFilePointer(); long chunkSize=u32(in); long chunkEnd=chunkOffset+chunkSize;
|
||||
if (chunkSize < 21 || chunkEnd > fileSize-96) throw new IOException("Некорректный chunk_size");
|
||||
int nameLen=in.readUnsignedByte(); String bch=readUtf8(in,nameLen); long count=u32(in);
|
||||
if (count > 10_000_000L) throw new IOException("Слишком много records в chunk " + bch);
|
||||
List<byte[]> records=new ArrayList<>((int)Math.min(count,Integer.MAX_VALUE));
|
||||
for(long ri=0;ri<count;ri++) {
|
||||
long n=u32(in); if(n<=0 || n>Integer.MAX_VALUE || in.getFilePointer()+n>chunkEnd-12) throw new IOException("Некорректный record_size");
|
||||
records.add(readFixed(in,(int)n)); parsedRecords++;
|
||||
}
|
||||
long prevRef=u32(in), prevOffset=u32(in), prevSize=u32(in);
|
||||
if (prevRef != ShineArchiveWriter.NO_REFERENCE_U32 && prevRef >= refs.size()) throw new IOException("previous_big_block_ref вне таблицы");
|
||||
if (in.getFilePointer()!=chunkEnd) throw new IOException("Chunk framing mismatch: " + bch);
|
||||
chunks.add(new Chunk(chunkOffset,chunkSize,bch,List.copyOf(records),prevRef,prevOffset,prevSize));
|
||||
}
|
||||
if (parsedRecords != recordsCount) throw new IOException("total_user_records_count mismatch");
|
||||
|
||||
int closerLen=in.readUnsignedByte(); String closer=readUtf8(in,closerLen);
|
||||
if(!closer.equals(creator)) throw new SecurityException("creator_login != closer_login");
|
||||
if(in.getFilePointer()!=fileSize-96) throw new IOException("Некорректная граница footer/hash");
|
||||
byte[] storedHash=readFixed(in,32); byte[] signature=readFixed(in,64);
|
||||
if(in.getFilePointer()!=fileSize) throw new IOException("Лишние данные после signature");
|
||||
if(!Arrays.equals(storedHash,computedHash)) throw new SecurityException("SHA-256 archive не совпадает с footer");
|
||||
if(expectedHash!=null && !Arrays.equals(expectedHash,computedHash)) throw new SecurityException("SHA-256 archive не совпадает с ожидаемым hash");
|
||||
byte[] root=Base58Util.decode(rootPublicKeyBase58);
|
||||
if(root.length!=32) throw new SecurityException("Некорректный root public key publisher-а");
|
||||
byte[] signed=new byte[ShineArchiveWriter.SIGNATURE_DOMAIN.length+32];
|
||||
System.arraycopy(ShineArchiveWriter.SIGNATURE_DOMAIN,0,signed,0,ShineArchiveWriter.SIGNATURE_DOMAIN.length);
|
||||
System.arraycopy(computedHash,0,signed,ShineArchiveWriter.SIGNATURE_DOMAIN.length,32);
|
||||
if(!Ed25519Util.verify(signed,signature,root)) throw new SecurityException("Не прошла подпись SHINE-ARCHIVE publisher-а");
|
||||
return new Document(bigBlock,createdAt,creator,List.copyOf(refs),List.copyOf(chunks),computedHash);
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] digestPrefix(Path file,long bytes) throws Exception {
|
||||
MessageDigest md=MessageDigest.getInstance("SHA-256");
|
||||
try(InputStream in=new BufferedInputStream(Files.newInputStream(file))) {
|
||||
byte[] buf=new byte[1024*1024]; long left=bytes;
|
||||
while(left>0){int n=in.read(buf,0,(int)Math.min(buf.length,left)); if(n<0) throw new EOFException(); md.update(buf,0,n); left-=n;}
|
||||
}
|
||||
return md.digest();
|
||||
}
|
||||
private static long u32(RandomAccessFile in) throws IOException { return Integer.toUnsignedLong(in.readInt()); }
|
||||
private static byte[] readFixed(RandomAccessFile in,int n) throws IOException { byte[] b=new byte[n]; in.readFully(b); return b; }
|
||||
private static String readUtf8(RandomAccessFile in,int n) throws IOException { return new String(readFixed(in,n), StandardCharsets.UTF_8); }
|
||||
}
|
||||
-310
@@ -1,310 +0,0 @@
|
||||
package server.archive;
|
||||
|
||||
import shine.db.archive.ArchiveBigBlockRef;
|
||||
import shine.db.archive.ArchivePublishJobChain;
|
||||
import shine.db.entities.BlockEntry;
|
||||
import utils.crypto.Ed25519Util;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.*;
|
||||
import java.security.DigestOutputStream;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Writer бинарного SHINE-ARCHIVE v1.0.
|
||||
*
|
||||
* Один blockchain_name встречается максимум один раз и содержит все новые raw records этой цепочки.
|
||||
*/
|
||||
public final class ShineArchiveWriter {
|
||||
public static final byte VERSION_MAJOR = 1;
|
||||
public static final byte VERSION_MINOR = 0;
|
||||
public static final byte REFERENCES_FULL = 0;
|
||||
public static final long NO_REFERENCE_U32 = 0xffff_ffffL;
|
||||
public static final int REFERENCE_ENTRY_SIZE = 68;
|
||||
public static final byte[] MAGIC = "SHINE-ARCHIVE".getBytes(StandardCharsets.US_ASCII);
|
||||
public static final byte[] SIGNATURE_DOMAIN = "SHINE-ARCHIVE-V1".getBytes(StandardCharsets.US_ASCII);
|
||||
|
||||
public record ChunkPosition(long offset, long size) {}
|
||||
public record BuildResult(Path file, byte[] archiveHash, Map<String, ChunkPosition> chunks, long fileSize) {}
|
||||
|
||||
public BuildResult build(
|
||||
Path finalPath,
|
||||
String creatorLogin,
|
||||
long bigBlockNumber,
|
||||
long createdAtMs,
|
||||
List<ArchiveBigBlockRef> previousBigBlocks,
|
||||
List<ArchivePublishJobChain> chains,
|
||||
Map<String, List<BlockEntry>> recordsByBlockchain,
|
||||
byte[] signingPrivateSeed32,
|
||||
long maxFileBytes
|
||||
) throws IOException {
|
||||
Objects.requireNonNull(finalPath, "finalPath");
|
||||
Objects.requireNonNull(previousBigBlocks, "previousBigBlocks");
|
||||
Objects.requireNonNull(chains, "chains");
|
||||
Objects.requireNonNull(recordsByBlockchain, "recordsByBlockchain");
|
||||
requireU32(bigBlockNumber, "big_block_number");
|
||||
if (signingPrivateSeed32 == null || signingPrivateSeed32.length != 32) {
|
||||
throw new IllegalArgumentException("Archive signing key должен быть 32-byte Ed25519 seed");
|
||||
}
|
||||
|
||||
byte[] loginBytes = utf8U8(creatorLogin, "creator_login");
|
||||
if (previousBigBlocks.size() > 0xffff_ffffL || chains.size() > 0xffff_ffffL) {
|
||||
throw new IllegalArgumentException("Слишком много references/chunks для v1");
|
||||
}
|
||||
|
||||
List<ArchivePublishJobChain> sortedChains = new ArrayList<>(chains);
|
||||
sortedChains.sort(Comparator.comparing(ArchivePublishJobChain::blockchainName));
|
||||
|
||||
long totalRecords = 0;
|
||||
for (ArchivePublishJobChain ch : sortedChains) {
|
||||
List<BlockEntry> rs = recordsByBlockchain.get(ch.blockchainName());
|
||||
if (rs == null || rs.isEmpty()) {
|
||||
throw new IllegalArgumentException("Нет frozen records для " + ch.blockchainName());
|
||||
}
|
||||
totalRecords += rs.size();
|
||||
requireU32(totalRecords, "total_user_records_count");
|
||||
}
|
||||
|
||||
Map<Long, Integer> referenceIndexByBlock = new HashMap<>();
|
||||
for (int i = 0; i < previousBigBlocks.size(); i++) {
|
||||
ArchiveBigBlockRef ref = previousBigBlocks.get(i);
|
||||
requireU32(ref.bigBlockNumber(), "PreviousBigBlockReference.big_block_number");
|
||||
require32(ref.archiveHash(), "PreviousBigBlockReference.big_block_hash");
|
||||
require32(ref.arweaveTxId(), "PreviousBigBlockReference.arweave_tx_id");
|
||||
referenceIndexByBlock.put(ref.bigBlockNumber(), i);
|
||||
}
|
||||
|
||||
Files.createDirectories(finalPath.toAbsolutePath().getParent());
|
||||
Path tmp = finalPath.resolveSibling(finalPath.getFileName() + ".tmp");
|
||||
Files.deleteIfExists(tmp);
|
||||
|
||||
Map<String, ChunkPosition> positions = new LinkedHashMap<>();
|
||||
MessageDigest digest = sha256();
|
||||
|
||||
try (OutputStream fileOut = new BufferedOutputStream(Files.newOutputStream(tmp, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE));
|
||||
CountingOutputStream counting = new CountingOutputStream(fileOut);
|
||||
DigestOutputStream digestOut = new DigestOutputStream(counting, digest);
|
||||
DataOutputStream out = new DataOutputStream(digestOut)) {
|
||||
|
||||
// ---------- header ----------
|
||||
out.write(MAGIC);
|
||||
out.writeByte(VERSION_MAJOR);
|
||||
out.writeByte(VERSION_MINOR);
|
||||
|
||||
long headerSize = fixedHeaderSize() + loginBytes.length;
|
||||
writeU32(out, headerSize);
|
||||
writeU32(out, bigBlockNumber);
|
||||
out.writeLong(createdAtMs);
|
||||
out.writeByte(loginBytes.length);
|
||||
out.writeByte(REFERENCES_FULL);
|
||||
writeU32(out, previousBigBlocks.size());
|
||||
out.writeShort(REFERENCE_ENTRY_SIZE);
|
||||
long parentIndex = previousBigBlocks.isEmpty() ? NO_REFERENCE_U32 : previousBigBlocks.size() - 1L;
|
||||
writeU32(out, parentIndex);
|
||||
writeU32(out, sortedChains.size());
|
||||
writeU32(out, totalRecords);
|
||||
out.write(loginBytes);
|
||||
|
||||
// ---------- полный список предыдущих больших блоков ----------
|
||||
for (ArchiveBigBlockRef ref : previousBigBlocks) {
|
||||
writeU32(out, ref.bigBlockNumber());
|
||||
out.write(ref.archiveHash());
|
||||
out.write(ref.arweaveTxId());
|
||||
}
|
||||
|
||||
// ---------- по одному chunk на blockchain_name ----------
|
||||
for (ArchivePublishJobChain ch : sortedChains) {
|
||||
List<BlockEntry> records = new ArrayList<>(recordsByBlockchain.get(ch.blockchainName()));
|
||||
records.sort(Comparator.comparingInt(BlockEntry::getBlockNumber));
|
||||
validateFrozenRange(ch, records);
|
||||
|
||||
byte[] name = utf8U8(ch.blockchainName(), "blockchain_name");
|
||||
long chunkSize = calculateChunkSize(name.length, records);
|
||||
requireU32(chunkSize, "chunk_size");
|
||||
long chunkOffset = counting.getCount();
|
||||
requireU32(chunkOffset, "chunk_offset");
|
||||
if (chunkOffset + chunkSize > maxFileBytes) {
|
||||
throw new ArchiveTooLargeException("Большой архив превысит archive.maxFileBytes: " + (chunkOffset + chunkSize));
|
||||
}
|
||||
|
||||
writeU32(out, chunkSize);
|
||||
out.writeByte(name.length);
|
||||
out.write(name);
|
||||
writeU32(out, records.size());
|
||||
for (BlockEntry record : records) {
|
||||
byte[] raw = record.getBlockBytes();
|
||||
if (raw == null) throw new IllegalArgumentException("block_bytes=NULL: " + ch.blockchainName() + "#" + record.getBlockNumber());
|
||||
writeU32(out, raw.length);
|
||||
out.write(raw);
|
||||
}
|
||||
|
||||
if (ch.previousArchiveBigBlockNumber() == null) {
|
||||
writeU32(out, NO_REFERENCE_U32);
|
||||
writeU32(out, 0);
|
||||
writeU32(out, 0);
|
||||
} else {
|
||||
Integer refIndex = referenceIndexByBlock.get(ch.previousArchiveBigBlockNumber());
|
||||
if (refIndex == null) {
|
||||
throw new IllegalStateException("Предыдущий big block blockchain отсутствует в FULL reference table: "
|
||||
+ ch.blockchainName() + " -> " + ch.previousArchiveBigBlockNumber());
|
||||
}
|
||||
ArchiveBigBlockRef previousRef = previousBigBlocks.get(refIndex);
|
||||
require32(ch.previousArchiveBigBlockHash(), "previous_archive_big_block_hash " + ch.blockchainName());
|
||||
if (!Arrays.equals(previousRef.archiveHash(), ch.previousArchiveBigBlockHash())) {
|
||||
throw new IllegalStateException("Hash предыдущего big block не совпадает с FULL reference table: "
|
||||
+ ch.blockchainName() + " -> " + ch.previousArchiveBigBlockNumber());
|
||||
}
|
||||
if (ch.previousChunkOffset() == null || ch.previousChunkSize() == null) {
|
||||
throw new IllegalStateException("Нет предыдущих chunk offset/size: " + ch.blockchainName());
|
||||
}
|
||||
writeU32(out, refIndex);
|
||||
writeU32(out, ch.previousChunkOffset());
|
||||
writeU32(out, ch.previousChunkSize());
|
||||
}
|
||||
positions.put(ch.blockchainName(), new ChunkPosition(chunkOffset, chunkSize));
|
||||
}
|
||||
|
||||
// ---------- footer, login входит в hash ----------
|
||||
out.writeByte(loginBytes.length);
|
||||
out.write(loginBytes);
|
||||
out.flush();
|
||||
|
||||
byte[] archiveHash = digest.digest();
|
||||
byte[] signedPayload = new byte[SIGNATURE_DOMAIN.length + archiveHash.length];
|
||||
System.arraycopy(SIGNATURE_DOMAIN, 0, signedPayload, 0, SIGNATURE_DOMAIN.length);
|
||||
System.arraycopy(archiveHash, 0, signedPayload, SIGNATURE_DOMAIN.length, archiveHash.length);
|
||||
byte[] signature = Ed25519Util.sign(signedPayload, signingPrivateSeed32);
|
||||
|
||||
// hash/signature не должны попасть в hash самого блока.
|
||||
digestOut.on(false);
|
||||
out.write(archiveHash);
|
||||
out.write(signature);
|
||||
out.flush();
|
||||
|
||||
long finalSize = counting.getCount();
|
||||
if (finalSize >= 0x1_0000_0000L || finalSize > maxFileBytes) {
|
||||
throw new ArchiveTooLargeException("Размер SHINE-ARCHIVE v1 превышает лимит: " + finalSize);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Files.deleteIfExists(tmp);
|
||||
if (e instanceof IOException ioe) throw ioe;
|
||||
throw new IOException("Не удалось собрать SHINE-ARCHIVE", e);
|
||||
}
|
||||
|
||||
// До Arweave upload существует уже готовый локальный файл с окончательным именем.
|
||||
try {
|
||||
Files.move(tmp, finalPath, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (AtomicMoveNotSupportedException e) {
|
||||
Files.move(tmp, finalPath, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
|
||||
byte[] hash = readStoredArchiveHash(finalPath);
|
||||
long size = Files.size(finalPath);
|
||||
return new BuildResult(finalPath, hash, Collections.unmodifiableMap(positions), size);
|
||||
}
|
||||
|
||||
/** Проверяет локальный файл перед оплатой Arweave. */
|
||||
public static byte[] verifyLocalFile(Path file) throws IOException {
|
||||
long size = Files.size(file);
|
||||
if (size < MAGIC.length + 2 + 96) throw new IOException("Слишком короткий SHINE-ARCHIVE: " + file);
|
||||
byte[] stored = readStoredArchiveHash(file);
|
||||
MessageDigest md = sha256();
|
||||
long hashInputSize = size - 96; // [archive_hash 32][signature 64]
|
||||
try (InputStream in = Files.newInputStream(file)) {
|
||||
byte[] buf = new byte[1024 * 1024];
|
||||
long left = hashInputSize;
|
||||
while (left > 0) {
|
||||
int n = in.read(buf, 0, (int)Math.min(buf.length, left));
|
||||
if (n < 0) throw new EOFException("Неожиданный EOF при проверке архива");
|
||||
md.update(buf, 0, n);
|
||||
left -= n;
|
||||
}
|
||||
}
|
||||
byte[] actual = md.digest();
|
||||
if (!Arrays.equals(stored, actual)) throw new IOException("SHA-256 локального SHINE-ARCHIVE не совпадает с footer");
|
||||
return actual;
|
||||
}
|
||||
|
||||
public static byte[] readStoredArchiveHash(Path file) throws IOException {
|
||||
long size = Files.size(file);
|
||||
if (size < 96) throw new EOFException("Файл слишком короткий");
|
||||
try (RandomAccessFile raf = new RandomAccessFile(file.toFile(), "r")) {
|
||||
raf.seek(size - 96);
|
||||
byte[] hash = new byte[32];
|
||||
raf.readFully(hash);
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
|
||||
private static long fixedHeaderSize() {
|
||||
// magic + major/minor + headerSize + blockNo + time + creatorLen + mode + refCount + refSize + parentIndex + chunkCount + recordCount
|
||||
return 13L + 1 + 1 + 4 + 4 + 8 + 1 + 1 + 4 + 2 + 4 + 4 + 4;
|
||||
}
|
||||
|
||||
private static long calculateChunkSize(int nameLen, List<BlockEntry> records) {
|
||||
long size = 4L + 1L + nameLen + 4L + 12L;
|
||||
for (BlockEntry e : records) {
|
||||
byte[] raw = e.getBlockBytes();
|
||||
if (raw == null) throw new IllegalArgumentException("raw record is null");
|
||||
size += 4L + raw.length;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
private static void validateFrozenRange(ArchivePublishJobChain ch, List<BlockEntry> records) {
|
||||
long expected = ch.toSourceBlockNumber() - ch.fromSourceBlockNumber() + 1;
|
||||
if (expected <= 0 || expected != records.size()) {
|
||||
throw new IllegalArgumentException("Frozen range неполон для " + ch.blockchainName()
|
||||
+ ": ожидалось=" + expected + ", получено=" + records.size());
|
||||
}
|
||||
long n = ch.fromSourceBlockNumber();
|
||||
for (BlockEntry e : records) {
|
||||
if (Integer.toUnsignedLong(e.getBlockNumber()) != n) {
|
||||
throw new IllegalArgumentException("Нарушена непрерывность " + ch.blockchainName()
|
||||
+ ": ожидался block " + n + ", получен " + Integer.toUnsignedLong(e.getBlockNumber()));
|
||||
}
|
||||
n++;
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] utf8U8(String value, String name) {
|
||||
if (value == null) throw new IllegalArgumentException(name + " == null");
|
||||
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
|
||||
if (bytes.length == 0 || bytes.length > 255) throw new IllegalArgumentException(name + " должен занимать 1..255 UTF-8 байт");
|
||||
return bytes;
|
||||
}
|
||||
|
||||
private static void require32(byte[] v, String name) {
|
||||
if (v == null || v.length != 32) throw new IllegalArgumentException(name + " должен быть 32 bytes");
|
||||
}
|
||||
|
||||
private static void requireU32(long v, String name) {
|
||||
if (v < 0 || v > 0xffff_ffffL) throw new IllegalArgumentException(name + " вне u32: " + v);
|
||||
}
|
||||
|
||||
private static void writeU32(DataOutputStream out, long v) throws IOException {
|
||||
requireU32(v, "u32");
|
||||
out.writeInt((int)v); // DataOutputStream = big-endian; битовый шаблон корректен и для unsigned > 2^31.
|
||||
}
|
||||
|
||||
private static MessageDigest sha256() {
|
||||
try { return MessageDigest.getInstance("SHA-256"); }
|
||||
catch (NoSuchAlgorithmException e) { throw new IllegalStateException(e); }
|
||||
}
|
||||
|
||||
/** OutputStream со счётчиком без ограничения int. */
|
||||
private static final class CountingOutputStream extends FilterOutputStream {
|
||||
private long count;
|
||||
CountingOutputStream(OutputStream out) { super(out); }
|
||||
long getCount() { return count; }
|
||||
@Override public void write(int b) throws IOException { out.write(b); count++; }
|
||||
@Override public void write(byte[] b, int off, int len) throws IOException { out.write(b, off, len); count += len; }
|
||||
}
|
||||
|
||||
public static final class ArchiveTooLargeException extends IOException {
|
||||
public ArchiveTooLargeException(String message) { super(message); }
|
||||
}
|
||||
}
|
||||
-373
@@ -1,373 +0,0 @@
|
||||
package server.archive;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sync.codec.ShineUsersCodec;
|
||||
import sync.codec.ShineUsersCodec.UserPdaSnapshot;
|
||||
import sync.util.Base58Util;
|
||||
import sync.util.SolanaPdaUtil;
|
||||
import utils.crypto.Ed25519Util;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.security.MessageDigest;
|
||||
import java.time.Duration;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Обновляет ArchiveHeadBlock (type=100) обычной инструкцией update_user_pda.
|
||||
* Отдельной Solana-инструкции для архива нет.
|
||||
*/
|
||||
public final class SolanaArchiveHeadWriter {
|
||||
private static final Logger log = LoggerFactory.getLogger(SolanaArchiveHeadWriter.class);
|
||||
private static final ObjectMapper JSON = new ObjectMapper();
|
||||
|
||||
private static final String SYSTEM_PROGRAM = "11111111111111111111111111111111";
|
||||
private static final String ED25519_PROGRAM = "Ed25519SigVerify111111111111111111111111111";
|
||||
private static final String COMPUTE_BUDGET_PROGRAM = "ComputeBudget111111111111111111111111111111";
|
||||
private static final String SYSVAR_INSTRUCTIONS = "Sysvar1nstructions1111111111111111111111111";
|
||||
private static final String USER_SEED_PREFIX = "user_login=";
|
||||
private static final String ECONOMY_SEED = "shine_users_economy_config";
|
||||
private static final String INFLOW_SEED = "shine_payments_inflow_vault";
|
||||
private static final byte[] LAST_BLOCK_STATE_PREFIX = "SHiNE_LAST_BLOCK".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
private final ArchivePublisherConfig cfg;
|
||||
private final HttpClient http = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(20)).build();
|
||||
private final byte[] rootSeed32;
|
||||
private final byte[] clientSeed32;
|
||||
|
||||
public SolanaArchiveHeadWriter(ArchivePublisherConfig cfg) throws IOException {
|
||||
this.cfg = Objects.requireNonNull(cfg);
|
||||
this.rootSeed32 = ArchiveKeyLoader.loadSeed32(cfg.solanaRootKeyPath());
|
||||
this.clientSeed32 = ArchiveKeyLoader.loadSeed32(cfg.solanaClientKeyPath());
|
||||
}
|
||||
|
||||
public record SubmitResult(String signature, String userPda) {}
|
||||
|
||||
/** Preflight: проверяет, что локальные root/client private keys соответствуют текущему User PDA. */
|
||||
public void validateKeysAgainstPda() throws Exception {
|
||||
Current current = loadCurrent();
|
||||
byte[] expectedRoot = Base58Util.decode(current.snapshot.rootKey());
|
||||
byte[] expectedClient = Base58Util.decode(current.snapshot.clientKey());
|
||||
if (!Arrays.equals(expectedRoot, Ed25519Util.derivePublicKey(rootSeed32))) {
|
||||
throw new IllegalStateException("archive.solana.rootKeyPath не соответствует root_key User PDA " + cfg.serverLogin());
|
||||
}
|
||||
if (!Arrays.equals(expectedClient, Ed25519Util.derivePublicKey(clientSeed32))) {
|
||||
throw new IllegalStateException("archive.solana.clientKeyPath не соответствует client_key User PDA " + cfg.serverLogin());
|
||||
}
|
||||
}
|
||||
|
||||
public SubmitResult submitArchiveHead(byte[] arweaveTxId32, byte[] archiveHash32) throws Exception {
|
||||
require32(arweaveTxId32, "Arweave TX ID");
|
||||
require32(archiveHash32, "archive hash");
|
||||
|
||||
Current current = loadCurrent();
|
||||
UserPdaSnapshot old = current.snapshot;
|
||||
byte[] rootPub = Ed25519Util.derivePublicKey(rootSeed32);
|
||||
byte[] clientPub = Ed25519Util.derivePublicKey(clientSeed32);
|
||||
if (!Arrays.equals(rootPub, Base58Util.decode(old.rootKey()))) {
|
||||
throw new IllegalStateException("Root key archive publisher-а не совпадает с User PDA");
|
||||
}
|
||||
if (!Arrays.equals(clientPub, Base58Util.decode(old.clientKey()))) {
|
||||
throw new IllegalStateException("Client key archive publisher-а не совпадает с User PDA");
|
||||
}
|
||||
|
||||
byte[] currentRaw = Base64.getDecoder().decode(old.rawDataBase64());
|
||||
int currentRecordLen = u16le(currentRaw, 7);
|
||||
byte[] currentUnsigned = Arrays.copyOf(currentRaw, currentRecordLen - 64);
|
||||
byte[] prevHash = sha256(currentUnsigned);
|
||||
|
||||
long updatedAt = System.currentTimeMillis();
|
||||
String txB64Url = Base64.getUrlEncoder().withoutPadding().encodeToString(arweaveTxId32);
|
||||
String archiveHashHex = hex(archiveHash32);
|
||||
String zeroSig = Base58Util.encode(new byte[64]);
|
||||
|
||||
UserPdaSnapshot nextUnsignedSnapshot = new UserPdaSnapshot(
|
||||
old.pdaAddress(), old.login(), old.recordNumber() + 1, old.slot(), "",
|
||||
old.recoveryKey(), old.rootKey(), old.clientKey(), old.blockchainName(), old.blockchainKey(),
|
||||
old.paidLimitBytes(), old.usedBytes(), old.lastBlockNumber(), old.lastBlockHash(),
|
||||
old.lastBlockSignature(), old.arweaveTxId(), txB64Url, archiveHashHex,
|
||||
old.isServer(), old.addressFormatType(), old.addressFormatVersion(), old.serverAddress(),
|
||||
old.syncServers(), old.accessServers(), old.sessionsMode(), old.sessions(), old.trustedCount(),
|
||||
old.createdAtMs(), updatedAt, hex(prevHash), zeroSig, ""
|
||||
);
|
||||
byte[] nextFull = Base64.getDecoder().decode(ShineUsersCodec.serializeSnapshotToBase64(nextUnsignedSnapshot));
|
||||
int nextRecordLen = u16le(nextFull, 7);
|
||||
byte[] nextUnsigned = Arrays.copyOf(nextFull, nextRecordLen - 64);
|
||||
byte[] nextUnsignedHash = sha256(nextUnsigned);
|
||||
byte[] rootSignature = Ed25519Util.sign(nextUnsignedHash, rootSeed32);
|
||||
|
||||
byte[] updateData = buildUpdateInstruction(old, updatedAt, prevHash, arweaveTxId32, archiveHash32, rootSignature);
|
||||
byte[] lastStateHash = sha256(buildLastBlockState(old));
|
||||
byte[] lastBlockSignature = Base58Util.decode(old.lastBlockSignature());
|
||||
byte[] blockchainPub = Base58Util.decode(old.blockchainKey());
|
||||
|
||||
byte[] rootEdData = buildEd25519Instruction(rootSignature, rootPub, nextUnsignedHash);
|
||||
byte[] bchEdData = buildEd25519Instruction(lastBlockSignature, blockchainPub, lastStateHash);
|
||||
|
||||
String blockhash = getLatestBlockhash();
|
||||
byte[] message = buildLegacyMessage(current.userPda, updateData, rootEdData, bchEdData, blockhash, clientPub);
|
||||
byte[] clientSignature = Ed25519Util.sign(message, clientSeed32);
|
||||
byte[] transaction = serializeTransaction(clientSignature, message);
|
||||
String signature = sendTransaction(transaction);
|
||||
log.info("Archive head отправлен в Solana: login={} pda={} signature={}", cfg.serverLogin(), current.userPda, signature);
|
||||
return new SubmitResult(signature, current.userPda);
|
||||
}
|
||||
|
||||
public void waitFinalized(String signature) throws Exception {
|
||||
long deadline = System.currentTimeMillis() + cfg.solanaConfirmTimeoutMinutes() * 60_000L;
|
||||
while (System.currentTimeMillis() < deadline) {
|
||||
JsonNode result = rpc("getSignatureStatuses", List.of(List.of(signature), Map.of("searchTransactionHistory", true))).path("result");
|
||||
JsonNode values = result.path("value");
|
||||
JsonNode status = values.isArray() && values.size() > 0 ? values.get(0) : null;
|
||||
if (status != null && !status.isNull()) {
|
||||
if (status.hasNonNull("err")) {
|
||||
throw new IOException("Solana archive-head tx завершилась ошибкой: " + status.path("err"));
|
||||
}
|
||||
String confirmation = status.path("confirmationStatus").asText("");
|
||||
if ("finalized".equals(confirmation)) return;
|
||||
}
|
||||
Thread.sleep(cfg.solanaConfirmPollSeconds() * 1000L);
|
||||
}
|
||||
throw new IOException("Timeout ожидания Solana finalized: " + signature);
|
||||
}
|
||||
|
||||
public boolean currentHeadMatches(byte[] txId32, byte[] hash32) throws Exception {
|
||||
Current current = loadCurrent();
|
||||
if (current.snapshot.archiveHeadTxId() == null || current.snapshot.archiveHeadTxId().isBlank()) return false;
|
||||
byte[] currentTx = Base64.getUrlDecoder().decode(current.snapshot.archiveHeadTxId());
|
||||
byte[] currentHash = unhex(current.snapshot.archiveHeadHash());
|
||||
return Arrays.equals(currentTx, txId32) && Arrays.equals(currentHash, hash32);
|
||||
}
|
||||
|
||||
private Current loadCurrent() throws Exception {
|
||||
String normalized = cfg.serverLogin().trim().toLowerCase(Locale.ROOT);
|
||||
String userPda = SolanaPdaUtil.findProgramAddress(
|
||||
List.of(USER_SEED_PREFIX.getBytes(StandardCharsets.UTF_8), normalized.getBytes(StandardCharsets.UTF_8)),
|
||||
cfg.solanaUsersProgramId());
|
||||
JsonNode accountResult = rpc("getAccountInfo", List.of(userPda, Map.of("encoding", "base64", "commitment", cfg.solanaCommitment()))).path("result");
|
||||
JsonNode value = accountResult.path("value");
|
||||
if (value.isMissingNode() || value.isNull()) throw new IOException("User PDA не найден для " + cfg.serverLogin());
|
||||
String owner = value.path("owner").asText("");
|
||||
if (!cfg.solanaUsersProgramId().equals(owner)) throw new IOException("User PDA принадлежит другой программе: " + owner);
|
||||
JsonNode data = value.path("data");
|
||||
if (!data.isArray() || data.size() < 1) throw new IOException("Некорректный getAccountInfo.data");
|
||||
String rawB64 = data.get(0).asText();
|
||||
UserPdaSnapshot snapshot = ShineUsersCodec.parseUserPdaAccount(userPda, 0L, rawB64, "");
|
||||
return new Current(userPda, snapshot);
|
||||
}
|
||||
|
||||
private byte[] buildUpdateInstruction(UserPdaSnapshot s, long updatedAt, byte[] prevHash,
|
||||
byte[] archiveTx, byte[] archiveHash, byte[] rootSig) throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
out.write(4); // IX_UPDATE_USER_PDA
|
||||
strU8(out, s.login());
|
||||
out.write(Base58Util.decode(s.recoveryKey()));
|
||||
out.write(Base58Util.decode(s.rootKey()));
|
||||
u64le(out, s.createdAtMs());
|
||||
u64le(out, updatedAt);
|
||||
u32le(out, Integer.toUnsignedLong(s.recordNumber() + 1));
|
||||
out.write(prevHash);
|
||||
u64le(out, 0L); // additional_limit
|
||||
out.write(Base58Util.decode(s.clientKey()));
|
||||
out.write(Base58Util.decode(s.blockchainKey()));
|
||||
strU8(out, s.blockchainName());
|
||||
u64le(out, s.usedBytes());
|
||||
u32le(out, Integer.toUnsignedLong(s.lastBlockNumber()));
|
||||
out.write(unhex(s.lastBlockHash()));
|
||||
out.write(Base58Util.decode(s.lastBlockSignature()));
|
||||
strU8(out, s.arweaveTxId());
|
||||
out.write(s.isServer() ? 1 : 0);
|
||||
if (s.isServer()) {
|
||||
out.write(s.addressFormatType() & 0xff);
|
||||
out.write(s.addressFormatVersion() & 0xff);
|
||||
strU8(out, s.serverAddress());
|
||||
if (s.syncServers().size() > 255) throw new IOException("Слишком много sync_servers");
|
||||
out.write(s.syncServers().size());
|
||||
for (String v : s.syncServers()) strU8(out, v);
|
||||
}
|
||||
if (s.accessServers().size() > 255) throw new IOException("Слишком много access_servers");
|
||||
out.write(s.accessServers().size());
|
||||
for (String v : s.accessServers()) strU8(out, v);
|
||||
out.write(s.sessionsMode() & 0xff);
|
||||
if (s.sessions().size() > 255) throw new IOException("Слишком много sessions");
|
||||
out.write(s.sessions().size());
|
||||
for (ShineUsersCodec.UserSessionSnapshot session : s.sessions()) {
|
||||
out.write(session.sessionType() & 0xff);
|
||||
out.write(session.sessionVersion() & 0xff);
|
||||
strU8(out, session.sessionName());
|
||||
out.write(Base58Util.decode(session.sessionPubKey()));
|
||||
}
|
||||
out.write(s.trustedCount() & 0xff);
|
||||
// Archive extension к обычному update_user_pda: marker=1 + tx32 + hash32.
|
||||
out.write(1);
|
||||
out.write(archiveTx);
|
||||
out.write(archiveHash);
|
||||
out.write(rootSig);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
private static byte[] buildLastBlockState(UserPdaSnapshot s) throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
out.write(LAST_BLOCK_STATE_PREFIX);
|
||||
strU8(out, s.login());
|
||||
strU8(out, s.blockchainName());
|
||||
u32le(out, Integer.toUnsignedLong(s.lastBlockNumber()));
|
||||
out.write(unhex(s.lastBlockHash()));
|
||||
u64le(out, s.usedBytes());
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
private byte[] buildLegacyMessage(String userPda, byte[] updateData, byte[] rootEd, byte[] bchEd,
|
||||
String recentBlockhash, byte[] clientPub) throws IOException {
|
||||
String economy = SolanaPdaUtil.findProgramAddress(List.of(ECONOMY_SEED.getBytes(StandardCharsets.UTF_8)), cfg.solanaUsersProgramId());
|
||||
String inflow = SolanaPdaUtil.findProgramAddress(List.of(INFLOW_SEED.getBytes(StandardCharsets.UTF_8)), cfg.solanaPaymentsProgramId());
|
||||
|
||||
List<Account> accounts = List.of(
|
||||
new Account(Base58Util.encode(clientPub), true, true),
|
||||
new Account(userPda, false, true),
|
||||
new Account(inflow, false, true),
|
||||
new Account(SYSTEM_PROGRAM, false, false),
|
||||
new Account(SYSVAR_INSTRUCTIONS, false, false),
|
||||
new Account(economy, false, false),
|
||||
new Account(COMPUTE_BUDGET_PROGRAM, false, false),
|
||||
new Account(ED25519_PROGRAM, false, false),
|
||||
new Account(cfg.solanaUsersProgramId(), false, false)
|
||||
);
|
||||
Map<String,Integer> ix = new HashMap<>();
|
||||
for (int i=0;i<accounts.size();i++) ix.put(accounts.get(i).pubkey, i);
|
||||
|
||||
List<CompiledInstruction> instructions = new ArrayList<>();
|
||||
instructions.add(new CompiledInstruction(ix.get(COMPUTE_BUDGET_PROGRAM), new int[0], computeHeapData(262_144)));
|
||||
instructions.add(new CompiledInstruction(ix.get(COMPUTE_BUDGET_PROGRAM), new int[0], computeLimitData(800_000)));
|
||||
instructions.add(new CompiledInstruction(ix.get(ED25519_PROGRAM), new int[0], rootEd));
|
||||
instructions.add(new CompiledInstruction(ix.get(ED25519_PROGRAM), new int[0], bchEd));
|
||||
instructions.add(new CompiledInstruction(ix.get(cfg.solanaUsersProgramId()), new int[]{
|
||||
ix.get(Base58Util.encode(clientPub)), ix.get(userPda), ix.get(SYSTEM_PROGRAM), ix.get(inflow),
|
||||
ix.get(SYSVAR_INSTRUCTIONS), ix.get(economy)
|
||||
}, updateData));
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
out.write(1); // numRequiredSignatures
|
||||
out.write(0); // numReadonlySignedAccounts
|
||||
out.write(6); // readonly unsigned: system, sysvar, economy, compute, ed25519, users
|
||||
shortVec(out, accounts.size());
|
||||
for (Account a : accounts) {
|
||||
byte[] pk = Base58Util.decode(a.pubkey);
|
||||
if (pk.length != 32) throw new IOException("Solana pubkey !=32: " + a.pubkey);
|
||||
out.write(pk);
|
||||
}
|
||||
byte[] bh = Base58Util.decode(recentBlockhash);
|
||||
if (bh.length != 32) throw new IOException("recentBlockhash !=32");
|
||||
out.write(bh);
|
||||
shortVec(out, instructions.size());
|
||||
for (CompiledInstruction ci : instructions) {
|
||||
out.write(ci.programIdIndex);
|
||||
shortVec(out, ci.accounts.length);
|
||||
for (int a : ci.accounts) out.write(a);
|
||||
shortVec(out, ci.data.length);
|
||||
out.write(ci.data);
|
||||
}
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
private String getLatestBlockhash() throws Exception {
|
||||
JsonNode result = rpc("getLatestBlockhash", List.of(Map.of("commitment", cfg.solanaCommitment()))).path("result");
|
||||
String blockhash = result.path("value").path("blockhash").asText("");
|
||||
if (blockhash.isBlank()) throw new IOException("Solana getLatestBlockhash не вернул blockhash");
|
||||
return blockhash;
|
||||
}
|
||||
|
||||
private String sendTransaction(byte[] tx) throws Exception {
|
||||
String txB64 = Base64.getEncoder().encodeToString(tx);
|
||||
JsonNode result = rpc("sendTransaction", List.of(txB64, Map.of(
|
||||
"encoding", "base64",
|
||||
"skipPreflight", false,
|
||||
"preflightCommitment", "confirmed",
|
||||
"maxRetries", 5
|
||||
))).path("result");
|
||||
String sig = result.asText("");
|
||||
if (sig.isBlank()) throw new IOException("Solana sendTransaction не вернул signature");
|
||||
return sig;
|
||||
}
|
||||
|
||||
private JsonNode rpc(String method, List<?> params) throws Exception {
|
||||
Map<String,Object> body = new LinkedHashMap<>();
|
||||
body.put("jsonrpc", "2.0"); body.put("id", 1); body.put("method", method); body.put("params", params);
|
||||
HttpRequest req = HttpRequest.newBuilder(URI.create(cfg.solanaRpcUrl()))
|
||||
.timeout(Duration.ofSeconds(60))
|
||||
.header("Content-Type", "application/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(JSON.writeValueAsString(body), StandardCharsets.UTF_8))
|
||||
.build();
|
||||
HttpResponse<String> resp = http.send(req, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
||||
if (resp.statusCode() < 200 || resp.statusCode() >= 300) throw new IOException("Solana RPC HTTP " + resp.statusCode() + ": " + safe(resp.body()));
|
||||
JsonNode root = JSON.readTree(resp.body());
|
||||
if (root.hasNonNull("error")) throw new IOException("Solana RPC " + method + " error: " + root.path("error"));
|
||||
return root;
|
||||
}
|
||||
|
||||
private static byte[] serializeTransaction(byte[] signature64, byte[] message) throws IOException {
|
||||
if (signature64.length != 64) throw new IOException("Solana signature !=64");
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
shortVec(out, 1);
|
||||
out.write(signature64);
|
||||
out.write(message);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
private static byte[] buildEd25519Instruction(byte[] signature64, byte[] publicKey32, byte[] message) {
|
||||
if (signature64.length != 64 || publicKey32.length != 32) throw new IllegalArgumentException("Некорректный Ed25519 ix key/signature");
|
||||
int sigOff=16, pkOff=sigOff+64, msgOff=pkOff+32;
|
||||
if (message.length > 0xffff) throw new IllegalArgumentException("Ed25519 message слишком длинный");
|
||||
byte[] data = new byte[msgOff + message.length];
|
||||
data[0]=1; data[1]=0;
|
||||
putU16le(data,2,sigOff); putU16le(data,4,0xffff);
|
||||
putU16le(data,6,pkOff); putU16le(data,8,0xffff);
|
||||
putU16le(data,10,msgOff); putU16le(data,12,message.length); putU16le(data,14,0xffff);
|
||||
System.arraycopy(signature64,0,data,sigOff,64);
|
||||
System.arraycopy(publicKey32,0,data,pkOff,32);
|
||||
System.arraycopy(message,0,data,msgOff,message.length);
|
||||
return data;
|
||||
}
|
||||
|
||||
private static byte[] computeHeapData(int bytes) throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write(1); u32le(out, Integer.toUnsignedLong(bytes)); return out.toByteArray();
|
||||
}
|
||||
private static byte[] computeLimitData(int units) throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write(2); u32le(out, Integer.toUnsignedLong(units)); return out.toByteArray();
|
||||
}
|
||||
|
||||
private static void shortVec(ByteArrayOutputStream out, int value) {
|
||||
int rem=value;
|
||||
while (true) {
|
||||
int elem=rem & 0x7f; rem >>>=7;
|
||||
if (rem==0) { out.write(elem); return; }
|
||||
out.write(elem | 0x80);
|
||||
}
|
||||
}
|
||||
private static void strU8(ByteArrayOutputStream out, String value) throws IOException {
|
||||
byte[] b=String.valueOf(value==null?"":value).getBytes(StandardCharsets.UTF_8);
|
||||
if (b.length>255) throw new IOException("Строка >255 bytes"); out.write(b.length); out.write(b);
|
||||
}
|
||||
private static void u32le(ByteArrayOutputStream out, long v) { for(int i=0;i<4;i++) out.write((int)(v >>> (8*i)) & 0xff); }
|
||||
private static void u64le(ByteArrayOutputStream out, long v) { for(int i=0;i<8;i++) out.write((int)(v >>> (8*i)) & 0xff); }
|
||||
private static void putU16le(byte[] d,int o,int v){d[o]=(byte)v;d[o+1]=(byte)(v>>>8);}
|
||||
private static int u16le(byte[] b,int o){return (b[o]&255)|((b[o+1]&255)<<8);}
|
||||
private static byte[] sha256(byte[] bytes) throws Exception { return MessageDigest.getInstance("SHA-256").digest(bytes); }
|
||||
private static String hex(byte[] b){StringBuilder s=new StringBuilder(b.length*2);for(byte x:b)s.append(String.format("%02x",x));return s.toString();}
|
||||
private static byte[] unhex(String s){String x=String.valueOf(s==null?"":s).trim(); if(x.length()!=64) throw new IllegalArgumentException("hex32 expected");byte[]o=new byte[32];for(int i=0;i<32;i++)o[i]=(byte)Integer.parseInt(x.substring(i*2,i*2+2),16);return o;}
|
||||
private static void require32(byte[] b,String n){if(b==null||b.length!=32)throw new IllegalArgumentException(n+" должен быть 32 bytes");}
|
||||
private static String safe(String s){String x=String.valueOf(s==null?"":s).replace('\n',' ').replace('\r',' ').trim();return x.length()<=500?x:x.substring(0,500)+"...";}
|
||||
|
||||
private record Current(String userPda, UserPdaSnapshot snapshot) {}
|
||||
private record Account(String pubkey, boolean signer, boolean writable) {}
|
||||
private record CompiledInstruction(int programIdIndex, int[] accounts, byte[] data) {}
|
||||
}
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
package server.archive;
|
||||
|
||||
import blockchain.Ans104DataItem;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.security.MessageDigest;
|
||||
import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.Objects;
|
||||
|
||||
/** Uploads an already user-signed ANS-104 DataItem to Turbo without modifying it. */
|
||||
public final class TurboDataItemUploader {
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final Base64.Encoder B64URL = Base64.getUrlEncoder().withoutPadding();
|
||||
private static final Base64.Decoder B64URL_DECODER = Base64.getUrlDecoder();
|
||||
|
||||
public record UploadResult(String dataItemId, String owner) {}
|
||||
|
||||
private final ArweaveBlocksConfig cfg;
|
||||
private final HttpClient http = HttpClient.newBuilder()
|
||||
.connectTimeout(Duration.ofSeconds(20))
|
||||
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||
.build();
|
||||
private volatile String resolvedPaidByAddress;
|
||||
|
||||
public TurboDataItemUploader(ArweaveBlocksConfig cfg) {
|
||||
this.cfg = Objects.requireNonNull(cfg);
|
||||
}
|
||||
|
||||
public UploadResult upload(byte[] rawDataItem, byte[] expectedId32) throws Exception {
|
||||
if (rawDataItem == null || rawDataItem.length == 0) throw new IllegalArgumentException("Turbo DataItem is empty");
|
||||
Ans104DataItem item = new Ans104DataItem(rawDataItem);
|
||||
if (!item.verifySignature()) throw new IllegalArgumentException("Turbo DataItem has bad ANS-104 signature");
|
||||
if (expectedId32 != null && !Arrays.equals(item.id32(), expectedId32)) {
|
||||
throw new IllegalArgumentException("Turbo DataItem id does not match blocks.data_item_id");
|
||||
}
|
||||
|
||||
String expectedId = B64URL.encodeToString(item.id32());
|
||||
HttpRequest.Builder request = HttpRequest.newBuilder(URI.create(cfg.turboUploadUrl()))
|
||||
.timeout(Duration.ofMinutes(2))
|
||||
.header("Content-Type", "application/octet-stream")
|
||||
.header("Accept", "application/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofByteArray(rawDataItem));
|
||||
String paidBy = paidByAddress();
|
||||
if (paidBy != null) request.header("x-paid-by", paidBy);
|
||||
|
||||
HttpResponse<String> response = http.send(request.build(), HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
||||
String body = response.body() == null ? "" : response.body().trim();
|
||||
if (response.statusCode() == 409 && body.toLowerCase().contains("data item exists")) {
|
||||
return new UploadResult(expectedId, B64URL.encodeToString(item.owner32()));
|
||||
}
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||||
String hint = response.statusCode() == 402
|
||||
? " (Turbo payment required: check server credits / Credit Share Approval / x-paid-by)"
|
||||
: "";
|
||||
throw new IOException("Turbo HTTP " + response.statusCode() + hint + ": " + safe(body));
|
||||
}
|
||||
|
||||
if (body.isBlank()) return new UploadResult(expectedId, B64URL.encodeToString(item.owner32()));
|
||||
JsonNode json;
|
||||
try { json = MAPPER.readTree(body); }
|
||||
catch (Exception ignored) { return new UploadResult(expectedId, B64URL.encodeToString(item.owner32())); }
|
||||
String returnedId = json.path("id").asText("").trim();
|
||||
if (!returnedId.isBlank() && !returnedId.equals(expectedId)) {
|
||||
throw new IOException("Turbo returned another DataItem id: expected=" + expectedId + " got=" + returnedId);
|
||||
}
|
||||
return new UploadResult(expectedId, json.path("owner").asText(""));
|
||||
}
|
||||
|
||||
/**
|
||||
* x-paid-by contains the payer's public native address, never the private key.
|
||||
* A configured Arweave JWK is used only to derive that public address.
|
||||
*/
|
||||
private String paidByAddress() throws Exception {
|
||||
if (resolvedPaidByAddress != null) return resolvedPaidByAddress.isBlank() ? null : resolvedPaidByAddress;
|
||||
synchronized (this) {
|
||||
if (resolvedPaidByAddress != null) return resolvedPaidByAddress.isBlank() ? null : resolvedPaidByAddress;
|
||||
String explicit = cfg.turboPaidByAddress();
|
||||
if (explicit != null && !explicit.isBlank()) return resolvedPaidByAddress = explicit.trim();
|
||||
if (cfg.turboWalletJwkPath() == null) {
|
||||
resolvedPaidByAddress = "";
|
||||
return null;
|
||||
}
|
||||
JsonNode jwk = MAPPER.readTree(Files.readString(cfg.turboWalletJwkPath(), StandardCharsets.UTF_8));
|
||||
String modulus = jwk.path("n").asText("").trim();
|
||||
if (modulus.isBlank()) throw new IllegalStateException("Turbo payer JWK missing n");
|
||||
byte[] owner = B64URL_DECODER.decode(modulus);
|
||||
resolvedPaidByAddress = B64URL.encodeToString(MessageDigest.getInstance("SHA-256").digest(owner));
|
||||
return resolvedPaidByAddress;
|
||||
}
|
||||
}
|
||||
|
||||
private static String safe(String value) {
|
||||
String v = value == null ? "" : value.replace('\n',' ').replace('\r',' ').trim();
|
||||
return v.length() <= 500 ? v : v.substring(0, 500);
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package server.archive.registration;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashSet;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/** Список разрешённых доменов для бесплатной регистрации; читается при старте Java-сервера. */
|
||||
public final class AllowedEmailDomains {
|
||||
// Обычные ASCII-домены (в том числе punycode), без масок, пробелов и URL.
|
||||
private static final Pattern DOMAIN = Pattern.compile(
|
||||
"(?=.{1,253}$)[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)+");
|
||||
private final Set<String> domains;
|
||||
|
||||
private AllowedEmailDomains(Set<String> domains) {
|
||||
this.domains = Set.copyOf(domains);
|
||||
}
|
||||
|
||||
/** Ошибки чтения, пустой или неверный список — ошибка запуска регистрации (fail-closed). */
|
||||
public static AllowedEmailDomains load(Path file) throws IOException {
|
||||
if (!Files.isRegularFile(file))
|
||||
throw new IOException("Email domain allowlist is missing: " + file.toAbsolutePath());
|
||||
Set<String> result = new HashSet<>();
|
||||
int lineNumber = 0;
|
||||
for (String line : Files.readAllLines(file, StandardCharsets.UTF_8)) {
|
||||
lineNumber++;
|
||||
String domain = line.strip().toLowerCase(Locale.ROOT);
|
||||
if (domain.isEmpty() || domain.startsWith("#")) continue;
|
||||
if (!DOMAIN.matcher(domain).matches())
|
||||
throw new IOException("Invalid allowed email domain at " + file + ":" + lineNumber);
|
||||
result.add(domain);
|
||||
}
|
||||
if (result.isEmpty())
|
||||
throw new IOException("Email domain allowlist is empty: " + file.toAbsolutePath());
|
||||
return new AllowedEmailDomains(result);
|
||||
}
|
||||
|
||||
/** Сопоставление строго по части после одного символа @, без wildcard и суффиксов. */
|
||||
public boolean permits(String email) {
|
||||
if (email == null) return false;
|
||||
String value = email.strip().toLowerCase(Locale.ROOT);
|
||||
int at = value.indexOf('@');
|
||||
if (at <= 0 || at != value.lastIndexOf('@')) return false;
|
||||
return domains.contains(value.substring(at + 1));
|
||||
}
|
||||
}
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
package server.archive.registration;
|
||||
|
||||
import server.archive.ArweaveBlocksConfig;
|
||||
import server.archive.TurboDataItemUploader;
|
||||
import server.logic.ws_protocol.JSON.handlers.registration.RegistrationApiBridge;
|
||||
import server.logic.ws_protocol.JSON.handlers.registration.Net_Registration_Request;
|
||||
import shine.db.DbController;
|
||||
import utils.config.AppConfig;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.PrintWriter;
|
||||
import java.nio.file.Path;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLFeatureNotSupportedException;
|
||||
import java.util.Base64;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/** Единственная точка подключения API без циклической зависимости Gradle-модулей. */
|
||||
public final class FreeRegistrationApiBootstrap {
|
||||
private static volatile FreeRegistrationWorkflow workflow;
|
||||
private static volatile LocalRegistrarSigner signer;
|
||||
private FreeRegistrationApiBootstrap() {}
|
||||
|
||||
public static synchronized void startOrThrow() throws Exception {
|
||||
if (workflow != null) return;
|
||||
AppConfig cfg=AppConfig.getInstance();
|
||||
LocalRegistrarSigner newSigner=null;
|
||||
FreeRegistrationIssuer issuer=null;
|
||||
FreeRegistrationPublisher publisher=null;
|
||||
RegistrationEmailSender emailSender=null;
|
||||
AllowedEmailDomains allowedEmailDomains=null;
|
||||
if (cfg.getBoolean("freeRegistration.enabled",false)) {
|
||||
String domainsFile = cfg.getStringOrEmpty("freeRegistration.email.allowedDomainsFile");
|
||||
if (domainsFile.isBlank()) domainsFile = "allowed-email-domains.txt";
|
||||
// Относительный путь — от WorkingDirectory процесса Java.
|
||||
allowedEmailDomains = AllowedEmailDomains.load(Path.of(domainsFile));
|
||||
boolean allowEmailReuse=cfg.getBoolean("freeRegistration.email.allowReuse",false);
|
||||
// Не выводить seed/path в журнал ни при успешном старте, ни при ошибке.
|
||||
ArweaveBlocksConfig transport=ArweaveBlocksConfig.load();
|
||||
if(transport.publishMode()!=ArweaveBlocksConfig.PublishMode.TURBO)
|
||||
throw new IllegalStateException("Free registration requires Turbo transport");
|
||||
newSigner=LocalRegistrarSigner.fromConfig(cfg);
|
||||
issuer=new FreeRegistrationIssuer(dbSource(),newSigner,allowEmailReuse);
|
||||
publisher=new FreeRegistrationPublisher(issuer,new TurboDataItemUploader(transport),
|
||||
new VerifiedFreeRegistrationImporter(allowEmailReuse));
|
||||
// Секрет берём ТОЛЬКО из окружения, не из файлов репозитория.
|
||||
String resendKey=System.getenv("RESEND_API_KEY");
|
||||
if(resendKey==null || resendKey.isBlank())
|
||||
resendKey=cfg.getStringOrEmpty("freeRegistration.email.apiKey");
|
||||
emailSender=new ResendRegistrationEmailSender(resendKey,
|
||||
cfg.getStringOrEmpty("freeRegistration.email.from").isBlank()
|
||||
? "SHiNE <noreply@shineup.me>"
|
||||
: cfg.getStringOrEmpty("freeRegistration.email.from"));
|
||||
}
|
||||
FreeRegistrationWorkflow newWorkflow=new FreeRegistrationWorkflow(issuer,publisher,emailSender,
|
||||
cfg.getBoolean("freeRegistration.email.allowTestCode",false),
|
||||
cfg.getBoolean("freeRegistration.email.allowReuse",false), allowedEmailDomains);
|
||||
RegistrationApiBridge.install((op,req)->dispatch(newWorkflow,op,req));
|
||||
signer=newSigner; workflow=newWorkflow;
|
||||
}
|
||||
public static synchronized void close() {
|
||||
RegistrationApiBridge.uninstall();
|
||||
workflow=null;
|
||||
if(signer!=null) signer.close();
|
||||
signer=null;
|
||||
}
|
||||
public static Map<String,Object> dispatch(FreeRegistrationWorkflow w, String op,Net_Registration_Request req) throws Exception {
|
||||
Map<String,Object> out=new LinkedHashMap<>();
|
||||
switch(op) {
|
||||
case "GetNamespaces" -> {
|
||||
out.put("namespaces",w.listNamespaces().stream().map(ns->Map.of(
|
||||
"name",ns.namespace(), "type",ns.type(), "minLocalLength",ns.minLoginLength(),
|
||||
"registrationFeeLamports",ns.priceLamports(), "defaultServerLogin",ns.serverLogin()==null?"":ns.serverLogin()
|
||||
)).toList());
|
||||
}
|
||||
case "CheckRegistrationLogin" -> {
|
||||
var info=w.checkLogin(req.getFullLogin());
|
||||
out.put("loginAvailable",info.loginAvailable());
|
||||
out.put("namespaceType",info.namespaceType());
|
||||
out.put("minLocalLength",info.minLocalLength());
|
||||
out.put("registrationFeeLamports",info.registrationFeeLamports());
|
||||
}
|
||||
case "CheckRegistrationEmail" -> {
|
||||
var check=w.checkEmail(req.getEmail());
|
||||
out.put("emailAvailable",check.emailAvailable());
|
||||
out.put("emailStatus",check.emailStatus());
|
||||
}
|
||||
case "StartFreeRegistration" -> {
|
||||
var state=w.start(req.getFullLogin(),decodeKey(req.getRootPublicKeyB64()),
|
||||
decodeKey(req.getBlockchainPublicKeyB64()),decodeKey(req.getClientPublicKeyB64()),req.getEmail());
|
||||
out.put("challengeId",state.challengeId()); out.put("expiresAtMs",state.expiresAtMs());
|
||||
}
|
||||
case "ConfirmFreeRegistration" -> {
|
||||
var value=w.confirm(req.getChallengeId(),req.getCode());
|
||||
out.put("fullLogin",value.fullLogin());out.put("dataItemId",value.dataItemId());
|
||||
out.put("publicationStatus",value.state());out.put("chainConfirmed",false);
|
||||
}
|
||||
case "GetRegistrationStatus" -> {
|
||||
var value=w.getStatus(req.getFullLogin());
|
||||
out.put("registrationStatus",value.status());
|
||||
out.put("dataItemId",value.dataItemId()==null?"":value.dataItemId());
|
||||
out.put("publicationStatus",value.publicationStatus()==null?"":value.publicationStatus());
|
||||
// Публичная историческая часть сертификата для бесплатного → Solana upgrade.
|
||||
if("FREE_CERT".equals(value.status())) {
|
||||
out.put("innerSignature64B64",value.innerSignature64B64()==null?"":value.innerSignature64B64());
|
||||
out.put("rootPublicKey",value.rootPublicKey()==null?"":value.rootPublicKey());
|
||||
out.put("blockchainPublicKey",value.blockchainPublicKey()==null?"":value.blockchainPublicKey());
|
||||
out.put("clientPublicKey",value.clientPublicKey()==null?"":value.clientPublicKey());
|
||||
out.put("defaultServerLogin",value.defaultServerLogin()==null?"":value.defaultServerLogin());
|
||||
}
|
||||
}
|
||||
default -> throw new IllegalArgumentException("Unknown registration operation");
|
||||
}
|
||||
return out;
|
||||
}
|
||||
private static byte[] decodeKey(String base64) {
|
||||
if(base64==null || base64.length()>64)throw new IllegalArgumentException("Invalid public key");
|
||||
byte[] decoded=Base64.getDecoder().decode(base64);
|
||||
if(decoded.length!=32)throw new IllegalArgumentException("Invalid public key length");
|
||||
return decoded;
|
||||
}
|
||||
private static DataSource dbSource() {
|
||||
return new DataSource() {
|
||||
@Override public Connection getConnection() throws SQLException {return DbController.getInstance().getConnection();}
|
||||
@Override public Connection getConnection(String u,String p) throws SQLException {throw new SQLFeatureNotSupportedException();}
|
||||
@Override public PrintWriter getLogWriter(){return null;}
|
||||
@Override public void setLogWriter(PrintWriter p){}
|
||||
@Override public void setLoginTimeout(int t){}
|
||||
@Override public int getLoginTimeout(){return 0;}
|
||||
@Override public Logger getParentLogger(){return Logger.getGlobal();}
|
||||
@Override public <T> T unwrap(Class<T> type) throws SQLException {throw new SQLFeatureNotSupportedException();}
|
||||
@Override public boolean isWrapperFor(Class<?> type){return false;}
|
||||
};
|
||||
}
|
||||
}
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
package server.archive.registration;
|
||||
|
||||
import blockchain.Ans104DataItem;
|
||||
import blockchain.ShineProtocolTags;
|
||||
import utils.crypto.Ed25519Util;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.text.Normalizer;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/** Строгое формирование и проверка двух подписей нового независимого DataItem `t=reg`. */
|
||||
public final class FreeRegistrationCertificateV1 {
|
||||
private static final byte[] DOMAIN = "SHiNE_FREE_REG_V1\0".getBytes(StandardCharsets.UTF_8);
|
||||
private static final List<Ans104DataItem.Tag> TAGS = List.of(
|
||||
new Ans104DataItem.Tag(ShineProtocolTags.APP_TAG_NAME, ShineProtocolTags.APP_TAG_VALUE),
|
||||
new Ans104DataItem.Tag("t", "reg"),
|
||||
new Ans104DataItem.Tag("Protocol", "SHiNE-Free-Registration-V1"));
|
||||
|
||||
public record Verified(String login, byte[] rootKey, byte[] blockchainKey,
|
||||
byte[] clientKey, byte[] innerSignature,
|
||||
byte[] emailSha256, byte[] owner32, String idBase64Url) {}
|
||||
|
||||
private FreeRegistrationCertificateV1() {}
|
||||
|
||||
public static String validateLogin(String login) {
|
||||
if (login == null || !login.matches("[A-Za-z0-9_]+@[a-z0-9_]+") || login.length() > 30)
|
||||
throw new IllegalArgumentException("Full login must use ASCII letters (any local-part case), lowercase namespace, <= 30 bytes");
|
||||
return login;
|
||||
}
|
||||
|
||||
public static byte[] identityFields(String login, byte[] root32, byte[] blockchain32, byte[] client32) {
|
||||
byte[] name = validateLogin(login).getBytes(StandardCharsets.UTF_8);
|
||||
require32(root32); require32(blockchain32); require32(client32);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
out.write(1); out.write(0); out.write(name.length); out.writeBytes(name);
|
||||
out.writeBytes(root32); out.writeBytes(blockchain32); out.writeBytes(client32);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
public static byte[] innerMessage(byte[] identity) {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
out.writeBytes(DOMAIN); out.writeBytes(identity);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
public static String normalizeEmail(String email) {
|
||||
if (email == null) throw new IllegalArgumentException("Email is required");
|
||||
String value = Normalizer.normalize(email.strip(), Normalizer.Form.NFC).toLowerCase(Locale.ROOT);
|
||||
if (value.isBlank() || value.indexOf('@') < 1 || value.contains(" "))
|
||||
throw new IllegalArgumentException("Invalid email");
|
||||
return value;
|
||||
}
|
||||
|
||||
public static byte[] emailHash(String email) {
|
||||
return sha256(normalizeEmail(email).getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
public static byte[] issue(String login, byte[] root32, byte[] blockchain32,
|
||||
byte[] client32, String email, RegistrarSigner signer) {
|
||||
byte[] fields = identityFields(login, root32, blockchain32, client32);
|
||||
byte[] inner = signer.sign(innerMessage(fields));
|
||||
if (inner.length != 64) throw new IllegalStateException("Invalid inner signature length");
|
||||
ByteArrayOutputStream payload = new ByteArrayOutputStream();
|
||||
payload.writeBytes(fields);
|
||||
payload.writeBytes(inner);
|
||||
payload.writeBytes(emailHash(email));
|
||||
byte[] data = payload.toByteArray();
|
||||
byte[] owner = signer.publicKey32();
|
||||
require32(owner);
|
||||
byte[] outer = signer.sign(Ans104DataItem.buildSigningMessage(owner, TAGS, data));
|
||||
byte[] raw = Ans104DataItem.serializeEd25519(outer, owner, TAGS, data);
|
||||
verify(raw, owner);
|
||||
return raw;
|
||||
}
|
||||
|
||||
public static Verified verify(byte[] signedDataItem, byte[] registrarPublicKey32) {
|
||||
require32(registrarPublicKey32);
|
||||
Ans104DataItem item = new Ans104DataItem(signedDataItem);
|
||||
if (!item.ownerEquals(registrarPublicKey32) || !item.verifySignature())
|
||||
throw new IllegalArgumentException("Invalid outer ANS-104 registrar signature");
|
||||
if (!item.tags().equals(TAGS) || item.target().length != 0 || item.anchor().length != 0)
|
||||
throw new IllegalArgumentException("Unexpected registration tags/target/anchor");
|
||||
byte[] data = item.data();
|
||||
if (data.length < 3 + 96 + 64 + 32 || data[0] != 1 || data[1] != 0)
|
||||
throw new IllegalArgumentException("Invalid certificate version/length");
|
||||
int len = data[2] & 0xff;
|
||||
if (data.length != 3 + len + 96 + 64 + 32)
|
||||
throw new IllegalArgumentException("Invalid certificate byte length");
|
||||
String login = validateLogin(new String(data, 3, len, StandardCharsets.UTF_8));
|
||||
int offset = 3 + len;
|
||||
byte[] root = Arrays.copyOfRange(data, offset, offset+32); offset += 32;
|
||||
byte[] blockchain = Arrays.copyOfRange(data, offset, offset+32); offset += 32;
|
||||
byte[] client = Arrays.copyOfRange(data, offset, offset+32); offset += 32;
|
||||
byte[] innerSig = Arrays.copyOfRange(data, offset, offset+64); offset += 64;
|
||||
byte[] emailSha = Arrays.copyOfRange(data, offset, offset+32);
|
||||
byte[] fields = Arrays.copyOfRange(data, 0, 3 + len + 96);
|
||||
if (!Ed25519Util.verify(innerMessage(fields), innerSig, registrarPublicKey32))
|
||||
throw new IllegalArgumentException("Invalid inner certificate signature");
|
||||
return new Verified(login, root, blockchain, client, innerSig, emailSha,
|
||||
item.owner32(), Base64.getUrlEncoder().withoutPadding().encodeToString(item.id32()));
|
||||
}
|
||||
|
||||
private static void require32(byte[] key) {
|
||||
if (key == null || key.length != 32) throw new IllegalArgumentException("Expected 32-byte public key");
|
||||
}
|
||||
private static byte[] sha256(byte[] data) {
|
||||
try { return MessageDigest.getInstance("SHA-256").digest(data); }
|
||||
catch (Exception e) { throw new IllegalStateException("SHA-256 unavailable", e); }
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package server.archive.registration;
|
||||
|
||||
import sync.util.Base58Util;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.Arrays;
|
||||
|
||||
/** Выдача (без публикации). Stage 8 публикует через Turbo и импортирует тот же raw DataItem. */
|
||||
public final class FreeRegistrationIssuer {
|
||||
private final DataSource db;
|
||||
private final RegistrarSigner signer;
|
||||
private final boolean allowEmailReuse;
|
||||
|
||||
public FreeRegistrationIssuer(DataSource db, RegistrarSigner signer) {
|
||||
this(db, signer, false);
|
||||
}
|
||||
|
||||
public FreeRegistrationIssuer(DataSource db, RegistrarSigner signer, boolean allowEmailReuse) {
|
||||
this.db = java.util.Objects.requireNonNull(db);
|
||||
this.signer = java.util.Objects.requireNonNull(signer);
|
||||
this.allowEmailReuse = allowEmailReuse;
|
||||
}
|
||||
|
||||
/** Выдаётся только после успешной проверки challenge в FreeRegistrationWorkflow. */
|
||||
synchronized byte[] issue(String fullLogin, byte[] root32, byte[] blockchain32,
|
||||
byte[] client32, String email) throws Exception {
|
||||
FreeRegistrationCertificateV1.validateLogin(fullLogin);
|
||||
String namespace = fullLogin.substring(fullLogin.indexOf('@') + 1);
|
||||
byte[] expectedEmail = FreeRegistrationCertificateV1.emailHash(email);
|
||||
try (Connection conn = db.getConnection()) {
|
||||
conn.setReadOnly(true);
|
||||
String sql = """
|
||||
SELECT namespace_type, min_localpart_len, registrar_pubkey, default_server_login
|
||||
FROM shine_namespace_current WHERE namespace = ?
|
||||
""";
|
||||
try (PreparedStatement statement = conn.prepareStatement(sql)) {
|
||||
statement.setString(1, namespace);
|
||||
try (ResultSet rs = statement.executeQuery()) {
|
||||
if (!rs.next() || rs.getInt("namespace_type") != 2 ||
|
||||
fullLogin.indexOf('@') < rs.getInt("min_localpart_len"))
|
||||
throw new IllegalArgumentException("Free namespace is unavailable");
|
||||
byte[] nsPub = Base58Util.decode(rs.getString("registrar_pubkey"));
|
||||
if (!Arrays.equals(nsPub, signer.publicKey32()))
|
||||
throw new IllegalStateException("Registrar public key does not match on-chain namespace");
|
||||
String server = rs.getString("default_server_login");
|
||||
if (server == null || server.isBlank())
|
||||
throw new IllegalStateException("Free namespace server is not configured");
|
||||
}
|
||||
}
|
||||
try (PreparedStatement ps = conn.prepareStatement(
|
||||
"SELECT 1 FROM user_identity_current WHERE normalized_login = ?")) {
|
||||
ps.setString(1, fullLogin.toLowerCase(java.util.Locale.ROOT));
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) throw new IllegalArgumentException("Login is already registered");
|
||||
}
|
||||
}
|
||||
if (!allowEmailReuse) {
|
||||
try (PreparedStatement ps = conn.prepareStatement(
|
||||
"SELECT 1 FROM free_registration_certificate WHERE email_sha256 = ?")) {
|
||||
ps.setBytes(1, expectedEmail);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) throw new IllegalArgumentException("Email hash already registered");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Это только подписанный артефакт, не активная регистрация. Перед Turbo
|
||||
// и локальным импортом обязательно повторять уникальность в транзакции БД.
|
||||
return FreeRegistrationCertificateV1.issue(fullLogin, root32, blockchain32, client32, email, signer);
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package server.archive.registration;
|
||||
|
||||
import server.archive.TurboDataItemUploader;
|
||||
import blockchain.Ans104DataItem;
|
||||
|
||||
/** Отдельный транспорт регистрации. Публикация не является пользовательским AddBlock. */
|
||||
public final class FreeRegistrationPublisher {
|
||||
private final FreeRegistrationIssuer issuer;
|
||||
private final TurboDataItemUploader uploader;
|
||||
private final VerifiedFreeRegistrationImporter importer;
|
||||
|
||||
public FreeRegistrationPublisher(FreeRegistrationIssuer issuer, TurboDataItemUploader uploader,
|
||||
VerifiedFreeRegistrationImporter importer) {
|
||||
this.issuer = java.util.Objects.requireNonNull(issuer);
|
||||
this.uploader = java.util.Objects.requireNonNull(uploader);
|
||||
this.importer = java.util.Objects.requireNonNull(importer);
|
||||
}
|
||||
/** Для повтора ПОСЛЕ потери ACK передавайте точно те же signedBytes. */
|
||||
public VerifiedFreeRegistrationImporter.ImportResult publishSigned(byte[] signedBytes) throws Exception {
|
||||
Ans104DataItem parsed = new Ans104DataItem(signedBytes);
|
||||
importer.validateForUpload(signedBytes); // строго до HTTP/Turbo списания кредитов
|
||||
// Импорт на этой точке вызовет полную проверку обеих подписей и namespace
|
||||
// ПОСЛЕ успешного ACK Turbo, а не до окончательного подтверждения Arweave.
|
||||
TurboDataItemUploader.UploadResult acknowledged = uploader.upload(signedBytes, parsed.id32());
|
||||
String expectedId = java.util.Base64.getUrlEncoder().withoutPadding().encodeToString(parsed.id32());
|
||||
if (!expectedId.equals(acknowledged.dataItemId()))
|
||||
throw new IllegalStateException("Unexpected Turbo DataItem id");
|
||||
return importer.importSigned(signedBytes, VerifiedFreeRegistrationImporter.Provenance.TURBO_ACCEPTED);
|
||||
}
|
||||
}
|
||||
+250
@@ -0,0 +1,250 @@
|
||||
package server.archive.registration;
|
||||
|
||||
import shine.db.DbController;
|
||||
import java.security.SecureRandom;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/** Внутренний state machine публичных операций регистрации v2. */
|
||||
public final class FreeRegistrationWorkflow {
|
||||
public record NamespaceInfo(String namespace, int type, int minLoginLength, String priceLamports, String serverLogin) {}
|
||||
public record Availability(boolean nameFree, boolean emailHashFree) {}
|
||||
public record LoginCheck(boolean loginAvailable, int namespaceType, int minLocalLength, String registrationFeeLamports) {}
|
||||
public record EmailCheck(boolean emailAvailable, String emailStatus) {}
|
||||
public record Started(String challengeId, long expiresAtMs) {}
|
||||
public record Completed(String fullLogin, String dataItemId, String state, boolean alreadyImported) {}
|
||||
public record State(String status, String dataItemId, String publicationStatus, String innerSignature64B64,
|
||||
String rootPublicKey, String blockchainPublicKey, String clientPublicKey,
|
||||
String defaultServerLogin) {}
|
||||
|
||||
private record Draft(String login, byte[] root, byte[] blockchain, byte[] client, String email,
|
||||
long expiresAtMs, byte[] codeHash, int attempts, boolean delivered,
|
||||
boolean verified, byte[] signedBytes) {
|
||||
Draft update(int attempts, boolean delivered, boolean verified, byte[] signedBytes) {
|
||||
return new Draft(login, root, blockchain, client, email, expiresAtMs,
|
||||
codeHash, attempts, delivered, verified, signedBytes);
|
||||
}
|
||||
}
|
||||
private final Map<String, Draft> pending = new ConcurrentHashMap<>();
|
||||
// Не храним в БД почтовые адреса, пока регистрация не завершилась.
|
||||
private final Map<String, Long> recentEmails = new HashMap<>();
|
||||
private final Map<String, Long> recentLogins = new HashMap<>();
|
||||
private final java.util.ArrayDeque<Long> globalSendTimes = new java.util.ArrayDeque<>();
|
||||
private static final long TTL_MS = Duration.ofHours(1).toMillis();
|
||||
private static final long EMAIL_COOLDOWN_MS = Duration.ofMinutes(1).toMillis();
|
||||
private static final long GLOBAL_WINDOW_MS = Duration.ofHours(1).toMillis();
|
||||
private static final int MAX_EMAILS_PER_HOUR = 60;
|
||||
private static final int MAX_ATTEMPTS = 5;
|
||||
private static final int MAX_PENDING = 512;
|
||||
private static final SecureRandom RANDOM = new SecureRandom();
|
||||
private final FreeRegistrationIssuer issuer;
|
||||
private final FreeRegistrationPublisher publisher;
|
||||
private final RegistrationEmailSender emailSender;
|
||||
private final boolean allowTestCode;
|
||||
private final boolean allowEmailReuse;
|
||||
private final AllowedEmailDomains allowedEmailDomains;
|
||||
|
||||
public FreeRegistrationWorkflow(FreeRegistrationIssuer issuer, FreeRegistrationPublisher publisher,
|
||||
RegistrationEmailSender emailSender, boolean allowTestCode, boolean allowEmailReuse,
|
||||
AllowedEmailDomains allowedEmailDomains) {
|
||||
if ((issuer == null) != (publisher == null)) throw new IllegalArgumentException("Signer/publisher mismatch");
|
||||
if (issuer != null && emailSender == null) throw new IllegalArgumentException("Email sender is required");
|
||||
if (emailSender != null && allowedEmailDomains == null)
|
||||
throw new IllegalArgumentException("Allowed email domain list is required");
|
||||
this.allowedEmailDomains = allowedEmailDomains;
|
||||
this.issuer = issuer;
|
||||
this.publisher = publisher;
|
||||
this.emailSender = emailSender;
|
||||
this.allowTestCode = allowTestCode;
|
||||
this.allowEmailReuse = allowEmailReuse;
|
||||
}
|
||||
public FreeRegistrationWorkflow(FreeRegistrationIssuer issuer, FreeRegistrationPublisher publisher,
|
||||
RegistrationEmailSender emailSender, boolean allowTestCode, boolean allowEmailReuse) {
|
||||
this(issuer, publisher, emailSender, allowTestCode, allowEmailReuse,
|
||||
emailSender == null ? null : loadDefaultEmailDomains());
|
||||
}
|
||||
private static AllowedEmailDomains loadDefaultEmailDomains() {
|
||||
try {
|
||||
return AllowedEmailDomains.load(java.nio.file.Path.of("allowed-email-domains.txt"));
|
||||
} catch (java.io.IOException e) {
|
||||
throw new IllegalStateException("Cannot load allowed email domains", e);
|
||||
}
|
||||
}
|
||||
public FreeRegistrationWorkflow(FreeRegistrationIssuer issuer, FreeRegistrationPublisher publisher,
|
||||
RegistrationEmailSender emailSender, boolean allowTestCode) {
|
||||
this(issuer, publisher, emailSender, allowTestCode, false);
|
||||
}
|
||||
public FreeRegistrationWorkflow(FreeRegistrationIssuer issuer, FreeRegistrationPublisher publisher,
|
||||
RegistrationEmailSender emailSender) {
|
||||
this(issuer, publisher, emailSender, false);
|
||||
}
|
||||
public FreeRegistrationWorkflow(FreeRegistrationIssuer issuer, FreeRegistrationPublisher publisher) {
|
||||
this(issuer, publisher, null);
|
||||
}
|
||||
public List<NamespaceInfo> listNamespaces() throws Exception {
|
||||
List<NamespaceInfo> result = new ArrayList<>();
|
||||
try (Connection c = DbController.getInstance().getConnection();
|
||||
PreparedStatement ps = c.prepareStatement("""
|
||||
SELECT namespace,namespace_type,min_localpart_len,registration_fee_lamports,default_server_login
|
||||
FROM shine_namespace_current ORDER BY namespace
|
||||
"""); ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) result.add(new NamespaceInfo(rs.getString(1),rs.getInt(2),rs.getInt(3),
|
||||
rs.getString(4),rs.getString(5)));
|
||||
}
|
||||
return List.copyOf(result);
|
||||
}
|
||||
public LoginCheck checkLogin(String login) throws Exception {
|
||||
FreeRegistrationCertificateV1.validateLogin(login);
|
||||
String namespace=login.substring(login.indexOf('@')+1);
|
||||
try (Connection c=DbController.getInstance().getConnection()) {
|
||||
int type, minimum; String fee;
|
||||
try (PreparedStatement p=c.prepareStatement("SELECT namespace_type,min_localpart_len,registration_fee_lamports FROM shine_namespace_current WHERE namespace=?")) {
|
||||
p.setString(1,namespace);
|
||||
try (ResultSet rs=p.executeQuery()) {
|
||||
if (!rs.next()) throw new IllegalArgumentException("Namespace is unavailable");
|
||||
type=rs.getInt(1); minimum=rs.getInt(2); fee=rs.getString(3);
|
||||
if (login.indexOf('@') < minimum) throw new IllegalArgumentException("Login shorter than namespace minimum");
|
||||
}
|
||||
}
|
||||
try (PreparedStatement p=c.prepareStatement("SELECT 1 FROM user_identity_current WHERE normalized_login=?")) {
|
||||
p.setString(1, login.toLowerCase(java.util.Locale.ROOT));
|
||||
try (ResultSet rs=p.executeQuery()) { return new LoginCheck(!rs.next(),type,minimum,fee); }
|
||||
}
|
||||
}
|
||||
}
|
||||
public EmailCheck checkEmail(String email) throws Exception {
|
||||
// До запроса к БД: отсутствие разрешения не должно раскрывать занятость email.
|
||||
String normalizedEmail = FreeRegistrationCertificateV1.normalizeEmail(email);
|
||||
if (allowedEmailDomains == null || !allowedEmailDomains.permits(normalizedEmail))
|
||||
return new EmailCheck(false, "DOMAIN_NOT_ALLOWED");
|
||||
byte[] hash=FreeRegistrationCertificateV1.emailHash(normalizedEmail);
|
||||
if (allowEmailReuse) return new EmailCheck(true, "AVAILABLE");
|
||||
try (Connection c=DbController.getInstance().getConnection();
|
||||
PreparedStatement p=c.prepareStatement("SELECT 1 FROM free_registration_certificate WHERE email_sha256=?")) {
|
||||
p.setBytes(1,hash);
|
||||
try(ResultSet rs=p.executeQuery()) { return rs.next() ? new EmailCheck(false, "ALREADY_REGISTERED")
|
||||
: new EmailCheck(true, "AVAILABLE"); }
|
||||
}
|
||||
}
|
||||
/** Устаревший внутренний helper: внешний API использует две отдельные операции. */
|
||||
public Availability availability(String login,String email) throws Exception {
|
||||
return new Availability(checkLogin(login).loginAvailable(),checkEmail(email).emailAvailable());
|
||||
}
|
||||
public Started start(String login, byte[] root, byte[] blockchain, byte[] client, String email) throws Exception {
|
||||
if (issuer == null || publisher == null || emailSender == null)
|
||||
throw new IllegalStateException("Free registration is disabled");
|
||||
FreeRegistrationCertificateV1.identityFields(login,root,blockchain,client);
|
||||
LoginCheck checked = checkLogin(login);
|
||||
if (checked.namespaceType()!=2 || !checked.loginAvailable())
|
||||
throw new IllegalStateException("Login is unavailable");
|
||||
if (!checkEmail(email).emailAvailable()) throw new IllegalStateException("Email is unavailable");
|
||||
String cleanEmail=FreeRegistrationCertificateV1.normalizeEmail(email);
|
||||
String normalizedLogin=login.toLowerCase(java.util.Locale.ROOT);
|
||||
byte[] id=new byte[32]; RANDOM.nextBytes(id);
|
||||
String challenge=Base64.getUrlEncoder().withoutPadding().encodeToString(id);
|
||||
String code=RegistrationEmailCode.generate();
|
||||
long now=System.currentTimeMillis();
|
||||
long expires=now+TTL_MS;
|
||||
Draft draft=new Draft(login,root.clone(),blockchain.clone(),client.clone(),cleanEmail,
|
||||
expires,RegistrationEmailCode.hash(challenge,code),0,false,false,null);
|
||||
synchronized (this) {
|
||||
pending.entrySet().removeIf(e->e.getValue().expiresAtMs < now);
|
||||
recentEmails.entrySet().removeIf(e->e.getValue() < now-TTL_MS);
|
||||
recentLogins.entrySet().removeIf(e->e.getValue() < now-TTL_MS);
|
||||
while(!globalSendTimes.isEmpty() && globalSendTimes.peekFirst() < now-GLOBAL_WINDOW_MS)
|
||||
globalSendTimes.removeFirst();
|
||||
if (pending.size() >= MAX_PENDING) throw new IllegalStateException("Too many pending registrations");
|
||||
if (now-recentEmails.getOrDefault(cleanEmail,0L) < EMAIL_COOLDOWN_MS ||
|
||||
now-recentLogins.getOrDefault(normalizedLogin,0L) < EMAIL_COOLDOWN_MS)
|
||||
throw new IllegalStateException("Please wait before requesting another code");
|
||||
if (globalSendTimes.size() >= MAX_EMAILS_PER_HOUR)
|
||||
throw new IllegalStateException("Email verification temporarily limited");
|
||||
recentEmails.put(cleanEmail,now);
|
||||
recentLogins.put(normalizedLogin,now);
|
||||
globalSendTimes.addLast(now);
|
||||
pending.put(challenge,draft);
|
||||
}
|
||||
try {
|
||||
// Не блокируем monitor workflow на время HTTP-запроса к Resend.
|
||||
emailSender.sendVerificationCode(cleanEmail,code);
|
||||
} catch (Exception e) {
|
||||
synchronized (this) {
|
||||
pending.remove(challenge);
|
||||
recentEmails.remove(cleanEmail,now);
|
||||
recentLogins.remove(normalizedLogin,now);
|
||||
globalSendTimes.removeFirstOccurrence(now);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
synchronized (this) {
|
||||
// Новый успешно отправленный код заменяет старые заявки на тот же login/email.
|
||||
pending.entrySet().removeIf(e -> !e.getKey().equals(challenge) &&
|
||||
(e.getValue().email.equals(cleanEmail) ||
|
||||
e.getValue().login.equalsIgnoreCase(login)));
|
||||
pending.computeIfPresent(challenge,(key,d)->d.update(d.attempts,true,d.verified,d.signedBytes));
|
||||
}
|
||||
return new Started(challenge,expires);
|
||||
}
|
||||
boolean isValidVerificationCode(byte[] expectedHash, String challenge, String code) {
|
||||
return (allowTestCode && "777777".equals(code))
|
||||
|| RegistrationEmailCode.matches(expectedHash, challenge, code);
|
||||
}
|
||||
|
||||
public synchronized Completed confirm(String challenge, String code) throws Exception {
|
||||
if (publisher == null) throw new IllegalStateException("Free registration disabled");
|
||||
if (challenge == null || challenge.isBlank())
|
||||
throw new IllegalArgumentException("Registration challenge missing");
|
||||
Draft d=pending.get(challenge);
|
||||
if (d==null || d.expiresAtMs < System.currentTimeMillis()) {
|
||||
pending.remove(challenge); throw new IllegalArgumentException("Registration challenge missing or expired");
|
||||
}
|
||||
if (!d.delivered) throw new IllegalStateException("Verification email is not delivered yet");
|
||||
if (!d.verified) {
|
||||
if (!isValidVerificationCode(d.codeHash, challenge, code)) {
|
||||
if (d.attempts+1 >= MAX_ATTEMPTS) pending.remove(challenge);
|
||||
else pending.put(challenge,d.update(d.attempts+1,true,false,d.signedBytes));
|
||||
throw new IllegalArgumentException("Invalid email code");
|
||||
}
|
||||
// Если Turbo не ответит, подтверждённая заявка может быть повторена до TTL.
|
||||
d=d.update(d.attempts,true,true,d.signedBytes);
|
||||
pending.put(challenge,d);
|
||||
}
|
||||
byte[] signed=d.signedBytes;
|
||||
if(signed==null) {
|
||||
signed=issuer.issue(d.login,d.root,d.blockchain,d.client,d.email);
|
||||
d=d.update(d.attempts,true,true,signed);
|
||||
pending.put(challenge,d);
|
||||
}
|
||||
VerifiedFreeRegistrationImporter.ImportResult result=publisher.publishSigned(signed);
|
||||
// Повтор использует тот же подписанный DataItem, без смены ключей и адреса email.
|
||||
return new Completed(result.login(),result.dataItemId(),"TURBO_ACCEPTED",result.alreadyImported());
|
||||
}
|
||||
public State getStatus(String login) throws Exception {
|
||||
FreeRegistrationCertificateV1.validateLogin(login);
|
||||
try (Connection c=DbController.getInstance().getConnection();
|
||||
PreparedStatement p=c.prepareStatement("""
|
||||
SELECT i.registration_status,i.free_data_item_id,f.publication_status,f.inner_signature,
|
||||
i.root_key,i.blockchain_key,i.client_key,n.default_server_login
|
||||
FROM user_identity_current i
|
||||
LEFT JOIN free_registration_certificate f ON f.data_item_id=i.free_data_item_id
|
||||
LEFT JOIN shine_namespace_current n ON n.namespace=i.namespace
|
||||
WHERE i.normalized_login=?
|
||||
""")) {
|
||||
p.setString(1,login.toLowerCase(java.util.Locale.ROOT));
|
||||
try (ResultSet r=p.executeQuery()) {
|
||||
return r.next()?new State(r.getString(1),r.getString(2),r.getString(3),
|
||||
r.getBytes(4)==null?null:Base64.getEncoder().encodeToString(r.getBytes(4)),
|
||||
r.getString(5),r.getString(6),r.getString(7),r.getString(8))
|
||||
:new State("NOT_FOUND",null,null,null,null,null,null,null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package server.archive.registration;
|
||||
|
||||
import utils.crypto.Ed25519Util;
|
||||
import utils.config.AppConfig;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
|
||||
/** Временный signer. Файл вне Git: одно значение Base64 от 32-байтного Ed25519 seed. */
|
||||
public final class LocalRegistrarSigner implements RegistrarSigner {
|
||||
private final byte[] seed32;
|
||||
private final byte[] publicKey32;
|
||||
private boolean closed;
|
||||
|
||||
public LocalRegistrarSigner(byte[] seed) {
|
||||
if (seed == null || seed.length != 32) throw new IllegalArgumentException("Registrar seed must be 32 bytes");
|
||||
this.seed32 = seed.clone();
|
||||
this.publicKey32 = Ed25519Util.derivePublicKey(seed32);
|
||||
}
|
||||
|
||||
public static LocalRegistrarSigner fromConfig(AppConfig config) throws Exception {
|
||||
if (!config.getBoolean("freeRegistration.enabled", false))
|
||||
throw new IllegalStateException("Free registration is disabled");
|
||||
String path = config.getStringOrEmpty("freeRegistration.registrarSeedFile");
|
||||
if (path.isEmpty()) throw new IllegalStateException("Registrar key path is not configured");
|
||||
byte[] seed = Base64.getDecoder().decode(Files.readString(Path.of(path), StandardCharsets.UTF_8).trim());
|
||||
try { return new LocalRegistrarSigner(seed); }
|
||||
finally { Arrays.fill(seed, (byte) 0); }
|
||||
}
|
||||
|
||||
@Override public synchronized byte[] publicKey32() { return publicKey32.clone(); }
|
||||
@Override public synchronized byte[] sign(byte[] message) {
|
||||
if (closed) throw new IllegalStateException("Signer closed");
|
||||
return Ed25519Util.sign(message, seed32);
|
||||
}
|
||||
@Override public synchronized void close() {
|
||||
Arrays.fill(seed32, (byte) 0);
|
||||
closed = true;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package server.archive.registration;
|
||||
|
||||
/** Интерфейс приватного регистратора: private key не передаётся в бизнес-логику. */
|
||||
public interface RegistrarSigner extends AutoCloseable {
|
||||
byte[] publicKey32();
|
||||
byte[] sign(byte[] message);
|
||||
@Override default void close() {}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package server.archive.registration;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.HexFormat;
|
||||
|
||||
/** Одноразовый код не сохраняется в открытом виде и не попадает в журнал. */
|
||||
final class RegistrationEmailCode {
|
||||
private static final SecureRandom RANDOM = new SecureRandom();
|
||||
private RegistrationEmailCode() {}
|
||||
|
||||
static String generate() {
|
||||
return String.format(java.util.Locale.ROOT, "%06d", RANDOM.nextInt(1_000_000));
|
||||
}
|
||||
|
||||
static byte[] hash(String challengeId, String code) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("SHA-256");
|
||||
return md.digest((challengeId + ":" + code).getBytes(StandardCharsets.UTF_8));
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new IllegalStateException("SHA-256 unavailable", e);
|
||||
}
|
||||
}
|
||||
|
||||
static boolean matches(byte[] expectedHash, String challengeId, String submitted) {
|
||||
return submitted != null && submitted.matches("[0-9]{6}") &&
|
||||
MessageDigest.isEqual(expectedHash, hash(challengeId, submitted));
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package server.archive.registration;
|
||||
|
||||
/** Отправка кода. Детали внешнего почтового провайдера не влияют на протокол регистрации. */
|
||||
@FunctionalInterface
|
||||
public interface RegistrationEmailSender {
|
||||
void sendVerificationCode(String recipient, String code) throws Exception;
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package server.archive.registration;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
import java.util.Map;
|
||||
|
||||
/** Транзакционная отправка через Resend: HTTP API, без SMTP и внешней Java SDK. */
|
||||
public final class ResendRegistrationEmailSender implements RegistrationEmailSender {
|
||||
private static final URI ENDPOINT = URI.create("https://api.resend.com/emails");
|
||||
private static final String SUBJECT = "SHiNE — код подтверждения email";
|
||||
private final HttpClient client = HttpClient.newBuilder()
|
||||
.connectTimeout(Duration.ofSeconds(5)).build();
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private final String apiKey;
|
||||
private final String from;
|
||||
|
||||
public ResendRegistrationEmailSender(String apiKey, String from) {
|
||||
if (apiKey == null || apiKey.isBlank())
|
||||
throw new IllegalStateException("Resend API key is required for free registration");
|
||||
if (from == null || from.isBlank())
|
||||
throw new IllegalStateException("Sender email is missing");
|
||||
this.apiKey = apiKey.trim();
|
||||
this.from = from.trim();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendVerificationCode(String recipient, String code) throws Exception {
|
||||
if (recipient == null || recipient.isBlank() || !code.matches("[0-9]{6}"))
|
||||
throw new IllegalArgumentException("Invalid email verification request");
|
||||
String text = "Добро пожаловать в SHiNE!\n\n"
|
||||
+ "Для завершения регистрации введите код подтверждения:\n\n"
|
||||
+ code + "\n\n"
|
||||
+ "Код действителен в течение 1 часа.\n\n"
|
||||
+ "Если вы не регистрировались в SHiNE, просто проигнорируйте это письмо.\n\n"
|
||||
+ "Команда SHiNE";
|
||||
String html = "<div style=\"font-family:Arial,sans-serif;max-width:520px;margin:auto;"
|
||||
+ "padding:24px;color:#1d2433;line-height:1.6\">"
|
||||
+ "<h2 style=\"margin:0 0 20px\">Добро пожаловать в SHiNE!</h2>"
|
||||
+ "<p>Для завершения регистрации введите код подтверждения:</p>"
|
||||
+ "<p style=\"font-size:32px;font-weight:700;letter-spacing:6px\">" + code + "</p>"
|
||||
+ "<p>Код действителен в течение 1 часа.</p>"
|
||||
+ "<p style=\"color:#5d6470\">Если вы не регистрировались в SHiNE, "
|
||||
+ "просто проигнорируйте это письмо.</p><p>Команда SHiNE</p></div>";
|
||||
String json = mapper.writeValueAsString(Map.of(
|
||||
"from", from, "to", new String[]{recipient}, "subject", SUBJECT,
|
||||
"text", text, "html", html));
|
||||
HttpRequest request = HttpRequest.newBuilder(ENDPOINT)
|
||||
.header("Authorization", "Bearer " + apiKey)
|
||||
.header("Content-Type", "application/json")
|
||||
.timeout(Duration.ofSeconds(12))
|
||||
.POST(HttpRequest.BodyPublishers.ofString(json))
|
||||
.build();
|
||||
// Тело ошибки не выводим: нельзя логировать email или токен провайдера.
|
||||
HttpResponse<Void> response = client.send(request, HttpResponse.BodyHandlers.discarding());
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300)
|
||||
throw new IllegalStateException("Resend delivery rejected (HTTP " + response.statusCode() + ")");
|
||||
}
|
||||
}
|
||||
+184
@@ -0,0 +1,184 @@
|
||||
package server.archive.registration;
|
||||
|
||||
import shine.db.DbController;
|
||||
import sync.util.Base58Util;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
|
||||
/** Только верифицированный сертификат. Локальная БД — проекция Arweave/Solana. */
|
||||
public final class VerifiedFreeRegistrationImporter {
|
||||
public enum Provenance { TURBO_ACCEPTED, ARWEAVE_CONFIRMED }
|
||||
public record ImportResult(String login, String dataItemId, boolean alreadyImported, Provenance provenance) {}
|
||||
private final boolean allowEmailReuse;
|
||||
|
||||
public VerifiedFreeRegistrationImporter() {
|
||||
this(false);
|
||||
}
|
||||
|
||||
public VerifiedFreeRegistrationImporter(boolean allowEmailReuse) {
|
||||
this.allowEmailReuse = allowEmailReuse;
|
||||
}
|
||||
|
||||
/** Предварительная проверка ДО Turbo: не расходовать кредиты на чужую/битую запись. */
|
||||
public FreeRegistrationCertificateV1.Verified validateForUpload(byte[] raw) throws Exception {
|
||||
if (raw == null || raw.length < 200 || raw.length > 16_384)
|
||||
throw new IllegalArgumentException("Invalid registration size");
|
||||
blockchain.Ans104DataItem item = new blockchain.Ans104DataItem(raw);
|
||||
String login = extractCanonicalLogin(item);
|
||||
try (Connection c = DbController.getInstance().getConnection()) {
|
||||
String namespace = login.substring(login.indexOf('@') + 1);
|
||||
var cert = FreeRegistrationCertificateV1.verify(raw, readRegistrar(c, namespace, login));
|
||||
try (PreparedStatement ps = c.prepareStatement(
|
||||
"SELECT free_data_item_id FROM user_identity_current WHERE normalized_login=?")) {
|
||||
ps.setString(1, login.toLowerCase(java.util.Locale.ROOT));
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next() && !cert.idBase64Url().equals(rs.getString(1)))
|
||||
throw new IllegalArgumentException("Login is already registered");
|
||||
}
|
||||
}
|
||||
if (!allowEmailReuse) {
|
||||
try (PreparedStatement ps = c.prepareStatement(
|
||||
"SELECT data_item_id FROM free_registration_certificate WHERE email_sha256=?")) {
|
||||
ps.setBytes(1, cert.emailSha256());
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next() && !cert.idBase64Url().equals(rs.getString(1)))
|
||||
throw new IllegalArgumentException("Email hash belongs to another account");
|
||||
}
|
||||
}
|
||||
}
|
||||
return cert;
|
||||
}
|
||||
}
|
||||
|
||||
/** Единая точка импорта: один и тот же raw ANS-104 из Turbo и Arweave. */
|
||||
public ImportResult importSigned(byte[] raw, Provenance provenance) throws Exception {
|
||||
if (raw == null || raw.length > 16_384 || raw.length < 200)
|
||||
throw new IllegalArgumentException("Invalid registration data item size");
|
||||
// Сначала разбираем/проверяем структурный контейнер, затем сверяем owner с on-chain namespace.
|
||||
blockchain.Ans104DataItem item = new blockchain.Ans104DataItem(raw);
|
||||
String login = extractCanonicalLogin(item);
|
||||
String namespace = login.substring(login.indexOf('@') + 1);
|
||||
try (Connection c = DbController.getInstance().getConnection()) {
|
||||
boolean oldAutoCommit = c.getAutoCommit();
|
||||
c.setAutoCommit(false);
|
||||
try {
|
||||
byte[] registrar = readRegistrar(c, namespace, login);
|
||||
FreeRegistrationCertificateV1.Verified cert = FreeRegistrationCertificateV1.verify(raw, registrar);
|
||||
String id = cert.idBase64Url();
|
||||
String publication = provenance == Provenance.ARWEAVE_CONFIRMED ? "ARWEAVE_CONFIRMED" : "TURBO_ACCEPTED";
|
||||
long now = System.currentTimeMillis();
|
||||
// Защита от коллизий и двойного импорта: имя и email захвачены индексами UNIQUE.
|
||||
try (PreparedStatement check = c.prepareStatement("""
|
||||
SELECT f.raw_signed_data_item, f.publication_status, u.root_key, u.client_key,
|
||||
u.blockchain_key FROM free_registration_certificate f
|
||||
JOIN user_identity_current u ON u.login=f.login
|
||||
WHERE f.data_item_id=? FOR UPDATE OF f
|
||||
""")) {
|
||||
check.setString(1, id);
|
||||
try (ResultSet r = check.executeQuery()) {
|
||||
if (r.next()) {
|
||||
if (!Arrays.equals(raw, r.getBytes(1)))
|
||||
throw new SQLException("DataItem id collision with different raw bytes");
|
||||
if (!r.getString(3).equals(Base58Util.encode(cert.rootKey())) ||
|
||||
!r.getString(4).equals(Base58Util.encode(cert.clientKey())) ||
|
||||
!r.getString(5).equals(Base58Util.encode(cert.blockchainKey())))
|
||||
throw new SQLException("Accepted identity keys conflict with certificate");
|
||||
if (provenance == Provenance.ARWEAVE_CONFIRMED) {
|
||||
try (PreparedStatement up = c.prepareStatement("""
|
||||
UPDATE free_registration_certificate SET publication_status='ARWEAVE_CONFIRMED',last_seen_at_ms=?
|
||||
WHERE data_item_id=?
|
||||
""")) {
|
||||
up.setLong(1, now); up.setString(2, id); up.executeUpdate();
|
||||
}
|
||||
}
|
||||
c.commit();
|
||||
return new ImportResult(login, id, true, provenance);
|
||||
}
|
||||
}
|
||||
}
|
||||
try (PreparedStatement insert = c.prepareStatement("""
|
||||
INSERT INTO user_identity_current (
|
||||
login, normalized_login, namespace, registration_source, registration_status,
|
||||
solana_pda_address, free_data_item_id, root_key, client_key, blockchain_key,
|
||||
blockchain_name, is_server, registered_at_ms, updated_at_ms)
|
||||
VALUES (?, ?, ?, 'ARWEAVE', 'FREE_CERT', NULL, ?, ?, ?, ?, ?, false, ?, ?)
|
||||
""")) {
|
||||
insert.setString(1, login); insert.setString(2, login.toLowerCase(java.util.Locale.ROOT)); insert.setString(3, namespace);
|
||||
insert.setString(4, id); insert.setString(5, Base58Util.encode(cert.rootKey()));
|
||||
insert.setString(6, Base58Util.encode(cert.clientKey()));
|
||||
insert.setString(7, Base58Util.encode(cert.blockchainKey()));
|
||||
insert.setString(8, login.toLowerCase(java.util.Locale.ROOT) + "-001"); insert.setLong(9, now); insert.setLong(10, now);
|
||||
insert.executeUpdate();
|
||||
}
|
||||
// blockchain_state — обязательная runtime-проекция новой цепочки.
|
||||
// Без неё первый пользовательский AddBlock (например, «О себе») будет
|
||||
// отклонён ещё до проверки подписи.
|
||||
try (PreparedStatement insert = c.prepareStatement("""
|
||||
INSERT INTO blockchain_state_store (
|
||||
login, fork_number, blockchain_key, size_limit, file_size_bytes,
|
||||
last_block_number, last_block_hash, updated_at_ms)
|
||||
VALUES (?, 1, ?, 100000, 0, -1, ?, ?)
|
||||
""")) {
|
||||
insert.setString(1, login);
|
||||
// blockchain_state — runtime-слой: его ключ всегда Base64(32),
|
||||
// как и у пользователей, импортированных из Solana PDA.
|
||||
insert.setString(2, Base64.getEncoder().encodeToString(cert.blockchainKey()));
|
||||
insert.setBytes(3, new byte[32]);
|
||||
insert.setLong(4, now);
|
||||
insert.executeUpdate();
|
||||
}
|
||||
try (PreparedStatement insert = c.prepareStatement("""
|
||||
INSERT INTO free_registration_certificate (
|
||||
data_item_id, login, namespace, email_sha256, inner_signature,
|
||||
raw_signed_data_item, publication_status, registered_at_ms, last_seen_at_ms)
|
||||
VALUES (?,?,?,?,?,?,?,?,?)
|
||||
""")) {
|
||||
insert.setString(1, id); insert.setString(2, login); insert.setString(3, namespace);
|
||||
insert.setBytes(4, cert.emailSha256()); insert.setBytes(5, cert.innerSignature());
|
||||
insert.setBytes(6, raw); insert.setString(7, publication);
|
||||
insert.setLong(8, now); insert.setLong(9, now); insert.executeUpdate();
|
||||
}
|
||||
c.commit();
|
||||
return new ImportResult(login, id, false, provenance);
|
||||
} catch (Exception e) {
|
||||
c.rollback(); throw e;
|
||||
} finally { c.setAutoCommit(oldAutoCommit); }
|
||||
}
|
||||
}
|
||||
|
||||
private static String extractCanonicalLogin(blockchain.Ans104DataItem item) {
|
||||
boolean marker = item.tags().contains(new blockchain.Ans104DataItem.Tag("t", "reg"));
|
||||
if (!marker) throw new IllegalArgumentException("Not a registration DataItem");
|
||||
byte[] bytes = item.data();
|
||||
if (bytes.length < 3 || bytes[0] != 1 || bytes[1] != 0) throw new IllegalArgumentException("Bad certificate header");
|
||||
int len = bytes[2] & 0xff;
|
||||
if (len < 3 || len > 30 || bytes.length < 3 + len + 96 + 64 + 32)
|
||||
throw new IllegalArgumentException("Invalid certificate login length");
|
||||
return FreeRegistrationCertificateV1.validateLogin(
|
||||
new String(bytes, 3, len, java.nio.charset.StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private static byte[] readRegistrar(Connection c, String namespace, String login) throws Exception {
|
||||
try (PreparedStatement ps = c.prepareStatement("""
|
||||
SELECT registrar_pubkey, min_localpart_len, default_server_login
|
||||
FROM shine_namespace_current WHERE namespace=? AND namespace_type=2
|
||||
""")) {
|
||||
ps.setString(1, namespace);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (!rs.next()) throw new IllegalStateException("Unknown/free namespace not yet synchronized");
|
||||
if (login.indexOf('@') < rs.getInt("min_localpart_len"))
|
||||
throw new IllegalArgumentException("Login shorter than namespace minimum");
|
||||
if (rs.getString("default_server_login") == null || rs.getString("default_server_login").isBlank())
|
||||
throw new IllegalArgumentException("Namespace has no access server");
|
||||
byte[] key = Base58Util.decode(rs.getString("registrar_pubkey"));
|
||||
if (key.length != 32) throw new IllegalArgumentException("Invalid registrar pubkey");
|
||||
return key;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
0200a9de19b3551ace117973b80d632503bf2a55f0aa2490deb1474a280b675fa01bb2ccb5890faadcb213d3882fb21a7af13702526a569f7807b1c8b6963c0be402174553b456dddfc6908ecab1c101fe6ab21e2baa0617795b7d43a63482993fd500000300000000000000390000000000000006064170701074657374353539310274067265671050726f746f636f6c345348694e452d467265652d526567697374726174696f6e2d563100010008626f62406672656503a107bff3ce10be1d70dd18e74bc09967e4d6309ba50d5f1ddc8664125531b829acbae141bccaf0b22e1a94d34d0bc7361e526d0bfe12c89794bc9322966dd72543b92ff1095511476adc8369db6ddc933665a11978dda1404ee1066ca9559de2edfcd8b1b7a1823f56b827ddfa8a3682793e036141b526467ae346e220d08757d0a878a2f722562e6d3132ef9df04b1ab9e2f65d72bb0f48c17b7ada3e5a0acbc605c49fd774ff4d9ea789d8f6bffa719e9774cb86f62d11fd244121150956
|
||||
@@ -0,0 +1,36 @@
|
||||
package server.archive;
|
||||
|
||||
import blockchain.Ans104DataItem;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import utils.crypto.Ed25519Util;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
final class Ans104BundleTest {
|
||||
@Test
|
||||
void writeReadAndFindPreserveExactDataItems() {
|
||||
byte[] a = item("a");
|
||||
byte[] b = item("b");
|
||||
byte[] bundle = Ans104Bundle.write(List.of(a, b));
|
||||
|
||||
List<Ans104Bundle.Entry> entries = Ans104Bundle.read(bundle, 10);
|
||||
assertEquals(2, entries.size());
|
||||
assertArrayEquals(a, entries.get(0).rawDataItem());
|
||||
assertArrayEquals(b, entries.get(1).rawDataItem());
|
||||
|
||||
byte[] bId = new Ans104DataItem(b).id32();
|
||||
assertArrayEquals(b, Ans104Bundle.find(bundle, bId, 10));
|
||||
}
|
||||
|
||||
private static byte[] item(String payload) {
|
||||
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(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);
|
||||
}
|
||||
}
|
||||
-34
@@ -1,34 +0,0 @@
|
||||
package server.archive;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Base64;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class ArchiveFileNamesTest {
|
||||
private static final long CREATED_AT = Instant.parse("2026-09-11T00:00:00Z").toEpochMilli();
|
||||
private static final ZoneId UTC = ZoneId.of("UTC");
|
||||
|
||||
@Test
|
||||
void usesTemporaryAndFinalArweaveNames() {
|
||||
byte[] tx = new byte[32];
|
||||
for (int i = 0; i < tx.length; i++) tx[i] = (byte) i;
|
||||
String txText = Base64.getUrlEncoder().withoutPadding().encodeToString(tx);
|
||||
|
||||
assertEquals("archive01.00001.11.09.26.tmp.SHiNE-archive",
|
||||
ArchiveFileNames.pendingFileName("archive01", 1, CREATED_AT, UTC));
|
||||
assertEquals("archive01.00001.11.09.26." + txText + ".SHiNE-archive",
|
||||
ArchiveFileNames.uploadedFileName("archive01", 1, CREATED_AT, UTC, tx));
|
||||
assertEquals("archive01.100000.11.09.26.tmp.SHiNE-archive",
|
||||
ArchiveFileNames.pendingFileName("archive01", 100000, CREATED_AT, UTC));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sanitizesLoginForFileSystemOnly() {
|
||||
assertEquals("archive_user.00002.11.09.26.tmp.SHiNE-archive",
|
||||
ArchiveFileNames.pendingFileName("archive/user", 2, CREATED_AT, UTC));
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package server.archive;
|
||||
|
||||
import server.archive.registration.*;
|
||||
import blockchain.Ans104DataItem;
|
||||
import blockchain.ShineProtocolTags;
|
||||
import utils.crypto.Ed25519Util;
|
||||
import java.util.Arrays;
|
||||
import java.util.HexFormat;
|
||||
|
||||
/** Standalone test (no legacy signed fixture): certificate issue/verify for CURRENT App tag. */
|
||||
public final class FreeRegistrationCertificateV1Check {
|
||||
static final HexFormat HEX = HexFormat.of();
|
||||
public static void main(String[] args) throws Exception {
|
||||
byte[] seed = Ed25519Util.generatePrivateKeyFromString("TEST-only-reg-v1-check");
|
||||
byte[] root = HEX.parseHex("03a107bff3ce10be1d70dd18e74bc09967e4d6309ba50d5f1ddc8664125531b8");
|
||||
byte[] chain = HEX.parseHex("29acbae141bccaf0b22e1a94d34d0bc7361e526d0bfe12c89794bc9322966dd7");
|
||||
byte[] client = HEX.parseHex("2543b92ff1095511476adc8369db6ddc933665a11978dda1404ee1066ca9559d");
|
||||
try (var signer = new LocalRegistrarSigner(seed)) {
|
||||
byte[] cert = FreeRegistrationCertificateV1.issue("SuperMan@free", root, chain, client,
|
||||
" Alice.Example+Tag@Example.COM ", signer);
|
||||
var item = new Ans104DataItem(cert);
|
||||
if (!item.hasTag(ShineProtocolTags.APP_TAG_NAME, ShineProtocolTags.APP_TAG_VALUE))
|
||||
throw new AssertionError("Current App tag missing");
|
||||
var result = FreeRegistrationCertificateV1.verify(cert, signer.publicKey32());
|
||||
if (!"SuperMan@free".equals(result.login()) || !Arrays.equals(result.rootKey(), root)
|
||||
|| !Arrays.equals(result.blockchainKey(), chain) || !Arrays.equals(result.clientKey(), client))
|
||||
throw new AssertionError("Certificate identity corrupted");
|
||||
if (!Arrays.equals(result.emailSha256(), HEX.parseHex(
|
||||
"cbc605c49fd774ff4d9ea789d8f6bffa719e9774cb86f62d11fd244121150956")))
|
||||
throw new AssertionError("Email normalization differs");
|
||||
byte[] bad = cert.clone(); bad[bad.length - 1] ^= 1;
|
||||
try { FreeRegistrationCertificateV1.verify(bad, signer.publicKey32());
|
||||
throw new AssertionError("Email bit flip accepted"); }
|
||||
catch (IllegalArgumentException expected) {}
|
||||
bad = cert.clone(); bad[bad.length - 40] ^= 1;
|
||||
try { FreeRegistrationCertificateV1.verify(bad, signer.publicKey32());
|
||||
throw new AssertionError("Inner signature tampering accepted"); }
|
||||
catch (IllegalArgumentException expected) {}
|
||||
}
|
||||
System.out.println("PASS: current App=" + ShineProtocolTags.APP_TAG_VALUE
|
||||
+ ", Java registrar inner+outer Ed25519, original case, email, tamper rejection");
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package server.archive;
|
||||
|
||||
import server.archive.registration.*;
|
||||
import javax.sql.DataSource;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HexFormat;
|
||||
|
||||
/** Только проверки без БД/сети; полное E2E требует PostgreSQL и Turbo. */
|
||||
public final class FreeRegistrationWorkflowLocalCheck {
|
||||
public static void main(String[] args) throws Exception {
|
||||
var signer = new RegistrarSigner() {
|
||||
public byte[] publicKey32() { return new byte[32]; }
|
||||
public byte[] sign(byte[] message) { return new byte[64]; }
|
||||
};
|
||||
DataSource noDb = new DataSource() {
|
||||
public java.sql.Connection getConnection() throws java.sql.SQLException { throw new java.sql.SQLException("No database in local test"); }
|
||||
public java.sql.Connection getConnection(String u, String p) throws java.sql.SQLException { return getConnection(); }
|
||||
public java.io.PrintWriter getLogWriter() {return null;}
|
||||
public void setLogWriter(java.io.PrintWriter x) {}
|
||||
public void setLoginTimeout(int x) {}
|
||||
public int getLoginTimeout() {return 0;}
|
||||
public java.util.logging.Logger getParentLogger() {return java.util.logging.Logger.getAnonymousLogger();}
|
||||
public <T> T unwrap(Class<T> iface) throws java.sql.SQLException {throw new java.sql.SQLException("unsupported");}
|
||||
public boolean isWrapperFor(Class<?> iface) {return false;}
|
||||
};
|
||||
var issuer = new FreeRegistrationIssuer(noDb, signer);
|
||||
var importer = new VerifiedFreeRegistrationImporter();
|
||||
var workflow = new FreeRegistrationWorkflow(issuer,
|
||||
new FreeRegistrationPublisher(issuer, new TurboDataItemUploader(ArweaveBlocksConfig.load()), importer),
|
||||
(email, code) -> { throw new AssertionError("No real email in an offline check"); },
|
||||
false, false, AllowedEmailDomains.load(Files.isRegularFile(Path.of("allowed-email-domains.txt"))
|
||||
? Path.of("allowed-email-domains.txt") : Path.of("../allowed-email-domains.txt")));
|
||||
try { workflow.confirm("unknown", "123456"); throw new AssertionError("Missing challenge accepted"); }
|
||||
catch (IllegalArgumentException expected) { }
|
||||
try { importer.importSigned(new byte[]{1,2,3}, VerifiedFreeRegistrationImporter.Provenance.TURBO_ACCEPTED);
|
||||
throw new AssertionError("Invalid bytes accepted"); }
|
||||
catch (IllegalArgumentException expected) { }
|
||||
System.out.println("PASS: workflow missing challenge + reject invalid signed bytes");
|
||||
}
|
||||
}
|
||||
-83
@@ -1,83 +0,0 @@
|
||||
package server.archive;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import shine.db.archive.ArchiveBigBlockRef;
|
||||
import shine.db.archive.ArchivePublishJobChain;
|
||||
import shine.db.entities.BlockEntry;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ShineArchiveWriterTest {
|
||||
@TempDir Path dir;
|
||||
|
||||
@Test
|
||||
void writesOneChunkWithAllRecordsAndOneBacklink() throws Exception {
|
||||
Path file = ArchiveFileNames.pendingFilePath(dir, "serverA", 2, 123456789L, ZoneId.of("UTC"));
|
||||
ArchiveBigBlockRef previous = new ArchiveBigBlockRef(1, new byte[32], new byte[32]);
|
||||
ArchivePublishJobChain chain = new ArchivePublishJobChain(
|
||||
1, "alice-001", 10, 11, new byte[32], new byte[32],
|
||||
1L, new byte[32], 100L, 55L, null, null);
|
||||
|
||||
ShineArchiveWriter.BuildResult result = new ShineArchiveWriter().build(
|
||||
file, "serverA", 2, 123456789L,
|
||||
List.of(previous), List.of(chain),
|
||||
Map.of("alice-001", List.of(block(10, new byte[]{1,2,3}), block(11, new byte[]{4,5}))),
|
||||
new byte[32], 4_000_000_000L);
|
||||
|
||||
assertArrayEquals(result.archiveHash(), ShineArchiveWriter.verifyLocalFile(file));
|
||||
assertEquals("serverA.00002.02.01.70.tmp.SHiNE-archive", file.getFileName().toString());
|
||||
|
||||
try (InputStream raw = java.nio.file.Files.newInputStream(file); DataInputStream in = new DataInputStream(raw)) {
|
||||
assertEquals("SHINE-ARCHIVE", new String(in.readNBytes(13), StandardCharsets.US_ASCII));
|
||||
assertEquals(1, in.readUnsignedByte());
|
||||
assertEquals(0, in.readUnsignedByte());
|
||||
long headerSize = Integer.toUnsignedLong(in.readInt());
|
||||
assertEquals(2, Integer.toUnsignedLong(in.readInt()));
|
||||
assertEquals(123456789L, in.readLong());
|
||||
int creatorLen = in.readUnsignedByte();
|
||||
assertEquals(ShineArchiveWriter.REFERENCES_FULL, in.readUnsignedByte());
|
||||
assertEquals(1, Integer.toUnsignedLong(in.readInt()));
|
||||
assertEquals(68, in.readUnsignedShort());
|
||||
assertEquals(0, Integer.toUnsignedLong(in.readInt()));
|
||||
assertEquals(1, Integer.toUnsignedLong(in.readInt()));
|
||||
assertEquals(2, Integer.toUnsignedLong(in.readInt()));
|
||||
assertEquals("serverA", new String(in.readNBytes(creatorLen), StandardCharsets.UTF_8));
|
||||
assertEquals(headerSize, 51L + creatorLen);
|
||||
|
||||
assertEquals(1, Integer.toUnsignedLong(in.readInt()));
|
||||
in.skipNBytes(64); // previous hash + txid
|
||||
|
||||
long chunkSize = Integer.toUnsignedLong(in.readInt());
|
||||
int nameLen = in.readUnsignedByte();
|
||||
assertEquals("alice-001", new String(in.readNBytes(nameLen), StandardCharsets.UTF_8));
|
||||
assertEquals(2, Integer.toUnsignedLong(in.readInt()));
|
||||
assertEquals(3, Integer.toUnsignedLong(in.readInt()));
|
||||
assertArrayEquals(new byte[]{1,2,3}, in.readNBytes(3));
|
||||
assertEquals(2, Integer.toUnsignedLong(in.readInt()));
|
||||
assertArrayEquals(new byte[]{4,5}, in.readNBytes(2));
|
||||
assertEquals(0, Integer.toUnsignedLong(in.readInt())); // previous ref index
|
||||
assertEquals(100, Integer.toUnsignedLong(in.readInt()));
|
||||
assertEquals(55, Integer.toUnsignedLong(in.readInt()));
|
||||
assertEquals(4L + 1 + nameLen + 4 + (4+3) + (4+2) + 12, chunkSize);
|
||||
}
|
||||
}
|
||||
|
||||
private static BlockEntry block(int number, byte[] bytes) {
|
||||
BlockEntry e = new BlockEntry();
|
||||
e.setBlockNumber(number);
|
||||
e.setBlockBytes(bytes);
|
||||
e.setBlockHash(new byte[32]);
|
||||
e.setBlockSignature(new byte[64]);
|
||||
return e;
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package server.archive.registration;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
/** Самостоятельный офлайн-тест: javac AllowedEmailDomains.java AllowedEmailDomainsCheck.java + java. */
|
||||
public final class AllowedEmailDomainsCheck {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Path dir = Files.createTempDirectory("shine-email-domains-test-");
|
||||
try {
|
||||
Path file = dir.resolve("allowed-email-domains.txt");
|
||||
Files.writeString(file, "# Test\n Gmail.com \nproton.me\n\n", StandardCharsets.UTF_8);
|
||||
AllowedEmailDomains allowed = AllowedEmailDomains.load(file);
|
||||
expect(allowed.permits("User@GMAIL.COM"), "case insensitive match");
|
||||
expect(allowed.permits(" user@proton.me "), "trimmed match");
|
||||
expect(!allowed.permits("user@gmail.com.evil.org"), "suffix bypass");
|
||||
expect(!allowed.permits("user@sub.gmail.com"), "subdomain bypass");
|
||||
expect(!allowed.permits("user@other.com"), "unknown domain");
|
||||
expect(!allowed.permits("user@@gmail.com"), "invalid multiple @");
|
||||
expect(!allowed.permits("@gmail.com"), "missing local part");
|
||||
expect(!allowed.permits(null), "null address");
|
||||
Files.writeString(file, "# no domains\n \n", StandardCharsets.UTF_8);
|
||||
expectLoadFailure(file, "empty file");
|
||||
Files.writeString(file, "*.gmail.com\n", StandardCharsets.UTF_8);
|
||||
expectLoadFailure(file, "wildcard not allowed");
|
||||
Files.writeString(file, "gmail.com\ninvalid_domain\n", StandardCharsets.UTF_8);
|
||||
expectLoadFailure(file, "invalid line not ignored");
|
||||
Files.delete(file);
|
||||
expectLoadFailure(file, "missing file");
|
||||
System.out.println("PASS: allowed email domains, strict matching, fail-closed");
|
||||
} finally {
|
||||
try (var files = Files.list(dir)) { files.forEach(path -> {try {Files.deleteIfExists(path);}catch(Exception ex){throw new RuntimeException(ex);}}); }
|
||||
Files.deleteIfExists(dir);
|
||||
}
|
||||
}
|
||||
|
||||
private static void expect(boolean condition, String scenario) {
|
||||
if (!condition) throw new AssertionError(scenario);
|
||||
}
|
||||
private static void expectLoadFailure(Path file, String scenario) throws Exception {
|
||||
try {
|
||||
AllowedEmailDomains.load(file);
|
||||
throw new AssertionError("Invalid allowlist accepted: " + scenario);
|
||||
} catch (java.io.IOException expected) { }
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package server.archive.registration;
|
||||
|
||||
/** Локальная проверка генератора и сверки кода без БД/HTTP. */
|
||||
public final class RegistrationEmailCodeCheck {
|
||||
public static void main(String[] args) {
|
||||
for (int i=0; i<1000; ++i) {
|
||||
String code = RegistrationEmailCode.generate();
|
||||
if (!code.matches("[0-9]{6}")) throw new AssertionError("Wrong length");
|
||||
byte[] hash = RegistrationEmailCode.hash("challenge-id", code);
|
||||
if (!RegistrationEmailCode.matches(hash, "challenge-id", code)) throw new AssertionError("Cannot verify");
|
||||
if (RegistrationEmailCode.matches(hash, "another-challenge", code)) throw new AssertionError("Challenge replay");
|
||||
if (RegistrationEmailCode.matches(hash, "challenge-id", "1111")) throw new AssertionError("Old demo code accepted");
|
||||
if (RegistrationEmailCode.matches(hash, "challenge-id", "bad")) throw new AssertionError("Bad code accepted");
|
||||
}
|
||||
System.out.println("PASS: email code generation and challenge binding");
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package server.archive.registration;
|
||||
|
||||
/** Offline проверка часового TTL на уровне UX отдельно; bypass должен быть строго opt-in. */
|
||||
public final class RegistrationEmailSettingsCheck {
|
||||
public static void main(String[] args) {
|
||||
byte[] digest = RegistrationEmailCode.hash("challenge", "123456");
|
||||
var safe = new FreeRegistrationWorkflow(null, null, null, false);
|
||||
var test = new FreeRegistrationWorkflow(null, null, null, true);
|
||||
if (safe.isValidVerificationCode(digest, "challenge", "777777"))
|
||||
throw new AssertionError("Cheat unexpectedly enabled by default");
|
||||
if (!test.isValidVerificationCode(digest, "challenge", "777777"))
|
||||
throw new AssertionError("Explicit test code not accepted");
|
||||
if (!safe.isValidVerificationCode(digest, "challenge", "123456"))
|
||||
throw new AssertionError("Real code rejected");
|
||||
if (test.isValidVerificationCode(digest, "challenge", "1111"))
|
||||
throw new AssertionError("Old demo code unexpectedly accepted");
|
||||
if (test.isValidVerificationCode(digest, "challenge", "777778"))
|
||||
throw new AssertionError("Invalid code unexpectedly accepted");
|
||||
System.out.println("PASS: real code and opt-in test code");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,363 @@
|
||||
package blockchain;
|
||||
|
||||
import utils.crypto.Ed25519Util;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Minimal ANS-104 DataItem parser/verifier used by SHiNE.
|
||||
*
|
||||
* SHiNE accepts only generic Ed25519 signature type 2:
|
||||
* uint16le signatureType
|
||||
* signature[64]
|
||||
* owner[32]
|
||||
* target_present[1] + target[32] when present
|
||||
* anchor_present[1] + anchor[32] when present
|
||||
* tags_count uint64le
|
||||
* tags_size uint64le
|
||||
* avro_tags[tags_size]
|
||||
* data[*]
|
||||
*
|
||||
* The signing message matches the deployed arbundles/Turbo DataItem implementation:
|
||||
* deepHash(["dataitem", "1", signatureType, owner, target, anchor, rawAvroTags, data]).
|
||||
*/
|
||||
public final class Ans104DataItem {
|
||||
public static final int SIGNATURE_TYPE_ED25519 = 2;
|
||||
public static final int SIGNATURE_LEN = 64;
|
||||
public static final int OWNER_LEN = 32;
|
||||
public static final int OPTIONAL_FIELD_LEN = 32;
|
||||
public static final int ID_LEN = 32;
|
||||
public static final int MAX_DATA_ITEM_BYTES = 8 * 1024 * 1024;
|
||||
public static final int MAX_TAGS_BYTES = 64 * 1024;
|
||||
public static final long MAX_TAGS_COUNT = 128;
|
||||
|
||||
public record Tag(String name, String value) {}
|
||||
|
||||
private final int signatureType;
|
||||
private final byte[] signature;
|
||||
private final byte[] owner;
|
||||
private final byte[] target;
|
||||
private final byte[] anchor;
|
||||
private final long tagsCount;
|
||||
private final byte[] rawTags;
|
||||
private final List<Tag> tags;
|
||||
private final byte[] data;
|
||||
private final byte[] rawBytes;
|
||||
private final byte[] id;
|
||||
|
||||
public Ans104DataItem(byte[] bytes) {
|
||||
Objects.requireNonNull(bytes, "bytes == null");
|
||||
if (bytes.length < 2 + SIGNATURE_LEN + OWNER_LEN + 1 + 1 + 8 + 8 + 1) {
|
||||
throw new IllegalArgumentException("ANS-104 DataItem too short: " + bytes.length);
|
||||
}
|
||||
if (bytes.length > MAX_DATA_ITEM_BYTES) {
|
||||
throw new IllegalArgumentException("ANS-104 DataItem too large: " + bytes.length);
|
||||
}
|
||||
|
||||
ByteBuffer bb = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
|
||||
this.signatureType = Short.toUnsignedInt(bb.getShort());
|
||||
if (signatureType != SIGNATURE_TYPE_ED25519) {
|
||||
throw new IllegalArgumentException("Unsupported ANS-104 signature type: " + signatureType + " (expected 2)");
|
||||
}
|
||||
|
||||
this.signature = readExact(bb, SIGNATURE_LEN, "signature");
|
||||
this.owner = readExact(bb, OWNER_LEN, "owner");
|
||||
this.target = readOptional32(bb, "target");
|
||||
this.anchor = readOptional32(bb, "anchor");
|
||||
|
||||
this.tagsCount = readUnsignedLongAsNonNegative(bb, "tags_count");
|
||||
long tagsSizeLong = readUnsignedLongAsNonNegative(bb, "tags_size");
|
||||
if (tagsCount > MAX_TAGS_COUNT) {
|
||||
throw new IllegalArgumentException("Too many ANS-104 tags: " + tagsCount);
|
||||
}
|
||||
if (tagsSizeLong > MAX_TAGS_BYTES || tagsSizeLong > bb.remaining()) {
|
||||
throw new IllegalArgumentException("Bad ANS-104 tags_size: " + tagsSizeLong + ", remaining=" + bb.remaining());
|
||||
}
|
||||
this.rawTags = readExact(bb, (int) tagsSizeLong, "tags");
|
||||
this.tags = Collections.unmodifiableList(parseAvroTags(rawTags, tagsCount));
|
||||
this.data = new byte[bb.remaining()];
|
||||
bb.get(this.data);
|
||||
this.rawBytes = Arrays.copyOf(bytes, bytes.length);
|
||||
this.id = sha256(signature);
|
||||
}
|
||||
|
||||
public int signatureType() { return signatureType; }
|
||||
public byte[] signature64() { return Arrays.copyOf(signature, signature.length); }
|
||||
public byte[] owner32() { return Arrays.copyOf(owner, owner.length); }
|
||||
public byte[] target() { return Arrays.copyOf(target, target.length); }
|
||||
public byte[] anchor() { return Arrays.copyOf(anchor, anchor.length); }
|
||||
public long tagsCount() { return tagsCount; }
|
||||
public byte[] rawTags() { return Arrays.copyOf(rawTags, rawTags.length); }
|
||||
public List<Tag> tags() { return tags; }
|
||||
public byte[] data() { return Arrays.copyOf(data, data.length); }
|
||||
public byte[] rawBytes() { return Arrays.copyOf(rawBytes, rawBytes.length); }
|
||||
public byte[] id32() { return Arrays.copyOf(id, id.length); }
|
||||
|
||||
public String tagValue(String name) {
|
||||
if (name == null) return null;
|
||||
for (Tag tag : tags) {
|
||||
if (name.equals(tag.name())) return tag.value();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean hasTag(String name, String value) {
|
||||
for (Tag tag : tags) {
|
||||
if (Objects.equals(name, tag.name()) && Objects.equals(value, tag.value())) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean verifySignature() {
|
||||
return Ed25519Util.verify(signingMessage(), signature, owner);
|
||||
}
|
||||
|
||||
public boolean ownerEquals(byte[] expectedOwner32) {
|
||||
return expectedOwner32 != null && expectedOwner32.length == OWNER_LEN && Arrays.equals(owner, expectedOwner32);
|
||||
}
|
||||
|
||||
public byte[] signingMessage() {
|
||||
return buildSigningMessage(owner, target, anchor, tags, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the exact ANS-104 signing message for generic Ed25519 DataItems.
|
||||
* Useful for SHiNE clients/tests that must sign a DataItem before serializing it.
|
||||
*/
|
||||
public static byte[] buildSigningMessage(byte[] owner32, List<Tag> tags, byte[] data) {
|
||||
return buildSigningMessage(owner32, new byte[0], new byte[0], tags, data);
|
||||
}
|
||||
|
||||
public static byte[] buildSigningMessage(byte[] owner32, byte[] target, byte[] anchor, List<Tag> tags, byte[] data) {
|
||||
requireLen(owner32, OWNER_LEN, "owner");
|
||||
byte[] safeTarget = target == null ? new byte[0] : Arrays.copyOf(target, target.length);
|
||||
byte[] safeAnchor = anchor == null ? new byte[0] : Arrays.copyOf(anchor, anchor.length);
|
||||
if (!(safeTarget.length == 0 || safeTarget.length == OPTIONAL_FIELD_LEN)) {
|
||||
throw new IllegalArgumentException("target must be empty or 32 bytes");
|
||||
}
|
||||
if (!(safeAnchor.length == 0 || safeAnchor.length == OPTIONAL_FIELD_LEN)) {
|
||||
throw new IllegalArgumentException("anchor must be empty or 32 bytes");
|
||||
}
|
||||
List<Tag> safeTags = validateTags(tags);
|
||||
byte[] safeData = data == null ? new byte[0] : Arrays.copyOf(data, data.length);
|
||||
|
||||
byte[] rawTags = serializeAvroTags(safeTags);
|
||||
return deepHash(List.of(
|
||||
utf8("dataitem"),
|
||||
utf8("1"),
|
||||
utf8(Integer.toString(SIGNATURE_TYPE_ED25519)),
|
||||
Arrays.copyOf(owner32, owner32.length),
|
||||
safeTarget,
|
||||
safeAnchor,
|
||||
rawTags,
|
||||
safeData
|
||||
));
|
||||
}
|
||||
|
||||
/** Serialize one SHiNE-ready ANS-104 DataItem with empty target/anchor. */
|
||||
public static byte[] serializeEd25519(byte[] signature64, byte[] owner32, List<Tag> tags, byte[] data) {
|
||||
requireLen(signature64, SIGNATURE_LEN, "signature");
|
||||
requireLen(owner32, OWNER_LEN, "owner");
|
||||
List<Tag> safeTags = validateTags(tags);
|
||||
byte[] rawTags = serializeAvroTags(safeTags);
|
||||
byte[] safeData = data == null ? new byte[0] : data;
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream(
|
||||
2 + SIGNATURE_LEN + OWNER_LEN + 1 + 1 + 8 + 8 + rawTags.length + safeData.length);
|
||||
writeU16LE(out, SIGNATURE_TYPE_ED25519);
|
||||
out.writeBytes(signature64);
|
||||
out.writeBytes(owner32);
|
||||
out.write(0); // target absent
|
||||
out.write(0); // anchor absent
|
||||
writeU64LE(out, safeTags.size());
|
||||
writeU64LE(out, rawTags.length);
|
||||
out.writeBytes(rawTags);
|
||||
out.writeBytes(safeData);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
private static byte[] readOptional32(ByteBuffer bb, String name) {
|
||||
if (!bb.hasRemaining()) throw new IllegalArgumentException("Missing " + name + " presence byte");
|
||||
int present = Byte.toUnsignedInt(bb.get());
|
||||
if (present == 0) return new byte[0];
|
||||
if (present != 1) throw new IllegalArgumentException("Bad " + name + " presence byte: " + present);
|
||||
return readExact(bb, OPTIONAL_FIELD_LEN, name);
|
||||
}
|
||||
|
||||
private static byte[] readExact(ByteBuffer bb, int len, String field) {
|
||||
if (len < 0 || bb.remaining() < len) {
|
||||
throw new IllegalArgumentException("ANS-104 " + field + " truncated: need=" + len + " remaining=" + bb.remaining());
|
||||
}
|
||||
byte[] out = new byte[len];
|
||||
bb.get(out);
|
||||
return out;
|
||||
}
|
||||
|
||||
private static long readUnsignedLongAsNonNegative(ByteBuffer bb, String field) {
|
||||
if (bb.remaining() < 8) throw new IllegalArgumentException("Missing ANS-104 " + field);
|
||||
long v = bb.getLong();
|
||||
if (v < 0) throw new IllegalArgumentException("ANS-104 " + field + " exceeds signed long range");
|
||||
return v;
|
||||
}
|
||||
|
||||
private static List<Tag> validateTags(List<Tag> tags) {
|
||||
List<Tag> safe = tags == null ? List.of() : List.copyOf(tags);
|
||||
if (safe.size() > MAX_TAGS_COUNT) {
|
||||
throw new IllegalArgumentException("Too many ANS-104 tags: " + safe.size());
|
||||
}
|
||||
for (Tag tag : safe) {
|
||||
if (tag == null || tag.name() == null || tag.value() == null || tag.name().isEmpty() || tag.value().isEmpty()) {
|
||||
throw new IllegalArgumentException("ANS-104 tag name/value must be non-empty");
|
||||
}
|
||||
if (utf8(tag.name()).length > 1024) throw new IllegalArgumentException("ANS-104 tag name exceeds 1024 bytes");
|
||||
if (utf8(tag.value()).length > 3072) throw new IllegalArgumentException("ANS-104 tag value exceeds 3072 bytes");
|
||||
}
|
||||
return safe;
|
||||
}
|
||||
|
||||
private static byte[] serializeAvroTags(List<Tag> tags) {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
if (!tags.isEmpty()) {
|
||||
writeAvroLong(out, tags.size());
|
||||
for (Tag tag : tags) {
|
||||
writeAvroString(out, tag.name());
|
||||
writeAvroString(out, tag.value());
|
||||
}
|
||||
}
|
||||
writeAvroLong(out, 0); // array terminator; also the complete encoding for an empty array
|
||||
byte[] raw = out.toByteArray();
|
||||
if (raw.length > MAX_TAGS_BYTES) throw new IllegalArgumentException("ANS-104 tags exceed " + MAX_TAGS_BYTES + " bytes");
|
||||
return raw;
|
||||
}
|
||||
|
||||
private static void writeAvroString(ByteArrayOutputStream out, String value) {
|
||||
byte[] bytes = utf8(value);
|
||||
writeAvroLong(out, bytes.length);
|
||||
out.writeBytes(bytes);
|
||||
}
|
||||
|
||||
private static void writeAvroLong(ByteArrayOutputStream out, long value) {
|
||||
long n = (value << 1) ^ (value >> 63);
|
||||
while ((n & ~0x7fL) != 0) {
|
||||
out.write((int) ((n & 0x7f) | 0x80));
|
||||
n >>>= 7;
|
||||
}
|
||||
out.write((int) n);
|
||||
}
|
||||
|
||||
private static void writeU16LE(ByteArrayOutputStream out, int value) {
|
||||
out.write(value & 0xff);
|
||||
out.write((value >>> 8) & 0xff);
|
||||
}
|
||||
|
||||
private static void writeU64LE(ByteArrayOutputStream out, long value) {
|
||||
for (int i = 0; i < 8; i++) out.write((int) ((value >>> (8 * i)) & 0xff));
|
||||
}
|
||||
|
||||
private static void requireLen(byte[] bytes, int expected, String name) {
|
||||
if (bytes == null || bytes.length != expected) {
|
||||
throw new IllegalArgumentException(name + " must be " + expected + " bytes");
|
||||
}
|
||||
}
|
||||
|
||||
private static List<Tag> parseAvroTags(byte[] raw, long declaredCount) {
|
||||
AvroCursor c = new AvroCursor(raw);
|
||||
List<Tag> out = new ArrayList<>();
|
||||
while (!c.eof()) {
|
||||
long count = c.readLong();
|
||||
if (count == 0) break;
|
||||
if (count < 0) {
|
||||
count = -count;
|
||||
long blockBytes = c.readLong();
|
||||
if (blockBytes < 0) throw new IllegalArgumentException("Negative Avro tag block size");
|
||||
}
|
||||
if (count > MAX_TAGS_COUNT || out.size() + count > MAX_TAGS_COUNT) {
|
||||
throw new IllegalArgumentException("Too many Avro tags");
|
||||
}
|
||||
for (long i = 0; i < count; i++) {
|
||||
String name = c.readString();
|
||||
String value = c.readString();
|
||||
if (name.isEmpty() || value.isEmpty()) throw new IllegalArgumentException("ANS-104 tag name/value must be non-empty");
|
||||
if (name.getBytes(StandardCharsets.UTF_8).length > 1024) throw new IllegalArgumentException("ANS-104 tag name exceeds 1024 bytes");
|
||||
if (value.getBytes(StandardCharsets.UTF_8).length > 3072) throw new IllegalArgumentException("ANS-104 tag value exceeds 3072 bytes");
|
||||
out.add(new Tag(name, value));
|
||||
}
|
||||
}
|
||||
if (!c.eof()) {
|
||||
throw new IllegalArgumentException("Trailing bytes in ANS-104 Avro tags");
|
||||
}
|
||||
if (out.size() != declaredCount) {
|
||||
throw new IllegalArgumentException("ANS-104 tags_count mismatch: declared=" + declaredCount + " parsed=" + out.size());
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private static final class AvroCursor {
|
||||
private final byte[] data;
|
||||
private int p;
|
||||
private AvroCursor(byte[] data) { this.data = data == null ? new byte[0] : data; }
|
||||
boolean eof() { return p == data.length; }
|
||||
long readLong() {
|
||||
long raw = 0;
|
||||
int shift = 0;
|
||||
while (true) {
|
||||
if (p >= data.length) throw new IllegalArgumentException("Truncated Avro long");
|
||||
int b = data[p++] & 0xff;
|
||||
raw |= (long) (b & 0x7f) << shift;
|
||||
if ((b & 0x80) == 0) break;
|
||||
shift += 7;
|
||||
if (shift > 63) throw new IllegalArgumentException("Avro long too large");
|
||||
}
|
||||
return (raw >>> 1) ^ -(raw & 1L);
|
||||
}
|
||||
String readString() {
|
||||
long len = readLong();
|
||||
if (len < 0 || len > Integer.MAX_VALUE || p + len > data.length) {
|
||||
throw new IllegalArgumentException("Bad Avro string length: " + len);
|
||||
}
|
||||
String s = new String(data, p, (int) len, StandardCharsets.UTF_8);
|
||||
p += (int) len;
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] deepHash(Object item) {
|
||||
if (item instanceof byte[] bytes) {
|
||||
return deepHashBlob(bytes);
|
||||
}
|
||||
if (item instanceof List<?> items) {
|
||||
byte[] acc = sha384(utf8("list" + items.size()));
|
||||
for (Object child : items) {
|
||||
acc = sha384(concat(acc, deepHash(child)));
|
||||
}
|
||||
return acc;
|
||||
}
|
||||
throw new IllegalArgumentException("Unsupported deepHash item: " + (item == null ? "null" : item.getClass()));
|
||||
}
|
||||
|
||||
private static byte[] deepHashBlob(byte[] bytes) {
|
||||
byte[] tag = utf8("blob" + bytes.length);
|
||||
return sha384(concat(sha384(tag), sha384(bytes)));
|
||||
}
|
||||
|
||||
private static byte[] utf8(String s) { return s.getBytes(StandardCharsets.UTF_8); }
|
||||
private static byte[] sha256(byte[] b) { return digest("SHA-256", b); }
|
||||
private static byte[] sha384(byte[] b) { return digest("SHA-384", b); }
|
||||
private static byte[] digest(String alg, byte[] b) {
|
||||
try { return MessageDigest.getInstance(alg).digest(b); }
|
||||
catch (Exception e) { throw new IllegalStateException(alg + " unavailable", e); }
|
||||
}
|
||||
private static byte[] concat(byte[] a, byte[] b) {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream(a.length + b.length);
|
||||
out.writeBytes(a); out.writeBytes(b); return out.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -9,364 +9,120 @@ import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* BchBlockEntry — универсальный блок формата SHiNE (Frame v0).
|
||||
* SHiNE user block, Frame v1, carried as the data payload of an ANS-104 DataItem.
|
||||
*
|
||||
* =========================================================================
|
||||
* FRAME v0 — ФИКСИРОВАННЫЙ ФОРМАТ БЛОКА (ДОКУМЕНТ ПРОТОКОЛА)
|
||||
* =========================================================================
|
||||
* The constructor accepts the complete serialized ANS-104 DataItem because this is
|
||||
* exactly what is stored in PostgreSQL and exchanged between servers.
|
||||
*
|
||||
* Все числа BigEndian.
|
||||
* 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
|
||||
* [2] type
|
||||
* [2] subType
|
||||
* [2] version
|
||||
* [N] bodyBytes
|
||||
*
|
||||
* PREIMAGE (входит в blockSize, подписывается):
|
||||
* [2] frameCode (uint16) код/версия рамки:
|
||||
* - 0x0000 = Frame v0 (текущий)
|
||||
* [32] prevHash32 (bytes) SHA-256(preimage) предыдущего блока (цепочка)
|
||||
* [4] blockSize (int32) размер preimage (в байтах), ВКЛЮЧАЯ frameCode,
|
||||
* НО БЕЗ sigMarker и БЕЗ signature64
|
||||
* [4] blockNumber (int32) глобальный номер блока (>=0)
|
||||
* [8] timestamp (int64) unix seconds
|
||||
* [2] type (uint16) тип сообщения
|
||||
* [2] subType (uint16) подтип сообщения
|
||||
* [2] version (uint16) версия формата сообщения
|
||||
* [N] bodyBytes (bytes) тело сообщения (БЕЗ type/subType/version)
|
||||
*
|
||||
* TAIL (НЕ входит в blockSize, НЕ подписывается в Frame v0):
|
||||
* [2] sigMarker (uint16) маркер подписи:
|
||||
* - 0x0100 (256) = далее подпись Ed25519 64 байта
|
||||
* [64] signature64 (bytes) Ed25519 signature над hash32
|
||||
*
|
||||
* hash32 НЕ хранится в блоке.
|
||||
* hash32 вычисляется при парсинге:
|
||||
* preimage = первые blockSize байт
|
||||
* hash32 = SHA-256(preimage)
|
||||
*
|
||||
* Правила MVP-парсера (Frame v0):
|
||||
* - frameCode должен быть строго 0x0000, иначе REJECT.
|
||||
* - sigMarker должен быть строго 0x0100, иначе REJECT.
|
||||
* - подпись обязана присутствовать всегда (sigMarker+signature64).
|
||||
* - НИКАКИХ fallback-веток “если маркер другой, то подписи нет/другой хвост”.
|
||||
*
|
||||
* Важно по безопасности:
|
||||
* - sigMarker в v0 не входит в подписываемые байты → его можно подменить,
|
||||
* поэтому единственная безопасная логика: "если не 0x0100 — reject".
|
||||
* =========================================================================
|
||||
* The user signature is NOT stored in this frame. It is the single Ed25519/Solana
|
||||
* signature of the surrounding ANS-104 DataItem.
|
||||
*/
|
||||
public final class BchBlockEntry {
|
||||
|
||||
public static final int SIGNATURE_LEN = 64;
|
||||
public static final int HASH_LEN = 32;
|
||||
|
||||
public static final int FRAME_CODE_LEN = 2;
|
||||
public static final int SIG_MARKER_LEN = 2;
|
||||
|
||||
/** Frame v0 */
|
||||
public static final int FRAME_CODE_V0 = 0x0000;
|
||||
|
||||
/** sigMarker: 256 = 0x0100 */
|
||||
public static final int SIG_MARKER_ED25519 = 0x0100;
|
||||
|
||||
/**
|
||||
* Максимальный допустимый размер блока (fullBytes = preimage + sigMarker + signature),
|
||||
* чтобы не уложить сервер по памяти/диску.
|
||||
*/
|
||||
public static final int MAX_BLOCK_FULL_BYTES = 4 * 1024 * 1024;
|
||||
|
||||
/**
|
||||
* Насколько блок может “обгонять” текущее время (защита от кривых часов/вбросов).
|
||||
* Если timestamp больше now + 60 сек — блок считаем неверным.
|
||||
*/
|
||||
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 + 32 + 4 + 4 + 8 + 2 + 2 + 2;
|
||||
|
||||
/**
|
||||
* Размер фиксированной части PREIMAGE (без bodyBytes).
|
||||
*
|
||||
* PREIMAGE header:
|
||||
* frameCode(2) + prevHash32(32) + blockSize(4) + blockNumber(4) + timestamp(8)
|
||||
* + type(2) + subType(2) + version(2)
|
||||
*/
|
||||
public static final int PREIMAGE_HEADER_SIZE =
|
||||
2 // frameCode
|
||||
+ 32 // prevHash32
|
||||
+ 4 // blockSize
|
||||
+ 4 // blockNumber
|
||||
+ 8 // timestamp
|
||||
+ 2 // type
|
||||
+ 2 // subType
|
||||
+ 2; // version
|
||||
|
||||
/** Минимальный полный размер блока (без bodyBytes). */
|
||||
public static final int MIN_FULL_BYTES =
|
||||
PREIMAGE_HEADER_SIZE + SIG_MARKER_LEN + SIGNATURE_LEN;
|
||||
|
||||
// --- HEADER (PREIMAGE) ---
|
||||
public final int frameCode; // uint16 (v0=0)
|
||||
public final byte[] prevHash32; // 32
|
||||
public final int blockSize; // preimage size (включая frameCode)
|
||||
public final int blockNumber; // >=0
|
||||
public final int frameCode;
|
||||
public final byte[] prevHash32;
|
||||
public final byte[] back32Hash32;
|
||||
public final int blockSize;
|
||||
public final int blockNumber;
|
||||
public final long timestamp;
|
||||
public final short type;
|
||||
public final short subType;
|
||||
public final short version;
|
||||
|
||||
// --- BODY (PREIMAGE) ---
|
||||
public final byte[] bodyBytes;
|
||||
|
||||
/** Распарсенное тело (создаётся сразу при парсинге блока). */
|
||||
public final BodyRecord body;
|
||||
|
||||
// --- TAIL ---
|
||||
public final int sigMarker; // uint16 (v0: 0x0100)
|
||||
private final byte[] signature64; // 64
|
||||
private final Ans104DataItem dataItem;
|
||||
private final byte[] frameBytes;
|
||||
private final byte[] hash32;
|
||||
|
||||
// --- derived ---
|
||||
private final byte[] hash32; // 32, computed
|
||||
private final byte[] preimage; // blockSize bytes
|
||||
private final byte[] fullBytes; // preimage + sigMarker + signature
|
||||
|
||||
/* ===================================================================== */
|
||||
/* ====================== Конструктор из байт ========================== */
|
||||
/* ===================================================================== */
|
||||
|
||||
public BchBlockEntry(byte[] fullBytes) {
|
||||
Objects.requireNonNull(fullBytes, "fullBytes == null");
|
||||
|
||||
if (fullBytes.length < MIN_FULL_BYTES) {
|
||||
throw new IllegalArgumentException("Block too short: " + fullBytes.length + " < " + MIN_FULL_BYTES);
|
||||
}
|
||||
if (fullBytes.length > MAX_BLOCK_FULL_BYTES) {
|
||||
throw new IllegalArgumentException("Block too large: " + fullBytes.length + " > " + MAX_BLOCK_FULL_BYTES);
|
||||
/** Parse a complete serialized ANS-104 DataItem containing one SHiNE Frame v1. */
|
||||
public BchBlockEntry(byte[] fullDataItemBytes) {
|
||||
Objects.requireNonNull(fullDataItemBytes, "fullDataItemBytes == null");
|
||||
this.dataItem = new Ans104DataItem(fullDataItemBytes);
|
||||
this.frameBytes = dataItem.data();
|
||||
if (frameBytes.length < FRAME_HEADER_SIZE) {
|
||||
throw new IllegalArgumentException("SHiNE Frame v1 too short: " + frameBytes.length);
|
||||
}
|
||||
|
||||
ByteBuffer bb = ByteBuffer.wrap(fullBytes).order(ByteOrder.BIG_ENDIAN);
|
||||
|
||||
// [2] frameCode
|
||||
ByteBuffer bb = ByteBuffer.wrap(frameBytes).order(ByteOrder.BIG_ENDIAN);
|
||||
this.frameCode = Short.toUnsignedInt(bb.getShort());
|
||||
if (this.frameCode != FRAME_CODE_V0) {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"Bad frameCode: 0x%04X (expected 0x%04X)", this.frameCode, FRAME_CODE_V0
|
||||
));
|
||||
if (frameCode != FRAME_CODE_V1) {
|
||||
throw new IllegalArgumentException(String.format("Bad SHiNE frameCode: 0x%04X (expected 0x%04X)", frameCode, FRAME_CODE_V1));
|
||||
}
|
||||
|
||||
// [32] prevHash32
|
||||
this.prevHash32 = new byte[32];
|
||||
bb.get(this.prevHash32);
|
||||
|
||||
// [4] blockSize
|
||||
bb.get(prevHash32);
|
||||
this.back32Hash32 = new byte[32];
|
||||
bb.get(back32Hash32);
|
||||
this.blockSize = bb.getInt();
|
||||
if (blockSize < PREIMAGE_HEADER_SIZE) {
|
||||
throw new IllegalArgumentException("blockSize too small: " + blockSize + " < " + PREIMAGE_HEADER_SIZE);
|
||||
if (blockSize != frameBytes.length || blockSize < FRAME_HEADER_SIZE) {
|
||||
throw new IllegalArgumentException("SHiNE blockSize mismatch: declared=" + blockSize + " actual=" + frameBytes.length);
|
||||
}
|
||||
|
||||
// fullLen must match exactly: blockSize + sigMarker(2) + signature(64)
|
||||
int expectedFullLen = blockSize + SIG_MARKER_LEN + SIGNATURE_LEN;
|
||||
if (expectedFullLen != fullBytes.length) {
|
||||
throw new IllegalArgumentException("blockSize mismatch: blockSize=" + blockSize
|
||||
+ " expectedFullLen=" + expectedFullLen
|
||||
+ " fullLen=" + fullBytes.length);
|
||||
}
|
||||
if (expectedFullLen > MAX_BLOCK_FULL_BYTES) {
|
||||
throw new IllegalArgumentException("Block too large by blockSize: " + expectedFullLen + " > " + MAX_BLOCK_FULL_BYTES);
|
||||
}
|
||||
|
||||
// [4] blockNumber
|
||||
this.blockNumber = bb.getInt();
|
||||
if (this.blockNumber < 0) {
|
||||
throw new IllegalArgumentException("blockNumber < 0: " + this.blockNumber);
|
||||
}
|
||||
|
||||
// [8] timestamp
|
||||
if (blockNumber < 0) throw new IllegalArgumentException("blockNumber < 0: " + blockNumber);
|
||||
this.timestamp = bb.getLong();
|
||||
|
||||
// запрет “в будущее” больше чем на 1 минуту
|
||||
long now = Instant.now().getEpochSecond();
|
||||
if (this.timestamp > now + MAX_FUTURE_SECONDS) {
|
||||
throw new IllegalArgumentException("timestamp is too far in future: ts=" + this.timestamp
|
||||
+ " now=" + now + " maxFutureSec=" + MAX_FUTURE_SECONDS);
|
||||
if (timestamp > now + MAX_FUTURE_SECONDS) {
|
||||
throw new IllegalArgumentException("timestamp is too far in future: ts=" + timestamp + " now=" + now);
|
||||
}
|
||||
|
||||
// [2][2][2] type/subType/version
|
||||
this.type = bb.getShort();
|
||||
this.subType = bb.getShort();
|
||||
this.version = bb.getShort();
|
||||
|
||||
// [N] bodyBytes
|
||||
int bodyLen = blockSize - PREIMAGE_HEADER_SIZE;
|
||||
if (bodyLen < 0) {
|
||||
throw new IllegalArgumentException("Invalid body length: " + bodyLen);
|
||||
}
|
||||
this.bodyBytes = new byte[bodyLen];
|
||||
bb.get(this.bodyBytes);
|
||||
|
||||
// TAIL: [2] sigMarker
|
||||
this.sigMarker = Short.toUnsignedInt(bb.getShort());
|
||||
if (this.sigMarker != SIG_MARKER_ED25519) {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"Bad sigMarker: 0x%04X (expected 0x%04X)", this.sigMarker, SIG_MARKER_ED25519
|
||||
));
|
||||
}
|
||||
|
||||
// TAIL: [64] signature64
|
||||
this.signature64 = new byte[SIGNATURE_LEN];
|
||||
bb.get(this.signature64);
|
||||
|
||||
// preimage = первые blockSize байт (включая frameCode)
|
||||
this.preimage = Arrays.copyOfRange(fullBytes, 0, blockSize);
|
||||
|
||||
// hash32 = sha256(preimage)
|
||||
this.hash32 = BchCryptoVerifier.sha256(preimage);
|
||||
|
||||
// parse body по header.type/subType/version + ОБЯЗАТЕЛЬНЫЙ check()
|
||||
this.body = BodyRecordParser.parse(this.type, this.subType, this.version, this.bodyBytes);
|
||||
|
||||
this.fullBytes = Arrays.copyOf(fullBytes, fullBytes.length);
|
||||
|
||||
if (bb.remaining() != 0) {
|
||||
throw new IllegalArgumentException("Unexpected tail bytes, remaining=" + bb.remaining());
|
||||
}
|
||||
this.bodyBytes = new byte[bb.remaining()];
|
||||
bb.get(bodyBytes);
|
||||
this.body = BodyRecordParser.parse(type, subType, version, bodyBytes);
|
||||
this.hash32 = BchCryptoVerifier.sha256(frameBytes);
|
||||
}
|
||||
|
||||
/* ===================================================================== */
|
||||
/* ====================== Конструктор сборки ============================ */
|
||||
/* ===================================================================== */
|
||||
|
||||
public BchBlockEntry(byte[] prevHash32,
|
||||
int blockNumber,
|
||||
long timestamp,
|
||||
short type,
|
||||
short subType,
|
||||
short version,
|
||||
byte[] bodyBytes,
|
||||
byte[] signature64) {
|
||||
|
||||
Objects.requireNonNull(prevHash32, "prevHash32 == null");
|
||||
Objects.requireNonNull(bodyBytes, "bodyBytes == null");
|
||||
Objects.requireNonNull(signature64, "signature64 == null");
|
||||
|
||||
if (prevHash32.length != 32) throw new IllegalArgumentException("prevHash32 != 32");
|
||||
if (signature64.length != SIGNATURE_LEN) throw new IllegalArgumentException("signature64 != 64");
|
||||
|
||||
if (blockNumber < 0) {
|
||||
throw new IllegalArgumentException("blockNumber < 0: " + blockNumber);
|
||||
}
|
||||
|
||||
// запрет “в будущее” больше чем на 1 минуту
|
||||
long now = Instant.now().getEpochSecond();
|
||||
if (timestamp > now + MAX_FUTURE_SECONDS) {
|
||||
throw new IllegalArgumentException("timestamp is too far in future: ts=" + timestamp
|
||||
+ " now=" + now + " maxFutureSec=" + MAX_FUTURE_SECONDS);
|
||||
}
|
||||
|
||||
this.frameCode = FRAME_CODE_V0;
|
||||
this.prevHash32 = Arrays.copyOf(prevHash32, 32);
|
||||
this.blockNumber = blockNumber;
|
||||
this.timestamp = timestamp;
|
||||
this.type = type;
|
||||
this.subType = subType;
|
||||
this.version = version;
|
||||
this.bodyBytes = Arrays.copyOf(bodyBytes, bodyBytes.length);
|
||||
|
||||
// blockSize = размер preimage (включая frameCode)
|
||||
this.blockSize = PREIMAGE_HEADER_SIZE + this.bodyBytes.length;
|
||||
|
||||
int fullLen = this.blockSize + SIG_MARKER_LEN + SIGNATURE_LEN;
|
||||
if (fullLen > MAX_BLOCK_FULL_BYTES) {
|
||||
throw new IllegalArgumentException("Block too large: " + fullLen + " > " + MAX_BLOCK_FULL_BYTES);
|
||||
}
|
||||
|
||||
// parse body по header + ОБЯЗАТЕЛЬНЫЙ check()
|
||||
this.body = BodyRecordParser.parse(this.type, this.subType, this.version, this.bodyBytes);
|
||||
|
||||
// tail marker фиксирован
|
||||
this.sigMarker = SIG_MARKER_ED25519;
|
||||
this.signature64 = Arrays.copyOf(signature64, SIGNATURE_LEN);
|
||||
|
||||
// build preimage
|
||||
ByteBuffer pre = ByteBuffer.allocate(blockSize).order(ByteOrder.BIG_ENDIAN);
|
||||
pre.putShort((short) (FRAME_CODE_V0 & 0xFFFF));
|
||||
pre.put(this.prevHash32);
|
||||
pre.putInt(this.blockSize);
|
||||
pre.putInt(this.blockNumber);
|
||||
pre.putLong(this.timestamp);
|
||||
pre.putShort(this.type);
|
||||
pre.putShort(this.subType);
|
||||
pre.putShort(this.version);
|
||||
pre.put(this.bodyBytes);
|
||||
|
||||
this.preimage = pre.array();
|
||||
this.hash32 = BchCryptoVerifier.sha256(preimage);
|
||||
|
||||
// build fullBytes: preimage + sigMarker + signature64
|
||||
ByteBuffer full = ByteBuffer.allocate(fullLen).order(ByteOrder.BIG_ENDIAN);
|
||||
full.put(this.preimage);
|
||||
full.putShort((short) (SIG_MARKER_ED25519 & 0xFFFF));
|
||||
full.put(this.signature64);
|
||||
this.fullBytes = full.array();
|
||||
}
|
||||
|
||||
/* ===================================================================== */
|
||||
/* ============================ Getters ================================= */
|
||||
/* ===================================================================== */
|
||||
|
||||
public byte[] getPreimageBytes() {
|
||||
return Arrays.copyOf(preimage, preimage.length);
|
||||
}
|
||||
|
||||
/** Возвращает подпись Ed25519 (64 байта). */
|
||||
public byte[] getSignature64() {
|
||||
return Arrays.copyOf(signature64, SIGNATURE_LEN);
|
||||
}
|
||||
|
||||
/** Возвращает hash32 = SHA-256(preimage). */
|
||||
public byte[] getHash32() {
|
||||
return Arrays.copyOf(hash32, HASH_LEN);
|
||||
}
|
||||
|
||||
/** Возвращает полный блок: preimage + sigMarker + signature. */
|
||||
public byte[] toBytes() {
|
||||
return Arrays.copyOf(fullBytes, fullBytes.length);
|
||||
}
|
||||
public Ans104DataItem getDataItem() { return dataItem; }
|
||||
public byte[] getFrameBytes() { return Arrays.copyOf(frameBytes, frameBytes.length); }
|
||||
/** Kept as an alias for code/docs that called the old field preimage. */
|
||||
public byte[] getPreimageBytes() { return getFrameBytes(); }
|
||||
public byte[] getSignature64() { return dataItem.signature64(); }
|
||||
public byte[] getOwner32() { return dataItem.owner32(); }
|
||||
public byte[] getDataItemId32() { return dataItem.id32(); }
|
||||
public byte[] getHash32() { return Arrays.copyOf(hash32, hash32.length); }
|
||||
/** Full serialized ANS-104 DataItem. */
|
||||
public byte[] toBytes() { return dataItem.rawBytes(); }
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String timeIso;
|
||||
try {
|
||||
timeIso = Instant.ofEpochSecond(timestamp).toString();
|
||||
} catch (Exception e) {
|
||||
timeIso = "некорректныйTimestamp";
|
||||
}
|
||||
|
||||
return "BchBlockEntry{"
|
||||
+ "FRAME{frameCode=0x" + hex4(frameCode)
|
||||
+ "}, HDR{"
|
||||
+ "blockSize=" + blockSize
|
||||
+ ", blockNumber=" + blockNumber
|
||||
+ ", timestamp=" + timestamp + " (" + timeIso + ")"
|
||||
+ ", type=" + (type & 0xFFFF)
|
||||
+ ", subType=" + (subType & 0xFFFF)
|
||||
+ ", version=" + (version & 0xFFFF)
|
||||
+ ", prevHash32(hex)=" + toHex(prevHash32)
|
||||
+ "}"
|
||||
+ ", BODY{len=" + (bodyBytes == null ? -1 : bodyBytes.length) + "}"
|
||||
+ ", TAIL{sigMarker=0x" + hex4(sigMarker) + ", signature64(hex)=" + toHex(signature64) + "}"
|
||||
+ ", DERIVED{hash32(hex)=" + toHex(hash32) + "}"
|
||||
+ "}";
|
||||
}
|
||||
|
||||
private static String hex4(int v) {
|
||||
String s = Integer.toHexString(v & 0xFFFF);
|
||||
while (s.length() < 4) s = "0" + s;
|
||||
return s;
|
||||
return "BchBlockEntry{" +
|
||||
"frameCode=0x" + Integer.toHexString(frameCode) +
|
||||
", blockNumber=" + blockNumber +
|
||||
", timestamp=" + timestamp +
|
||||
", type=" + (type & 0xffff) +
|
||||
", subType=" + (subType & 0xffff) +
|
||||
", version=" + (version & 0xffff) +
|
||||
", bodyLen=" + bodyBytes.length +
|
||||
", dataItemId=" + toHex(getDataItemId32()) +
|
||||
", blockHash=" + toHex(hash32) +
|
||||
'}';
|
||||
}
|
||||
|
||||
private static String toHex(byte[] bytes) {
|
||||
if (bytes == null) return "null";
|
||||
char[] HEX = "0123456789abcdef".toCharArray();
|
||||
char[] h = "0123456789abcdef".toCharArray();
|
||||
char[] out = new char[bytes.length * 2];
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
int vv = bytes[i] & 0xFF;
|
||||
out[i * 2] = HEX[vv >>> 4];
|
||||
out[i * 2 + 1] = HEX[vv & 0x0F];
|
||||
int v = bytes[i] & 0xff;
|
||||
out[i * 2] = h[v >>> 4]; out[i * 2 + 1] = h[v & 15];
|
||||
}
|
||||
return new String(out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+10
-25
@@ -1,42 +1,27 @@
|
||||
package blockchain;
|
||||
|
||||
import utils.crypto.Ed25519Util;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Верификатор SHiNE (Frame v0):
|
||||
*
|
||||
* preimage = первые blockSize байт блока (ВКЛЮЧАЯ frameCode=0x0000),
|
||||
* = всё до TAIL (sigMarker+signature).
|
||||
*
|
||||
* hash32 = SHA-256(preimage)
|
||||
* verify = Ed25519.verify(hash32, signature64, pubKey32)
|
||||
*/
|
||||
/** Cryptographic checks for SHiNE Frame v1 + surrounding ANS-104 DataItem. */
|
||||
public final class BchCryptoVerifier {
|
||||
|
||||
private BchCryptoVerifier() {}
|
||||
|
||||
public static byte[] sha256(byte[] data) {
|
||||
Objects.requireNonNull(data, "data == null");
|
||||
try {
|
||||
MessageDigest d = MessageDigest.getInstance("SHA-256");
|
||||
return d.digest(data);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException("SHA-256 unavailable", e);
|
||||
}
|
||||
try { return MessageDigest.getInstance("SHA-256").digest(data); }
|
||||
catch (Exception e) { throw new IllegalStateException("SHA-256 unavailable", e); }
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the ANS-104 owner is the current blockchain key and that the
|
||||
* ANS-104 Ed25519 signature is valid. The SHiNE block hash itself remains
|
||||
* SHA-256(Frame v1 bytes) and is intentionally independent of Arweave.
|
||||
*/
|
||||
public static boolean verifyBlock(BchBlockEntry block, byte[] publicKey32) {
|
||||
Objects.requireNonNull(block, "block == null");
|
||||
Objects.requireNonNull(publicKey32, "publicKey32 == null");
|
||||
|
||||
if (publicKey32.length != 32) throw new IllegalArgumentException("publicKey32 != 32");
|
||||
|
||||
byte[] hash32 = block.getHash32();
|
||||
byte[] sig64 = block.getSignature64();
|
||||
|
||||
return Ed25519Util.verify(hash32, sig64, publicKey32);
|
||||
return block.getDataItem().ownerEquals(publicKey32) && block.getDataItem().verifySignature();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,10 @@ public final class BodyRecordParser {
|
||||
&& (v == (CreateChannelBody.VER & 0xFFFF))) {
|
||||
return new CreateChannelBody(subType, version, bodyBytes).check();
|
||||
}
|
||||
if (st == (ForkBody.SUBTYPE & 0xFFFF)
|
||||
&& (v == (ForkBody.VER & 0xFFFF))) {
|
||||
return new ForkBody(subType, version, bodyBytes).check();
|
||||
}
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Unknown TECH body type/version/subType: type=%d ver=%d subType=%d", t, v, st)
|
||||
);
|
||||
@@ -33,7 +37,7 @@ public final class BodyRecordParser {
|
||||
int key = (t << 16) | v;
|
||||
|
||||
BodyRecord r = switch (key) {
|
||||
case TextBody.KEY -> {
|
||||
case TextLineBody.KEY -> {
|
||||
if (st == (MsgSubType.TEXT_POST & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_EXERCISE & 0xFFFF)
|
||||
@@ -41,7 +45,8 @@ public final class BodyRecordParser {
|
||||
|| st == (MsgSubType.TEXT_COURSE & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_ENTRYPOINT & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_REPOST & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_CHANNEL_META & 0xFFFF)) {
|
||||
|| st == (MsgSubType.TEXT_CHANNEL_META & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_CHANNEL_DELETE & 0xFFFF)) {
|
||||
yield new TextLineBody(subType, version, bodyBytes);
|
||||
}
|
||||
|
||||
|
||||
@@ -13,5 +13,5 @@
|
||||
// public static final short TEXT = 1; // сообщения да надо
|
||||
// public static final short REACTION = 2; // реакции не надо
|
||||
// public static final short CONNECTION = 3; // связи (friend/contact/follow) да надо
|
||||
// public static final short USER_PARAM = 4; // параметры профиля да надо
|
||||
// public static final short USER_PROFILE= 4; // профиль пользователя да надо
|
||||
//}
|
||||
@@ -35,6 +35,8 @@ public final class MsgSubType {
|
||||
/** HeaderBody: subType всегда 0 (compat). */
|
||||
public static final short HEADER_COMPAT = 0;
|
||||
public static final short TECH_CREATE_CHANNEL = 1;
|
||||
/** Новый fork/ротация ключей: ссылка на родительскую цепочку и точку отката. */
|
||||
public static final short TECH_FORK = 2;
|
||||
|
||||
/* ===================== TEXT (msg_type=1) ===================== */
|
||||
|
||||
@@ -53,7 +55,7 @@ public final class MsgSubType {
|
||||
|
||||
/**
|
||||
* REPLY — ответ на сообщение.
|
||||
* НЕ в линии. Имеет target (toBlockchainName + blockNumber + hash32).
|
||||
* НЕ в линии. Имеет target (toLogin + blockNumber + hash32).
|
||||
* Может указывать на чужой блокчейн/чужую линию/чужой канал.
|
||||
*/
|
||||
public static final short TEXT_REPLY = 20;
|
||||
@@ -69,7 +71,7 @@ public final class MsgSubType {
|
||||
|
||||
/**
|
||||
* REPOST — отложенная будущая заготовка репоста сообщения в линии канала.
|
||||
* Имеет hasLine + target (toBlockchainName + toBlockGlobalNumber + toBlockHash32) + текст комментария.
|
||||
* Имеет hasLine + target (toLogin + toBlockGlobalNumber + toBlockHash32) + текст комментария.
|
||||
*/
|
||||
public static final short TEXT_REPOST = 50;
|
||||
|
||||
@@ -79,6 +81,9 @@ public final class MsgSubType {
|
||||
*/
|
||||
public static final short TEXT_CHANNEL_META = 90;
|
||||
|
||||
/** CHANNEL_DELETE — soft-delete marker for a channel line. */
|
||||
public static final short TEXT_CHANNEL_DELETE = 92;
|
||||
|
||||
/** ENTRYPOINT — входная страница канала (line-based). */
|
||||
public static final short TEXT_ENTRYPOINT = 100;
|
||||
/** EXERCISE — упражнение/комплекс (line-based). */
|
||||
|
||||
@@ -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 = "test5592";
|
||||
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";
|
||||
}
|
||||
+14
-12
@@ -1,21 +1,23 @@
|
||||
package blockchain.body;
|
||||
|
||||
/**
|
||||
* BodyHasLine — для типов, которые имеют линейные поля в body.
|
||||
* BodyHasLine — типы блоков, которые входят в проверяемую логическую линию.
|
||||
*
|
||||
* Line-prefix (BigEndian) в НАЧАЛЕ bodyBytes:
|
||||
* [4] lineCode код линии (root-идентификатор):
|
||||
* - 0 для дефолтной линии/канала "0" (root = HEADER, blockNumber=0)
|
||||
* - для канала "X": blockNumber root-блока канала (CREATE_CHANNEL)
|
||||
* Общая семантика:
|
||||
* prevLineBlockGlobalNumber — GLOBAL blockNumber предыдущего блока этой линии;
|
||||
* prevLineBlockHash32 — hash32 предыдущего блока этой линии;
|
||||
* lineSeq — порядковый номер шага линии.
|
||||
*
|
||||
* [4] prevLineBlockGlobalNumber глобальный номер предыдущего блока в этой линии
|
||||
* [32] prevLineBlockHash32 hash32 предыдущего блока в этой линии
|
||||
* lineCode — логический идентификатор линии:
|
||||
* - 0 для фиксированных системных линий (TECH, CONNECTION, USER_PROFILE)
|
||||
* и для дефолтного канала, корнем которого является HEADER #0;
|
||||
* - для пользовательского канала: GLOBAL blockNumber его CREATE_CHANNEL root.
|
||||
*
|
||||
* [4] lineSeq порядковый номер сообщения внутри линии (1..N)
|
||||
* Важно: lineCode не обязан физически присутствовать в body. Для фиксированных
|
||||
* USER_PROFILE и CONNECTION он выводится из msg_type и метод lineCode() возвращает 0.
|
||||
* Для TEXT-каналов и TECH-блоков lineCode хранится в body явно.
|
||||
*
|
||||
* Важно:
|
||||
* - Проверка связности линии (prevLineBlockGlobalNumber ↔ prevLineBlockHash32) и корректности lineSeq
|
||||
* выполняется на сервере/в БД при вставке (а не в body.check()).
|
||||
* Полная проверка хвоста линии, prev hash и lineSeq выполняется сервером при AddBlock.
|
||||
*/
|
||||
public interface BodyHasLine {
|
||||
|
||||
@@ -26,4 +28,4 @@ public interface BodyHasLine {
|
||||
byte[] prevLineBlockHash32();
|
||||
|
||||
int lineSeq();
|
||||
}
|
||||
}
|
||||
|
||||
+14
-20
@@ -1,31 +1,25 @@
|
||||
package blockchain.body;
|
||||
|
||||
import utils.blockchain.BlockchainNameUtil;
|
||||
|
||||
/**
|
||||
* BodyHasTarget — дополнительный интерфейс для body, которые "ссылаются" на цель (to-поля).
|
||||
* Body that refers to a logical block target.
|
||||
*
|
||||
* Новое правило:
|
||||
* - toLogin НЕ храним в байтах блока.
|
||||
* - toLogin всегда вычисляется из toBchName по стандарту login+"-NNN".
|
||||
*
|
||||
* Все методы могут возвращать null.
|
||||
* Canonical rules:
|
||||
* - external target: login + forkNumber + blockNumber + blockHash;
|
||||
* - own EDIT target: blockNumber + blockHash only;
|
||||
* - logical identity NEVER includes forkNumber: login + blockNumber + blockHash;
|
||||
* - forkNumber is informational/historical only and must be in 1..999.
|
||||
*/
|
||||
public interface BodyHasTarget {
|
||||
/** Target login; null for own-chain compact EDIT targets. */
|
||||
default String toLogin() { return null; }
|
||||
|
||||
/** login цели (nullable). Вычисляется из toBchName(). */
|
||||
default String toLogin() {
|
||||
String bch = toBchName();
|
||||
if (bch == null) return null;
|
||||
return BlockchainNameUtil.loginFromBlockchainName(bch);
|
||||
}
|
||||
/** Historical fork number recorded by the author; null for compact EDIT targets. */
|
||||
default Integer toForkNumber() { return null; }
|
||||
|
||||
/** blockchainName цели (nullable). */
|
||||
String toBchName();
|
||||
/** Runtime-only physical blockchain cache; never serialized in canonical targets. */
|
||||
@Deprecated
|
||||
default String toBchName() { return null; }
|
||||
|
||||
/** globalNumber цели (nullable). */
|
||||
Integer toBlockGlobalNumber();
|
||||
|
||||
/** hash целевого блока (обычно 32 байта). Может быть null, если ссылки нет. */
|
||||
byte[] toBlockHashBytes();
|
||||
}
|
||||
}
|
||||
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
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.Arrays;
|
||||
|
||||
/** Shared codec/validation for canonical SHiNE target formats. */
|
||||
final class BodyTargetSupport {
|
||||
private BodyTargetSupport() {}
|
||||
|
||||
record ExternalTarget(String login, int forkNumber, int blockNumber, byte[] blockHash32) {}
|
||||
|
||||
static ExternalTarget readExternal(ByteBuffer bb, String ctx, int bytesRequiredAfterTarget) {
|
||||
ensureMin(bb, 1 + 1 + 2 + 4 + 32 + bytesRequiredAfterTarget, ctx + " target too short");
|
||||
int loginLen = Byte.toUnsignedInt(bb.get());
|
||||
if (loginLen <= 0) throw new IllegalArgumentException(ctx + " toLoginLen is 0");
|
||||
ensureMin(bb, loginLen + 2 + 4 + 32 + bytesRequiredAfterTarget, ctx + " target payload too short");
|
||||
|
||||
byte[] loginBytes = new byte[loginLen];
|
||||
bb.get(loginBytes);
|
||||
String login = decodeStrictUtf8(loginBytes, ctx + " toLogin");
|
||||
if (login.isBlank()) throw new IllegalArgumentException(ctx + " toLogin is blank");
|
||||
|
||||
int forkNumber = Short.toUnsignedInt(bb.getShort());
|
||||
validateForkNumber(forkNumber, ctx);
|
||||
int blockNumber = bb.getInt();
|
||||
if (blockNumber < 0) throw new IllegalArgumentException(ctx + " toBlockGlobalNumber < 0");
|
||||
|
||||
byte[] hash = new byte[32];
|
||||
bb.get(hash);
|
||||
requireNonZeroHash32(hash, ctx + " toBlockHash32");
|
||||
return new ExternalTarget(login, forkNumber, blockNumber, hash);
|
||||
}
|
||||
|
||||
static int externalSize(String login) {
|
||||
return 1 + loginBytes(login).length + 2 + 4 + 32;
|
||||
}
|
||||
|
||||
static void writeExternal(ByteBuffer bb, String login, int forkNumber, int blockNumber, byte[] hash32, String ctx) {
|
||||
byte[] loginBytes = loginBytes(login);
|
||||
validateForkNumber(forkNumber, ctx);
|
||||
if (blockNumber < 0) throw new IllegalArgumentException(ctx + " toBlockGlobalNumber < 0");
|
||||
requireNonZeroHash32(hash32, ctx + " toBlockHash32");
|
||||
|
||||
bb.put((byte) loginBytes.length);
|
||||
bb.put(loginBytes);
|
||||
bb.putShort((short) forkNumber);
|
||||
bb.putInt(blockNumber);
|
||||
bb.put(hash32);
|
||||
}
|
||||
|
||||
static void validateExternal(String login, Integer forkNumber, Integer blockNumber, byte[] hash32, String ctx) {
|
||||
loginBytes(login);
|
||||
if (forkNumber == null) throw new IllegalArgumentException(ctx + " toForkNumber is null");
|
||||
validateForkNumber(forkNumber, ctx);
|
||||
if (blockNumber == null || blockNumber < 0) throw new IllegalArgumentException(ctx + " toBlockGlobalNumber invalid");
|
||||
requireNonZeroHash32(hash32, ctx + " toBlockHash32");
|
||||
}
|
||||
|
||||
static void validateCompact(Integer blockNumber, byte[] hash32, String ctx) {
|
||||
if (blockNumber == null || blockNumber < 0) throw new IllegalArgumentException(ctx + " toBlockGlobalNumber invalid");
|
||||
requireNonZeroHash32(hash32, ctx + " toBlockHash32");
|
||||
}
|
||||
|
||||
static byte[] readCompactHash(ByteBuffer bb, String ctx) {
|
||||
ensureMin(bb, 4 + 32, ctx + " target too short");
|
||||
int blockNumber = bb.getInt();
|
||||
if (blockNumber < 0) throw new IllegalArgumentException(ctx + " toBlockGlobalNumber < 0");
|
||||
byte[] hash = new byte[32];
|
||||
bb.get(hash);
|
||||
requireNonZeroHash32(hash, ctx + " toBlockHash32");
|
||||
return hash;
|
||||
}
|
||||
|
||||
static byte[] loginBytes(String login) {
|
||||
if (login == null || login.isBlank()) throw new IllegalArgumentException("toLogin is blank");
|
||||
byte[] bytes = login.getBytes(StandardCharsets.UTF_8);
|
||||
if (bytes.length == 0 || bytes.length > 255) throw new IllegalArgumentException("toLogin utf8 len must be 1..255");
|
||||
return bytes;
|
||||
}
|
||||
|
||||
static void validateForkNumber(int forkNumber, String ctx) {
|
||||
if (forkNumber < 1 || forkNumber > 999) {
|
||||
throw new IllegalArgumentException(ctx + " toForkNumber must be 1..999");
|
||||
}
|
||||
}
|
||||
|
||||
static void requireNonZeroHash32(byte[] hash32, String field) {
|
||||
if (hash32 == null || hash32.length != 32) throw new IllegalArgumentException(field + " must be 32 bytes");
|
||||
boolean any = false;
|
||||
for (byte b : hash32) any |= b != 0;
|
||||
if (!any) throw new IllegalArgumentException(field + " must not be zero hash");
|
||||
}
|
||||
|
||||
static byte[] copyHash(byte[] hash32) {
|
||||
return hash32 == null ? null : Arrays.copyOf(hash32, 32);
|
||||
}
|
||||
|
||||
static String decodeStrictUtf8(byte[] bytes, String field) {
|
||||
var decoder = StandardCharsets.UTF_8.newDecoder()
|
||||
.onMalformedInput(CodingErrorAction.REPORT)
|
||||
.onUnmappableCharacter(CodingErrorAction.REPORT);
|
||||
try {
|
||||
return decoder.decode(ByteBuffer.wrap(bytes)).toString();
|
||||
} catch (CharacterCodingException e) {
|
||||
throw new IllegalArgumentException(field + " is not valid UTF-8", e);
|
||||
}
|
||||
}
|
||||
|
||||
static void ensureMin(ByteBuffer bb, int need, String msg) {
|
||||
if (bb.remaining() < need) throw new IllegalArgumentException(msg + " (need=" + need + ", remaining=" + bb.remaining() + ")");
|
||||
}
|
||||
}
|
||||
+65
-247
@@ -1,286 +1,104 @@
|
||||
package blockchain.body;
|
||||
|
||||
import blockchain.MsgSubType;
|
||||
import utils.blockchain.BlockchainNameUtil;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* ConnectionBody — type=3, ver=1 (в заголовке блока).
|
||||
*
|
||||
* subType (в заголовке блока) как MsgSubType:
|
||||
* FRIEND=10, UNFRIEND=11
|
||||
* CONTACT=20, UNCONTACT=21
|
||||
* FOLLOW=30, UNFOLLOW=31
|
||||
* SPOUSE=40, UNSPOUSE=41
|
||||
* PARENT=50, UNPARENT=51
|
||||
* CHILD=52, UNCHILD=53
|
||||
* SIBLING=54, UNSIBLING=55
|
||||
* FRIEND=14, UNFRIEND=15
|
||||
* KNOWN_PERSON=60, UNKNOWN_PERSON=61 (legacy; accepted but not used by current UI)
|
||||
* SHINE_CONFIRMED=70, SHINE_UNCONFIRMED=71
|
||||
* SHINE_SEEN=74, SHINE_UNSEEN=75 (currently not used by UI)
|
||||
* OFFICIAL_ACCOUNT_CONFIRMED=80, OFFICIAL_ACCOUNT_UNCONFIRMED=81
|
||||
*
|
||||
* bodyBytes (BigEndian), новый формат (toLogin НЕ ХРАНИМ):
|
||||
* [4] lineCode
|
||||
* [4] prevLineNumber
|
||||
* [32] prevLineHash32
|
||||
* [4] thisLineNumber
|
||||
*
|
||||
* [1] toBlockchainNameLen (uint8)
|
||||
* [N] toBlockchainName UTF-8
|
||||
* [4] toBlockGlobalNumber (int32)
|
||||
* [32] toBlockHash32 (raw 32 bytes)
|
||||
*
|
||||
* toLogin вычисляется автоматически из toBlockchainName:
|
||||
* toLogin = BlockchainNameUtil.loginFromBlockchainName(toBlockchainName)
|
||||
* CONNECTION with one fixed connection-history line and canonical external target.
|
||||
* lineCode is not stored in body: the logical connection line always has code 0.
|
||||
*/
|
||||
|
||||
/**
|
||||
* =========================================================================
|
||||
* ПРАВИЛО TARGET/ROOT ДЛЯ КАНАЛОВ И СВЯЗЕЙ (важно для подписок/друзей/контактов)
|
||||
* =========================================================================
|
||||
*
|
||||
* Термины:
|
||||
* - ROOT линии/канала = блок, который "начинает" линию:
|
||||
* * для канала "0" root = HEADER (blockNumber=0)
|
||||
* * для канала "X" root = CREATE_CHANNEL (blockNumber этого блока)
|
||||
*
|
||||
* 1) СВЯЗИ МЕЖДУ ПОЛЬЗОВАТЕЛЯМИ (CONNECTION_*):
|
||||
* FRIEND / CONTACT -> цель ВСЕГДА HEADER пользователя:
|
||||
* toBlockNumber = 0
|
||||
* toBlockHash32 = hash32(HEADER цели)
|
||||
*
|
||||
* 2) ПОДПИСКИ НА КОНТЕНТ (FOLLOW/SUBSCRIBE):
|
||||
* FOLLOW пользователя (в целом) -> цель = ROOT дефолтного канала "0" (то есть HEADER):
|
||||
* toBlockNumber = 0
|
||||
* toBlockHash32 = hash32(HEADER цели)
|
||||
*
|
||||
* FOLLOW/подписка на конкретный канал пользователя ->
|
||||
* цель = ROOT этого канала:
|
||||
* - канал "0": toBlockNumber=0, toBlockHash32=hash32(HEADER)
|
||||
* - канал "X": toBlockNumber=blockNumber(CREATE_CHANNEL),
|
||||
* toBlockHash32=hash32(CREATE_CHANNEL)
|
||||
*
|
||||
* 3) ЗАПРЕТЫ ВАЛИДАЦИИ (желательно на сервере/в БД):
|
||||
* - CONNECTION_CLOSE_FRIEND/CONTACT не могут ссылаться на не-HEADER (toBlockNumber != 0 запрещено).
|
||||
* - FOLLOW на канал "X" не может ссылаться на произвольный пост внутри канала:
|
||||
* разрешено ТОЛЬКО на ROOT (HEADER или CREATE_CHANNEL).
|
||||
*
|
||||
* Зачем так:
|
||||
* - связи и подписки всегда стабильны и не ломаются при новых постах,
|
||||
* - один понятный инвариант: "подписка всегда указывает на root линии".
|
||||
* =========================================================================
|
||||
*/
|
||||
|
||||
public final class ConnectionBody implements BodyRecord, BodyHasTarget, BodyHasLine {
|
||||
|
||||
public static final short TYPE = 3;
|
||||
public static final short VER = 1;
|
||||
|
||||
public static final short VER = 1;
|
||||
public static final int KEY = ((TYPE & 0xFFFF) << 16) | (VER & 0xFFFF);
|
||||
|
||||
public final short subType; // из header
|
||||
public final short version; // из header
|
||||
|
||||
// line
|
||||
public final int lineCode;
|
||||
public final int prevLineNumber;
|
||||
public final byte[] prevLineHash32;
|
||||
public final int thisLineNumber;
|
||||
|
||||
// payload
|
||||
public final String toBlockchainName;
|
||||
public final short subType;
|
||||
public final short version;
|
||||
public final int prevConnectionBlockNumber;
|
||||
public final byte[] prevConnectionBlockHash32;
|
||||
public final int connectionSequence;
|
||||
public final String toLogin;
|
||||
public final int toForkNumber;
|
||||
public final int toBlockGlobalNumber;
|
||||
public final byte[] toBlockHash32;
|
||||
|
||||
public ConnectionBody(short subType, short version, byte[] bodyBytes) {
|
||||
Objects.requireNonNull(bodyBytes, "bodyBytes == null");
|
||||
|
||||
this.subType = subType;
|
||||
this.version = version;
|
||||
|
||||
if ((this.version & 0xFFFF) != (VER & 0xFFFF)) {
|
||||
throw new IllegalArgumentException("ConnectionBody version must be 1, got=" + (this.version & 0xFFFF));
|
||||
}
|
||||
if (!isValidSubType(this.subType)) {
|
||||
throw new IllegalArgumentException("Bad connection subType: " + (this.subType & 0xFFFF));
|
||||
}
|
||||
|
||||
// минимум:
|
||||
// lineCode(4) + line(4+32+4) + toBchLen[1]+toBch[1] + global[4] + hash[32]
|
||||
if (bodyBytes.length < 4 + (4 + 32 + 4) + 1 + 1 + 4 + 32) {
|
||||
throw new IllegalArgumentException("ConnectionBody too short");
|
||||
}
|
||||
|
||||
if ((version & 0xFFFF) != 1 || !isValidSubType(subType)) throw new IllegalArgumentException("Bad Connection header");
|
||||
ByteBuffer bb = ByteBuffer.wrap(bodyBytes).order(ByteOrder.BIG_ENDIAN);
|
||||
|
||||
this.lineCode = bb.getInt();
|
||||
|
||||
this.prevLineNumber = bb.getInt();
|
||||
|
||||
this.prevLineHash32 = new byte[32];
|
||||
bb.get(this.prevLineHash32);
|
||||
|
||||
this.thisLineNumber = bb.getInt();
|
||||
|
||||
int bchLen = Byte.toUnsignedInt(bb.get());
|
||||
if (bchLen <= 0) throw new IllegalArgumentException("toBlockchainNameLen is 0");
|
||||
if (bb.remaining() < bchLen + 4 + 32) throw new IllegalArgumentException("Connection payload too short");
|
||||
|
||||
byte[] bchBytes = new byte[bchLen];
|
||||
bb.get(bchBytes);
|
||||
this.toBlockchainName = new String(bchBytes, StandardCharsets.UTF_8);
|
||||
|
||||
this.toBlockGlobalNumber = bb.getInt();
|
||||
|
||||
this.toBlockHash32 = new byte[32];
|
||||
bb.get(this.toBlockHash32);
|
||||
|
||||
if (bb.remaining() != 0) throw new IllegalArgumentException("Unexpected tail bytes, remaining=" + bb.remaining());
|
||||
BodyTargetSupport.ensureMin(bb, 4 + 32 + 4 + 1 + 1 + 2 + 4 + 32, "ConnectionBody too short");
|
||||
this.prevConnectionBlockNumber = bb.getInt();
|
||||
this.prevConnectionBlockHash32 = new byte[32];
|
||||
bb.get(this.prevConnectionBlockHash32);
|
||||
this.connectionSequence = bb.getInt();
|
||||
BodyTargetSupport.ExternalTarget t = BodyTargetSupport.readExternal(bb, "CONNECTION", 0);
|
||||
this.toLogin = t.login();
|
||||
this.toForkNumber = t.forkNumber();
|
||||
this.toBlockGlobalNumber = t.blockNumber();
|
||||
this.toBlockHash32 = t.blockHash32();
|
||||
if (bb.hasRemaining()) throw new IllegalArgumentException("Unexpected ConnectionBody tail");
|
||||
}
|
||||
|
||||
public ConnectionBody(int lineCode,
|
||||
int prevLineNumber,
|
||||
byte[] prevLineHash32,
|
||||
int thisLineNumber,
|
||||
short subType,
|
||||
String toBlockchainName,
|
||||
int toBlockGlobalNumber,
|
||||
byte[] toBlockHash32) {
|
||||
|
||||
Objects.requireNonNull(toBlockchainName, "toBlockchainName == null");
|
||||
Objects.requireNonNull(toBlockHash32, "toBlockHash32 == null");
|
||||
|
||||
if (lineCode < 0) throw new IllegalArgumentException("lineCode < 0");
|
||||
if (!isValidSubType(subType)) throw new IllegalArgumentException("Bad connection subType: " + (subType & 0xFFFF));
|
||||
|
||||
if (toBlockchainName.isBlank()) throw new IllegalArgumentException("toBlockchainName is blank");
|
||||
// Железное правило формата: bchName -> login + "-NNN"
|
||||
if (BlockchainNameUtil.loginFromBlockchainName(toBlockchainName) == null) {
|
||||
throw new IllegalArgumentException("toBlockchainName must match login+\"-NNN\": " + toBlockchainName);
|
||||
}
|
||||
|
||||
if (toBlockGlobalNumber < 0) throw new IllegalArgumentException("toBlockGlobalNumber < 0");
|
||||
if (toBlockHash32.length != 32) throw new IllegalArgumentException("toBlockHash32 != 32");
|
||||
|
||||
this.lineCode = lineCode;
|
||||
|
||||
this.prevLineNumber = prevLineNumber;
|
||||
this.prevLineHash32 = (prevLineHash32 == null ? new byte[32] : Arrays.copyOf(prevLineHash32, 32));
|
||||
this.thisLineNumber = thisLineNumber;
|
||||
|
||||
public ConnectionBody(int prevConnectionBlockNumber, byte[] prevConnectionBlockHash32, int connectionSequence, short subType,
|
||||
String toLogin, int toForkNumber, int toBlockGlobalNumber, byte[] toBlockHash32) {
|
||||
if (!isValidSubType(subType)) throw new IllegalArgumentException("Bad connection fields");
|
||||
BodyTargetSupport.validateExternal(toLogin, toForkNumber, toBlockGlobalNumber, toBlockHash32, "CONNECTION");
|
||||
this.prevConnectionBlockNumber = prevConnectionBlockNumber;
|
||||
this.prevConnectionBlockHash32 = prevConnectionBlockHash32 == null ? new byte[32] : Arrays.copyOf(prevConnectionBlockHash32, 32);
|
||||
this.connectionSequence = connectionSequence;
|
||||
this.subType = subType;
|
||||
this.version = VER;
|
||||
|
||||
this.toBlockchainName = toBlockchainName;
|
||||
this.toLogin = toLogin;
|
||||
this.toForkNumber = toForkNumber;
|
||||
this.toBlockGlobalNumber = toBlockGlobalNumber;
|
||||
this.toBlockHash32 = Arrays.copyOf(toBlockHash32, 32);
|
||||
}
|
||||
|
||||
private static boolean isValidSubType(short st) {
|
||||
int v = st & 0xFFFF;
|
||||
return v == (MsgSubType.CONNECTION_CLOSE_FRIEND & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_UNCLOSE_FRIEND & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_FRIEND & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_UNFRIEND & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_CONTACT & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_UNCONTACT & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_FOLLOW & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_UNFOLLOW & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_SPOUSE & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_UNSPOUSE & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_PARENT & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_UNPARENT & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_CHILD & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_UNCHILD & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_SIBLING & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_UNSIBLING & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_KNOWN_PERSON & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_UNKNOWN_PERSON & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_SHINE_CONFIRMED & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_SHINE_UNCONFIRMED & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_SHINE_SEEN & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_SHINE_UNSEEN & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_OFFICIAL_ACCOUNT_CONFIRMED & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_OFFICIAL_ACCOUNT_UNCONFIRMED & 0xFFFF);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConnectionBody check() {
|
||||
if (lineCode < 0) throw new IllegalArgumentException("lineCode < 0");
|
||||
if (!isValidSubType(subType)) throw new IllegalArgumentException("Bad connection subType: " + (subType & 0xFFFF));
|
||||
|
||||
// line rule (как было)
|
||||
if (prevLineNumber == -1) {
|
||||
if (!isAllZero32(prevLineHash32)) throw new IllegalArgumentException("prevLineHash32 must be zero when prevLineNumber=-1");
|
||||
if (thisLineNumber != -1) throw new IllegalArgumentException("thisLineNumber must be -1 when prevLineNumber=-1");
|
||||
} else {
|
||||
if (prevLineHash32 == null || prevLineHash32.length != 32) throw new IllegalArgumentException("prevLineHash32 invalid");
|
||||
}
|
||||
|
||||
if (toBlockchainName == null || toBlockchainName.isBlank())
|
||||
throw new IllegalArgumentException("toBlockchainName is blank");
|
||||
|
||||
// гарантируем вычислимый toLogin (иначе target “битый” по стандарту)
|
||||
if (BlockchainNameUtil.loginFromBlockchainName(toBlockchainName) == null)
|
||||
throw new IllegalArgumentException("toBlockchainName must match login+\"-NNN\": " + toBlockchainName);
|
||||
|
||||
if (toBlockGlobalNumber < 0) throw new IllegalArgumentException("toBlockGlobalNumber < 0");
|
||||
if (toBlockHash32 == null || toBlockHash32.length != 32) throw new IllegalArgumentException("toBlockHash32 invalid");
|
||||
|
||||
@Override public ConnectionBody check() {
|
||||
if (!isValidSubType(subType)) throw new IllegalArgumentException("Bad connection fields");
|
||||
if (prevConnectionBlockNumber < 0) throw new IllegalArgumentException("prevConnectionBlockNumber must be >=0");
|
||||
if (prevConnectionBlockHash32 == null || prevConnectionBlockHash32.length != 32) throw new IllegalArgumentException("prevConnectionBlockHash32 invalid");
|
||||
if (connectionSequence <= 0) throw new IllegalArgumentException("connectionSequence must be >=1");
|
||||
BodyTargetSupport.validateExternal(toLogin, toForkNumber, toBlockGlobalNumber, toBlockHash32, "CONNECTION");
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toBytes() {
|
||||
byte[] bchBytes = toBlockchainName.getBytes(StandardCharsets.UTF_8);
|
||||
if (bchBytes.length == 0 || bchBytes.length > 255)
|
||||
throw new IllegalArgumentException("toBlockchainName utf8 len must be 1..255");
|
||||
|
||||
if (toBlockHash32 == null || toBlockHash32.length != 32)
|
||||
throw new IllegalArgumentException("toBlockHash32 != 32");
|
||||
|
||||
int cap = 4 + (4 + 32 + 4)
|
||||
+ 1 + bchBytes.length
|
||||
+ 4 + 32;
|
||||
|
||||
ByteBuffer bb = ByteBuffer.allocate(cap).order(ByteOrder.BIG_ENDIAN);
|
||||
|
||||
bb.putInt(lineCode);
|
||||
|
||||
bb.putInt(prevLineNumber);
|
||||
bb.put(prevLineHash32 == null ? new byte[32] : Arrays.copyOf(prevLineHash32, 32));
|
||||
bb.putInt(thisLineNumber);
|
||||
|
||||
bb.put((byte) bchBytes.length);
|
||||
bb.put(bchBytes);
|
||||
|
||||
bb.putInt(toBlockGlobalNumber);
|
||||
bb.put(toBlockHash32);
|
||||
|
||||
@Override public byte[] toBytes() {
|
||||
check();
|
||||
ByteBuffer bb = ByteBuffer.allocate(4 + 32 + 4 + BodyTargetSupport.externalSize(toLogin)).order(ByteOrder.BIG_ENDIAN);
|
||||
bb.putInt(prevConnectionBlockNumber).put(prevConnectionBlockHash32).putInt(connectionSequence);
|
||||
BodyTargetSupport.writeExternal(bb, toLogin, toForkNumber, toBlockGlobalNumber, toBlockHash32, "CONNECTION");
|
||||
return bb.array();
|
||||
}
|
||||
|
||||
private static boolean isAllZero32(byte[] b) {
|
||||
if (b == null || b.length != 32) return true;
|
||||
for (int i = 0; i < 32; i++) if (b[i] != 0) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ====================== BodyHasLine ====================== */
|
||||
@Override public int lineCode() { return lineCode; }
|
||||
@Override public int prevLineBlockGlobalNumber() { return prevLineNumber; }
|
||||
@Override public byte[] prevLineBlockHash32() { return prevLineHash32 == null ? null : Arrays.copyOf(prevLineHash32, 32); }
|
||||
@Override public int lineSeq() { return thisLineNumber; }
|
||||
|
||||
/* ====================== BodyHasTarget ===================== */
|
||||
@Override public String toBchName() { return toBlockchainName; }
|
||||
@Override public int lineCode() { return 0; }
|
||||
@Override public int prevLineBlockGlobalNumber() { return prevConnectionBlockNumber; }
|
||||
@Override public byte[] prevLineBlockHash32() { return Arrays.copyOf(prevConnectionBlockHash32, 32); }
|
||||
@Override public int lineSeq() { return connectionSequence; }
|
||||
@Override public String toLogin() { return toLogin; }
|
||||
@Override public Integer toForkNumber() { return toForkNumber; }
|
||||
@Override public Integer toBlockGlobalNumber() { return toBlockGlobalNumber; }
|
||||
@Override public byte[] toBlockHashBytes() { return toBlockHash32; }
|
||||
@Override public byte[] toBlockHashBytes() { return Arrays.copyOf(toBlockHash32, 32); }
|
||||
|
||||
private static boolean isValidSubType(short st) {
|
||||
int v = st & 0xFFFF;
|
||||
return v == (MsgSubType.CONNECTION_CLOSE_FRIEND & 0xFFFF) || v == (MsgSubType.CONNECTION_UNCLOSE_FRIEND & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_FRIEND & 0xFFFF) || v == (MsgSubType.CONNECTION_UNFRIEND & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_CONTACT & 0xFFFF) || v == (MsgSubType.CONNECTION_UNCONTACT & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_FOLLOW & 0xFFFF) || v == (MsgSubType.CONNECTION_UNFOLLOW & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_SPOUSE & 0xFFFF) || v == (MsgSubType.CONNECTION_UNSPOUSE & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_PARENT & 0xFFFF) || v == (MsgSubType.CONNECTION_UNPARENT & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_CHILD & 0xFFFF) || v == (MsgSubType.CONNECTION_UNCHILD & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_SIBLING & 0xFFFF) || v == (MsgSubType.CONNECTION_UNSIBLING & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_KNOWN_PERSON & 0xFFFF) || v == (MsgSubType.CONNECTION_UNKNOWN_PERSON & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_SHINE_CONFIRMED & 0xFFFF) || v == (MsgSubType.CONNECTION_SHINE_UNCONFIRMED & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_SHINE_SEEN & 0xFFFF) || v == (MsgSubType.CONNECTION_SHINE_UNSEEN & 0xFFFF)
|
||||
|| v == (MsgSubType.CONNECTION_OFFICIAL_ACCOUNT_CONFIRMED & 0xFFFF) || v == (MsgSubType.CONNECTION_OFFICIAL_ACCOUNT_UNCONFIRMED & 0xFFFF);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,235 @@
|
||||
package blockchain.body;
|
||||
|
||||
import blockchain.MsgSubType;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* TECH_FORK body (type=0, subType=2, version=1).
|
||||
*
|
||||
* TECH_FORK является обычным шагом общей TECH-линии. Поэтому body начинается
|
||||
* тем же line-prefix, что и TECH_CREATE_CHANNEL.
|
||||
*
|
||||
* body bytes (BigEndian):
|
||||
* [4] lineCode (=0 для TECH-линии)
|
||||
* [4] prevLineNumber
|
||||
* [32] prevLineHash32
|
||||
* [4] thisLineNumber
|
||||
*
|
||||
* [32] parentBlockchainKey
|
||||
* [4] forkPointBlockNumber
|
||||
* [32] forkPointBlockHash32
|
||||
* [8] forkPointTimestampMs
|
||||
* [4] parentTipBlockNumber
|
||||
* [32] parentTipBlockHash32
|
||||
* [8] parentTipTimestampMs
|
||||
* [4] discardedBlocksCount
|
||||
* [1] reasonCode
|
||||
* [2] commentUtf8Length
|
||||
* [N] comment UTF-8 (0..1024 bytes)
|
||||
*/
|
||||
public final class ForkBody implements BodyRecord, BodyHasLine {
|
||||
|
||||
public static final short TYPE = 0;
|
||||
public static final short VER = 1;
|
||||
public static final short SUBTYPE = MsgSubType.TECH_FORK;
|
||||
public static final int KEY = ((TYPE & 0xFFFF) << 16) | (VER & 0xFFFF);
|
||||
|
||||
public static final int REASON_ROUTINE_ROTATION = 1;
|
||||
public static final int REASON_POSSIBLE_COMPROMISE = 2;
|
||||
public static final int REASON_CONFIRMED_COMPROMISE_ROLLBACK = 3;
|
||||
public static final int REASON_RECOVERY = 4;
|
||||
|
||||
public static final int MAX_COMMENT_UTF8_LEN = 1024;
|
||||
private static final int LINE_PREFIX_LEN = 4 + 4 + 32 + 4;
|
||||
private static final int FIXED_PAYLOAD_LEN = 32 + 4 + 32 + 8 + 4 + 32 + 8 + 4 + 1 + 2;
|
||||
private static final int FIXED_LEN = LINE_PREFIX_LEN + FIXED_PAYLOAD_LEN;
|
||||
|
||||
public final short subType;
|
||||
public final short version;
|
||||
|
||||
public final int lineCode;
|
||||
public final int prevLineNumber;
|
||||
public final byte[] prevLineHash32;
|
||||
public final int thisLineNumber;
|
||||
|
||||
public final byte[] parentBlockchainKey32;
|
||||
public final int forkPointBlockNumber;
|
||||
public final byte[] forkPointBlockHash32;
|
||||
public final long forkPointTimestampMs;
|
||||
public final int parentTipBlockNumber;
|
||||
public final byte[] parentTipBlockHash32;
|
||||
public final long parentTipTimestampMs;
|
||||
public final int discardedBlocksCount;
|
||||
public final int reasonCode;
|
||||
public final String comment;
|
||||
|
||||
public ForkBody(short subType, short version, byte[] bodyBytes) {
|
||||
Objects.requireNonNull(bodyBytes, "bodyBytes == null");
|
||||
this.subType = subType;
|
||||
this.version = version;
|
||||
|
||||
if ((subType & 0xFFFF) != (SUBTYPE & 0xFFFF)) {
|
||||
throw new IllegalArgumentException("ForkBody subType must be TECH_FORK(2)");
|
||||
}
|
||||
if ((version & 0xFFFF) != (VER & 0xFFFF)) {
|
||||
throw new IllegalArgumentException("ForkBody version must be 1");
|
||||
}
|
||||
if (bodyBytes.length < FIXED_LEN) {
|
||||
throw new IllegalArgumentException("ForkBody too short");
|
||||
}
|
||||
|
||||
ByteBuffer bb = ByteBuffer.wrap(bodyBytes).order(ByteOrder.BIG_ENDIAN);
|
||||
this.lineCode = bb.getInt();
|
||||
this.prevLineNumber = bb.getInt();
|
||||
this.prevLineHash32 = new byte[32];
|
||||
bb.get(this.prevLineHash32);
|
||||
this.thisLineNumber = bb.getInt();
|
||||
|
||||
this.parentBlockchainKey32 = new byte[32];
|
||||
bb.get(this.parentBlockchainKey32);
|
||||
this.forkPointBlockNumber = bb.getInt();
|
||||
this.forkPointBlockHash32 = new byte[32];
|
||||
bb.get(this.forkPointBlockHash32);
|
||||
this.forkPointTimestampMs = bb.getLong();
|
||||
this.parentTipBlockNumber = bb.getInt();
|
||||
this.parentTipBlockHash32 = new byte[32];
|
||||
bb.get(this.parentTipBlockHash32);
|
||||
this.parentTipTimestampMs = bb.getLong();
|
||||
this.discardedBlocksCount = bb.getInt();
|
||||
this.reasonCode = Byte.toUnsignedInt(bb.get());
|
||||
int commentLen = Short.toUnsignedInt(bb.getShort());
|
||||
if (commentLen > MAX_COMMENT_UTF8_LEN) {
|
||||
throw new IllegalArgumentException("ForkBody comment utf8 len must be <=1024");
|
||||
}
|
||||
if (bb.remaining() != commentLen) {
|
||||
throw new IllegalArgumentException("ForkBody tail mismatch: remaining=" + bb.remaining() + " commentLen=" + commentLen);
|
||||
}
|
||||
byte[] commentBytes = new byte[commentLen];
|
||||
bb.get(commentBytes);
|
||||
this.comment = new String(commentBytes, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public ForkBody(int lineCode,
|
||||
int prevLineNumber,
|
||||
byte[] prevLineHash32,
|
||||
int thisLineNumber,
|
||||
byte[] parentBlockchainKey32,
|
||||
int forkPointBlockNumber,
|
||||
byte[] forkPointBlockHash32,
|
||||
long forkPointTimestampMs,
|
||||
int parentTipBlockNumber,
|
||||
byte[] parentTipBlockHash32,
|
||||
long parentTipTimestampMs,
|
||||
int discardedBlocksCount,
|
||||
int reasonCode,
|
||||
String comment) {
|
||||
this.subType = SUBTYPE;
|
||||
this.version = VER;
|
||||
this.lineCode = lineCode;
|
||||
this.prevLineNumber = prevLineNumber;
|
||||
this.prevLineHash32 = copy32(prevLineHash32, "prevLineHash32");
|
||||
this.thisLineNumber = thisLineNumber;
|
||||
this.parentBlockchainKey32 = copy32(parentBlockchainKey32, "parentBlockchainKey32");
|
||||
this.forkPointBlockNumber = forkPointBlockNumber;
|
||||
this.forkPointBlockHash32 = copy32(forkPointBlockHash32, "forkPointBlockHash32");
|
||||
this.forkPointTimestampMs = forkPointTimestampMs;
|
||||
this.parentTipBlockNumber = parentTipBlockNumber;
|
||||
this.parentTipBlockHash32 = copy32(parentTipBlockHash32, "parentTipBlockHash32");
|
||||
this.parentTipTimestampMs = parentTipTimestampMs;
|
||||
this.discardedBlocksCount = discardedBlocksCount;
|
||||
this.reasonCode = reasonCode;
|
||||
this.comment = normalizeComment(comment);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ForkBody check() {
|
||||
if ((subType & 0xFFFF) != (SUBTYPE & 0xFFFF)) {
|
||||
throw new IllegalArgumentException("ForkBody subType must be TECH_FORK(2)");
|
||||
}
|
||||
if ((version & 0xFFFF) != (VER & 0xFFFF)) {
|
||||
throw new IllegalArgumentException("ForkBody version must be 1");
|
||||
}
|
||||
if (lineCode != 0) throw new IllegalArgumentException("ForkBody lineCode must be 0");
|
||||
if (prevLineNumber < 0) throw new IllegalArgumentException("prevLineNumber must be >=0");
|
||||
require32(prevLineHash32, "prevLineHash32");
|
||||
if (thisLineNumber <= 0) throw new IllegalArgumentException("thisLineNumber must be >=1");
|
||||
|
||||
require32(parentBlockchainKey32, "parentBlockchainKey32");
|
||||
require32(forkPointBlockHash32, "forkPointBlockHash32");
|
||||
require32(parentTipBlockHash32, "parentTipBlockHash32");
|
||||
if (forkPointBlockNumber < 0) throw new IllegalArgumentException("forkPointBlockNumber < 0");
|
||||
if (parentTipBlockNumber < forkPointBlockNumber) {
|
||||
throw new IllegalArgumentException("parentTipBlockNumber < forkPointBlockNumber");
|
||||
}
|
||||
if (forkPointTimestampMs < 0) throw new IllegalArgumentException("forkPointTimestampMs < 0");
|
||||
if (parentTipTimestampMs < 0) throw new IllegalArgumentException("parentTipTimestampMs < 0");
|
||||
int expectedDiscarded = parentTipBlockNumber - forkPointBlockNumber;
|
||||
if (discardedBlocksCount != expectedDiscarded) {
|
||||
throw new IllegalArgumentException("discardedBlocksCount must equal parentTipBlockNumber - forkPointBlockNumber");
|
||||
}
|
||||
if (!isReasonSupported(reasonCode)) {
|
||||
throw new IllegalArgumentException("Unsupported fork reasonCode=" + reasonCode);
|
||||
}
|
||||
byte[] commentUtf8 = normalizeComment(comment).getBytes(StandardCharsets.UTF_8);
|
||||
if (commentUtf8.length > MAX_COMMENT_UTF8_LEN) {
|
||||
throw new IllegalArgumentException("ForkBody comment utf8 len must be <=1024");
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toBytes() {
|
||||
check();
|
||||
byte[] commentUtf8 = normalizeComment(comment).getBytes(StandardCharsets.UTF_8);
|
||||
ByteBuffer bb = ByteBuffer.allocate(FIXED_LEN + commentUtf8.length).order(ByteOrder.BIG_ENDIAN);
|
||||
bb.putInt(lineCode);
|
||||
bb.putInt(prevLineNumber);
|
||||
bb.put(prevLineHash32);
|
||||
bb.putInt(thisLineNumber);
|
||||
bb.put(parentBlockchainKey32);
|
||||
bb.putInt(forkPointBlockNumber);
|
||||
bb.put(forkPointBlockHash32);
|
||||
bb.putLong(forkPointTimestampMs);
|
||||
bb.putInt(parentTipBlockNumber);
|
||||
bb.put(parentTipBlockHash32);
|
||||
bb.putLong(parentTipTimestampMs);
|
||||
bb.putInt(discardedBlocksCount);
|
||||
bb.put((byte) reasonCode);
|
||||
bb.putShort((short) commentUtf8.length);
|
||||
bb.put(commentUtf8);
|
||||
return bb.array();
|
||||
}
|
||||
|
||||
@Override public int lineCode() { return lineCode; }
|
||||
@Override public int prevLineBlockGlobalNumber() { return prevLineNumber; }
|
||||
@Override public byte[] prevLineBlockHash32() { return Arrays.copyOf(prevLineHash32, 32); }
|
||||
@Override public int lineSeq() { return thisLineNumber; }
|
||||
|
||||
public static boolean isReasonSupported(int code) {
|
||||
return code == REASON_ROUTINE_ROTATION
|
||||
|| code == REASON_POSSIBLE_COMPROMISE
|
||||
|| code == REASON_CONFIRMED_COMPROMISE_ROLLBACK
|
||||
|| code == REASON_RECOVERY;
|
||||
}
|
||||
|
||||
private static byte[] copy32(byte[] value, String name) {
|
||||
require32(value, name);
|
||||
return Arrays.copyOf(value, 32);
|
||||
}
|
||||
|
||||
private static void require32(byte[] value, String name) {
|
||||
if (value == null || value.length != 32) {
|
||||
throw new IllegalArgumentException(name + " must be 32 bytes");
|
||||
}
|
||||
}
|
||||
|
||||
private static String normalizeComment(String value) {
|
||||
if (value == null) return "";
|
||||
return value.trim().replace("\r\n", "\n").replace('\r', '\n');
|
||||
}
|
||||
}
|
||||
+55
-65
@@ -5,123 +5,113 @@ import utils.config.ShineSignatureConstants;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* HeaderBody — type=0, version=1.
|
||||
*
|
||||
* В новом формате type/subType/version живут в HEADER блока,
|
||||
* поэтому bodyBytes для HeaderBody содержат только payload:
|
||||
* HeaderBody — type=0, subType=0, version=1.
|
||||
*
|
||||
* bodyBytes (BigEndian):
|
||||
* [TAG_LEN] tag ASCII "SHiNE"
|
||||
* [1] loginLength=N (uint8)
|
||||
* [N] login UTF-8
|
||||
* [TAG_LEN] tag ASCII
|
||||
* [1] loginLength (uint8)
|
||||
* [N] login UTF-8
|
||||
* [32] initialBlockchainKey32
|
||||
*
|
||||
* initialBlockchainKey32 is the public blockchain-signing key that created fork #1.
|
||||
* It is historical identity metadata and MUST remain byte-for-byte unchanged when a
|
||||
* preserved prefix (including block #0) is republished into later forks.
|
||||
*/
|
||||
public final class HeaderBody implements BodyRecord {
|
||||
|
||||
public static final short TYPE = 0;
|
||||
public static final short VER = 1;
|
||||
|
||||
public static final short VER = 1;
|
||||
public static final int KEY = ((TYPE & 0xFFFF) << 16) | (VER & 0xFFFF);
|
||||
|
||||
/** Для header subType всегда 0 (служебная совместимость). */
|
||||
public static final short SUBTYPE_COMPAT = 0;
|
||||
|
||||
/** TAG формата (ASCII). */
|
||||
public static final String TAG = ShineSignatureConstants.BLOCKCHAIN_HEADER_TAG;
|
||||
|
||||
private static final byte[] TAG_ASCII = TAG.getBytes(StandardCharsets.US_ASCII);
|
||||
private static final int TAG_LEN = TAG_ASCII.length;
|
||||
|
||||
public final short subType; // всегда 0 (из заголовка блока)
|
||||
public final short version; // из заголовка блока
|
||||
public final String tag; // "SHiNE"
|
||||
public final short subType;
|
||||
public final short version;
|
||||
public final String tag;
|
||||
public final String login;
|
||||
public final byte[] initialBlockchainKey32;
|
||||
|
||||
/** Десериализация из payload bodyBytes (без type/subType/version). */
|
||||
public HeaderBody(short subType, short version, byte[] bodyBytes) {
|
||||
Objects.requireNonNull(bodyBytes, "bodyBytes == null");
|
||||
|
||||
this.subType = subType;
|
||||
this.version = version;
|
||||
|
||||
if ((this.subType & 0xFFFF) != (SUBTYPE_COMPAT & 0xFFFF)) {
|
||||
throw new IllegalArgumentException("HeaderBody subType must be 0, got=" + (this.subType & 0xFFFF));
|
||||
}
|
||||
if ((this.version & 0xFFFF) != (VER & 0xFFFF)) {
|
||||
throw new IllegalArgumentException("HeaderBody version must be 1, got=" + (this.version & 0xFFFF));
|
||||
}
|
||||
|
||||
// минимум: tag[TAG_LEN] + loginLen[1]
|
||||
if (bodyBytes.length < TAG_LEN + 1) throw new IllegalArgumentException("HeaderBody too short");
|
||||
if ((subType & 0xFFFF) != 0) throw new IllegalArgumentException("HeaderBody subType must be 0");
|
||||
if ((version & 0xFFFF) != (VER & 0xFFFF)) throw new IllegalArgumentException("HeaderBody version must be 1");
|
||||
if (bodyBytes.length < TAG_LEN + 1 + 1 + 32) throw new IllegalArgumentException("HeaderBody too short");
|
||||
|
||||
ByteBuffer bb = ByteBuffer.wrap(bodyBytes).order(ByteOrder.BIG_ENDIAN);
|
||||
|
||||
byte[] tagBytes = new byte[TAG_LEN];
|
||||
bb.get(tagBytes);
|
||||
String t = new String(tagBytes, StandardCharsets.US_ASCII);
|
||||
if (!TAG.equals(t)) throw new IllegalArgumentException("Bad tag: " + t);
|
||||
this.tag = t;
|
||||
String parsedTag = new String(tagBytes, StandardCharsets.US_ASCII);
|
||||
if (!TAG.equals(parsedTag)) throw new IllegalArgumentException("Bad tag: " + parsedTag);
|
||||
this.tag = parsedTag;
|
||||
|
||||
int loginLen = Byte.toUnsignedInt(bb.get());
|
||||
if (loginLen <= 0 || bb.remaining() < loginLen)
|
||||
throw new IllegalArgumentException("Bad login length");
|
||||
|
||||
if (loginLen <= 0 || bb.remaining() != loginLen + 32) throw new IllegalArgumentException("Bad HeaderBody login/key length");
|
||||
byte[] loginBytes = new byte[loginLen];
|
||||
bb.get(loginBytes);
|
||||
this.login = new String(loginBytes, StandardCharsets.UTF_8);
|
||||
|
||||
if (bb.remaining() != 0) throw new IllegalArgumentException("Unexpected tail bytes, remaining=" + bb.remaining());
|
||||
this.login = BodyTargetSupport.decodeStrictUtf8(loginBytes, "HeaderBody login");
|
||||
this.initialBlockchainKey32 = new byte[32];
|
||||
bb.get(this.initialBlockchainKey32);
|
||||
requireNonZeroKey(this.initialBlockchainKey32);
|
||||
if (bb.hasRemaining()) throw new IllegalArgumentException("Unexpected HeaderBody tail");
|
||||
}
|
||||
|
||||
/** Создание “вручную”. */
|
||||
public HeaderBody(String login) {
|
||||
public HeaderBody(String login, byte[] initialBlockchainKey32) {
|
||||
Objects.requireNonNull(login, "login == null");
|
||||
Objects.requireNonNull(initialBlockchainKey32, "initialBlockchainKey32 == null");
|
||||
this.subType = SUBTYPE_COMPAT;
|
||||
this.version = VER;
|
||||
this.tag = TAG;
|
||||
this.login = login;
|
||||
this.initialBlockchainKey32 = Arrays.copyOf(initialBlockchainKey32, initialBlockchainKey32.length);
|
||||
check();
|
||||
}
|
||||
|
||||
@Override
|
||||
public HeaderBody check() {
|
||||
if ((subType & 0xFFFF) != (SUBTYPE_COMPAT & 0xFFFF))
|
||||
throw new IllegalArgumentException("HeaderBody subType must be 0");
|
||||
|
||||
if (login == null || login.isBlank())
|
||||
throw new IllegalArgumentException("Login is blank");
|
||||
if (!login.matches("^[A-Za-z0-9_]+$"))
|
||||
throw new IllegalArgumentException("Login must match ^[A-Za-z0-9_]+$");
|
||||
|
||||
if ((subType & 0xFFFF) != 0) throw new IllegalArgumentException("HeaderBody subType must be 0");
|
||||
if (login == null || login.isBlank()) throw new IllegalArgumentException("Login is blank");
|
||||
byte[] loginUtf8 = login.getBytes(StandardCharsets.UTF_8);
|
||||
// В HEADER хранится неизменяемое написание зарегистрированного login@namespace.
|
||||
// Namespace остаётся lowercase, регистр локальной части сохраняется байт-в-байт.
|
||||
boolean legacyLogin = login.matches("^[A-Za-z0-9_]+$");
|
||||
boolean namespacedLogin = loginUtf8.length <= 30 && login.matches("^[A-Za-z0-9_]+@[a-z0-9_]+$");
|
||||
if (!legacyLogin && !namespacedLogin) {
|
||||
throw new IllegalArgumentException("Login must be legacy [A-Za-z0-9_]+ or registered-case login@namespace (max 30 bytes)");
|
||||
}
|
||||
if (loginUtf8.length == 0 || loginUtf8.length > 255) throw new IllegalArgumentException("Login utf8 len must be 1..255");
|
||||
requireNonZeroKey(initialBlockchainKey32);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toBytes() {
|
||||
check();
|
||||
byte[] loginUtf8 = login.getBytes(StandardCharsets.UTF_8);
|
||||
if (loginUtf8.length == 0 || loginUtf8.length > 255)
|
||||
throw new IllegalArgumentException("Login utf8 len must be 1..255");
|
||||
|
||||
int cap = TAG_LEN + 1 + loginUtf8.length;
|
||||
|
||||
ByteBuffer bb = ByteBuffer.allocate(cap).order(ByteOrder.BIG_ENDIAN);
|
||||
ByteBuffer bb = ByteBuffer.allocate(TAG_LEN + 1 + loginUtf8.length + 32).order(ByteOrder.BIG_ENDIAN);
|
||||
bb.put(TAG_ASCII);
|
||||
bb.put((byte) loginUtf8.length);
|
||||
bb.put(loginUtf8);
|
||||
|
||||
bb.put(initialBlockchainKey32);
|
||||
return bb.array();
|
||||
}
|
||||
|
||||
private static void requireNonZeroKey(byte[] key) {
|
||||
if (key == null || key.length != 32) throw new IllegalArgumentException("initialBlockchainKey32 must be 32 bytes");
|
||||
boolean any = false;
|
||||
for (byte b : key) any |= b != 0;
|
||||
if (!any) throw new IllegalArgumentException("initialBlockchainKey32 must not be zero");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return """
|
||||
HeaderBody {
|
||||
тип записи : HEADER (type=0, ver=1) [в заголовке блока]
|
||||
subType : 0 (compat)
|
||||
тег формата : "%s"
|
||||
login владельца : "%s"
|
||||
}
|
||||
""".formatted(tag, login);
|
||||
return "HeaderBody{login='" + login + "', initialBlockchainKey32=<32 bytes>}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+31
-93
@@ -4,130 +4,68 @@ import blockchain.MsgSubType;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* ReactionBody — type=2, version=1 (в заголовке блока).
|
||||
*
|
||||
* subType (в заголовке блока):
|
||||
* 1 = LIKE
|
||||
* 2 = UNLIKE
|
||||
*
|
||||
* bodyBytes (BigEndian), новый формат:
|
||||
* [1] toBlockchainNameLen (uint8)
|
||||
* [N] toBlockchainName UTF-8
|
||||
* [4] toBlockGlobalNumber (int32)
|
||||
* [32] toBlockHash32 (raw 32 bytes)
|
||||
*
|
||||
* ЛИНИИ НЕТ.
|
||||
*/
|
||||
/** REACTION LIKE/UNLIKE. External target = login + forkNumber + blockNumber + hash32. */
|
||||
public final class ReactionBody implements BodyRecord, BodyHasTarget {
|
||||
|
||||
public static final short TYPE = 2;
|
||||
public static final short VER = 1;
|
||||
|
||||
public static final short VER = 1;
|
||||
public static final int KEY = ((TYPE & 0xFFFF) << 16) | (VER & 0xFFFF);
|
||||
|
||||
public final short subType; // из header
|
||||
public final short version; // из header
|
||||
|
||||
public final String toBlockchainName;
|
||||
public final short subType;
|
||||
public final short version;
|
||||
public final String toLogin;
|
||||
public final int toForkNumber;
|
||||
public final int toBlockGlobalNumber;
|
||||
public final byte[] toBlockHash32;
|
||||
|
||||
public ReactionBody(short subType, short version, byte[] bodyBytes) {
|
||||
Objects.requireNonNull(bodyBytes, "bodyBytes == null");
|
||||
|
||||
this.subType = subType;
|
||||
this.version = version;
|
||||
|
||||
if ((this.version & 0xFFFF) != (VER & 0xFFFF)) {
|
||||
throw new IllegalArgumentException("ReactionBody version must be 1, got=" + (this.version & 0xFFFF));
|
||||
}
|
||||
if (!isSupportedSubType(this.subType)) {
|
||||
throw new IllegalArgumentException("Bad reaction subType: " + (this.subType & 0xFFFF));
|
||||
}
|
||||
|
||||
// минимум: nameLen[1]+name[1]+global[4]+hash[32]
|
||||
if (bodyBytes.length < 1 + 1 + 4 + 32) throw new IllegalArgumentException("ReactionBody too short");
|
||||
|
||||
if ((version & 0xFFFF) != (VER & 0xFFFF)) throw new IllegalArgumentException("ReactionBody version must be 1");
|
||||
if (!isSupportedSubType(subType)) throw new IllegalArgumentException("Bad reaction subType: " + (subType & 0xFFFF));
|
||||
ByteBuffer bb = ByteBuffer.wrap(bodyBytes).order(ByteOrder.BIG_ENDIAN);
|
||||
|
||||
int nameLen = Byte.toUnsignedInt(bb.get());
|
||||
if (nameLen <= 0) throw new IllegalArgumentException("toBlockchainNameLen is 0");
|
||||
if (bb.remaining() < nameLen + 4 + 32) throw new IllegalArgumentException("ReactionBody payload too short");
|
||||
|
||||
byte[] nameBytes = new byte[nameLen];
|
||||
bb.get(nameBytes);
|
||||
this.toBlockchainName = new String(nameBytes, StandardCharsets.UTF_8);
|
||||
|
||||
this.toBlockGlobalNumber = bb.getInt();
|
||||
|
||||
this.toBlockHash32 = new byte[32];
|
||||
bb.get(this.toBlockHash32);
|
||||
|
||||
if (bb.remaining() != 0) throw new IllegalArgumentException("Unexpected tail bytes, remaining=" + bb.remaining());
|
||||
BodyTargetSupport.ExternalTarget t = BodyTargetSupport.readExternal(bb, "REACTION", 0);
|
||||
this.toLogin = t.login();
|
||||
this.toForkNumber = t.forkNumber();
|
||||
this.toBlockGlobalNumber = t.blockNumber();
|
||||
this.toBlockHash32 = t.blockHash32();
|
||||
if (bb.hasRemaining()) throw new IllegalArgumentException("Unexpected REACTION tail");
|
||||
}
|
||||
|
||||
public ReactionBody(String toBlockchainName, int toBlockGlobalNumber, byte[] toBlockHash32) {
|
||||
Objects.requireNonNull(toBlockchainName, "toBlockchainName == null");
|
||||
Objects.requireNonNull(toBlockHash32, "toBlockHash32 == null");
|
||||
|
||||
this.subType = MsgSubType.REACTION_LIKE;
|
||||
public ReactionBody(short subType, String toLogin, int toForkNumber, int toBlockGlobalNumber, byte[] toBlockHash32) {
|
||||
if (!isSupportedSubType(subType)) throw new IllegalArgumentException("Bad reaction subType");
|
||||
BodyTargetSupport.validateExternal(toLogin, toForkNumber, toBlockGlobalNumber, toBlockHash32, "REACTION");
|
||||
this.subType = subType;
|
||||
this.version = VER;
|
||||
|
||||
if (toBlockchainName.isBlank()) throw new IllegalArgumentException("toBlockchainName is blank");
|
||||
if (toBlockGlobalNumber < 0) throw new IllegalArgumentException("toBlockGlobalNumber < 0");
|
||||
if (toBlockHash32.length != 32) throw new IllegalArgumentException("toBlockHash32 != 32");
|
||||
|
||||
this.toBlockchainName = toBlockchainName;
|
||||
this.toLogin = toLogin;
|
||||
this.toForkNumber = toForkNumber;
|
||||
this.toBlockGlobalNumber = toBlockGlobalNumber;
|
||||
this.toBlockHash32 = Arrays.copyOf(toBlockHash32, 32);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReactionBody check() {
|
||||
if (!isSupportedSubType(subType))
|
||||
throw new IllegalArgumentException("Bad reaction subType: " + (subType & 0xFFFF));
|
||||
|
||||
if (toBlockchainName == null || toBlockchainName.isBlank())
|
||||
throw new IllegalArgumentException("toBlockchainName is blank");
|
||||
if (toBlockGlobalNumber < 0)
|
||||
throw new IllegalArgumentException("toBlockGlobalNumber < 0");
|
||||
if (toBlockHash32 == null || toBlockHash32.length != 32)
|
||||
throw new IllegalArgumentException("toBlockHash32 invalid");
|
||||
|
||||
@Override public ReactionBody check() {
|
||||
if (!isSupportedSubType(subType)) throw new IllegalArgumentException("Bad reaction subType");
|
||||
BodyTargetSupport.validateExternal(toLogin, toForkNumber, toBlockGlobalNumber, toBlockHash32, "REACTION");
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toBytes() {
|
||||
byte[] nameBytes = toBlockchainName.getBytes(StandardCharsets.UTF_8);
|
||||
if (nameBytes.length == 0 || nameBytes.length > 255)
|
||||
throw new IllegalArgumentException("toBlockchainName utf8 len must be 1..255");
|
||||
|
||||
int cap = 1 + nameBytes.length + 4 + 32;
|
||||
|
||||
ByteBuffer bb = ByteBuffer.allocate(cap).order(ByteOrder.BIG_ENDIAN);
|
||||
bb.put((byte) nameBytes.length);
|
||||
bb.put(nameBytes);
|
||||
bb.putInt(toBlockGlobalNumber);
|
||||
bb.put(toBlockHash32);
|
||||
|
||||
@Override public byte[] toBytes() {
|
||||
check();
|
||||
ByteBuffer bb = ByteBuffer.allocate(BodyTargetSupport.externalSize(toLogin)).order(ByteOrder.BIG_ENDIAN);
|
||||
BodyTargetSupport.writeExternal(bb, toLogin, toForkNumber, toBlockGlobalNumber, toBlockHash32, "REACTION");
|
||||
return bb.array();
|
||||
}
|
||||
|
||||
/* ====================== BodyHasTarget ====================== */
|
||||
|
||||
@Override public String toBchName() { return toBlockchainName; }
|
||||
@Override public String toLogin() { return toLogin; }
|
||||
@Override public Integer toForkNumber() { return toForkNumber; }
|
||||
@Override public Integer toBlockGlobalNumber() { return toBlockGlobalNumber; }
|
||||
@Override public byte[] toBlockHashBytes() { return toBlockHash32; }
|
||||
@Override public byte[] toBlockHashBytes() { return Arrays.copyOf(toBlockHash32, 32); }
|
||||
|
||||
private static boolean isSupportedSubType(short subType) {
|
||||
int st = subType & 0xFFFF;
|
||||
return st == (MsgSubType.REACTION_LIKE & 0xFFFF)
|
||||
|| st == (MsgSubType.REACTION_UNLIKE & 0xFFFF);
|
||||
return st == (MsgSubType.REACTION_LIKE & 0xFFFF) || st == (MsgSubType.REACTION_UNLIKE & 0xFFFF);
|
||||
}
|
||||
}
|
||||
|
||||
+37
-116
@@ -10,157 +10,78 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* StatusActionBody — type=5, ver=1.
|
||||
*
|
||||
* Все STATUS_ACTION имеют target на конкретный блок и опциональный текст-пояснение.
|
||||
*
|
||||
* Формат bodyBytes (BigEndian):
|
||||
* [1] toBlockchainNameLen (uint8)
|
||||
* [N] toBlockchainName UTF-8
|
||||
* [4] toBlockGlobalNumber
|
||||
* [32] toBlockHash32
|
||||
* [2] textLenBytes (uint16)
|
||||
* [M] text UTF-8
|
||||
*/
|
||||
/** STATUS_ACTION. External target = login + forkNumber + blockNumber + hash32. */
|
||||
public final class StatusActionBody implements BodyRecord, BodyHasTarget {
|
||||
|
||||
public static final short TYPE = 5;
|
||||
public static final short VER = 1;
|
||||
public static final int KEY = ((TYPE & 0xFFFF) << 16) | (VER & 0xFFFF);
|
||||
|
||||
public final short subType;
|
||||
public final short version;
|
||||
public final String toBlockchainName;
|
||||
public final String toLogin;
|
||||
public final int toForkNumber;
|
||||
public final int toBlockGlobalNumber;
|
||||
public final byte[] toBlockHash32;
|
||||
public final String message;
|
||||
|
||||
public StatusActionBody(short subType, short version, byte[] bodyBytes) {
|
||||
Objects.requireNonNull(bodyBytes, "bodyBytes == null");
|
||||
this.subType = subType;
|
||||
this.version = version;
|
||||
|
||||
if ((version & 0xFFFF) != (VER & 0xFFFF)) {
|
||||
throw new IllegalArgumentException("StatusActionBody version must be 1, got=" + (version & 0xFFFF));
|
||||
}
|
||||
if (!isSupportedSubType(subType)) {
|
||||
throw new IllegalArgumentException("Unsupported STATUS_ACTION subType: " + (subType & 0xFFFF));
|
||||
}
|
||||
|
||||
this.subType = subType; this.version = version;
|
||||
if ((version & 0xFFFF) != 1 || !isSupportedSubType(subType)) throw new IllegalArgumentException("Bad STATUS_ACTION header");
|
||||
ByteBuffer bb = ByteBuffer.wrap(bodyBytes).order(ByteOrder.BIG_ENDIAN);
|
||||
ensureMin(bb, 1 + 1 + 4 + 32 + 2, "STATUS_ACTION too short");
|
||||
|
||||
int nameLen = Byte.toUnsignedInt(bb.get());
|
||||
if (nameLen <= 0) throw new IllegalArgumentException("STATUS_ACTION toBlockchainNameLen is 0");
|
||||
ensureMin(bb, nameLen + 4 + 32 + 2, "STATUS_ACTION payload too short");
|
||||
|
||||
byte[] nameBytes = new byte[nameLen];
|
||||
bb.get(nameBytes);
|
||||
this.toBlockchainName = new String(nameBytes, StandardCharsets.UTF_8);
|
||||
this.toBlockGlobalNumber = bb.getInt();
|
||||
this.toBlockHash32 = new byte[32];
|
||||
bb.get(this.toBlockHash32);
|
||||
this.message = readStrictUtf8Len16AllowEmpty(bb, "StatusActionBody text");
|
||||
|
||||
ensureNoTail(bb, "StatusActionBody");
|
||||
BodyTargetSupport.ExternalTarget t = BodyTargetSupport.readExternal(bb, "STATUS_ACTION", 2);
|
||||
this.toLogin = t.login(); this.toForkNumber = t.forkNumber(); this.toBlockGlobalNumber = t.blockNumber(); this.toBlockHash32 = t.blockHash32();
|
||||
this.message = readText(bb);
|
||||
if (bb.hasRemaining()) throw new IllegalArgumentException("Unexpected STATUS_ACTION tail");
|
||||
}
|
||||
|
||||
public StatusActionBody(short subType, String toBlockchainName, int toBlockGlobalNumber, byte[] toBlockHash32, String message) {
|
||||
Objects.requireNonNull(toBlockchainName, "toBlockchainName == null");
|
||||
Objects.requireNonNull(toBlockHash32, "toBlockHash32 == null");
|
||||
public StatusActionBody(short subType, String toLogin, int toForkNumber, int toBlockGlobalNumber, byte[] toBlockHash32, String message) {
|
||||
Objects.requireNonNull(message, "message == null");
|
||||
if (!isSupportedSubType(subType)) throw new IllegalArgumentException("Unsupported STATUS_ACTION subType");
|
||||
if (toBlockchainName.isBlank()) throw new IllegalArgumentException("toBlockchainName is blank");
|
||||
if (toBlockGlobalNumber < 0) throw new IllegalArgumentException("toBlockGlobalNumber < 0");
|
||||
if (toBlockHash32.length != 32) throw new IllegalArgumentException("toBlockHash32 != 32");
|
||||
|
||||
this.subType = subType;
|
||||
this.version = VER;
|
||||
this.toBlockchainName = toBlockchainName;
|
||||
this.toBlockGlobalNumber = toBlockGlobalNumber;
|
||||
this.toBlockHash32 = Arrays.copyOf(toBlockHash32, 32);
|
||||
this.message = message;
|
||||
BodyTargetSupport.validateExternal(toLogin, toForkNumber, toBlockGlobalNumber, toBlockHash32, "STATUS_ACTION");
|
||||
this.subType = subType; this.version = VER; this.toLogin = toLogin; this.toForkNumber = toForkNumber;
|
||||
this.toBlockGlobalNumber = toBlockGlobalNumber; this.toBlockHash32 = Arrays.copyOf(toBlockHash32, 32); this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StatusActionBody check() {
|
||||
if (!isSupportedSubType(subType)) {
|
||||
throw new IllegalArgumentException("Bad STATUS_ACTION subType: " + (subType & 0xFFFF));
|
||||
}
|
||||
if (toBlockchainName == null || toBlockchainName.isBlank()) {
|
||||
throw new IllegalArgumentException("STATUS_ACTION toBlockchainName is blank");
|
||||
}
|
||||
if (toBlockGlobalNumber < 0) throw new IllegalArgumentException("toBlockGlobalNumber < 0");
|
||||
if (toBlockHash32 == null || toBlockHash32.length != 32) {
|
||||
throw new IllegalArgumentException("toBlockHash32 invalid");
|
||||
}
|
||||
@Override public StatusActionBody check() {
|
||||
if (!isSupportedSubType(subType)) throw new IllegalArgumentException("Bad STATUS_ACTION subType");
|
||||
BodyTargetSupport.validateExternal(toLogin, toForkNumber, toBlockGlobalNumber, toBlockHash32, "STATUS_ACTION");
|
||||
if (message == null) throw new IllegalArgumentException("message is null");
|
||||
if (message.getBytes(StandardCharsets.UTF_8).length > 65535) throw new IllegalArgumentException("Text too long");
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toBytes() {
|
||||
byte[] msgUtf8 = message.getBytes(StandardCharsets.UTF_8);
|
||||
if (msgUtf8.length > 65535) throw new IllegalArgumentException("Text too long (>65535 bytes)");
|
||||
|
||||
byte[] nameUtf8 = toBlockchainName.getBytes(StandardCharsets.UTF_8);
|
||||
if (nameUtf8.length == 0 || nameUtf8.length > 255) {
|
||||
throw new IllegalArgumentException("STATUS_ACTION toBlockchainName utf8 len must be 1..255");
|
||||
}
|
||||
|
||||
ByteBuffer bb = ByteBuffer.allocate(1 + nameUtf8.length + 4 + 32 + 2 + msgUtf8.length)
|
||||
.order(ByteOrder.BIG_ENDIAN);
|
||||
bb.put((byte) nameUtf8.length);
|
||||
bb.put(nameUtf8);
|
||||
bb.putInt(toBlockGlobalNumber);
|
||||
bb.put(toBlockHash32);
|
||||
bb.putShort((short) msgUtf8.length);
|
||||
bb.put(msgUtf8);
|
||||
@Override public byte[] toBytes() {
|
||||
check();
|
||||
byte[] text = message.getBytes(StandardCharsets.UTF_8);
|
||||
ByteBuffer bb = ByteBuffer.allocate(BodyTargetSupport.externalSize(toLogin) + 2 + text.length).order(ByteOrder.BIG_ENDIAN);
|
||||
BodyTargetSupport.writeExternal(bb, toLogin, toForkNumber, toBlockGlobalNumber, toBlockHash32, "STATUS_ACTION");
|
||||
bb.putShort((short) text.length).put(text);
|
||||
return bb.array();
|
||||
}
|
||||
|
||||
@Override public String toBchName() { return toBlockchainName; }
|
||||
@Override public String toLogin() { return toLogin; }
|
||||
@Override public Integer toForkNumber() { return toForkNumber; }
|
||||
@Override public Integer toBlockGlobalNumber() { return toBlockGlobalNumber; }
|
||||
@Override public byte[] toBlockHashBytes() { return toBlockHash32; }
|
||||
@Override public byte[] toBlockHashBytes() { return Arrays.copyOf(toBlockHash32, 32); }
|
||||
|
||||
private static boolean isSupportedSubType(short subType) {
|
||||
int st = subType & 0xFFFF;
|
||||
return st == (MsgSubType.STATUS_DONE_ONCE & 0xFFFF)
|
||||
|| st == (MsgSubType.STATUS_LEARNED & 0xFFFF)
|
||||
|| st == (MsgSubType.STATUS_SERVICE_PASSED & 0xFFFF)
|
||||
|| st == (MsgSubType.STATUS_CONFIRMED & 0xFFFF)
|
||||
|| st == (MsgSubType.STATUS_INTERESTED & 0xFFFF)
|
||||
|| st == (MsgSubType.STATUS_STARTED & 0xFFFF)
|
||||
|| st == (MsgSubType.STATUS_IN_STUDY & 0xFFFF)
|
||||
|| st == (MsgSubType.STATUS_ABANDONED & 0xFFFF)
|
||||
return st == (MsgSubType.STATUS_DONE_ONCE & 0xFFFF) || st == (MsgSubType.STATUS_LEARNED & 0xFFFF)
|
||||
|| st == (MsgSubType.STATUS_SERVICE_PASSED & 0xFFFF) || st == (MsgSubType.STATUS_CONFIRMED & 0xFFFF)
|
||||
|| st == (MsgSubType.STATUS_INTERESTED & 0xFFFF) || st == (MsgSubType.STATUS_STARTED & 0xFFFF)
|
||||
|| st == (MsgSubType.STATUS_IN_STUDY & 0xFFFF) || st == (MsgSubType.STATUS_ABANDONED & 0xFFFF)
|
||||
|| st == (MsgSubType.STATUS_COMPLETED & 0xFFFF);
|
||||
}
|
||||
|
||||
private static String readStrictUtf8Len16AllowEmpty(ByteBuffer bb, String fieldName) {
|
||||
private static String readText(ByteBuffer bb) {
|
||||
if (bb.remaining() < 2) throw new IllegalArgumentException("STATUS_ACTION text length missing");
|
||||
int len = Short.toUnsignedInt(bb.getShort());
|
||||
if (bb.remaining() < len) throw new IllegalArgumentException("STATUS_ACTION text payload too short");
|
||||
if (len == 0) return "";
|
||||
if (bb.remaining() < len) throw new IllegalArgumentException(fieldName + " payload too short (len=" + len + ")");
|
||||
|
||||
byte[] bytes = new byte[len];
|
||||
bb.get(bytes);
|
||||
|
||||
var decoder = StandardCharsets.UTF_8.newDecoder()
|
||||
.onMalformedInput(CodingErrorAction.REPORT)
|
||||
.onUnmappableCharacter(CodingErrorAction.REPORT);
|
||||
|
||||
try {
|
||||
return decoder.decode(ByteBuffer.wrap(bytes)).toString();
|
||||
} catch (CharacterCodingException e) {
|
||||
throw new IllegalArgumentException(fieldName + " is not valid UTF-8", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ensureMin(ByteBuffer bb, int need, String msg) {
|
||||
if (bb.remaining() < need) throw new IllegalArgumentException(msg + " (need=" + need + ", remaining=" + bb.remaining() + ")");
|
||||
}
|
||||
|
||||
private static void ensureNoTail(ByteBuffer bb, String ctx) {
|
||||
if (bb.remaining() != 0) throw new IllegalArgumentException("Unexpected tail bytes for " + ctx + ", remaining=" + bb.remaining());
|
||||
byte[] bytes = new byte[len]; bb.get(bytes);
|
||||
var dec = StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT);
|
||||
try { return dec.decode(ByteBuffer.wrap(bytes)).toString(); }
|
||||
catch (CharacterCodingException e) { throw new IllegalArgumentException("STATUS_ACTION text invalid UTF-8", e); }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,544 +0,0 @@
|
||||
package blockchain.body;
|
||||
|
||||
import blockchain.MsgSubType;
|
||||
|
||||
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.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* TextBody — type=1, ver=1 (в заголовке блока).
|
||||
*
|
||||
* subType (в заголовке блока):
|
||||
* 10 = POST
|
||||
* 11 = EDIT_POST
|
||||
* 20 = REPLY
|
||||
* 21 = EDIT_REPLY
|
||||
*
|
||||
* =========================================================================
|
||||
* КОНЦЕПЦИЯ ЛИНИЙ ДЛЯ ТЕКСТОВЫХ СООБЩЕНИЙ:
|
||||
*
|
||||
* POST и EDIT_POST принадлежат ЛИНИИ КАНАЛА и имеют hasLine.
|
||||
* В новом формате добавлен lineCode:
|
||||
* lineCode = 0 для канала "0"
|
||||
* lineCode = blockNumber "заглавия линии/канала" (например CREATE_CHANNEL)
|
||||
*
|
||||
* REPLY и EDIT_REPLY НЕ имеют линии (нет hasLine в байтах).
|
||||
*
|
||||
* =========================================================================
|
||||
* ФОРМАТЫ bodyBytes (BigEndian):
|
||||
*
|
||||
* 1) POST (subType=10):
|
||||
* [4] lineCode
|
||||
* [4] prevLineNumber
|
||||
* [32] prevLineHash32
|
||||
* [4] thisLineNumber
|
||||
* [2] textLenBytes (uint16)
|
||||
* [N] text UTF-8
|
||||
*
|
||||
* 2) EDIT_POST (subType=11):
|
||||
* [4] lineCode
|
||||
* [4] prevLineNumber
|
||||
* [32] prevLineHash32
|
||||
* [4] thisLineNumber
|
||||
*
|
||||
* hasTarget (на ОРИГИНАЛЬНЫЙ POST, toBchName НЕ хранить):
|
||||
* [4] toBlockGlobalNumber
|
||||
* [32] toBlockHash32
|
||||
*
|
||||
* [2] textLenBytes (uint16)
|
||||
* [N] text UTF-8
|
||||
*
|
||||
* 3) REPLY (subType=20) — НЕ в линии:
|
||||
* hasTarget:
|
||||
* [1] toBlockchainNameLen (uint8)
|
||||
* [N] toBlockchainName UTF-8
|
||||
* [4] toBlockGlobalNumber
|
||||
* [32] toBlockHash32
|
||||
*
|
||||
* [2] textLenBytes (uint16)
|
||||
* [M] text UTF-8
|
||||
*
|
||||
* 4) EDIT_REPLY (subType=21) — НЕ в линии:
|
||||
* hasTarget (на ОРИГИНАЛЬНЫЙ REPLY, toBchName НЕ хранить):
|
||||
* [4] toBlockGlobalNumber
|
||||
* [32] toBlockHash32
|
||||
*
|
||||
* [2] textLenBytes (uint16)
|
||||
* [N] text UTF-8
|
||||
*/
|
||||
public final class TextBody implements BodyRecord, BodyHasTarget, BodyHasLine {
|
||||
|
||||
public static final short TYPE = 1;
|
||||
public static final short VER = 1;
|
||||
|
||||
public static final int KEY = ((TYPE & 0xFFFF) << 16) | (VER & 0xFFFF);
|
||||
|
||||
public final short subType; // из header
|
||||
public final short version; // из header
|
||||
|
||||
// ===== line fields (только для POST/EDIT_POST) =====
|
||||
// Для REPLY/EDIT_REPLY эти поля НЕ сериализуются; значения держим как "пустые".
|
||||
public final int lineCode; // только для line-message; иначе -1
|
||||
public final int prevLineNumber;
|
||||
public final byte[] prevLineHash32; // 32 or null
|
||||
public final int thisLineNumber;
|
||||
|
||||
// ===== message text =====
|
||||
public final String message;
|
||||
|
||||
// ===== target fields =====
|
||||
// REPLY: toBlockchainName + globalNumber + hash32
|
||||
// EDIT_POST / EDIT_REPLY: только globalNumber + hash32 (без toBlockchainName)
|
||||
public final String toBlockchainName; // nullable
|
||||
public final Integer toBlockGlobalNumber; // nullable
|
||||
public final byte[] toBlockHash32; // nullable (но если target есть -> 32)
|
||||
|
||||
/* ===================================================================== */
|
||||
/* ====================== Конструктор из байт ========================== */
|
||||
/* ===================================================================== */
|
||||
|
||||
public TextBody(short subType, short version, byte[] bodyBytes) {
|
||||
Objects.requireNonNull(bodyBytes, "bodyBytes == null");
|
||||
|
||||
this.subType = subType;
|
||||
this.version = version;
|
||||
|
||||
if ((this.version & 0xFFFF) != (VER & 0xFFFF)) {
|
||||
throw new IllegalArgumentException("TextBody version must be 1, got=" + (this.version & 0xFFFF));
|
||||
}
|
||||
if (!isValidSubType(this.subType)) {
|
||||
throw new IllegalArgumentException("Bad Text subType: " + (this.subType & 0xFFFF));
|
||||
}
|
||||
|
||||
ByteBuffer bb = ByteBuffer.wrap(bodyBytes).order(ByteOrder.BIG_ENDIAN);
|
||||
|
||||
int st = this.subType & 0xFFFF;
|
||||
|
||||
if (st == (MsgSubType.TEXT_POST & 0xFFFF)) {
|
||||
// POST: hasLine(lineCode+line) + text
|
||||
ensureMin(bb, (4 + 4 + 32 + 4) + 2, "POST too short");
|
||||
|
||||
this.lineCode = bb.getInt();
|
||||
this.prevLineNumber = bb.getInt();
|
||||
this.prevLineHash32 = new byte[32];
|
||||
bb.get(this.prevLineHash32);
|
||||
this.thisLineNumber = bb.getInt();
|
||||
|
||||
this.message = readStrictUtf8Len16(bb, "POST text");
|
||||
|
||||
this.toBlockchainName = null;
|
||||
this.toBlockGlobalNumber = null;
|
||||
this.toBlockHash32 = null;
|
||||
|
||||
ensureNoTail(bb, "POST");
|
||||
|
||||
} else if (st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF)) {
|
||||
// EDIT_POST: hasLine(lineCode+line) + target(no bch) + text
|
||||
ensureMin(bb, (4 + 4 + 32 + 4) + (4 + 32) + 2, "EDIT_POST too short");
|
||||
|
||||
this.lineCode = bb.getInt();
|
||||
this.prevLineNumber = bb.getInt();
|
||||
this.prevLineHash32 = new byte[32];
|
||||
bb.get(this.prevLineHash32);
|
||||
this.thisLineNumber = bb.getInt();
|
||||
|
||||
int tgtNum = bb.getInt();
|
||||
byte[] tgtHash = new byte[32];
|
||||
bb.get(tgtHash);
|
||||
|
||||
this.toBlockchainName = null;
|
||||
this.toBlockGlobalNumber = tgtNum;
|
||||
this.toBlockHash32 = tgtHash;
|
||||
|
||||
this.message = readStrictUtf8Len16(bb, "EDIT_POST text");
|
||||
|
||||
ensureNoTail(bb, "EDIT_POST");
|
||||
|
||||
} else if (st == (MsgSubType.TEXT_REPLY & 0xFFFF)) {
|
||||
// REPLY: target(with bch) + text (без line)
|
||||
ensureMin(bb, 1 + 1 + 4 + 32 + 2, "REPLY too short");
|
||||
|
||||
int nameLen = Byte.toUnsignedInt(bb.get());
|
||||
if (nameLen <= 0) throw new IllegalArgumentException("REPLY toBlockchainNameLen is 0");
|
||||
ensureMin(bb, nameLen + 4 + 32 + 2, "REPLY payload too short");
|
||||
|
||||
byte[] nameBytes = new byte[nameLen];
|
||||
bb.get(nameBytes);
|
||||
this.toBlockchainName = new String(nameBytes, StandardCharsets.UTF_8);
|
||||
|
||||
this.toBlockGlobalNumber = bb.getInt();
|
||||
|
||||
this.toBlockHash32 = new byte[32];
|
||||
bb.get(this.toBlockHash32);
|
||||
|
||||
this.message = readStrictUtf8Len16(bb, "REPLY text");
|
||||
|
||||
// line fields отсутствуют в байтах
|
||||
this.lineCode = -1;
|
||||
this.prevLineNumber = -1;
|
||||
this.prevLineHash32 = null;
|
||||
this.thisLineNumber = -1;
|
||||
|
||||
ensureNoTail(bb, "REPLY");
|
||||
|
||||
} else if (st == (MsgSubType.TEXT_EDIT_REPLY & 0xFFFF)) {
|
||||
// EDIT_REPLY: target(no bch) + text (без line)
|
||||
ensureMin(bb, (4 + 32) + 2, "EDIT_REPLY too short");
|
||||
|
||||
int tgtNum = bb.getInt();
|
||||
byte[] tgtHash = new byte[32];
|
||||
bb.get(tgtHash);
|
||||
|
||||
this.toBlockchainName = null;
|
||||
this.toBlockGlobalNumber = tgtNum;
|
||||
this.toBlockHash32 = tgtHash;
|
||||
|
||||
this.message = readStrictUtf8Len16(bb, "EDIT_REPLY text");
|
||||
|
||||
// line fields отсутствуют в байтах
|
||||
this.lineCode = -1;
|
||||
this.prevLineNumber = -1;
|
||||
this.prevLineHash32 = null;
|
||||
this.thisLineNumber = -1;
|
||||
|
||||
ensureNoTail(bb, "EDIT_REPLY");
|
||||
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unsupported Text subType: " + st);
|
||||
}
|
||||
}
|
||||
|
||||
/* ===================================================================== */
|
||||
/* ====================== Фабрики (удобно) ============================= */
|
||||
/* ===================================================================== */
|
||||
|
||||
public static TextBody newPost(int lineCode, int prevLineNumber, byte[] prevLineHash32, int thisLineNumber, String message) {
|
||||
return new TextBody(MsgSubType.TEXT_POST, lineCode, prevLineNumber, prevLineHash32, thisLineNumber,
|
||||
message, null, null, null);
|
||||
}
|
||||
|
||||
public static TextBody newEditPost(int lineCode, int prevLineNumber, byte[] prevLineHash32, int thisLineNumber,
|
||||
int targetBlockNumber, byte[] targetHash32,
|
||||
String message) {
|
||||
return new TextBody(MsgSubType.TEXT_EDIT_POST, lineCode, prevLineNumber, prevLineHash32, thisLineNumber,
|
||||
message, null, targetBlockNumber, targetHash32);
|
||||
}
|
||||
|
||||
public static TextBody newReply(String toBlockchainName, int targetBlockNumber, byte[] targetHash32, String message) {
|
||||
return new TextBody(MsgSubType.TEXT_REPLY, -1, -1, null, -1,
|
||||
message, toBlockchainName, targetBlockNumber, targetHash32);
|
||||
}
|
||||
|
||||
public static TextBody newEditReply(int targetBlockNumber, byte[] targetHash32, String message) {
|
||||
return new TextBody(MsgSubType.TEXT_EDIT_REPLY, -1, -1, null, -1,
|
||||
message, null, targetBlockNumber, targetHash32);
|
||||
}
|
||||
|
||||
/**
|
||||
* Универсальный конструктор “вручную”.
|
||||
* Для REPLY/EDIT_REPLY line поля игнорируются при сериализации (их в формате нет).
|
||||
*/
|
||||
public TextBody(short subType,
|
||||
int lineCode,
|
||||
int prevLineNumber,
|
||||
byte[] prevLineHash32,
|
||||
int thisLineNumber,
|
||||
String message,
|
||||
String toBlockchainName,
|
||||
Integer toBlockGlobalNumber,
|
||||
byte[] toBlockHash32) {
|
||||
|
||||
Objects.requireNonNull(message, "message == null");
|
||||
|
||||
if (!isValidSubType(subType)) throw new IllegalArgumentException("Bad Text subType: " + (subType & 0xFFFF));
|
||||
if (message.isBlank()) throw new IllegalArgumentException("message is blank");
|
||||
|
||||
this.subType = subType;
|
||||
this.version = VER;
|
||||
|
||||
int st = subType & 0xFFFF;
|
||||
|
||||
// line применима только к POST/EDIT_POST
|
||||
if (st == (MsgSubType.TEXT_POST & 0xFFFF) || st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF)) {
|
||||
if (lineCode < 0) throw new IllegalArgumentException("lineCode < 0 for line message");
|
||||
this.lineCode = lineCode;
|
||||
this.prevLineNumber = prevLineNumber;
|
||||
this.prevLineHash32 = (prevLineHash32 == null ? new byte[32] : Arrays.copyOf(prevLineHash32, 32));
|
||||
this.thisLineNumber = thisLineNumber;
|
||||
} else {
|
||||
this.lineCode = -1;
|
||||
this.prevLineNumber = -1;
|
||||
this.prevLineHash32 = null;
|
||||
this.thisLineNumber = -1;
|
||||
}
|
||||
|
||||
this.message = message;
|
||||
|
||||
// target правила
|
||||
if (st == (MsgSubType.TEXT_POST & 0xFFFF)) {
|
||||
this.toBlockchainName = null;
|
||||
this.toBlockGlobalNumber = null;
|
||||
this.toBlockHash32 = null;
|
||||
|
||||
} else if (st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF)) {
|
||||
Objects.requireNonNull(toBlockGlobalNumber, "toBlockGlobalNumber == null");
|
||||
Objects.requireNonNull(toBlockHash32, "toBlockHash32 == null");
|
||||
if (toBlockGlobalNumber < 0) throw new IllegalArgumentException("toBlockGlobalNumber < 0");
|
||||
if (toBlockHash32.length != 32) throw new IllegalArgumentException("toBlockHash32 != 32");
|
||||
|
||||
this.toBlockchainName = null; // по ТЗ: не хранить
|
||||
this.toBlockGlobalNumber = toBlockGlobalNumber;
|
||||
this.toBlockHash32 = Arrays.copyOf(toBlockHash32, 32);
|
||||
|
||||
} else if (st == (MsgSubType.TEXT_REPLY & 0xFFFF)) {
|
||||
Objects.requireNonNull(toBlockchainName, "toBlockchainName == null");
|
||||
Objects.requireNonNull(toBlockGlobalNumber, "toBlockGlobalNumber == null");
|
||||
Objects.requireNonNull(toBlockHash32, "toBlockHash32 == null");
|
||||
if (toBlockchainName.isBlank()) throw new IllegalArgumentException("toBlockchainName is blank");
|
||||
if (toBlockGlobalNumber < 0) throw new IllegalArgumentException("toBlockGlobalNumber < 0");
|
||||
if (toBlockHash32.length != 32) throw new IllegalArgumentException("toBlockHash32 != 32");
|
||||
|
||||
this.toBlockchainName = toBlockchainName;
|
||||
this.toBlockGlobalNumber = toBlockGlobalNumber;
|
||||
this.toBlockHash32 = Arrays.copyOf(toBlockHash32, 32);
|
||||
|
||||
} else if (st == (MsgSubType.TEXT_EDIT_REPLY & 0xFFFF)) {
|
||||
Objects.requireNonNull(toBlockGlobalNumber, "toBlockGlobalNumber == null");
|
||||
Objects.requireNonNull(toBlockHash32, "toBlockHash32 == null");
|
||||
if (toBlockGlobalNumber < 0) throw new IllegalArgumentException("toBlockGlobalNumber < 0");
|
||||
if (toBlockHash32.length != 32) throw new IllegalArgumentException("toBlockHash32 != 32");
|
||||
|
||||
this.toBlockchainName = null; // по ТЗ: не хранить
|
||||
this.toBlockGlobalNumber = toBlockGlobalNumber;
|
||||
this.toBlockHash32 = Arrays.copyOf(toBlockHash32, 32);
|
||||
|
||||
} else {
|
||||
this.toBlockchainName = null;
|
||||
this.toBlockGlobalNumber = null;
|
||||
this.toBlockHash32 = null;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isValidSubType(short st) {
|
||||
int v = st & 0xFFFF;
|
||||
return v == (MsgSubType.TEXT_POST & 0xFFFF)
|
||||
|| v == (MsgSubType.TEXT_EDIT_POST & 0xFFFF)
|
||||
|| v == (MsgSubType.TEXT_REPLY & 0xFFFF)
|
||||
|| v == (MsgSubType.TEXT_EDIT_REPLY & 0xFFFF);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextBody check() {
|
||||
if (!isValidSubType(subType))
|
||||
throw new IllegalArgumentException("Bad Text subType: " + (subType & 0xFFFF));
|
||||
|
||||
if (message == null || message.isBlank())
|
||||
throw new IllegalArgumentException("Text message is blank");
|
||||
|
||||
int st = subType & 0xFFFF;
|
||||
|
||||
// локальные проверки line (БД не трогаем)
|
||||
if (st == (MsgSubType.TEXT_POST & 0xFFFF) || st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF)) {
|
||||
if (lineCode < 0) throw new IllegalArgumentException("lineCode < 0 for line message");
|
||||
if (prevLineHash32 == null || prevLineHash32.length != 32)
|
||||
throw new IllegalArgumentException("prevLineHash32 invalid");
|
||||
} else {
|
||||
// reply/edit_reply: line отсутствует
|
||||
if (prevLineHash32 != null)
|
||||
throw new IllegalArgumentException("REPLY/EDIT_REPLY must not contain line hash");
|
||||
}
|
||||
|
||||
// target rules
|
||||
if (st == (MsgSubType.TEXT_POST & 0xFFFF)) {
|
||||
if (toBlockchainName != null || toBlockGlobalNumber != null || toBlockHash32 != null)
|
||||
throw new IllegalArgumentException("POST must not contain target fields");
|
||||
|
||||
} else if (st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF)) {
|
||||
if (toBlockchainName != null)
|
||||
throw new IllegalArgumentException("EDIT_POST must not contain toBlockchainName in target");
|
||||
if (toBlockGlobalNumber == null || toBlockGlobalNumber < 0)
|
||||
throw new IllegalArgumentException("EDIT_POST toBlockGlobalNumber invalid");
|
||||
if (toBlockHash32 == null || toBlockHash32.length != 32)
|
||||
throw new IllegalArgumentException("EDIT_POST toBlockHash32 invalid");
|
||||
|
||||
} else if (st == (MsgSubType.TEXT_REPLY & 0xFFFF)) {
|
||||
if (toBlockchainName == null || toBlockchainName.isBlank())
|
||||
throw new IllegalArgumentException("REPLY toBlockchainName is blank");
|
||||
if (toBlockGlobalNumber == null || toBlockGlobalNumber < 0)
|
||||
throw new IllegalArgumentException("REPLY toBlockGlobalNumber invalid");
|
||||
if (toBlockHash32 == null || toBlockHash32.length != 32)
|
||||
throw new IllegalArgumentException("REPLY toBlockHash32 invalid");
|
||||
|
||||
} else if (st == (MsgSubType.TEXT_EDIT_REPLY & 0xFFFF)) {
|
||||
if (toBlockchainName != null)
|
||||
throw new IllegalArgumentException("EDIT_REPLY must not contain toBlockchainName in target");
|
||||
if (toBlockGlobalNumber == null || toBlockGlobalNumber < 0)
|
||||
throw new IllegalArgumentException("EDIT_REPLY toBlockGlobalNumber invalid");
|
||||
if (toBlockHash32 == null || toBlockHash32.length != 32)
|
||||
throw new IllegalArgumentException("EDIT_REPLY toBlockHash32 invalid");
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toBytes() {
|
||||
byte[] msgUtf8 = message.getBytes(StandardCharsets.UTF_8);
|
||||
if (msgUtf8.length == 0) throw new IllegalArgumentException("Text payload is empty");
|
||||
if (msgUtf8.length > 65535) throw new IllegalArgumentException("Text too long (>65535 bytes)");
|
||||
|
||||
int st = subType & 0xFFFF;
|
||||
|
||||
if (st == (MsgSubType.TEXT_POST & 0xFFFF)) {
|
||||
// hasLine(lineCode+line) + text
|
||||
int cap = (4 + 4 + 32 + 4) + 2 + msgUtf8.length;
|
||||
|
||||
ByteBuffer bb = ByteBuffer.allocate(cap).order(ByteOrder.BIG_ENDIAN);
|
||||
bb.putInt(lineCode);
|
||||
bb.putInt(prevLineNumber);
|
||||
bb.put(prevLineHash32 == null ? new byte[32] : Arrays.copyOf(prevLineHash32, 32));
|
||||
bb.putInt(thisLineNumber);
|
||||
bb.putShort((short) msgUtf8.length);
|
||||
bb.put(msgUtf8);
|
||||
return bb.array();
|
||||
|
||||
} else if (st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF)) {
|
||||
// hasLine(lineCode+line) + target(no bch) + text
|
||||
if (toBlockGlobalNumber == null) throw new IllegalArgumentException("EDIT_POST missing toBlockGlobalNumber");
|
||||
if (toBlockHash32 == null || toBlockHash32.length != 32) throw new IllegalArgumentException("EDIT_POST toBlockHash32 != 32");
|
||||
|
||||
int cap = (4 + 4 + 32 + 4) + (4 + 32) + 2 + msgUtf8.length;
|
||||
|
||||
ByteBuffer bb = ByteBuffer.allocate(cap).order(ByteOrder.BIG_ENDIAN);
|
||||
bb.putInt(lineCode);
|
||||
bb.putInt(prevLineNumber);
|
||||
bb.put(prevLineHash32 == null ? new byte[32] : Arrays.copyOf(prevLineHash32, 32));
|
||||
bb.putInt(thisLineNumber);
|
||||
|
||||
bb.putInt(toBlockGlobalNumber);
|
||||
bb.put(toBlockHash32);
|
||||
|
||||
bb.putShort((short) msgUtf8.length);
|
||||
bb.put(msgUtf8);
|
||||
return bb.array();
|
||||
|
||||
} else if (st == (MsgSubType.TEXT_REPLY & 0xFFFF)) {
|
||||
// target(with bch) + text
|
||||
if (toBlockchainName == null) throw new IllegalArgumentException("REPLY missing toBlockchainName");
|
||||
if (toBlockGlobalNumber == null) throw new IllegalArgumentException("REPLY missing toBlockGlobalNumber");
|
||||
if (toBlockHash32 == null || toBlockHash32.length != 32) throw new IllegalArgumentException("REPLY toBlockHash32 != 32");
|
||||
|
||||
byte[] nameUtf8 = toBlockchainName.getBytes(StandardCharsets.UTF_8);
|
||||
if (nameUtf8.length == 0 || nameUtf8.length > 255)
|
||||
throw new IllegalArgumentException("REPLY toBlockchainName utf8 len must be 1..255");
|
||||
|
||||
int cap = 1 + nameUtf8.length + 4 + 32
|
||||
+ 2 + msgUtf8.length;
|
||||
|
||||
ByteBuffer bb = ByteBuffer.allocate(cap).order(ByteOrder.BIG_ENDIAN);
|
||||
bb.put((byte) nameUtf8.length);
|
||||
bb.put(nameUtf8);
|
||||
bb.putInt(toBlockGlobalNumber);
|
||||
bb.put(toBlockHash32);
|
||||
|
||||
bb.putShort((short) msgUtf8.length);
|
||||
bb.put(msgUtf8);
|
||||
return bb.array();
|
||||
|
||||
} else if (st == (MsgSubType.TEXT_EDIT_REPLY & 0xFFFF)) {
|
||||
// target(no bch) + text
|
||||
if (toBlockGlobalNumber == null) throw new IllegalArgumentException("EDIT_REPLY missing toBlockGlobalNumber");
|
||||
if (toBlockHash32 == null || toBlockHash32.length != 32) throw new IllegalArgumentException("EDIT_REPLY toBlockHash32 != 32");
|
||||
|
||||
int cap = (4 + 32) + 2 + msgUtf8.length;
|
||||
|
||||
ByteBuffer bb = ByteBuffer.allocate(cap).order(ByteOrder.BIG_ENDIAN);
|
||||
bb.putInt(toBlockGlobalNumber);
|
||||
bb.put(toBlockHash32);
|
||||
|
||||
bb.putShort((short) msgUtf8.length);
|
||||
bb.put(msgUtf8);
|
||||
return bb.array();
|
||||
|
||||
} else {
|
||||
throw new IllegalStateException("Unsupported Text subType: " + st);
|
||||
}
|
||||
}
|
||||
|
||||
/* ===================================================================== */
|
||||
/* ========================== Helpers ================================== */
|
||||
/* ===================================================================== */
|
||||
|
||||
private static String readStrictUtf8Len16(ByteBuffer bb, String fieldName) {
|
||||
int len = Short.toUnsignedInt(bb.getShort());
|
||||
if (len <= 0) throw new IllegalArgumentException(fieldName + " is empty");
|
||||
if (bb.remaining() < len) throw new IllegalArgumentException(fieldName + " payload too short (len=" + len + ")");
|
||||
|
||||
byte[] bytes = new byte[len];
|
||||
bb.get(bytes);
|
||||
|
||||
var decoder = StandardCharsets.UTF_8.newDecoder()
|
||||
.onMalformedInput(CodingErrorAction.REPORT)
|
||||
.onUnmappableCharacter(CodingErrorAction.REPORT);
|
||||
|
||||
try {
|
||||
String s = decoder.decode(ByteBuffer.wrap(bytes)).toString();
|
||||
if (s.isBlank()) throw new IllegalArgumentException(fieldName + " is blank");
|
||||
return s;
|
||||
} catch (CharacterCodingException e) {
|
||||
throw new IllegalArgumentException(fieldName + " is not valid UTF-8", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ensureMin(ByteBuffer bb, int need, String msg) {
|
||||
if (bb.remaining() < need) throw new IllegalArgumentException(msg + " (need=" + need + ", remaining=" + bb.remaining() + ")");
|
||||
}
|
||||
|
||||
private static void ensureNoTail(ByteBuffer bb, String ctx) {
|
||||
if (bb.remaining() != 0) throw new IllegalArgumentException("Unexpected tail bytes for " + ctx + ", remaining=" + bb.remaining());
|
||||
}
|
||||
|
||||
/* ====================== BodyHasLine ====================== */
|
||||
@Override public int lineCode() { return lineCode; }
|
||||
@Override public int prevLineBlockGlobalNumber() { return prevLineNumber; }
|
||||
@Override public byte[] prevLineBlockHash32() {
|
||||
if (prevLineHash32 == null) return null;
|
||||
return Arrays.copyOf(prevLineHash32, 32);
|
||||
}
|
||||
@Override public int lineSeq() { return thisLineNumber; }
|
||||
|
||||
/* ====================== BodyHasTarget ===================== */
|
||||
@Override public String toBchName() { return toBlockchainName; }
|
||||
@Override public Integer toBlockGlobalNumber() { return toBlockGlobalNumber; }
|
||||
@Override public byte[] toBlockHashBytes() { return toBlockHash32; }
|
||||
|
||||
/* ===================================================================== */
|
||||
/* ===================== Удобные хелперы (для ChainState) =============== */
|
||||
/* ===================================================================== */
|
||||
|
||||
/** true только для POST / EDIT_POST (т.е. это сообщение в линии канала). */
|
||||
public boolean isLineMessage() {
|
||||
int st = subType & 0xFFFF;
|
||||
return st == (MsgSubType.TEXT_POST & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF);
|
||||
}
|
||||
|
||||
/** true только для EDIT_POST / EDIT_REPLY. */
|
||||
public boolean isEditMessage() {
|
||||
int st = subType & 0xFFFF;
|
||||
return st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_EDIT_REPLY & 0xFFFF);
|
||||
}
|
||||
|
||||
/** true только для REPLY / EDIT_REPLY (т.е. “не в линии”). */
|
||||
public boolean isReplyFamily() {
|
||||
int st = subType & 0xFFFF;
|
||||
return st == (MsgSubType.TEXT_REPLY & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_EDIT_REPLY & 0xFFFF);
|
||||
}
|
||||
}
|
||||
+88
-285
@@ -11,374 +11,177 @@ import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* TextLineBody — type=1, ver=1.
|
||||
*
|
||||
* subType:
|
||||
* - POST (10)
|
||||
* - EDIT_POST (11)
|
||||
* - REPOST (50)
|
||||
* - CHANNEL_META (90)
|
||||
* - ENTRYPOINT (100)
|
||||
* - EXERCISE (110)
|
||||
* - SERVICE (120)
|
||||
* - COURSE (130)
|
||||
*
|
||||
* Формат bodyBytes (BigEndian):
|
||||
* Line-based TEXT body.
|
||||
*
|
||||
* POST / CHANNEL_META / ENTRYPOINT / EXERCISE / SERVICE / COURSE:
|
||||
* [4] lineCode
|
||||
* [4] prevLineNumber
|
||||
* [32] prevLineHash32
|
||||
* [4] thisLineNumber
|
||||
* [2] textLenBytes (uint16)
|
||||
* [N] text UTF-8
|
||||
* line fields + text
|
||||
*
|
||||
* EDIT_POST:
|
||||
* [4] lineCode
|
||||
* [4] prevLineNumber
|
||||
* [32] prevLineHash32
|
||||
* [4] thisLineNumber
|
||||
* [4] toBlockGlobalNumber (int32)
|
||||
* [32] toBlockHash32
|
||||
* [2] textLenBytes (uint16)
|
||||
* [N] text UTF-8
|
||||
* EDIT_POST (own-chain compact target):
|
||||
* line fields + [4] targetBlockNumber + [32] targetHash + text
|
||||
*
|
||||
* REPOST:
|
||||
* [4] lineCode
|
||||
* [4] prevLineNumber
|
||||
* [32] prevLineHash32
|
||||
* [4] thisLineNumber
|
||||
* [1] toBlockchainNameLen (uint8)
|
||||
* [N] toBlockchainName UTF-8
|
||||
* [4] toBlockGlobalNumber (int32)
|
||||
* [32] toBlockHash32
|
||||
* [2] textLenBytes (uint16)
|
||||
* [N] text UTF-8
|
||||
* REPOST (external target):
|
||||
* line fields + [1]loginLen + login + [2]forkNumber + [4]blockNumber + [32]hash + text
|
||||
*/
|
||||
public final class TextLineBody implements BodyRecord, BodyHasLine, BodyHasTarget {
|
||||
|
||||
public static final short TYPE = 1;
|
||||
public static final short VER = 1;
|
||||
|
||||
public static final short VER = 1;
|
||||
public static final int KEY = ((TYPE & 0xFFFF) << 16) | (VER & 0xFFFF);
|
||||
|
||||
public final short subType; // из header
|
||||
public final short version; // из header (=1)
|
||||
|
||||
// line
|
||||
public final short subType;
|
||||
public final short version;
|
||||
public final int lineCode;
|
||||
public final int prevLineNumber;
|
||||
public final byte[] prevLineHash32; // 32 (может быть нули)
|
||||
public final byte[] prevLineHash32;
|
||||
public final int thisLineNumber;
|
||||
|
||||
// target (для EDIT_POST / REPOST)
|
||||
public final String toBlockchainName; // nullable для POST/EDIT_POST
|
||||
public final Integer toBlockGlobalNumber; // nullable для POST
|
||||
public final byte[] toBlockHash32; // nullable для POST
|
||||
|
||||
// text
|
||||
public final String toLogin;
|
||||
public final Integer toForkNumber;
|
||||
public final Integer toBlockGlobalNumber;
|
||||
public final byte[] toBlockHash32;
|
||||
public final String message;
|
||||
|
||||
/* ====================== parse from bytes ====================== */
|
||||
|
||||
public TextLineBody(short subType, short version, byte[] bodyBytes) {
|
||||
Objects.requireNonNull(bodyBytes, "bodyBytes == null");
|
||||
|
||||
this.subType = subType;
|
||||
this.version = version;
|
||||
|
||||
if ((this.version & 0xFFFF) != (VER & 0xFFFF)) {
|
||||
throw new IllegalArgumentException("TextLineBody version must be 1, got=" + (this.version & 0xFFFF));
|
||||
}
|
||||
|
||||
int st = this.subType & 0xFFFF;
|
||||
if (st != (MsgSubType.TEXT_POST & 0xFFFF)
|
||||
&& st != (MsgSubType.TEXT_EDIT_POST & 0xFFFF)
|
||||
&& st != (MsgSubType.TEXT_REPOST & 0xFFFF)
|
||||
&& st != (MsgSubType.TEXT_CHANNEL_META & 0xFFFF)
|
||||
&& st != (MsgSubType.TEXT_ENTRYPOINT & 0xFFFF)
|
||||
&& st != (MsgSubType.TEXT_EXERCISE & 0xFFFF)
|
||||
&& st != (MsgSubType.TEXT_SERVICE & 0xFFFF)
|
||||
&& st != (MsgSubType.TEXT_COURSE & 0xFFFF)) {
|
||||
throw new IllegalArgumentException("TextLineBody supports only POST/EDIT_POST/REPOST/CHANNEL_META, got subType=" + st);
|
||||
}
|
||||
if ((version & 0xFFFF) != 1 || !isSupportedSubType(subType)) throw new IllegalArgumentException("Bad TextLineBody header");
|
||||
|
||||
ByteBuffer bb = ByteBuffer.wrap(bodyBytes).order(ByteOrder.BIG_ENDIAN);
|
||||
|
||||
// минимум line + textLen(2)
|
||||
ensureMin(bb, (4 + 4 + 32 + 4) + 2, "TextLineBody too short");
|
||||
|
||||
BodyTargetSupport.ensureMin(bb, 4 + 4 + 32 + 4 + 2, "TextLineBody too short");
|
||||
this.lineCode = bb.getInt();
|
||||
this.prevLineNumber = bb.getInt();
|
||||
|
||||
this.prevLineHash32 = new byte[32];
|
||||
bb.get(this.prevLineHash32);
|
||||
|
||||
this.prevLineHash32 = new byte[32]; bb.get(this.prevLineHash32);
|
||||
this.thisLineNumber = bb.getInt();
|
||||
|
||||
int st = subType & 0xFFFF;
|
||||
if (st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF)) {
|
||||
// нужен target
|
||||
ensureMin(bb, (4 + 32) + 2, "EDIT_POST missing target");
|
||||
int tgtNum = bb.getInt();
|
||||
byte[] tgtHash = new byte[32];
|
||||
bb.get(tgtHash);
|
||||
|
||||
this.toBlockchainName = null;
|
||||
this.toBlockGlobalNumber = tgtNum;
|
||||
this.toBlockHash32 = tgtHash;
|
||||
} else if (st == (MsgSubType.TEXT_REPOST & 0xFFFF)) {
|
||||
ensureMin(bb, 1 + 1 + 4 + 32 + 2, "REPOST missing target");
|
||||
int nameLen = Byte.toUnsignedInt(bb.get());
|
||||
if (nameLen <= 0) throw new IllegalArgumentException("REPOST toBlockchainNameLen is 0");
|
||||
ensureMin(bb, nameLen + 4 + 32 + 2, "REPOST payload too short");
|
||||
byte[] nameBytes = new byte[nameLen];
|
||||
bb.get(nameBytes);
|
||||
this.toBlockchainName = new String(nameBytes, StandardCharsets.UTF_8);
|
||||
BodyTargetSupport.ensureMin(bb, 4 + 32 + 2, "EDIT_POST target missing");
|
||||
this.toLogin = null;
|
||||
this.toForkNumber = null;
|
||||
this.toBlockGlobalNumber = bb.getInt();
|
||||
this.toBlockHash32 = new byte[32];
|
||||
bb.get(this.toBlockHash32);
|
||||
this.toBlockHash32 = new byte[32]; bb.get(this.toBlockHash32);
|
||||
BodyTargetSupport.validateCompact(this.toBlockGlobalNumber, this.toBlockHash32, "EDIT_POST");
|
||||
} else if (st == (MsgSubType.TEXT_REPOST & 0xFFFF)) {
|
||||
BodyTargetSupport.ExternalTarget t = BodyTargetSupport.readExternal(bb, "REPOST", 2);
|
||||
this.toLogin = t.login();
|
||||
this.toForkNumber = t.forkNumber();
|
||||
this.toBlockGlobalNumber = t.blockNumber();
|
||||
this.toBlockHash32 = t.blockHash32();
|
||||
} else {
|
||||
this.toBlockchainName = null;
|
||||
this.toLogin = null;
|
||||
this.toForkNumber = null;
|
||||
this.toBlockGlobalNumber = null;
|
||||
this.toBlockHash32 = null;
|
||||
}
|
||||
|
||||
this.message = readStrictUtf8Len16(bb, "TextLineBody text",
|
||||
st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF) || st == (MsgSubType.TEXT_CHANNEL_META & 0xFFFF));
|
||||
|
||||
ensureNoTail(bb, "TextLineBody");
|
||||
boolean allowEmpty = st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF) || st == (MsgSubType.TEXT_CHANNEL_META & 0xFFFF) || st == (MsgSubType.TEXT_CHANNEL_DELETE & 0xFFFF);
|
||||
this.message = readText(bb, allowEmpty);
|
||||
if (bb.hasRemaining()) throw new IllegalArgumentException("Unexpected TextLineBody tail");
|
||||
}
|
||||
|
||||
/* ====================== manual ctor ====================== */
|
||||
|
||||
public TextLineBody(int lineCode,
|
||||
int prevLineNumber,
|
||||
byte[] prevLineHash32,
|
||||
int thisLineNumber,
|
||||
short subType,
|
||||
String toLogin,
|
||||
Integer toForkNumber,
|
||||
Integer toBlockGlobalNumber,
|
||||
byte[] toBlockHash32,
|
||||
String toBlockchainName,
|
||||
String message) {
|
||||
|
||||
Objects.requireNonNull(message, "message == null");
|
||||
|
||||
int st = subType & 0xFFFF;
|
||||
if (st != (MsgSubType.TEXT_POST & 0xFFFF)
|
||||
&& st != (MsgSubType.TEXT_EDIT_POST & 0xFFFF)
|
||||
&& st != (MsgSubType.TEXT_REPOST & 0xFFFF)
|
||||
&& st != (MsgSubType.TEXT_CHANNEL_META & 0xFFFF)
|
||||
&& st != (MsgSubType.TEXT_ENTRYPOINT & 0xFFFF)
|
||||
&& st != (MsgSubType.TEXT_EXERCISE & 0xFFFF)
|
||||
&& st != (MsgSubType.TEXT_SERVICE & 0xFFFF)
|
||||
&& st != (MsgSubType.TEXT_COURSE & 0xFFFF)) {
|
||||
throw new IllegalArgumentException("TextLineBody supports only POST/EDIT_POST/REPOST/CHANNEL_META");
|
||||
}
|
||||
|
||||
if (!isSupportedSubType(subType)) throw new IllegalArgumentException("Bad TextLineBody subtype");
|
||||
if (lineCode < 0) throw new IllegalArgumentException("lineCode < 0");
|
||||
if (requiresNonBlankMessage(st) && message.isBlank()) {
|
||||
throw new IllegalArgumentException("message is blank");
|
||||
}
|
||||
|
||||
this.subType = subType;
|
||||
this.version = VER;
|
||||
|
||||
this.lineCode = lineCode;
|
||||
this.prevLineNumber = prevLineNumber;
|
||||
this.prevLineHash32 = (prevLineHash32 == null ? new byte[32] : Arrays.copyOf(prevLineHash32, 32));
|
||||
this.prevLineHash32 = prevLineHash32 == null ? new byte[32] : Arrays.copyOf(prevLineHash32, 32);
|
||||
this.thisLineNumber = thisLineNumber;
|
||||
|
||||
if (st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF)) {
|
||||
Objects.requireNonNull(toBlockGlobalNumber, "toBlockGlobalNumber == null");
|
||||
Objects.requireNonNull(toBlockHash32, "toBlockHash32 == null");
|
||||
if (toBlockGlobalNumber < 0) throw new IllegalArgumentException("toBlockGlobalNumber < 0");
|
||||
if (toBlockHash32.length != 32) throw new IllegalArgumentException("toBlockHash32 != 32");
|
||||
|
||||
this.toBlockchainName = null;
|
||||
this.toBlockGlobalNumber = toBlockGlobalNumber;
|
||||
this.toBlockHash32 = Arrays.copyOf(toBlockHash32, 32);
|
||||
} else if (st == (MsgSubType.TEXT_REPOST & 0xFFFF)) {
|
||||
Objects.requireNonNull(toBlockchainName, "toBlockchainName == null");
|
||||
if (toBlockchainName.isBlank()) throw new IllegalArgumentException("toBlockchainName is blank");
|
||||
Objects.requireNonNull(toBlockGlobalNumber, "toBlockGlobalNumber == null");
|
||||
Objects.requireNonNull(toBlockHash32, "toBlockHash32 == null");
|
||||
if (toBlockGlobalNumber < 0) throw new IllegalArgumentException("toBlockGlobalNumber < 0");
|
||||
if (toBlockHash32.length != 32) throw new IllegalArgumentException("toBlockHash32 != 32");
|
||||
|
||||
this.toBlockchainName = toBlockchainName;
|
||||
this.toBlockGlobalNumber = toBlockGlobalNumber;
|
||||
this.toBlockHash32 = Arrays.copyOf(toBlockHash32, 32);
|
||||
} else {
|
||||
this.toBlockchainName = null;
|
||||
this.toBlockGlobalNumber = null;
|
||||
this.toBlockHash32 = null;
|
||||
}
|
||||
|
||||
this.toLogin = toLogin;
|
||||
this.toForkNumber = toForkNumber;
|
||||
this.toBlockGlobalNumber = toBlockGlobalNumber;
|
||||
this.toBlockHash32 = toBlockHash32 == null ? null : Arrays.copyOf(toBlockHash32, 32);
|
||||
this.message = message;
|
||||
check();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextLineBody check() {
|
||||
int st = subType & 0xFFFF;
|
||||
if (st != (MsgSubType.TEXT_POST & 0xFFFF)
|
||||
&& st != (MsgSubType.TEXT_EDIT_POST & 0xFFFF)
|
||||
&& st != (MsgSubType.TEXT_REPOST & 0xFFFF)
|
||||
&& st != (MsgSubType.TEXT_CHANNEL_META & 0xFFFF)
|
||||
&& st != (MsgSubType.TEXT_ENTRYPOINT & 0xFFFF)
|
||||
&& st != (MsgSubType.TEXT_EXERCISE & 0xFFFF)
|
||||
&& st != (MsgSubType.TEXT_SERVICE & 0xFFFF)
|
||||
&& st != (MsgSubType.TEXT_COURSE & 0xFFFF))
|
||||
throw new IllegalArgumentException("Bad TextLineBody subType: " + st);
|
||||
|
||||
@Override public TextLineBody check() {
|
||||
if (!isSupportedSubType(subType)) throw new IllegalArgumentException("Bad TextLineBody subtype");
|
||||
if (lineCode < 0) throw new IllegalArgumentException("lineCode < 0");
|
||||
if (prevLineHash32 == null || prevLineHash32.length != 32)
|
||||
throw new IllegalArgumentException("prevLineHash32 invalid");
|
||||
if (prevLineHash32 == null || prevLineHash32.length != 32) throw new IllegalArgumentException("prevLineHash32 invalid");
|
||||
|
||||
int st = subType & 0xFFFF;
|
||||
if (st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF)) {
|
||||
if (toLogin != null || toForkNumber != null) throw new IllegalArgumentException("EDIT_POST must not contain login/fork target");
|
||||
BodyTargetSupport.validateCompact(toBlockGlobalNumber, toBlockHash32, "EDIT_POST");
|
||||
if (message == null) throw new IllegalArgumentException("EDIT_POST message is null");
|
||||
if (toBlockGlobalNumber == null || toBlockGlobalNumber < 0)
|
||||
throw new IllegalArgumentException("EDIT_POST toBlockGlobalNumber invalid");
|
||||
if (toBlockHash32 == null || toBlockHash32.length != 32)
|
||||
throw new IllegalArgumentException("EDIT_POST toBlockHash32 invalid");
|
||||
if (toBlockchainName != null)
|
||||
throw new IllegalArgumentException("EDIT_POST must not contain toBlockchainName");
|
||||
} else if (st == (MsgSubType.TEXT_REPOST & 0xFFFF)) {
|
||||
if (message == null || message.isBlank())
|
||||
throw new IllegalArgumentException("REPOST message is blank");
|
||||
if (toBlockchainName == null || toBlockchainName.isBlank())
|
||||
throw new IllegalArgumentException("REPOST toBlockchainName is blank");
|
||||
if (toBlockGlobalNumber == null || toBlockGlobalNumber < 0)
|
||||
throw new IllegalArgumentException("REPOST toBlockGlobalNumber invalid");
|
||||
if (toBlockHash32 == null || toBlockHash32.length != 32)
|
||||
throw new IllegalArgumentException("REPOST toBlockHash32 invalid");
|
||||
BodyTargetSupport.validateExternal(toLogin, toForkNumber, toBlockGlobalNumber, toBlockHash32, "REPOST");
|
||||
if (message == null || message.isBlank()) throw new IllegalArgumentException("REPOST message is blank");
|
||||
} else {
|
||||
if (toLogin != null || toForkNumber != null || toBlockGlobalNumber != null || toBlockHash32 != null) {
|
||||
throw new IllegalArgumentException("Non-target text body must not contain target fields");
|
||||
}
|
||||
if (st == (MsgSubType.TEXT_CHANNEL_META & 0xFFFF)) {
|
||||
if (message == null) throw new IllegalArgumentException("CHANNEL_META message is null");
|
||||
} else if (requiresNonBlankMessage(st) && (message == null || message.isBlank())) {
|
||||
} else if (st == (MsgSubType.TEXT_CHANNEL_DELETE & 0xFFFF)) {
|
||||
if (message == null || !message.isEmpty()) throw new IllegalArgumentException("CHANNEL_DELETE message must be empty");
|
||||
} else if (message == null || message.isBlank()) {
|
||||
throw new IllegalArgumentException("Text message is blank");
|
||||
} else if (message == null) {
|
||||
throw new IllegalArgumentException("Text message is null");
|
||||
}
|
||||
if (toBlockchainName != null || toBlockGlobalNumber != null || toBlockHash32 != null)
|
||||
throw new IllegalArgumentException("POST/CHANNEL_META must not contain target fields");
|
||||
}
|
||||
|
||||
if (message.getBytes(StandardCharsets.UTF_8).length > 65535) throw new IllegalArgumentException("Text too long");
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toBytes() {
|
||||
byte[] msgUtf8 = message.getBytes(StandardCharsets.UTF_8);
|
||||
if (msgUtf8.length > 65535) throw new IllegalArgumentException("Text too long (>65535 bytes)");
|
||||
|
||||
@Override public byte[] toBytes() {
|
||||
check();
|
||||
byte[] text = message.getBytes(StandardCharsets.UTF_8);
|
||||
int st = subType & 0xFFFF;
|
||||
if (requiresNonBlankMessage(st) && msgUtf8.length == 0) {
|
||||
throw new IllegalArgumentException("Text payload is empty");
|
||||
}
|
||||
|
||||
int cap;
|
||||
if (st == (MsgSubType.TEXT_POST & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_CHANNEL_META & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_ENTRYPOINT & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_EXERCISE & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_SERVICE & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_COURSE & 0xFFFF)) {
|
||||
cap = (4 + 4 + 32 + 4) + 2 + msgUtf8.length;
|
||||
} else if (st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF)) {
|
||||
// EDIT_POST
|
||||
if (toBlockGlobalNumber == null) throw new IllegalArgumentException("EDIT_POST missing toBlockGlobalNumber");
|
||||
if (toBlockHash32 == null || toBlockHash32.length != 32) throw new IllegalArgumentException("EDIT_POST toBlockHash32 != 32");
|
||||
cap = (4 + 4 + 32 + 4) + (4 + 32) + 2 + msgUtf8.length;
|
||||
} else {
|
||||
if (toBlockchainName == null) throw new IllegalArgumentException("REPOST missing toBlockchainName");
|
||||
byte[] nameUtf8 = toBlockchainName.getBytes(StandardCharsets.UTF_8);
|
||||
if (nameUtf8.length == 0 || nameUtf8.length > 255) {
|
||||
throw new IllegalArgumentException("REPOST toBlockchainName utf8 len must be 1..255");
|
||||
}
|
||||
if (toBlockGlobalNumber == null) throw new IllegalArgumentException("REPOST missing toBlockGlobalNumber");
|
||||
if (toBlockHash32 == null || toBlockHash32.length != 32) throw new IllegalArgumentException("REPOST toBlockHash32 != 32");
|
||||
cap = (4 + 4 + 32 + 4) + (1 + nameUtf8.length + 4 + 32) + 2 + msgUtf8.length;
|
||||
}
|
||||
|
||||
ByteBuffer bb = ByteBuffer.allocate(cap).order(ByteOrder.BIG_ENDIAN);
|
||||
|
||||
bb.putInt(lineCode);
|
||||
bb.putInt(prevLineNumber);
|
||||
bb.put(prevLineHash32 == null ? new byte[32] : Arrays.copyOf(prevLineHash32, 32));
|
||||
bb.putInt(thisLineNumber);
|
||||
int targetSize = 0;
|
||||
if (st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF)) targetSize = 4 + 32;
|
||||
else if (st == (MsgSubType.TEXT_REPOST & 0xFFFF)) targetSize = BodyTargetSupport.externalSize(toLogin);
|
||||
|
||||
ByteBuffer bb = ByteBuffer.allocate(4 + 4 + 32 + 4 + targetSize + 2 + text.length).order(ByteOrder.BIG_ENDIAN);
|
||||
bb.putInt(lineCode).putInt(prevLineNumber).put(prevLineHash32).putInt(thisLineNumber);
|
||||
if (st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF)) {
|
||||
bb.putInt(toBlockGlobalNumber);
|
||||
bb.put(toBlockHash32);
|
||||
bb.putInt(toBlockGlobalNumber).put(toBlockHash32);
|
||||
} else if (st == (MsgSubType.TEXT_REPOST & 0xFFFF)) {
|
||||
byte[] nameUtf8 = toBlockchainName.getBytes(StandardCharsets.UTF_8);
|
||||
bb.put((byte) nameUtf8.length);
|
||||
bb.put(nameUtf8);
|
||||
bb.putInt(toBlockGlobalNumber);
|
||||
bb.put(toBlockHash32);
|
||||
BodyTargetSupport.writeExternal(bb, toLogin, toForkNumber, toBlockGlobalNumber, toBlockHash32, "REPOST");
|
||||
}
|
||||
|
||||
bb.putShort((short) msgUtf8.length);
|
||||
bb.put(msgUtf8);
|
||||
|
||||
bb.putShort((short) text.length).put(text);
|
||||
return bb.array();
|
||||
}
|
||||
|
||||
/* ====================== BodyHasLine ====================== */
|
||||
@Override public int lineCode() { return lineCode; }
|
||||
@Override public int prevLineBlockGlobalNumber() { return prevLineNumber; }
|
||||
@Override public byte[] prevLineBlockHash32() { return Arrays.copyOf(prevLineHash32, 32); }
|
||||
@Override public int lineSeq() { return thisLineNumber; }
|
||||
|
||||
/* ====================== BodyHasTarget ===================== */
|
||||
@Override public String toBchName() { return toBlockchainName; }
|
||||
@Override public String toLogin() { return toLogin; }
|
||||
@Override public Integer toForkNumber() { return toForkNumber; }
|
||||
@Override public Integer toBlockGlobalNumber() { return toBlockGlobalNumber; }
|
||||
@Override public byte[] toBlockHashBytes() { return toBlockHash32; }
|
||||
@Override public byte[] toBlockHashBytes() { return toBlockHash32 == null ? null : Arrays.copyOf(toBlockHash32, 32); }
|
||||
public boolean isEditPost() { return (subType & 0xFFFF) == (MsgSubType.TEXT_EDIT_POST & 0xFFFF); }
|
||||
|
||||
/* ====================== helpers ====================== */
|
||||
|
||||
public boolean isEditPost() {
|
||||
return (subType & 0xFFFF) == (MsgSubType.TEXT_EDIT_POST & 0xFFFF);
|
||||
private static boolean isSupportedSubType(short subType) {
|
||||
int st = subType & 0xFFFF;
|
||||
return st == (MsgSubType.TEXT_POST & 0xFFFF) || st == (MsgSubType.TEXT_EDIT_POST & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_REPOST & 0xFFFF) || st == (MsgSubType.TEXT_CHANNEL_META & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_CHANNEL_DELETE & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_ENTRYPOINT & 0xFFFF) || st == (MsgSubType.TEXT_EXERCISE & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_SERVICE & 0xFFFF) || st == (MsgSubType.TEXT_COURSE & 0xFFFF);
|
||||
}
|
||||
|
||||
private static boolean requiresNonBlankMessage(int st) {
|
||||
return st == (MsgSubType.TEXT_POST & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_REPOST & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_ENTRYPOINT & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_EXERCISE & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_SERVICE & 0xFFFF)
|
||||
|| st == (MsgSubType.TEXT_COURSE & 0xFFFF);
|
||||
}
|
||||
|
||||
private static String readStrictUtf8Len16(ByteBuffer bb, String fieldName, boolean allowEmpty) {
|
||||
private static String readText(ByteBuffer bb, boolean allowEmpty) {
|
||||
if (bb.remaining() < 2) throw new IllegalArgumentException("Text length missing");
|
||||
int len = Short.toUnsignedInt(bb.getShort());
|
||||
if (len == 0) {
|
||||
if (allowEmpty) return "";
|
||||
throw new IllegalArgumentException(fieldName + " is empty");
|
||||
}
|
||||
if (bb.remaining() < len) throw new IllegalArgumentException(fieldName + " payload too short (len=" + len + ")");
|
||||
|
||||
byte[] bytes = new byte[len];
|
||||
bb.get(bytes);
|
||||
|
||||
var decoder = StandardCharsets.UTF_8.newDecoder()
|
||||
.onMalformedInput(CodingErrorAction.REPORT)
|
||||
.onUnmappableCharacter(CodingErrorAction.REPORT);
|
||||
|
||||
try {
|
||||
String s = decoder.decode(ByteBuffer.wrap(bytes)).toString();
|
||||
if (!allowEmpty && s.isBlank()) throw new IllegalArgumentException(fieldName + " is blank");
|
||||
return s;
|
||||
} catch (CharacterCodingException e) {
|
||||
throw new IllegalArgumentException(fieldName + " is not valid UTF-8", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ensureMin(ByteBuffer bb, int need, String msg) {
|
||||
if (bb.remaining() < need) throw new IllegalArgumentException(msg + " (need=" + need + ", remaining=" + bb.remaining() + ")");
|
||||
}
|
||||
|
||||
private static void ensureNoTail(ByteBuffer bb, String ctx) {
|
||||
if (bb.remaining() != 0) throw new IllegalArgumentException("Unexpected tail bytes for " + ctx + ", remaining=" + bb.remaining());
|
||||
if (len == 0) { if (allowEmpty) return ""; throw new IllegalArgumentException("Text is empty"); }
|
||||
if (bb.remaining() < len) throw new IllegalArgumentException("Text payload too short");
|
||||
byte[] bytes = new byte[len]; bb.get(bytes);
|
||||
var dec = StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT);
|
||||
try { String s = dec.decode(ByteBuffer.wrap(bytes)).toString(); if (!allowEmpty && s.isBlank()) throw new IllegalArgumentException("Text blank"); return s; }
|
||||
catch (CharacterCodingException e) { throw new IllegalArgumentException("Text invalid UTF-8", e); }
|
||||
}
|
||||
}
|
||||
|
||||
+36
-106
@@ -10,29 +10,16 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* TextRatingBody — type=1, ver=1.
|
||||
*
|
||||
* subType:
|
||||
* - RATING (30)
|
||||
*
|
||||
* Формат bodyBytes (BigEndian):
|
||||
* [1] toBlockchainNameLen (uint8)
|
||||
* [N] toBlockchainName UTF-8
|
||||
* [4] toBlockGlobalNumber
|
||||
* [32] toBlockHash32
|
||||
* [2] textLenBytes (uint16)
|
||||
* [M] text UTF-8
|
||||
*/
|
||||
/** TEXT_RATING. External target = login + forkNumber + blockNumber + hash32. */
|
||||
public final class TextRatingBody implements BodyRecord, BodyHasTarget {
|
||||
|
||||
public static final short TYPE = 1;
|
||||
public static final short VER = 1;
|
||||
public static final int KEY = ((TYPE & 0xFFFF) << 16) | (VER & 0xFFFF);
|
||||
|
||||
public final short subType;
|
||||
public final short version;
|
||||
public final String toBlockchainName;
|
||||
public final String toLogin;
|
||||
public final int toForkNumber;
|
||||
public final int toBlockGlobalNumber;
|
||||
public final byte[] toBlockHash32;
|
||||
public final String message;
|
||||
@@ -41,117 +28,60 @@ public final class TextRatingBody implements BodyRecord, BodyHasTarget {
|
||||
Objects.requireNonNull(bodyBytes, "bodyBytes == null");
|
||||
this.subType = subType;
|
||||
this.version = version;
|
||||
|
||||
if ((version & 0xFFFF) != (VER & 0xFFFF)) {
|
||||
throw new IllegalArgumentException("TextRatingBody version must be 1, got=" + (version & 0xFFFF));
|
||||
}
|
||||
if ((subType & 0xFFFF) != (MsgSubType.TEXT_RATING & 0xFFFF)) {
|
||||
throw new IllegalArgumentException("TextRatingBody supports only TEXT_RATING");
|
||||
}
|
||||
|
||||
if ((version & 0xFFFF) != (VER & 0xFFFF)) throw new IllegalArgumentException("TextRatingBody version must be 1");
|
||||
if ((subType & 0xFFFF) != (MsgSubType.TEXT_RATING & 0xFFFF)) throw new IllegalArgumentException("TextRatingBody supports only TEXT_RATING");
|
||||
ByteBuffer bb = ByteBuffer.wrap(bodyBytes).order(ByteOrder.BIG_ENDIAN);
|
||||
ensureMin(bb, 1 + 1 + 4 + 32 + 2, "RATING too short");
|
||||
|
||||
int nameLen = Byte.toUnsignedInt(bb.get());
|
||||
if (nameLen <= 0) throw new IllegalArgumentException("RATING toBlockchainNameLen is 0");
|
||||
ensureMin(bb, nameLen + 4 + 32 + 2, "RATING payload too short");
|
||||
|
||||
byte[] nameBytes = new byte[nameLen];
|
||||
bb.get(nameBytes);
|
||||
this.toBlockchainName = new String(nameBytes, StandardCharsets.UTF_8);
|
||||
this.toBlockGlobalNumber = bb.getInt();
|
||||
this.toBlockHash32 = new byte[32];
|
||||
bb.get(this.toBlockHash32);
|
||||
this.message = readStrictUtf8Len16(bb, "TextRatingBody text");
|
||||
|
||||
ensureNoTail(bb, "TextRatingBody");
|
||||
BodyTargetSupport.ExternalTarget t = BodyTargetSupport.readExternal(bb, "RATING", 2);
|
||||
this.toLogin = t.login();
|
||||
this.toForkNumber = t.forkNumber();
|
||||
this.toBlockGlobalNumber = t.blockNumber();
|
||||
this.toBlockHash32 = t.blockHash32();
|
||||
this.message = readText(bb);
|
||||
if (bb.hasRemaining()) throw new IllegalArgumentException("Unexpected RATING tail");
|
||||
}
|
||||
|
||||
public TextRatingBody(String toBlockchainName, int toBlockGlobalNumber, byte[] toBlockHash32, String message) {
|
||||
Objects.requireNonNull(toBlockchainName, "toBlockchainName == null");
|
||||
Objects.requireNonNull(toBlockHash32, "toBlockHash32 == null");
|
||||
public TextRatingBody(String toLogin, int toForkNumber, int toBlockGlobalNumber, byte[] toBlockHash32, String message) {
|
||||
Objects.requireNonNull(message, "message == null");
|
||||
if (toBlockchainName.isBlank()) throw new IllegalArgumentException("toBlockchainName is blank");
|
||||
if (toBlockGlobalNumber < 0) throw new IllegalArgumentException("toBlockGlobalNumber < 0");
|
||||
if (toBlockHash32.length != 32) throw new IllegalArgumentException("toBlockHash32 != 32");
|
||||
if (message.isBlank()) throw new IllegalArgumentException("message is blank");
|
||||
|
||||
BodyTargetSupport.validateExternal(toLogin, toForkNumber, toBlockGlobalNumber, toBlockHash32, "RATING");
|
||||
this.subType = MsgSubType.TEXT_RATING;
|
||||
this.version = VER;
|
||||
this.toBlockchainName = toBlockchainName;
|
||||
this.toLogin = toLogin;
|
||||
this.toForkNumber = toForkNumber;
|
||||
this.toBlockGlobalNumber = toBlockGlobalNumber;
|
||||
this.toBlockHash32 = Arrays.copyOf(toBlockHash32, 32);
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextRatingBody check() {
|
||||
if ((subType & 0xFFFF) != (MsgSubType.TEXT_RATING & 0xFFFF)) {
|
||||
throw new IllegalArgumentException("Bad TextRatingBody subType: " + (subType & 0xFFFF));
|
||||
}
|
||||
if (toBlockchainName == null || toBlockchainName.isBlank()) {
|
||||
throw new IllegalArgumentException("RATING toBlockchainName is blank");
|
||||
}
|
||||
if (toBlockGlobalNumber < 0) throw new IllegalArgumentException("toBlockGlobalNumber < 0");
|
||||
if (toBlockHash32 == null || toBlockHash32.length != 32) {
|
||||
throw new IllegalArgumentException("toBlockHash32 invalid");
|
||||
}
|
||||
@Override public TextRatingBody check() {
|
||||
if ((subType & 0xFFFF) != (MsgSubType.TEXT_RATING & 0xFFFF)) throw new IllegalArgumentException("Bad RATING subType");
|
||||
BodyTargetSupport.validateExternal(toLogin, toForkNumber, toBlockGlobalNumber, toBlockHash32, "RATING");
|
||||
if (message == null || message.isBlank()) throw new IllegalArgumentException("message is blank");
|
||||
if (message.getBytes(StandardCharsets.UTF_8).length > 65535) throw new IllegalArgumentException("Text too long");
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toBytes() {
|
||||
byte[] msgUtf8 = message.getBytes(StandardCharsets.UTF_8);
|
||||
if (msgUtf8.length == 0) throw new IllegalArgumentException("Text payload is empty");
|
||||
if (msgUtf8.length > 65535) throw new IllegalArgumentException("Text too long (>65535 bytes)");
|
||||
|
||||
byte[] nameUtf8 = toBlockchainName.getBytes(StandardCharsets.UTF_8);
|
||||
if (nameUtf8.length == 0 || nameUtf8.length > 255) {
|
||||
throw new IllegalArgumentException("RATING toBlockchainName utf8 len must be 1..255");
|
||||
}
|
||||
|
||||
ByteBuffer bb = ByteBuffer.allocate(1 + nameUtf8.length + 4 + 32 + 2 + msgUtf8.length)
|
||||
.order(ByteOrder.BIG_ENDIAN);
|
||||
bb.put((byte) nameUtf8.length);
|
||||
bb.put(nameUtf8);
|
||||
bb.putInt(toBlockGlobalNumber);
|
||||
bb.put(toBlockHash32);
|
||||
bb.putShort((short) msgUtf8.length);
|
||||
bb.put(msgUtf8);
|
||||
@Override public byte[] toBytes() {
|
||||
check();
|
||||
byte[] text = message.getBytes(StandardCharsets.UTF_8);
|
||||
ByteBuffer bb = ByteBuffer.allocate(BodyTargetSupport.externalSize(toLogin) + 2 + text.length).order(ByteOrder.BIG_ENDIAN);
|
||||
BodyTargetSupport.writeExternal(bb, toLogin, toForkNumber, toBlockGlobalNumber, toBlockHash32, "RATING");
|
||||
bb.putShort((short) text.length).put(text);
|
||||
return bb.array();
|
||||
}
|
||||
|
||||
@Override public String toBchName() { return toBlockchainName; }
|
||||
@Override public String toLogin() { return toLogin; }
|
||||
@Override public Integer toForkNumber() { return toForkNumber; }
|
||||
@Override public Integer toBlockGlobalNumber() { return toBlockGlobalNumber; }
|
||||
@Override public byte[] toBlockHashBytes() { return toBlockHash32; }
|
||||
@Override public byte[] toBlockHashBytes() { return Arrays.copyOf(toBlockHash32, 32); }
|
||||
|
||||
private static String readStrictUtf8Len16(ByteBuffer bb, String fieldName) {
|
||||
private static String readText(ByteBuffer bb) {
|
||||
if (bb.remaining() < 2) throw new IllegalArgumentException("RATING text length missing");
|
||||
int len = Short.toUnsignedInt(bb.getShort());
|
||||
if (len == 0) throw new IllegalArgumentException(fieldName + " is empty");
|
||||
if (bb.remaining() < len) throw new IllegalArgumentException(fieldName + " payload too short (len=" + len + ")");
|
||||
|
||||
byte[] bytes = new byte[len];
|
||||
bb.get(bytes);
|
||||
|
||||
var decoder = StandardCharsets.UTF_8.newDecoder()
|
||||
.onMalformedInput(CodingErrorAction.REPORT)
|
||||
.onUnmappableCharacter(CodingErrorAction.REPORT);
|
||||
|
||||
try {
|
||||
String s = decoder.decode(ByteBuffer.wrap(bytes)).toString();
|
||||
if (s.isBlank()) throw new IllegalArgumentException(fieldName + " is blank");
|
||||
return s;
|
||||
} catch (CharacterCodingException e) {
|
||||
throw new IllegalArgumentException(fieldName + " is not valid UTF-8", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ensureMin(ByteBuffer bb, int need, String msg) {
|
||||
if (bb.remaining() < need) throw new IllegalArgumentException(msg + " (need=" + need + ", remaining=" + bb.remaining() + ")");
|
||||
}
|
||||
|
||||
private static void ensureNoTail(ByteBuffer bb, String ctx) {
|
||||
if (bb.remaining() != 0) throw new IllegalArgumentException("Unexpected tail bytes for " + ctx + ", remaining=" + bb.remaining());
|
||||
if (len <= 0 || bb.remaining() < len) throw new IllegalArgumentException("RATING text invalid");
|
||||
byte[] bytes = new byte[len]; bb.get(bytes);
|
||||
var dec = StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT);
|
||||
try { String s = dec.decode(ByteBuffer.wrap(bytes)).toString(); if (s.isBlank()) throw new IllegalArgumentException("RATING text blank"); return s; }
|
||||
catch (CharacterCodingException e) { throw new IllegalArgumentException("RATING text invalid UTF-8", e); }
|
||||
}
|
||||
}
|
||||
|
||||
+60
-190
@@ -11,241 +11,111 @@ import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* TextReplyBody — type=1, ver=1.
|
||||
*
|
||||
* subType:
|
||||
* - REPLY (20)
|
||||
* - EDIT_REPLY (21)
|
||||
*
|
||||
* Форматы bodyBytes (BigEndian):
|
||||
*
|
||||
* REPLY:
|
||||
* [1] toBlockchainNameLen (uint8)
|
||||
* [N] toBlockchainName UTF-8
|
||||
* [4] toBlockGlobalNumber
|
||||
* [32] toBlockHash32
|
||||
* [2] textLenBytes (uint16)
|
||||
* [M] text UTF-8
|
||||
*
|
||||
* EDIT_REPLY:
|
||||
* [4] toBlockGlobalNumber
|
||||
* [32] toBlockHash32
|
||||
* [2] textLenBytes (uint16)
|
||||
* [N] text UTF-8
|
||||
* TEXT_REPLY / TEXT_EDIT_REPLY.
|
||||
* REPLY external target: login + forkNumber + blockNumber + hash32.
|
||||
* EDIT_REPLY compact own-chain target: blockNumber + hash32.
|
||||
*/
|
||||
public final class TextReplyBody implements BodyRecord, BodyHasTarget {
|
||||
|
||||
public static final short TYPE = 1;
|
||||
public static final short VER = 1;
|
||||
|
||||
public static final short VER = 1;
|
||||
public static final int KEY = ((TYPE & 0xFFFF) << 16) | (VER & 0xFFFF);
|
||||
|
||||
public final short subType; // из header
|
||||
public final short version; // (=1)
|
||||
|
||||
// target
|
||||
public final String toBlockchainName; // nullable для EDIT_REPLY
|
||||
public final short subType;
|
||||
public final short version;
|
||||
public final String toLogin;
|
||||
public final Integer toForkNumber;
|
||||
public final int toBlockGlobalNumber;
|
||||
public final byte[] toBlockHash32; // 32
|
||||
|
||||
// text
|
||||
public final byte[] toBlockHash32;
|
||||
public final String message;
|
||||
|
||||
public TextReplyBody(short subType, short version, byte[] bodyBytes) {
|
||||
Objects.requireNonNull(bodyBytes, "bodyBytes == null");
|
||||
|
||||
this.subType = subType;
|
||||
this.version = version;
|
||||
|
||||
if ((this.version & 0xFFFF) != (VER & 0xFFFF)) {
|
||||
throw new IllegalArgumentException("TextReplyBody version must be 1, got=" + (this.version & 0xFFFF));
|
||||
}
|
||||
|
||||
int st = this.subType & 0xFFFF;
|
||||
if (st != (MsgSubType.TEXT_REPLY & 0xFFFF) && st != (MsgSubType.TEXT_EDIT_REPLY & 0xFFFF)) {
|
||||
throw new IllegalArgumentException("TextReplyBody supports only REPLY/EDIT_REPLY, got subType=" + st);
|
||||
}
|
||||
if ((version & 0xFFFF) != (VER & 0xFFFF)) throw new IllegalArgumentException("TextReplyBody version must be 1");
|
||||
int st = subType & 0xFFFF;
|
||||
if (st != (MsgSubType.TEXT_REPLY & 0xFFFF) && st != (MsgSubType.TEXT_EDIT_REPLY & 0xFFFF)) throw new IllegalArgumentException("Bad reply subtype");
|
||||
|
||||
ByteBuffer bb = ByteBuffer.wrap(bodyBytes).order(ByteOrder.BIG_ENDIAN);
|
||||
|
||||
if (st == (MsgSubType.TEXT_REPLY & 0xFFFF)) {
|
||||
// минимум: nameLen[1]+name[1]+global[4]+hash[32]+textLen[2]
|
||||
ensureMin(bb, 1 + 1 + 4 + 32 + 2, "REPLY too short");
|
||||
|
||||
int nameLen = Byte.toUnsignedInt(bb.get());
|
||||
if (nameLen <= 0) throw new IllegalArgumentException("REPLY toBlockchainNameLen is 0");
|
||||
ensureMin(bb, nameLen + 4 + 32 + 2, "REPLY payload too short");
|
||||
|
||||
byte[] nameBytes = new byte[nameLen];
|
||||
bb.get(nameBytes);
|
||||
this.toBlockchainName = new String(nameBytes, StandardCharsets.UTF_8);
|
||||
|
||||
this.toBlockGlobalNumber = bb.getInt();
|
||||
|
||||
this.toBlockHash32 = new byte[32];
|
||||
bb.get(this.toBlockHash32);
|
||||
|
||||
BodyTargetSupport.ExternalTarget t = BodyTargetSupport.readExternal(bb, "REPLY", 2);
|
||||
this.toLogin = t.login();
|
||||
this.toForkNumber = t.forkNumber();
|
||||
this.toBlockGlobalNumber = t.blockNumber();
|
||||
this.toBlockHash32 = t.blockHash32();
|
||||
} else {
|
||||
// EDIT_REPLY: target без имени
|
||||
ensureMin(bb, (4 + 32) + 2, "EDIT_REPLY too short");
|
||||
|
||||
this.toBlockchainName = null;
|
||||
BodyTargetSupport.ensureMin(bb, 4 + 32 + 2, "EDIT_REPLY too short");
|
||||
this.toLogin = null;
|
||||
this.toForkNumber = null;
|
||||
this.toBlockGlobalNumber = bb.getInt();
|
||||
|
||||
this.toBlockHash32 = new byte[32];
|
||||
bb.get(this.toBlockHash32);
|
||||
BodyTargetSupport.validateCompact(this.toBlockGlobalNumber, this.toBlockHash32, "EDIT_REPLY");
|
||||
}
|
||||
|
||||
this.message = readStrictUtf8Len16(bb, "TextReplyBody text", st == (MsgSubType.TEXT_EDIT_REPLY & 0xFFFF));
|
||||
ensureNoTail(bb, "TextReplyBody");
|
||||
this.message = readText(bb, st == (MsgSubType.TEXT_EDIT_REPLY & 0xFFFF));
|
||||
if (bb.hasRemaining()) throw new IllegalArgumentException("Unexpected TextReplyBody tail");
|
||||
}
|
||||
|
||||
public TextReplyBody(short subType,
|
||||
int toBlockGlobalNumber,
|
||||
byte[] toBlockHash32,
|
||||
String toBlockchainName,
|
||||
String message) {
|
||||
|
||||
public TextReplyBody(short subType, String toLogin, Integer toForkNumber, int toBlockGlobalNumber, byte[] toBlockHash32, String message) {
|
||||
Objects.requireNonNull(message, "message == null");
|
||||
Objects.requireNonNull(toBlockHash32, "toBlockHash32 == null");
|
||||
|
||||
int st = subType & 0xFFFF;
|
||||
if (st != (MsgSubType.TEXT_REPLY & 0xFFFF) && st != (MsgSubType.TEXT_EDIT_REPLY & 0xFFFF)) {
|
||||
throw new IllegalArgumentException("TextReplyBody supports only REPLY/EDIT_REPLY");
|
||||
}
|
||||
if (st == (MsgSubType.TEXT_REPLY & 0xFFFF) && message.isBlank()) {
|
||||
throw new IllegalArgumentException("message is blank");
|
||||
}
|
||||
|
||||
if (toBlockGlobalNumber < 0) throw new IllegalArgumentException("toBlockGlobalNumber < 0");
|
||||
if (toBlockHash32.length != 32) throw new IllegalArgumentException("toBlockHash32 != 32");
|
||||
|
||||
if (st != (MsgSubType.TEXT_REPLY & 0xFFFF) && st != (MsgSubType.TEXT_EDIT_REPLY & 0xFFFF)) throw new IllegalArgumentException("Bad reply subtype");
|
||||
if (st == (MsgSubType.TEXT_REPLY & 0xFFFF)) {
|
||||
Objects.requireNonNull(toBlockchainName, "toBlockchainName == null");
|
||||
if (toBlockchainName.isBlank()) throw new IllegalArgumentException("toBlockchainName is blank");
|
||||
this.toBlockchainName = toBlockchainName;
|
||||
if (message.isBlank()) throw new IllegalArgumentException("REPLY message is blank");
|
||||
BodyTargetSupport.validateExternal(toLogin, toForkNumber, toBlockGlobalNumber, toBlockHash32, "REPLY");
|
||||
} else {
|
||||
// EDIT_REPLY: имя не хранить
|
||||
this.toBlockchainName = null;
|
||||
if (toLogin != null || toForkNumber != null) throw new IllegalArgumentException("EDIT_REPLY must not contain login/fork target");
|
||||
BodyTargetSupport.validateCompact(toBlockGlobalNumber, toBlockHash32, "EDIT_REPLY");
|
||||
}
|
||||
|
||||
this.subType = subType;
|
||||
this.version = VER;
|
||||
|
||||
this.toLogin = toLogin;
|
||||
this.toForkNumber = toForkNumber;
|
||||
this.toBlockGlobalNumber = toBlockGlobalNumber;
|
||||
this.toBlockHash32 = Arrays.copyOf(toBlockHash32, 32);
|
||||
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextReplyBody check() {
|
||||
@Override public TextReplyBody check() {
|
||||
int st = subType & 0xFFFF;
|
||||
if (st != (MsgSubType.TEXT_REPLY & 0xFFFF) && st != (MsgSubType.TEXT_EDIT_REPLY & 0xFFFF))
|
||||
throw new IllegalArgumentException("Bad TextReplyBody subType: " + st);
|
||||
|
||||
if (toBlockGlobalNumber < 0)
|
||||
throw new IllegalArgumentException("toBlockGlobalNumber < 0");
|
||||
if (toBlockHash32 == null || toBlockHash32.length != 32)
|
||||
throw new IllegalArgumentException("toBlockHash32 invalid");
|
||||
|
||||
if (st == (MsgSubType.TEXT_REPLY & 0xFFFF)) {
|
||||
if (message == null || message.isBlank())
|
||||
throw new IllegalArgumentException("Text message is blank");
|
||||
if (toBlockchainName == null || toBlockchainName.isBlank())
|
||||
throw new IllegalArgumentException("REPLY toBlockchainName is blank");
|
||||
} else {
|
||||
BodyTargetSupport.validateExternal(toLogin, toForkNumber, toBlockGlobalNumber, toBlockHash32, "REPLY");
|
||||
if (message == null || message.isBlank()) throw new IllegalArgumentException("REPLY message is blank");
|
||||
} else if (st == (MsgSubType.TEXT_EDIT_REPLY & 0xFFFF)) {
|
||||
if (toLogin != null || toForkNumber != null) throw new IllegalArgumentException("EDIT_REPLY must not contain login/fork target");
|
||||
BodyTargetSupport.validateCompact(toBlockGlobalNumber, toBlockHash32, "EDIT_REPLY");
|
||||
if (message == null) throw new IllegalArgumentException("EDIT_REPLY message is null");
|
||||
if (toBlockchainName != null)
|
||||
throw new IllegalArgumentException("EDIT_REPLY must not contain toBlockchainName");
|
||||
}
|
||||
|
||||
} else throw new IllegalArgumentException("Bad reply subtype");
|
||||
if (message.getBytes(StandardCharsets.UTF_8).length > 65535) throw new IllegalArgumentException("Text too long");
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] toBytes() {
|
||||
byte[] msgUtf8 = message.getBytes(StandardCharsets.UTF_8);
|
||||
if (msgUtf8.length > 65535) throw new IllegalArgumentException("Text too long (>65535 bytes)");
|
||||
|
||||
int st = subType & 0xFFFF;
|
||||
if (st == (MsgSubType.TEXT_REPLY & 0xFFFF) && msgUtf8.length == 0) {
|
||||
throw new IllegalArgumentException("Text payload is empty");
|
||||
}
|
||||
|
||||
if (st == (MsgSubType.TEXT_REPLY & 0xFFFF)) {
|
||||
if (toBlockchainName == null) throw new IllegalArgumentException("REPLY missing toBlockchainName");
|
||||
|
||||
byte[] nameUtf8 = toBlockchainName.getBytes(StandardCharsets.UTF_8);
|
||||
if (nameUtf8.length == 0 || nameUtf8.length > 255)
|
||||
throw new IllegalArgumentException("REPLY toBlockchainName utf8 len must be 1..255");
|
||||
|
||||
int cap = 1 + nameUtf8.length + 4 + 32 + 2 + msgUtf8.length;
|
||||
|
||||
ByteBuffer bb = ByteBuffer.allocate(cap).order(ByteOrder.BIG_ENDIAN);
|
||||
bb.put((byte) nameUtf8.length);
|
||||
bb.put(nameUtf8);
|
||||
bb.putInt(toBlockGlobalNumber);
|
||||
bb.put(toBlockHash32);
|
||||
bb.putShort((short) msgUtf8.length);
|
||||
bb.put(msgUtf8);
|
||||
|
||||
return bb.array();
|
||||
}
|
||||
|
||||
// EDIT_REPLY
|
||||
int cap = (4 + 32) + 2 + msgUtf8.length;
|
||||
|
||||
ByteBuffer bb = ByteBuffer.allocate(cap).order(ByteOrder.BIG_ENDIAN);
|
||||
bb.putInt(toBlockGlobalNumber);
|
||||
bb.put(toBlockHash32);
|
||||
bb.putShort((short) msgUtf8.length);
|
||||
bb.put(msgUtf8);
|
||||
|
||||
@Override public byte[] toBytes() {
|
||||
check();
|
||||
byte[] text = message.getBytes(StandardCharsets.UTF_8);
|
||||
boolean reply = (subType & 0xFFFF) == (MsgSubType.TEXT_REPLY & 0xFFFF);
|
||||
int targetSize = reply ? BodyTargetSupport.externalSize(toLogin) : 4 + 32;
|
||||
ByteBuffer bb = ByteBuffer.allocate(targetSize + 2 + text.length).order(ByteOrder.BIG_ENDIAN);
|
||||
if (reply) BodyTargetSupport.writeExternal(bb, toLogin, toForkNumber, toBlockGlobalNumber, toBlockHash32, "REPLY");
|
||||
else bb.putInt(toBlockGlobalNumber).put(toBlockHash32);
|
||||
bb.putShort((short) text.length).put(text);
|
||||
return bb.array();
|
||||
}
|
||||
|
||||
/* ====================== BodyHasTarget ====================== */
|
||||
|
||||
@Override public String toBchName() { return toBlockchainName; }
|
||||
@Override public String toLogin() { return toLogin; }
|
||||
@Override public Integer toForkNumber() { return toForkNumber; }
|
||||
@Override public Integer toBlockGlobalNumber() { return toBlockGlobalNumber; }
|
||||
@Override public byte[] toBlockHashBytes() { return toBlockHash32; }
|
||||
@Override public byte[] toBlockHashBytes() { return Arrays.copyOf(toBlockHash32, 32); }
|
||||
public boolean isEditReply() { return (subType & 0xFFFF) == (MsgSubType.TEXT_EDIT_REPLY & 0xFFFF); }
|
||||
|
||||
public boolean isEditReply() {
|
||||
return (subType & 0xFFFF) == (MsgSubType.TEXT_EDIT_REPLY & 0xFFFF);
|
||||
}
|
||||
|
||||
/* ====================== helpers ====================== */
|
||||
|
||||
private static String readStrictUtf8Len16(ByteBuffer bb, String fieldName, boolean allowEmpty) {
|
||||
private static String readText(ByteBuffer bb, boolean allowEmpty) {
|
||||
if (bb.remaining() < 2) throw new IllegalArgumentException("Text length missing");
|
||||
int len = Short.toUnsignedInt(bb.getShort());
|
||||
if (len == 0) {
|
||||
if (allowEmpty) return "";
|
||||
throw new IllegalArgumentException(fieldName + " is empty");
|
||||
}
|
||||
if (bb.remaining() < len) throw new IllegalArgumentException(fieldName + " payload too short (len=" + len + ")");
|
||||
|
||||
byte[] bytes = new byte[len];
|
||||
bb.get(bytes);
|
||||
|
||||
var decoder = StandardCharsets.UTF_8.newDecoder()
|
||||
.onMalformedInput(CodingErrorAction.REPORT)
|
||||
.onUnmappableCharacter(CodingErrorAction.REPORT);
|
||||
|
||||
try {
|
||||
String s = decoder.decode(ByteBuffer.wrap(bytes)).toString();
|
||||
if (!allowEmpty && s.isBlank()) throw new IllegalArgumentException(fieldName + " is blank");
|
||||
return s;
|
||||
} catch (CharacterCodingException e) {
|
||||
throw new IllegalArgumentException(fieldName + " is not valid UTF-8", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ensureMin(ByteBuffer bb, int need, String msg) {
|
||||
if (bb.remaining() < need) throw new IllegalArgumentException(msg + " (need=" + need + ", remaining=" + bb.remaining() + ")");
|
||||
}
|
||||
|
||||
private static void ensureNoTail(ByteBuffer bb, String ctx) {
|
||||
if (bb.remaining() != 0) throw new IllegalArgumentException("Unexpected tail bytes for " + ctx + ", remaining=" + bb.remaining());
|
||||
if (len == 0) { if (allowEmpty) return ""; throw new IllegalArgumentException("Text is empty"); }
|
||||
if (bb.remaining() < len) throw new IllegalArgumentException("Text payload too short");
|
||||
byte[] bytes = new byte[len]; bb.get(bytes);
|
||||
var dec = StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT);
|
||||
try { String s = dec.decode(ByteBuffer.wrap(bytes)).toString(); if (!allowEmpty && s.isBlank()) throw new IllegalArgumentException("Text is blank"); return s; }
|
||||
catch (CharacterCodingException e) { throw new IllegalArgumentException("Text invalid UTF-8", e); }
|
||||
}
|
||||
}
|
||||
|
||||
+38
-64
@@ -11,16 +11,16 @@ import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* UserParamBody — type=4, ver=1 (в заголовке блока).
|
||||
* UserParamBody — type=4, ver=1 (в документации: USER_PROFILE).
|
||||
*
|
||||
* subType (в заголовке блока):
|
||||
* 1 = TEXT_TEXT
|
||||
*
|
||||
* bodyBytes (BigEndian), новый формат:
|
||||
* [4] lineCode
|
||||
* [4] prevLineNumber
|
||||
* [32] prevLineHash32
|
||||
* [4] thisLineNumber
|
||||
* USER_PROFILE имеет единственную фиксированную линию, поэтому lineCode в body не хранится.
|
||||
* bodyBytes (BigEndian):
|
||||
* [4] prevProfileBlockNumber — GLOBAL blockNumber предыдущего USER_PROFILE-блока
|
||||
* [32] prevProfileBlockHash32 — hash32 предыдущего USER_PROFILE-блока
|
||||
* [4] profileSequence — порядковый номер изменения профиля (1..N)
|
||||
*
|
||||
* [2] keyLenBytes (uint16)
|
||||
* [N] keyUtf8
|
||||
@@ -35,14 +35,12 @@ public final class UserParamBody implements BodyRecord, BodyHasLine {
|
||||
|
||||
public static final int KEY = ((TYPE & 0xFFFF) << 16) | (VER & 0xFFFF);
|
||||
|
||||
public final short subType; // из header
|
||||
public final short version; // из header
|
||||
public final short subType;
|
||||
public final short version;
|
||||
|
||||
// line
|
||||
public final int lineCode;
|
||||
public final int prevLineNumber;
|
||||
public final byte[] prevLineHash32;
|
||||
public final int thisLineNumber;
|
||||
public final int prevProfileBlockNumber;
|
||||
public final byte[] prevProfileBlockHash32;
|
||||
public final int profileSequence;
|
||||
|
||||
public final String paramKey;
|
||||
public final String paramValue;
|
||||
@@ -60,21 +58,17 @@ public final class UserParamBody implements BodyRecord, BodyHasLine {
|
||||
throw new IllegalArgumentException("Bad UserParam subType: " + (this.subType & 0xFFFF));
|
||||
}
|
||||
|
||||
// минимум: lineCode(4)+line(4+32+4) + keyLen(2)+key(1) + valLen(2)+val(1)
|
||||
if (bodyBytes.length < 4 + (4 + 32 + 4) + 2 + 1 + 2 + 1) {
|
||||
// prevGlobal(4)+prevHash(32)+seq(4) + keyLen(2)+key(1) + valLen(2)+val(1)
|
||||
if (bodyBytes.length < 4 + 32 + 4 + 2 + 1 + 2 + 1) {
|
||||
throw new IllegalArgumentException("UserParamBody too short");
|
||||
}
|
||||
|
||||
ByteBuffer bb = ByteBuffer.wrap(bodyBytes).order(ByteOrder.BIG_ENDIAN);
|
||||
|
||||
this.lineCode = bb.getInt();
|
||||
|
||||
this.prevLineNumber = bb.getInt();
|
||||
|
||||
this.prevLineHash32 = new byte[32];
|
||||
bb.get(this.prevLineHash32);
|
||||
|
||||
this.thisLineNumber = bb.getInt();
|
||||
this.prevProfileBlockNumber = bb.getInt();
|
||||
this.prevProfileBlockHash32 = new byte[32];
|
||||
bb.get(this.prevProfileBlockHash32);
|
||||
this.profileSequence = bb.getInt();
|
||||
|
||||
int keyLen = Short.toUnsignedInt(bb.getShort());
|
||||
if (keyLen <= 0) throw new IllegalArgumentException("paramKeyLen is 0");
|
||||
@@ -99,25 +93,21 @@ public final class UserParamBody implements BodyRecord, BodyHasLine {
|
||||
if (this.paramValue.isBlank()) throw new IllegalArgumentException("paramValue is blank");
|
||||
}
|
||||
|
||||
public UserParamBody(int lineCode,
|
||||
int prevLineNumber,
|
||||
byte[] prevLineHash32,
|
||||
int thisLineNumber,
|
||||
public UserParamBody(int prevProfileBlockNumber,
|
||||
byte[] prevProfileBlockHash32,
|
||||
int profileSequence,
|
||||
String paramKey,
|
||||
String paramValue) {
|
||||
|
||||
Objects.requireNonNull(paramKey, "paramKey == null");
|
||||
Objects.requireNonNull(paramValue, "paramValue == null");
|
||||
|
||||
if (lineCode < 0) throw new IllegalArgumentException("lineCode < 0");
|
||||
|
||||
this.subType = MsgSubType.USER_PARAM_TEXT_TEXT;
|
||||
this.version = VER;
|
||||
|
||||
this.lineCode = lineCode;
|
||||
this.prevLineNumber = prevLineNumber;
|
||||
this.prevLineHash32 = (prevLineHash32 == null ? new byte[32] : Arrays.copyOf(prevLineHash32, 32));
|
||||
this.thisLineNumber = thisLineNumber;
|
||||
this.prevProfileBlockNumber = prevProfileBlockNumber;
|
||||
this.prevProfileBlockHash32 = (prevProfileBlockHash32 == null ? new byte[32] : Arrays.copyOf(prevProfileBlockHash32, 32));
|
||||
this.profileSequence = profileSequence;
|
||||
|
||||
if (paramKey.isBlank()) throw new IllegalArgumentException("paramKey is blank");
|
||||
if (paramValue.isBlank()) throw new IllegalArgumentException("paramValue is blank");
|
||||
@@ -128,17 +118,12 @@ public final class UserParamBody implements BodyRecord, BodyHasLine {
|
||||
|
||||
@Override
|
||||
public UserParamBody check() {
|
||||
if (lineCode < 0) throw new IllegalArgumentException("lineCode < 0");
|
||||
|
||||
if ((subType & 0xFFFF) != (MsgSubType.USER_PARAM_TEXT_TEXT & 0xFFFF))
|
||||
if ((subType & 0xFFFF) != (MsgSubType.USER_PARAM_TEXT_TEXT & 0xFFFF)) {
|
||||
throw new IllegalArgumentException("Bad UserParam subType: " + (subType & 0xFFFF));
|
||||
|
||||
if (prevLineNumber == -1) {
|
||||
if (!isAllZero32(prevLineHash32)) throw new IllegalArgumentException("prevLineHash32 must be zero when prevLineNumber=-1");
|
||||
if (thisLineNumber != -1) throw new IllegalArgumentException("thisLineNumber must be -1 when prevLineNumber=-1");
|
||||
} else {
|
||||
if (prevLineHash32 == null || prevLineHash32.length != 32) throw new IllegalArgumentException("prevLineHash32 invalid");
|
||||
}
|
||||
if (prevProfileBlockNumber < 0) throw new IllegalArgumentException("prevProfileBlockNumber must be >=0");
|
||||
if (prevProfileBlockHash32 == null || prevProfileBlockHash32.length != 32) throw new IllegalArgumentException("prevProfileBlockHash32 invalid");
|
||||
if (profileSequence <= 0) throw new IllegalArgumentException("profileSequence must be >=1");
|
||||
|
||||
if (paramKey == null || paramKey.isBlank()) throw new IllegalArgumentException("paramKey is blank");
|
||||
if (paramValue == null || paramValue.isBlank()) throw new IllegalArgumentException("paramValue is blank");
|
||||
@@ -148,30 +133,25 @@ public final class UserParamBody implements BodyRecord, BodyHasLine {
|
||||
|
||||
@Override
|
||||
public byte[] toBytes() {
|
||||
check();
|
||||
byte[] keyUtf8 = paramKey.getBytes(StandardCharsets.UTF_8);
|
||||
byte[] valUtf8 = paramValue.getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
if (keyUtf8.length == 0 || keyUtf8.length > 65535) throw new IllegalArgumentException("paramKey utf8 len must be 1..65535");
|
||||
if (valUtf8.length == 0 || valUtf8.length > 65535) throw new IllegalArgumentException("paramValue utf8 len must be 1..65535");
|
||||
|
||||
int cap = 4 + (4 + 32 + 4)
|
||||
int cap = 4 + 32 + 4
|
||||
+ 2 + keyUtf8.length
|
||||
+ 2 + valUtf8.length;
|
||||
|
||||
ByteBuffer bb = ByteBuffer.allocate(cap).order(ByteOrder.BIG_ENDIAN);
|
||||
|
||||
bb.putInt(lineCode);
|
||||
|
||||
bb.putInt(prevLineNumber);
|
||||
bb.put(prevLineHash32 == null ? new byte[32] : Arrays.copyOf(prevLineHash32, 32));
|
||||
bb.putInt(thisLineNumber);
|
||||
|
||||
bb.putInt(prevProfileBlockNumber);
|
||||
bb.put(prevProfileBlockHash32);
|
||||
bb.putInt(profileSequence);
|
||||
bb.putShort((short) keyUtf8.length);
|
||||
bb.put(keyUtf8);
|
||||
|
||||
bb.putShort((short) valUtf8.length);
|
||||
bb.put(valUtf8);
|
||||
|
||||
return bb.array();
|
||||
}
|
||||
|
||||
@@ -187,15 +167,9 @@ public final class UserParamBody implements BodyRecord, BodyHasLine {
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isAllZero32(byte[] b) {
|
||||
if (b == null || b.length != 32) return true;
|
||||
for (int i = 0; i < 32; i++) if (b[i] != 0) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ====================== BodyHasLine ====================== */
|
||||
@Override public int lineCode() { return lineCode; }
|
||||
@Override public int prevLineBlockGlobalNumber() { return prevLineNumber; }
|
||||
@Override public byte[] prevLineBlockHash32() { return prevLineHash32 == null ? null : Arrays.copyOf(prevLineHash32, 32); }
|
||||
@Override public int lineSeq() { return thisLineNumber; }
|
||||
}
|
||||
/* USER_PROFILE имеет единственную линию, логический lineCode всегда 0. */
|
||||
@Override public int lineCode() { return 0; }
|
||||
@Override public int prevLineBlockGlobalNumber() { return prevProfileBlockNumber; }
|
||||
@Override public byte[] prevLineBlockHash32() { return Arrays.copyOf(prevProfileBlockHash32, 32); }
|
||||
@Override public int lineSeq() { return profileSequence; }
|
||||
}
|
||||
|
||||
+36
-53
@@ -1,71 +1,54 @@
|
||||
package utils.blockchain;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/** Canonical physical fork name helper: login-NNN, forks are limited to 001..999. */
|
||||
public final class BlockchainNameUtil {
|
||||
|
||||
/**
|
||||
* Теперь новое правило:
|
||||
* blockchainName = login + "-"+ 3 цифры
|
||||
* Пример: "Dima-001" -> "Dima"
|
||||
*
|
||||
* Сколько символов отрезаем с конца blockchainName, чтобы получить login: "-001" = 4
|
||||
*/
|
||||
public static final int BLOCKCHAIN_NAME_LOGIN_SUFFIX_LEN = 4;
|
||||
private static final Pattern SUFFIX = Pattern.compile("^(.*)-(\\d{3})$");
|
||||
public static final int MIN_FORK_NUMBER = 1;
|
||||
public static final int MAX_FORK_NUMBER = 999;
|
||||
|
||||
private BlockchainNameUtil() {}
|
||||
|
||||
/**
|
||||
* Извлечь login из blockchainName: отрезаем последние 4 символа ("-NNN").
|
||||
* Пример: "Dima-001" -> "Dima"
|
||||
*/
|
||||
public static String loginFromBlockchainName(String blockchainName) {
|
||||
if (blockchainName == null) return null;
|
||||
Matcher m = matcher(blockchainName);
|
||||
return m == null ? null : m.group(1);
|
||||
}
|
||||
|
||||
String s = blockchainName.trim();
|
||||
if (!hasDashAnd3DigitsSuffix(s)) return null;
|
||||
|
||||
return s.substring(0, s.length() - BLOCKCHAIN_NAME_LOGIN_SUFFIX_LEN);
|
||||
public static Integer forkNumberFromBlockchainName(String blockchainName) {
|
||||
Matcher m = matcher(blockchainName);
|
||||
if (m == null) return null;
|
||||
int fork = Integer.parseInt(m.group(2));
|
||||
return fork >= MIN_FORK_NUMBER && fork <= MAX_FORK_NUMBER ? fork : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Проверка правила:
|
||||
* - blockchainName должен оканчиваться на "-"+3 цифры
|
||||
* - blockchainName без суффикса "-NNN" должен равняться login
|
||||
*
|
||||
* ВАЖНО:
|
||||
* - сравнение строгое (case-sensitive)
|
||||
* - null/blank считаем невалидным
|
||||
*/
|
||||
public static boolean isBlockchainNameMatchesLogin(String blockchainName, String login) {
|
||||
if (blockchainName == null || login == null) return false;
|
||||
|
||||
String bn = blockchainName.trim();
|
||||
String lg = login.trim();
|
||||
|
||||
if (bn.isEmpty() || lg.isEmpty()) return false;
|
||||
if (!hasDashAnd3DigitsSuffix(bn)) return false;
|
||||
|
||||
String extracted = bn.substring(0, bn.length() - BLOCKCHAIN_NAME_LOGIN_SUFFIX_LEN);
|
||||
return Objects.equals(extracted, lg);
|
||||
if (login == null || login.isBlank()) return false;
|
||||
String extracted = loginFromBlockchainName(blockchainName);
|
||||
return extracted != null && Objects.equals(extracted, login.trim());
|
||||
}
|
||||
|
||||
private static boolean hasDashAnd3DigitsSuffix(String s) {
|
||||
if (s == null) return false;
|
||||
int len = s.length();
|
||||
if (len <= BLOCKCHAIN_NAME_LOGIN_SUFFIX_LEN) return false;
|
||||
|
||||
int dashPos = len - 4;
|
||||
if (s.charAt(dashPos) != '-') return false;
|
||||
|
||||
char c1 = s.charAt(len - 3);
|
||||
char c2 = s.charAt(len - 2);
|
||||
char c3 = s.charAt(len - 1);
|
||||
|
||||
return isDigit(c1) && isDigit(c2) && isDigit(c3);
|
||||
public static String blockchainName(String login, int forkNumber) {
|
||||
if (login == null || login.isBlank()) throw new IllegalArgumentException("login is blank");
|
||||
if (forkNumber < MIN_FORK_NUMBER || forkNumber > MAX_FORK_NUMBER) {
|
||||
throw new IllegalArgumentException("forkNumber must be 1..999");
|
||||
}
|
||||
return login.trim() + "-" + String.format("%03d", forkNumber);
|
||||
}
|
||||
|
||||
private static boolean isDigit(char c) {
|
||||
return c >= '0' && c <= '9';
|
||||
private static Matcher matcher(String blockchainName) {
|
||||
if (blockchainName == null) return null;
|
||||
String s = blockchainName.trim();
|
||||
if (s.isEmpty()) return null;
|
||||
Matcher m = SUFFIX.matcher(s);
|
||||
if (!m.matches() || m.group(1).isBlank()) return null;
|
||||
try {
|
||||
int fork = Integer.parseInt(m.group(2));
|
||||
return fork >= MIN_FORK_NUMBER && fork <= MAX_FORK_NUMBER ? m : null;
|
||||
} catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,297 +0,0 @@
|
||||
package utils.files;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.*;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* ===============================================================
|
||||
* FileStoreUtil — утилита работы с файлами в папке data/.
|
||||
*
|
||||
* Теперь поддерживает:
|
||||
* - основной файл блокчейна: <blockchainName>.bch
|
||||
* - временный файл блокчейна: <blockchainName>.tmp_bch
|
||||
* - sidecar-файл проверки записи: <blockchainName>.write_check
|
||||
* - marker-файл записи: <blockchainName>.write_pending
|
||||
*
|
||||
* Важное:
|
||||
* - validateSimpleFileName() запрещает path traversal.
|
||||
* - atomicReplaceBlockchainFile(): пытается сделать ATOMIC_MOVE (если ФС поддерживает),
|
||||
* иначе делает обычный REPLACE_EXISTING move.
|
||||
* ===============================================================
|
||||
*/
|
||||
public final class FileStoreUtil {
|
||||
|
||||
/** Базовая папка для хранения всех файлов (создаётся автоматически). */
|
||||
public static final String DATA_DIR_NAME = "data";
|
||||
|
||||
/** Расширение основного файла блокчейна. */
|
||||
public static final String BLOCKCHAIN_FILE_EXTENSION = ".bch";
|
||||
|
||||
/** Расширение временного файла (старое+новое). */
|
||||
public static final String BLOCKCHAIN_TMP_EXTENSION = ".tmp_bch";
|
||||
|
||||
/** Маркер того, что chain сейчас в процессе полного resync. */
|
||||
public static final String BLOCKCHAIN_RESYNC_MARKER_EXTENSION = ".resync_pending";
|
||||
|
||||
/** Marker того, что обычный AddBlock находится в опасной фазе записи. */
|
||||
public static final String BLOCKCHAIN_WRITE_PENDING_MARKER_EXTENSION = ".write_pending";
|
||||
|
||||
/** Sidecar-файл с blockNumber/blockHash для обычного AddBlock. */
|
||||
public static final String BLOCKCHAIN_WRITE_CHECK_EXTENSION = ".write_check";
|
||||
|
||||
private static final FileStoreUtil INSTANCE = new FileStoreUtil();
|
||||
|
||||
private final Path dataDirPath;
|
||||
|
||||
private FileStoreUtil() {
|
||||
this.dataDirPath = Paths.get(DATA_DIR_NAME);
|
||||
ensureDataDirExists();
|
||||
}
|
||||
|
||||
public static FileStoreUtil getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
/* ===================================================================== */
|
||||
/* ======================== Базовые операции =========================== */
|
||||
/* ===================================================================== */
|
||||
|
||||
public void newFile(String fileName, byte[] data) {
|
||||
Objects.requireNonNull(data, "data == null");
|
||||
Path target = resolveSafe(fileName);
|
||||
try {
|
||||
Files.write(target, data,
|
||||
StandardOpenOption.CREATE,
|
||||
StandardOpenOption.TRUNCATE_EXISTING,
|
||||
StandardOpenOption.WRITE);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Не удалось записать файл: " + target, e);
|
||||
}
|
||||
}
|
||||
|
||||
public void addDataToFile(String fileName, byte[] data) {
|
||||
Objects.requireNonNull(data, "data == null");
|
||||
Path target = resolveSafe(fileName);
|
||||
try {
|
||||
Files.write(target, data,
|
||||
StandardOpenOption.CREATE,
|
||||
StandardOpenOption.WRITE,
|
||||
StandardOpenOption.APPEND);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Не удалось дописать файл: " + target, e);
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] readAllDataFromFile(String fileName) {
|
||||
Path target = resolveSafe(fileName);
|
||||
if (!Files.exists(target)) {
|
||||
throw new IllegalStateException("Файл не найден: " + target);
|
||||
}
|
||||
try {
|
||||
return Files.readAllBytes(target);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Не удалось прочитать файл: " + target, e);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean exists(String fileName) {
|
||||
Path target = resolveSafe(fileName);
|
||||
return Files.exists(target);
|
||||
}
|
||||
|
||||
public long size(String fileName) {
|
||||
Path target = resolveSafe(fileName);
|
||||
try {
|
||||
return Files.size(target);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Не удалось получить размер файла: " + target, e);
|
||||
}
|
||||
}
|
||||
|
||||
/* ===================================================================== */
|
||||
/* ===================== Блокчейн-файлы по имени ======================= */
|
||||
/* ===================================================================== */
|
||||
|
||||
/** <blockchainName>.bch */
|
||||
public String buildBlockchainFileName(String blockchainName) {
|
||||
validateSimpleFileName(blockchainName);
|
||||
return blockchainName + BLOCKCHAIN_FILE_EXTENSION;
|
||||
}
|
||||
|
||||
/** <blockchainName>.tmp_bch */
|
||||
public String buildBlockchainTmpFileName(String blockchainName) {
|
||||
validateSimpleFileName(blockchainName);
|
||||
return blockchainName + BLOCKCHAIN_TMP_EXTENSION;
|
||||
}
|
||||
|
||||
public Path resolveBlockchainPath(String blockchainName) {
|
||||
return resolveSafe(buildBlockchainFileName(blockchainName));
|
||||
}
|
||||
|
||||
public Path resolveBlockchainTmpPath(String blockchainName) {
|
||||
return resolveSafe(buildBlockchainTmpFileName(blockchainName));
|
||||
}
|
||||
|
||||
public byte[] readBlockchain(String blockchainName) {
|
||||
return readAllDataFromFile(buildBlockchainFileName(blockchainName));
|
||||
}
|
||||
|
||||
public void writeBlockchainTmp(String blockchainName, byte[] data) {
|
||||
newFile(buildBlockchainTmpFileName(blockchainName), data);
|
||||
}
|
||||
|
||||
/** <blockchainName>.write_check */
|
||||
public String buildBlockchainWriteCheckFileName(String blockchainName) {
|
||||
validateSimpleFileName(blockchainName);
|
||||
return blockchainName + BLOCKCHAIN_WRITE_CHECK_EXTENSION;
|
||||
}
|
||||
|
||||
public Path resolveBlockchainWriteCheckPath(String blockchainName) {
|
||||
return resolveSafe(buildBlockchainWriteCheckFileName(blockchainName));
|
||||
}
|
||||
|
||||
public void writeBlockchainWriteCheck(String blockchainName, int blockNumber, String blockHashHex) {
|
||||
StringBuilder sb = new StringBuilder(128);
|
||||
sb.append("blockNumber=").append(blockNumber).append('\n');
|
||||
sb.append("blockHash=").append(blockHashHex == null ? "" : blockHashHex).append('\n');
|
||||
newFile(buildBlockchainWriteCheckFileName(blockchainName), sb.toString().getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
/** <blockchainName>.write_pending */
|
||||
public String buildBlockchainWritePendingMarkerFileName(String blockchainName) {
|
||||
validateSimpleFileName(blockchainName);
|
||||
return blockchainName + BLOCKCHAIN_WRITE_PENDING_MARKER_EXTENSION;
|
||||
}
|
||||
|
||||
public Path resolveBlockchainWritePendingMarkerPath(String blockchainName) {
|
||||
return resolveSafe(buildBlockchainWritePendingMarkerFileName(blockchainName));
|
||||
}
|
||||
|
||||
public void writeBlockchainWritePendingMarker(String blockchainName) {
|
||||
newFile(buildBlockchainWritePendingMarkerFileName(blockchainName), new byte[0]);
|
||||
}
|
||||
|
||||
/** <blockchainName>.resync_pending */
|
||||
public String buildBlockchainResyncMarkerFileName(String blockchainName) {
|
||||
validateSimpleFileName(blockchainName);
|
||||
return blockchainName + BLOCKCHAIN_RESYNC_MARKER_EXTENSION;
|
||||
}
|
||||
|
||||
public Path resolveBlockchainResyncMarkerPath(String blockchainName) {
|
||||
return resolveSafe(buildBlockchainResyncMarkerFileName(blockchainName));
|
||||
}
|
||||
|
||||
public void writeBlockchainResyncMarker(String blockchainName, String markerContent) {
|
||||
byte[] data = markerContent == null ? new byte[0] : markerContent.getBytes(java.nio.charset.StandardCharsets.UTF_8);
|
||||
newFile(buildBlockchainResyncMarkerFileName(blockchainName), data);
|
||||
}
|
||||
|
||||
public void deleteIfExists(Path path) {
|
||||
if (path == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Files.deleteIfExists(path);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Не удалось удалить файл: " + path, e);
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteBlockchainFileIfExists(String blockchainName) {
|
||||
deleteIfExists(resolveBlockchainPath(blockchainName));
|
||||
}
|
||||
|
||||
public void deleteBlockchainTmpFileIfExists(String blockchainName) {
|
||||
deleteIfExists(resolveBlockchainTmpPath(blockchainName));
|
||||
}
|
||||
|
||||
public void deleteBlockchainWriteCheckIfExists(String blockchainName) {
|
||||
deleteIfExists(resolveBlockchainWriteCheckPath(blockchainName));
|
||||
}
|
||||
|
||||
public void deleteBlockchainWritePendingMarkerIfExists(String blockchainName) {
|
||||
deleteIfExists(resolveBlockchainWritePendingMarkerPath(blockchainName));
|
||||
}
|
||||
|
||||
public void deleteBlockchainResyncMarkerIfExists(String blockchainName) {
|
||||
deleteIfExists(resolveBlockchainResyncMarkerPath(blockchainName));
|
||||
}
|
||||
|
||||
public boolean existsBlockchainResyncMarker(String blockchainName) {
|
||||
return exists(buildBlockchainResyncMarkerFileName(blockchainName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Атомарно заменить основной файл блокчейна временным:
|
||||
* <name>.tmp_bch -> <name>.bch
|
||||
*
|
||||
* Стратегия:
|
||||
* 1) Пытаемся Files.move(..., ATOMIC_MOVE, REPLACE_EXISTING)
|
||||
* 2) Если ATOMIC_MOVE не поддерживается — делаем move с REPLACE_EXISTING без атомарности
|
||||
*
|
||||
* Важный нюанс:
|
||||
* - атомарность гарантируется только в пределах одной файловой системы.
|
||||
*/
|
||||
public void atomicReplaceBlockchainFile(String blockchainName) {
|
||||
Path tmp = resolveBlockchainTmpPath(blockchainName);
|
||||
Path main = resolveBlockchainPath(blockchainName);
|
||||
|
||||
if (!Files.exists(tmp)) {
|
||||
throw new IllegalStateException("TMP-файл не найден: " + tmp);
|
||||
}
|
||||
|
||||
try {
|
||||
// 1) Пытаемся атомарный move
|
||||
Files.move(tmp, main,
|
||||
StandardCopyOption.REPLACE_EXISTING,
|
||||
StandardCopyOption.ATOMIC_MOVE);
|
||||
} catch (AtomicMoveNotSupportedException e) {
|
||||
// 2) Если ФС не поддерживает атомарный move — делаем обычный replace
|
||||
try {
|
||||
Files.move(tmp, main, StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (IOException ex) {
|
||||
throw new IllegalStateException("Не удалось заменить файл блокчейна (non-atomic): " + main, ex);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Не удалось заменить файл блокчейна (atomic): " + main, e);
|
||||
}
|
||||
}
|
||||
|
||||
/* ===================================================================== */
|
||||
/* ============================ Helpers ================================= */
|
||||
/* ===================================================================== */
|
||||
|
||||
private void ensureDataDirExists() {
|
||||
try {
|
||||
if (!Files.exists(dataDirPath)) {
|
||||
Files.createDirectories(dataDirPath);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("Не удалось создать директорию хранения: " + dataDirPath, e);
|
||||
}
|
||||
}
|
||||
|
||||
private Path resolveSafe(String fileName) {
|
||||
validateSimpleFileName(fileName);
|
||||
return dataDirPath.resolve(fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Валидация "простого имени":
|
||||
* - запрещаем слэши, обратные слэши, ".."
|
||||
* - запрещаем пустоту
|
||||
*
|
||||
* Важно: сюда у нас попадает и blockchainName (как часть имени файла),
|
||||
* поэтому blockchainName должен быть "простым": без путей.
|
||||
*/
|
||||
private void validateSimpleFileName(String fileName) {
|
||||
Objects.requireNonNull(fileName, "fileName == null");
|
||||
if (fileName.isBlank()) {
|
||||
throw new IllegalArgumentException("Имя файла не должно быть пустым");
|
||||
}
|
||||
if (fileName.contains("/") || fileName.contains("\\") || fileName.contains("..")) {
|
||||
throw new IllegalArgumentException("Недопустимое имя файла: " + fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package blockchain;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import utils.crypto.Ed25519Util;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
final class Ans104DataItemTest {
|
||||
@Test
|
||||
void ed25519RoundTripWithTestAndChannelTags() {
|
||||
byte[] privateKey = Ed25519Util.generatePrivateKeyFromString("shine-ans104-test-key");
|
||||
byte[] owner = Ed25519Util.derivePublicKey(privateKey);
|
||||
byte[] data = "frame-v1-test".getBytes(StandardCharsets.UTF_8);
|
||||
List<Ans104DataItem.Tag> tags = List.of(
|
||||
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);
|
||||
byte[] signature = Ed25519Util.sign(message, privateKey);
|
||||
byte[] raw = Ans104DataItem.serializeEd25519(signature, owner, tags, data);
|
||||
|
||||
Ans104DataItem parsed = new Ans104DataItem(raw);
|
||||
assertEquals(2, parsed.signatureType());
|
||||
assertArrayEquals(owner, parsed.owner32());
|
||||
assertArrayEquals(data, parsed.data());
|
||||
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);
|
||||
}
|
||||
|
||||
@Test
|
||||
void mutationBreaksSignature() {
|
||||
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(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;
|
||||
assertFalse(new Ans104DataItem(raw).verifySignature());
|
||||
}
|
||||
@Test
|
||||
void signingMessageMatchesArbundlesTurboCompatibilityVector() {
|
||||
byte[] owner = new byte[32];
|
||||
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(ShineProtocolTags.APP_TAG_NAME, ShineProtocolTags.APP_TAG_VALUE),
|
||||
new Ans104DataItem.Tag(ShineProtocolTags.CHANNEL_TAG_NAME, "books")
|
||||
);
|
||||
|
||||
byte[] actual = Ans104DataItem.buildSigningMessage(owner, tags, data);
|
||||
byte[] expected = hex("1abe0371d12268b34be32ca5ebf3d3d2189f9d311d9004c142538ca8a4312faa329d1a4873df72dd01b632a2ecce6b87");
|
||||
assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
private static byte[] hex(String value) {
|
||||
if ((value.length() & 1) != 0) throw new IllegalArgumentException("odd hex length");
|
||||
byte[] out = new byte[value.length() / 2];
|
||||
for (int i = 0; i < out.length; i++) {
|
||||
int hi = Character.digit(value.charAt(i * 2), 16);
|
||||
int lo = Character.digit(value.charAt(i * 2 + 1), 16);
|
||||
if (hi < 0 || lo < 0) throw new IllegalArgumentException("bad hex");
|
||||
out[i] = (byte) ((hi << 4) | lo);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package blockchain;
|
||||
|
||||
import blockchain.body.ForkBody;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
final class ForkBodyTest {
|
||||
|
||||
@Test
|
||||
void roundTripThroughBodyRecordParser() {
|
||||
byte[] parentKey = filled(32, 0x11);
|
||||
byte[] forkHash = filled(32, 0x22);
|
||||
byte[] tipHash = filled(32, 0x33);
|
||||
|
||||
ForkBody source = new ForkBody(
|
||||
0,
|
||||
77,
|
||||
filled(32, 0x44),
|
||||
5,
|
||||
parentKey,
|
||||
120,
|
||||
forkHash,
|
||||
1_700_000_000_000L,
|
||||
137,
|
||||
tipHash,
|
||||
1_700_100_000_000L,
|
||||
17,
|
||||
ForkBody.REASON_CONFIRMED_COMPROMISE_ROLLBACK,
|
||||
"Удаляю неизвестные записи"
|
||||
).check();
|
||||
|
||||
ForkBody parsed = assertInstanceOf(
|
||||
ForkBody.class,
|
||||
BodyRecordParser.parse(ForkBody.TYPE, ForkBody.SUBTYPE, ForkBody.VER, source.toBytes())
|
||||
);
|
||||
|
||||
assertEquals(0, parsed.lineCode);
|
||||
assertEquals(77, parsed.prevLineNumber);
|
||||
assertEquals(5, parsed.thisLineNumber);
|
||||
assertArrayEquals(parentKey, parsed.parentBlockchainKey32);
|
||||
assertEquals(120, parsed.forkPointBlockNumber);
|
||||
assertArrayEquals(forkHash, parsed.forkPointBlockHash32);
|
||||
assertEquals(137, parsed.parentTipBlockNumber);
|
||||
assertArrayEquals(tipHash, parsed.parentTipBlockHash32);
|
||||
assertEquals(17, parsed.discardedBlocksCount);
|
||||
assertEquals(ForkBody.REASON_CONFIRMED_COMPROMISE_ROLLBACK, parsed.reasonCode);
|
||||
assertEquals("Удаляю неизвестные записи", parsed.comment);
|
||||
}
|
||||
|
||||
@Test
|
||||
void routineRotationMayDiscardNothing() {
|
||||
ForkBody body = new ForkBody(
|
||||
0,
|
||||
0,
|
||||
filled(32, 9),
|
||||
1,
|
||||
filled(32, 1),
|
||||
10,
|
||||
filled(32, 2),
|
||||
1000,
|
||||
10,
|
||||
filled(32, 2),
|
||||
1000,
|
||||
0,
|
||||
ForkBody.REASON_ROUTINE_ROTATION,
|
||||
""
|
||||
);
|
||||
assertDoesNotThrow(body::check);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectedWhenDiscardCountDoesNotMatchForkPointAndTip() {
|
||||
ForkBody body = new ForkBody(
|
||||
0,
|
||||
0,
|
||||
filled(32, 9),
|
||||
1,
|
||||
filled(32, 1),
|
||||
10,
|
||||
filled(32, 2),
|
||||
1000,
|
||||
12,
|
||||
filled(32, 3),
|
||||
2000,
|
||||
99,
|
||||
ForkBody.REASON_POSSIBLE_COMPROMISE,
|
||||
""
|
||||
);
|
||||
assertThrows(IllegalArgumentException.class, body::check);
|
||||
}
|
||||
|
||||
private static byte[] filled(int size, int value) {
|
||||
byte[] out = new byte[size];
|
||||
Arrays.fill(out, (byte) value);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
package blockchain;
|
||||
|
||||
import blockchain.body.ConnectionBody;
|
||||
import blockchain.body.HeaderBody;
|
||||
import blockchain.body.ReactionBody;
|
||||
import blockchain.body.StatusActionBody;
|
||||
import blockchain.body.TextLineBody;
|
||||
import blockchain.body.TextRatingBody;
|
||||
import blockchain.body.TextReplyBody;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
final class TargetLoginFormatTest {
|
||||
|
||||
private static final byte[] HASH = hash(7);
|
||||
private static final byte[] INITIAL_KEY = hash(11);
|
||||
|
||||
@Test
|
||||
void externalTargetRoundTripKeepsHistoricalForkMetadata() {
|
||||
ReactionBody source = new ReactionBody(MsgSubType.REACTION_LIKE, "alice", 7, 42, HASH);
|
||||
ReactionBody parsed = new ReactionBody(MsgSubType.REACTION_LIKE, ReactionBody.VER, source.toBytes()).check();
|
||||
|
||||
assertEquals("alice", parsed.toLogin());
|
||||
assertEquals(7, parsed.toForkNumber());
|
||||
assertEquals(42, parsed.toBlockGlobalNumber());
|
||||
assertArrayEquals(HASH, parsed.toBlockHashBytes());
|
||||
assertFalse(new String(source.toBytes()).contains("alice-007"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void replyIsExternalButEditReplyIsCompactOwnChainTarget() {
|
||||
TextReplyBody reply = new TextReplyBody(MsgSubType.TEXT_REPLY, "alice", 12, 11, HASH, "reply");
|
||||
TextReplyBody parsedReply = new TextReplyBody(MsgSubType.TEXT_REPLY, TextReplyBody.VER, reply.toBytes()).check();
|
||||
assertEquals("alice", parsedReply.toLogin());
|
||||
assertEquals(12, parsedReply.toForkNumber());
|
||||
|
||||
TextReplyBody edit = new TextReplyBody(MsgSubType.TEXT_EDIT_REPLY, null, null, 11, HASH, "");
|
||||
TextReplyBody parsedEdit = new TextReplyBody(MsgSubType.TEXT_EDIT_REPLY, TextReplyBody.VER, edit.toBytes()).check();
|
||||
assertNull(parsedEdit.toLogin());
|
||||
assertNull(parsedEdit.toForkNumber());
|
||||
assertEquals(11, parsedEdit.toBlockGlobalNumber());
|
||||
assertArrayEquals(HASH, parsedEdit.toBlockHashBytes());
|
||||
assertTrue(edit.toBytes().length < reply.toBytes().length);
|
||||
}
|
||||
|
||||
@Test
|
||||
void editPostIsCompactAndConnectionUsesRealExternalTarget() {
|
||||
TextLineBody editPost = new TextLineBody(
|
||||
0, -1, new byte[32], -1,
|
||||
MsgSubType.TEXT_EDIT_POST,
|
||||
null, null, 5, HASH, "edited"
|
||||
);
|
||||
TextLineBody parsedEditPost = new TextLineBody(MsgSubType.TEXT_EDIT_POST, TextLineBody.VER, editPost.toBytes()).check();
|
||||
assertNull(parsedEditPost.toLogin());
|
||||
assertNull(parsedEditPost.toForkNumber());
|
||||
assertEquals(5, parsedEditPost.toBlockGlobalNumber());
|
||||
|
||||
ConnectionBody connection = new ConnectionBody(
|
||||
0, HASH, 1,
|
||||
MsgSubType.CONNECTION_FOLLOW,
|
||||
"alice", 3, 0, HASH
|
||||
);
|
||||
ConnectionBody parsedConnection = new ConnectionBody(MsgSubType.CONNECTION_FOLLOW, ConnectionBody.VER, connection.toBytes()).check();
|
||||
assertEquals("alice", parsedConnection.toLogin());
|
||||
assertEquals(3, parsedConnection.toForkNumber());
|
||||
assertEquals(0, parsedConnection.toBlockGlobalNumber());
|
||||
assertArrayEquals(HASH, parsedConnection.toBlockHashBytes());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ratingAndStatusCarryHistoricalForkNumber() {
|
||||
TextRatingBody rating = new TextRatingBody("alice", 9, 70, HASH, "5");
|
||||
TextRatingBody parsedRating = new TextRatingBody(MsgSubType.TEXT_RATING, TextRatingBody.VER, rating.toBytes()).check();
|
||||
assertEquals(9, parsedRating.toForkNumber());
|
||||
|
||||
StatusActionBody status = new StatusActionBody(MsgSubType.STATUS_STARTED, "alice", 9, 77, HASH, "");
|
||||
StatusActionBody parsedStatus = new StatusActionBody(MsgSubType.STATUS_STARTED, StatusActionBody.VER, status.toBytes()).check();
|
||||
assertEquals("alice", parsedStatus.toLogin());
|
||||
assertEquals(9, parsedStatus.toForkNumber());
|
||||
assertEquals(77, parsedStatus.toBlockGlobalNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
void forkZeroIsReservedButStillDecodableAsHistoricalUnknown() {
|
||||
ReactionBody source = new ReactionBody(MsgSubType.REACTION_LIKE, "alice", 0, 42, HASH);
|
||||
ReactionBody parsed = new ReactionBody(MsgSubType.REACTION_LIKE, ReactionBody.VER, source.toBytes()).check();
|
||||
assertEquals(0, parsed.toForkNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
void headerCarriesInitialBlockchainKey() {
|
||||
HeaderBody source = new HeaderBody("alice", INITIAL_KEY);
|
||||
HeaderBody parsed = new HeaderBody(HeaderBody.SUBTYPE_COMPAT, HeaderBody.VER, source.toBytes()).check();
|
||||
assertEquals("alice", parsed.login);
|
||||
assertArrayEquals(INITIAL_KEY, parsed.initialBlockchainKey32);
|
||||
}
|
||||
|
||||
@Test
|
||||
void namespacedHeaderRoundTripAcceptsPaidAndFreeUsers() {
|
||||
for (String login : new String[]{"alice@free", "AlexSmith@shine", "a_B@free", "abcdefghijklmnopqrstuvwxy@free"}) {
|
||||
HeaderBody source = new HeaderBody(login, INITIAL_KEY);
|
||||
HeaderBody parsed = new HeaderBody(HeaderBody.SUBTYPE_COMPAT, HeaderBody.VER, source.toBytes()).check();
|
||||
assertEquals(login, parsed.login);
|
||||
assertArrayEquals(INITIAL_KEY, parsed.initialBlockchainKey32);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void namespacedHeaderRejectsMalformedOrOversizedLogin() {
|
||||
for (String login : new String[]{"alice@", "@shine", "alice@@free", "alice@FREE", "alice.free@shine", "abcdefghijklmnopqrstuvwxyz@free"}) {
|
||||
assertThrows(IllegalArgumentException.class, () -> new HeaderBody(login, INITIAL_KEY), login);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void signedReferencesPreserveArbitraryLoginCaseWithoutServerSideCaseEnforcement() {
|
||||
// Адресация не зависит от регистра, но блок сохраняет выбранные автором байты.
|
||||
for (String login : new String[]{"AlexSmith@shine", "alexsmith@shine"}) {
|
||||
TextReplyBody reply = new TextReplyBody(MsgSubType.TEXT_REPLY, login, 1, 42, HASH, "тест");
|
||||
assertEquals(login, new TextReplyBody(MsgSubType.TEXT_REPLY, TextReplyBody.VER, reply.toBytes()).check().toLogin());
|
||||
|
||||
ReactionBody like = new ReactionBody(MsgSubType.REACTION_LIKE, login, 1, 42, HASH);
|
||||
assertEquals(login, new ReactionBody(MsgSubType.REACTION_LIKE, ReactionBody.VER, like.toBytes()).check().toLogin());
|
||||
|
||||
ConnectionBody follow = new ConnectionBody(0, HASH, 1, MsgSubType.CONNECTION_FOLLOW, login, 1, 0, HASH);
|
||||
assertEquals(login, new ConnectionBody(MsgSubType.CONNECTION_FOLLOW, ConnectionBody.VER, follow.toBytes()).check().toLogin());
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] hash(int seed) {
|
||||
byte[] out = new byte[32];
|
||||
Arrays.fill(out, (byte) seed);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -20,8 +20,8 @@ public final class ShineSignatureConstants {
|
||||
/** Подписанный watermark чтения канала: prefix + login + owner_bch_name + channel_name + time_ms + read_count */
|
||||
public static final String CHANNEL_READ_STATE_PREFIX = "SHiNe/ChannelReadState:";
|
||||
|
||||
/** TAG в HeaderBody (genesis). ASCII "SHiNe". */
|
||||
public static final String BLOCKCHAIN_HEADER_TAG = "SHiNe";
|
||||
/** TAG в HeaderBody (genesis). ASCII "SHiNE". */
|
||||
public static final String BLOCKCHAIN_HEADER_TAG = "SHiNE";
|
||||
|
||||
/** DOMAIN для preimage при расчёте hash32 блока. ASCII "SHiNe". */
|
||||
public static final String BLOCK_HASH_DOMAIN = "SHiNe";
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# utils.crypto
|
||||
|
||||
Пакет отвечает за криптографию — подписи и хэши блоков.
|
||||
Используется при создании и проверке целостности `.bch`-блоков.
|
||||
Используется при создании и проверке криптографических сущностей SHiNE; пользовательские blockchain-файлы `.bch` больше не используются.
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -17,52 +17,29 @@ import java.util.List;
|
||||
public final class DatabaseInitializer {
|
||||
|
||||
public static final String DB_SCHEMA_VERSION_TABLE = "db_schema_version";
|
||||
public static final String DB_FORMAT_MARKER_TABLE = "shine_database_metadata";
|
||||
public static final String DB_FORMAT_MARKER = "SHiNE_DB_V2";
|
||||
public static final int DB_FORMAT_VERSION = 2;
|
||||
public static final int SCHEMA_VERSION_1 = 1;
|
||||
public static final int SCHEMA_VERSION_2 = 2;
|
||||
public static final int SCHEMA_VERSION_3 = 3;
|
||||
public static final int SCHEMA_VERSION_4 = 4;
|
||||
public static final int SCHEMA_VERSION_5 = 5;
|
||||
public static final int SCHEMA_VERSION_6 = 6;
|
||||
public static final int SCHEMA_VERSION_7 = 7;
|
||||
public static final int SCHEMA_VERSION_8 = 8;
|
||||
public static final int SCHEMA_VERSION_9 = 9;
|
||||
public static final int SCHEMA_VERSION_10 = 10;
|
||||
public static final int SCHEMA_VERSION_11 = 11;
|
||||
public static final int SCHEMA_VERSION_12 = 12;
|
||||
public static final int SCHEMA_VERSION_13 = 13;
|
||||
public static final int SCHEMA_VERSION_14 = 14;
|
||||
public static final int SCHEMA_VERSION_15 = 15;
|
||||
public static final int SCHEMA_VERSION_16 = 16;
|
||||
public static final int SCHEMA_VERSION_17 = 17;
|
||||
public static final int SCHEMA_VERSION_18 = 18;
|
||||
public static final int SCHEMA_VERSION_19 = 19;
|
||||
public static final int SCHEMA_VERSION_20 = 20;
|
||||
public static final int SCHEMA_VERSION_21 = 21;
|
||||
public static final int SCHEMA_VERSION_22 = 22;
|
||||
public static final int SCHEMA_VERSION_23 = 23;
|
||||
public static final int SCHEMA_VERSION_31 = 31;
|
||||
public static final int SCHEMA_VERSION_32 = 32;
|
||||
public static final int SCHEMA_VERSION_33 = 33;
|
||||
public static final int SCHEMA_VERSION_34 = 34;
|
||||
public static final int SCHEMA_VERSION_35 = 35;
|
||||
public static final int SCHEMA_VERSION_36 = 36;
|
||||
public static final int SCHEMA_VERSION_37 = 37;
|
||||
public static final int SCHEMA_VERSION_38 = 38;
|
||||
public static final int SCHEMA_VERSION_39 = 39;
|
||||
public static final String POSTGRES_SCHEMA_RESOURCE = "postgres/schema_v1.sql";
|
||||
public static final String POSTGRES_MIGRATION_V2_RESOURCE = "postgres/migration_v2.sql";
|
||||
public static final String POSTGRES_MIGRATION_V3_RESOURCE = "postgres/migration_v3.sql";
|
||||
public static final String POSTGRES_MIGRATION_V4_RESOURCE = "postgres/migration_v4.sql";
|
||||
public static final String POSTGRES_MIGRATION_V5_RESOURCE = "postgres/migration_v5.sql";
|
||||
public static final String POSTGRES_MIGRATION_V6_RESOURCE = "postgres/migration_v6.sql";
|
||||
public static final String POSTGRES_MIGRATION_V7_RESOURCE = "postgres/migration_v7.sql";
|
||||
public static final String POSTGRES_MIGRATION_V8_RESOURCE = "postgres/migration_v8.sql";
|
||||
public static final String POSTGRES_MIGRATION_V9_RESOURCE = "postgres/migration_v9.sql";
|
||||
public static final String POSTGRES_MIGRATION_V10_RESOURCE = "postgres/migration_v10.sql";
|
||||
public static final String POSTGRES_MIGRATION_V11_RESOURCE = "postgres/migration_v11.sql";
|
||||
public static final String POSTGRES_MIGRATION_V12_RESOURCE = "postgres/migration_v12.sql";
|
||||
public static final String POSTGRES_MIGRATION_V13_RESOURCE = "postgres/migration_v13.sql";
|
||||
public static final String POSTGRES_MIGRATION_V14_RESOURCE = "postgres/migration_v14.sql";
|
||||
public static final String POSTGRES_MIGRATION_V15_RESOURCE = "postgres/migration_v15.sql";
|
||||
public static final String POSTGRES_MIGRATION_V16_RESOURCE = "postgres/migration_v16.sql";
|
||||
public static final String POSTGRES_MIGRATION_V17_RESOURCE = "postgres/migration_v17.sql";
|
||||
public static final String POSTGRES_MIGRATION_V18_RESOURCE = "postgres/migration_v18.sql";
|
||||
public static final String POSTGRES_MIGRATION_V19_RESOURCE = "postgres/migration_v19.sql";
|
||||
public static final String POSTGRES_MIGRATION_V20_RESOURCE = "postgres/migration_v20.sql";
|
||||
public static final String POSTGRES_MIGRATION_V21_RESOURCE = "postgres/migration_v21.sql";
|
||||
public static final String POSTGRES_MIGRATION_V22_RESOURCE = "postgres/migration_v22.sql";
|
||||
public static final String POSTGRES_MIGRATION_V23_RESOURCE = "postgres/migration_v23.sql";
|
||||
public static final String POSTGRES_MIGRATION_V31_RESOURCE = "postgres/migration_v31.sql";
|
||||
public static final String POSTGRES_MIGRATION_V32_RESOURCE = "postgres/migration_v32.sql";
|
||||
public static final String POSTGRES_MIGRATION_V33_RESOURCE = "postgres/migration_v33.sql";
|
||||
public static final String POSTGRES_MIGRATION_V34_RESOURCE = "postgres/migration_v34.sql";
|
||||
public static final String POSTGRES_MIGRATION_V35_RESOURCE = "postgres/migration_v35.sql";
|
||||
public static final String POSTGRES_MIGRATION_V36_RESOURCE = "postgres/migration_v36.sql";
|
||||
public static final String POSTGRES_MIGRATION_V37_RESOURCE = "postgres/migration_v37.sql";
|
||||
public static final String POSTGRES_MIGRATION_V38_RESOURCE = "postgres/migration_v38.sql";
|
||||
public static final String POSTGRES_MIGRATION_V39_RESOURCE = "postgres/migration_v39.sql";
|
||||
|
||||
private DatabaseInitializer() {}
|
||||
|
||||
@@ -73,6 +50,7 @@ public final class DatabaseInitializer {
|
||||
public static final short TEXT_RATING = 30;
|
||||
public static final short TEXT_REPOST = 50;
|
||||
public static final short TEXT_CHANNEL_META = 90;
|
||||
public static final short TEXT_CHANNEL_DELETE = 92;
|
||||
public static final short TEXT_ENTRYPOINT = 100;
|
||||
public static final short TEXT_EXERCISE = 110;
|
||||
public static final short TEXT_SERVICE = 120;
|
||||
@@ -131,99 +109,69 @@ public final class DatabaseInitializer {
|
||||
}
|
||||
|
||||
try (Connection conn = openConnection(jdbcUrl, user, password)) {
|
||||
if (!postgresSchemaVersionTableExists(conn)) {
|
||||
boolean markerTableExists = postgresTableExists(conn, DB_FORMAT_MARKER_TABLE);
|
||||
boolean schemaVersionTableExists = postgresTableExists(conn, DB_SCHEMA_VERSION_TABLE);
|
||||
|
||||
if (!markerTableExists) {
|
||||
if (schemaVersionTableExists || currentSchemaHasBaseTables(conn)) {
|
||||
throw new SQLException(
|
||||
"Legacy/non-v2 SHiNE PostgreSQL database detected. "
|
||||
+ "Stage 2 requires an empty database initialized as " + DB_FORMAT_MARKER
|
||||
+ "; automatic migration from the old database is intentionally disabled.");
|
||||
}
|
||||
runSqlScript(conn, POSTGRES_SCHEMA_RESOURCE);
|
||||
return;
|
||||
markerTableExists = postgresTableExists(conn, DB_FORMAT_MARKER_TABLE);
|
||||
schemaVersionTableExists = postgresTableExists(conn, DB_SCHEMA_VERSION_TABLE);
|
||||
}
|
||||
|
||||
if (!markerTableExists || !schemaVersionTableExists) {
|
||||
throw new SQLException(
|
||||
"Invalid SHiNE PostgreSQL database: required v2 metadata/schema tables are missing.");
|
||||
}
|
||||
|
||||
validateDatabaseFormatV2(conn);
|
||||
int currentVersion = readCurrentSchemaVersion(conn);
|
||||
if (currentVersion < SCHEMA_VERSION_2) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V2_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_2;
|
||||
if (currentVersion == 30) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V31_RESOURCE);
|
||||
currentVersion = readCurrentSchemaVersion(conn);
|
||||
}
|
||||
if (currentVersion < SCHEMA_VERSION_3) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V3_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_3;
|
||||
if (currentVersion == SCHEMA_VERSION_31) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V32_RESOURCE);
|
||||
currentVersion = readCurrentSchemaVersion(conn);
|
||||
}
|
||||
if (currentVersion < SCHEMA_VERSION_4) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V4_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_4;
|
||||
if (currentVersion == SCHEMA_VERSION_32) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V33_RESOURCE);
|
||||
currentVersion = readCurrentSchemaVersion(conn);
|
||||
}
|
||||
if (currentVersion < SCHEMA_VERSION_5) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V5_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_5;
|
||||
if (currentVersion == SCHEMA_VERSION_33) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V34_RESOURCE);
|
||||
currentVersion = readCurrentSchemaVersion(conn);
|
||||
}
|
||||
if (currentVersion < SCHEMA_VERSION_6) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V6_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_6;
|
||||
if (currentVersion == SCHEMA_VERSION_34) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V35_RESOURCE);
|
||||
currentVersion = readCurrentSchemaVersion(conn);
|
||||
}
|
||||
if (currentVersion < SCHEMA_VERSION_7) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V7_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_7;
|
||||
if (currentVersion == SCHEMA_VERSION_35) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V36_RESOURCE);
|
||||
currentVersion = readCurrentSchemaVersion(conn);
|
||||
}
|
||||
if (currentVersion < SCHEMA_VERSION_8) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V8_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_8;
|
||||
if (currentVersion == SCHEMA_VERSION_36) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V37_RESOURCE);
|
||||
currentVersion = readCurrentSchemaVersion(conn);
|
||||
}
|
||||
if (currentVersion < SCHEMA_VERSION_9) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V9_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_9;
|
||||
if (currentVersion == SCHEMA_VERSION_37) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V38_RESOURCE);
|
||||
currentVersion = readCurrentSchemaVersion(conn);
|
||||
}
|
||||
if (currentVersion < SCHEMA_VERSION_10) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V10_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_10;
|
||||
if (currentVersion == SCHEMA_VERSION_38) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V39_RESOURCE);
|
||||
currentVersion = readCurrentSchemaVersion(conn);
|
||||
}
|
||||
if (currentVersion < SCHEMA_VERSION_11) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V11_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_11;
|
||||
}
|
||||
if (currentVersion < SCHEMA_VERSION_12) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V12_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_12;
|
||||
}
|
||||
if (currentVersion < SCHEMA_VERSION_13) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V13_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_13;
|
||||
}
|
||||
if (currentVersion < SCHEMA_VERSION_14) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V14_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_14;
|
||||
}
|
||||
if (currentVersion < SCHEMA_VERSION_15) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V15_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_15;
|
||||
}
|
||||
if (currentVersion < SCHEMA_VERSION_16) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V16_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_16;
|
||||
}
|
||||
if (currentVersion < SCHEMA_VERSION_17) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V17_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_17;
|
||||
}
|
||||
if (currentVersion < SCHEMA_VERSION_18) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V18_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_18;
|
||||
}
|
||||
if (currentVersion < SCHEMA_VERSION_19) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V19_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_19;
|
||||
}
|
||||
if (currentVersion < SCHEMA_VERSION_20) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V20_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_20;
|
||||
}
|
||||
if (currentVersion < SCHEMA_VERSION_21) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V21_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_21;
|
||||
}
|
||||
if (currentVersion < SCHEMA_VERSION_22) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V22_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_22;
|
||||
}
|
||||
if (currentVersion < SCHEMA_VERSION_23) {
|
||||
runSqlScript(conn, POSTGRES_MIGRATION_V23_RESOURCE);
|
||||
currentVersion = SCHEMA_VERSION_23;
|
||||
if (currentVersion != SCHEMA_VERSION_39) {
|
||||
throw new SQLException(
|
||||
"Unsupported SHiNE DB v2 schema version: expected 39, found " + currentVersion);
|
||||
}
|
||||
normalizeBlockchainStateKeysToBase64(conn);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,23 +182,60 @@ public final class DatabaseInitializer {
|
||||
return DriverManager.getConnection(jdbcUrl, user, password == null ? "" : password);
|
||||
}
|
||||
|
||||
private static boolean postgresSchemaVersionTableExists(Connection conn) throws SQLException {
|
||||
private static boolean postgresTableExists(Connection conn, String tableName) throws SQLException {
|
||||
String sql = """
|
||||
SELECT EXISTS (
|
||||
SELECT 1
|
||||
FROM information_schema.tables
|
||||
WHERE table_schema = current_schema()
|
||||
AND table_name = ?
|
||||
AND table_type = 'BASE TABLE'
|
||||
)
|
||||
""";
|
||||
try (var ps = conn.prepareStatement(sql)) {
|
||||
ps.setString(1, DB_SCHEMA_VERSION_TABLE);
|
||||
ps.setString(1, tableName);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
return rs.next() && rs.getBoolean(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean currentSchemaHasBaseTables(Connection conn) throws SQLException {
|
||||
String sql = """
|
||||
SELECT EXISTS (
|
||||
SELECT 1
|
||||
FROM information_schema.tables
|
||||
WHERE table_schema = current_schema()
|
||||
AND table_type = 'BASE TABLE'
|
||||
)
|
||||
""";
|
||||
try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql)) {
|
||||
return rs.next() && rs.getBoolean(1);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateDatabaseFormatV2(Connection conn) throws SQLException {
|
||||
String sql = """
|
||||
SELECT database_format, database_generation
|
||||
FROM shine_database_metadata
|
||||
WHERE id = 1
|
||||
""";
|
||||
try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql)) {
|
||||
if (!rs.next()) {
|
||||
throw new SQLException(
|
||||
"Invalid SHiNE PostgreSQL database: SHiNE_DB_V2 metadata row is missing.");
|
||||
}
|
||||
String format = rs.getString("database_format");
|
||||
int generation = rs.getInt("database_generation");
|
||||
if (!DB_FORMAT_MARKER.equals(format) || generation != DB_FORMAT_VERSION) {
|
||||
throw new SQLException(
|
||||
"Unsupported SHiNE PostgreSQL database format: expected " + DB_FORMAT_MARKER
|
||||
+ " generation " + DB_FORMAT_VERSION
|
||||
+ ", found format=" + format + ", generation=" + generation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int readCurrentSchemaVersion(Connection conn) throws SQLException {
|
||||
String sql = """
|
||||
SELECT schema_version
|
||||
@@ -266,6 +251,38 @@ public final class DatabaseInitializer {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* blockchain_state — runtime-проекция: публичный blockchain key хранится
|
||||
* строго как Base64(32). Ранний импорт free-регистраций ошибочно сохранял
|
||||
* Base58, поэтому нормализация выполняется идемпотентно при старте.
|
||||
*/
|
||||
private static void normalizeBlockchainStateKeysToBase64(Connection conn) throws SQLException {
|
||||
record StateKey(String login, int forkNumber, String key) {}
|
||||
List<StateKey> entries = new ArrayList<>();
|
||||
try (Statement st = conn.createStatement();
|
||||
ResultSet rs = st.executeQuery("SELECT login, fork_number, blockchain_key FROM blockchain_state_store")) {
|
||||
while (rs.next()) {
|
||||
entries.add(new StateKey(rs.getString("login"), rs.getInt("fork_number"), rs.getString("blockchain_key")));
|
||||
}
|
||||
}
|
||||
|
||||
try (var update = conn.prepareStatement("""
|
||||
UPDATE blockchain_state_store
|
||||
SET blockchain_key = ?
|
||||
WHERE login = ? AND fork_number = ?
|
||||
""")) {
|
||||
for (StateKey entry : entries) {
|
||||
String normalized = KeyEncodingUtil.normalizeKeyToBase64_32(entry.key());
|
||||
if (normalized == null || normalized.equals(entry.key())) continue;
|
||||
update.setString(1, normalized);
|
||||
update.setString(2, entry.login());
|
||||
update.setInt(3, entry.forkNumber());
|
||||
update.addBatch();
|
||||
}
|
||||
update.executeBatch();
|
||||
}
|
||||
}
|
||||
|
||||
private static void runSqlScript(Connection conn, String resourcePath) throws SQLException {
|
||||
String sqlScript = loadClasspathResource(resourcePath);
|
||||
List<String> statements = splitSqlStatements(sqlScript);
|
||||
|
||||
@@ -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++) {
|
||||
|
||||
@@ -4,7 +4,7 @@ package shine.db;
|
||||
* MsgSubType — единое место для ВСЕХ subType сообщений (msg_sub_type).
|
||||
*
|
||||
* ВАЖНО:
|
||||
* - Значения должны совпадать с body-классами (TextBody/ReactionBody/ConnectionBody/UserParamBody/HeaderBody).
|
||||
* - Значения должны совпадать с body-классами (TextLineBody/TextReplyBody/TextRatingBody/ReactionBody/ConnectionBody/UserParamBody/HeaderBody).
|
||||
* - После релиза менять числа нельзя (иначе ломается совместимость данных).
|
||||
*/
|
||||
public final class MsgSubType {
|
||||
@@ -38,6 +38,7 @@ public final class MsgSubType {
|
||||
|
||||
/** CHANNEL_META — скрытый технический снимок профиля канала. */
|
||||
public static final short TEXT_CHANNEL_META = 90;
|
||||
public static final short TEXT_CHANNEL_DELETE = 92;
|
||||
|
||||
/** ENTRYPOINT — входная страница канала (line-based). */
|
||||
public static final short TEXT_ENTRYPOINT = 100;
|
||||
|
||||
@@ -28,7 +28,7 @@ public final class PostgresDbController {
|
||||
try {
|
||||
DatabaseInitializer.ensurePostgresSchemaInitialized(jdbcUrl, dbUser, dbPassword);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("PostgreSQL schema auto-init failed", e);
|
||||
throw new RuntimeException("PostgreSQL database compatibility/init failed: " + e.getMessage(), e);
|
||||
}
|
||||
|
||||
this.delegate = new DriverManagerDbProvider(jdbcUrl, dbUser, dbPassword, connection -> {
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
package shine.db.archive;
|
||||
|
||||
/** Ссылка на уже успешно опубликованный большой архивный блок. */
|
||||
public record ArchiveBigBlockRef(
|
||||
long bigBlockNumber,
|
||||
byte[] archiveHash,
|
||||
byte[] arweaveTxId
|
||||
) {}
|
||||
-14
@@ -1,14 +0,0 @@
|
||||
package shine.db.archive;
|
||||
|
||||
/** Последний известный архивный chunk конкретной blockchain на этом сервере. */
|
||||
public record ArchiveBlockchainLocation(
|
||||
String blockchainName,
|
||||
String publisherLogin,
|
||||
String arweaveTxId,
|
||||
byte[] archiveHash,
|
||||
long bigBlockNumber,
|
||||
long chunkOffset,
|
||||
long chunkSize,
|
||||
long sourceLastBlockNumber,
|
||||
long updatedAtMs
|
||||
) {}
|
||||
@@ -1,15 +0,0 @@
|
||||
package shine.db.archive;
|
||||
|
||||
/**
|
||||
* Последнее окончательно опубликованное архивное положение одной SHiNE blockchain.
|
||||
*/
|
||||
public record ArchiveChainCursor(
|
||||
String blockchainName,
|
||||
long lastArchivedSourceBlockNumber,
|
||||
byte[] lastArchivedSourceBlockHash,
|
||||
long lastArchiveBigBlockNumber,
|
||||
byte[] lastArchiveBigBlockHash,
|
||||
long lastChunkOffset,
|
||||
long lastChunkSize,
|
||||
long updatedAtMs
|
||||
) {}
|
||||
@@ -1,16 +0,0 @@
|
||||
package shine.db.archive;
|
||||
|
||||
/** Crash-safe состояние публикации одного большого SHINE-ARCHIVE блока. */
|
||||
public record ArchivePublishJob(
|
||||
long id,
|
||||
long bigBlockNumber,
|
||||
String status,
|
||||
long createdAtMs,
|
||||
String localArchivePath,
|
||||
byte[] archiveHash,
|
||||
byte[] arweaveTxId,
|
||||
Integer arweaveConfirmations,
|
||||
String solanaSignature,
|
||||
String errorText,
|
||||
long updatedAtMs
|
||||
) {}
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
package shine.db.archive;
|
||||
|
||||
/** Замороженный диапазон одной blockchain внутри конкретной архивной публикации. */
|
||||
public record ArchivePublishJobChain(
|
||||
long jobId,
|
||||
String blockchainName,
|
||||
long fromSourceBlockNumber,
|
||||
long toSourceBlockNumber,
|
||||
byte[] previousSourceBlockHash,
|
||||
byte[] lastSourceBlockHash,
|
||||
Long previousArchiveBigBlockNumber,
|
||||
byte[] previousArchiveBigBlockHash,
|
||||
Long previousChunkOffset,
|
||||
Long previousChunkSize,
|
||||
Long newChunkOffset,
|
||||
Long newChunkSize
|
||||
) {}
|
||||
@@ -1,14 +0,0 @@
|
||||
package shine.db.archive;
|
||||
|
||||
/**
|
||||
* Текущая archive head trusted publisher-а из локальной таблицы,
|
||||
* которую уже заполнил обычный Solana users sync.
|
||||
*/
|
||||
public record ArchivePublisherHead(
|
||||
String login,
|
||||
String rootKey,
|
||||
String archiveHeadTxId,
|
||||
String archiveHeadHash,
|
||||
boolean archiveImported,
|
||||
String lastImportedArchiveTxId
|
||||
) {}
|
||||
@@ -269,6 +269,57 @@ public final class ActiveSessionsDAO {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Удаляет все сессии пользователя, кроме указанной текущей, и возвращает их sessionId.
|
||||
* Вариант с Connection используется внутри транзакции запуска key rotation.
|
||||
*/
|
||||
public List<String> deleteOtherSessions(Connection c, String login, String keepSessionId) throws SQLException {
|
||||
List<String> sessionIds = new ArrayList<>();
|
||||
String selectSql = """
|
||||
SELECT session_id
|
||||
FROM active_sessions
|
||||
WHERE LOWER(login) = LOWER(?)
|
||||
AND session_id <> ?
|
||||
""";
|
||||
try (PreparedStatement ps = c.prepareStatement(selectSql)) {
|
||||
ps.setString(1, login);
|
||||
ps.setString(2, keepSessionId == null ? "" : keepSessionId);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) sessionIds.add(rs.getString(1));
|
||||
}
|
||||
}
|
||||
if (sessionIds.isEmpty()) return sessionIds;
|
||||
|
||||
String deleteSql = """
|
||||
DELETE FROM active_sessions
|
||||
WHERE LOWER(login) = LOWER(?)
|
||||
AND session_id <> ?
|
||||
""";
|
||||
try (PreparedStatement ps = c.prepareStatement(deleteSql)) {
|
||||
ps.setString(1, login);
|
||||
ps.setString(2, keepSessionId == null ? "" : keepSessionId);
|
||||
ps.executeUpdate();
|
||||
}
|
||||
return sessionIds;
|
||||
}
|
||||
|
||||
public List<String> deleteOtherSessions(String login, String keepSessionId) throws SQLException {
|
||||
try (Connection c = db.getConnection()) {
|
||||
boolean oldAutoCommit = c.getAutoCommit();
|
||||
c.setAutoCommit(false);
|
||||
try {
|
||||
List<String> ids = deleteOtherSessions(c, login, keepSessionId);
|
||||
c.commit();
|
||||
return ids;
|
||||
} catch (SQLException e) {
|
||||
c.rollback();
|
||||
throw e;
|
||||
} finally {
|
||||
c.setAutoCommit(oldAutoCommit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------- MAPPER --------------------
|
||||
|
||||
private ActiveSessionEntry mapRow(ResultSet rs) throws SQLException {
|
||||
|
||||
@@ -1,150 +0,0 @@
|
||||
package shine.db.dao;
|
||||
|
||||
import shine.db.DbController;
|
||||
import shine.db.archive.ArchiveBlockchainLocation;
|
||||
import shine.db.archive.ArchivePublisherHead;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
|
||||
/** БД индекс последних archive chunks и локальное состояние импорта archive head. */
|
||||
public final class ArchiveImportDAO {
|
||||
private static volatile ArchiveImportDAO instance;
|
||||
private final DbController db = DbController.getInstance();
|
||||
|
||||
private ArchiveImportDAO() {}
|
||||
|
||||
public static ArchiveImportDAO getInstance() {
|
||||
if (instance == null) synchronized (ArchiveImportDAO.class) {
|
||||
if (instance == null) instance = new ArchiveImportDAO();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает только доверенных publisher-ов, для которых обычный Solana users sync
|
||||
* уже увидел archive head, но этот head ещё не был полностью импортирован локально.
|
||||
*/
|
||||
public List<ArchivePublisherHead> listPendingPublisherHeads(Set<String> approvedLogins) throws SQLException {
|
||||
if (approvedLogins == null || approvedLogins.isEmpty()) return List.of();
|
||||
List<ArchivePublisherHead> out = new ArrayList<>();
|
||||
String sql = """
|
||||
SELECT login, root_key, archive_head_tx_id, archive_head_hash,
|
||||
archive_imported, archive_last_imported_tx_id
|
||||
FROM solana_user_pda_current
|
||||
WHERE LOWER(login)=LOWER(?)
|
||||
AND is_server=TRUE
|
||||
AND archive_head_tx_id<>''
|
||||
AND archive_head_hash<>''
|
||||
AND archive_imported=FALSE
|
||||
""";
|
||||
try (Connection c = db.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
for (String login : approvedLogins) {
|
||||
ps.setString(1, login);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
out.add(new ArchivePublisherHead(
|
||||
rs.getString("login"),
|
||||
rs.getString("root_key"),
|
||||
rs.getString("archive_head_tx_id"),
|
||||
rs.getString("archive_head_hash"),
|
||||
rs.getBoolean("archive_imported"),
|
||||
rs.getString("archive_last_imported_tx_id")));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Сдвигает локальный курсор импорта внутри FULL archive chain.
|
||||
* Update выполняется только если текущий head всё ещё тот же, с которым работает importer.
|
||||
*/
|
||||
public boolean advanceImportCursor(String publisherLogin, String expectedHeadTxId, String importedTxId) throws SQLException {
|
||||
String sql = """
|
||||
UPDATE solana_user_pda_current
|
||||
SET archive_last_imported_tx_id=?
|
||||
WHERE LOWER(login)=LOWER(?)
|
||||
AND archive_head_tx_id=?
|
||||
""";
|
||||
try (Connection c = db.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
ps.setString(1, importedTxId == null ? "" : importedTxId);
|
||||
ps.setString(2, publisherLogin);
|
||||
ps.setString(3, expectedHeadTxId);
|
||||
return ps.executeUpdate() == 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Помечает именно текущий head как полностью импортированный.
|
||||
* Если Solana users sync успел заменить head на более новый, update не произойдёт.
|
||||
*/
|
||||
public boolean markHeadImported(String publisherLogin, String expectedHeadTxId) throws SQLException {
|
||||
String sql = """
|
||||
UPDATE solana_user_pda_current
|
||||
SET archive_imported=TRUE,
|
||||
archive_last_imported_tx_id=archive_head_tx_id
|
||||
WHERE LOWER(login)=LOWER(?)
|
||||
AND archive_head_tx_id=?
|
||||
""";
|
||||
try (Connection c = db.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
ps.setString(1, publisherLogin);
|
||||
ps.setString(2, expectedHeadTxId);
|
||||
return ps.executeUpdate() == 1;
|
||||
}
|
||||
}
|
||||
|
||||
public void upsertLocation(ArchiveBlockchainLocation e) throws SQLException {
|
||||
String sql="""
|
||||
INSERT INTO archive_blockchain_location(
|
||||
blockchain_name,publisher_login,arweave_tx_id,archive_hash,big_block_number,
|
||||
chunk_offset,chunk_size,source_last_block_number,updated_at_ms)
|
||||
VALUES(?,?,?,?,?,?,?,?,?)
|
||||
ON CONFLICT(blockchain_name) DO UPDATE SET
|
||||
publisher_login=EXCLUDED.publisher_login,arweave_tx_id=EXCLUDED.arweave_tx_id,
|
||||
archive_hash=EXCLUDED.archive_hash,big_block_number=EXCLUDED.big_block_number,
|
||||
chunk_offset=EXCLUDED.chunk_offset,chunk_size=EXCLUDED.chunk_size,
|
||||
source_last_block_number=EXCLUDED.source_last_block_number,updated_at_ms=EXCLUDED.updated_at_ms
|
||||
WHERE EXCLUDED.source_last_block_number >= archive_blockchain_location.source_last_block_number
|
||||
""";
|
||||
try(Connection c=db.getConnection(); PreparedStatement ps=c.prepareStatement(sql)) {
|
||||
ps.setString(1,e.blockchainName()); ps.setString(2,e.publisherLogin()); ps.setString(3,e.arweaveTxId());
|
||||
ps.setBytes(4,e.archiveHash()); ps.setLong(5,e.bigBlockNumber()); ps.setLong(6,e.chunkOffset());
|
||||
ps.setLong(7,e.chunkSize()); ps.setLong(8,e.sourceLastBlockNumber()); ps.setLong(9,e.updatedAtMs()); ps.executeUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
public ArchiveBlockchainLocation getLocation(String blockchainName) throws SQLException {
|
||||
String sql="""
|
||||
SELECT blockchain_name,publisher_login,arweave_tx_id,archive_hash,big_block_number,
|
||||
chunk_offset,chunk_size,source_last_block_number,updated_at_ms
|
||||
FROM archive_blockchain_location WHERE LOWER(blockchain_name)=LOWER(?)
|
||||
""";
|
||||
try(Connection c=db.getConnection(); PreparedStatement ps=c.prepareStatement(sql)) {
|
||||
ps.setString(1,blockchainName);
|
||||
try(ResultSet rs=ps.executeQuery()) {
|
||||
if(!rs.next()) return null;
|
||||
return new ArchiveBlockchainLocation(rs.getString(1),rs.getString(2),rs.getString(3),rs.getBytes(4),
|
||||
rs.getLong(5),rs.getLong(6),rs.getLong(7),rs.getLong(8),rs.getLong(9));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Данные PDA, нужные для создания пустого локального blockchain_state перед первым архивным импортом. */
|
||||
public BlockchainIdentity getBlockchainIdentity(String blockchainName) throws SQLException {
|
||||
String sql="""
|
||||
SELECT login, blockchain_name, blockchain_key, paid_limit_bytes
|
||||
FROM solana_user_pda_current WHERE LOWER(blockchain_name)=LOWER(?) LIMIT 1
|
||||
""";
|
||||
try(Connection c=db.getConnection(); PreparedStatement ps=c.prepareStatement(sql)) {
|
||||
ps.setString(1,blockchainName);
|
||||
try(ResultSet rs=ps.executeQuery()) {
|
||||
if(!rs.next()) return null;
|
||||
return new BlockchainIdentity(rs.getString(1),rs.getString(2),rs.getString(3),rs.getLong(4));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public record BlockchainIdentity(String login,String blockchainName,String blockchainKey,long sizeLimit) {}
|
||||
}
|
||||
@@ -1,472 +0,0 @@
|
||||
package shine.db.dao;
|
||||
|
||||
import shine.db.DbController;
|
||||
import shine.db.archive.ArchiveBigBlockRef;
|
||||
import shine.db.archive.ArchiveChainCursor;
|
||||
import shine.db.archive.ArchivePublishJob;
|
||||
import shine.db.archive.ArchivePublishJobChain;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* DAO архивного publisher-а. Все курсоры считаются подтверждёнными только после Solana finalized.
|
||||
*/
|
||||
public final class ArchivePublicationDAO {
|
||||
private static volatile ArchivePublicationDAO instance;
|
||||
private final DbController db = DbController.getInstance();
|
||||
|
||||
private ArchivePublicationDAO() {}
|
||||
|
||||
public static ArchivePublicationDAO getInstance() {
|
||||
if (instance == null) {
|
||||
synchronized (ArchivePublicationDAO.class) {
|
||||
if (instance == null) instance = new ArchivePublicationDAO();
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public ArchiveChainCursor getCursor(String blockchainName) throws SQLException {
|
||||
try (Connection c = db.getConnection()) {
|
||||
return getCursor(c, blockchainName);
|
||||
}
|
||||
}
|
||||
|
||||
public ArchiveChainCursor getCursor(Connection c, String blockchainName) throws SQLException {
|
||||
String sql = """
|
||||
SELECT blockchain_name, last_archived_source_block_number, last_archived_source_block_hash,
|
||||
last_archive_big_block_number, last_archive_big_block_hash,
|
||||
last_chunk_offset, last_chunk_size, updated_at_ms
|
||||
FROM archive_chain_cursor
|
||||
WHERE blockchain_name = ?
|
||||
""";
|
||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
ps.setString(1, blockchainName);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (!rs.next()) return null;
|
||||
return mapCursor(rs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public long nextBigBlockNumber() throws SQLException {
|
||||
String sql = """
|
||||
SELECT COALESCE(MAX(big_block_number), 0) + 1
|
||||
FROM archive_publish_job
|
||||
WHERE status = 'CURSORS_COMMITTED'
|
||||
""";
|
||||
try (Connection c = db.getConnection();
|
||||
PreparedStatement ps = c.prepareStatement(sql);
|
||||
ResultSet rs = ps.executeQuery()) {
|
||||
rs.next();
|
||||
return rs.getLong(1);
|
||||
}
|
||||
}
|
||||
|
||||
public List<ArchiveBigBlockRef> listFinalizedBigBlocks() throws SQLException {
|
||||
String sql = """
|
||||
SELECT big_block_number, archive_hash, arweave_tx_id
|
||||
FROM archive_publish_job
|
||||
WHERE status = 'CURSORS_COMMITTED'
|
||||
AND archive_hash IS NOT NULL
|
||||
AND arweave_tx_id IS NOT NULL
|
||||
ORDER BY big_block_number ASC
|
||||
""";
|
||||
List<ArchiveBigBlockRef> out = new ArrayList<>();
|
||||
try (Connection c = db.getConnection();
|
||||
PreparedStatement ps = c.prepareStatement(sql);
|
||||
ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
out.add(new ArchiveBigBlockRef(
|
||||
rs.getLong("big_block_number"),
|
||||
rs.getBytes("archive_hash"),
|
||||
rs.getBytes("arweave_tx_id")
|
||||
));
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
public ArchivePublishJob findUnfinishedJob() throws SQLException {
|
||||
String sql = """
|
||||
SELECT id, big_block_number, status, created_at_ms, local_archive_path,
|
||||
archive_hash, arweave_tx_id, arweave_confirmations, solana_signature,
|
||||
error_text, updated_at_ms
|
||||
FROM archive_publish_job
|
||||
WHERE status NOT IN ('CURSORS_COMMITTED', 'FAILED')
|
||||
ORDER BY id ASC
|
||||
LIMIT 1
|
||||
""";
|
||||
try (Connection c = db.getConnection();
|
||||
PreparedStatement ps = c.prepareStatement(sql);
|
||||
ResultSet rs = ps.executeQuery()) {
|
||||
return rs.next() ? mapJob(rs) : null;
|
||||
}
|
||||
}
|
||||
|
||||
public ArchivePublishJob getJob(long id) throws SQLException {
|
||||
String sql = """
|
||||
SELECT id, big_block_number, status, created_at_ms, local_archive_path,
|
||||
archive_hash, arweave_tx_id, arweave_confirmations, solana_signature,
|
||||
error_text, updated_at_ms
|
||||
FROM archive_publish_job
|
||||
WHERE id = ?
|
||||
""";
|
||||
try (Connection c = db.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
ps.setLong(1, id);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
return rs.next() ? mapJob(rs) : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Атомарно создаёт frozen archive job и все его blockchain ranges. */
|
||||
public long createJobWithChains(long bigBlockNumber, long createdAtMs, List<ArchivePublishJobChain> chains) throws SQLException {
|
||||
if (chains == null || chains.isEmpty()) throw new IllegalArgumentException("Archive job без chains не создаётся");
|
||||
try (Connection c = db.getConnection()) {
|
||||
boolean oldAuto = c.getAutoCommit();
|
||||
c.setAutoCommit(false);
|
||||
try {
|
||||
long jobId;
|
||||
try (PreparedStatement ps = c.prepareStatement("""
|
||||
INSERT INTO archive_publish_job(big_block_number, status, created_at_ms, updated_at_ms)
|
||||
VALUES(?, 'SNAPSHOT_CREATED', ?, ?)
|
||||
RETURNING id
|
||||
""")) {
|
||||
ps.setLong(1, bigBlockNumber);
|
||||
ps.setLong(2, createdAtMs);
|
||||
ps.setLong(3, createdAtMs);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (!rs.next()) throw new SQLException("Не удалось получить id archive_publish_job");
|
||||
jobId = rs.getLong(1);
|
||||
}
|
||||
}
|
||||
String sql = """
|
||||
INSERT INTO archive_publish_job_chain(
|
||||
job_id, blockchain_name, from_source_block_number, to_source_block_number,
|
||||
previous_source_block_hash, last_source_block_hash,
|
||||
previous_archive_big_block_number, previous_archive_big_block_hash,
|
||||
previous_chunk_offset, previous_chunk_size, new_chunk_offset, new_chunk_size
|
||||
) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)
|
||||
""";
|
||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
for (ArchivePublishJobChain ch : chains) {
|
||||
bindJobChain(ps, new ArchivePublishJobChain(
|
||||
jobId, ch.blockchainName(), ch.fromSourceBlockNumber(), ch.toSourceBlockNumber(),
|
||||
ch.previousSourceBlockHash(), ch.lastSourceBlockHash(),
|
||||
ch.previousArchiveBigBlockNumber(), ch.previousArchiveBigBlockHash(),
|
||||
ch.previousChunkOffset(), ch.previousChunkSize(), null, null));
|
||||
ps.addBatch();
|
||||
}
|
||||
ps.executeBatch();
|
||||
}
|
||||
c.commit();
|
||||
return jobId;
|
||||
} catch (Exception e) {
|
||||
c.rollback();
|
||||
if (e instanceof SQLException se) throw se;
|
||||
throw new SQLException("Не удалось создать frozen archive job", e);
|
||||
} finally {
|
||||
c.setAutoCommit(oldAuto);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public long createJob(long bigBlockNumber, long createdAtMs) throws SQLException {
|
||||
String sql = """
|
||||
INSERT INTO archive_publish_job(big_block_number, status, created_at_ms, updated_at_ms)
|
||||
VALUES(?, 'SNAPSHOT_CREATED', ?, ?)
|
||||
RETURNING id
|
||||
""";
|
||||
try (Connection c = db.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
ps.setLong(1, bigBlockNumber);
|
||||
ps.setLong(2, createdAtMs);
|
||||
ps.setLong(3, createdAtMs);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (!rs.next()) throw new SQLException("Не удалось получить id archive_publish_job");
|
||||
return rs.getLong(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void insertJobChain(ArchivePublishJobChain e) throws SQLException {
|
||||
String sql = """
|
||||
INSERT INTO archive_publish_job_chain(
|
||||
job_id, blockchain_name, from_source_block_number, to_source_block_number,
|
||||
previous_source_block_hash, last_source_block_hash,
|
||||
previous_archive_big_block_number, previous_archive_big_block_hash,
|
||||
previous_chunk_offset, previous_chunk_size,
|
||||
new_chunk_offset, new_chunk_size
|
||||
) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)
|
||||
""";
|
||||
try (Connection c = db.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
bindJobChain(ps, e);
|
||||
ps.executeUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
public List<ArchivePublishJobChain> listJobChains(long jobId) throws SQLException {
|
||||
String sql = """
|
||||
SELECT job_id, blockchain_name, from_source_block_number, to_source_block_number,
|
||||
previous_source_block_hash, last_source_block_hash,
|
||||
previous_archive_big_block_number, previous_archive_big_block_hash,
|
||||
previous_chunk_offset, previous_chunk_size,
|
||||
new_chunk_offset, new_chunk_size
|
||||
FROM archive_publish_job_chain
|
||||
WHERE job_id = ?
|
||||
ORDER BY blockchain_name ASC
|
||||
""";
|
||||
List<ArchivePublishJobChain> out = new ArrayList<>();
|
||||
try (Connection c = db.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
ps.setLong(1, jobId);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) out.add(mapJobChain(rs));
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
public void updateNewChunkPosition(long jobId, String blockchainName, long offset, long size) throws SQLException {
|
||||
String sql = """
|
||||
UPDATE archive_publish_job_chain
|
||||
SET new_chunk_offset=?, new_chunk_size=?
|
||||
WHERE job_id=? AND blockchain_name=?
|
||||
""";
|
||||
try (Connection c = db.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
ps.setLong(1, offset);
|
||||
ps.setLong(2, size);
|
||||
ps.setLong(3, jobId);
|
||||
ps.setString(4, blockchainName);
|
||||
if (ps.executeUpdate() != 1) throw new SQLException("Не найдена archive_publish_job_chain для " + blockchainName);
|
||||
}
|
||||
}
|
||||
|
||||
public void markFileBuilt(long jobId, String localPath, byte[] hash) throws SQLException {
|
||||
updateJob(jobId, "FILE_BUILT", "local_archive_path=? , archive_hash=?", ps -> {
|
||||
ps.setString(1, localPath);
|
||||
ps.setBytes(2, hash);
|
||||
});
|
||||
}
|
||||
|
||||
public void markArweaveUploaded(long jobId, byte[] txId) throws SQLException {
|
||||
updateJob(jobId, "ARWEAVE_UPLOADED", "arweave_tx_id=?", ps -> ps.setBytes(1, txId));
|
||||
}
|
||||
|
||||
/** Обновляет только локальное имя/путь уже собранного archive-файла. */
|
||||
public void updateLocalArchivePath(long jobId, String localPath) throws SQLException {
|
||||
updateJob(jobId, null, "local_archive_path=?", ps -> ps.setString(1, localPath));
|
||||
}
|
||||
|
||||
public void markArweaveConfirmed(long jobId, int confirmations) throws SQLException {
|
||||
updateJob(jobId, "ARWEAVE_CONFIRMED", "arweave_confirmations=?", ps -> ps.setInt(1, confirmations));
|
||||
}
|
||||
|
||||
public void markSolanaSubmitted(long jobId, String signature) throws SQLException {
|
||||
updateJob(jobId, "SOLANA_SUBMITTED", "solana_signature=?", ps -> ps.setString(1, signature));
|
||||
}
|
||||
|
||||
public void markSolanaFinalized(long jobId) throws SQLException {
|
||||
updateJob(jobId, "SOLANA_FINALIZED", null, null);
|
||||
}
|
||||
|
||||
public void markFailed(long jobId, String error) throws SQLException {
|
||||
updateJob(jobId, "FAILED", "error_text=?", ps -> ps.setString(1, error == null ? "" : error));
|
||||
}
|
||||
|
||||
/**
|
||||
* После Solana finalized атомарно переводит курсоры всех blockchain на новый chunk и закрывает job.
|
||||
*/
|
||||
public void commitCursors(long jobId, byte[] bigBlockHash) throws SQLException {
|
||||
try (Connection c = db.getConnection()) {
|
||||
boolean oldAuto = c.getAutoCommit();
|
||||
c.setAutoCommit(false);
|
||||
try {
|
||||
ArchivePublishJob job = getJob(c, jobId);
|
||||
if (job == null) throw new SQLException("Archive job не найден: " + jobId);
|
||||
List<ArchivePublishJobChain> chains = listJobChains(c, jobId);
|
||||
long now = System.currentTimeMillis();
|
||||
String upsert = """
|
||||
INSERT INTO archive_chain_cursor(
|
||||
blockchain_name, last_archived_source_block_number, last_archived_source_block_hash,
|
||||
last_archive_big_block_number, last_archive_big_block_hash,
|
||||
last_chunk_offset, last_chunk_size, updated_at_ms
|
||||
) VALUES(?,?,?,?,?,?,?,?)
|
||||
ON CONFLICT(blockchain_name) DO UPDATE SET
|
||||
last_archived_source_block_number=EXCLUDED.last_archived_source_block_number,
|
||||
last_archived_source_block_hash=EXCLUDED.last_archived_source_block_hash,
|
||||
last_archive_big_block_number=EXCLUDED.last_archive_big_block_number,
|
||||
last_archive_big_block_hash=EXCLUDED.last_archive_big_block_hash,
|
||||
last_chunk_offset=EXCLUDED.last_chunk_offset,
|
||||
last_chunk_size=EXCLUDED.last_chunk_size,
|
||||
updated_at_ms=EXCLUDED.updated_at_ms
|
||||
""";
|
||||
try (PreparedStatement ps = c.prepareStatement(upsert)) {
|
||||
for (ArchivePublishJobChain ch : chains) {
|
||||
if (ch.newChunkOffset() == null || ch.newChunkSize() == null) {
|
||||
throw new SQLException("Не сохранены координаты нового chunk: " + ch.blockchainName());
|
||||
}
|
||||
ps.setString(1, ch.blockchainName());
|
||||
ps.setLong(2, ch.toSourceBlockNumber());
|
||||
ps.setBytes(3, ch.lastSourceBlockHash());
|
||||
ps.setLong(4, job.bigBlockNumber());
|
||||
ps.setBytes(5, bigBlockHash);
|
||||
ps.setLong(6, ch.newChunkOffset());
|
||||
ps.setLong(7, ch.newChunkSize());
|
||||
ps.setLong(8, now);
|
||||
ps.addBatch();
|
||||
}
|
||||
ps.executeBatch();
|
||||
}
|
||||
try (PreparedStatement ps = c.prepareStatement(
|
||||
"UPDATE archive_publish_job SET status='CURSORS_COMMITTED', updated_at_ms=? WHERE id=?")) {
|
||||
ps.setLong(1, now);
|
||||
ps.setLong(2, jobId);
|
||||
ps.executeUpdate();
|
||||
}
|
||||
c.commit();
|
||||
} catch (Exception e) {
|
||||
c.rollback();
|
||||
if (e instanceof SQLException se) throw se;
|
||||
throw new SQLException("Не удалось commit archive cursors", e);
|
||||
} finally {
|
||||
c.setAutoCommit(oldAuto);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ArchivePublishJob getJob(Connection c, long id) throws SQLException {
|
||||
String sql = """
|
||||
SELECT id, big_block_number, status, created_at_ms, local_archive_path,
|
||||
archive_hash, arweave_tx_id, arweave_confirmations, solana_signature,
|
||||
error_text, updated_at_ms
|
||||
FROM archive_publish_job WHERE id=?
|
||||
""";
|
||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
ps.setLong(1, id);
|
||||
try (ResultSet rs = ps.executeQuery()) { return rs.next() ? mapJob(rs) : null; }
|
||||
}
|
||||
}
|
||||
|
||||
private List<ArchivePublishJobChain> listJobChains(Connection c, long jobId) throws SQLException {
|
||||
String sql = """
|
||||
SELECT job_id, blockchain_name, from_source_block_number, to_source_block_number,
|
||||
previous_source_block_hash, last_source_block_hash,
|
||||
previous_archive_big_block_number, previous_archive_big_block_hash,
|
||||
previous_chunk_offset, previous_chunk_size,
|
||||
new_chunk_offset, new_chunk_size
|
||||
FROM archive_publish_job_chain
|
||||
WHERE job_id=? ORDER BY blockchain_name ASC
|
||||
""";
|
||||
List<ArchivePublishJobChain> out = new ArrayList<>();
|
||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
ps.setLong(1, jobId);
|
||||
try (ResultSet rs = ps.executeQuery()) { while (rs.next()) out.add(mapJobChain(rs)); }
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
private interface Binder { void bind(PreparedStatement ps) throws SQLException; }
|
||||
|
||||
private void updateJob(long jobId, String status, String setFragment, Binder binder) throws SQLException {
|
||||
if (setFragment != null && !setFragment.isBlank()) {
|
||||
try (Connection c = db.getConnection()) {
|
||||
updateJobWithExtra(c, jobId, status, setFragment, binder);
|
||||
}
|
||||
return;
|
||||
}
|
||||
String sql = status == null
|
||||
? "UPDATE archive_publish_job SET updated_at_ms=? WHERE id=?"
|
||||
: "UPDATE archive_publish_job SET status=?, updated_at_ms=? WHERE id=?";
|
||||
try (Connection c = db.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
int i = 1;
|
||||
if (status != null) ps.setString(i++, status);
|
||||
ps.setLong(i++, System.currentTimeMillis());
|
||||
ps.setLong(i, jobId);
|
||||
ps.executeUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
private void updateJobWithExtra(Connection c, long jobId, String status, String setFragment, Binder binder) throws SQLException {
|
||||
String statusSql = status == null ? "" : ", status=?";
|
||||
String sql = "UPDATE archive_publish_job SET " + setFragment + statusSql + ", updated_at_ms=? WHERE id=?";
|
||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||
binder.bind(ps);
|
||||
int count = countParameters(setFragment);
|
||||
int i = count + 1;
|
||||
if (status != null) ps.setString(i++, status);
|
||||
ps.setLong(i++, System.currentTimeMillis());
|
||||
ps.setLong(i, jobId);
|
||||
ps.executeUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
private static int countParameters(String sqlPart) {
|
||||
int n = 0;
|
||||
for (int i = 0; i < sqlPart.length(); i++) if (sqlPart.charAt(i) == '?') n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
private static void bindJobChain(PreparedStatement ps, ArchivePublishJobChain e) throws SQLException {
|
||||
int i=1;
|
||||
ps.setLong(i++, e.jobId());
|
||||
ps.setString(i++, e.blockchainName());
|
||||
ps.setLong(i++, e.fromSourceBlockNumber());
|
||||
ps.setLong(i++, e.toSourceBlockNumber());
|
||||
ps.setBytes(i++, e.previousSourceBlockHash());
|
||||
ps.setBytes(i++, e.lastSourceBlockHash());
|
||||
setLongNullable(ps, i++, e.previousArchiveBigBlockNumber());
|
||||
ps.setBytes(i++, e.previousArchiveBigBlockHash());
|
||||
setLongNullable(ps, i++, e.previousChunkOffset());
|
||||
setLongNullable(ps, i++, e.previousChunkSize());
|
||||
setLongNullable(ps, i++, e.newChunkOffset());
|
||||
setLongNullable(ps, i, e.newChunkSize());
|
||||
}
|
||||
|
||||
private static ArchiveChainCursor mapCursor(ResultSet rs) throws SQLException {
|
||||
return new ArchiveChainCursor(
|
||||
rs.getString("blockchain_name"),
|
||||
rs.getLong("last_archived_source_block_number"),
|
||||
rs.getBytes("last_archived_source_block_hash"),
|
||||
rs.getLong("last_archive_big_block_number"),
|
||||
rs.getBytes("last_archive_big_block_hash"),
|
||||
rs.getLong("last_chunk_offset"),
|
||||
rs.getLong("last_chunk_size"),
|
||||
rs.getLong("updated_at_ms")
|
||||
);
|
||||
}
|
||||
|
||||
private static ArchivePublishJob mapJob(ResultSet rs) throws SQLException {
|
||||
int conf = rs.getInt("arweave_confirmations");
|
||||
Integer confObj = rs.wasNull() ? null : conf;
|
||||
return new ArchivePublishJob(
|
||||
rs.getLong("id"), rs.getLong("big_block_number"), rs.getString("status"),
|
||||
rs.getLong("created_at_ms"), rs.getString("local_archive_path"),
|
||||
rs.getBytes("archive_hash"), rs.getBytes("arweave_tx_id"), confObj,
|
||||
rs.getString("solana_signature"), rs.getString("error_text"), rs.getLong("updated_at_ms")
|
||||
);
|
||||
}
|
||||
|
||||
private static ArchivePublishJobChain mapJobChain(ResultSet rs) throws SQLException {
|
||||
return new ArchivePublishJobChain(
|
||||
rs.getLong("job_id"), rs.getString("blockchain_name"),
|
||||
rs.getLong("from_source_block_number"), rs.getLong("to_source_block_number"),
|
||||
rs.getBytes("previous_source_block_hash"), rs.getBytes("last_source_block_hash"),
|
||||
getLongNullable(rs, "previous_archive_big_block_number"), rs.getBytes("previous_archive_big_block_hash"),
|
||||
getLongNullable(rs, "previous_chunk_offset"), getLongNullable(rs, "previous_chunk_size"),
|
||||
getLongNullable(rs, "new_chunk_offset"), getLongNullable(rs, "new_chunk_size")
|
||||
);
|
||||
}
|
||||
|
||||
private static Long getLongNullable(ResultSet rs, String name) throws SQLException {
|
||||
long v = rs.getLong(name);
|
||||
return rs.wasNull() ? null : v;
|
||||
}
|
||||
|
||||
private static void setLongNullable(PreparedStatement ps, int idx, Long v) throws SQLException {
|
||||
if (v == null) ps.setNull(idx, Types.BIGINT); else ps.setLong(idx, v);
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user