lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

commerce_coverage.mjs (4040B)


      1 // Independent portable scope descriptors over original synthetic signed inputs.
      2 // This producer never calls a Radroots coverage or record implementation.
      3 import {createHash} from 'node:crypto';
      4 import {readFileSync,writeFileSync} from 'node:fs';
      5 import assert from 'node:assert/strict';
      6 const root=new URL('../../',import.meta.url),sourcePath='crates/storage_sqlite/tests/fixtures/stock_history.v1.json';
      7 const sourceBytes=readFileSync(new URL(sourcePath,root));const source=JSON.parse(sourceBytes);
      8 const sha=value=>createHash('sha256').update(value).digest('hex');
      9 const records=Object.fromEntries(source.sources.filter(row=>row.record_id!==null).map(row=>[row.name,{id:row.record_id,wire:JSON.parse(row.raw_rumor).content}]));
     10 const chat=source.sources.find(row=>row.name==='chat').rumor_id;
     11 const future=source.sources.find(row=>row.name==='future_commerce').rumor_id;
     12 const unique=values=>[...new Set(values)].sort();
     13 function checkpoint(names,external) {
     14   const known=unique(names.map(name=>records[name].id));
     15   const required=unique(names.flatMap(name=>JSON.parse(records[name].wire).parents));
     16   const used=unique(external.filter(id=>required.includes(id)&&!known.includes(id)));
     17   const hash=createHash('sha256').update(Buffer.from('radroots.commerce.coverage.v1\0'));
     18   for(const key of ['thread','order','buyer','seller'])hash.update(Buffer.from(source.scope[key],'hex'));
     19   for(const identities of [known,used,[]]) {
     20     const count=Buffer.alloc(8);count.writeBigUInt64BE(BigInt(identities.length));hash.update(count);
     21     for(const id of identities) {assert.match(id,/^[0-9a-f]{64}$/);hash.update(Buffer.from(id,'hex'));}
     22   }
     23   return {digest:hash.digest('hex'),sequence:known.length+used.length};
     24 }
     25 const external=[source.listing_id,chat];
     26 const base=checkpoint(['quote','request'],external);
     27 const vectors=[
     28   {name:'complete',records:['quote','request'],external,uninterpreted:[],readiness:'complete',checkpoint:base},
     29   {name:'duplicates_and_unrelated_external',records:['request','quote','request'],external:[...external,...external,'cd'.repeat(32)],uninterpreted:[],readiness:'complete',checkpoint:base},
     30   {name:'withdrawal_is_relevant_evidence',records:['quote','request','withdrawal'],external,uninterpreted:[],readiness:'complete',checkpoint:checkpoint(['quote','request','withdrawal'],external)},
     31   {name:'seller_order_head_is_relevant',records:['quote','request','decline'],external,uninterpreted:[],readiness:'complete',checkpoint:checkpoint(['quote','request','decline'],external)},
     32   {name:'missing_chat',records:['quote','request'],external:[source.listing_id],uninterpreted:[],readiness:'missing_history',checkpoint:null},
     33   {name:'missing_quote',records:['request'],external,uninterpreted:[],readiness:'missing_history',checkpoint:null},
     34   {name:'author_fork',records:['quote','request','withdrawal','buyer_fork'],external,uninterpreted:[],readiness:'conflicted',checkpoint:null},
     35   {name:'uninterpreted',records:['quote','request'],external,uninterpreted:[future],readiness:'uninterpreted',checkpoint:null},
     36   {name:'empty_is_data_only',records:[],external:[],uninterpreted:[],readiness:'complete',checkpoint:checkpoint([],[])},
     37 ];
     38 const output={schema_version:1,transcript:'radroots.commerce.coverage.v1: NUL domain separator, scope thread/order/buyer/seller each32bytes, then known/required available external/uninterpreted sets eachu64BE count followed by sorted unique32byte identities',producer_sha256:sha(readFileSync(new URL(import.meta.url))),source_path:sourcePath,source_sha256:sha(sourceBytes),scope:source.scope,records,vectors};
     39 const bytes=JSON.stringify(output,null,2)+'\n',target=new URL('crates/protocol/tests/fixtures/commerce_coverage.v1.json',root);
     40 const args=process.argv.slice(2);assert(args.length===1&&['--write','--check'].includes(args[0]));
     41 if(args[0]==='--write')writeFileSync(target,bytes);else assert.equal(readFileSync(target,'utf8'),bytes);
     42 console.log(JSON.stringify({vectors:vectors.length,sha256:sha(bytes),bytes:Buffer.byteLength(bytes),check:args[0]==='--check'}));