aboutsummaryrefslogtreecommitdiffstats
path: root/Dockerfile.dev
Commit message (Collapse)AuthorAgeFilesLines
* use docker build argsAdam Soltys2022-11-281-1/+8
|
* logAdam Soltys2022-11-111-0/+1
|
* style tweaksAdam Soltys2022-11-111-0/+15
ed */ .highlight .kt { color: #888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #00D; font-weight: bold } /* Literal.Number */ .highlight .s { color: #D20; background-color: #FFF0F0 } /* Literal.String */ .highlight .na { color: #369 } /* Name.Attribute */ .highlight .nb { color: #038 } /* Name.Builtin */ .highlight .nc { color: #B06; font-weight: bold } /* Name.Class */ .highlight .no { color: #036; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555 } /* Name.Decorator */ .highlight .ne { color: #B06; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #06B; font-weight: bold } /* Name.Function */ .highlight .nl { color: #369; font-style: italic } /* Name.Label */ .highlight .nn { color: #B06; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #369; font-weight: bold } /* Name.Property */ .highlight .nt { color: #B06; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #369 } /* Name.Variable */ .highlight .ow { color: #080 } /* Operator.Word */ .highlight .w { color: #BBB } /* Text.Whitespace */ .highlight .mb { color: #00D; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #00D; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #00D; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #00D; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #00D; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #D20; background-color: #FFF0F0 } /* Literal.String.Affix */ .highlight .sb { color: #D20; background-color: #FFF0F0 } /* Literal.String.Backtick */ .highlight .sc { color: #D20; background-color: #FFF0F0 } /* Literal.String.Char */ .highlight .dl { color: #D20; background-color: #FFF0F0 } /* Literal.String.Delimiter */ .highlight .sd { color: #D20; background-color: #FFF0F0 } /* Literal.String.Doc */ .highlight .s2 { color: #D20; background-color: #FFF0F0 } /* Literal.String.Double */ .highlight .se { color: #04D; background-color: #FFF0F0 } /* Literal.String.Escape */ .highlight .sh { color: #D20; background-color: #FFF0F0 } /* Literal.String.Heredoc */ .highlight .si { color: #33B; background-color: #FFF0F0 } /* Literal.String.Interpol */ .highlight .sx { color: #2B2; background-color: #F0FFF0 } /* Literal.String.Other */ .highlight .sr { color: #080; background-color: #FFF0FF } /* Literal.String.Regex */ .highlight .s1 { color: #D20; background-color: #FFF0F0 } /* Literal.String.Single */ .highlight .ss { color: #A60; background-color: #FFF0F0 } /* Literal.String.Symbol */ .highlight .bp { color: #038 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #06B; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #369 } /* Name.Variable.Class */ .highlight .vg { color: #D70 } /* Name.Variable.Global */ .highlight .vi { color: #33B } /* Name.Variable.Instance */ .highlight .vm { color: #369 } /* Name.Variable.Magic */ .highlight .il { color: #00D; font-weight: bold } /* Literal.Number.Integer.Long */
// The negentropy client (NIP-77, src/negentropy.js) on the wire as strfry
// and the reference implementation speak it: the opening message for a
// fixed set is byte-identical to the reference's (checked against
// hoytech/negentropy's JS on 2026-10-16: seven pairings of ours and theirs
// as client and relay agreed on every have/need), and a reconciliation
// against our own relay side finds exactly what each side lacks — in one
// round for a small difference, in several when the relay's frame is
// small — and ends with nothing to say once the sets match.
// Run: bun tools/negentropy-test.js
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { NegSet, Negentropy } from '../src/negentropy.js';
const id = (n) => createHash('sha256').update('item-' + n).digest('hex');
const sha = (s) => createHash('sha256').update(s).digest('hex');

// the fixed sets the reference was run on
const A = Array.from({ length: 120 }, (_, i) => [1000 + Math.floor(i / 3), id(i)]);
const B = [...A.filter((_, i) => i % 17 !== 0), ...Array.from({ length: 9 }, (_, i) => [1000 + i * 4, id(1000 + i)])];
const first = await new Negentropy(new NegSet(A)).initiate();
assert.equal(first.length, 634, 'the opening message for the fixed set is 317 bytes');
assert.equal(first.slice(0, 2), '61', 'protocol version 1');
assert.equal(sha(first), 'c926c013652f6e2f727abff15c0838aebd7f613fb4749ee691952dfc8be8ff81', 'the opening message is the reference implementation\'s, byte for byte');

async function talk(client, relay) {
  let msg = await client.initiate(), rounds = 0;
  const have = [], need = [];
  while (msg) {
    rounds++;
    const r = await relay.reconcile(msg);
    assert(r.next, 'the relay side always answers');
    const c = await client.reconcile(r.next);
    have.push(...c.have); need.push(...c.need); msg = c.next;
  }
  return { rounds, have: have.sort(), need: need.sort() };
}
{
  const r = await talk(new Negentropy(new NegSet(A)), new Negentropy(new NegSet(B)));
  assert.equal(r.rounds, 1, 'a small difference reconciles in one round');
  assert.deepEqual(r.have, A.filter((_, i) => i % 17 === 0).map((x) => x[1]).sort(), 'have: the ids the relay lacks');
  assert.deepEqual(r.need, Array.from({ length: 9 }, (_, i) => id(1000 + i)).sort(), 'need: the ids we lack');
}
{
  const r = await talk(new Negentropy(new NegSet(A)), new Negentropy(new NegSet(A)));
  assert.equal(r.rounds, 1); assert.deepEqual(r.have, []); assert.deepEqual(r.need, []);
  const r2 = await talk(new Negentropy(new NegSet([])), new Negentropy(new NegSet(B)));
  assert.equal(r2.need.length, B.length, 'holding nothing, we need everything');
  const r3 = await talk(new Negentropy(new NegSet(B)), new Negentropy(new NegSet([])));
  assert.equal(r3.have.length, B.length, 'the relay holding nothing lacks everything');
}
{
  // a big set and a relay with a small frame: many rounds, the same answer
  const big = Array.from({ length: 3000 }, (_, i) => [1000 + i, id(i)]);
  const theirs = [...big.filter((_, i) => i % 101 !== 0), ...Array.from({ length: 25 }, (_, i) => [1000 + i * 7, id(5000 + i)])];
  const r = await talk(new Negentropy(new NegSet(big)), new Negentropy(new NegSet(theirs), { frameSizeLimit: 4096 }));
  assert(r.rounds > 1, 'a small frame takes several rounds: ' + r.rounds);
  assert.equal(r.have.length, 30); assert.equal(r.need.length, 25);
}
{
  // the set drops duplicates and bad ids, and sorts by time then id
  const s = new NegSet([[5, id(1)], [5, id(1)], [3, 'nope'], [4, id(2)], [5, id(0)]]);
  assert.equal(s.size, 3);
  assert.deepEqual(s.items.map((x) => x.ts), [4, 5, 5]);
}
console.log('✓ negentropy: the reference\'s bytes, one round for a small difference, several for a small frame, nothing to say when equal');