SHA256
69 lines
3.6 KiB
JavaScript
69 lines
3.6 KiB
JavaScript
import { base64ToBytes } from './crypto-utils.js';
|
|
import { unpackCoreContent, verifyCoreContentPair } from './core-content.js';
|
|
import { parseAns104DataItem } from './ans104-data-item.js';
|
|
import { parseServerEnvironment } from './server-environment.js';
|
|
import { uploadSignedBlockDataItem } from './turbo-upload-service.js';
|
|
import { showToast } from './channels-ux.js';
|
|
import { t } from '../i18n/index.js';
|
|
|
|
const running = new Set();
|
|
const scheduled = new Map();
|
|
const lastNotice = new Map();
|
|
|
|
/** После перезапуска браузер повторно получает подписанные bytes с сервера. */
|
|
export async function publishPendingBlocks({ ws, login, storagePwd, blockchainName }) {
|
|
if (!ws || ws.retired || !login || !storagePwd || !blockchainName) return;
|
|
const key = `${ws.url}|${blockchainName}`;
|
|
if (running.has(key)) return;
|
|
running.add(key);
|
|
let keepPolling = false;
|
|
try {
|
|
const env = parseServerEnvironment(await ws.request('GetServerInfo', {}, 9000));
|
|
const generation = ws.connectionGeneration;
|
|
const response = await ws.request('BlockPublication', { action: 'list', blockchainName });
|
|
if (response.status !== 200) return;
|
|
keepPolling = true;
|
|
for (const block of response.payload?.pendingBlocks || []) {
|
|
const read = await ws.request('GetBlockchainBlock', { blockchainName, blockNumber: block.blockNumber });
|
|
if (read.status !== 200 || String(read.payload?.blockHash || '').toLowerCase() !== block.coreHash) {
|
|
throw new Error('Pending block bytes unavailable or changed');
|
|
}
|
|
const { contentDataItem, coreDataItem } = unpackCoreContent(base64ToBytes(read.payload.blockBytesB64));
|
|
await verifyCoreContentPair(coreDataItem, contentDataItem, parseAns104DataItem(coreDataItem).owner32, env.arweaveAppTag);
|
|
if (ws.retired || ws.connectionGeneration !== generation) return;
|
|
let uploadError = '';
|
|
try {
|
|
if (!block.contentConfirmed) await uploadSignedBlockDataItem({ login, storagePwd, bytes: contentDataItem, expectedId: block.contentId });
|
|
if (ws.retired || ws.connectionGeneration !== generation) return;
|
|
if (!block.coreConfirmed) await uploadSignedBlockDataItem({ login, storagePwd, bytes: coreDataItem, expectedId: block.coreId });
|
|
} catch (error) {
|
|
uploadError = String(error?.message || error);
|
|
const insufficientCredits = /(?:\b402\b|insufficient (?:balance|funds|credits)|not enough (?:balance|funds|credits)|payment required|недостаточно (?:средств|кредитов))/i.test(uploadError);
|
|
const noticeKey = `${key}:${block.blockNumber}`;
|
|
if (Date.now() - (lastNotice.get(noticeKey) || 0) >= 30 * 60_000) {
|
|
showToast(t(insufficientCredits ? 'block_publication.insufficient_credits' : 'block_publication.upload_failed',
|
|
{ number: block.blockNumber }), { kind: 'error', timeoutMs: 7000 });
|
|
lastNotice.set(noticeKey, Date.now());
|
|
}
|
|
}
|
|
await ws.request('BlockPublication', {
|
|
action: 'report', blockchainName, blockNumber: block.blockNumber,
|
|
contentId: block.contentId, coreId: block.coreId,
|
|
contentHash: block.contentHash, coreHash: block.coreHash, error: uploadError,
|
|
});
|
|
if (uploadError) break;
|
|
}
|
|
} catch (error) {
|
|
console.warn('Block publication queue unavailable', error);
|
|
} finally {
|
|
running.delete(key);
|
|
if (!ws.retired && keepPolling && !scheduled.has(key)) {
|
|
const timer = globalThis.setTimeout(() => {
|
|
scheduled.delete(key);
|
|
void publishPendingBlocks({ ws, login, storagePwd, blockchainName });
|
|
}, 30_000);
|
|
scheduled.set(key, timer);
|
|
}
|
|
}
|
|
}
|