Новый формат блоков CORE/CONTENT test5595

This commit is contained in:
AidarKC
2026-11-27 10:04:22 +03:00
parent d3a84dd451
commit f9e7c4ef94
180 changed files with 15365 additions and 1301 deletions
+74
View File
@@ -0,0 +1,74 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { signEventContainer, unpackCoreContent, packCoreContent, decodeCore, decodeContent, readStoredLineMeta, submitSignedEvent } from '../js/services/core-content.js';
import { parseAns104DataItem } from '../js/services/ans104-data-item.js';
import { inspectBlockchainDataItem } from '../js/services/blockchain-entry-inspector.js';
import { checkArweavePair, readSignedBlock } from '../js/services/blockchain-proof-service.js';
import { buildBlockchainSnapshotFile } from '../js/services/shine-blockchain-snapshot-service.js';
const rows=fs.readFileSync(new URL('../../docs/Blockchain/vectors/core-content-v1.tsv',import.meta.url),'utf8').trim().split('\n').slice(1).map(r=>r.split('\t'));
const unhex=s=>Buffer.from(s,'hex');
const fixture=r=>packCoreContent(unhex(r[6]),unhex(r[8]));
const base64=b=>Buffer.from(b).toString('base64');
const cursor={ blockchainName:'Alice@shine-001', blockNumber:2, prevBlockHash:rows[1][3] };
const signed={bytes:fixture(rows[2]),hash:rows[2][3]};
function oldFactoryBody(core,content,ordinal=7){
if(!core.line) return content.body;
const fixed=[3,4].includes(content.type), prefix=Buffer.alloc(fixed?40:44), offset=fixed?0:4;
if(!fixed) prefix.writeInt32BE(content.type===0?0:core.line.lineCode,0);
prefix.writeInt32BE(core.line.prevLineBlockNumber,offset);
Buffer.from(core.line.prevLineCoreHash32).copy(prefix,offset+4);
prefix.writeInt32BE(ordinal,offset+36);
return Buffer.concat([prefix,content.body]);
}
test('existing body factories produce exact independent signed pair; ordinal is not signed',async()=>{
const key=await crypto.subtle.importKey('pkcs8',Buffer.concat([unhex('302e020100300506032b657004220420'),Buffer.from(Array.from({length:32},(_,i)=>i))]),{name:'Ed25519'},false,['sign']);
for(const r of rows){
const core=decodeCore(unhex(r[2])),content=decodeContent(unhex(r[4]));
const args={owner32:unhex(r[1]),privateKey:key,blockNumber:core.blockNumber,prevBlockHash:Buffer.from(core.prevCoreHash32).toString('hex'),timestamp:core.timestamp,msgType:content.type,msgSubType:content.subType,msgVersion:content.version};
const result=await signEventContainer({...args,bodyBytes:oldFactoryBody(core,content)});
assert.deepEqual(result.bytes,fixture(r),r[0]);assert.equal(result.hash,r[3]);
if(core.line){
const again=await signEventContainer({...args,bodyBytes:oldFactoryBody(core,content,42)});
assert.deepEqual(again.bytes,result.bytes);
const meta=readStoredLineMeta(result.bytes,42);
assert.equal(meta.lineSequence,42);assert.equal(meta.lineCode,core.line.lineCode>2147482623?0:core.line.lineCode);
assert.throws(()=>readStoredLineMeta(result.bytes,null));assert.throws(()=>readStoredLineMeta(result.bytes,-1));
}
const inspection=await inspectBlockchainDataItem(base64(result.bytes),42);
assert.equal(inspection.frame.blockNumber,core.blockNumber);assert.equal(inspection.frame.type,content.type);
assert.deepEqual(Buffer.from(inspection.frame.bodyBytes),Buffer.from(content.body));
if(core.line) assert.equal(inspection.frame.body.line.lineSequence,42);
if(core.blockNumber===0) assert.equal(inspection.frame.body.license,null);
}
});
test('lost response: retransmit the identical signed container, then confirm canonical bytes',async()=>{
const calls=[];
const ws={request:async(op,payload)=>{calls.push({op,payload});if(calls.length===1)throw Error('timeout');if(op==='AddBlock')return {status:400,payload:{code:'bad_block_number',serverLastGlobalNumber:3}};return {status:200,payload:{blockHash:signed.hash,blockBytesB64:base64(signed.bytes)}};}};
const result=await submitSignedEvent(ws,cursor,signed);
assert.equal(result.status,200);assert.equal(result.payload.serverLastGlobalNumber,2);assert.equal(result.payload.serverLastGlobalHash,signed.hash);
assert.deepEqual(calls[0],calls[1]);assert.equal(calls[2].op,'GetBlockchainBlock');
});
test('ambiguous outcome with mismatched bytes never authorizes fresh signing',async()=>{
let count=0;
const ws={request:async(op)=>{count++;if(op==='AddBlock')throw Error('timeout');return {status:200,payload:{blockHash:signed.hash,blockBytesB64:base64(fixture(rows[3]))}};}};
await assert.rejects(submitSignedEvent(ws,cursor,signed),/outcome unknown/);assert.equal(count,3);
});
test('definite rejection preserves original error for existing line retry',async()=>{
const response={status:400,payload:{code:'bad_prev_line_hash',serverLastGlobalNumber:1}};
assert.equal(await submitSignedEvent({request:async()=>response},cursor,signed),response);
});
test('proof checks both archive payloads, not just CORE',async()=>{
const r=rows[2];const proof=await readSignedBlock({authService:{ws:{request:async()=>({status:200,payload:{blockHash:r[3],blockBytesB64:base64(fixture(r))}})}},blockchainName:cursor.blockchainName,blockNumber:2,blockHash:r[3]});
const original=globalThis.fetch;const calls=[];
try{globalThis.fetch=async url=>{calls.push(url);return new Response(url.endsWith(proof.actualId)?proof.data:proof.contentData);};assert.equal((await checkArweavePair(proof)).status,'valid');assert.equal(calls.length,2);
globalThis.fetch=async url=>new Response(url.endsWith(proof.actualId)?proof.data:new Uint8Array([0]));assert.equal((await checkArweavePair(proof)).status,'invalid');
globalThis.fetch=async url=>url.endsWith(proof.actualId)?new Response(proof.data):new Response(null,{status:404});assert.equal((await checkArweavePair(proof)).status,'unavailable');
}finally{globalThis.fetch=original;}
});
test('snapshot records are independently delimited and retain original bytes',async()=>{
const result=await buildBlockchainSnapshotFile({authService:{ws:{request:async(op,{blockNumber})=>({status:200,payload:{blockBytesB64:base64(fixture(rows[blockNumber]))}})}},login:'Alice@shine',blockchainName:cursor.blockchainName,lastBlockNumber:2});
const bytes=Buffer.from(await result.file.arrayBuffer());assert.equal(bytes.subarray(0,8).toString(),'SHINECC2');let pos=8;
for(const r of rows.slice(0,3)){const n=bytes.readUInt32BE(pos);pos+=4;assert.deepEqual(bytes.subarray(pos,pos+n),Buffer.from(fixture(r)));pos+=n;}
assert.equal(pos,bytes.length);assert.equal(result.totalBytes,pos);
});