Улучшить Arweave-синхронизацию и ротацию ключей

Улучшено скачивание блоков из Arweave и обработка канонической цепочки.

Ротация ключей переведена на новую схему с пересборкой публичных каналов; личные сообщения пока не меняются.
This commit is contained in:
AidarKC
2026-10-06 16:39:39 +03:00
parent daa221a158
commit 67010a57f2
40 changed files with 2520 additions and 570 deletions
+12 -33
View File
@@ -116,50 +116,29 @@ export class KeyRotationClient {
const cutoff=Number(rotation.forkFromBlock); const source=String(rotation.sourceBlockchainName||'');
if(!source || !Number.isInteger(cutoff) || cutoff<0) throw new Error('Некорректное состояние ротации');
if(String(newBundle?.blockchainPair?.publicKeyB64||'') !== String(rotation.newBlockchainKey||'')) throw new Error('Новый пароль выводит другой blockchain key');
const total=cutoff+2;
if(Number(rotation.progressCurrent)||0 >= total) {
onProgress({ phase:'copy', current:total, total });
return;
}
const total=1;
if((Number(rotation.progressCurrent)||0)>=total){onProgress({phase:'copy',current:total,total});return;}
const owner32=base64ToBytes(newBundle.blockchainPair.publicKeyB64);
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 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 tipParsed=parseAns104DataItem(base64ToBytes(tipSource.blockBytesB64));
const techTail=await this.#resolveTechLineTail(source,cutoff);
const forkBody=makeForkBody({
lineCode:0,
prevLineNumber:techTail.prevLineNumber,
prevLineHash:techTail.prevLineHash,
lineSequence:techTail.lineSequence,
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,
lineCode:0, prevLineNumber:techTail.prevLineNumber, prevLineHash:techTail.prevLineHash, lineSequence:techTail.lineSequence,
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 techBlockNumber=cutoff+1;
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 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) });
onProgress({ phase:'copy', current:total, total });
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 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:techBlockNumber,prevBlockHash:rotation.forkFromHash,blockBytesB64:bytesToBase64(techRaw)});
onProgress({phase:'copy',current:1,total:1});
}
async #sourcePrevHash(source, blockNumber) {