SHA256
Улучшить Arweave-синхронизацию и ротацию ключей
Улучшено скачивание блоков из Arweave и обработка канонической цепочки. Ротация ключей переведена на новую схему с пересборкой публичных каналов; личные сообщения пока не меняются.
This commit is contained in:
@@ -47,10 +47,13 @@ case "${MODE}" in
|
|||||||
lvgl-official-based-test) SKETCH_DIR="${ROOT_DIR}/test_sketches/lvgl_official_based_test" ;;
|
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-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" ;;
|
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-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" ;;
|
lvgl-nav-minimal-test) SKETCH_DIR="${ROOT_DIR}/shine_homeserver_main" ;;
|
||||||
*)
|
*)
|
||||||
echo "Unknown mode: ${MODE}" >&2
|
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-screen-start-stub" >&2
|
||||||
exit 2
|
exit 2
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
@@ -78,6 +81,7 @@ compile_args=(
|
|||||||
--library "${DEMO_BASE}/libraries/XPowersLib"
|
--library "${DEMO_BASE}/libraries/XPowersLib"
|
||||||
--library "${DEMO_BASE}/libraries/lvgl"
|
--library "${DEMO_BASE}/libraries/lvgl"
|
||||||
--library "${DEMO_BASE}/libraries/Mylibrary"
|
--library "${DEMO_BASE}/libraries/Mylibrary"
|
||||||
|
--library "${ROOT_DIR}/simple_av_test"
|
||||||
"${SKETCH_DIR}"
|
"${SKETCH_DIR}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,8 @@
|
|||||||
- `lvgl_official_based_test/` - минимальный наш экран поверх максимально близкой к официальному `05_LVGL_Widgets` инициализации
|
- `lvgl_official_based_test/` - минимальный наш экран поверх максимально близкой к официальному `05_LVGL_Widgets` инициализации
|
||||||
- `lvgl_subserver_touch_test/` - старый гибридный тест: `LVGL`-экран с инициализацией дисплея и чтением touch из старого `shine_homeserver_ui`; подтверждён на реальном устройстве
|
- `lvgl_subserver_touch_test/` - старый гибридный тест: `LVGL`-экран с инициализацией дисплея и чтением touch из старого `shine_homeserver_ui`; подтверждён на реальном устройстве
|
||||||
- `lvgl_russian_font_test/` - тест кастомного кириллического `LVGL`-шрифта с русскими кнопками, длинными строками и рабочим touch
|
- `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
|
||||||
- `lvgl_nav_minimal_test/` - старое тестовое имя, этот скетч перенесён в `shine_homeserver_main/` и теперь является основным
|
- `lvgl_nav_minimal_test/` - старое тестовое имя, этот скетч перенесён в `shine_homeserver_main/` и теперь является основным
|
||||||
|
|
||||||
## Запуск
|
## Запуск
|
||||||
@@ -29,3 +31,5 @@
|
|||||||
- `./burn.sh lvgl-official-based-test`
|
- `./burn.sh lvgl-official-based-test`
|
||||||
- `./burn.sh lvgl-subserver-touch-test`
|
- `./burn.sh lvgl-subserver-touch-test`
|
||||||
- `./burn.sh lvgl-russian-font-test`
|
- `./burn.sh lvgl-russian-font-test`
|
||||||
|
- `./burn.sh usb-contact-test`
|
||||||
|
- `./burn.sh usb-contact-debug-test`
|
||||||
|
|||||||
+459
@@ -0,0 +1,459 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
#include <Wire.h>
|
||||||
|
#include <math.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;
|
||||||
|
|
||||||
|
#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;
|
||||||
|
|
||||||
|
struct Rect {
|
||||||
|
int16_t x;
|
||||||
|
int16_t y;
|
||||||
|
int16_t w;
|
||||||
|
int16_t h;
|
||||||
|
};
|
||||||
|
|
||||||
|
static const Rect kStartButton = {96, 348, 288, 78};
|
||||||
|
static const uint16_t kLimitsSec[] = {5, 7, 10, 15, 20};
|
||||||
|
|
||||||
|
static bool gStarted = 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 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 gShortStreak = 0;
|
||||||
|
|
||||||
|
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 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 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) {
|
||||||
|
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, 2500);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void playAlertBeep() {
|
||||||
|
playBeep(1200, 1600, 12000);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 clearArea(int x, int y, int w, int h, uint16_t fill) {
|
||||||
|
gfx->fillRect(x, y, w, h, fill);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
uint32_t avgMs = averageCycleMs();
|
||||||
|
|
||||||
|
if (force || gLastDrawnClosed != gStableClosed) {
|
||||||
|
clearArea(42, 118, 396, 68, C_CARD);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
clearArea(28, 58, 424, 28, C_PANEL);
|
||||||
|
snprintf(line, sizeof(line), "Limit: %us alert by average", kLimitsSec[gLimitIndex]);
|
||||||
|
drawText(28, 60, line, C_MUTE, 2);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
clearArea(28, 398, 250, 28, C_PANEL);
|
||||||
|
snprintf(line, sizeof(line), "Short avg streak: %u", gShortStreak);
|
||||||
|
drawText(32, 402, line, gShortStreak > 0 ? C_BAD : C_MUTE, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void redrawScreen() {
|
||||||
|
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 nextLimit() {
|
||||||
|
gLimitIndex = (gLimitIndex + 1) % (sizeof(kLimitsSec) / sizeof(kLimitsSec[0]));
|
||||||
|
gNeedRedraw = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void handleStableContact(bool closed) {
|
||||||
|
uint32_t now = millis();
|
||||||
|
gStableClosed = closed;
|
||||||
|
if (closed) {
|
||||||
|
if (gLastCycleStartMs != 0) {
|
||||||
|
addCycle(now - gLastCycleStartMs);
|
||||||
|
playCycleTick();
|
||||||
|
alertIfNeeded();
|
||||||
|
} else {
|
||||||
|
playCycleTick();
|
||||||
|
}
|
||||||
|
gLastCycleStartMs = now;
|
||||||
|
}
|
||||||
|
gNeedRedraw = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
nextLimit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 && !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);
|
||||||
|
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 && 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,81 @@
|
|||||||
|
# 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/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.
|
||||||
|
|
||||||
|
## 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 cycles the short-cycle limit through `5`, `7`, `10`, `15`, `20` seconds
|
||||||
|
- the working screen uses a static frame and only updates value regions to avoid display flicker
|
||||||
|
- 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
|
||||||
|
- reset/power cycle restores ordinary native USB boot
|
||||||
|
|
||||||
|
This version is intended to monitor breathing cadence from a dry-contact USB D-/D+ sensor.
|
||||||
+5
-1
@@ -22,10 +22,14 @@ public final class ArweaveBlockSyncScheduler {
|
|||||||
if (!STARTED.compareAndSet(false,true)) return;
|
if (!STARTED.compareAndSet(false,true)) return;
|
||||||
try {
|
try {
|
||||||
ArweaveBlockSyncService service = new ArweaveBlockSyncService(cfg);
|
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; });
|
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 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(task,0,cfg.syncIntervalMinutes(),TimeUnit.MINUTES);
|
||||||
log.info("Arweave user-block sync enabled: interval={}m gateway={}",cfg.syncIntervalMinutes(),cfg.syncGateway());
|
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); }
|
} 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();}
|
public static void close(){ScheduledExecutorService e=executor;if(e!=null)e.shutdownNow();}
|
||||||
|
|||||||
+489
-51
@@ -7,9 +7,15 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
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;
|
||||||
|
import server.sync.BlockchainResyncGuard;
|
||||||
|
import shine.db.DbController;
|
||||||
import shine.db.dao.ArweaveBlockImportDAO;
|
import shine.db.dao.ArweaveBlockImportDAO;
|
||||||
import shine.db.dao.BlockchainStateDAO;
|
import shine.db.dao.BlockchainStateDAO;
|
||||||
import shine.db.dao.BlocksDAO;
|
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.dao.SolanaUserPdaCurrentDAO;
|
||||||
import shine.db.entities.BlockchainStateEntry;
|
import shine.db.entities.BlockchainStateEntry;
|
||||||
import shine.db.entities.SolanaUserPdaCurrentEntry;
|
import shine.db.entities.SolanaUserPdaCurrentEntry;
|
||||||
@@ -22,6 +28,10 @@ import java.net.http.HttpClient;
|
|||||||
import java.net.http.HttpRequest;
|
import java.net.http.HttpRequest;
|
||||||
import java.net.http.HttpResponse;
|
import java.net.http.HttpResponse;
|
||||||
import java.nio.charset.StandardCharsets;
|
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.time.Duration;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@@ -45,14 +55,28 @@ public final class ArweaveBlockSyncService {
|
|||||||
private final SolanaUserPdaCurrentDAO usersDAO = SolanaUserPdaCurrentDAO.getInstance();
|
private final SolanaUserPdaCurrentDAO usersDAO = SolanaUserPdaCurrentDAO.getInstance();
|
||||||
private final BlockchainStateDAO stateDAO = BlockchainStateDAO.getInstance();
|
private final BlockchainStateDAO stateDAO = BlockchainStateDAO.getInstance();
|
||||||
private final Net_AddBlock_Handler addBlock = new Net_AddBlock_Handler();
|
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 ArweaveBlockSyncService(ArweaveBlocksConfig cfg) { this.cfg = cfg; }
|
||||||
|
|
||||||
public void runCycle() throws Exception {
|
public void runCycle() throws Exception {
|
||||||
|
recoverInterruptedCanonicalRebuilds();
|
||||||
discover();
|
discover();
|
||||||
drainQueue();
|
drainQueue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Быстрый retry только для уже обнаруженных DataItem, без повторного полного GraphQL discovery. */
|
||||||
|
public void runRetryCycle() throws Exception {
|
||||||
|
int queued = retryPendingDownloads();
|
||||||
|
if (queued > 0) drainQueue();
|
||||||
|
}
|
||||||
|
|
||||||
private void discover() throws Exception {
|
private void discover() throws Exception {
|
||||||
long stored = importDAO.getLastBlockHeight();
|
long stored = importDAO.getLastBlockHeight();
|
||||||
long minHeight = Math.max(cfg.syncStartBlockHeight(), stored);
|
long minHeight = Math.max(cfg.syncStartBlockHeight(), stored);
|
||||||
@@ -85,11 +109,20 @@ public final class ArweaveBlockSyncService {
|
|||||||
log.warn("Ignoring malformed Arweave DataItem id {}: {}", dataItemId, e.getMessage());
|
log.warn("Ignoring malformed Arweave DataItem id {}: {}", dataItemId, e.getMessage());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (blocksDAO.existsByDataItemId(id32) || importDAO.exists(id32)) continue;
|
if (blocksDAO.existsByDataItemId(id32) || importDAO.exists(id32)) {
|
||||||
|
unresolvedDownloads.remove(dataItemId);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
DownloadedDataItem downloaded = downloadSignedDataItem(dataItemId);
|
DownloadedDataItem downloaded = downloadSignedDataItem(dataItemId);
|
||||||
|
unresolvedDownloads.remove(dataItemId);
|
||||||
if (importDAO.enqueueIfMissing(id32, downloaded.rootTxId(), height, downloaded.rawDataItem(), System.currentTimeMillis())) discovered++;
|
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) {
|
} catch (Exception e) {
|
||||||
lowestRetryHeight = Math.min(lowestRetryHeight, height);
|
lowestRetryHeight = Math.min(lowestRetryHeight, height);
|
||||||
log.warn("Cannot retrieve signed DataItem {} at height {} yet: {}", dataItemId, height, e.getMessage());
|
log.warn("Cannot retrieve signed DataItem {} at height {} yet: {}", dataItemId, height, e.getMessage());
|
||||||
@@ -108,37 +141,117 @@ public final class ArweaveBlockSyncService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 (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 {
|
private void drainQueue() throws Exception {
|
||||||
int passes = 0;
|
int passes = 0;
|
||||||
int importedTotal = 0;
|
int importedTotal = 0;
|
||||||
boolean progress;
|
boolean progress;
|
||||||
|
Set<String> replayedLogins = new HashSet<>();
|
||||||
do {
|
do {
|
||||||
progress = false;
|
progress = false;
|
||||||
List<ArweaveBlockImportDAO.QueueItem> pending = importDAO.listPending(cfg.syncQueueBatchSize());
|
|
||||||
|
// 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;
|
if (pending.isEmpty()) break;
|
||||||
for (ArweaveBlockImportDAO.QueueItem q : pending) {
|
for (ArweaveBlockImportDAO.QueueItem q : pending) {
|
||||||
Ans104DataItem item;
|
if (q.parsedLogin() == null || q.parsedForkNumber() == null || q.parsedBlockNumber() == null) continue;
|
||||||
try {
|
SolanaUserPdaCurrentEntry activeUser = usersDAO.getByLogin(q.parsedLogin());
|
||||||
item = new Ans104DataItem(q.rawDataItem());
|
if (activeUser == null || activeUser.getBlockchainName() == null || activeUser.getBlockchainName().isBlank()) {
|
||||||
if (!Arrays.equals(item.id32(), q.dataItemId())) throw new IllegalArgumentException("data_item_id mismatch");
|
importDAO.setPendingError(q.dataItemId(), "active_user_missing", System.currentTimeMillis());
|
||||||
if (!item.hasTag(ArweaveBlocksConfig.TEST_TAG_NAME, ArweaveBlocksConfig.TEST_TAG_VALUE)) throw new IllegalArgumentException("bad App tag");
|
continue;
|
||||||
if (!item.verifySignature()) throw new IllegalArgumentException("bad ANS-104 signature");
|
}
|
||||||
new BchBlockEntry(q.rawDataItem());
|
int activeFork = forkOf(activeUser.getBlockchainName());
|
||||||
} catch (Exception e) {
|
if (!isAncestryComplete(q.parsedLogin(), activeFork)) {
|
||||||
importDAO.reject(q.dataItemId(), "invalid_data_item: " + e.getMessage(), System.currentTimeMillis());
|
importDAO.setPendingError(q.dataItemId(), "ancestry_incomplete", System.currentTimeMillis());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
SolanaUserPdaCurrentEntry user = usersDAO.getByBlockchainKey(item.owner32());
|
int canonicalFork;
|
||||||
if (user == null || user.getBlockchainName() == null || user.getBlockchainName().isBlank()) {
|
try (Connection cc = DbController.getInstance().getConnection()) {
|
||||||
importDAO.setPendingError(q.dataItemId(), "unknown_owner", System.currentTimeMillis());
|
canonicalFork = canonical.resolveFork(cc, q.parsedLogin(), activeFork, q.parsedBlockNumber());
|
||||||
|
}
|
||||||
|
if (canonicalFork != q.parsedForkNumber()) {
|
||||||
|
importDAO.reject(q.dataItemId(), "orphan_noncanonical", System.currentTimeMillis());
|
||||||
|
progress = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
ensureState(user);
|
|
||||||
Net_AddBlock_Handler.ArweaveImportResult result = addBlock.addBlockFromArweave(user.getBlockchainName(), q.rawDataItem());
|
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()) {
|
if (result.ok()) {
|
||||||
importDAO.delete(q.dataItemId());
|
importDAO.delete(q.dataItemId());
|
||||||
importedTotal++; progress = true;
|
importedTotal++;
|
||||||
|
replayedLogins.add(q.parsedLogin());
|
||||||
|
progress = true;
|
||||||
|
rebuildIfLocalOrphansExist(q.parsedLogin(), activeFork);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
String reason = result.reasonCode();
|
String reason = result.reasonCode();
|
||||||
@@ -148,21 +261,215 @@ public final class ArweaveBlockSyncService {
|
|||||||
importDAO.setPendingError(q.dataItemId(), reason, System.currentTimeMillis());
|
importDAO.setPendingError(q.dataItemId(), reason, System.currentTimeMillis());
|
||||||
} else {
|
} else {
|
||||||
importDAO.reject(q.dataItemId(), reason, System.currentTimeMillis());
|
importDAO.reject(q.dataItemId(), reason, System.currentTimeMillis());
|
||||||
|
progress = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
passes++;
|
passes++;
|
||||||
} while (progress && passes < 8);
|
} 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);
|
if (importedTotal > 0) log.info("Imported {} SHiNE test DataItems from Arweave", importedTotal);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ensureState(SolanaUserPdaCurrentEntry user) throws Exception {
|
/**
|
||||||
BlockchainStateEntry existing = stateDAO.getByBlockchainName(user.getBlockchainName());
|
* Phase 1 of Arweave recovery: scan the complete persistent PENDING queue and register only
|
||||||
if (existing != null) return;
|
* immutable segment ancestry metadata. No block is applied or rejected here.
|
||||||
BlockchainStateEntry s = new BlockchainStateEntry();
|
*/
|
||||||
s.setBlockchainName(user.getBlockchainName()); s.setLogin(user.getLogin()); s.setBlockchainKey(user.getBlockchainKey());
|
private void prepareAllSegmentMetadata() throws Exception {
|
||||||
s.setSizeLimit(user.getPaidLimitBytes() > 0 ? user.getPaidLimitBytes() : 100_000L);
|
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";
|
||||||
s.setFileSizeBytes(0L); s.setLastBlockNumber(-1); s.setLastBlockHash(null); s.setUpdatedAtMs(System.currentTimeMillis());
|
try (Connection c = DbController.getInstance().getConnection();
|
||||||
stateDAO.insertIfMissing(s);
|
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 {
|
||||||
|
BlockchainStateEntry existing=stateDAO.getByBlockchainName(user.getBlockchainName()); if(existing!=null)return;
|
||||||
|
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);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
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);}
|
||||||
}
|
}
|
||||||
|
|
||||||
private JsonNode graphQlPage(long minHeight, String cursor) throws Exception {
|
private JsonNode graphQlPage(long minHeight, String cursor) throws Exception {
|
||||||
@@ -181,55 +488,186 @@ public final class ArweaveBlockSyncService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gateways normally expose only the payload at /{dataItemId}. SHiNE needs the complete signed
|
* Gateway по /{dataItemId} обычно отдаёт только payload. Для импорта SHiNE нужен полный
|
||||||
* DataItem, so obtain its exact offset/size inside the root L1 transaction and range-read it.
|
* подписанный ANS-104 DataItem, поэтому сначала резолвим rootTx + offset + size, а затем
|
||||||
|
* читаем точный range из root L1 transaction.
|
||||||
*/
|
*/
|
||||||
private DownloadedDataItem downloadSignedDataItem(String dataItemId) throws Exception {
|
private DownloadedDataItem downloadSignedDataItem(String dataItemId) throws Exception {
|
||||||
JsonNode offsets = getOffsets(dataItemId);
|
DataItemLocation location = resolveDataItemLocation(dataItemId);
|
||||||
String rootTxId = offsets.path("rootTxId").asText("").trim();
|
long endInclusive = location.rootOffset() + location.size() - 1;
|
||||||
long rootOffset = offsets.path("rootOffset").asLong(-1L);
|
|
||||||
long size = offsets.path("size").asLong(-1L);
|
|
||||||
if (rootTxId.isBlank() || rootOffset < 0 || size <= 0) {
|
|
||||||
throw new IOException("Bad AR.IO offsets for " + dataItemId + ": " + offsets);
|
|
||||||
}
|
|
||||||
if (size > cfg.syncMaxDataItemBytes()) {
|
|
||||||
throw new IOException("DataItem exceeds syncMaxDataItemBytes: " + size);
|
|
||||||
}
|
|
||||||
if (rootOffset > Long.MAX_VALUE - size) throw new IOException("DataItem offset overflow");
|
|
||||||
long endInclusive = rootOffset + size - 1;
|
|
||||||
|
|
||||||
HttpRequest req = HttpRequest.newBuilder(URI.create(trim(cfg.syncGateway()) + "/raw/" + rootTxId))
|
HttpRequest req = HttpRequest.newBuilder(URI.create(trim(cfg.syncGateway()) + "/raw/" + location.rootTxId()))
|
||||||
.timeout(Duration.ofMinutes(2))
|
.timeout(Duration.ofMinutes(2))
|
||||||
.header("Range", "bytes=" + rootOffset + "-" + endInclusive)
|
.header("Range", "bytes=" + location.rootOffset() + "-" + endInclusive)
|
||||||
.header("Accept", "application/octet-stream")
|
.header("Accept", "application/octet-stream")
|
||||||
.GET().build();
|
.GET().build();
|
||||||
HttpResponse<InputStream> resp = http.send(req, HttpResponse.BodyHandlers.ofInputStream());
|
HttpResponse<InputStream> resp = http.send(req, HttpResponse.BodyHandlers.ofInputStream());
|
||||||
try (InputStream in = resp.body()) {
|
try (InputStream in = resp.body()) {
|
||||||
if (resp.statusCode() != 206) {
|
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());
|
throw new IOException("Gateway ignored root range for DataItem " + dataItemId + ": HTTP " + resp.statusCode());
|
||||||
}
|
}
|
||||||
byte[] bytes = readExactlyBounded(in, (int) size);
|
byte[] bytes = readExactlyBounded(in, Math.toIntExact(location.size()));
|
||||||
if (bytes.length != size) throw new IOException("Truncated DataItem range: expected=" + size + " got=" + bytes.length);
|
if (bytes.length != location.size()) {
|
||||||
|
throw new RetryableDataItemDownloadException(
|
||||||
|
"truncated DataItem range: expected=" + location.size() + " got=" + bytes.length);
|
||||||
|
}
|
||||||
Ans104DataItem parsed = new Ans104DataItem(bytes);
|
Ans104DataItem parsed = new Ans104DataItem(bytes);
|
||||||
byte[] expectedId = B64URL.decode(dataItemId);
|
byte[] expectedId = B64URL.decode(dataItemId);
|
||||||
if (!Arrays.equals(parsed.id32(), expectedId)) {
|
if (!Arrays.equals(parsed.id32(), expectedId)) {
|
||||||
throw new IOException("Range returned another ANS-104 DataItem for " + dataItemId);
|
throw new IOException("Range returned another ANS-104 DataItem for " + dataItemId);
|
||||||
}
|
}
|
||||||
return new DownloadedDataItem(rootTxId, bytes);
|
return new DownloadedDataItem(location.rootTxId(), bytes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private record DownloadedDataItem(String rootTxId, byte[] rawDataItem) {}
|
/**
|
||||||
|
* Координаты считаются только подсказкой транспорта: источник может ошибиться, поэтому после
|
||||||
|
* range-read всё равно обязательно сверяется настоящий ANS-104 DataItem ID и затем подпись.
|
||||||
|
*/
|
||||||
|
private DataItemLocation resolveDataItemLocation(String dataItemId) throws Exception {
|
||||||
|
List<String> misses = new ArrayList<>(3);
|
||||||
|
|
||||||
private JsonNode getOffsets(String dataItemId) throws Exception {
|
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))
|
HttpRequest req = HttpRequest.newBuilder(URI.create(trim(cfg.syncGateway()) + "/ar-io/offsets/" + dataItemId))
|
||||||
.timeout(Duration.ofSeconds(30)).header("Accept","application/json").GET().build();
|
.timeout(Duration.ofSeconds(30)).header("Accept", "application/json").GET().build();
|
||||||
HttpResponse<String> resp = http.send(req, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
HttpResponse<String> resp = http.send(req, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
||||||
if (resp.statusCode() == 404) throw new IOException("AR.IO offsets not indexed yet");
|
if (resp.statusCode() == 404) return null;
|
||||||
if (resp.statusCode() < 200 || resp.statusCode() >= 300) {
|
if (resp.statusCode() < 200 || resp.statusCode() >= 300) {
|
||||||
throw new IOException("AR.IO offsets HTTP " + resp.statusCode() + ": " + safe(resp.body()));
|
throw new IOException("HTTP " + resp.statusCode() + ": " + safe(resp.body()));
|
||||||
}
|
}
|
||||||
return MAPPER.readTree(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 {
|
private static byte[] readExactlyBounded(InputStream in, int expected) throws IOException {
|
||||||
|
|||||||
+6
-2
@@ -27,7 +27,9 @@ public record ArweaveBlocksConfig(
|
|||||||
int syncQueueBatchSize,
|
int syncQueueBatchSize,
|
||||||
long syncStartBlockHeight,
|
long syncStartBlockHeight,
|
||||||
long syncMaxDataItemBytes,
|
long syncMaxDataItemBytes,
|
||||||
String syncGateway
|
String syncGateway,
|
||||||
|
String syncTurboApiBase,
|
||||||
|
int syncLocationRetrySeconds
|
||||||
) {
|
) {
|
||||||
public enum PublishMode {
|
public enum PublishMode {
|
||||||
TURBO,
|
TURBO,
|
||||||
@@ -73,7 +75,9 @@ public record ArweaveBlocksConfig(
|
|||||||
positive(c.getInt("arweave.blocks.sync.queueBatchSize", 10_000), "sync.queueBatchSize"),
|
positive(c.getInt("arweave.blocks.sync.queueBatchSize", 10_000), "sync.queueBatchSize"),
|
||||||
nonNegativeLong(parseLong(c.getParam("arweave.blocks.sync.startBlockHeight"), 0L), "sync.startBlockHeight"),
|
nonNegativeLong(parseLong(c.getParam("arweave.blocks.sync.startBlockHeight"), 0L), "sync.startBlockHeight"),
|
||||||
positiveLong(maxDataItemBytes, "sync.maxDataItemBytes"),
|
positiveLong(maxDataItemBytes, "sync.maxDataItemBytes"),
|
||||||
orDefault(c.getParam("arweave.blocks.sync.gateway"), "https://turbo-gateway.com")
|
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")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ public final class DatabaseInitializer {
|
|||||||
public static final String DB_FORMAT_MARKER = "SHiNE_DB_V2";
|
public static final String DB_FORMAT_MARKER = "SHiNE_DB_V2";
|
||||||
public static final int DB_FORMAT_VERSION = 2;
|
public static final int DB_FORMAT_VERSION = 2;
|
||||||
public static final int SCHEMA_VERSION_1 = 1;
|
public static final int SCHEMA_VERSION_1 = 1;
|
||||||
public static final int SCHEMA_VERSION_29 = 29;
|
public static final int SCHEMA_VERSION_30 = 30;
|
||||||
public static final String POSTGRES_SCHEMA_RESOURCE = "postgres/schema_v1.sql";
|
public static final String POSTGRES_SCHEMA_RESOURCE = "postgres/schema_v1.sql";
|
||||||
|
|
||||||
private DatabaseInitializer() {}
|
private DatabaseInitializer() {}
|
||||||
@@ -113,9 +113,9 @@ public final class DatabaseInitializer {
|
|||||||
|
|
||||||
validateDatabaseFormatV2(conn);
|
validateDatabaseFormatV2(conn);
|
||||||
int currentVersion = readCurrentSchemaVersion(conn);
|
int currentVersion = readCurrentSchemaVersion(conn);
|
||||||
if (currentVersion != SCHEMA_VERSION_29) {
|
if (currentVersion != SCHEMA_VERSION_30) {
|
||||||
throw new SQLException(
|
throw new SQLException(
|
||||||
"Unsupported SHiNE DB v2 schema version: expected 29, found " + currentVersion);
|
"Unsupported SHiNE DB v2 schema version: expected 30, found " + currentVersion);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,9 @@ public final class ArweaveBlockImportDAO {
|
|||||||
|
|
||||||
public record QueueItem(byte[] dataItemId, long blockHeight,
|
public record QueueItem(byte[] dataItemId, long blockHeight,
|
||||||
byte[] rawDataItem, String status, String lastError,
|
byte[] rawDataItem, String status, String lastError,
|
||||||
long firstSeenAtMs, long updatedAtMs) {}
|
long firstSeenAtMs, long updatedAtMs,
|
||||||
|
String parsedLogin, Integer parsedForkNumber, Integer parsedBlockNumber, byte[] parsedBlockHash,
|
||||||
|
boolean metadataValidated) {}
|
||||||
|
|
||||||
private static volatile ArweaveBlockImportDAO instance;
|
private static volatile ArweaveBlockImportDAO instance;
|
||||||
private final DbController db = DbController.getInstance();
|
private final DbController db = DbController.getInstance();
|
||||||
@@ -82,7 +84,8 @@ public final class ArweaveBlockImportDAO {
|
|||||||
public List<QueueItem> listPending(int limit) throws SQLException {
|
public List<QueueItem> listPending(int limit) throws SQLException {
|
||||||
int safeLimit = Math.max(1, Math.min(limit, 100_000));
|
int safeLimit = Math.max(1, Math.min(limit, 100_000));
|
||||||
String sql = """
|
String sql = """
|
||||||
SELECT data_item_id,block_height,raw_data_item,status,last_error,first_seen_at_ms,updated_at_ms
|
SELECT data_item_id,block_height,raw_data_item,status,last_error,first_seen_at_ms,updated_at_ms,
|
||||||
|
parsed_login,parsed_fork_number,parsed_block_number,parsed_block_hash,metadata_validated
|
||||||
FROM arweave_block_import_queue
|
FROM arweave_block_import_queue
|
||||||
WHERE status=?
|
WHERE status=?
|
||||||
ORDER BY block_height ASC, first_seen_at_ms ASC
|
ORDER BY block_height ASC, first_seen_at_ms ASC
|
||||||
@@ -97,13 +100,55 @@ public final class ArweaveBlockImportDAO {
|
|||||||
out.add(new QueueItem(
|
out.add(new QueueItem(
|
||||||
rs.getBytes("data_item_id"), rs.getLong("block_height"),
|
rs.getBytes("data_item_id"), rs.getLong("block_height"),
|
||||||
rs.getBytes("raw_data_item"), rs.getString("status"), rs.getString("last_error"),
|
rs.getBytes("raw_data_item"), rs.getString("status"), rs.getString("last_error"),
|
||||||
rs.getLong("first_seen_at_ms"), rs.getLong("updated_at_ms")));
|
rs.getLong("first_seen_at_ms"), rs.getLong("updated_at_ms"),
|
||||||
|
rs.getString("parsed_login"), (Integer) rs.getObject("parsed_fork_number"),
|
||||||
|
(Integer) rs.getObject("parsed_block_number"), rs.getBytes("parsed_block_hash"),
|
||||||
|
rs.getBoolean("metadata_validated")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Persist cryptographically validated parse metadata used to order canonical replay. */
|
||||||
|
public void markMetadataValidated(byte[] id, String login, int forkNumber, int blockNumber, byte[] blockHash, long nowMs)
|
||||||
|
throws SQLException {
|
||||||
|
String sql = """
|
||||||
|
UPDATE arweave_block_import_queue
|
||||||
|
SET parsed_login=?,parsed_fork_number=?,parsed_block_number=?,parsed_block_hash=?,metadata_validated=TRUE,
|
||||||
|
last_error='',updated_at_ms=?
|
||||||
|
WHERE data_item_id=? AND status=?
|
||||||
|
""";
|
||||||
|
try (Connection c=db.getConnection(); PreparedStatement ps=c.prepareStatement(sql)) {
|
||||||
|
ps.setString(1, login); ps.setInt(2, forkNumber); ps.setInt(3, blockNumber); ps.setBytes(4, blockHash);
|
||||||
|
ps.setLong(5, nowMs); ps.setBytes(6, id); ps.setString(7, STATUS_PENDING); ps.executeUpdate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Pending items with verified signatures/owner metadata, in deterministic logical replay order. */
|
||||||
|
public List<QueueItem> listPendingValidated(int limit) throws SQLException {
|
||||||
|
int safeLimit=Math.max(1,Math.min(limit,100_000));
|
||||||
|
String sql="""
|
||||||
|
SELECT data_item_id,block_height,raw_data_item,status,last_error,first_seen_at_ms,updated_at_ms,
|
||||||
|
parsed_login,parsed_fork_number,parsed_block_number,parsed_block_hash,metadata_validated
|
||||||
|
FROM arweave_block_import_queue
|
||||||
|
WHERE status=? AND metadata_validated=TRUE
|
||||||
|
ORDER BY LOWER(parsed_login) ASC, parsed_block_number ASC, parsed_fork_number ASC, first_seen_at_ms ASC
|
||||||
|
LIMIT ?
|
||||||
|
""";
|
||||||
|
List<QueueItem> out=new ArrayList<>();
|
||||||
|
try(Connection c=db.getConnection();PreparedStatement ps=c.prepareStatement(sql)){
|
||||||
|
ps.setString(1,STATUS_PENDING);ps.setInt(2,safeLimit);
|
||||||
|
try(ResultSet rs=ps.executeQuery()){while(rs.next())out.add(new QueueItem(
|
||||||
|
rs.getBytes("data_item_id"),rs.getLong("block_height"),rs.getBytes("raw_data_item"),
|
||||||
|
rs.getString("status"),rs.getString("last_error"),rs.getLong("first_seen_at_ms"),rs.getLong("updated_at_ms"),
|
||||||
|
rs.getString("parsed_login"),(Integer)rs.getObject("parsed_fork_number"),(Integer)rs.getObject("parsed_block_number"),
|
||||||
|
rs.getBytes("parsed_block_hash"),rs.getBoolean("metadata_validated")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
public void setPendingError(byte[] id, String error, long nowMs) throws SQLException {
|
public void setPendingError(byte[] id, String error, long nowMs) throws SQLException {
|
||||||
updateStatus(id, STATUS_PENDING, error, nowMs);
|
updateStatus(id, STATUS_PENDING, error, nowMs);
|
||||||
}
|
}
|
||||||
|
|||||||
+27
@@ -0,0 +1,27 @@
|
|||||||
|
package shine.db.dao;
|
||||||
|
|
||||||
|
import shine.db.DbController;
|
||||||
|
import java.sql.*;
|
||||||
|
|
||||||
|
/** Persistent ancestry/cache for segmented logical blockchains. */
|
||||||
|
public final class BlockchainForkSegmentsDAO {
|
||||||
|
private static volatile BlockchainForkSegmentsDAO instance;
|
||||||
|
private final DbController db=DbController.getInstance();
|
||||||
|
private BlockchainForkSegmentsDAO(){}
|
||||||
|
public static BlockchainForkSegmentsDAO getInstance(){ if(instance==null) synchronized(BlockchainForkSegmentsDAO.class){ if(instance==null) instance=new BlockchainForkSegmentsDAO(); } return instance; }
|
||||||
|
|
||||||
|
public record Segment(String login,int forkNumber,Integer parentForkNumber,int forkPointBlockNumber,byte[] forkPointBlockHash,int firstPhysicalBlockNumber,long createdAtMs){}
|
||||||
|
|
||||||
|
public Segment get(Connection c,String login,int fork)throws SQLException{
|
||||||
|
try(PreparedStatement ps=c.prepareStatement("SELECT login,fork_number,parent_fork_number,fork_point_block_number,fork_point_block_hash,first_physical_block_number,created_at_ms FROM blockchain_fork_segments WHERE LOWER(login)=LOWER(?) AND fork_number=?")){
|
||||||
|
ps.setString(1,login);ps.setInt(2,fork);try(ResultSet rs=ps.executeQuery()){if(!rs.next())return null; Object parent=rs.getObject(3); return new Segment(rs.getString(1),rs.getInt(2),parent==null?null:rs.getInt(3),rs.getInt(4),rs.getBytes(5),rs.getInt(6),rs.getLong(7));}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public Segment get(String login,int fork)throws SQLException{try(Connection c=db.getConnection()){return get(c,login,fork);}}
|
||||||
|
public void upsert(Connection c,Segment s)throws SQLException{
|
||||||
|
try(PreparedStatement ps=c.prepareStatement("""
|
||||||
|
INSERT INTO blockchain_fork_segments(login,fork_number,parent_fork_number,fork_point_block_number,fork_point_block_hash,first_physical_block_number,created_at_ms)
|
||||||
|
VALUES(?,?,?,?,?,?,?) ON CONFLICT(login,fork_number) DO UPDATE SET parent_fork_number=EXCLUDED.parent_fork_number,fork_point_block_number=EXCLUDED.fork_point_block_number,fork_point_block_hash=EXCLUDED.fork_point_block_hash,first_physical_block_number=EXCLUDED.first_physical_block_number
|
||||||
|
""")){ps.setString(1,s.login());ps.setInt(2,s.forkNumber());if(s.parentForkNumber()==null)ps.setNull(3,Types.INTEGER);else ps.setInt(3,s.parentForkNumber());ps.setInt(4,s.forkPointBlockNumber());if(s.forkPointBlockHash()==null)ps.setNull(5,Types.BINARY);else ps.setBytes(5,s.forkPointBlockHash());ps.setInt(6,s.firstPhysicalBlockNumber());ps.setLong(7,s.createdAtMs());ps.executeUpdate();}
|
||||||
|
}
|
||||||
|
}
|
||||||
+174
@@ -94,6 +94,167 @@ public final class BlockchainResyncCleanupDAO {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Clears all materialized state and raw local forks for one login before canonical replay.
|
||||||
|
* Raw canonical bytes must already be persisted in rebuild staging.
|
||||||
|
*
|
||||||
|
* This operation is deliberately idempotent. Before deleting the actor's projections we
|
||||||
|
* remember every foreign user/channel/message aggregate touched by them, then repair those
|
||||||
|
* aggregates after the rows/blocks have been removed. Canonical replay can therefore start
|
||||||
|
* from a clean baseline without double-counting replies, likes or subscriptions.
|
||||||
|
*/
|
||||||
|
public void cleanupLoginForCanonicalReplay(String login) throws SQLException {
|
||||||
|
if(login==null||login.isBlank())throw new IllegalArgumentException("login is blank");
|
||||||
|
try(Connection c=db.getConnection()){boolean ac=c.getAutoCommit();c.setAutoCommit(false);try{
|
||||||
|
try(Statement st=c.createStatement()){
|
||||||
|
st.execute("CREATE TEMP TABLE IF NOT EXISTS shine_cleanup_targets(to_login TEXT,to_block_number INTEGER,to_block_hash BYTEA,PRIMARY KEY(to_login,to_block_number,to_block_hash)) ON COMMIT DROP");
|
||||||
|
st.execute("TRUNCATE shine_cleanup_targets");
|
||||||
|
st.execute("CREATE TEMP TABLE IF NOT EXISTS shine_cleanup_reply_targets(to_login TEXT,to_block_number INTEGER,to_block_hash BYTEA,PRIMARY KEY(to_login,to_block_number,to_block_hash)) ON COMMIT DROP");
|
||||||
|
st.execute("TRUNCATE shine_cleanup_reply_targets");
|
||||||
|
st.execute("CREATE TEMP TABLE IF NOT EXISTS shine_cleanup_channels(owner_bch_name TEXT,root_number INTEGER,root_hash BYTEA,PRIMARY KEY(owner_bch_name,root_number,root_hash)) ON COMMIT DROP");
|
||||||
|
st.execute("TRUNCATE shine_cleanup_channels");
|
||||||
|
st.execute("CREATE TEMP TABLE IF NOT EXISTS shine_cleanup_users(login TEXT PRIMARY KEY) ON COMMIT DROP");
|
||||||
|
st.execute("TRUNCATE shine_cleanup_users");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Existing LIKE state produced by this actor.
|
||||||
|
try(PreparedStatement ps=c.prepareStatement("INSERT INTO shine_cleanup_targets SELECT DISTINCT to_login,to_block_number,to_block_hash FROM reactions_state WHERE LOWER(from_login)=LOWER(?) AND reaction_type=1 AND last_sub_type=1 ON CONFLICT DO NOTHING")){ps.setString(1,login);ps.executeUpdate();}
|
||||||
|
|
||||||
|
// Existing REPLY blocks produced by this actor. Their counters must be recomputed
|
||||||
|
// after deleting the old canonical blocks and before replay inserts the new canonical set.
|
||||||
|
try(PreparedStatement ps=c.prepareStatement("""
|
||||||
|
INSERT INTO shine_cleanup_reply_targets(to_login,to_block_number,to_block_hash)
|
||||||
|
SELECT DISTINCT to_login,to_block_number,to_block_hash
|
||||||
|
FROM blocks_store
|
||||||
|
WHERE LOWER(login)=LOWER(?) AND msg_type=1 AND msg_sub_type=20
|
||||||
|
AND to_login IS NOT NULL AND to_block_number IS NOT NULL AND to_block_hash IS NOT NULL
|
||||||
|
ON CONFLICT DO NOTHING
|
||||||
|
""")){ps.setString(1,login);ps.executeUpdate();}
|
||||||
|
|
||||||
|
// Users whose social counters may change when this actor's connection projection disappears.
|
||||||
|
try(PreparedStatement ps=c.prepareStatement("""
|
||||||
|
INSERT INTO shine_cleanup_users(login)
|
||||||
|
SELECT DISTINCT to_login FROM connections_state
|
||||||
|
WHERE LOWER(login)=LOWER(?) AND to_login IS NOT NULL AND btrim(to_login)<>''
|
||||||
|
ON CONFLICT DO NOTHING
|
||||||
|
""")){ps.setString(1,login);ps.executeUpdate();}
|
||||||
|
try(PreparedStatement ps=c.prepareStatement("INSERT INTO shine_cleanup_users(login) VALUES(?) ON CONFLICT DO NOTHING")){ps.setString(1,login);ps.executeUpdate();}
|
||||||
|
|
||||||
|
// Foreign channels followed/unfollowed by this actor.
|
||||||
|
try(PreparedStatement ps=c.prepareStatement("""
|
||||||
|
INSERT INTO shine_cleanup_channels(owner_bch_name,root_number,root_hash)
|
||||||
|
SELECT DISTINCT cn.owner_bch_name,cn.channel_root_block_number,cn.channel_root_block_hash
|
||||||
|
FROM connections_state cs JOIN channel_names_state cn
|
||||||
|
ON LOWER(cn.owner_login)=LOWER(cs.to_login)
|
||||||
|
AND cn.channel_root_block_number=cs.to_block_number
|
||||||
|
AND cn.channel_root_block_hash=cs.to_block_hash
|
||||||
|
WHERE LOWER(cs.login)=LOWER(?) AND cs.rel_type=30
|
||||||
|
ON CONFLICT DO NOTHING
|
||||||
|
""")){ps.setString(1,login);ps.executeUpdate();}
|
||||||
|
|
||||||
|
// Followers of channels owned by this login are also affected while channel projections
|
||||||
|
// disappear and are recreated by replay.
|
||||||
|
try(PreparedStatement ps=c.prepareStatement("""
|
||||||
|
INSERT INTO shine_cleanup_users(login)
|
||||||
|
SELECT DISTINCT cs.login
|
||||||
|
FROM connections_state cs
|
||||||
|
JOIN channel_names_state cn
|
||||||
|
ON LOWER(cn.owner_login)=LOWER(?)
|
||||||
|
AND LOWER(cs.to_login)=LOWER(cn.owner_login)
|
||||||
|
AND cs.to_block_number=cn.channel_root_block_number
|
||||||
|
AND cs.to_block_hash=cn.channel_root_block_hash
|
||||||
|
WHERE cs.rel_type=30
|
||||||
|
ON CONFLICT DO NOTHING
|
||||||
|
""")){ps.setString(1,login);ps.executeUpdate();}
|
||||||
|
|
||||||
|
delete(c,"DELETE FROM reactions_state WHERE LOWER(from_login)=LOWER(?)",login);
|
||||||
|
recountAffectedLikes(c);
|
||||||
|
delete(c,"DELETE FROM connections_state WHERE LOWER(login)=LOWER(?)",login);
|
||||||
|
refreshAffectedChannelStats(c);
|
||||||
|
delete(c,"DELETE FROM users_params WHERE LOWER(login)=LOWER(?)",login);
|
||||||
|
delete(c,"DELETE FROM channel_stats_state WHERE LOWER(owner_login)=LOWER(?)",login);
|
||||||
|
delete(c,"DELETE FROM channel_names_state WHERE LOWER(owner_login)=LOWER(?)",login);
|
||||||
|
|
||||||
|
// Notifications are projections of source blockchain blocks. Canonical replay recreates
|
||||||
|
// only the notifications that still exist on the selected branch.
|
||||||
|
delete(c,"DELETE FROM user_notifications_state WHERE LOWER(source_login)=LOWER(?)",login);
|
||||||
|
|
||||||
|
// Chat200 is legacy/unused and is intentionally not maintained by segmented-fork replay.
|
||||||
|
delete(c,"DELETE FROM chat200_state WHERE LOWER(owner_login)=LOWER(?)",login);
|
||||||
|
delete(c,"DELETE FROM chat200_members_state WHERE owner_bch_name LIKE ?",login+"-%");
|
||||||
|
|
||||||
|
delete(c,"DELETE FROM blocks_store WHERE LOWER(login)=LOWER(?)",login);
|
||||||
|
delete(c,"DELETE FROM blockchain_state_store WHERE LOWER(login)=LOWER(?)",login);
|
||||||
|
|
||||||
|
// With the old reply blocks gone, establish the exact baseline produced by all other
|
||||||
|
// actors. Replay will then add this actor's canonical replies exactly once.
|
||||||
|
recountAffectedReplies(c);
|
||||||
|
|
||||||
|
try(PreparedStatement ps=c.prepareStatement("SELECT shine_refresh_user_profile(?)")){ps.setString(1,login);ps.execute();}
|
||||||
|
try(PreparedStatement ps=c.prepareStatement("SELECT shine_refresh_related_user_stats(?)")){ps.setString(1,login);ps.execute();}
|
||||||
|
refreshAffectedUserStats(c);
|
||||||
|
c.commit();
|
||||||
|
}catch(Exception e){c.rollback();if(e instanceof SQLException x)throw x;throw new SQLException("canonical replay cleanup failed",e);}finally{c.setAutoCommit(ac);}}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Repairs server-owned projections after canonical replay. It is safe to call repeatedly.
|
||||||
|
* Signed channel read watermarks are preserved; only server-owned unread values are recomputed.
|
||||||
|
*/
|
||||||
|
public void finalizeCanonicalReplay(String login) throws SQLException {
|
||||||
|
if(login==null||login.isBlank())throw new IllegalArgumentException("login is blank");
|
||||||
|
try(Connection c=db.getConnection()){boolean ac=c.getAutoCommit();c.setAutoCommit(false);try{
|
||||||
|
// Remove view references to discarded messages of this user. Valid canonical targets survive.
|
||||||
|
try(PreparedStatement ps=c.prepareStatement("""
|
||||||
|
DELETE FROM message_views_state mv
|
||||||
|
WHERE LOWER(mv.to_login)=LOWER(?)
|
||||||
|
AND NOT EXISTS (
|
||||||
|
SELECT 1 FROM blocks_store b
|
||||||
|
WHERE LOWER(b.login)=LOWER(mv.to_login)
|
||||||
|
AND b.block_number=mv.to_block_number AND b.block_hash=mv.to_block_hash
|
||||||
|
)
|
||||||
|
""")){ps.setString(1,login);ps.executeUpdate();}
|
||||||
|
|
||||||
|
// Recompute all message aggregates whose target belongs to this user. This also fixes
|
||||||
|
// external reactions/replies that intentionally survive this user's rebuild.
|
||||||
|
try(PreparedStatement ps=c.prepareStatement("""
|
||||||
|
INSERT INTO message_stats(to_login,to_block_number,to_block_hash,likes_count,primary_likes_count,shining_likes_count,replies_count)
|
||||||
|
SELECT t.login,t.block_number,t.block_hash,
|
||||||
|
(SELECT COUNT(*)::INTEGER FROM reactions_state r WHERE r.reaction_type=1 AND r.last_sub_type=1 AND LOWER(r.to_login)=LOWER(t.login) AND r.to_block_number=t.block_number AND r.to_block_hash=t.block_hash),
|
||||||
|
(SELECT COUNT(*)::INTEGER FROM reactions_state r WHERE r.reaction_type=1 AND r.last_sub_type=1 AND LOWER(r.to_login)=LOWER(t.login) AND r.to_block_number=t.block_number AND r.to_block_hash=t.block_hash AND shine_is_primary(r.from_login)),
|
||||||
|
(SELECT COUNT(*)::INTEGER FROM reactions_state r WHERE r.reaction_type=1 AND r.last_sub_type=1 AND LOWER(r.to_login)=LOWER(t.login) AND r.to_block_number=t.block_number AND r.to_block_hash=t.block_hash AND shine_is_primary(r.from_login) AND shine_is_shining(r.from_login)),
|
||||||
|
(SELECT COUNT(*)::INTEGER FROM blocks_store rb WHERE rb.msg_type=1 AND rb.msg_sub_type=20 AND LOWER(rb.to_login)=LOWER(t.login) AND rb.to_block_number=t.block_number AND rb.to_block_hash=t.block_hash)
|
||||||
|
FROM blocks_store t
|
||||||
|
WHERE LOWER(t.login)=LOWER(?)
|
||||||
|
AND (
|
||||||
|
EXISTS(SELECT 1 FROM message_stats oldms WHERE LOWER(oldms.to_login)=LOWER(t.login) AND oldms.to_block_number=t.block_number AND oldms.to_block_hash=t.block_hash)
|
||||||
|
OR EXISTS(SELECT 1 FROM reactions_state r WHERE LOWER(r.to_login)=LOWER(t.login) AND r.to_block_number=t.block_number AND r.to_block_hash=t.block_hash)
|
||||||
|
OR EXISTS(SELECT 1 FROM blocks_store rb WHERE rb.msg_type=1 AND rb.msg_sub_type=20 AND LOWER(rb.to_login)=LOWER(t.login) AND rb.to_block_number=t.block_number AND rb.to_block_hash=t.block_hash)
|
||||||
|
)
|
||||||
|
ON CONFLICT(to_login,to_block_number,to_block_hash) DO UPDATE SET
|
||||||
|
likes_count=EXCLUDED.likes_count,
|
||||||
|
primary_likes_count=EXCLUDED.primary_likes_count,
|
||||||
|
shining_likes_count=EXCLUDED.shining_likes_count,
|
||||||
|
replies_count=EXCLUDED.replies_count
|
||||||
|
""")){ps.setString(1,login);ps.executeUpdate();}
|
||||||
|
try(PreparedStatement ps=c.prepareStatement("""
|
||||||
|
DELETE FROM message_stats ms
|
||||||
|
WHERE LOWER(ms.to_login)=LOWER(?)
|
||||||
|
AND NOT EXISTS (SELECT 1 FROM blocks_store b WHERE LOWER(b.login)=LOWER(ms.to_login) AND b.block_number=ms.to_block_number AND b.block_hash=ms.to_block_hash)
|
||||||
|
""")){ps.setString(1,login);ps.executeUpdate();}
|
||||||
|
|
||||||
|
// Recalculate social/channel aggregates from the final canonical projections.
|
||||||
|
try(PreparedStatement ps=c.prepareStatement("SELECT shine_refresh_user_stats(?)")){ps.setString(1,login);ps.execute();}
|
||||||
|
try(PreparedStatement ps=c.prepareStatement("SELECT shine_refresh_related_user_stats(?)")){ps.setString(1,login);ps.execute();}
|
||||||
|
try(PreparedStatement ps=c.prepareStatement("""
|
||||||
|
SELECT shine_refresh_channel_follow_stats(owner_bch_name,channel_root_block_number,channel_root_block_hash)
|
||||||
|
FROM channel_names_state WHERE LOWER(owner_login)=LOWER(?) AND channel_type_code=1
|
||||||
|
""")){ps.setString(1,login);ps.execute();}
|
||||||
|
|
||||||
|
ChannelReadStateDAO.getInstance().recalculateUnreadForOwnedChannels(c,login,System.currentTimeMillis());
|
||||||
|
c.commit();
|
||||||
|
}catch(Exception e){c.rollback();if(e instanceof SQLException x)throw x;throw new SQLException("canonical replay finalize failed",e);}finally{c.setAutoCommit(ac);}}
|
||||||
|
}
|
||||||
|
|
||||||
private void recountAffectedLikes(Connection c)throws SQLException{
|
private void recountAffectedLikes(Connection c)throws SQLException{
|
||||||
try(PreparedStatement ps=c.prepareStatement("""
|
try(PreparedStatement ps=c.prepareStatement("""
|
||||||
UPDATE message_stats ms SET
|
UPDATE message_stats ms SET
|
||||||
@@ -104,6 +265,19 @@ public final class BlockchainResyncCleanupDAO {
|
|||||||
""")){ps.executeUpdate();}
|
""")){ps.executeUpdate();}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void recountAffectedReplies(Connection c)throws SQLException{
|
||||||
|
try(PreparedStatement ps=c.prepareStatement("""
|
||||||
|
UPDATE message_stats ms SET replies_count=(
|
||||||
|
SELECT COUNT(*)::INTEGER FROM blocks_store b
|
||||||
|
WHERE b.msg_type=1 AND b.msg_sub_type=20
|
||||||
|
AND LOWER(b.to_login)=LOWER(ms.to_login)
|
||||||
|
AND b.to_block_number=ms.to_block_number AND b.to_block_hash=ms.to_block_hash
|
||||||
|
)
|
||||||
|
WHERE EXISTS(SELECT 1 FROM shine_cleanup_reply_targets t
|
||||||
|
WHERE LOWER(t.to_login)=LOWER(ms.to_login) AND t.to_block_number=ms.to_block_number AND t.to_block_hash=ms.to_block_hash)
|
||||||
|
""")){ps.executeUpdate();}
|
||||||
|
}
|
||||||
|
|
||||||
private void refreshAffectedChannelStats(Connection c)throws SQLException{
|
private void refreshAffectedChannelStats(Connection c)throws SQLException{
|
||||||
try(PreparedStatement q=c.prepareStatement("SELECT owner_bch_name,root_number,root_hash FROM shine_cleanup_channels"); ResultSet rs=q.executeQuery(); PreparedStatement p=c.prepareStatement("SELECT shine_refresh_channel_follow_stats(?,?,?)")){
|
try(PreparedStatement q=c.prepareStatement("SELECT owner_bch_name,root_number,root_hash FROM shine_cleanup_channels"); ResultSet rs=q.executeQuery(); PreparedStatement p=c.prepareStatement("SELECT shine_refresh_channel_follow_stats(?,?,?)")){
|
||||||
while(rs.next()){p.setString(1,rs.getString(1));p.setInt(2,rs.getInt(2));p.setBytes(3,rs.getBytes(3));p.execute();}
|
while(rs.next()){p.setString(1,rs.getString(1));p.setInt(2,rs.getInt(2));p.setBytes(3,rs.getBytes(3));p.execute();}
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package shine.db.dao;
|
||||||
|
|
||||||
|
import shine.db.DbController;
|
||||||
|
import shine.db.entities.BlockEntry;
|
||||||
|
import java.sql.*;
|
||||||
|
|
||||||
|
/** Resolves a logical block number to the physical fork that owns it in a selected canonical history. */
|
||||||
|
public final class CanonicalChainResolver {
|
||||||
|
private static volatile CanonicalChainResolver instance;
|
||||||
|
private final DbController db=DbController.getInstance();
|
||||||
|
private final BlocksDAO blocks=BlocksDAO.getInstance();
|
||||||
|
private final BlockchainForkSegmentsDAO segments=BlockchainForkSegmentsDAO.getInstance();
|
||||||
|
private CanonicalChainResolver(){}
|
||||||
|
public static CanonicalChainResolver getInstance(){if(instance==null)synchronized(CanonicalChainResolver.class){if(instance==null)instance=new CanonicalChainResolver();}return instance;}
|
||||||
|
public BlockEntry getBlock(String blockchainName,int blockNumber)throws SQLException{try(Connection c=db.getConnection()){return getBlock(c,blockchainName,blockNumber);}}
|
||||||
|
public BlockEntry getBlock(Connection c,String blockchainName,int blockNumber)throws SQLException{
|
||||||
|
ChainRef r=ref(blockchainName); int physical=resolveFork(c,r.login,r.fork,blockNumber); return blocks.getByNumber(c,r.login+"-"+String.format("%03d",physical),blockNumber);
|
||||||
|
}
|
||||||
|
public byte[] getHash(String blockchainName,int blockNumber)throws SQLException{BlockEntry b=getBlock(blockchainName,blockNumber);return b==null?null:b.getBlockHash();}
|
||||||
|
public byte[] getHash(Connection c,String blockchainName,int blockNumber)throws SQLException{BlockEntry b=getBlock(c,blockchainName,blockNumber);return b==null?null:b.getBlockHash();}
|
||||||
|
public int resolveFork(Connection c,String login,int activeFork,int blockNumber)throws SQLException{
|
||||||
|
if(blockNumber<0)throw new IllegalArgumentException("blockNumber < 0"); int f=activeFork; int guard=0;
|
||||||
|
while(true){ if(++guard>1000)throw new SQLException("fork ancestry cycle for "+login); BlockchainForkSegmentsDAO.Segment s=segments.get(c,login,f);
|
||||||
|
if(s==null){ // legacy self-contained fork
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
if(s.parentForkNumber()==null || blockNumber>=s.firstPhysicalBlockNumber()) return f;
|
||||||
|
f=s.parentForkNumber();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public BlockEntry getLastLineBlock(Connection c,String blockchainName,int msgType,int lineCode,int beforeBlockExclusive)throws SQLException{
|
||||||
|
ChainRef r=ref(blockchainName); for(int n=beforeBlockExclusive-1;n>=0;n--){ BlockEntry b=getBlock(c,blockchainName,n); if(b!=null && b.getMsgType()==msgType && b.getLineCode()!=null && b.getLineCode()==lineCode)return b; } return null;
|
||||||
|
}
|
||||||
|
private record ChainRef(String login,int fork){}
|
||||||
|
private static ChainRef ref(String n){int d=n==null?-1:n.lastIndexOf('-');if(d<=0||n.length()-d-1!=3)throw new IllegalArgumentException("Bad blockchainName: "+n);return new ChainRef(n.substring(0,d),Integer.parseInt(n.substring(d+1)));}
|
||||||
|
}
|
||||||
@@ -28,11 +28,11 @@ public final class ChannelNameStateDAO {
|
|||||||
String sql = """
|
String sql = """
|
||||||
SELECT 1
|
SELECT 1
|
||||||
FROM channel_names_state
|
FROM channel_names_state
|
||||||
WHERE owner_bch_name = ? AND channel_type_code = ? AND slug = ?
|
WHERE LOWER(owner_login) = LOWER(?) AND channel_type_code = ? AND slug = ?
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""";
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, ownerBchName);
|
ps.setString(1, loginOf(ownerBchName));
|
||||||
ps.setInt(2, channelTypeCode);
|
ps.setInt(2, channelTypeCode);
|
||||||
ps.setString(3, slug);
|
ps.setString(3, slug);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
@@ -100,7 +100,7 @@ public final class ChannelNameStateDAO {
|
|||||||
ava_sha256 = ?,
|
ava_sha256 = ?,
|
||||||
ava_size = ?,
|
ava_size = ?,
|
||||||
meta_updated_at_ms = ?
|
meta_updated_at_ms = ?
|
||||||
WHERE owner_bch_name = ? AND channel_root_block_number = ?
|
WHERE LOWER(owner_login)=LOWER(?) AND channel_root_block_number = ?
|
||||||
""";
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, entry.getDisplayName() == null ? "" : entry.getDisplayName());
|
ps.setString(1, entry.getDisplayName() == null ? "" : entry.getDisplayName());
|
||||||
@@ -109,7 +109,7 @@ public final class ChannelNameStateDAO {
|
|||||||
ps.setString(4, entry.getAvaSha256() == null ? "" : entry.getAvaSha256());
|
ps.setString(4, entry.getAvaSha256() == null ? "" : entry.getAvaSha256());
|
||||||
ps.setLong(5, Math.max(0L, entry.getAvaSize()));
|
ps.setLong(5, Math.max(0L, entry.getAvaSize()));
|
||||||
ps.setLong(6, Math.max(0L, entry.getMetaUpdatedAtMs()));
|
ps.setLong(6, Math.max(0L, entry.getMetaUpdatedAtMs()));
|
||||||
ps.setString(7, entry.getOwnerBlockchainName());
|
ps.setString(7, entry.getOwnerLogin());
|
||||||
ps.setInt(8, entry.getChannelRootBlockNumber());
|
ps.setInt(8, entry.getChannelRootBlockNumber());
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
}
|
}
|
||||||
@@ -121,9 +121,9 @@ public final class ChannelNameStateDAO {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
public String getSlugByOwnerAndRoot(String ownerBchName, int rootBlockNumber) throws SQLException {
|
public String getSlugByOwnerAndRoot(String ownerBchName, int rootBlockNumber) throws SQLException {
|
||||||
String sql = "SELECT slug FROM channel_names_state WHERE owner_bch_name=? AND channel_root_block_number=? LIMIT 1";
|
String sql = "SELECT slug FROM channel_names_state WHERE LOWER(owner_login)=LOWER(?) AND channel_root_block_number=? LIMIT 1";
|
||||||
try (Connection c = db.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
|
try (Connection c = db.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, ownerBchName);
|
ps.setString(1, loginOf(ownerBchName));
|
||||||
ps.setInt(2, rootBlockNumber);
|
ps.setInt(2, rootBlockNumber);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
return rs.next() ? rs.getString(1) : null;
|
return rs.next() ? rs.getString(1) : null;
|
||||||
@@ -131,4 +131,5 @@ public final class ChannelNameStateDAO {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String loginOf(String bch){ if(bch==null)return ""; int d=bch.lastIndexOf('-'); return d>0?bch.substring(0,d):bch; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ public final class ChannelReadStateDAO {
|
|||||||
AND LOWER(cs.to_login)=LOWER(cns.owner_login)
|
AND LOWER(cs.to_login)=LOWER(cns.owner_login)
|
||||||
AND COALESCE(cs.to_block_number,0)=cns.channel_root_block_number
|
AND COALESCE(cs.to_block_number,0)=cns.channel_root_block_number
|
||||||
AND cs.to_block_hash=cns.channel_root_block_hash
|
AND cs.to_block_hash=cns.channel_root_block_hash
|
||||||
WHERE cns.owner_bch_name=?
|
WHERE LOWER(cns.owner_login)=LOWER(?)
|
||||||
AND cns.slug=?
|
AND cns.slug=?
|
||||||
AND cns.channel_type_code=1
|
AND cns.channel_type_code=1
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
@@ -112,7 +112,7 @@ public final class ChannelReadStateDAO {
|
|||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, viewerLogin);
|
ps.setString(1, viewerLogin);
|
||||||
ps.setInt(2, followRelType);
|
ps.setInt(2, followRelType);
|
||||||
ps.setString(3, ownerBchName);
|
ps.setString(3, loginFromChainName(ownerBchName));
|
||||||
ps.setString(4, channelName);
|
ps.setString(4, channelName);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
return rs.next();
|
return rs.next();
|
||||||
@@ -124,11 +124,11 @@ public final class ChannelReadStateDAO {
|
|||||||
String sql = """
|
String sql = """
|
||||||
SELECT slug
|
SELECT slug
|
||||||
FROM channel_names_state
|
FROM channel_names_state
|
||||||
WHERE owner_bch_name=? AND channel_root_block_number=? AND channel_type_code=1
|
WHERE LOWER(owner_login)=LOWER(?) AND channel_root_block_number=? AND channel_type_code=1
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""";
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, ownerBchName);
|
ps.setString(1, loginFromChainName(ownerBchName));
|
||||||
ps.setInt(2, rootBlockNumber);
|
ps.setInt(2, rootBlockNumber);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
return rs.next() ? rs.getString("slug") : null;
|
return rs.next() ? rs.getString("slug") : null;
|
||||||
@@ -141,11 +141,11 @@ public final class ChannelReadStateDAO {
|
|||||||
String sql = """
|
String sql = """
|
||||||
SELECT channel_root_block_number
|
SELECT channel_root_block_number
|
||||||
FROM channel_names_state
|
FROM channel_names_state
|
||||||
WHERE owner_bch_name=? AND slug=? AND channel_type_code=1
|
WHERE LOWER(owner_login)=LOWER(?) AND slug=? AND channel_type_code=1
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""";
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, ownerBchName);
|
ps.setString(1, loginFromChainName(ownerBchName));
|
||||||
ps.setString(2, channelName);
|
ps.setString(2, channelName);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
return rs.next() ? rs.getInt("channel_root_block_number") : null;
|
return rs.next() ? rs.getInt("channel_root_block_number") : null;
|
||||||
@@ -157,11 +157,11 @@ public final class ChannelReadStateDAO {
|
|||||||
String sql = """
|
String sql = """
|
||||||
SELECT owner_login
|
SELECT owner_login
|
||||||
FROM channel_names_state
|
FROM channel_names_state
|
||||||
WHERE owner_bch_name=? AND slug=? AND channel_type_code=1
|
WHERE LOWER(owner_login)=LOWER(?) AND slug=? AND channel_type_code=1
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""";
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, ownerBchName);
|
ps.setString(1, loginFromChainName(ownerBchName));
|
||||||
ps.setString(2, channelName);
|
ps.setString(2, channelName);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
return rs.next() ? rs.getString("owner_login") : null;
|
return rs.next() ? rs.getString("owner_login") : null;
|
||||||
@@ -210,7 +210,7 @@ public final class ChannelReadStateDAO {
|
|||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, viewerLogin);
|
ps.setString(1, viewerLogin);
|
||||||
ps.setString(2, ownerLogin);
|
ps.setString(2, ownerLogin);
|
||||||
ps.setString(3, ownerBchName);
|
ps.setString(3, loginFromChainName(ownerBchName));
|
||||||
ps.setString(4, channelName);
|
ps.setString(4, channelName);
|
||||||
ps.setLong(5, Math.max(0L, readCount));
|
ps.setLong(5, Math.max(0L, readCount));
|
||||||
ps.setLong(6, readUpdatedAtMs);
|
ps.setLong(6, readUpdatedAtMs);
|
||||||
@@ -296,6 +296,38 @@ public final class ChannelReadStateDAO {
|
|||||||
return ps.executeUpdate() > 0;
|
return ps.executeUpdate() > 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Recomputes server-owned unread counters for all public channels owned by one logical login.
|
||||||
|
* Signed read_count/read signature fields are preserved exactly. Safe to call repeatedly after replay.
|
||||||
|
*/
|
||||||
|
public int recalculateUnreadForOwnedChannels(Connection c, String ownerLogin, long nowMs) throws SQLException {
|
||||||
|
String sql = """
|
||||||
|
WITH counts AS (
|
||||||
|
SELECT cns.owner_login, cns.slug, COUNT(b.block_number)::BIGINT AS message_count
|
||||||
|
FROM channel_names_state cns
|
||||||
|
LEFT JOIN blocks_store b
|
||||||
|
ON LOWER(b.login)=LOWER(cns.owner_login)
|
||||||
|
AND b.line_code=cns.channel_root_block_number
|
||||||
|
AND b.msg_type=1
|
||||||
|
AND b.msg_sub_type IN (10,50,100,110,120,130)
|
||||||
|
WHERE LOWER(cns.owner_login)=LOWER(?)
|
||||||
|
AND cns.channel_type_code=1
|
||||||
|
GROUP BY cns.owner_login,cns.slug
|
||||||
|
)
|
||||||
|
UPDATE channel_read_state crs
|
||||||
|
SET unread_count=GREATEST(0, counts.message_count-crs.read_count),
|
||||||
|
updated_at_ms=?
|
||||||
|
FROM counts
|
||||||
|
WHERE LOWER(crs.owner_login)=LOWER(counts.owner_login)
|
||||||
|
AND crs.channel_name=counts.slug
|
||||||
|
""";
|
||||||
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
|
ps.setString(1, ownerLogin);
|
||||||
|
ps.setLong(2, nowMs);
|
||||||
|
return ps.executeUpdate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static String loginFromChainName(String bch){ if(bch==null)return null; int d=bch.lastIndexOf('-'); return d>0?bch.substring(0,d):bch; }
|
private static String loginFromChainName(String bch){ if(bch==null)return null; int d=bch.lastIndexOf('-'); return d>0?bch.substring(0,d):bch; }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
-- SHiNE PostgreSQL database format v2 / runtime schema revision 29
|
-- SHiNE PostgreSQL database format v2 / runtime schema revision 30
|
||||||
-- Stage 2 baseline. This file is only for a NEW, EMPTY database.
|
-- Stage 2 baseline. This file is only for a NEW, EMPTY database.
|
||||||
-- Дата: 2026-09-29
|
-- Дата: 2026-09-29
|
||||||
--
|
--
|
||||||
@@ -572,11 +572,19 @@ CREATE TABLE IF NOT EXISTS arweave_block_import_queue (
|
|||||||
status TEXT NOT NULL,
|
status TEXT NOT NULL,
|
||||||
last_error TEXT NOT NULL DEFAULT '',
|
last_error TEXT NOT NULL DEFAULT '',
|
||||||
first_seen_at_ms BIGINT NOT NULL,
|
first_seen_at_ms BIGINT NOT NULL,
|
||||||
updated_at_ms BIGINT NOT NULL
|
updated_at_ms BIGINT NOT NULL,
|
||||||
|
parsed_login TEXT,
|
||||||
|
parsed_fork_number INTEGER CHECK (parsed_fork_number IS NULL OR parsed_fork_number BETWEEN 1 AND 999),
|
||||||
|
parsed_block_number INTEGER CHECK (parsed_block_number IS NULL OR parsed_block_number >= 0),
|
||||||
|
parsed_block_hash BYTEA,
|
||||||
|
metadata_validated BOOLEAN NOT NULL DEFAULT FALSE
|
||||||
);
|
);
|
||||||
CREATE INDEX IF NOT EXISTS idx_arweave_block_import_queue_pending
|
CREATE INDEX IF NOT EXISTS idx_arweave_block_import_queue_pending
|
||||||
ON arweave_block_import_queue(block_height,first_seen_at_ms)
|
ON arweave_block_import_queue(block_height,first_seen_at_ms)
|
||||||
WHERE status='PENDING';
|
WHERE status='PENDING';
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_arweave_block_import_queue_canonical_order
|
||||||
|
ON arweave_block_import_queue(parsed_login,parsed_block_number,parsed_fork_number,first_seen_at_ms)
|
||||||
|
WHERE status='PENDING' AND metadata_validated=TRUE;
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS connections_state (
|
CREATE TABLE IF NOT EXISTS connections_state (
|
||||||
login TEXT NOT NULL REFERENCES solana_user_pda_current(normalized_login),
|
login TEXT NOT NULL REFERENCES solana_user_pda_current(normalized_login),
|
||||||
@@ -2548,8 +2556,105 @@ CREATE TRIGGER trg_blocks_message_stats_reply_ai AFTER INSERT ON blocks_store FO
|
|||||||
DROP TRIGGER IF EXISTS trg_blocks_edit_apply_ai ON blocks_store;
|
DROP TRIGGER IF EXISTS trg_blocks_edit_apply_ai ON blocks_store;
|
||||||
CREATE TRIGGER trg_blocks_edit_apply_ai AFTER INSERT ON blocks_store FOR EACH ROW EXECUTE FUNCTION shine_blocks_edit_apply_ai();
|
CREATE TRIGGER trg_blocks_edit_apply_ai AFTER INSERT ON blocks_store FOR EACH ROW EXECUTE FUNCTION shine_blocks_edit_apply_ai();
|
||||||
|
|
||||||
INSERT INTO db_schema_version(id,schema_version,updated_at_ms)
|
-- Segmented fork ancestry (schema v30).
|
||||||
VALUES(1,29,CAST(EXTRACT(EPOCH FROM clock_timestamp())*1000 AS BIGINT))
|
|
||||||
ON CONFLICT(id) DO UPDATE SET schema_version=EXCLUDED.schema_version, updated_at_ms=EXCLUDED.updated_at_ms;
|
|
||||||
|
CREATE TABLE IF NOT EXISTS blockchain_fork_segments (
|
||||||
|
login TEXT NOT NULL,
|
||||||
|
fork_number INTEGER NOT NULL CHECK (fork_number BETWEEN 1 AND 999),
|
||||||
|
parent_fork_number INTEGER,
|
||||||
|
fork_point_block_number INTEGER NOT NULL DEFAULT -1,
|
||||||
|
fork_point_block_hash BYTEA,
|
||||||
|
first_physical_block_number INTEGER NOT NULL DEFAULT 0,
|
||||||
|
created_at_ms BIGINT NOT NULL DEFAULT 0,
|
||||||
|
PRIMARY KEY(login,fork_number),
|
||||||
|
CHECK ((fork_number=1 AND parent_fork_number IS NULL AND fork_point_block_number=-1 AND first_physical_block_number=0)
|
||||||
|
OR (fork_number>1 AND parent_fork_number IS NOT NULL AND parent_fork_number BETWEEN 1 AND 999
|
||||||
|
AND fork_point_block_number>=0 AND octet_length(fork_point_block_hash)=32
|
||||||
|
AND first_physical_block_number=fork_point_block_number+1))
|
||||||
|
);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_blockchain_fork_segments_parent ON blockchain_fork_segments(login,parent_fork_number);
|
||||||
|
|
||||||
|
-- Existing chains predate segmented forks. Their current physical fork is self-contained;
|
||||||
|
-- register fork 1 where possible. Later segmented forks are inserted by key rotation/recovery.
|
||||||
|
INSERT INTO blockchain_fork_segments(login,fork_number,parent_fork_number,fork_point_block_number,fork_point_block_hash,first_physical_block_number,created_at_ms)
|
||||||
|
SELECT login,fork_number,NULL,-1,NULL,0,updated_at_ms FROM blockchain_state_store
|
||||||
|
WHERE fork_number=1
|
||||||
|
ON CONFLICT(login,fork_number) DO NOTHING;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- Segmented-fork canonical helpers/triggers (schema v30 final overrides).
|
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION shine_canonical_fork(p_login TEXT,p_active_fork INTEGER,p_block_number INTEGER) RETURNS INTEGER LANGUAGE plpgsql STABLE AS $$
|
||||||
|
DECLARE f INTEGER:=p_active_fork; seg RECORD; guard INTEGER:=0;
|
||||||
|
BEGIN
|
||||||
|
LOOP
|
||||||
|
guard:=guard+1; IF guard>1000 THEN RAISE EXCEPTION 'fork ancestry cycle'; END IF;
|
||||||
|
SELECT * INTO seg FROM blockchain_fork_segments WHERE LOWER(login)=LOWER(p_login) AND fork_number=f;
|
||||||
|
IF NOT FOUND THEN RETURN f; END IF;
|
||||||
|
IF seg.parent_fork_number IS NULL OR p_block_number>=seg.first_physical_block_number THEN RETURN f; END IF;
|
||||||
|
f:=seg.parent_fork_number;
|
||||||
|
END LOOP;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION shine_blocks_line_integrity_bi() RETURNS TRIGGER LANGUAGE plpgsql AS $$
|
||||||
|
DECLARE prev_block RECORD; tail_block RECORD; prev_fork INTEGER;
|
||||||
|
BEGIN
|
||||||
|
IF NEW.line_code IS NULL AND NEW.prev_line_number IS NULL AND NEW.prev_line_hash IS NULL AND NEW.this_line_number IS NULL THEN RETURN NEW; END IF;
|
||||||
|
IF NEW.msg_type NOT IN (0,1,3,4) THEN RAISE EXCEPTION 'LINE_ERR_UNSUPPORTED_TYPE_WITH_LINE'; END IF;
|
||||||
|
IF NEW.line_code IS NULL OR NEW.prev_line_number IS NULL OR NEW.prev_line_hash IS NULL OR NEW.this_line_number IS NULL THEN RAISE EXCEPTION 'LINE_ERR_PARTIAL_FIELDS'; END IF;
|
||||||
|
prev_fork:=shine_canonical_fork(NEW.login,NEW.fork_number,NEW.prev_line_number);
|
||||||
|
SELECT * INTO prev_block FROM blocks_store WHERE LOWER(login)=LOWER(NEW.login) AND fork_number=prev_fork AND block_number=NEW.prev_line_number LIMIT 1;
|
||||||
|
IF NOT FOUND THEN RAISE EXCEPTION 'LINE_ERR_NO_PREV'; END IF;
|
||||||
|
IF prev_block.block_hash IS DISTINCT FROM NEW.prev_line_hash THEN RAISE EXCEPTION 'LINE_ERR_PREV_HASH_MISMATCH'; END IF;
|
||||||
|
IF NEW.prev_line_number<>NEW.line_code AND prev_block.line_code IS DISTINCT FROM NEW.line_code THEN RAISE EXCEPTION 'LINE_ERR_LINE_CODE_MISMATCH'; END IF;
|
||||||
|
SELECT b.* INTO tail_block FROM blocks_store b
|
||||||
|
WHERE LOWER(b.login)=LOWER(NEW.login) AND b.block_number<NEW.block_number AND b.msg_type=NEW.msg_type AND b.line_code=NEW.line_code
|
||||||
|
AND b.fork_number=shine_canonical_fork(NEW.login,NEW.fork_number,b.block_number)
|
||||||
|
ORDER BY b.block_number DESC LIMIT 1;
|
||||||
|
IF NOT FOUND THEN
|
||||||
|
IF NEW.prev_line_number<>NEW.line_code THEN RAISE EXCEPTION 'LINE_ERR_NOT_CURRENT_TAIL'; END IF;
|
||||||
|
IF NEW.this_line_number<>(CASE WHEN NEW.msg_type=1 THEN 0 ELSE 1 END) THEN RAISE EXCEPTION 'LINE_ERR_FIRST_STEP_BAD_THIS'; END IF;
|
||||||
|
RETURN NEW;
|
||||||
|
END IF;
|
||||||
|
IF NEW.prev_line_number<>tail_block.block_number THEN RAISE EXCEPTION 'LINE_ERR_NOT_CURRENT_TAIL'; END IF;
|
||||||
|
IF prev_block.this_line_number IS NULL THEN RAISE EXCEPTION 'LINE_ERR_THIS_LINE_BAD_STEP'; END IF;
|
||||||
|
IF NEW.this_line_number<>prev_block.this_line_number+1 THEN RAISE EXCEPTION 'LINE_ERR_THIS_LINE_BAD_STEP'; END IF;
|
||||||
|
RETURN NEW;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION shine_blocks_edit_apply_ai() RETURNS TRIGGER LANGUAGE plpgsql AS $$
|
||||||
|
DECLARE target_fork INTEGER;
|
||||||
|
BEGIN
|
||||||
|
IF NEW.msg_type<>1 OR NEW.msg_sub_type NOT IN (11,21) THEN RETURN NEW; END IF;
|
||||||
|
IF NEW.to_block_number IS NULL OR NEW.to_block_hash IS NULL THEN RETURN NEW; END IF;
|
||||||
|
target_fork:=shine_canonical_fork(NEW.login,NEW.fork_number,NEW.to_block_number);
|
||||||
|
UPDATE blocks_store SET edited_by_block_number=NEW.block_number
|
||||||
|
WHERE LOWER(login)=LOWER(NEW.login) AND fork_number=target_fork AND block_number=NEW.to_block_number AND block_hash=NEW.to_block_hash;
|
||||||
|
RETURN NEW;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS key_rotation_rebuild_staging (
|
||||||
|
rotation_session_id BIGINT NOT NULL REFERENCES key_rotation_sessions(id) ON DELETE CASCADE,
|
||||||
|
logical_block_number INTEGER NOT NULL, physical_fork_number INTEGER NOT NULL,
|
||||||
|
blockchain_key TEXT NOT NULL, block_bytes BYTEA NOT NULL, block_hash BYTEA NOT NULL,
|
||||||
|
PRIMARY KEY(rotation_session_id,logical_block_number)
|
||||||
|
);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_key_rotation_rebuild_staging_session ON key_rotation_rebuild_staging(rotation_session_id,logical_block_number);
|
||||||
|
|
||||||
|
-- Generic crash-safe staging used when this server learns a remote fork through Arweave.
|
||||||
|
CREATE TABLE IF NOT EXISTS canonical_rebuild_staging (
|
||||||
|
login TEXT NOT NULL,
|
||||||
|
logical_block_number INTEGER NOT NULL,
|
||||||
|
physical_fork_number INTEGER NOT NULL,
|
||||||
|
block_bytes BYTEA NOT NULL,
|
||||||
|
block_hash BYTEA NOT NULL,
|
||||||
|
PRIMARY KEY(login,logical_block_number)
|
||||||
|
);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_canonical_rebuild_staging_login ON canonical_rebuild_staging(login,logical_block_number);
|
||||||
|
|
||||||
|
INSERT INTO db_schema_version(id,schema_version,updated_at_ms) VALUES(1,30,CAST(EXTRACT(EPOCH FROM clock_timestamp())*1000 AS BIGINT)) ON CONFLICT(id) DO UPDATE SET schema_version=EXCLUDED.schema_version,updated_at_ms=EXCLUDED.updated_at_ms;
|
||||||
|
|
||||||
COMMIT;
|
COMMIT;
|
||||||
|
|||||||
+54
-40
@@ -31,11 +31,11 @@ import server.logic.ws_protocol.JSON.handlers.channels.ChannelMetaTextParser;
|
|||||||
import server.logic.ws_protocol.JSON.handlers.channels.ChannelNamesStateBootstrapper;
|
import server.logic.ws_protocol.JSON.handlers.channels.ChannelNamesStateBootstrapper;
|
||||||
import server.logic.ws_protocol.JSON.handlers.channels.UserCountersSupport;
|
import server.logic.ws_protocol.JSON.handlers.channels.UserCountersSupport;
|
||||||
import server.logic.ws_protocol.WireCodes;
|
import server.logic.ws_protocol.WireCodes;
|
||||||
import server.sync.AddBlockSyncService;
|
|
||||||
import server.sync.ArweavePublishSignal;
|
import server.sync.ArweavePublishSignal;
|
||||||
import shine.db.channels.ChannelNameRules;
|
import shine.db.channels.ChannelNameRules;
|
||||||
import shine.db.dao.BlockchainStateDAO;
|
import shine.db.dao.BlockchainStateDAO;
|
||||||
import shine.db.dao.BlocksDAO;
|
import shine.db.dao.BlocksDAO;
|
||||||
|
import shine.db.dao.CanonicalChainResolver;
|
||||||
import shine.db.dao.ChannelNameStateDAO;
|
import shine.db.dao.ChannelNameStateDAO;
|
||||||
import shine.db.dao.UserNotificationsStateDAO;
|
import shine.db.dao.UserNotificationsStateDAO;
|
||||||
import shine.db.dao.UserParamsDAO;
|
import shine.db.dao.UserParamsDAO;
|
||||||
@@ -66,10 +66,10 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
private static final Logger log = LoggerFactory.getLogger(Net_AddBlock_Handler.class);
|
private static final Logger log = LoggerFactory.getLogger(Net_AddBlock_Handler.class);
|
||||||
|
|
||||||
private final BlocksDAO blocksDAO = BlocksDAO.getInstance();
|
private final BlocksDAO blocksDAO = BlocksDAO.getInstance();
|
||||||
|
private final CanonicalChainResolver canonicalChain = CanonicalChainResolver.getInstance();
|
||||||
private final BlockchainStateDAO stateDAO = BlockchainStateDAO.getInstance();
|
private final BlockchainStateDAO stateDAO = BlockchainStateDAO.getInstance();
|
||||||
private final UserParamsDAO userParamsDAO = UserParamsDAO.getInstance();
|
private final UserParamsDAO userParamsDAO = UserParamsDAO.getInstance();
|
||||||
private final ChannelNameStateDAO channelNameStateDAO = ChannelNameStateDAO.getInstance();
|
private final ChannelNameStateDAO channelNameStateDAO = ChannelNameStateDAO.getInstance();
|
||||||
private final AddBlockSyncService addBlockSyncService = new AddBlockSyncService();
|
|
||||||
|
|
||||||
private final BlockchainWriter dbWriter = new BlockchainWriter(
|
private final BlockchainWriter dbWriter = new BlockchainWriter(
|
||||||
blocksDAO, stateDAO, userParamsDAO, channelNameStateDAO, UserNotificationsStateDAO.getInstance());
|
blocksDAO, stateDAO, userParamsDAO, channelNameStateDAO, UserNotificationsStateDAO.getInstance());
|
||||||
@@ -120,7 +120,8 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
req.getPrevBlockHash(), // старое поле, пока оставляем
|
req.getPrevBlockHash(), // старое поле, пока оставляем
|
||||||
req.getBlockBytesB64(),
|
req.getBlockBytesB64(),
|
||||||
true,
|
true,
|
||||||
true
|
true,
|
||||||
|
false
|
||||||
);
|
);
|
||||||
|
|
||||||
// ✅ УСПЕХ: как раньше
|
// ✅ УСПЕХ: как раньше
|
||||||
@@ -238,7 +239,8 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
String prevGlobalHashHexFromReq,
|
String prevGlobalHashHexFromReq,
|
||||||
String blockBytesB64,
|
String blockBytesB64,
|
||||||
boolean replicateAfterWrite,
|
boolean replicateAfterWrite,
|
||||||
boolean publishToArweave
|
boolean publishToArweave,
|
||||||
|
boolean replayMode
|
||||||
) {
|
) {
|
||||||
if (blockchainName == null || blockchainName.isBlank()) {
|
if (blockchainName == null || blockchainName.isBlank()) {
|
||||||
log.warn("AddBlock: пустой blockchainName (reqGlobalNumber={})", globalNumberFromReq);
|
log.warn("AddBlock: пустой blockchainName (reqGlobalNumber={})", globalNumberFromReq);
|
||||||
@@ -367,7 +369,6 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
|
|
||||||
ChannelNameStateEntry channelNameStateEntry = null;
|
ChannelNameStateEntry channelNameStateEntry = null;
|
||||||
ChannelNameStateEntry channelMetaUpdateEntry = null;
|
ChannelNameStateEntry channelMetaUpdateEntry = null;
|
||||||
Chat200CreateSeed chat200CreateSeed = null;
|
|
||||||
if (block.body instanceof CreateChannelBody createChannelBody) {
|
if (block.body instanceof CreateChannelBody createChannelBody) {
|
||||||
int channelTypeCode = Short.toUnsignedInt(createChannelBody.channelTypeCode);
|
int channelTypeCode = Short.toUnsignedInt(createChannelBody.channelTypeCode);
|
||||||
int channelTypeVersion = Short.toUnsignedInt(createChannelBody.channelTypeVersion);
|
int channelTypeVersion = Short.toUnsignedInt(createChannelBody.channelTypeVersion);
|
||||||
@@ -430,17 +431,7 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
channelNameStateEntry.setChannelRootBlockHash(block.getHash32());
|
channelNameStateEntry.setChannelRootBlockHash(block.getHash32());
|
||||||
channelNameStateEntry.setCreatedAtMs(block.timestamp * 1000L);
|
channelNameStateEntry.setCreatedAtMs(block.timestamp * 1000L);
|
||||||
|
|
||||||
if (channelTypeCode == (CreateChannelBody.CHANNEL_TYPE_GROUP & 0xFFFF)) {
|
// Chat200 group projection intentionally retired; legacy helpers remain @Deprecated only.
|
||||||
chat200CreateSeed = new Chat200CreateSeed();
|
|
||||||
chat200CreateSeed.ownerLogin = login;
|
|
||||||
chat200CreateSeed.ownerBch = blockchainName;
|
|
||||||
chat200CreateSeed.rootBlockNumber = block.blockNumber;
|
|
||||||
chat200CreateSeed.rootBlockHash = block.getHash32();
|
|
||||||
chat200CreateSeed.channelName = normalizedName;
|
|
||||||
chat200CreateSeed.channelTypeVersion = channelTypeVersion;
|
|
||||||
chat200CreateSeed.chatTitle = channelNameStateEntry.getChannelDescription();
|
|
||||||
chat200CreateSeed.updatedAtMs = block.timestamp * 1000L;
|
|
||||||
}
|
|
||||||
} else if ((block.type & 0xFFFF) == 1
|
} else if ((block.type & 0xFFFF) == 1
|
||||||
&& (block.subType & 0xFFFF) == (MsgSubType.TEXT_CHANNEL_META & 0xFFFF)
|
&& (block.subType & 0xFFFF) == (MsgSubType.TEXT_CHANNEL_META & 0xFFFF)
|
||||||
&& block.body instanceof TextLineBody metaBody) {
|
&& block.body instanceof TextLineBody metaBody) {
|
||||||
@@ -466,6 +457,7 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
return new AddBlockResult(WireCodes.Status.BAD_REQUEST, e.getMessage(), serverLastNum, serverLastHashHex);
|
return new AddBlockResult(WireCodes.Status.BAD_REQUEST, e.getMessage(), serverLastNum, serverLastHashHex);
|
||||||
}
|
}
|
||||||
channelMetaUpdateEntry = new ChannelNameStateEntry();
|
channelMetaUpdateEntry = new ChannelNameStateEntry();
|
||||||
|
channelMetaUpdateEntry.setOwnerLogin(login);
|
||||||
channelMetaUpdateEntry.setOwnerBlockchainName(blockchainName);
|
channelMetaUpdateEntry.setOwnerBlockchainName(blockchainName);
|
||||||
channelMetaUpdateEntry.setChannelRootBlockNumber(lineCode);
|
channelMetaUpdateEntry.setChannelRootBlockNumber(lineCode);
|
||||||
channelMetaUpdateEntry.setDisplayName(parsedMeta.title());
|
channelMetaUpdateEntry.setDisplayName(parsedMeta.title());
|
||||||
@@ -559,7 +551,7 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
try {
|
try {
|
||||||
byte[] expectedBack32 = new byte[32];
|
byte[] expectedBack32 = new byte[32];
|
||||||
if (block.blockNumber >= 32) {
|
if (block.blockNumber >= 32) {
|
||||||
byte[] storedBack32 = blocksDAO.getHashByNumber(blockchainName, block.blockNumber - 32);
|
byte[] storedBack32 = canonicalChain.getHash(blockchainName, block.blockNumber - 32);
|
||||||
if (storedBack32 == null || storedBack32.length != 32) {
|
if (storedBack32 == null || storedBack32.length != 32) {
|
||||||
log.error("AddBlock: missing N-32 block for back32 validation (blockchainName={}, blockNumber={}, target={})",
|
log.error("AddBlock: missing N-32 block for back32 validation (blockchainName={}, blockNumber={}, target={})",
|
||||||
blockchainName, block.blockNumber, block.blockNumber - 32);
|
blockchainName, block.blockNumber, block.blockNumber - 32);
|
||||||
@@ -666,7 +658,7 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
String targetLogin = t.toLogin();
|
String targetLogin = t.toLogin();
|
||||||
Integer targetForkNumber = t.toForkNumber();
|
Integer targetForkNumber = t.toForkNumber();
|
||||||
if (isTextEdit) {
|
if (isTextEdit) {
|
||||||
// EDIT target is compact and always belongs to this physical fork.
|
// EDIT target is compact and belongs to this logical user history; physical fork is resolved canonically.
|
||||||
targetLogin = login;
|
targetLogin = login;
|
||||||
targetForkNumber = null;
|
targetForkNumber = null;
|
||||||
}
|
}
|
||||||
@@ -677,7 +669,7 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
be.setToBlockHash(t.toBlockHashBytes());
|
be.setToBlockHash(t.toBlockHashBytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
// edit helper (optional): если TEXT_EDIT_* — это "редактирование блока цели"
|
// edit helper (optional): TEXT_EDIT_* targets are resolved against logical canonical history
|
||||||
int type = block.type & 0xFFFF;
|
int type = block.type & 0xFFFF;
|
||||||
int sub = block.subType & 0xFFFF;
|
int sub = block.subType & 0xFFFF;
|
||||||
|
|
||||||
@@ -711,16 +703,16 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
|
|
||||||
UserNotificationEntry notificationEntry = buildNotificationEntry(block, be);
|
UserNotificationEntry notificationEntry = buildNotificationEntry(block, be);
|
||||||
List<String> counterChangedLogins = dbWriter.appendBlockAndState(
|
List<String> counterChangedLogins = dbWriter.appendBlockAndState(
|
||||||
blockchainName, block, st, be, upsertedParam, channelNameStateEntry, channelMetaUpdateEntry, notificationEntry);
|
blockchainName, block, st, be, upsertedParam, channelNameStateEntry, channelMetaUpdateEntry, notificationEntry, replayMode);
|
||||||
|
|
||||||
|
if (!replayMode) {
|
||||||
for (String changedLogin : counterChangedLogins) {
|
for (String changedLogin : counterChangedLogins) {
|
||||||
UserCountersSupport.pushChanged(changedLogin);
|
UserCountersSupport.pushChanged(changedLogin);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (chat200CreateSeed != null) {
|
|
||||||
upsertChat200StateFromCreate(chat200CreateSeed);
|
|
||||||
}
|
}
|
||||||
maybeApplyChat200Command(blockchainName, block);
|
|
||||||
|
// Chat200 is legacy/non-working/unused. Do not create or mutate Chat200 projections.
|
||||||
|
// The historical helper code remains below only for archive compatibility and may be removed later.
|
||||||
|
|
||||||
if (publishToArweave) {
|
if (publishToArweave) {
|
||||||
// Только ставим быструю публикацию в очередь: ответ клиенту не ждёт сеть Turbo/Arweave.
|
// Только ставим быструю публикацию в очередь: ответ клиенту не ждёт сеть Turbo/Arweave.
|
||||||
@@ -740,9 +732,8 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
log.info("✅ AddBlock ok: login={}, blockchainName={}, blockNumber={}, newHash={}",
|
log.info("✅ AddBlock ok: login={}, blockchainName={}, blockNumber={}, newHash={}",
|
||||||
login, blockchainName, block.blockNumber, newHashHex);
|
login, blockchainName, block.blockNumber, newHashHex);
|
||||||
|
|
||||||
if (replicateAfterWrite) {
|
// Legacy server-to-server replication is intentionally disabled.
|
||||||
addBlockSyncService.replicateAsync(blockchainName, block.blockNumber);
|
// Arweave/Turbo is the supported cross-server recovery/synchronization source.
|
||||||
}
|
|
||||||
|
|
||||||
return new AddBlockResult(WireCodes.Status.OK, null, block.blockNumber, newHashHex);
|
return new AddBlockResult(WireCodes.Status.OK, null, block.blockNumber, newHashHex);
|
||||||
}
|
}
|
||||||
@@ -752,7 +743,16 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
* но не запускает повторную межсерверную репликацию уже импортированного блока.
|
* но не запускает повторную межсерверную репликацию уже импортированного блока.
|
||||||
*/
|
*/
|
||||||
public ArweaveImportResult addBlockFromArweave(String blockchainName, byte[] rawBlockBytes) {
|
public ArweaveImportResult addBlockFromArweave(String blockchainName, byte[] rawBlockBytes) {
|
||||||
if (BlockchainResyncGuard.isBlockedForExternalAddBlock(blockchainName)) {
|
return addBlockFromStoredBytes(blockchainName, rawBlockBytes, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Deterministic local replay: rebuild SQL projections without live notifications/unread/push side effects. */
|
||||||
|
public ArweaveImportResult addBlockFromReplay(String blockchainName, byte[] rawBlockBytes) {
|
||||||
|
return addBlockFromStoredBytes(blockchainName, rawBlockBytes, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ArweaveImportResult addBlockFromStoredBytes(String blockchainName, byte[] rawBlockBytes, boolean replayMode) {
|
||||||
|
if (!replayMode && BlockchainResyncGuard.isBlockedForExternalAddBlock(blockchainName)) {
|
||||||
return new ArweaveImportResult(false, "chain_resync_in_progress", -1, "");
|
return new ArweaveImportResult(false, "chain_resync_in_progress", -1, "");
|
||||||
}
|
}
|
||||||
if (rawBlockBytes == null || rawBlockBytes.length == 0) {
|
if (rawBlockBytes == null || rawBlockBytes.length == 0) {
|
||||||
@@ -781,7 +781,8 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
toHex(parsed.prevHash32),
|
toHex(parsed.prevHash32),
|
||||||
Base64Ws.encode(rawBlockBytes),
|
Base64Ws.encode(rawBlockBytes),
|
||||||
false,
|
false,
|
||||||
false
|
false,
|
||||||
|
replayMode
|
||||||
);
|
);
|
||||||
return new ArweaveImportResult(r.isOk(), r.reasonCode, r.serverLastBlockNumber, r.serverLastBlockHashHex);
|
return new ArweaveImportResult(r.isOk(), r.reasonCode, r.serverLastBlockNumber, r.serverLastBlockHashHex);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -832,7 +833,7 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
return "unsupported_line_body";
|
return "unsupported_line_body";
|
||||||
}
|
}
|
||||||
|
|
||||||
BlockEntry root = blocksDAO.getByNumber(blockchainName, rootBlockNumber);
|
BlockEntry root = canonicalChain.getBlock(blockchainName, rootBlockNumber);
|
||||||
if (root == null) return "line_root_not_found";
|
if (root == null) return "line_root_not_found";
|
||||||
if (rootBlockNumber == 0) {
|
if (rootBlockNumber == 0) {
|
||||||
try {
|
try {
|
||||||
@@ -848,7 +849,10 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BlockEntry tail = blocksDAO.getLastLineBlock(blockchainName, type, lineCode);
|
BlockEntry tail;
|
||||||
|
try (Connection resolverConnection = shine.db.DbController.getInstance().getConnection()) {
|
||||||
|
tail = canonicalChain.getLastLineBlock(resolverConnection, blockchainName, type, lineCode, block.blockNumber);
|
||||||
|
}
|
||||||
int expectedPrevNumber;
|
int expectedPrevNumber;
|
||||||
byte[] expectedPrevHash;
|
byte[] expectedPrevHash;
|
||||||
int expectedSequence;
|
int expectedSequence;
|
||||||
@@ -933,6 +937,8 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @deprecated Chat200 is legacy/non-working/unused. */
|
||||||
|
@Deprecated
|
||||||
private static final class Chat200CreateSeed {
|
private static final class Chat200CreateSeed {
|
||||||
String ownerLogin;
|
String ownerLogin;
|
||||||
String ownerBch;
|
String ownerBch;
|
||||||
@@ -954,7 +960,7 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
byte[] targetHash = editBody.toBlockHashBytes();
|
byte[] targetHash = editBody.toBlockHashBytes();
|
||||||
if (targetBlockNumber == null || targetHash == null || targetHash.length != 32) return "bad_block_body";
|
if (targetBlockNumber == null || targetHash == null || targetHash.length != 32) return "bad_block_body";
|
||||||
|
|
||||||
BlockEntry target = blocksDAO.getByNumber(ownerBch, targetBlockNumber);
|
BlockEntry target = canonicalChain.getBlock(ownerBch, targetBlockNumber);
|
||||||
if (target == null || target.getBlockHash() == null || !Arrays.equals(target.getBlockHash(), targetHash)) {
|
if (target == null || target.getBlockHash() == null || !Arrays.equals(target.getBlockHash(), targetHash)) {
|
||||||
return "edit_target_not_found";
|
return "edit_target_not_found";
|
||||||
}
|
}
|
||||||
@@ -970,7 +976,7 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
byte[] targetHash = editBody.toBlockHashBytes();
|
byte[] targetHash = editBody.toBlockHashBytes();
|
||||||
if (targetBlockNumber == null || targetHash == null || targetHash.length != 32) return "bad_block_body";
|
if (targetBlockNumber == null || targetHash == null || targetHash.length != 32) return "bad_block_body";
|
||||||
|
|
||||||
BlockEntry target = blocksDAO.getByNumber(ownerBch, targetBlockNumber);
|
BlockEntry target = canonicalChain.getBlock(ownerBch, targetBlockNumber);
|
||||||
if (target == null || target.getBlockHash() == null || !Arrays.equals(target.getBlockHash(), targetHash)) {
|
if (target == null || target.getBlockHash() == null || !Arrays.equals(target.getBlockHash(), targetHash)) {
|
||||||
return "edit_target_not_found";
|
return "edit_target_not_found";
|
||||||
}
|
}
|
||||||
@@ -1027,10 +1033,10 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
PreparedStatement ps = c.prepareStatement("""
|
PreparedStatement ps = c.prepareStatement("""
|
||||||
SELECT slug
|
SELECT slug
|
||||||
FROM channel_names_state
|
FROM channel_names_state
|
||||||
WHERE owner_bch_name = ? AND channel_root_block_number = ?
|
WHERE LOWER(owner_login)=LOWER(?) AND channel_root_block_number = ?
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""")) {
|
""")) {
|
||||||
ps.setString(1, ownerBch);
|
ps.setString(1, BlockchainNameUtil.loginFromBlockchainName(ownerBch));
|
||||||
ps.setInt(2, rootBlockNumber);
|
ps.setInt(2, rootBlockNumber);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
if (!rs.next()) return null;
|
if (!rs.next()) return null;
|
||||||
@@ -1041,6 +1047,8 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @deprecated Chat200 is legacy/non-working/unused. */
|
||||||
|
@Deprecated
|
||||||
private void upsertChat200StateFromCreate(Chat200CreateSeed seed) throws Exception {
|
private void upsertChat200StateFromCreate(Chat200CreateSeed seed) throws Exception {
|
||||||
try (Connection c = shine.db.DbController.getInstance().getConnection();
|
try (Connection c = shine.db.DbController.getInstance().getConnection();
|
||||||
PreparedStatement ps = c.prepareStatement("""
|
PreparedStatement ps = c.prepareStatement("""
|
||||||
@@ -1068,6 +1076,8 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @deprecated Chat200 is legacy/non-working/unused. */
|
||||||
|
@Deprecated
|
||||||
private void maybeApplyChat200Command(String ownerBch, BchBlockEntry block) throws Exception {
|
private void maybeApplyChat200Command(String ownerBch, BchBlockEntry block) throws Exception {
|
||||||
int msgType = block.type & 0xFFFF;
|
int msgType = block.type & 0xFFFF;
|
||||||
int msgSubType = block.subType & 0xFFFF;
|
int msgSubType = block.subType & 0xFFFF;
|
||||||
@@ -1078,6 +1088,8 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
if (lineCode == null || lineCode <= 0) return;
|
if (lineCode == null || lineCode <= 0) return;
|
||||||
|
|
||||||
if (!isChat200Channel(ownerBch, lineCode)) return;
|
if (!isChat200Channel(ownerBch, lineCode)) return;
|
||||||
|
BlockEntry rootPhysical = canonicalChain.getBlock(ownerBch, lineCode);
|
||||||
|
String chatOwnerBch = rootPhysical == null ? ownerBch : rootPhysical.getBchName();
|
||||||
|
|
||||||
CommandParsed cmd = parseChatCommand(tlb.message);
|
CommandParsed cmd = parseChatCommand(tlb.message);
|
||||||
if (cmd == null) return;
|
if (cmd == null) return;
|
||||||
@@ -1088,11 +1100,11 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
PreparedStatement ps = c.prepareStatement("""
|
PreparedStatement ps = c.prepareStatement("""
|
||||||
UPDATE chat200_state
|
UPDATE chat200_state
|
||||||
SET chat_title = ?, updated_at_ms = ?
|
SET chat_title = ?, updated_at_ms = ?
|
||||||
WHERE owner_bch_name = ? AND channel_root_block_number = ?
|
WHERE LOWER(owner_login)=LOWER(?) AND channel_root_block_number = ?
|
||||||
""")) {
|
""")) {
|
||||||
ps.setString(1, cmd.arg1 == null ? "" : cmd.arg1);
|
ps.setString(1, cmd.arg1 == null ? "" : cmd.arg1);
|
||||||
ps.setLong(2, updatedAtMs);
|
ps.setLong(2, updatedAtMs);
|
||||||
ps.setString(3, ownerBch);
|
ps.setString(3, chatOwnerBch);
|
||||||
ps.setInt(4, lineCode);
|
ps.setInt(4, lineCode);
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
}
|
}
|
||||||
@@ -1116,7 +1128,7 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
updated_at_ms = excluded.updated_at_ms,
|
updated_at_ms = excluded.updated_at_ms,
|
||||||
updated_by_block_number = excluded.updated_by_block_number
|
updated_by_block_number = excluded.updated_by_block_number
|
||||||
""")) {
|
""")) {
|
||||||
ps.setString(1, ownerBch);
|
ps.setString(1, chatOwnerBch);
|
||||||
ps.setInt(2, lineCode);
|
ps.setInt(2, lineCode);
|
||||||
ps.setString(3, memberLogin);
|
ps.setString(3, memberLogin);
|
||||||
ps.setString(4, memberChannel);
|
ps.setString(4, memberChannel);
|
||||||
@@ -1127,15 +1139,17 @@ public final class Net_AddBlock_Handler implements JsonMessageHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @deprecated Chat200 is legacy/non-working/unused. */
|
||||||
|
@Deprecated
|
||||||
private boolean isChat200Channel(String ownerBch, int rootBlockNumber) throws Exception {
|
private boolean isChat200Channel(String ownerBch, int rootBlockNumber) throws Exception {
|
||||||
try (Connection c = shine.db.DbController.getInstance().getConnection();
|
try (Connection c = shine.db.DbController.getInstance().getConnection();
|
||||||
PreparedStatement ps = c.prepareStatement("""
|
PreparedStatement ps = c.prepareStatement("""
|
||||||
SELECT channel_type_code
|
SELECT channel_type_code
|
||||||
FROM channel_names_state
|
FROM channel_names_state
|
||||||
WHERE owner_bch_name = ? AND channel_root_block_number = ?
|
WHERE LOWER(owner_login)=LOWER(?) AND channel_root_block_number = ?
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""")) {
|
""")) {
|
||||||
ps.setString(1, ownerBch);
|
ps.setString(1, BlockchainNameUtil.loginFromBlockchainName(ownerBch));
|
||||||
ps.setInt(2, rootBlockNumber);
|
ps.setInt(2, rootBlockNumber);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
if (!rs.next()) return false;
|
if (!rs.next()) return false;
|
||||||
|
|||||||
+4
-3
@@ -55,7 +55,8 @@ public final class BlockchainWriter {
|
|||||||
UserParamEntry userParamEntry,
|
UserParamEntry userParamEntry,
|
||||||
ChannelNameStateEntry channelNameStateEntry,
|
ChannelNameStateEntry channelNameStateEntry,
|
||||||
ChannelNameStateEntry channelMetaUpdateEntry,
|
ChannelNameStateEntry channelMetaUpdateEntry,
|
||||||
UserNotificationEntry notificationEntry) throws SQLException {
|
UserNotificationEntry notificationEntry,
|
||||||
|
boolean replayMode) throws SQLException {
|
||||||
|
|
||||||
long nowMs = System.currentTimeMillis();
|
long nowMs = System.currentTimeMillis();
|
||||||
byte[] rawDataItem = block.toBytes();
|
byte[] rawDataItem = block.toBytes();
|
||||||
@@ -79,7 +80,7 @@ public final class BlockchainWriter {
|
|||||||
int msgType = block.type & 0xFFFF;
|
int msgType = block.type & 0xFFFF;
|
||||||
int msgSubType = block.subType & 0xFFFF;
|
int msgSubType = block.subType & 0xFFFF;
|
||||||
Integer lineCode = be.getLineCode();
|
Integer lineCode = be.getLineCode();
|
||||||
if (isPublicChannelPublication(msgType, msgSubType) && lineCode != null && lineCode > 0) {
|
if (!replayMode && isPublicChannelPublication(msgType, msgSubType) && lineCode != null && lineCode > 0) {
|
||||||
String channelName = channelReadStateDAO.resolvePublicChannelNameByRoot(c, blockchainName, lineCode);
|
String channelName = channelReadStateDAO.resolvePublicChannelNameByRoot(c, blockchainName, lineCode);
|
||||||
if (channelName != null && !channelName.isBlank()) {
|
if (channelName != null && !channelName.isBlank()) {
|
||||||
counterChangedLogins.addAll(channelReadStateDAO.incrementUnreadForSubscribers(
|
counterChangedLogins.addAll(channelReadStateDAO.incrementUnreadForSubscribers(
|
||||||
@@ -87,7 +88,7 @@ public final class BlockchainWriter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (msgType == 3
|
if (!replayMode && msgType == 3
|
||||||
&& msgSubType == (MsgSubType.CONNECTION_UNFOLLOW & 0xFFFF)
|
&& msgSubType == (MsgSubType.CONNECTION_UNFOLLOW & 0xFFFF)
|
||||||
&& be.getToBchName() != null && !be.getToBchName().isBlank()
|
&& be.getToBchName() != null && !be.getToBchName().isBlank()
|
||||||
&& be.getToBlockNumber() != null && be.getToBlockNumber() > 0) {
|
&& be.getToBlockNumber() != null && be.getToBlockNumber() > 0) {
|
||||||
|
|||||||
+2
-1
@@ -21,6 +21,7 @@ public final class Net_GetBlockchainBlock_Handler implements JsonMessageHandler
|
|||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(Net_GetBlockchainBlock_Handler.class);
|
private static final Logger log = LoggerFactory.getLogger(Net_GetBlockchainBlock_Handler.class);
|
||||||
private final BlocksDAO blocksDAO = BlocksDAO.getInstance();
|
private final BlocksDAO blocksDAO = BlocksDAO.getInstance();
|
||||||
|
private final shine.db.dao.CanonicalChainResolver canonicalChain = shine.db.dao.CanonicalChainResolver.getInstance();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Net_Response handle(Net_Request baseRequest, ConnectionContext ctx) {
|
public Net_Response handle(Net_Request baseRequest, ConnectionContext ctx) {
|
||||||
@@ -37,7 +38,7 @@ public final class Net_GetBlockchainBlock_Handler implements JsonMessageHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
BlockEntry block = blocksDAO.getByNumber(blockchainName, req.getBlockNumber());
|
BlockEntry block = canonicalChain.getBlock(blockchainName, req.getBlockNumber());
|
||||||
if (block == null || block.getBlockBytes() == null) {
|
if (block == null || block.getBlockBytes() == null) {
|
||||||
return NetExceptionResponseFactory.error(
|
return NetExceptionResponseFactory.error(
|
||||||
req,
|
req,
|
||||||
|
|||||||
+14
-6
@@ -12,7 +12,7 @@ import server.logic.ws_protocol.JSON.handlers.blockchain.entyties.Net_GetMyBlock
|
|||||||
import server.logic.ws_protocol.JSON.utils.NetExceptionResponseFactory;
|
import server.logic.ws_protocol.JSON.utils.NetExceptionResponseFactory;
|
||||||
import server.logic.ws_protocol.WireCodes;
|
import server.logic.ws_protocol.WireCodes;
|
||||||
import shine.db.dao.BlockchainStateDAO;
|
import shine.db.dao.BlockchainStateDAO;
|
||||||
import shine.db.dao.BlocksDAO;
|
import shine.db.dao.CanonicalChainResolver;
|
||||||
import shine.db.dao.SolanaUserPdaCurrentDAO;
|
import shine.db.dao.SolanaUserPdaCurrentDAO;
|
||||||
import shine.db.entities.BlockEntry;
|
import shine.db.entities.BlockEntry;
|
||||||
import shine.db.entities.BlockchainStateEntry;
|
import shine.db.entities.BlockchainStateEntry;
|
||||||
@@ -24,7 +24,7 @@ import java.util.List;
|
|||||||
|
|
||||||
/** Authenticated, paginated current-chain view used by "Мой блокчейн" and key rotation. */
|
/** Authenticated, paginated current-chain view used by "Мой блокчейн" and key rotation. */
|
||||||
public final class Net_GetMyBlockchain_Handler implements JsonMessageHandler {
|
public final class Net_GetMyBlockchain_Handler implements JsonMessageHandler {
|
||||||
private final BlocksDAO blocks = BlocksDAO.getInstance();
|
private final CanonicalChainResolver canonical = CanonicalChainResolver.getInstance();
|
||||||
private final BlockchainStateDAO states = BlockchainStateDAO.getInstance();
|
private final BlockchainStateDAO states = BlockchainStateDAO.getInstance();
|
||||||
private final SolanaUserPdaCurrentDAO users = SolanaUserPdaCurrentDAO.getInstance();
|
private final SolanaUserPdaCurrentDAO users = SolanaUserPdaCurrentDAO.getInstance();
|
||||||
|
|
||||||
@@ -48,10 +48,18 @@ public final class Net_GetMyBlockchain_Handler implements JsonMessageHandler {
|
|||||||
int before = req.getBeforeBlock() == null || req.getBeforeBlock() < 0 ? tip : Math.min(tip, req.getBeforeBlock());
|
int before = req.getBeforeBlock() == null || req.getBeforeBlock() < 0 ? tip : Math.min(tip, req.getBeforeBlock());
|
||||||
boolean includeBytes = Boolean.TRUE.equals(req.getIncludeBlockBytes());
|
boolean includeBytes = Boolean.TRUE.equals(req.getIncludeBlockBytes());
|
||||||
|
|
||||||
List<BlockEntry> rows = before < 0
|
List<BlockEntry> rows = new ArrayList<>();
|
||||||
? List.of()
|
if (before >= 0) {
|
||||||
: blocks.listRangeByNumber(bch, Math.max(0, before - limit + 1), before);
|
int low = Math.max(0, before - limit + 1);
|
||||||
Collections.reverse(rows);
|
for (int n = before; n >= low; n--) {
|
||||||
|
BlockEntry row = canonical.getBlock(bch, n);
|
||||||
|
if (row == null) {
|
||||||
|
return NetExceptionResponseFactory.error(req, 409, "CANONICAL_HISTORY_GAP",
|
||||||
|
"В канонической истории отсутствует блок #" + n);
|
||||||
|
}
|
||||||
|
rows.add(row);
|
||||||
|
}
|
||||||
|
}
|
||||||
List<Net_GetMyBlockchain_Response.BlockItem> items = new ArrayList<>(rows.size());
|
List<Net_GetMyBlockchain_Response.BlockItem> items = new ArrayList<>(rows.size());
|
||||||
for (BlockEntry row : rows) {
|
for (BlockEntry row : rows) {
|
||||||
BchBlockEntry parsed = new BchBlockEntry(row.getBlockBytes());
|
BchBlockEntry parsed = new BchBlockEntry(row.getBlockBytes());
|
||||||
|
|||||||
+5
-3
@@ -188,11 +188,11 @@ public final class ChannelNamesStateBootstrapper {
|
|||||||
List<String> skipped) throws Exception {
|
List<String> skipped) throws Exception {
|
||||||
Map<String, String> fallbackTitles = new LinkedHashMap<>();
|
Map<String, String> fallbackTitles = new LinkedHashMap<>();
|
||||||
for (ChannelNameStateEntry entry : entries) {
|
for (ChannelNameStateEntry entry : entries) {
|
||||||
fallbackTitles.put(entry.getOwnerBlockchainName() + "#" + entry.getChannelRootBlockNumber(), entry.getSlug());
|
fallbackTitles.put(entry.getOwnerLogin().toLowerCase() + "#" + entry.getChannelRootBlockNumber(), entry.getSlug());
|
||||||
}
|
}
|
||||||
|
|
||||||
String sql = """
|
String sql = """
|
||||||
SELECT bch_name, block_number, block_bytes
|
SELECT login, bch_name, block_number, block_bytes
|
||||||
FROM blocks
|
FROM blocks
|
||||||
WHERE msg_type = ? AND msg_sub_type = ?
|
WHERE msg_type = ? AND msg_sub_type = ?
|
||||||
ORDER BY bch_name, block_number
|
ORDER BY bch_name, block_number
|
||||||
@@ -202,6 +202,7 @@ public final class ChannelNamesStateBootstrapper {
|
|||||||
ps.setInt(2, MsgSubType.TEXT_CHANNEL_META);
|
ps.setInt(2, MsgSubType.TEXT_CHANNEL_META);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
|
String ownerLogin = rs.getString("login");
|
||||||
String ownerBch = rs.getString("bch_name");
|
String ownerBch = rs.getString("bch_name");
|
||||||
int blockNumber = rs.getInt("block_number");
|
int blockNumber = rs.getInt("block_number");
|
||||||
byte[] blockBytes = rs.getBytes("block_bytes");
|
byte[] blockBytes = rs.getBytes("block_bytes");
|
||||||
@@ -209,11 +210,12 @@ public final class ChannelNamesStateBootstrapper {
|
|||||||
BchBlockEntry parsed = new BchBlockEntry(blockBytes);
|
BchBlockEntry parsed = new BchBlockEntry(blockBytes);
|
||||||
if (!(parsed.body instanceof TextLineBody metaBody)) continue;
|
if (!(parsed.body instanceof TextLineBody metaBody)) continue;
|
||||||
int lineCode = metaBody.lineCode();
|
int lineCode = metaBody.lineCode();
|
||||||
String fallbackTitle = fallbackTitles.get(ownerBch + "#" + lineCode);
|
String fallbackTitle = fallbackTitles.get(ownerLogin.toLowerCase() + "#" + lineCode);
|
||||||
if (fallbackTitle == null) continue;
|
if (fallbackTitle == null) continue;
|
||||||
ChannelMetaTextParser.Parsed parsedMeta = ChannelMetaTextParser.parseStrict(metaBody.message, fallbackTitle);
|
ChannelMetaTextParser.Parsed parsedMeta = ChannelMetaTextParser.parseStrict(metaBody.message, fallbackTitle);
|
||||||
|
|
||||||
ChannelNameStateEntry update = new ChannelNameStateEntry();
|
ChannelNameStateEntry update = new ChannelNameStateEntry();
|
||||||
|
update.setOwnerLogin(ownerLogin);
|
||||||
update.setOwnerBlockchainName(ownerBch);
|
update.setOwnerBlockchainName(ownerBch);
|
||||||
update.setChannelRootBlockNumber(lineCode);
|
update.setChannelRootBlockNumber(lineCode);
|
||||||
update.setDisplayName(parsedMeta.title());
|
update.setDisplayName(parsedMeta.title());
|
||||||
|
|||||||
+65
-67
@@ -55,11 +55,11 @@ final class ChannelsReadSupport {
|
|||||||
SELECT slug, display_name, channel_description, channel_type_code, channel_type_version,
|
SELECT slug, display_name, channel_description, channel_type_code, channel_type_version,
|
||||||
ava_ar, ava_sha256, ava_size, meta_updated_at_ms
|
ava_ar, ava_sha256, ava_size, meta_updated_at_ms
|
||||||
FROM channel_names_state
|
FROM channel_names_state
|
||||||
WHERE owner_bch_name = ? AND channel_root_block_number = ?
|
WHERE LOWER(owner_login)=LOWER(?) AND channel_root_block_number = ?
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""";
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(stateSql)) {
|
try (PreparedStatement ps = c.prepareStatement(stateSql)) {
|
||||||
ps.setString(1, ownerBch);
|
ps.setString(1, loginFromChainName(ownerBch));
|
||||||
ps.setInt(2, rootNumber);
|
ps.setInt(2, rootNumber);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
@@ -79,10 +79,17 @@ final class ChannelsReadSupport {
|
|||||||
// fallback ниже для окружений до миграции.
|
// fallback ниже для окружений до миграции.
|
||||||
}
|
}
|
||||||
|
|
||||||
String sql = "SELECT block_bytes FROM blocks WHERE bch_name=? AND block_number=? LIMIT 1";
|
String sql = """
|
||||||
|
SELECT block_bytes FROM blocks
|
||||||
|
WHERE LOWER(login)=LOWER(?) AND block_number=?
|
||||||
|
AND fork_number=shine_canonical_fork(login,(SELECT substring(blockchain_name from '([0-9]{3})$')::INTEGER FROM solana_user_pda_current WHERE LOWER(login)=LOWER(?) LIMIT 1),block_number)
|
||||||
|
LIMIT 1
|
||||||
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, ownerBch);
|
String ownerLogin=loginFromChainName(ownerBch);
|
||||||
|
ps.setString(1, ownerLogin);
|
||||||
ps.setInt(2, rootNumber);
|
ps.setInt(2, rootNumber);
|
||||||
|
ps.setString(3, ownerLogin);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
if (!rs.next()) {
|
if (!rs.next()) {
|
||||||
meta.channelName = null;
|
meta.channelName = null;
|
||||||
@@ -126,20 +133,14 @@ final class ChannelsReadSupport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int countPosts(Connection c, String ownerBch, int lineCode) throws SQLException {
|
static int countPosts(Connection c, String ownerBch, int lineCode) throws SQLException {
|
||||||
String sql = "SELECT COUNT(*) AS cnt FROM blocks WHERE bch_name=? AND msg_type=? AND msg_sub_type IN (?, ?, ?, ?, ?, ?) AND line_code=?";
|
String sql = """
|
||||||
|
SELECT COUNT(*) AS cnt FROM blocks
|
||||||
|
WHERE LOWER(login)=LOWER(?) AND msg_type=? AND msg_sub_type IN (?, ?, ?, ?, ?, ?) AND line_code=?
|
||||||
|
AND fork_number=shine_canonical_fork(login,(SELECT substring(blockchain_name from '([0-9]{3})$')::INTEGER FROM solana_user_pda_current WHERE LOWER(login)=LOWER(?) LIMIT 1),block_number)
|
||||||
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, ownerBch);
|
String login=loginFromChainName(ownerBch); ps.setString(1,login); ps.setInt(2,MSG_TYPE_TEXT); ps.setInt(3,MsgSubType.TEXT_POST); ps.setInt(4,MsgSubType.TEXT_REPOST); ps.setInt(5,MsgSubType.TEXT_ENTRYPOINT); ps.setInt(6,MsgSubType.TEXT_EXERCISE); ps.setInt(7,MsgSubType.TEXT_SERVICE); ps.setInt(8,MsgSubType.TEXT_COURSE); ps.setInt(9,lineCode); ps.setString(10,login);
|
||||||
ps.setInt(2, MSG_TYPE_TEXT);
|
try(ResultSet rs=ps.executeQuery()){return rs.next()?rs.getInt("cnt"):0;}
|
||||||
ps.setInt(3, MsgSubType.TEXT_POST);
|
|
||||||
ps.setInt(4, MsgSubType.TEXT_REPOST);
|
|
||||||
ps.setInt(5, MsgSubType.TEXT_ENTRYPOINT);
|
|
||||||
ps.setInt(6, MsgSubType.TEXT_EXERCISE);
|
|
||||||
ps.setInt(7, MsgSubType.TEXT_SERVICE);
|
|
||||||
ps.setInt(8, MsgSubType.TEXT_COURSE);
|
|
||||||
ps.setInt(9, lineCode);
|
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
|
||||||
return rs.next() ? rs.getInt("cnt") : 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,51 +153,32 @@ final class ChannelsReadSupport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static PostBlock loadLastPost(Connection c, String ownerBch, int lineCode) throws SQLException {
|
static PostBlock loadLastPost(Connection c, String ownerBch, int lineCode) throws SQLException {
|
||||||
String sql = """
|
String sql="""
|
||||||
SELECT login,bch_name,block_number,block_hash,block_bytes,this_line_number
|
SELECT login,bch_name,block_number,block_hash,block_bytes,this_line_number FROM blocks
|
||||||
FROM blocks
|
WHERE LOWER(login)=LOWER(?) AND msg_type=? AND msg_sub_type IN (?, ?, ?, ?, ?, ?) AND line_code=?
|
||||||
WHERE bch_name=? AND msg_type=? AND msg_sub_type IN (?, ?, ?, ?, ?, ?) AND line_code=?
|
AND fork_number=shine_canonical_fork(login,(SELECT substring(blockchain_name from '([0-9]{3})$')::INTEGER FROM solana_user_pda_current WHERE LOWER(login)=LOWER(?) LIMIT 1),block_number)
|
||||||
ORDER BY block_number DESC
|
ORDER BY block_number DESC LIMIT 1
|
||||||
LIMIT 1
|
|
||||||
""";
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try(PreparedStatement ps=c.prepareStatement(sql)){String login=loginFromChainName(ownerBch);ps.setString(1,login);ps.setInt(2,MSG_TYPE_TEXT);ps.setInt(3,MsgSubType.TEXT_POST);ps.setInt(4,MsgSubType.TEXT_REPOST);ps.setInt(5,MsgSubType.TEXT_ENTRYPOINT);ps.setInt(6,MsgSubType.TEXT_EXERCISE);ps.setInt(7,MsgSubType.TEXT_SERVICE);ps.setInt(8,MsgSubType.TEXT_COURSE);ps.setInt(9,lineCode);ps.setString(10,login);try(ResultSet rs=ps.executeQuery()){if(!rs.next())return null;PostBlock pb=new PostBlock();pb.login=rs.getString("login");pb.bchName=rs.getString("bch_name");pb.blockNumber=rs.getInt("block_number");pb.blockHash=rs.getBytes("block_hash");pb.blockBytes=rs.getBytes("block_bytes");return pb;}}
|
||||||
ps.setString(1, ownerBch);
|
|
||||||
ps.setInt(2, MSG_TYPE_TEXT);
|
|
||||||
ps.setInt(3, MsgSubType.TEXT_POST);
|
|
||||||
ps.setInt(4, MsgSubType.TEXT_REPOST);
|
|
||||||
ps.setInt(5, MsgSubType.TEXT_ENTRYPOINT);
|
|
||||||
ps.setInt(6, MsgSubType.TEXT_EXERCISE);
|
|
||||||
ps.setInt(7, MsgSubType.TEXT_SERVICE);
|
|
||||||
ps.setInt(8, MsgSubType.TEXT_COURSE);
|
|
||||||
ps.setInt(9, lineCode);
|
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
|
||||||
if (!rs.next()) return null;
|
|
||||||
PostBlock pb = new PostBlock();
|
|
||||||
pb.login = rs.getString("login");
|
|
||||||
pb.bchName = rs.getString("bch_name");
|
|
||||||
pb.blockNumber = rs.getInt("block_number");
|
|
||||||
pb.blockHash = rs.getBytes("block_hash");
|
|
||||||
pb.blockBytes = rs.getBytes("block_bytes");
|
|
||||||
return pb;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static PostBlock loadLastVersion(Connection c, String ownerBch, int originalBlockNumber, byte[] originalHash) throws SQLException {
|
static PostBlock loadLastVersion(Connection c, String ownerBch, int originalBlockNumber, byte[] originalHash) throws SQLException {
|
||||||
String sql = """
|
String sql = """
|
||||||
SELECT login,bch_name,block_number,block_hash,block_bytes,this_line_number
|
SELECT login,bch_name,block_number,block_hash,block_bytes,this_line_number
|
||||||
FROM blocks
|
FROM blocks
|
||||||
WHERE bch_name=? AND msg_type=? AND msg_sub_type=?
|
WHERE LOWER(login)=LOWER(?) AND msg_type=? AND msg_sub_type=?
|
||||||
AND to_block_number=? AND to_block_hash=?
|
AND to_block_number=? AND to_block_hash=?
|
||||||
|
AND fork_number=shine_canonical_fork(login,(SELECT substring(blockchain_name from '([0-9]{3})$')::INTEGER FROM solana_user_pda_current WHERE LOWER(login)=LOWER(?) LIMIT 1),block_number)
|
||||||
ORDER BY block_number DESC
|
ORDER BY block_number DESC
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""";
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, ownerBch);
|
ps.setString(1, loginFromChainName(ownerBch));
|
||||||
ps.setInt(2, MSG_TYPE_TEXT);
|
ps.setInt(2, MSG_TYPE_TEXT);
|
||||||
ps.setInt(3, MsgSubType.TEXT_EDIT_POST);
|
ps.setInt(3, MsgSubType.TEXT_EDIT_POST);
|
||||||
ps.setInt(4, originalBlockNumber);
|
ps.setInt(4, originalBlockNumber);
|
||||||
ps.setBytes(5, originalHash);
|
ps.setBytes(5, originalHash);
|
||||||
|
ps.setString(6, loginFromChainName(ownerBch));
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
if (!rs.next()) return null;
|
if (!rs.next()) return null;
|
||||||
PostBlock pb = new PostBlock();
|
PostBlock pb = new PostBlock();
|
||||||
@@ -236,11 +218,12 @@ final class ChannelsReadSupport {
|
|||||||
String sql = """
|
String sql = """
|
||||||
SELECT login,bch_name,block_number,block_hash,block_bytes,to_bch_name,to_block_number,to_block_hash,msg_sub_type,this_line_number
|
SELECT login,bch_name,block_number,block_hash,block_bytes,to_bch_name,to_block_number,to_block_hash,msg_sub_type,this_line_number
|
||||||
FROM blocks
|
FROM blocks
|
||||||
WHERE bch_name=? AND msg_type=? AND msg_sub_type IN (?, ?, ?, ?, ?, ?) AND line_code=?
|
WHERE LOWER(login)=LOWER(?) AND msg_type=? AND msg_sub_type IN (?, ?, ?, ?, ?, ?) AND line_code=?
|
||||||
|
AND fork_number=shine_canonical_fork(login,(SELECT substring(blockchain_name from '([0-9]{3})$')::INTEGER FROM solana_user_pda_current WHERE LOWER(login)=LOWER(?) LIMIT 1),block_number)
|
||||||
ORDER BY block_number
|
ORDER BY block_number
|
||||||
""" + order + (bounded ? " LIMIT ?" : "");
|
""" + order + (bounded ? " LIMIT ?" : "");
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, ownerBch);
|
ps.setString(1, loginFromChainName(ownerBch));
|
||||||
ps.setInt(2, MSG_TYPE_TEXT);
|
ps.setInt(2, MSG_TYPE_TEXT);
|
||||||
ps.setInt(3, MsgSubType.TEXT_POST);
|
ps.setInt(3, MsgSubType.TEXT_POST);
|
||||||
ps.setInt(4, MsgSubType.TEXT_REPOST);
|
ps.setInt(4, MsgSubType.TEXT_REPOST);
|
||||||
@@ -249,7 +232,8 @@ final class ChannelsReadSupport {
|
|||||||
ps.setInt(7, MsgSubType.TEXT_SERVICE);
|
ps.setInt(7, MsgSubType.TEXT_SERVICE);
|
||||||
ps.setInt(8, MsgSubType.TEXT_COURSE);
|
ps.setInt(8, MsgSubType.TEXT_COURSE);
|
||||||
ps.setInt(9, lineCode);
|
ps.setInt(9, lineCode);
|
||||||
if (bounded) ps.setInt(10, limit);
|
ps.setString(10, loginFromChainName(ownerBch));
|
||||||
|
if (bounded) ps.setInt(11, limit);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
List<PostBlock> out = new ArrayList<>();
|
List<PostBlock> out = new ArrayList<>();
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
@@ -284,16 +268,18 @@ final class ChannelsReadSupport {
|
|||||||
String sql = """
|
String sql = """
|
||||||
SELECT login,bch_name,block_number,block_hash,block_bytes
|
SELECT login,bch_name,block_number,block_hash,block_bytes
|
||||||
FROM blocks
|
FROM blocks
|
||||||
WHERE bch_name=? AND msg_type=? AND msg_sub_type=?
|
WHERE LOWER(login)=LOWER(?) AND msg_type=? AND msg_sub_type=?
|
||||||
AND to_block_number=? AND to_block_hash=?
|
AND to_block_number=? AND to_block_hash=?
|
||||||
|
AND fork_number=shine_canonical_fork(login,(SELECT substring(blockchain_name from '([0-9]{3})$')::INTEGER FROM solana_user_pda_current WHERE LOWER(login)=LOWER(?) LIMIT 1),block_number)
|
||||||
ORDER BY block_number ASC
|
ORDER BY block_number ASC
|
||||||
""";
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, ownerBch);
|
ps.setString(1, loginFromChainName(ownerBch));
|
||||||
ps.setInt(2, MSG_TYPE_TEXT);
|
ps.setInt(2, MSG_TYPE_TEXT);
|
||||||
ps.setInt(3, MsgSubType.TEXT_EDIT_POST);
|
ps.setInt(3, MsgSubType.TEXT_EDIT_POST);
|
||||||
ps.setInt(4, originalBlock);
|
ps.setInt(4, originalBlock);
|
||||||
ps.setBytes(5, originalHash);
|
ps.setBytes(5, originalHash);
|
||||||
|
ps.setString(6, loginFromChainName(ownerBch));
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
List<PostBlock> out = new ArrayList<>();
|
List<PostBlock> out = new ArrayList<>();
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
@@ -337,6 +323,7 @@ final class ChannelsReadSupport {
|
|||||||
AND LOWER(to_login) = LOWER(?)
|
AND LOWER(to_login) = LOWER(?)
|
||||||
AND to_block_number = ?
|
AND to_block_number = ?
|
||||||
AND to_block_hash = ?
|
AND to_block_hash = ?
|
||||||
|
AND fork_number=shine_canonical_fork(login,(SELECT substring(blockchain_name from '([0-9]{3})$')::INTEGER FROM solana_user_pda_current WHERE LOWER(login)=LOWER(blocks.login) LIMIT 1),block_number)
|
||||||
""";
|
""";
|
||||||
int ratingsCount = 0;
|
int ratingsCount = 0;
|
||||||
try (PreparedStatement ps = c.prepareStatement(ratingsSql)) {
|
try (PreparedStatement ps = c.prepareStatement(ratingsSql)) {
|
||||||
@@ -363,11 +350,11 @@ final class ChannelsReadSupport {
|
|||||||
String stateSql = """
|
String stateSql = """
|
||||||
SELECT channel_description
|
SELECT channel_description
|
||||||
FROM channel_names_state
|
FROM channel_names_state
|
||||||
WHERE owner_bch_name = ? AND channel_root_block_number = ?
|
WHERE LOWER(owner_login)=LOWER(?) AND channel_root_block_number = ?
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""";
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(stateSql)) {
|
try (PreparedStatement ps = c.prepareStatement(stateSql)) {
|
||||||
ps.setString(1, ownerBch);
|
ps.setString(1, loginFromChainName(ownerBch));
|
||||||
ps.setInt(2, rootNumber);
|
ps.setInt(2, rootNumber);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
@@ -380,10 +367,17 @@ final class ChannelsReadSupport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fallback: parse root block directly.
|
// Fallback: parse root block directly.
|
||||||
String sql = "SELECT block_bytes FROM blocks WHERE bch_name=? AND block_number=? LIMIT 1";
|
String sql = """
|
||||||
|
SELECT block_bytes FROM blocks
|
||||||
|
WHERE LOWER(login)=LOWER(?) AND block_number=?
|
||||||
|
AND fork_number=shine_canonical_fork(login,(SELECT substring(blockchain_name from '([0-9]{3})$')::INTEGER FROM solana_user_pda_current WHERE LOWER(login)=LOWER(?) LIMIT 1),block_number)
|
||||||
|
LIMIT 1
|
||||||
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, ownerBch);
|
String ownerLogin=loginFromChainName(ownerBch);
|
||||||
|
ps.setString(1, ownerLogin);
|
||||||
ps.setInt(2, rootNumber);
|
ps.setInt(2, rootNumber);
|
||||||
|
ps.setString(3, ownerLogin);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
if (!rs.next()) return "";
|
if (!rs.next()) return "";
|
||||||
byte[] bytes = rs.getBytes("block_bytes");
|
byte[] bytes = rs.getBytes("block_bytes");
|
||||||
@@ -405,11 +399,11 @@ final class ChannelsReadSupport {
|
|||||||
String stateSql = """
|
String stateSql = """
|
||||||
SELECT channel_type_code
|
SELECT channel_type_code
|
||||||
FROM channel_names_state
|
FROM channel_names_state
|
||||||
WHERE owner_bch_name = ? AND channel_root_block_number = ?
|
WHERE LOWER(owner_login)=LOWER(?) AND channel_root_block_number = ?
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""";
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(stateSql)) {
|
try (PreparedStatement ps = c.prepareStatement(stateSql)) {
|
||||||
ps.setString(1, ownerBch);
|
ps.setString(1, loginFromChainName(ownerBch));
|
||||||
ps.setInt(2, rootNumber);
|
ps.setInt(2, rootNumber);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
if (rs.next()) return rs.getInt("channel_type_code");
|
if (rs.next()) return rs.getInt("channel_type_code");
|
||||||
@@ -428,11 +422,11 @@ final class ChannelsReadSupport {
|
|||||||
String stateSql = """
|
String stateSql = """
|
||||||
SELECT channel_type_version
|
SELECT channel_type_version
|
||||||
FROM channel_names_state
|
FROM channel_names_state
|
||||||
WHERE owner_bch_name = ? AND channel_root_block_number = ?
|
WHERE LOWER(owner_login)=LOWER(?) AND channel_root_block_number = ?
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""";
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(stateSql)) {
|
try (PreparedStatement ps = c.prepareStatement(stateSql)) {
|
||||||
ps.setString(1, ownerBch);
|
ps.setString(1, loginFromChainName(ownerBch));
|
||||||
ps.setInt(2, rootNumber);
|
ps.setInt(2, rootNumber);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
if (rs.next()) return rs.getInt("channel_type_version");
|
if (rs.next()) return rs.getInt("channel_type_version");
|
||||||
@@ -452,14 +446,17 @@ final class ChannelsReadSupport {
|
|||||||
String rootSql = """
|
String rootSql = """
|
||||||
SELECT login,bch_name,block_number,block_hash,block_bytes,this_line_number
|
SELECT login,bch_name,block_number,block_hash,block_bytes,this_line_number
|
||||||
FROM blocks
|
FROM blocks
|
||||||
WHERE bch_name=? AND msg_type=? AND msg_sub_type=? AND block_number=?
|
WHERE LOWER(login)=LOWER(?) AND msg_type=? AND msg_sub_type=? AND block_number=?
|
||||||
|
AND fork_number=shine_canonical_fork(login,(SELECT substring(blockchain_name from '([0-9]{3})$')::INTEGER FROM solana_user_pda_current WHERE LOWER(login)=LOWER(?) LIMIT 1),block_number)
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""";
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(rootSql)) {
|
try (PreparedStatement ps = c.prepareStatement(rootSql)) {
|
||||||
ps.setString(1, ownerBch);
|
String ownerLogin=loginFromChainName(ownerBch);
|
||||||
|
ps.setString(1, ownerLogin);
|
||||||
ps.setInt(2, MSG_TYPE_TECH);
|
ps.setInt(2, MSG_TYPE_TECH);
|
||||||
ps.setInt(3, 1);
|
ps.setInt(3, 1);
|
||||||
ps.setInt(4, lineCode);
|
ps.setInt(4, lineCode);
|
||||||
|
ps.setString(5, ownerLogin);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
MetaEvent event = new MetaEvent();
|
MetaEvent event = new MetaEvent();
|
||||||
@@ -501,14 +498,16 @@ final class ChannelsReadSupport {
|
|||||||
String sql = """
|
String sql = """
|
||||||
SELECT login,bch_name,block_number,block_hash,block_bytes,this_line_number
|
SELECT login,bch_name,block_number,block_hash,block_bytes,this_line_number
|
||||||
FROM blocks
|
FROM blocks
|
||||||
WHERE bch_name=? AND msg_type=? AND msg_sub_type=? AND line_code=?
|
WHERE LOWER(login)=LOWER(?) AND msg_type=? AND msg_sub_type=? AND line_code=?
|
||||||
|
AND fork_number=shine_canonical_fork(login,(SELECT substring(blockchain_name from '([0-9]{3})$')::INTEGER FROM solana_user_pda_current WHERE LOWER(login)=LOWER(?) LIMIT 1),block_number)
|
||||||
ORDER BY block_number ASC
|
ORDER BY block_number ASC
|
||||||
""";
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, ownerBch);
|
ps.setString(1, loginFromChainName(ownerBch));
|
||||||
ps.setInt(2, MSG_TYPE_TEXT);
|
ps.setInt(2, MSG_TYPE_TEXT);
|
||||||
ps.setInt(3, MsgSubType.TEXT_CHANNEL_META);
|
ps.setInt(3, MsgSubType.TEXT_CHANNEL_META);
|
||||||
ps.setInt(4, lineCode);
|
ps.setInt(4, lineCode);
|
||||||
|
ps.setString(5, loginFromChainName(ownerBch));
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
byte[] bytes = rs.getBytes("block_bytes");
|
byte[] bytes = rs.getBytes("block_bytes");
|
||||||
@@ -546,9 +545,8 @@ final class ChannelsReadSupport {
|
|||||||
|
|
||||||
String partnerBchSql = """
|
String partnerBchSql = """
|
||||||
SELECT blockchain_name
|
SELECT blockchain_name
|
||||||
FROM blockchain_state
|
FROM solana_user_pda_current
|
||||||
WHERE LOWER(login) = LOWER(?)
|
WHERE LOWER(login) = LOWER(?)
|
||||||
ORDER BY blockchain_name
|
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""";
|
""";
|
||||||
|
|
||||||
@@ -571,14 +569,14 @@ final class ChannelsReadSupport {
|
|||||||
String rootSql = """
|
String rootSql = """
|
||||||
SELECT channel_root_block_number
|
SELECT channel_root_block_number
|
||||||
FROM channel_names_state
|
FROM channel_names_state
|
||||||
WHERE owner_bch_name = ?
|
WHERE LOWER(owner_login)=LOWER(?)
|
||||||
AND channel_type_code = ?
|
AND channel_type_code = ?
|
||||||
AND slug = ?
|
AND slug = ?
|
||||||
ORDER BY channel_root_block_number DESC
|
ORDER BY channel_root_block_number DESC
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""";
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(rootSql)) {
|
try (PreparedStatement ps = c.prepareStatement(rootSql)) {
|
||||||
ps.setString(1, partnerBch);
|
ps.setString(1, canonicalPartner);
|
||||||
ps.setInt(2, CreateChannelBody.CHANNEL_TYPE_PERSONAL & 0xFFFF);
|
ps.setInt(2, CreateChannelBody.CHANNEL_TYPE_PERSONAL & 0xFFFF);
|
||||||
ps.setString(3, ownerSlug);
|
ps.setString(3, ownerSlug);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
|
|||||||
+2
-2
@@ -195,12 +195,12 @@ public class Net_GetChannelMessages_Handler implements JsonMessageHandler {
|
|||||||
String sql = """
|
String sql = """
|
||||||
SELECT subscribers_count
|
SELECT subscribers_count
|
||||||
FROM channel_stats_state
|
FROM channel_stats_state
|
||||||
WHERE owner_bch_name = ?
|
WHERE LOWER(owner_login)=LOWER(?)
|
||||||
AND channel_root_block_number = ?
|
AND channel_root_block_number = ?
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""";
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, ownerBch);
|
ps.setString(1, BlockchainNameUtil.loginFromBlockchainName(ownerBch));
|
||||||
ps.setInt(2, rootNumber);
|
ps.setInt(2, rootNumber);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
if (!rs.next()) return 0;
|
if (!rs.next()) return 0;
|
||||||
|
|||||||
+5
-2
@@ -20,6 +20,8 @@ import java.util.ArrayList;
|
|||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
/** @deprecated Chat200 is legacy/non-working/unused and is not maintained for segmented forks. */
|
||||||
|
@Deprecated
|
||||||
public class Net_GetGroupDialog_Handler implements JsonMessageHandler {
|
public class Net_GetGroupDialog_Handler implements JsonMessageHandler {
|
||||||
private static final Logger log = LoggerFactory.getLogger(Net_GetGroupDialog_Handler.class);
|
private static final Logger log = LoggerFactory.getLogger(Net_GetGroupDialog_Handler.class);
|
||||||
|
|
||||||
@@ -156,13 +158,14 @@ public class Net_GetGroupDialog_Handler implements JsonMessageHandler {
|
|||||||
String sql = """
|
String sql = """
|
||||||
SELECT login, bch_name, block_number, block_hash, block_bytes
|
SELECT login, bch_name, block_number, block_hash, block_bytes
|
||||||
FROM blocks
|
FROM blocks
|
||||||
WHERE bch_name = ? AND msg_type = 1 AND line_code = ?
|
WHERE LOWER(login)=LOWER(?) AND msg_type=1 AND line_code=? AND fork_number=shine_canonical_fork(login,(SELECT substring(blockchain_name from '([0-9]{3})$')::INTEGER FROM solana_user_pda_current WHERE LOWER(login)=LOWER(?) LIMIT 1),block_number)
|
||||||
ORDER BY block_number ASC
|
ORDER BY block_number ASC
|
||||||
""";
|
""";
|
||||||
List<MsgRow> out = new ArrayList<>();
|
List<MsgRow> out = new ArrayList<>();
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, ownerBch);
|
ps.setString(1, ChannelsReadSupport.loginFromChainName(ownerBch));
|
||||||
ps.setInt(2, line);
|
ps.setInt(2, line);
|
||||||
|
ps.setString(3, ChannelsReadSupport.loginFromChainName(ownerBch));
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
MsgRow row = new MsgRow();
|
MsgRow row = new MsgRow();
|
||||||
|
|||||||
+23
-13
@@ -118,19 +118,21 @@ public class Net_GetMessageThread_Handler implements JsonMessageHandler {
|
|||||||
WHERE msg_type=1
|
WHERE msg_type=1
|
||||||
AND to_block_number=? AND to_block_hash=?
|
AND to_block_number=? AND to_block_hash=?
|
||||||
AND (
|
AND (
|
||||||
(msg_sub_type=? AND to_bch_name=?)
|
(msg_sub_type=? AND (LOWER(to_login)=LOWER(?) OR to_bch_name=?))
|
||||||
OR (msg_sub_type=? AND LOWER(to_login)=LOWER(?))
|
OR (msg_sub_type=? AND LOWER(to_login)=LOWER(?))
|
||||||
)
|
)
|
||||||
|
AND fork_number=shine_canonical_fork(login,(SELECT substring(blockchain_name from '([0-9]{3})$')::INTEGER FROM solana_user_pda_current WHERE LOWER(login)=LOWER(blocks.login) LIMIT 1),block_number)
|
||||||
""";
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setInt(1, toBlockNumber);
|
ps.setInt(1, toBlockNumber);
|
||||||
ps.setBytes(2, toBlockHash);
|
ps.setBytes(2, toBlockHash);
|
||||||
// REPLY keeps its legacy physical-target lookup intentionally.
|
// REPLY keeps its legacy physical-target lookup intentionally.
|
||||||
ps.setInt(3, MsgSubType.TEXT_REPLY);
|
ps.setInt(3, MsgSubType.TEXT_REPLY);
|
||||||
ps.setString(4, toBchName);
|
ps.setString(4, targetLogin);
|
||||||
|
ps.setString(5, toBchName);
|
||||||
// RATING follows the Stage 2 logical target identity.
|
// RATING follows the Stage 2 logical target identity.
|
||||||
ps.setInt(5, MsgSubType.TEXT_RATING);
|
ps.setInt(6, MsgSubType.TEXT_RATING);
|
||||||
ps.setString(6, targetLogin);
|
ps.setString(7, targetLogin);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
List<PostRow> out = new ArrayList<>();
|
List<PostRow> out = new ArrayList<>();
|
||||||
while (rs.next()) out.add(mapRow(rs));
|
while (rs.next()) out.add(mapRow(rs));
|
||||||
@@ -151,13 +153,14 @@ public class Net_GetMessageThread_Handler implements JsonMessageHandler {
|
|||||||
SELECT login,bch_name,block_number,block_hash,block_bytes,to_login,to_bch_name,to_block_number,to_block_hash,line_code,msg_sub_type,this_line_number
|
SELECT login,bch_name,block_number,block_hash,block_bytes,to_login,to_bch_name,to_block_number,to_block_hash,line_code,msg_sub_type,this_line_number
|
||||||
FROM blocks
|
FROM blocks
|
||||||
WHERE LOWER(login)=LOWER(?) AND block_number=? AND block_hash=?
|
WHERE LOWER(login)=LOWER(?) AND block_number=? AND block_hash=?
|
||||||
ORDER BY fork_number DESC
|
AND fork_number=shine_canonical_fork(login,(SELECT substring(blockchain_name from '([0-9]{3})$')::INTEGER FROM solana_user_pda_current WHERE LOWER(login)=LOWER(?) LIMIT 1),block_number)
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""";
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, login);
|
ps.setString(1, login);
|
||||||
ps.setInt(2, blockNumber);
|
ps.setInt(2, blockNumber);
|
||||||
ps.setBytes(3, blockHash);
|
ps.setBytes(3, blockHash);
|
||||||
|
ps.setString(4, login);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
return rs.next() ? mapRow(rs) : null;
|
return rs.next() ? mapRow(rs) : null;
|
||||||
}
|
}
|
||||||
@@ -168,12 +171,15 @@ public class Net_GetMessageThread_Handler implements JsonMessageHandler {
|
|||||||
String sql = """
|
String sql = """
|
||||||
SELECT login,bch_name,block_number,block_hash,block_bytes,to_login,to_bch_name,to_block_number,to_block_hash,line_code,msg_sub_type,this_line_number
|
SELECT login,bch_name,block_number,block_hash,block_bytes,to_login,to_bch_name,to_block_number,to_block_hash,line_code,msg_sub_type,this_line_number
|
||||||
FROM blocks
|
FROM blocks
|
||||||
WHERE bch_name=? AND block_number=?
|
WHERE LOWER(login)=LOWER(?) AND block_number=?
|
||||||
|
AND fork_number=shine_canonical_fork(login,(SELECT substring(blockchain_name from '([0-9]{3})$')::INTEGER FROM solana_user_pda_current WHERE LOWER(login)=LOWER(?) LIMIT 1),block_number)
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""";
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, bchName);
|
String login=ChannelsReadSupport.loginFromChainName(bchName);
|
||||||
|
ps.setString(1, login);
|
||||||
ps.setInt(2, blockNumber);
|
ps.setInt(2, blockNumber);
|
||||||
|
ps.setString(3, login);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
return rs.next() ? mapRow(rs) : null;
|
return rs.next() ? mapRow(rs) : null;
|
||||||
}
|
}
|
||||||
@@ -184,13 +190,16 @@ public class Net_GetMessageThread_Handler implements JsonMessageHandler {
|
|||||||
String sql = """
|
String sql = """
|
||||||
SELECT login,bch_name,block_number,block_hash,block_bytes,to_login,to_bch_name,to_block_number,to_block_hash,line_code,msg_sub_type,this_line_number
|
SELECT login,bch_name,block_number,block_hash,block_bytes,to_login,to_bch_name,to_block_number,to_block_hash,line_code,msg_sub_type,this_line_number
|
||||||
FROM blocks
|
FROM blocks
|
||||||
WHERE bch_name=? AND block_number=? AND block_hash=?
|
WHERE LOWER(login)=LOWER(?) AND block_number=? AND block_hash=?
|
||||||
|
AND fork_number=shine_canonical_fork(login,(SELECT substring(blockchain_name from '([0-9]{3})$')::INTEGER FROM solana_user_pda_current WHERE LOWER(login)=LOWER(?) LIMIT 1),block_number)
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""";
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, bchName);
|
String login=ChannelsReadSupport.loginFromChainName(bchName);
|
||||||
|
ps.setString(1, login);
|
||||||
ps.setInt(2, blockNumber);
|
ps.setInt(2, blockNumber);
|
||||||
ps.setBytes(3, blockHash);
|
ps.setBytes(3, blockHash);
|
||||||
|
ps.setString(4, login);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
return rs.next() ? mapRow(rs) : null;
|
return rs.next() ? mapRow(rs) : null;
|
||||||
}
|
}
|
||||||
@@ -300,17 +309,18 @@ public class Net_GetMessageThread_Handler implements JsonMessageHandler {
|
|||||||
String sql = """
|
String sql = """
|
||||||
SELECT login,bch_name,block_number,block_hash,block_bytes,to_login,to_bch_name,to_block_number,to_block_hash,line_code,msg_sub_type,this_line_number
|
SELECT login,bch_name,block_number,block_hash,block_bytes,to_login,to_bch_name,to_block_number,to_block_hash,line_code,msg_sub_type,this_line_number
|
||||||
FROM blocks
|
FROM blocks
|
||||||
WHERE bch_name=? AND msg_type=1 AND msg_sub_type=?
|
WHERE LOWER(login)=LOWER(?) AND msg_type=1 AND msg_sub_type=?
|
||||||
AND to_block_number=? AND to_block_hash=?
|
AND to_block_number=? AND to_block_hash=?
|
||||||
AND (to_bch_name=? OR to_bch_name IS NULL OR to_bch_name='')
|
AND fork_number=shine_canonical_fork(login,(SELECT substring(blockchain_name from '([0-9]{3})$')::INTEGER FROM solana_user_pda_current WHERE LOWER(login)=LOWER(?) LIMIT 1),block_number)
|
||||||
ORDER BY block_number ASC
|
ORDER BY block_number ASC
|
||||||
""";
|
""";
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, bch);
|
String login=ChannelsReadSupport.loginFromChainName(bch);
|
||||||
|
ps.setString(1, login);
|
||||||
ps.setInt(2, subType);
|
ps.setInt(2, subType);
|
||||||
ps.setInt(3, targetBlock);
|
ps.setInt(3, targetBlock);
|
||||||
ps.setBytes(4, targetHash);
|
ps.setBytes(4, targetHash);
|
||||||
ps.setString(5, bch);
|
ps.setString(5, login);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
List<PostRow> out = new ArrayList<>();
|
List<PostRow> out = new ArrayList<>();
|
||||||
while (rs.next()) out.add(mapRow(rs));
|
while (rs.next()) out.add(mapRow(rs));
|
||||||
|
|||||||
+6
-7
@@ -89,9 +89,8 @@ public class Net_GetPersonalDiary_Handler implements JsonMessageHandler {
|
|||||||
private String loadPrimaryBlockchainName(Connection c, String canonicalLogin) throws Exception {
|
private String loadPrimaryBlockchainName(Connection c, String canonicalLogin) throws Exception {
|
||||||
try (PreparedStatement ps = c.prepareStatement("""
|
try (PreparedStatement ps = c.prepareStatement("""
|
||||||
SELECT blockchain_name
|
SELECT blockchain_name
|
||||||
FROM blockchain_state
|
FROM solana_user_pda_current
|
||||||
WHERE login = ?
|
WHERE LOWER(login)=LOWER(?)
|
||||||
ORDER BY blockchain_name
|
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
""")) {
|
""")) {
|
||||||
ps.setString(1, canonicalLogin);
|
ps.setString(1, canonicalLogin);
|
||||||
@@ -177,20 +176,20 @@ public class Net_GetPersonalDiary_Handler implements JsonMessageHandler {
|
|||||||
try (PreparedStatement ps = c.prepareStatement("""
|
try (PreparedStatement ps = c.prepareStatement("""
|
||||||
SELECT block_number, block_hash, block_bytes
|
SELECT block_number, block_hash, block_bytes
|
||||||
FROM blocks
|
FROM blocks
|
||||||
WHERE bch_name = ?
|
WHERE LOWER(login)=LOWER(?)
|
||||||
AND msg_type = ?
|
AND msg_type = ?
|
||||||
AND msg_sub_type = ?
|
AND msg_sub_type = ?
|
||||||
AND to_block_number = ?
|
AND to_block_number = ?
|
||||||
AND to_block_hash = ?
|
AND to_block_hash = ?
|
||||||
AND (to_bch_name = ? OR to_bch_name IS NULL OR to_bch_name = '')
|
AND fork_number=shine_canonical_fork(login,(SELECT substring(blockchain_name from '([0-9]{3})$')::INTEGER FROM solana_user_pda_current WHERE LOWER(login)=LOWER(?) LIMIT 1),block_number)
|
||||||
ORDER BY block_number ASC
|
ORDER BY block_number ASC
|
||||||
""")) {
|
""")) {
|
||||||
ps.setString(1, ownerBch);
|
ps.setString(1, ChannelsReadSupport.loginFromChainName(ownerBch));
|
||||||
ps.setInt(2, ChannelsReadSupport.MSG_TYPE_TEXT);
|
ps.setInt(2, ChannelsReadSupport.MSG_TYPE_TEXT);
|
||||||
ps.setInt(3, shine.db.MsgSubType.TEXT_EDIT_REPLY);
|
ps.setInt(3, shine.db.MsgSubType.TEXT_EDIT_REPLY);
|
||||||
ps.setInt(4, originalBlockNumber);
|
ps.setInt(4, originalBlockNumber);
|
||||||
ps.setBytes(5, originalBlockHash);
|
ps.setBytes(5, originalBlockHash);
|
||||||
ps.setString(6, ownerBch);
|
ps.setString(6, ChannelsReadSupport.loginFromChainName(ownerBch));
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
ChannelsReadSupport.TextInfo editText = ChannelsReadSupport.parseTextAndTime(rs.getBytes("block_bytes"));
|
ChannelsReadSupport.TextInfo editText = ChannelsReadSupport.parseTextAndTime(rs.getBytes("block_bytes"));
|
||||||
|
|||||||
+2
@@ -18,6 +18,8 @@ import java.sql.ResultSet;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
/** @deprecated Chat200 is legacy/non-working/unused and is not maintained for segmented forks. */
|
||||||
|
@Deprecated
|
||||||
public class Net_ListGroupChats200_Handler implements JsonMessageHandler {
|
public class Net_ListGroupChats200_Handler implements JsonMessageHandler {
|
||||||
private static final Logger log = LoggerFactory.getLogger(Net_ListGroupChats200_Handler.class);
|
private static final Logger log = LoggerFactory.getLogger(Net_ListGroupChats200_Handler.class);
|
||||||
|
|
||||||
|
|||||||
+8
-21
@@ -117,27 +117,14 @@ public class Net_ListSubscriptionsFeed_Handler implements JsonMessageHandler {
|
|||||||
|
|
||||||
private List<ChannelKey> loadOwnChannels(Connection c, String canonicalLogin) throws Exception {
|
private List<ChannelKey> loadOwnChannels(Connection c, String canonicalLogin) throws Exception {
|
||||||
List<ChannelKey> out = new ArrayList<>();
|
List<ChannelKey> out = new ArrayList<>();
|
||||||
String bchSql = "SELECT blockchain_name FROM blockchain_state WHERE login=? ORDER BY blockchain_name";
|
String activeBch = null;
|
||||||
try (PreparedStatement bchPs = c.prepareStatement(bchSql)) {
|
try (PreparedStatement ps=c.prepareStatement("SELECT blockchain_name FROM solana_user_pda_current WHERE LOWER(login)=LOWER(?) LIMIT 1")){
|
||||||
bchPs.setString(1, canonicalLogin);
|
ps.setString(1,canonicalLogin);try(ResultSet rs=ps.executeQuery()){if(rs.next())activeBch=rs.getString(1);}
|
||||||
try (ResultSet bchRs = bchPs.executeQuery()) {
|
|
||||||
while (bchRs.next()) {
|
|
||||||
String bch = bchRs.getString("blockchain_name");
|
|
||||||
out.add(new ChannelKey(canonicalLogin, bch, 0, new byte[32]));
|
|
||||||
|
|
||||||
String chSql = "SELECT block_number,block_hash FROM blocks WHERE bch_name=? AND msg_type=? AND msg_sub_type=? ORDER BY block_number";
|
|
||||||
try (PreparedStatement chPs = c.prepareStatement(chSql)) {
|
|
||||||
chPs.setString(1, bch);
|
|
||||||
chPs.setInt(2, ChannelsReadSupport.MSG_TYPE_TECH);
|
|
||||||
chPs.setInt(3, 1);
|
|
||||||
try (ResultSet chRs = chPs.executeQuery()) {
|
|
||||||
while (chRs.next()) {
|
|
||||||
out.add(new ChannelKey(canonicalLogin, bch, chRs.getInt("block_number"), chRs.getBytes("block_hash")));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if(activeBch==null||activeBch.isBlank())return out;
|
||||||
|
out.add(new ChannelKey(canonicalLogin,activeBch,0,new byte[32]));
|
||||||
|
try(PreparedStatement ps=c.prepareStatement("SELECT owner_bch_name,channel_root_block_number,channel_root_block_hash FROM channel_names_state WHERE LOWER(owner_login)=LOWER(?) ORDER BY channel_root_block_number")){
|
||||||
|
ps.setString(1,canonicalLogin);try(ResultSet rs=ps.executeQuery()){while(rs.next())out.add(new ChannelKey(canonicalLogin,rs.getString(1),rs.getInt(2),rs.getBytes(3)));}
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
@@ -146,7 +133,7 @@ public class Net_ListSubscriptionsFeed_Handler implements JsonMessageHandler {
|
|||||||
List<ChannelKey> out = new ArrayList<>();
|
List<ChannelKey> out = new ArrayList<>();
|
||||||
String sql = """
|
String sql = """
|
||||||
SELECT cs.to_login,
|
SELECT cs.to_login,
|
||||||
COALESCE(cn.owner_bch_name,(SELECT bs.blockchain_name FROM blockchain_state bs WHERE LOWER(bs.login)=LOWER(cs.to_login) ORDER BY bs.fork_number DESC LIMIT 1)) AS to_bch_name,
|
COALESCE(cn.owner_bch_name,(SELECT su.blockchain_name FROM solana_user_pda_current su WHERE LOWER(su.login)=LOWER(cs.to_login) LIMIT 1)) AS to_bch_name,
|
||||||
COALESCE(cs.to_block_number,0) AS root_number, cs.to_block_hash
|
COALESCE(cs.to_block_number,0) AS root_number, cs.to_block_hash
|
||||||
FROM connections_state cs
|
FROM connections_state cs
|
||||||
LEFT JOIN channel_names_state cn ON LOWER(cn.owner_login)=LOWER(cs.to_login) AND cn.channel_root_block_number=cs.to_block_number AND cn.channel_root_block_hash=cs.to_block_hash
|
LEFT JOIN channel_names_state cn ON LOWER(cn.owner_login)=LOWER(cs.to_login) AND cn.channel_root_block_number=cs.to_block_number AND cn.channel_root_block_hash=cs.to_block_hash
|
||||||
|
|||||||
+5
-3
@@ -73,7 +73,7 @@ public class Net_AddCloseFriend_Handler implements JsonMessageHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String findPrimaryBlockchain(Connection c, String login) throws Exception {
|
private String findPrimaryBlockchain(Connection c, String login) throws Exception {
|
||||||
String sql = "SELECT blockchain_name FROM blockchain_state WHERE LOWER(login) = LOWER(?) ORDER BY blockchain_name LIMIT 1";
|
String sql = "SELECT blockchain_name FROM solana_user_pda_current WHERE LOWER(login)=LOWER(?) LIMIT 1";
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, login);
|
ps.setString(1, login);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
@@ -83,9 +83,11 @@ public class Net_AddCloseFriend_Handler implements JsonMessageHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private byte[] findHeaderHash(Connection c, String blockchainName) throws Exception {
|
private byte[] findHeaderHash(Connection c, String blockchainName) throws Exception {
|
||||||
String sql = "SELECT block_hash FROM blocks WHERE bch_name = ? AND block_number = 0 LIMIT 1";
|
String login = blockchainName.substring(0, blockchainName.lastIndexOf('-'));
|
||||||
|
// Only fork1 owns HEADER #0 in segmented history.
|
||||||
|
String sql = "SELECT block_hash FROM blocks_store WHERE LOWER(login)=LOWER(?) AND block_number=0 ORDER BY fork_number ASC LIMIT 1";
|
||||||
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
try (PreparedStatement ps = c.prepareStatement(sql)) {
|
||||||
ps.setString(1, blockchainName);
|
ps.setString(1, login);
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
try (ResultSet rs = ps.executeQuery()) {
|
||||||
return rs.next() ? rs.getBytes("block_hash") : null;
|
return rs.next() ? rs.getBytes("block_hash") : null;
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-29
@@ -36,6 +36,7 @@ public final class Net_KeyRotationAddBlock_Handler implements JsonMessageHandler
|
|||||||
private final KeyRotationSessionsDAO rotations = KeyRotationSessionsDAO.getInstance();
|
private final KeyRotationSessionsDAO rotations = KeyRotationSessionsDAO.getInstance();
|
||||||
private final KeyRotationCandidateBlocksDAO candidates = KeyRotationCandidateBlocksDAO.getInstance();
|
private final KeyRotationCandidateBlocksDAO candidates = KeyRotationCandidateBlocksDAO.getInstance();
|
||||||
private final BlocksDAO blocks = BlocksDAO.getInstance();
|
private final BlocksDAO blocks = BlocksDAO.getInstance();
|
||||||
|
private final shine.db.dao.CanonicalChainResolver canonicalChain = shine.db.dao.CanonicalChainResolver.getInstance();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Net_Response handle(Net_Request baseReq, ConnectionContext ctx) {
|
public Net_Response handle(Net_Request baseReq, ConnectionContext ctx) {
|
||||||
@@ -67,25 +68,22 @@ public final class Net_KeyRotationAddBlock_Handler implements JsonMessageHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
int stored = candidates.countStored(c, session.getId());
|
int stored = candidates.countStored(c, session.getId());
|
||||||
if (req.getBlockNumber() < stored) {
|
int expectedBlockNumber = session.getForkFromBlock() + 1;
|
||||||
KeyRotationCandidateBlockEntry existing = candidates.getByNumber(c, session.getId(), req.getBlockNumber());
|
KeyRotationCandidateBlockEntry existing = candidates.getByNumber(c, session.getId(), req.getBlockNumber());
|
||||||
if (existing == null) {
|
if (existing != null) {
|
||||||
return NetExceptionResponseFactory.error(req, 409, "KEY_ROTATION_CANDIDATE_GAP", "Нарушена последовательность candidate-блоков");
|
|
||||||
}
|
|
||||||
BchBlockEntry repeated;
|
BchBlockEntry repeated;
|
||||||
try { repeated = new BchBlockEntry(Base64Ws.decode(req.getBlockBytesB64())); }
|
try { repeated = new BchBlockEntry(Base64Ws.decode(req.getBlockBytesB64())); }
|
||||||
catch (Exception e) { return NetExceptionResponseFactory.error(req, 400, "KEY_ROTATION_BAD_BLOCK", "Не удалось распарсить DataItem"); }
|
catch (Exception e) { return NetExceptionResponseFactory.error(req, 400, "KEY_ROTATION_BAD_BLOCK", "Не удалось распарсить DataItem"); }
|
||||||
if (!Arrays.equals(existing.getBlockHash(), repeated.getHash32())
|
if (!Arrays.equals(existing.getBlockHash(), repeated.getHash32()) || !Arrays.equals(existing.getDataItemId(), repeated.getDataItemId32())) {
|
||||||
|| !Arrays.equals(existing.getDataItemId(), repeated.getDataItemId32())) {
|
return NetExceptionResponseFactory.error(req, 409, "KEY_ROTATION_BLOCK_CONFLICT", "TECH_FORK уже сохранён с другим содержимым");
|
||||||
return NetExceptionResponseFactory.error(req, 409, "KEY_ROTATION_BLOCK_CONFLICT", "На этом номере уже сохранён другой candidate-блок");
|
|
||||||
}
|
}
|
||||||
return KeyRotationApiSupport.response(req.getOp(), req.getRequestId(), session);
|
return KeyRotationApiSupport.response(req.getOp(), req.getRequestId(), session);
|
||||||
}
|
}
|
||||||
if (req.getBlockNumber() != stored) {
|
if (req.getBlockNumber() != expectedBlockNumber) {
|
||||||
return NetExceptionResponseFactory.error(req, 409, "KEY_ROTATION_BLOCK_OUT_OF_ORDER", "Candidate-блоки нужно добавлять последовательно с block 0");
|
return NetExceptionResponseFactory.error(req, 409, "KEY_ROTATION_BLOCK_OUT_OF_ORDER", "Segmented fork принимает только TECH_FORK #" + expectedBlockNumber);
|
||||||
}
|
}
|
||||||
if (stored >= session.getProgressTotal()) {
|
if (stored >= 1) {
|
||||||
return NetExceptionResponseFactory.error(req, 409, "KEY_ROTATION_CHAIN_ALREADY_COMPLETE", "Все candidate-блоки уже приняты сервером");
|
return NetExceptionResponseFactory.error(req, 409, "KEY_ROTATION_CHAIN_ALREADY_COMPLETE", "TECH_FORK уже принят сервером");
|
||||||
}
|
}
|
||||||
|
|
||||||
final byte[] raw;
|
final byte[] raw;
|
||||||
@@ -113,11 +111,10 @@ public final class Net_KeyRotationAddBlock_Handler implements JsonMessageHandler
|
|||||||
}
|
}
|
||||||
byte[] expectedBack32 = new byte[32];
|
byte[] expectedBack32 = new byte[32];
|
||||||
if (block.blockNumber >= 32) {
|
if (block.blockNumber >= 32) {
|
||||||
KeyRotationCandidateBlockEntry back = candidates.getByNumber(c, session.getId(), block.blockNumber - 32);
|
expectedBack32 = canonicalChain.getHash(c, session.getSourceBlockchainName(), block.blockNumber - 32);
|
||||||
if (back == null || back.getBlockHash() == null || back.getBlockHash().length != 32) {
|
if (expectedBack32 == null || expectedBack32.length != 32) {
|
||||||
return NetExceptionResponseFactory.error(req, 409, "KEY_ROTATION_BACK32_SOURCE_MISSING", "Нет candidate-блока N-32 для проверки back32Hash");
|
return NetExceptionResponseFactory.error(req, 409, "KEY_ROTATION_BACK32_SOURCE_MISSING", "Нет канонического блока N-32 для проверки back32Hash");
|
||||||
}
|
}
|
||||||
expectedBack32 = back.getBlockHash();
|
|
||||||
}
|
}
|
||||||
if (!Arrays.equals(block.back32Hash32, expectedBack32)) {
|
if (!Arrays.equals(block.back32Hash32, expectedBack32)) {
|
||||||
return NetExceptionResponseFactory.error(req, 400, "KEY_ROTATION_BACK32_HASH_MISMATCH", "back32Hash candidate-блока не совпадает с N-32");
|
return NetExceptionResponseFactory.error(req, 400, "KEY_ROTATION_BACK32_HASH_MISMATCH", "back32Hash candidate-блока не совпадает с N-32");
|
||||||
@@ -180,19 +177,6 @@ public final class Net_KeyRotationAddBlock_Handler implements JsonMessageHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String validateCandidate(Connection c, KeyRotationSessionEntry session, BchBlockEntry candidate) throws Exception {
|
private String validateCandidate(Connection c, KeyRotationSessionEntry session, BchBlockEntry candidate) throws Exception {
|
||||||
if (candidate.blockNumber <= session.getForkFromBlock()) {
|
|
||||||
BlockEntry sourceEntry = blocks.getByNumber(c, session.getSourceBlockchainName(), candidate.blockNumber);
|
|
||||||
if (sourceEntry == null || sourceEntry.getBlockBytes() == null) return "KEY_ROTATION_SOURCE_BLOCK_MISSING";
|
|
||||||
BchBlockEntry source = new BchBlockEntry(sourceEntry.getBlockBytes());
|
|
||||||
|
|
||||||
// Перепубликуется тот же Frame и те же ANS-104 tags/target/anchor; меняются только owner/signature/DataItem id.
|
|
||||||
if (!Arrays.equals(source.getFrameBytes(), candidate.getFrameBytes())) return "KEY_ROTATION_FRAME_MISMATCH";
|
|
||||||
if (!Arrays.equals(source.getDataItem().rawTags(), candidate.getDataItem().rawTags())) return "KEY_ROTATION_TAGS_MISMATCH";
|
|
||||||
if (!Arrays.equals(source.getDataItem().target(), candidate.getDataItem().target())) return "KEY_ROTATION_TARGET_MISMATCH";
|
|
||||||
if (!Arrays.equals(source.getDataItem().anchor(), candidate.getDataItem().anchor())) return "KEY_ROTATION_ANCHOR_MISMATCH";
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
int techForkNumber = session.getForkFromBlock() + 1;
|
int techForkNumber = session.getForkFromBlock() + 1;
|
||||||
if (candidate.blockNumber != techForkNumber) return "KEY_ROTATION_UNEXPECTED_BLOCK_NUMBER";
|
if (candidate.blockNumber != techForkNumber) return "KEY_ROTATION_UNEXPECTED_BLOCK_NUMBER";
|
||||||
if ((candidate.type & 0xFFFF) != 0
|
if ((candidate.type & 0xFFFF) != 0
|
||||||
@@ -202,7 +186,7 @@ public final class Net_KeyRotationAddBlock_Handler implements JsonMessageHandler
|
|||||||
}
|
}
|
||||||
if (!Arrays.equals(candidate.prevHash32, session.getForkFromHash())) return "KEY_ROTATION_TECH_FORK_PREV_HASH";
|
if (!Arrays.equals(candidate.prevHash32, session.getForkFromHash())) return "KEY_ROTATION_TECH_FORK_PREV_HASH";
|
||||||
|
|
||||||
BlockEntry forkSourceEntry = blocks.getByNumber(c, session.getSourceBlockchainName(), session.getForkFromBlock());
|
BlockEntry forkSourceEntry = canonicalChain.getBlock(c, session.getSourceBlockchainName(), session.getForkFromBlock());
|
||||||
BlockEntry tipSourceEntry = blocks.getByNumber(c, session.getSourceBlockchainName(), session.getSourceTipBlock());
|
BlockEntry tipSourceEntry = blocks.getByNumber(c, session.getSourceBlockchainName(), session.getSourceTipBlock());
|
||||||
if (forkSourceEntry == null || tipSourceEntry == null) return "KEY_ROTATION_SOURCE_BLOCK_MISSING";
|
if (forkSourceEntry == null || tipSourceEntry == null) return "KEY_ROTATION_SOURCE_BLOCK_MISSING";
|
||||||
BchBlockEntry forkSource = new BchBlockEntry(forkSourceEntry.getBlockBytes());
|
BchBlockEntry forkSource = new BchBlockEntry(forkSourceEntry.getBlockBytes());
|
||||||
|
|||||||
+38
-86
@@ -39,6 +39,7 @@ public final class Net_KeyRotationFinishChain_Handler implements JsonMessageHand
|
|||||||
private final KeyRotationSessionsDAO rotations = KeyRotationSessionsDAO.getInstance();
|
private final KeyRotationSessionsDAO rotations = KeyRotationSessionsDAO.getInstance();
|
||||||
private final KeyRotationCandidateBlocksDAO candidates = KeyRotationCandidateBlocksDAO.getInstance();
|
private final KeyRotationCandidateBlocksDAO candidates = KeyRotationCandidateBlocksDAO.getInstance();
|
||||||
private final BlocksDAO blocks = BlocksDAO.getInstance();
|
private final BlocksDAO blocks = BlocksDAO.getInstance();
|
||||||
|
private final shine.db.dao.CanonicalChainResolver canonicalChain = shine.db.dao.CanonicalChainResolver.getInstance();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Net_Response handle(Net_Request baseReq, ConnectionContext ctx) {
|
public Net_Response handle(Net_Request baseReq, ConnectionContext ctx) {
|
||||||
@@ -124,96 +125,47 @@ public final class Net_KeyRotationFinishChain_Handler implements JsonMessageHand
|
|||||||
private String validateCompleteChain(Connection c,
|
private String validateCompleteChain(Connection c,
|
||||||
KeyRotationSessionEntry session,
|
KeyRotationSessionEntry session,
|
||||||
List<KeyRotationCandidateBlockEntry> entries) throws Exception {
|
List<KeyRotationCandidateBlockEntry> entries) throws Exception {
|
||||||
if (entries == null || entries.size() != session.getProgressTotal()) return "KEY_ROTATION_CHAIN_INCOMPLETE";
|
if (session.getProgressTotal() != 1 || entries == null || entries.size() != 1) return "KEY_ROTATION_CHAIN_INCOMPLETE";
|
||||||
if (session.getProgressTotal() != session.getForkFromBlock() + 2) return "KEY_ROTATION_BAD_PROGRESS_TOTAL";
|
KeyRotationCandidateBlockEntry stored=entries.get(0);
|
||||||
|
int expectedNumber=session.getForkFromBlock()+1;
|
||||||
byte[] newBlockchainKey = Base64.getDecoder().decode(session.getNewBlockchainKey());
|
if(stored.getBlockNumber()!=expectedNumber) return "KEY_ROTATION_CANDIDATE_GAP";
|
||||||
if (newBlockchainKey.length != 32) return "KEY_ROTATION_BAD_STORED_KEY";
|
if(stored.isArweavePublishPending() || stored.getArweavePublishedAtMs()==null) return "KEY_ROTATION_CHAIN_NOT_PUBLISHED";
|
||||||
|
|
||||||
BchBlockEntry previous = null;
|
|
||||||
for (int i = 0; i < entries.size(); i++) {
|
|
||||||
KeyRotationCandidateBlockEntry stored = entries.get(i);
|
|
||||||
if (stored.getBlockNumber() != i) return "KEY_ROTATION_CANDIDATE_GAP";
|
|
||||||
if (stored.isArweavePublishPending() || stored.getArweavePublishedAtMs() == null) {
|
|
||||||
return "KEY_ROTATION_CHAIN_NOT_PUBLISHED";
|
|
||||||
}
|
|
||||||
|
|
||||||
BchBlockEntry candidate;
|
BchBlockEntry candidate;
|
||||||
try {
|
try { candidate=new BchBlockEntry(stored.getBlockBytes()); candidate.body.check(); } catch(Exception e){ return "KEY_ROTATION_BAD_STORED_BLOCK"; }
|
||||||
candidate = new BchBlockEntry(stored.getBlockBytes());
|
if(candidate.blockNumber!=expectedNumber) return "KEY_ROTATION_BLOCK_NUMBER_MISMATCH";
|
||||||
candidate.body.check();
|
if(!Arrays.equals(candidate.getHash32(),stored.getBlockHash())) return "KEY_ROTATION_STORED_HASH_MISMATCH";
|
||||||
} catch (Exception e) {
|
if(!Arrays.equals(candidate.getDataItemId32(),stored.getDataItemId())) return "KEY_ROTATION_STORED_DATAITEM_MISMATCH";
|
||||||
return "KEY_ROTATION_BAD_STORED_BLOCK";
|
if(!candidate.getDataItem().hasTag(ShineProtocolTags.APP_TAG_NAME,ShineProtocolTags.APP_TAG_VALUE)) return "KEY_ROTATION_BAD_APP_TAG";
|
||||||
}
|
String expectedTypeTag=expectedTypeTag(candidate), actualTypeTag=candidate.getDataItem().tagValue(ShineProtocolTags.TYPE_TAG_NAME);
|
||||||
if (candidate.blockNumber != i) return "KEY_ROTATION_BLOCK_NUMBER_MISMATCH";
|
if(expectedTypeTag!=null?!expectedTypeTag.equals(actualTypeTag):actualTypeTag!=null) return "KEY_ROTATION_BAD_TYPE_TAG";
|
||||||
if (!Arrays.equals(candidate.getHash32(), stored.getBlockHash())) return "KEY_ROTATION_STORED_HASH_MISMATCH";
|
byte[] newKey=Base64.getDecoder().decode(session.getNewBlockchainKey());
|
||||||
if (!Arrays.equals(candidate.getDataItemId32(), stored.getDataItemId())) return "KEY_ROTATION_STORED_DATAITEM_MISMATCH";
|
if(newKey.length!=32 || !BchCryptoVerifier.verifyBlock(candidate,newKey)) return "KEY_ROTATION_BAD_SIGNATURE";
|
||||||
if (!candidate.getDataItem().hasTag(ShineProtocolTags.APP_TAG_NAME, ShineProtocolTags.APP_TAG_VALUE)) return "KEY_ROTATION_BAD_APP_TAG";
|
if(!Arrays.equals(candidate.prevHash32,session.getForkFromHash())) return "KEY_ROTATION_CHAIN_HASH_MISMATCH";
|
||||||
String expectedTypeTag = expectedTypeTag(candidate);
|
byte[] expectedBack32=new byte[32];
|
||||||
String actualTypeTag = candidate.getDataItem().tagValue(ShineProtocolTags.TYPE_TAG_NAME);
|
if(candidate.blockNumber>=32){ expectedBack32=canonicalChain.getHash(c,session.getSourceBlockchainName(),candidate.blockNumber-32); if(expectedBack32==null)return "KEY_ROTATION_BACK32_SOURCE_MISSING"; }
|
||||||
if (expectedTypeTag != null ? !expectedTypeTag.equals(actualTypeTag) : actualTypeTag != null) return "KEY_ROTATION_BAD_TYPE_TAG";
|
if(!Arrays.equals(candidate.back32Hash32,expectedBack32)) return "KEY_ROTATION_BACK32_HASH_MISMATCH";
|
||||||
if (!BchCryptoVerifier.verifyBlock(candidate, newBlockchainKey)) return "KEY_ROTATION_BAD_SIGNATURE";
|
return validateAgainstRotation(c,session,candidate);
|
||||||
|
|
||||||
if (i == 0) {
|
|
||||||
if (!isZero32(candidate.prevHash32)) return "KEY_ROTATION_GENESIS_PREV_HASH";
|
|
||||||
} else {
|
|
||||||
if (previous == null || !Arrays.equals(candidate.prevHash32, previous.getHash32())) {
|
|
||||||
return "KEY_ROTATION_CHAIN_HASH_MISMATCH";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (i < 32) {
|
|
||||||
if (!isZero32(candidate.back32Hash32)) return "KEY_ROTATION_BACK32_HASH_MISMATCH";
|
|
||||||
} else {
|
|
||||||
byte[] expectedBack32 = entries.get(i - 32).getBlockHash();
|
|
||||||
if (expectedBack32 == null || !Arrays.equals(candidate.back32Hash32, expectedBack32)) {
|
|
||||||
return "KEY_ROTATION_BACK32_HASH_MISMATCH";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
String semanticError = validateAgainstRotation(c, session, candidate);
|
|
||||||
if (semanticError != null) return semanticError;
|
|
||||||
previous = candidate;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String validateAgainstRotation(Connection c, KeyRotationSessionEntry session, BchBlockEntry candidate) throws Exception {
|
private String validateAgainstRotation(Connection c, KeyRotationSessionEntry session, BchBlockEntry candidate) throws Exception {
|
||||||
if (candidate.blockNumber <= session.getForkFromBlock()) {
|
if(candidate.blockNumber!=session.getForkFromBlock()+1) return "KEY_ROTATION_UNEXPECTED_BLOCK_NUMBER";
|
||||||
BlockEntry sourceEntry = blocks.getByNumber(c, session.getSourceBlockchainName(), candidate.blockNumber);
|
if((candidate.type&0xFFFF)!=0 || (candidate.subType&0xFFFF)!=(MsgSubType.TECH_FORK&0xFFFF) || !(candidate.body instanceof ForkBody fork)) return "KEY_ROTATION_TECH_FORK_REQUIRED";
|
||||||
if (sourceEntry == null || sourceEntry.getBlockBytes() == null) return "KEY_ROTATION_SOURCE_BLOCK_MISSING";
|
if(!Arrays.equals(candidate.prevHash32,session.getForkFromHash())) return "KEY_ROTATION_TECH_FORK_PREV_HASH";
|
||||||
BchBlockEntry source = new BchBlockEntry(sourceEntry.getBlockBytes());
|
BlockEntry forkSourceEntry=canonicalChain.getBlock(c,session.getSourceBlockchainName(),session.getForkFromBlock());
|
||||||
if (!Arrays.equals(source.getFrameBytes(), candidate.getFrameBytes())) return "KEY_ROTATION_FRAME_MISMATCH";
|
BlockEntry tipSourceEntry=blocks.getByNumber(c,session.getSourceBlockchainName(),session.getSourceTipBlock());
|
||||||
if (!Arrays.equals(source.getDataItem().rawTags(), candidate.getDataItem().rawTags())) return "KEY_ROTATION_TAGS_MISMATCH";
|
if(forkSourceEntry==null||tipSourceEntry==null)return "KEY_ROTATION_SOURCE_BLOCK_MISSING";
|
||||||
if (!Arrays.equals(source.getDataItem().target(), candidate.getDataItem().target())) return "KEY_ROTATION_TARGET_MISMATCH";
|
BchBlockEntry forkSource=new BchBlockEntry(forkSourceEntry.getBlockBytes()), tipSource=new BchBlockEntry(tipSourceEntry.getBlockBytes());
|
||||||
if (!Arrays.equals(source.getDataItem().anchor(), candidate.getDataItem().anchor())) return "KEY_ROTATION_ANCHOR_MISMATCH";
|
byte[] oldBlockchain=Base64.getDecoder().decode(session.getOldBlockchainKey());
|
||||||
return null;
|
if(!Arrays.equals(fork.parentBlockchainKey32,oldBlockchain))return "KEY_ROTATION_TECH_FORK_PARENT_KEY";
|
||||||
}
|
if(fork.forkPointBlockNumber!=session.getForkFromBlock())return "KEY_ROTATION_TECH_FORK_POINT";
|
||||||
|
if(!Arrays.equals(fork.forkPointBlockHash32,session.getForkFromHash()))return "KEY_ROTATION_TECH_FORK_POINT_HASH";
|
||||||
if (candidate.blockNumber != session.getForkFromBlock() + 1) return "KEY_ROTATION_UNEXPECTED_BLOCK_NUMBER";
|
if(fork.forkPointTimestampMs!=Math.multiplyExact(forkSource.timestamp,1000L))return "KEY_ROTATION_TECH_FORK_POINT_TIME";
|
||||||
if ((candidate.type & 0xFFFF) != 0
|
if(fork.parentTipBlockNumber!=session.getSourceTipBlock())return "KEY_ROTATION_TECH_FORK_TIP";
|
||||||
|| (candidate.subType & 0xFFFF) != (MsgSubType.TECH_FORK & 0xFFFF)
|
if(!Arrays.equals(fork.parentTipBlockHash32,session.getSourceTipHash()))return "KEY_ROTATION_TECH_FORK_TIP_HASH";
|
||||||
|| !(candidate.body instanceof ForkBody fork)) {
|
if(fork.parentTipTimestampMs!=Math.multiplyExact(tipSource.timestamp,1000L))return "KEY_ROTATION_TECH_FORK_TIP_TIME";
|
||||||
return "KEY_ROTATION_TECH_FORK_REQUIRED";
|
if(fork.discardedBlocksCount!=session.getSourceTipBlock()-session.getForkFromBlock())return "KEY_ROTATION_TECH_FORK_DISCARDED";
|
||||||
}
|
if(fork.reasonCode!=session.getReasonCode())return "KEY_ROTATION_TECH_FORK_REASON";
|
||||||
if (!Arrays.equals(candidate.prevHash32, session.getForkFromHash())) return "KEY_ROTATION_TECH_FORK_PREV_HASH";
|
if(!normalizeComment(fork.comment).equals(normalizeComment(session.getComment())))return "KEY_ROTATION_TECH_FORK_COMMENT";
|
||||||
|
|
||||||
BlockEntry forkSourceEntry = blocks.getByNumber(c, session.getSourceBlockchainName(), session.getForkFromBlock());
|
|
||||||
BlockEntry tipSourceEntry = blocks.getByNumber(c, session.getSourceBlockchainName(), session.getSourceTipBlock());
|
|
||||||
if (forkSourceEntry == null || tipSourceEntry == null) return "KEY_ROTATION_SOURCE_BLOCK_MISSING";
|
|
||||||
BchBlockEntry forkSource = new BchBlockEntry(forkSourceEntry.getBlockBytes());
|
|
||||||
BchBlockEntry tipSource = new BchBlockEntry(tipSourceEntry.getBlockBytes());
|
|
||||||
|
|
||||||
byte[] oldBlockchain = Base64.getDecoder().decode(session.getOldBlockchainKey());
|
|
||||||
if (!Arrays.equals(fork.parentBlockchainKey32, oldBlockchain)) return "KEY_ROTATION_TECH_FORK_PARENT_KEY";
|
|
||||||
if (fork.forkPointBlockNumber != session.getForkFromBlock()) return "KEY_ROTATION_TECH_FORK_POINT";
|
|
||||||
if (!Arrays.equals(fork.forkPointBlockHash32, session.getForkFromHash())) return "KEY_ROTATION_TECH_FORK_POINT_HASH";
|
|
||||||
if (fork.forkPointTimestampMs != Math.multiplyExact(forkSource.timestamp, 1000L)) return "KEY_ROTATION_TECH_FORK_POINT_TIME";
|
|
||||||
if (fork.parentTipBlockNumber != session.getSourceTipBlock()) return "KEY_ROTATION_TECH_FORK_TIP";
|
|
||||||
if (!Arrays.equals(fork.parentTipBlockHash32, session.getSourceTipHash())) return "KEY_ROTATION_TECH_FORK_TIP_HASH";
|
|
||||||
if (fork.parentTipTimestampMs != Math.multiplyExact(tipSource.timestamp, 1000L)) return "KEY_ROTATION_TECH_FORK_TIP_TIME";
|
|
||||||
if (fork.discardedBlocksCount != session.getSourceTipBlock() - session.getForkFromBlock()) return "KEY_ROTATION_TECH_FORK_DISCARDED";
|
|
||||||
if (fork.reasonCode != session.getReasonCode()) return "KEY_ROTATION_TECH_FORK_REASON";
|
|
||||||
if (!normalizeComment(fork.comment).equals(normalizeComment(session.getComment()))) return "KEY_ROTATION_TECH_FORK_COMMENT";
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-2
@@ -29,6 +29,7 @@ public final class Net_KeyRotationStart_Handler implements JsonMessageHandler {
|
|||||||
private final KeyRotationSessionsDAO rotations = KeyRotationSessionsDAO.getInstance();
|
private final KeyRotationSessionsDAO rotations = KeyRotationSessionsDAO.getInstance();
|
||||||
private final BlockchainStateDAO states = BlockchainStateDAO.getInstance();
|
private final BlockchainStateDAO states = BlockchainStateDAO.getInstance();
|
||||||
private final BlocksDAO blocks = BlocksDAO.getInstance();
|
private final BlocksDAO blocks = BlocksDAO.getInstance();
|
||||||
|
private final shine.db.dao.CanonicalChainResolver canonicalChain = shine.db.dao.CanonicalChainResolver.getInstance();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Net_Response handle(Net_Request baseReq, ConnectionContext ctx) {
|
public Net_Response handle(Net_Request baseReq, ConnectionContext ctx) {
|
||||||
@@ -113,7 +114,7 @@ public final class Net_KeyRotationStart_Handler implements JsonMessageHandler {
|
|||||||
c.rollback();
|
c.rollback();
|
||||||
return NetExceptionResponseFactory.error(req, 400, "KEY_ROTATION_BAD_FORK_POINT", "Точка fork находится после текущего tip");
|
return NetExceptionResponseFactory.error(req, 400, "KEY_ROTATION_BAD_FORK_POINT", "Точка fork находится после текущего tip");
|
||||||
}
|
}
|
||||||
BlockEntry forkBlock = blocks.getByNumber(c, pda.blockchainName, req.getForkFromBlock());
|
BlockEntry forkBlock = canonicalChain.getBlock(c, pda.blockchainName, req.getForkFromBlock());
|
||||||
if (forkBlock == null || forkBlock.getBlockHash() == null || forkBlock.getBlockHash().length != 32) {
|
if (forkBlock == null || forkBlock.getBlockHash() == null || forkBlock.getBlockHash().length != 32) {
|
||||||
c.rollback();
|
c.rollback();
|
||||||
return NetExceptionResponseFactory.error(req, 404, "KEY_ROTATION_FORK_BLOCK_NOT_FOUND", "Выбранный блок не найден на сервере");
|
return NetExceptionResponseFactory.error(req, 404, "KEY_ROTATION_FORK_BLOCK_NOT_FOUND", "Выбранный блок не найден на сервере");
|
||||||
@@ -148,7 +149,7 @@ public final class Net_KeyRotationStart_Handler implements JsonMessageHandler {
|
|||||||
entry.setReasonCode(req.getReasonCode());
|
entry.setReasonCode(req.getReasonCode());
|
||||||
entry.setComment(comment);
|
entry.setComment(comment);
|
||||||
entry.setProgressCurrent(0);
|
entry.setProgressCurrent(0);
|
||||||
entry.setProgressTotal(Math.addExact(req.getForkFromBlock(), 2)); // 0..N + TECH_FORK
|
entry.setProgressTotal(1); // segmented fork publishes only TECH_FORK
|
||||||
|
|
||||||
KeyRotationSessionEntry created = rotations.createCopyingSession(c, entry);
|
KeyRotationSessionEntry created = rotations.createCopyingSession(c, entry);
|
||||||
c.commit();
|
c.commit();
|
||||||
|
|||||||
+2
@@ -18,9 +18,11 @@ import java.util.concurrent.*;
|
|||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @deprecated Legacy direct server-to-server blockchain sync. Not compatible with segmented forks; Arweave/Turbo is supported.
|
||||||
* Фоновая репликация AddBlock через общий постоянный WSS-пул на серверы
|
* Фоновая репликация AddBlock через общий постоянный WSS-пул на серверы
|
||||||
* из локальной таблицы sync_servers.
|
* из локальной таблицы sync_servers.
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public final class AddBlockSyncService {
|
public final class AddBlockSyncService {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(AddBlockSyncService.class);
|
private static final Logger log = LoggerFactory.getLogger(AddBlockSyncService.class);
|
||||||
|
|||||||
@@ -1,165 +1,91 @@
|
|||||||
package server.keyrotation;
|
package server.keyrotation;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import blockchain.BchBlockEntry;
|
||||||
import org.slf4j.LoggerFactory;
|
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;
|
||||||
import server.logic.ws_protocol.JSON.handlers.blockchain.Net_AddBlock_Handler_utils.BlockchainLocks;
|
import server.logic.ws_protocol.JSON.handlers.blockchain.Net_AddBlock_Handler_utils.BlockchainLocks;
|
||||||
import server.sync.BlockchainResyncGuard;
|
import server.sync.BlockchainResyncGuard;
|
||||||
import shine.db.dao.BlockchainResyncCleanupDAO;
|
import shine.db.DbController;
|
||||||
import shine.db.dao.BlockchainStateDAO;
|
import shine.db.dao.*;
|
||||||
import shine.db.dao.KeyRotationCandidateBlocksDAO;
|
import shine.db.entities.*;
|
||||||
import shine.db.dao.KeyRotationSessionsDAO;
|
import java.sql.*;import java.util.*;import java.util.concurrent.locks.ReentrantLock;
|
||||||
import shine.db.dao.SolanaUserPdaCurrentDAO;
|
|
||||||
import shine.db.entities.BlockchainStateEntry;
|
|
||||||
import shine.db.entities.KeyRotationCandidateBlockEntry;
|
|
||||||
import shine.db.entities.KeyRotationSessionEntry;
|
|
||||||
import shine.db.entities.KeyRotationStatus;
|
|
||||||
import shine.db.entities.SolanaUserPdaCurrentEntry;
|
|
||||||
|
|
||||||
import java.util.Base64;
|
/** Deterministic segmented-fork rebuild: stage canonical raw blocks, clear projections, replay oldest->newest. */
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Автоматически materialize-ит candidate fork после того, как Solana sync подтвердил новое PDA.
|
|
||||||
* Candidate-блоки уже опубликованы в Arweave; здесь они только становятся единственной рабочей
|
|
||||||
* цепочкой PostgreSQL и повторно проходят обычный AddBlock validation/projection path.
|
|
||||||
*/
|
|
||||||
public final class KeyRotationRebuildService {
|
public final class KeyRotationRebuildService {
|
||||||
private static final Logger log = LoggerFactory.getLogger(KeyRotationRebuildService.class);
|
private static final Logger log=LoggerFactory.getLogger(KeyRotationRebuildService.class);
|
||||||
|
private final KeyRotationSessionsDAO rotations=KeyRotationSessionsDAO.getInstance();
|
||||||
|
private final KeyRotationCandidateBlocksDAO candidates=KeyRotationCandidateBlocksDAO.getInstance();
|
||||||
|
private final BlockchainResyncCleanupDAO cleanup=BlockchainResyncCleanupDAO.getInstance();
|
||||||
|
private final BlockchainStateDAO states=BlockchainStateDAO.getInstance();
|
||||||
|
private final SolanaUserPdaCurrentDAO users=SolanaUserPdaCurrentDAO.getInstance();
|
||||||
|
private final CanonicalChainResolver canonical=CanonicalChainResolver.getInstance();
|
||||||
|
private final BlockchainForkSegmentsDAO segments=BlockchainForkSegmentsDAO.getInstance();
|
||||||
|
private final Net_AddBlock_Handler addBlock=new Net_AddBlock_Handler();
|
||||||
|
|
||||||
private final KeyRotationSessionsDAO rotations = KeyRotationSessionsDAO.getInstance();
|
public int runReady(int limit){int processed=0;try{for(var s:rotations.listByStatus(KeyRotationStatus.PDA_ROTATED,limit))if(tryRebuild(s))processed++;for(var s:rotations.listByStatus(KeyRotationStatus.REBUILDING_SERVER,Math.max(1,limit-processed)))if(tryRebuild(s))processed++;}catch(Exception e){log.warn("Key rotation rebuild scan failed",e);}return processed;}
|
||||||
private final KeyRotationCandidateBlocksDAO candidates = KeyRotationCandidateBlocksDAO.getInstance();
|
public boolean tryRebuild(KeyRotationSessionEntry session){if(session==null)return false;try{var cur=rotations.getById(session.getId());if(cur==null)return false;if(cur.getStatus()==KeyRotationStatus.PDA_ROTATED)cur=rotations.transition(cur.getId(),KeyRotationStatus.PDA_ROTATED,KeyRotationStatus.REBUILDING_SERVER);if(cur.getStatus()!=KeyRotationStatus.REBUILDING_SERVER)return false;rebuild(cur);rotations.clearError(cur.getId());rotations.transition(cur.getId(),KeyRotationStatus.REBUILDING_SERVER,KeyRotationStatus.WALLET_MIGRATION);log.info("Segmented key rotation rebuild complete: login={} {} -> {}",cur.getLogin(),cur.getSourceBlockchainName(),cur.getCandidateBlockchainName());return true;}catch(Exception e){try{rotations.recordError(session.getId(),"REBUILDING_SERVER: "+safeMessage(e));}catch(Exception ignored){}log.warn("Key rotation rebuild failed: id={} login={}",session.getId(),session.getLogin(),e);return false;}}
|
||||||
private final BlockchainResyncCleanupDAO cleanup = BlockchainResyncCleanupDAO.getInstance();
|
|
||||||
private final BlockchainStateDAO states = BlockchainStateDAO.getInstance();
|
|
||||||
private final SolanaUserPdaCurrentDAO users = SolanaUserPdaCurrentDAO.getInstance();
|
|
||||||
private final Net_AddBlock_Handler addBlock = new Net_AddBlock_Handler();
|
|
||||||
|
|
||||||
public int runReady(int limit) {
|
private void rebuild(KeyRotationSessionEntry s)throws Exception{
|
||||||
int processed = 0;
|
verifyCurrentPda(s); List<KeyRotationCandidateBlockEntry> cb;try(Connection c=DbController.getInstance().getConnection()){cb=candidates.listBySession(c,s.getId());}
|
||||||
try {
|
if(cb.size()!=1||s.getProgressTotal()!=1)throw new IllegalStateException("segmented rotation requires exactly one TECH_FORK");
|
||||||
for (KeyRotationSessionEntry session : rotations.listByStatus(KeyRotationStatus.PDA_ROTATED, limit)) {
|
if(cb.get(0).isArweavePublishPending())throw new IllegalStateException("TECH_FORK is not published");
|
||||||
if (tryRebuild(session)) processed++;
|
String source=s.getSourceBlockchainName(),target=s.getCandidateBlockchainName();
|
||||||
}
|
ReentrantLock first=BlockchainLocks.lockFor(source.compareTo(target)<=0?source:target),second=BlockchainLocks.lockFor(source.compareTo(target)<=0?target:source);first.lock();second.lock();boolean sg=false,tg=false;
|
||||||
for (KeyRotationSessionEntry session : rotations.listByStatus(KeyRotationStatus.REBUILDING_SERVER, Math.max(1, limit - processed))) {
|
try{sg=BlockchainResyncGuard.tryBegin(source);tg=BlockchainResyncGuard.tryBegin(target);if(!sg||!tg)throw new IllegalStateException("another resync/rebuild is active");
|
||||||
if (tryRebuild(session)) processed++;
|
// Crash-safe invariant: staging is the durable immutable source for this rebuild.
|
||||||
}
|
// If it already exists, a previous attempt may have crashed at ANY point after staging.
|
||||||
} catch (Exception e) {
|
// Therefore every retry deliberately repeats cleanup + segment registration + replay from #0.
|
||||||
log.warn("Key rotation rebuild scan failed", e);
|
if(!hasStaging(s.getId())) stageCanonical(s,cb.get(0));
|
||||||
}
|
validateStaging(s);
|
||||||
return processed;
|
cleanup.cleanupLoginForCanonicalReplay(s.getLogin());
|
||||||
|
registerCandidateSegment(s);
|
||||||
|
replayStaging(s);
|
||||||
|
cleanup.finalizeCanonicalReplay(s.getLogin());
|
||||||
|
verifyReplayResult(s,cb.get(0));
|
||||||
|
clearStaging(s.getId());
|
||||||
|
}finally{if(tg)BlockchainResyncGuard.end(target);if(sg)BlockchainResyncGuard.end(source);second.unlock();first.unlock();}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean tryRebuild(KeyRotationSessionEntry session) {
|
private boolean hasStaging(long id)throws SQLException{try(Connection c=DbController.getInstance().getConnection();PreparedStatement ps=c.prepareStatement("SELECT 1 FROM key_rotation_rebuild_staging WHERE rotation_session_id=? LIMIT 1")){ps.setLong(1,id);try(ResultSet rs=ps.executeQuery()){return rs.next();}}}
|
||||||
if (session == null) return false;
|
private void stageCanonical(KeyRotationSessionEntry s,KeyRotationCandidateBlockEntry tech)throws Exception{
|
||||||
try {
|
try(Connection c=DbController.getInstance().getConnection()){boolean ac=c.getAutoCommit();c.setAutoCommit(false);try{
|
||||||
KeyRotationSessionEntry current = rotations.getById(session.getId());
|
try(PreparedStatement d=c.prepareStatement("DELETE FROM key_rotation_rebuild_staging WHERE rotation_session_id=?")){d.setLong(1,s.getId());d.executeUpdate();}
|
||||||
if (current == null) return false;
|
String ins="INSERT INTO key_rotation_rebuild_staging(rotation_session_id,logical_block_number,physical_fork_number,blockchain_key,block_bytes,block_hash) VALUES(?,?,?,?,?,?)";
|
||||||
if (current.getStatus() == KeyRotationStatus.PDA_ROTATED) {
|
try(PreparedStatement ps=c.prepareStatement(ins)){
|
||||||
current = rotations.transition(current.getId(), KeyRotationStatus.PDA_ROTATED, KeyRotationStatus.REBUILDING_SERVER);
|
for(int n=0;n<=s.getForkFromBlock();n++){BlockEntry e=canonical.getBlock(c,s.getSourceBlockchainName(),n);if(e==null)throw new IllegalStateException("canonical block missing #"+n);BchBlockEntry b=new BchBlockEntry(e.getBlockBytes());ps.setLong(1,s.getId());ps.setInt(2,n);ps.setInt(3,e.getForkNumber());ps.setString(4,Base64.getEncoder().encodeToString(b.getOwner32()));ps.setBytes(5,e.getBlockBytes());ps.setBytes(6,e.getBlockHash());ps.addBatch();}
|
||||||
}
|
BchBlockEntry b=new BchBlockEntry(tech.getBlockBytes());ps.setLong(1,s.getId());ps.setInt(2,b.blockNumber);ps.setInt(3,forkOf(s.getCandidateBlockchainName()));ps.setString(4,s.getNewBlockchainKey());ps.setBytes(5,tech.getBlockBytes());ps.setBytes(6,tech.getBlockHash());ps.addBatch();ps.executeBatch();
|
||||||
if (current.getStatus() != KeyRotationStatus.REBUILDING_SERVER) return false;
|
}c.commit();}catch(Exception e){c.rollback();throw e;}finally{c.setAutoCommit(ac);}}
|
||||||
rebuild(current);
|
|
||||||
rotations.clearError(current.getId());
|
|
||||||
rotations.transition(current.getId(), KeyRotationStatus.REBUILDING_SERVER, KeyRotationStatus.WALLET_MIGRATION);
|
|
||||||
log.info("Key rotation rebuild complete: login={} {} -> {}", current.getLogin(), current.getSourceBlockchainName(), current.getCandidateBlockchainName());
|
|
||||||
return true;
|
|
||||||
} catch (Exception e) {
|
|
||||||
try { rotations.recordError(session.getId(), "REBUILDING_SERVER: " + safeMessage(e)); } catch (Exception ignored) { }
|
|
||||||
log.warn("Key rotation rebuild failed: id={} login={}", session.getId(), session.getLogin(), e);
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
private void registerCandidateSegment(KeyRotationSessionEntry s)throws Exception{try(Connection c=DbController.getInstance().getConnection()){segments.upsert(c,new BlockchainForkSegmentsDAO.Segment(s.getLogin(),forkOf(s.getCandidateBlockchainName()),forkOf(s.getSourceBlockchainName()),s.getForkFromBlock(),s.getForkFromHash(),s.getForkFromBlock()+1,System.currentTimeMillis()));}}
|
||||||
|
private void replayStaging(KeyRotationSessionEntry s)throws Exception{
|
||||||
|
SolanaUserPdaCurrentEntry u=users.getByLogin(s.getLogin());long limit=u!=null&&u.getPaidLimitBytes()>0?u.getPaidLimitBytes():100_000L;long cumulative=0;int currentFork=-1;int prevNumber=-1;byte[] prevHash=null;
|
||||||
|
try(Connection c=DbController.getInstance().getConnection();PreparedStatement ps=c.prepareStatement("SELECT logical_block_number,physical_fork_number,blockchain_key,block_bytes,block_hash FROM key_rotation_rebuild_staging WHERE rotation_session_id=? ORDER BY logical_block_number ASC")){ps.setLong(1,s.getId());try(ResultSet rs=ps.executeQuery()){while(rs.next()){
|
||||||
|
int n=rs.getInt(1),f=rs.getInt(2);String key=rs.getString(3);byte[] raw=rs.getBytes(4),hash=rs.getBytes(5);String bch=s.getLogin()+"-"+String.format("%03d",f);
|
||||||
|
if(f!=currentFork){BlockchainStateEntry st=new BlockchainStateEntry();st.setBlockchainName(bch);st.setLogin(s.getLogin());st.setBlockchainKey(key);st.setSizeLimit(limit);st.setFileSizeBytes(cumulative);st.setLastBlockNumber(prevNumber);st.setLastBlockHash(prevHash);st.setUpdatedAtMs(System.currentTimeMillis());states.insertIfMissing(st);currentFork=f;}
|
||||||
|
String target=bch;BlockchainResyncGuard.withBypass(target,()->{Net_AddBlock_Handler.ArweaveImportResult r=addBlock.addBlockFromReplay(target,raw);if(!r.ok())throw new IllegalStateException("canonical replay rejected #"+n+": "+r.reasonCode());});
|
||||||
|
cumulative+=raw.length;prevNumber=n;prevHash=hash;
|
||||||
|
}}}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void rebuild(KeyRotationSessionEntry session) throws Exception {
|
private void validateStaging(KeyRotationSessionEntry s)throws Exception{
|
||||||
verifyCurrentPda(session);
|
int expected=0,last=-1;byte[] previousHash=null;
|
||||||
List<KeyRotationCandidateBlockEntry> candidateBlocks;
|
try(Connection c=DbController.getInstance().getConnection();PreparedStatement ps=c.prepareStatement("SELECT logical_block_number,block_bytes,block_hash FROM key_rotation_rebuild_staging WHERE rotation_session_id=? ORDER BY logical_block_number ASC")){
|
||||||
try (var c = shine.db.DbController.getInstance().getConnection()) {
|
ps.setLong(1,s.getId());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("rebuild staging gap: expected #"+expected+" got #"+n);BchBlockEntry b=new BchBlockEntry(raw);if(b.blockNumber!=n)throw new IllegalStateException("rebuild staging block number mismatch #"+n);if(n==0 && !(b.body instanceof blockchain.body.HeaderBody))throw new IllegalStateException("canonical history must start with HEADER #0");if(n>0 && (previousHash==null||!Arrays.equals(b.prevHash32,previousHash)))throw new IllegalStateException("rebuild staging prevHash mismatch #"+n);if(hash==null||hash.length!=32||!Arrays.equals(hash,b.getHash32()))throw new IllegalStateException("rebuild staging hash mismatch #"+n);previousHash=hash;last=n;expected++;}}
|
||||||
candidateBlocks = candidates.listBySession(c, session.getId());
|
|
||||||
}
|
|
||||||
if (candidateBlocks.size() != session.getProgressTotal()) {
|
|
||||||
throw new IllegalStateException("candidate count mismatch: " + candidateBlocks.size() + "/" + session.getProgressTotal());
|
|
||||||
}
|
|
||||||
for (KeyRotationCandidateBlockEntry block : candidateBlocks) {
|
|
||||||
if (block.isArweavePublishPending()) {
|
|
||||||
throw new IllegalStateException("candidate block is not published: #" + block.getBlockNumber());
|
|
||||||
}
|
}
|
||||||
|
int expectedTip=s.getForkFromBlock()+1;if(last!=expectedTip)throw new IllegalStateException("rebuild staging tip mismatch: expected #"+expectedTip+" got #"+last);
|
||||||
}
|
}
|
||||||
|
|
||||||
String source = session.getSourceBlockchainName();
|
private void verifyReplayResult(KeyRotationSessionEntry s,KeyRotationCandidateBlockEntry tech)throws Exception{
|
||||||
String target = session.getCandidateBlockchainName();
|
BlockchainStateEntry st=states.getByBlockchainName(s.getCandidateBlockchainName());if(st==null)throw new IllegalStateException("candidate blockchain state missing after replay");
|
||||||
ReentrantLock first = BlockchainLocks.lockFor(source.compareTo(target) <= 0 ? source : target);
|
BchBlockEntry b=new BchBlockEntry(tech.getBlockBytes());
|
||||||
ReentrantLock second = BlockchainLocks.lockFor(source.compareTo(target) <= 0 ? target : source);
|
if(st.getLastBlockNumber()!=b.blockNumber)throw new IllegalStateException("replay tip number mismatch");
|
||||||
first.lock();
|
if(st.getLastBlockHash()==null||!Arrays.equals(st.getLastBlockHash(),b.getHash32()))throw new IllegalStateException("replay tip hash mismatch");
|
||||||
second.lock();
|
BlockEntry zero=canonical.getBlock(s.getCandidateBlockchainName(),0);if(zero==null){throw new IllegalStateException("canonical HEADER #0 missing after replay");}
|
||||||
boolean sourceGuard = false;
|
BchBlockEntry header=new BchBlockEntry(zero.getBlockBytes());if(!(header.body instanceof blockchain.body.HeaderBody))throw new IllegalStateException("canonical block #0 is not HEADER");
|
||||||
boolean targetGuard = false;
|
|
||||||
try {
|
|
||||||
sourceGuard = BlockchainResyncGuard.tryBegin(source);
|
|
||||||
targetGuard = BlockchainResyncGuard.tryBegin(target);
|
|
||||||
if (!sourceGuard || !targetGuard) {
|
|
||||||
throw new IllegalStateException("another resync/rebuild is active");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Повторный запуск безопасен: сначала убираем любую частично materialized candidate-цепочку.
|
private void clearStaging(long id)throws SQLException{try(Connection c=DbController.getInstance().getConnection();PreparedStatement ps=c.prepareStatement("DELETE FROM key_rotation_rebuild_staging WHERE rotation_session_id=?")){ps.setLong(1,id);ps.executeUpdate();}}
|
||||||
if (states.getByBlockchainName(target) != null) {
|
private void verifyCurrentPda(KeyRotationSessionEntry s)throws Exception{var u=users.getByLogin(s.getLogin());if(u==null)throw new IllegalStateException("current PDA not found");if(!s.getCandidateBlockchainName().equals(u.getBlockchainName()))throw new IllegalStateException("current PDA blockchainName mismatch");if(!s.getNewBlockchainKey().equals(u.getBlockchainKey()))throw new IllegalStateException("current PDA blockchain key mismatch");}
|
||||||
cleanup.cleanupBlockchainForFullResync(target);
|
private static int forkOf(String n){int d=n.lastIndexOf('-');return Integer.parseInt(n.substring(d+1));}
|
||||||
}
|
private static String safeMessage(Throwable t){String v=t==null?"unknown":String.valueOf(t.getMessage());return v.length()>1000?v.substring(0,1000):v;}
|
||||||
if (states.getByBlockchainName(source) != null) {
|
|
||||||
cleanup.cleanupBlockchainForFullResync(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
createCandidateState(session);
|
|
||||||
BlockchainResyncGuard.withBypass(target, () -> {
|
|
||||||
for (KeyRotationCandidateBlockEntry candidate : candidateBlocks) {
|
|
||||||
Net_AddBlock_Handler.ArweaveImportResult result = addBlock.addBlockFromArweave(target, candidate.getBlockBytes());
|
|
||||||
if (!result.ok()) {
|
|
||||||
throw new IllegalStateException("candidate replay rejected at #" + candidate.getBlockNumber() + ": " + result.reasonCode());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Чужие подписанные ссылки уже адресованы логически: login+number+hash.
|
|
||||||
// После replay никакого rebind/cache refresh для target-ссылок не требуется.
|
|
||||||
} finally {
|
|
||||||
if (targetGuard) BlockchainResyncGuard.end(target);
|
|
||||||
if (sourceGuard) BlockchainResyncGuard.end(source);
|
|
||||||
second.unlock();
|
|
||||||
first.unlock();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void verifyCurrentPda(KeyRotationSessionEntry session) throws Exception {
|
|
||||||
SolanaUserPdaCurrentEntry user = users.getByLogin(session.getLogin());
|
|
||||||
if (user == null) throw new IllegalStateException("current PDA not found");
|
|
||||||
if (!session.getCandidateBlockchainName().equals(user.getBlockchainName())) {
|
|
||||||
throw new IllegalStateException("current PDA blockchainName mismatch");
|
|
||||||
}
|
|
||||||
if (!session.getNewBlockchainKey().equals(user.getBlockchainKey())) {
|
|
||||||
throw new IllegalStateException("current PDA blockchain key mismatch");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void createCandidateState(KeyRotationSessionEntry session) throws Exception {
|
|
||||||
SolanaUserPdaCurrentEntry user = users.getByLogin(session.getLogin());
|
|
||||||
byte[] key = Base64.getDecoder().decode(session.getNewBlockchainKey());
|
|
||||||
if (key.length != 32) throw new IllegalStateException("new blockchain key length != 32");
|
|
||||||
|
|
||||||
BlockchainStateEntry state = new BlockchainStateEntry();
|
|
||||||
state.setBlockchainName(session.getCandidateBlockchainName());
|
|
||||||
state.setLogin(session.getLogin());
|
|
||||||
state.setBlockchainKey(session.getNewBlockchainKey());
|
|
||||||
state.setSizeLimit(user != null && user.getPaidLimitBytes() > 0 ? user.getPaidLimitBytes() : 100_000L);
|
|
||||||
state.setFileSizeBytes(0L);
|
|
||||||
state.setLastBlockNumber(-1);
|
|
||||||
state.setLastBlockHash(null);
|
|
||||||
state.setUpdatedAtMs(System.currentTimeMillis());
|
|
||||||
states.insertIfMissing(state);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String safeMessage(Throwable t) {
|
|
||||||
String value = t == null ? "unknown" : String.valueOf(t.getMessage());
|
|
||||||
if (value.length() > 1000) value = value.substring(0, 1000);
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,11 +28,13 @@ import java.util.concurrent.TimeUnit;
|
|||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @deprecated Legacy direct server-to-server blockchain sync. Not compatible with segmented forks; Arweave/Turbo is supported.
|
||||||
* Плановый межсерверный sync блокчейнов.
|
* Плановый межсерверный sync блокчейнов.
|
||||||
* Сейчас реализует:
|
* Сейчас реализует:
|
||||||
* - догоняющую синхронизацию отсутствующего хвоста;
|
* - догоняющую синхронизацию отсутствующего хвоста;
|
||||||
* - базовый full-resync при divergence, если удалённая цепочка сильнее.
|
* - базовый full-resync при divergence, если удалённая цепочка сильнее.
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public final class PeriodicBlockchainSyncService {
|
public final class PeriodicBlockchainSyncService {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(PeriodicBlockchainSyncService.class);
|
private static final Logger log = LoggerFactory.getLogger(PeriodicBlockchainSyncService.class);
|
||||||
|
|||||||
@@ -53,12 +53,14 @@ public final class WsServer {
|
|||||||
// ============================================================
|
// ============================================================
|
||||||
// 1.1) Загрузка списка серверов синхронизации из Solana PDA
|
// 1.1) Загрузка списка серверов синхронизации из Solana PDA
|
||||||
// ============================================================
|
// ============================================================
|
||||||
SyncServersBootstrapService.refreshFromSolanaOrLog();
|
// Legacy direct server-to-server sync is retired. Do not bootstrap sync_servers.
|
||||||
|
// Arweave/Turbo is the supported blockchain recovery/synchronization source.
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// 1.2) Плановый межсерверный sync блокчейнов
|
// 1.2) Плановый межсерверный sync блокчейнов
|
||||||
// ============================================================
|
// ============================================================
|
||||||
PeriodicBlockchainSyncService.startOrLog();
|
// Legacy direct server-to-server blockchain sync is intentionally disabled.
|
||||||
|
// PeriodicBlockchainSyncService is retained only as deprecated historical code.
|
||||||
|
|
||||||
// ANS-104: publish locally-created signed user blocks and discover blocks published by other servers.
|
// ANS-104: publish locally-created signed user blocks and discover blocks published by other servers.
|
||||||
ArweaveBlockPublisherScheduler.startOrLog();
|
ArweaveBlockPublisherScheduler.startOrLog();
|
||||||
|
|||||||
@@ -153,7 +153,11 @@ arweave.blocks.publish.confirmTimeoutMinutes=180
|
|||||||
arweave.blocks.sync.enabled=false
|
arweave.blocks.sync.enabled=false
|
||||||
arweave.blocks.sync.intervalMinutes=15
|
arweave.blocks.sync.intervalMinutes=15
|
||||||
arweave.blocks.sync.gateway=https://turbo-gateway.com
|
arweave.blocks.sync.gateway=https://turbo-gateway.com
|
||||||
|
# Fallback к metadata API Turbo, если общий AR.IO index ещё не знает offsets.
|
||||||
|
arweave.blocks.sync.turboApiBase=https://turbo.ardrive.io
|
||||||
|
# Уже найденные GraphQL DataItem с временно неизвестными координатами ретраятся чаще полного discovery.
|
||||||
|
arweave.blocks.sync.locationRetrySeconds=30
|
||||||
arweave.blocks.sync.pageSize=100
|
arweave.blocks.sync.pageSize=100
|
||||||
arweave.blocks.sync.queueBatchSize=10000
|
arweave.blocks.sync.queueBatchSize=10000
|
||||||
arweave.blocks.sync.startBlockHeight=0
|
arweave.blocks.sync.startBlockHeight=0
|
||||||
arweave.blocks.sync.maxRootBundleBytes=268435456
|
arweave.blocks.sync.maxDataItemBytes=8388608
|
||||||
|
|||||||
+2
-2
@@ -1,2 +1,2 @@
|
|||||||
client.version=1.14.8
|
client.version=1.14.9
|
||||||
server.version=1.12.6
|
server.version=1.12.7
|
||||||
|
|||||||
@@ -116,50 +116,29 @@ export class KeyRotationClient {
|
|||||||
const cutoff=Number(rotation.forkFromBlock); const source=String(rotation.sourceBlockchainName||'');
|
const cutoff=Number(rotation.forkFromBlock); const source=String(rotation.sourceBlockchainName||'');
|
||||||
if(!source || !Number.isInteger(cutoff) || cutoff<0) throw new Error('Некорректное состояние ротации');
|
if(!source || !Number.isInteger(cutoff) || cutoff<0) throw new Error('Некорректное состояние ротации');
|
||||||
if(String(newBundle?.blockchainPair?.publicKeyB64||'') !== String(rotation.newBlockchainKey||'')) throw new Error('Новый пароль выводит другой blockchain key');
|
if(String(newBundle?.blockchainPair?.publicKeyB64||'') !== String(rotation.newBlockchainKey||'')) throw new Error('Новый пароль выводит другой blockchain key');
|
||||||
const total=cutoff+2;
|
const total=1;
|
||||||
if(Number(rotation.progressCurrent)||0 >= total) {
|
if((Number(rotation.progressCurrent)||0)>=total){onProgress({phase:'copy',current:total,total});return;}
|
||||||
onProgress({ phase:'copy', current:total, total });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const owner32=base64ToBytes(newBundle.blockchainPair.publicKeyB64);
|
const owner32=base64ToBytes(newBundle.blockchainPair.publicKeyB64);
|
||||||
const privateKey=newBundle.blockchainPair.privateKey;
|
const privateKey=newBundle.blockchainPair.privateKey;
|
||||||
let start=Math.max(0, Math.min(cutoff+1, Number(rotation.progressCurrent)||0));
|
|
||||||
// progressCurrent tracks published, while server may already have more pending blocks. Re-sends are idempotent.
|
|
||||||
for(let n=start;n<=cutoff;n++) {
|
|
||||||
const src=await this.getBlock(source,n);
|
|
||||||
const parsed=parseAns104DataItem(base64ToBytes(src.blockBytesB64));
|
|
||||||
const raw=await createAns104DataItemWithRawParts({ owner32, privateKey, data:parsed.data, rawTags:parsed.rawTags, tagsCount:parsed.tagsCount, target:parsed.target, anchor:parsed.anchor });
|
|
||||||
await this.addCandidate({ blockNumber:n, prevBlockHash:n===0?ZERO_HASH:nullIfEmpty(src.prevBlockHash)||await this.#sourcePrevHash(source,n), blockBytesB64:bytesToBase64(raw) });
|
|
||||||
onProgress({ phase:'copy', current:n+1, total:cutoff+2 });
|
|
||||||
}
|
|
||||||
const forkSource=await this.getBlock(source,cutoff);
|
const forkSource=await this.getBlock(source,cutoff);
|
||||||
const tipBlock=Number(rotation.sourceTipBlock);
|
const tipBlock=Number(rotation.sourceTipBlock);
|
||||||
const tipSource=tipBlock===cutoff ? forkSource : await this.getBlock(source,tipBlock);
|
const tipSource=tipBlock===cutoff?forkSource:await this.getBlock(source,tipBlock);
|
||||||
const forkParsed=parseAns104DataItem(base64ToBytes(forkSource.blockBytesB64));
|
const forkParsed=parseAns104DataItem(base64ToBytes(forkSource.blockBytesB64));
|
||||||
const tipParsed=parseAns104DataItem(base64ToBytes(tipSource.blockBytesB64));
|
const tipParsed=parseAns104DataItem(base64ToBytes(tipSource.blockBytesB64));
|
||||||
const techTail=await this.#resolveTechLineTail(source,cutoff);
|
const techTail=await this.#resolveTechLineTail(source,cutoff);
|
||||||
const forkBody=makeForkBody({
|
const forkBody=makeForkBody({
|
||||||
lineCode:0,
|
lineCode:0, prevLineNumber:techTail.prevLineNumber, prevLineHash:techTail.prevLineHash, lineSequence:techTail.lineSequence,
|
||||||
prevLineNumber:techTail.prevLineNumber,
|
oldBlockchainKeyB64:rotation.oldBlockchainKey, forkPointBlock:cutoff, forkPointHash:rotation.forkFromHash,
|
||||||
prevLineHash:techTail.prevLineHash,
|
forkPointTimestampMs:readFrameTimestampMs(forkParsed.data), parentTipBlock:tipBlock, parentTipHash:rotation.sourceTipHash,
|
||||||
lineSequence:techTail.lineSequence,
|
parentTipTimestampMs:readFrameTimestampMs(tipParsed.data), reasonCode:rotation.reasonCode, comment:rotation.comment,
|
||||||
oldBlockchainKeyB64:rotation.oldBlockchainKey,
|
|
||||||
forkPointBlock:cutoff,
|
|
||||||
forkPointHash:rotation.forkFromHash,
|
|
||||||
forkPointTimestampMs:readFrameTimestampMs(forkParsed.data),
|
|
||||||
parentTipBlock:tipBlock,
|
|
||||||
parentTipHash:rotation.sourceTipHash,
|
|
||||||
parentTipTimestampMs:readFrameTimestampMs(tipParsed.data),
|
|
||||||
reasonCode:rotation.reasonCode,
|
|
||||||
comment:rotation.comment,
|
|
||||||
});
|
});
|
||||||
const techTimestampMs=Number(rotation.createdAtMs)||readFrameTimestampMs(tipParsed.data)+1000;
|
const techTimestampMs=Number(rotation.createdAtMs)||readFrameTimestampMs(tipParsed.data)+1000;
|
||||||
const techBlockNumber=cutoff+1;
|
const techBlockNumber=cutoff+1;
|
||||||
const techBack32Hash=techBlockNumber<32 ? ZERO_HASH : String((await this.getBlock(source,techBlockNumber-32)).blockHash||'');
|
const techBack32Hash=techBlockNumber<32?ZERO_HASH:String((await this.getBlock(source,techBlockNumber-32)).blockHash||'');
|
||||||
const techFrame=makeFrame({ prevHash:rotation.forkFromHash, back32Hash:techBack32Hash, blockNumber:techBlockNumber, type:0, subType:TECH_FORK_SUBTYPE, version:1, body:forkBody, timestampMs:techTimestampMs });
|
const techFrame=makeFrame({prevHash:rotation.forkFromHash,back32Hash:techBack32Hash,blockNumber:techBlockNumber,type:0,subType:TECH_FORK_SUBTYPE,version:1,body:forkBody,timestampMs:techTimestampMs});
|
||||||
const techRaw=await createAns104DataItem({ owner32, privateKey, data:techFrame, tags:[{name:SHINE_APP_TAG_NAME,value:SHINE_APP_TAG_VALUE},{name:SHINE_TYPE_TAG_NAME,value:'tech'}] });
|
const techRaw=await createAns104DataItem({owner32,privateKey,data:techFrame,tags:[{name:SHINE_APP_TAG_NAME,value:SHINE_APP_TAG_VALUE},{name:SHINE_TYPE_TAG_NAME,value:'tech'}]});
|
||||||
await this.addCandidate({ blockNumber:cutoff+1, prevBlockHash:rotation.forkFromHash, blockBytesB64:bytesToBase64(techRaw) });
|
await this.addCandidate({blockNumber:techBlockNumber,prevBlockHash:rotation.forkFromHash,blockBytesB64:bytesToBase64(techRaw)});
|
||||||
onProgress({ phase:'copy', current:total, total });
|
onProgress({phase:'copy',current:1,total:1});
|
||||||
}
|
}
|
||||||
|
|
||||||
async #sourcePrevHash(source, blockNumber) {
|
async #sourcePrevHash(source, blockNumber) {
|
||||||
|
|||||||
Reference in New Issue
Block a user