@inversealtruism/cairnx-core 0.2.8 → 0.2.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +48 -0
- package/CONVENTION.md +27 -0
- package/README.md +18 -0
- package/dist/index.cjs +300 -155
- package/dist/index.d.cts +32 -4
- package/dist/index.d.ts +32 -4
- package/dist/index.js +293 -151
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -93,6 +93,7 @@ __export(index_exports, {
|
|
|
93
93
|
REG_FINALIZE_GRACE_BLOCKS: () => REG_FINALIZE_GRACE_BLOCKS,
|
|
94
94
|
RESERVED_NAMES: () => RESERVED_NAMES,
|
|
95
95
|
RecordError: () => RecordError,
|
|
96
|
+
ReplayContextRefusal: () => ReplayContextRefusal,
|
|
96
97
|
SALT_RE: () => SALT_RE,
|
|
97
98
|
SCAN_MARGIN: () => SCAN_MARGIN,
|
|
98
99
|
SCORE_CANCEL: () => SCORE_CANCEL,
|
|
@@ -156,6 +157,7 @@ __export(index_exports, {
|
|
|
156
157
|
fillTargetId: () => fillTargetId,
|
|
157
158
|
finalizeWinnerCheck: () => finalizeWinnerCheck,
|
|
158
159
|
hasLiveClaim: () => hasLiveClaim,
|
|
160
|
+
isChainHeight: () => isChainHeight,
|
|
159
161
|
isName: () => isName,
|
|
160
162
|
isNameGive: () => isNameGive,
|
|
161
163
|
isOpenClaimLane: () => isOpenClaimLane,
|
|
@@ -173,6 +175,7 @@ __export(index_exports, {
|
|
|
173
175
|
nameSet: () => nameSet,
|
|
174
176
|
nameView: () => nameView,
|
|
175
177
|
nameXfer: () => nameXfer,
|
|
178
|
+
newReplayContext: () => newReplayContext,
|
|
176
179
|
offer: () => offer,
|
|
177
180
|
offerCancelAll: () => offerCancelAll,
|
|
178
181
|
offerExpiryHeightOf: () => offerExpiryHeightOf,
|
|
@@ -206,6 +209,7 @@ var import_csd_codec = require("@inversealtruism/csd-codec");
|
|
|
206
209
|
var DOMAIN = "cairnx:v1";
|
|
207
210
|
var ACTIVATION_HEIGHT = 29860;
|
|
208
211
|
var beforeActivation = (height) => height < ACTIVATION_HEIGHT;
|
|
212
|
+
var isChainHeight = (height) => Number.isSafeInteger(height);
|
|
209
213
|
var V11_HEIGHT = 29960;
|
|
210
214
|
var V12_HEIGHT = 30300;
|
|
211
215
|
var V13_HEIGHT = 31100;
|
|
@@ -977,6 +981,10 @@ var MinQueue = class {
|
|
|
977
981
|
get size() {
|
|
978
982
|
return this.ids.length;
|
|
979
983
|
}
|
|
984
|
+
/** The smallest stored key, or undefined when the queue is empty. */
|
|
985
|
+
peekKey() {
|
|
986
|
+
return this.keys[0];
|
|
987
|
+
}
|
|
980
988
|
/** Adds an entry. A NaN key is not stored: no comparison with NaN is true, so such an item is never due. */
|
|
981
989
|
push(key, id) {
|
|
982
990
|
if (key !== key) return;
|
|
@@ -1062,6 +1070,7 @@ function createStore(opts = {}) {
|
|
|
1062
1070
|
s.add(v);
|
|
1063
1071
|
};
|
|
1064
1072
|
const regimeHeight = () => maxSweep >= V21_HEIGHT ? V21_HEIGHT : V21_HEIGHT - 1;
|
|
1073
|
+
let splitSweep = true;
|
|
1065
1074
|
const addOffer = (o) => {
|
|
1066
1075
|
if (!offers.has(o.id)) offerAt.set(o.id, ++stamp);
|
|
1067
1076
|
offers.set(o.id, o);
|
|
@@ -1202,11 +1211,27 @@ function createStore(opts = {}) {
|
|
|
1202
1211
|
stats.visits += popped.length;
|
|
1203
1212
|
return [...new Set(popped)].filter(isDue).sort((a, b) => at.get(a) - at.get(b));
|
|
1204
1213
|
};
|
|
1214
|
+
const nextDue = () => {
|
|
1215
|
+
let d = maxSweep >= V21_HEIGHT ? Infinity : V21_HEIGHT;
|
|
1216
|
+
const ko = offerQ.peekKey(), kb = bidQ.peekKey(), kn = nameQ.peekKey(), kr = recaptureQ.peekKey();
|
|
1217
|
+
if (ko !== void 0) d = Math.min(d, (ko + 1) * import_csd_codec.EPOCH_LEN);
|
|
1218
|
+
if (kb !== void 0) d = Math.min(d, (kb + 1) * import_csd_codec.EPOCH_LEN);
|
|
1219
|
+
if (kn !== void 0) d = Math.min(d, kn + 1);
|
|
1220
|
+
if (kr !== void 0) d = Math.min(d, kr + 1);
|
|
1221
|
+
return d;
|
|
1222
|
+
};
|
|
1205
1223
|
const sweep = (height, expireOffer) => {
|
|
1206
1224
|
if (!(height >= maxSweep)) {
|
|
1207
1225
|
walkSweep(height, expireOffer);
|
|
1208
1226
|
return;
|
|
1209
1227
|
}
|
|
1228
|
+
if (splitSweep) for (let d = nextDue(); d < height; d = nextDue()) {
|
|
1229
|
+
if (!(d > maxSweep)) break;
|
|
1230
|
+
sweepAt(d, expireOffer);
|
|
1231
|
+
}
|
|
1232
|
+
sweepAt(height, expireOffer);
|
|
1233
|
+
};
|
|
1234
|
+
const sweepAt = (height, expireOffer) => {
|
|
1210
1235
|
if (!(maxSweep >= V21_HEIGHT) && height >= V21_HEIGHT) {
|
|
1211
1236
|
stats.visits += openOffers.size + openBids.size;
|
|
1212
1237
|
for (const id of openOffers) {
|
|
@@ -1233,6 +1258,13 @@ function createStore(opts = {}) {
|
|
|
1233
1258
|
same(`expired bids at ${height}`, dueBids, want.bids);
|
|
1234
1259
|
same(`expired reservations at ${height}`, dueNames, want.names);
|
|
1235
1260
|
same(`expired recaptures at ${height}`, dueRecaptures, want.recaptures);
|
|
1261
|
+
const early = !splitSweep ? [] : [
|
|
1262
|
+
...dueOffers.filter((id) => offerDue(offers.get(id), height - 1)),
|
|
1263
|
+
...dueBids.filter((id) => bidDue(bids.get(id), height - 1)),
|
|
1264
|
+
...dueNames.filter((nm) => nameDue(names.get(nm), height - 1)),
|
|
1265
|
+
...dueRecaptures.filter((nm) => height - 1 > recaptures.get(nm).finalizeBy)
|
|
1266
|
+
];
|
|
1267
|
+
if (early.length) throw new Error(`replay index check: expired at ${height}, due before it: ${early.join(", ")}`);
|
|
1236
1268
|
}
|
|
1237
1269
|
for (const id of dueOffers) {
|
|
1238
1270
|
const o = offers.get(id);
|
|
@@ -1382,6 +1414,9 @@ function createStore(opts = {}) {
|
|
|
1382
1414
|
putRecapture,
|
|
1383
1415
|
deleteRecapture,
|
|
1384
1416
|
sweep,
|
|
1417
|
+
endSplit: () => {
|
|
1418
|
+
splitSweep = false;
|
|
1419
|
+
},
|
|
1385
1420
|
pendingCount,
|
|
1386
1421
|
recaptureCount,
|
|
1387
1422
|
liveHoldCount,
|
|
@@ -1993,27 +2028,31 @@ function resolve(events, tipHeight) {
|
|
|
1993
2028
|
return replay(events, tipHeight);
|
|
1994
2029
|
}
|
|
1995
2030
|
function replay(events, tipHeight, opts = {}) {
|
|
1996
|
-
const
|
|
2031
|
+
const fold = createFold(opts);
|
|
2032
|
+
for (const e of [...events].sort(applyOrder)) fold.apply(e);
|
|
2033
|
+
return fold.finish(tipHeight);
|
|
2034
|
+
}
|
|
2035
|
+
function createFold(opts = {}) {
|
|
1997
2036
|
const seenIds = /* @__PURE__ */ new Set();
|
|
1998
|
-
const applied = [];
|
|
1999
|
-
for (const e of ordered) {
|
|
2000
|
-
if (e.height >= V29_HEIGHT) {
|
|
2001
|
-
const k = eventId(e);
|
|
2002
|
-
if (seenIds.has(k)) continue;
|
|
2003
|
-
seenIds.add(k);
|
|
2004
|
-
}
|
|
2005
|
-
applied.push(e);
|
|
2006
|
-
}
|
|
2007
2037
|
const store = createStore(opts);
|
|
2008
2038
|
const c = createContext(store);
|
|
2009
|
-
const {
|
|
2039
|
+
const { refused, sweepExpired } = c;
|
|
2010
2040
|
let pendingBlock = -1;
|
|
2011
2041
|
const applyPendingCancels = () => {
|
|
2012
2042
|
for (const f of c.pendingCancels) f();
|
|
2013
2043
|
c.pendingCancels = [];
|
|
2014
2044
|
};
|
|
2015
|
-
|
|
2016
|
-
if (
|
|
2045
|
+
const inDomain = (height) => {
|
|
2046
|
+
if (!isChainHeight(height)) store.endSplit();
|
|
2047
|
+
};
|
|
2048
|
+
const apply = (ev) => {
|
|
2049
|
+
inDomain(ev.height);
|
|
2050
|
+
if (ev.height >= V29_HEIGHT) {
|
|
2051
|
+
const k = eventId(ev);
|
|
2052
|
+
if (seenIds.has(k)) return;
|
|
2053
|
+
seenIds.add(k);
|
|
2054
|
+
}
|
|
2055
|
+
if (beforeActivation(ev.height)) return;
|
|
2017
2056
|
if (ev.height !== pendingBlock) {
|
|
2018
2057
|
applyPendingCancels();
|
|
2019
2058
|
pendingBlock = ev.height;
|
|
@@ -2025,7 +2064,7 @@ function replay(events, tipHeight, opts = {}) {
|
|
|
2025
2064
|
const parsed = explainRecord(ev.uri, ev.payloadHash);
|
|
2026
2065
|
if (!parsed.ok) {
|
|
2027
2066
|
refused(ev, ev.id, "invalid", parsed.code, "unparseable/non-canonical record");
|
|
2028
|
-
|
|
2067
|
+
return;
|
|
2029
2068
|
}
|
|
2030
2069
|
const rec = parsed.record;
|
|
2031
2070
|
const who = ev.proposer.toLowerCase();
|
|
@@ -2079,10 +2118,18 @@ function replay(events, tipHeight, opts = {}) {
|
|
|
2079
2118
|
} else {
|
|
2080
2119
|
applyAttest(c, ev);
|
|
2081
2120
|
}
|
|
2082
|
-
}
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2121
|
+
};
|
|
2122
|
+
const finish = (tipHeight) => {
|
|
2123
|
+
applyPendingCancels();
|
|
2124
|
+
inDomain(tipHeight);
|
|
2125
|
+
sweepExpired(tipHeight + 1);
|
|
2126
|
+
store.verify(tipHeight + 1, true);
|
|
2127
|
+
return materialize(c, tipHeight);
|
|
2128
|
+
};
|
|
2129
|
+
return { apply, finish };
|
|
2130
|
+
}
|
|
2131
|
+
function materialize(c, tipHeight) {
|
|
2132
|
+
const { tokens, balances, names, recaptures, offers, bids, fclaims, log } = c;
|
|
2086
2133
|
const tokensOut = {};
|
|
2087
2134
|
for (const [t, tok] of [...tokens.entries()].sort(([a], [b]) => ord(a, b))) tokensOut[t] = { ...tok.meta, minted: tok.minted.toString() };
|
|
2088
2135
|
const balancesOut = {};
|
|
@@ -2142,15 +2189,242 @@ function canonicalState(s) {
|
|
|
2142
2189
|
return JSON.stringify(sortKeys(data));
|
|
2143
2190
|
}
|
|
2144
2191
|
|
|
2145
|
-
// src/
|
|
2192
|
+
// src/events.ts
|
|
2146
2193
|
var import_csd_codec3 = require("@inversealtruism/csd-codec");
|
|
2194
|
+
|
|
2195
|
+
// src/paidto.ts
|
|
2196
|
+
function paidToFromOutputs(outputs) {
|
|
2197
|
+
const m = /* @__PURE__ */ Object.create(null);
|
|
2198
|
+
for (const o of outputs) {
|
|
2199
|
+
if (typeof o.value !== "number" || !Number.isSafeInteger(o.value) || o.value < 0) continue;
|
|
2200
|
+
m[o.addr] = (BigInt(m[o.addr] ?? "0") + BigInt(o.value)).toString();
|
|
2201
|
+
}
|
|
2202
|
+
return m;
|
|
2203
|
+
}
|
|
2204
|
+
function burnedPayment(paidTo, payer, offer2) {
|
|
2205
|
+
const read = (addr) => addr === payer ? 0n : ptAmt(paidTo?.[addr]);
|
|
2206
|
+
let counterparty = 0n;
|
|
2207
|
+
if (offer2) {
|
|
2208
|
+
for (const addr of /* @__PURE__ */ new Set([offer2.seller, offer2.want.payto])) if (addr !== TREASURY_ADDR) counterparty += read(addr);
|
|
2209
|
+
}
|
|
2210
|
+
return { treasury: read(TREASURY_ADDR), counterparty };
|
|
2211
|
+
}
|
|
2212
|
+
|
|
2213
|
+
// src/events.ts
|
|
2214
|
+
var CAIRNX_EVENT_RULE = Object.freeze({ domain: DOMAIN, activationHeight: ACTIVATION_HEIGHT });
|
|
2215
|
+
var fail = (what) => {
|
|
2216
|
+
throw new Error(`events rule: ${what}`);
|
|
2217
|
+
};
|
|
2218
|
+
var isIndex = (v) => typeof v === "number" && Number.isSafeInteger(v) && v >= 0;
|
|
2219
|
+
function hexOf(v, bytes, what) {
|
|
2220
|
+
const h = typeof v === "string" && v.startsWith("0x") ? v.slice(2) : v;
|
|
2221
|
+
if (typeof h !== "string" || h.length !== bytes * 2 || !/^[0-9a-fA-F]*$/.test(h)) return fail(`${what} is not ${bytes}-byte hex`);
|
|
2222
|
+
return "0x" + h.toLowerCase();
|
|
2223
|
+
}
|
|
2224
|
+
function u64Of(v, what) {
|
|
2225
|
+
if (typeof v === "bigint" && v >= 0n && v < 1n << 64n) return v;
|
|
2226
|
+
if (typeof v === "number" && Number.isInteger(v) && v >= 0 && v <= 2 ** 53) return v;
|
|
2227
|
+
if (typeof v === "number" && Number.isInteger(v) && v > 2 ** 53) return fail(`${what} above 2^53 must be passed as a bigint`);
|
|
2228
|
+
return fail(`${what} is not a u64`);
|
|
2229
|
+
}
|
|
2230
|
+
function u32Of(v, what) {
|
|
2231
|
+
return typeof v === "number" && Number.isInteger(v) && v >= 0 && v <= 4294967295 ? v : fail(`${what} is not a u32`);
|
|
2232
|
+
}
|
|
2233
|
+
function checkRule(rule) {
|
|
2234
|
+
const d = rule?.domain;
|
|
2235
|
+
if (typeof d !== "string" || d === "" || !(0, import_csd_codec3.isWellFormedUtf16)(d) || d.includes("\0") || d.includes(""))
|
|
2236
|
+
fail("the rule's domain must be non-empty, well-formed text without U+0000 or U+0001");
|
|
2237
|
+
if (!isIndex(rule.activationHeight)) fail("the rule's activation height must be a safe integer >= 0");
|
|
2238
|
+
}
|
|
2239
|
+
var kindOf = (app) => {
|
|
2240
|
+
const t = app?.type;
|
|
2241
|
+
return t === "None" ? "none" : t === "Propose" ? "propose" : t === "Attest" ? "attest" : fail(`unknown app type ${String(t)}`);
|
|
2242
|
+
};
|
|
2243
|
+
var domainOf = (app) => typeof app.domain === "string" ? app.domain : fail("a Propose domain is not text");
|
|
2244
|
+
function paidToOf(outputs) {
|
|
2245
|
+
if (!Array.isArray(outputs)) fail("outputs is not a list");
|
|
2246
|
+
const counted = [];
|
|
2247
|
+
for (const o of outputs) {
|
|
2248
|
+
const addr = hexOf(o?.scriptPubkey, 20, "script_pubkey"), value = u64Of(o?.value, "output value");
|
|
2249
|
+
if (value <= Number.MAX_SAFE_INTEGER) counted.push({ addr, value: Number(value) });
|
|
2250
|
+
}
|
|
2251
|
+
return paidToFromOutputs(counted);
|
|
2252
|
+
}
|
|
2253
|
+
function eventOfTx(t, height, author, rule = CAIRNX_EVENT_RULE) {
|
|
2254
|
+
checkRule(rule);
|
|
2255
|
+
if (!isIndex(height)) fail("height is not a safe integer >= 0");
|
|
2256
|
+
if (!isIndex(t?.pos)) fail("pos is not a safe integer >= 0");
|
|
2257
|
+
const txid = hexOf(t.txid, 32, "txid");
|
|
2258
|
+
const app = t.tx?.app;
|
|
2259
|
+
const kind = kindOf(app);
|
|
2260
|
+
if (kind === "none" || height < rule.activationHeight) return null;
|
|
2261
|
+
if (t.pos === 0) fail("position 0 is the coinbase, which carries no app payload");
|
|
2262
|
+
if (kind === "propose" && domainOf(app) !== rule.domain) return null;
|
|
2263
|
+
const paidTo = paidToOf(t.tx.outputs), who = hexOf(author, 20, "author");
|
|
2264
|
+
if (kind === "propose") {
|
|
2265
|
+
const p = app;
|
|
2266
|
+
const uri = p.uri;
|
|
2267
|
+
if (typeof uri !== "string" || !(0, import_csd_codec3.isWellFormedUtf16)(uri)) return fail("uri is not well-formed text");
|
|
2268
|
+
const e = u64Of(p.expiresEpoch, "expiresEpoch");
|
|
2269
|
+
return {
|
|
2270
|
+
kind: "propose",
|
|
2271
|
+
id: txid,
|
|
2272
|
+
proposer: who,
|
|
2273
|
+
uri: uri.replaceAll("\0", ""),
|
|
2274
|
+
payloadHash: hexOf(p.payloadHash, 32, "payloadHash"),
|
|
2275
|
+
expiresEpoch: e <= Number.MAX_SAFE_INTEGER ? Number(e) : 2 ** 53,
|
|
2276
|
+
height,
|
|
2277
|
+
pos: t.pos,
|
|
2278
|
+
paidTo
|
|
2279
|
+
};
|
|
2280
|
+
}
|
|
2281
|
+
const a = app;
|
|
2282
|
+
return {
|
|
2283
|
+
kind: "attest",
|
|
2284
|
+
txid,
|
|
2285
|
+
proposalId: hexOf(a.proposalId, 32, "proposalId"),
|
|
2286
|
+
attester: who,
|
|
2287
|
+
score: u32Of(a.score, "score"),
|
|
2288
|
+
confidence: u32Of(a.confidence, "confidence"),
|
|
2289
|
+
height,
|
|
2290
|
+
pos: t.pos,
|
|
2291
|
+
paidTo
|
|
2292
|
+
};
|
|
2293
|
+
}
|
|
2294
|
+
function blockMembers(height, txs, sofar, rule = CAIRNX_EVENT_RULE) {
|
|
2295
|
+
checkRule(rule);
|
|
2296
|
+
if (!isIndex(height)) fail("height is not a safe integer >= 0");
|
|
2297
|
+
if (height < rule.activationHeight) return [];
|
|
2298
|
+
if (!Array.isArray(txs)) fail("txs is not a list");
|
|
2299
|
+
const ids = /* @__PURE__ */ new Set(), proposals = /* @__PURE__ */ new Set();
|
|
2300
|
+
const read = [];
|
|
2301
|
+
let last = -1;
|
|
2302
|
+
for (const item of txs) {
|
|
2303
|
+
if (!isIndex(item?.pos) || item.pos <= last) fail("positions are not safe integers >= 0 in strictly increasing order");
|
|
2304
|
+
last = item.pos;
|
|
2305
|
+
const id = hexOf(item.txid, 32, "txid");
|
|
2306
|
+
if (ids.has(id)) fail(`txid ${id} is repeated in the block`);
|
|
2307
|
+
ids.add(id);
|
|
2308
|
+
const kind = kindOf(item.tx?.app);
|
|
2309
|
+
if (kind === "none") continue;
|
|
2310
|
+
if (item.pos === 0) fail("position 0 is the coinbase, which carries no app payload");
|
|
2311
|
+
if (kind === "propose") {
|
|
2312
|
+
if (domainOf(item.tx.app) !== rule.domain || sofar.eventIds.has(id)) continue;
|
|
2313
|
+
proposals.add(id);
|
|
2314
|
+
read.push({ item, kind, id });
|
|
2315
|
+
} else read.push({ item, kind, id, proposal: hexOf(item.tx.app.proposalId, 32, "proposalId") });
|
|
2316
|
+
}
|
|
2317
|
+
return read.filter((r) => r.kind === "propose" || !sofar.eventIds.has(r.id) && (sofar.proposeIds.has(r.proposal) || proposals.has(r.proposal))).map((r) => ({ kind: r.kind, item: r.item }));
|
|
2318
|
+
}
|
|
2319
|
+
function blockEvents(height, txs, sofar, authors, rule = CAIRNX_EVENT_RULE) {
|
|
2320
|
+
const members = blockMembers(height, txs, sofar, rule);
|
|
2321
|
+
if (!Array.isArray(authors) || authors.length !== members.length) fail(`the block has ${members.length} members, not ${authors?.length} authors`);
|
|
2322
|
+
const events = members.map((m, i) => eventOfTx(m.item, height, authors[i], rule) ?? fail("a member mapped to no event"));
|
|
2323
|
+
return [...events.filter((e) => e.kind === "propose"), ...events.filter((e) => e.kind === "attest")];
|
|
2324
|
+
}
|
|
2325
|
+
|
|
2326
|
+
// src/replay-context.ts
|
|
2327
|
+
var ReplayContextRefusal = class extends Error {
|
|
2328
|
+
code;
|
|
2329
|
+
constructor(code, message) {
|
|
2330
|
+
super(message);
|
|
2331
|
+
this.name = "ReplayContextRefusal";
|
|
2332
|
+
this.code = code;
|
|
2333
|
+
}
|
|
2334
|
+
};
|
|
2335
|
+
var strictCode = /* @__PURE__ */ (function() {
|
|
2336
|
+
return this === void 0;
|
|
2337
|
+
})();
|
|
2338
|
+
function newReplayContext() {
|
|
2339
|
+
if (!strictCode) throw new ReplayContextRefusal("sloppy", "this build runs cairnx-core in sloppy mode, where a write to a frozen object is ignored");
|
|
2340
|
+
const fold = createFold();
|
|
2341
|
+
let height = -1;
|
|
2342
|
+
let broken = false;
|
|
2343
|
+
const live = () => {
|
|
2344
|
+
if (broken) throw new ReplayContextRefusal("broken", "the replay threw inside advance or stateAt; replay with resolve()");
|
|
2345
|
+
};
|
|
2346
|
+
return {
|
|
2347
|
+
get height() {
|
|
2348
|
+
return height;
|
|
2349
|
+
},
|
|
2350
|
+
advance(toHeight, events) {
|
|
2351
|
+
live();
|
|
2352
|
+
if (!isChainHeight(toHeight)) throw new ReplayContextRefusal("domain", `target height ${String(toHeight)} is not a safe integer`);
|
|
2353
|
+
const batch = [...events].map((e) => ({ ...e }));
|
|
2354
|
+
for (const e of batch) {
|
|
2355
|
+
if (!isChainHeight(e.height)) throw new ReplayContextRefusal("domain", `event height ${String(e.height)} is not a safe integer`);
|
|
2356
|
+
if (!isIndex(e.pos)) throw new ReplayContextRefusal("domain", `event position ${String(e.pos)} is not a safe integer >= 0`);
|
|
2357
|
+
if (typeof eventId(e) !== "string") throw new ReplayContextRefusal("domain", `an event id of type ${typeof eventId(e)}`);
|
|
2358
|
+
}
|
|
2359
|
+
if (toHeight < height) throw new ReplayContextRefusal("order", `target ${toHeight} is below the context's height ${height}`);
|
|
2360
|
+
for (const e of batch) {
|
|
2361
|
+
if (e.height <= height || e.height > toHeight) throw new ReplayContextRefusal("order", `event height ${e.height} is outside (${height}, ${toHeight}]`);
|
|
2362
|
+
}
|
|
2363
|
+
batch.sort(applyOrder);
|
|
2364
|
+
try {
|
|
2365
|
+
for (const e of batch) fold.apply(e);
|
|
2366
|
+
} catch (err) {
|
|
2367
|
+
broken = true;
|
|
2368
|
+
throw err;
|
|
2369
|
+
}
|
|
2370
|
+
height = toHeight;
|
|
2371
|
+
},
|
|
2372
|
+
stateAt(tipHeight) {
|
|
2373
|
+
live();
|
|
2374
|
+
if (tipHeight !== height) throw new ReplayContextRefusal("tip", `an answer at ${String(tipHeight)} from a context at ${height}`);
|
|
2375
|
+
try {
|
|
2376
|
+
return detach(fold.finish(tipHeight));
|
|
2377
|
+
} catch (err) {
|
|
2378
|
+
broken = true;
|
|
2379
|
+
throw err;
|
|
2380
|
+
}
|
|
2381
|
+
}
|
|
2382
|
+
};
|
|
2383
|
+
}
|
|
2384
|
+
var freezeDeep = (v) => {
|
|
2385
|
+
if (v === null || typeof v !== "object" || Object.isFrozen(v)) return;
|
|
2386
|
+
Object.freeze(v);
|
|
2387
|
+
for (const x of Object.values(v)) freezeDeep(x);
|
|
2388
|
+
};
|
|
2389
|
+
var copyRecord = (r) => {
|
|
2390
|
+
const out = {};
|
|
2391
|
+
for (const [k, v] of Object.entries(r)) {
|
|
2392
|
+
if (Array.isArray(v)) {
|
|
2393
|
+
for (const x of v) freezeDeep(x);
|
|
2394
|
+
out[k] = [...v];
|
|
2395
|
+
} else {
|
|
2396
|
+
freezeDeep(v);
|
|
2397
|
+
out[k] = v;
|
|
2398
|
+
}
|
|
2399
|
+
}
|
|
2400
|
+
return out;
|
|
2401
|
+
};
|
|
2402
|
+
function detach(st) {
|
|
2403
|
+
const offers = {};
|
|
2404
|
+
for (const [id, o] of Object.entries(st.offers)) {
|
|
2405
|
+
if (o.status === "open") offers[id] = copyRecord(o);
|
|
2406
|
+
else {
|
|
2407
|
+
freezeDeep(o);
|
|
2408
|
+
offers[id] = o;
|
|
2409
|
+
}
|
|
2410
|
+
}
|
|
2411
|
+
const bids = {};
|
|
2412
|
+
for (const [id, b] of Object.entries(st.bids)) bids[id] = copyRecord(b);
|
|
2413
|
+
for (const n of Object.values(st.names)) freezeDeep(n.profile);
|
|
2414
|
+
const events = st.events.slice();
|
|
2415
|
+
for (const e of events) freezeDeep(e);
|
|
2416
|
+
return { ...st, offers, bids, events };
|
|
2417
|
+
}
|
|
2418
|
+
|
|
2419
|
+
// src/client.ts
|
|
2420
|
+
var import_csd_codec4 = require("@inversealtruism/csd-codec");
|
|
2147
2421
|
var CLAIM_ATTACKER_Q = 0.2;
|
|
2148
2422
|
var CLAIM_MIN_DEPTH = 3;
|
|
2149
2423
|
var COMMIT_REVEAL_MIN_DEPTH = 3;
|
|
2150
2424
|
function requiredClaimDepth(valueSats, height) {
|
|
2151
2425
|
const r = CLAIM_ATTACKER_Q / (1 - CLAIM_ATTACKER_Q);
|
|
2152
2426
|
const V = Number(valueSats);
|
|
2153
|
-
const R = (0,
|
|
2427
|
+
const R = (0, import_csd_codec4.blockReward)(height) || import_csd_codec4.INITIAL_REWARD;
|
|
2154
2428
|
const max = claimWindowAt(height) - 5;
|
|
2155
2429
|
for (let D = CLAIM_MIN_DEPTH; D <= max; D++) {
|
|
2156
2430
|
if (Math.pow(r, D) * V <= D * R) return { depth: D, reversalPct: Math.pow(r, D) * 100, capped: false };
|
|
@@ -2330,7 +2604,7 @@ function nameView(names, name) {
|
|
|
2330
2604
|
}
|
|
2331
2605
|
|
|
2332
2606
|
// src/version.ts
|
|
2333
|
-
var CAIRNX_CORE_VERSION = "0.2.
|
|
2607
|
+
var CAIRNX_CORE_VERSION = "0.2.10";
|
|
2334
2608
|
|
|
2335
2609
|
// src/gates.ts
|
|
2336
2610
|
var GATE_HEIGHTS = Object.freeze({
|
|
@@ -2357,7 +2631,7 @@ var GATE_HEIGHTS = Object.freeze({
|
|
|
2357
2631
|
});
|
|
2358
2632
|
|
|
2359
2633
|
// src/verifyfill.ts
|
|
2360
|
-
var
|
|
2634
|
+
var import_csd_codec5 = require("@inversealtruism/csd-codec");
|
|
2361
2635
|
var FCLAIM_MAX_HOLD_SPAN = import_csd_codec.EPOCH_LEN * (FCLAIM_MAX_EPOCH_AHEAD + 1) - 1;
|
|
2362
2636
|
var GAP_NEEDED = FCLAIM_MAX_HOLD_SPAN + CLAIM_COOLDOWN_BLOCKS;
|
|
2363
2637
|
var SCAN_MARGIN = import_csd_codec.EPOCH_LEN;
|
|
@@ -2365,7 +2639,7 @@ var MAX_SCAN = GAP_NEEDED + SCAN_MARGIN;
|
|
|
2365
2639
|
function bindRecord(ev) {
|
|
2366
2640
|
const rec = parseRecord(ev.uri, ev.payloadHash);
|
|
2367
2641
|
if (!rec) return null;
|
|
2368
|
-
if ((0,
|
|
2642
|
+
if ((0, import_csd_codec5.payloadHash)(rec).toLowerCase() !== String(ev.payloadHash).toLowerCase()) return null;
|
|
2369
2643
|
return rec;
|
|
2370
2644
|
}
|
|
2371
2645
|
function replayLiveHold(proven, offerId, fclaimTxid, me, evalHeight) {
|
|
@@ -2520,138 +2794,6 @@ function bindProvenOffer(offerEv) {
|
|
|
2520
2794
|
return { payto, seller, terms: provenOfferTerms(rec, offerEv.height) };
|
|
2521
2795
|
}
|
|
2522
2796
|
|
|
2523
|
-
// src/paidto.ts
|
|
2524
|
-
function paidToFromOutputs(outputs) {
|
|
2525
|
-
const m = /* @__PURE__ */ Object.create(null);
|
|
2526
|
-
for (const o of outputs) {
|
|
2527
|
-
if (typeof o.value !== "number" || !Number.isSafeInteger(o.value) || o.value < 0) continue;
|
|
2528
|
-
m[o.addr] = (BigInt(m[o.addr] ?? "0") + BigInt(o.value)).toString();
|
|
2529
|
-
}
|
|
2530
|
-
return m;
|
|
2531
|
-
}
|
|
2532
|
-
function burnedPayment(paidTo, payer, offer2) {
|
|
2533
|
-
const read = (addr) => addr === payer ? 0n : ptAmt(paidTo?.[addr]);
|
|
2534
|
-
let counterparty = 0n;
|
|
2535
|
-
if (offer2) {
|
|
2536
|
-
for (const addr of /* @__PURE__ */ new Set([offer2.seller, offer2.want.payto])) if (addr !== TREASURY_ADDR) counterparty += read(addr);
|
|
2537
|
-
}
|
|
2538
|
-
return { treasury: read(TREASURY_ADDR), counterparty };
|
|
2539
|
-
}
|
|
2540
|
-
|
|
2541
|
-
// src/events.ts
|
|
2542
|
-
var import_csd_codec5 = require("@inversealtruism/csd-codec");
|
|
2543
|
-
var CAIRNX_EVENT_RULE = Object.freeze({ domain: DOMAIN, activationHeight: ACTIVATION_HEIGHT });
|
|
2544
|
-
var fail = (what) => {
|
|
2545
|
-
throw new Error(`events rule: ${what}`);
|
|
2546
|
-
};
|
|
2547
|
-
var isIndex = (v) => typeof v === "number" && Number.isSafeInteger(v) && v >= 0;
|
|
2548
|
-
function hexOf(v, bytes, what) {
|
|
2549
|
-
const h = typeof v === "string" && v.startsWith("0x") ? v.slice(2) : v;
|
|
2550
|
-
if (typeof h !== "string" || h.length !== bytes * 2 || !/^[0-9a-fA-F]*$/.test(h)) return fail(`${what} is not ${bytes}-byte hex`);
|
|
2551
|
-
return "0x" + h.toLowerCase();
|
|
2552
|
-
}
|
|
2553
|
-
function u64Of(v, what) {
|
|
2554
|
-
if (typeof v === "bigint" && v >= 0n && v < 1n << 64n) return v;
|
|
2555
|
-
if (typeof v === "number" && Number.isInteger(v) && v >= 0 && v <= 2 ** 53) return v;
|
|
2556
|
-
if (typeof v === "number" && Number.isInteger(v) && v > 2 ** 53) return fail(`${what} above 2^53 must be passed as a bigint`);
|
|
2557
|
-
return fail(`${what} is not a u64`);
|
|
2558
|
-
}
|
|
2559
|
-
function u32Of(v, what) {
|
|
2560
|
-
return typeof v === "number" && Number.isInteger(v) && v >= 0 && v <= 4294967295 ? v : fail(`${what} is not a u32`);
|
|
2561
|
-
}
|
|
2562
|
-
function checkRule(rule) {
|
|
2563
|
-
const d = rule?.domain;
|
|
2564
|
-
if (typeof d !== "string" || d === "" || !(0, import_csd_codec5.isWellFormedUtf16)(d) || d.includes("\0") || d.includes(""))
|
|
2565
|
-
fail("the rule's domain must be non-empty, well-formed text without U+0000 or U+0001");
|
|
2566
|
-
if (!isIndex(rule.activationHeight)) fail("the rule's activation height must be a safe integer >= 0");
|
|
2567
|
-
}
|
|
2568
|
-
var kindOf = (app) => {
|
|
2569
|
-
const t = app?.type;
|
|
2570
|
-
return t === "None" ? "none" : t === "Propose" ? "propose" : t === "Attest" ? "attest" : fail(`unknown app type ${String(t)}`);
|
|
2571
|
-
};
|
|
2572
|
-
var domainOf = (app) => typeof app.domain === "string" ? app.domain : fail("a Propose domain is not text");
|
|
2573
|
-
function paidToOf(outputs) {
|
|
2574
|
-
if (!Array.isArray(outputs)) fail("outputs is not a list");
|
|
2575
|
-
const counted = [];
|
|
2576
|
-
for (const o of outputs) {
|
|
2577
|
-
const addr = hexOf(o?.scriptPubkey, 20, "script_pubkey"), value = u64Of(o?.value, "output value");
|
|
2578
|
-
if (value <= Number.MAX_SAFE_INTEGER) counted.push({ addr, value: Number(value) });
|
|
2579
|
-
}
|
|
2580
|
-
return paidToFromOutputs(counted);
|
|
2581
|
-
}
|
|
2582
|
-
function eventOfTx(t, height, author, rule = CAIRNX_EVENT_RULE) {
|
|
2583
|
-
checkRule(rule);
|
|
2584
|
-
if (!isIndex(height)) fail("height is not a safe integer >= 0");
|
|
2585
|
-
if (!isIndex(t?.pos)) fail("pos is not a safe integer >= 0");
|
|
2586
|
-
const txid = hexOf(t.txid, 32, "txid");
|
|
2587
|
-
const app = t.tx?.app;
|
|
2588
|
-
const kind = kindOf(app);
|
|
2589
|
-
if (kind === "none" || height < rule.activationHeight) return null;
|
|
2590
|
-
if (t.pos === 0) fail("position 0 is the coinbase, which carries no app payload");
|
|
2591
|
-
if (kind === "propose" && domainOf(app) !== rule.domain) return null;
|
|
2592
|
-
const paidTo = paidToOf(t.tx.outputs), who = hexOf(author, 20, "author");
|
|
2593
|
-
if (kind === "propose") {
|
|
2594
|
-
const p = app;
|
|
2595
|
-
const uri = p.uri;
|
|
2596
|
-
if (typeof uri !== "string" || !(0, import_csd_codec5.isWellFormedUtf16)(uri)) return fail("uri is not well-formed text");
|
|
2597
|
-
const e = u64Of(p.expiresEpoch, "expiresEpoch");
|
|
2598
|
-
return {
|
|
2599
|
-
kind: "propose",
|
|
2600
|
-
id: txid,
|
|
2601
|
-
proposer: who,
|
|
2602
|
-
uri: uri.replaceAll("\0", ""),
|
|
2603
|
-
payloadHash: hexOf(p.payloadHash, 32, "payloadHash"),
|
|
2604
|
-
expiresEpoch: e <= Number.MAX_SAFE_INTEGER ? Number(e) : 2 ** 53,
|
|
2605
|
-
height,
|
|
2606
|
-
pos: t.pos,
|
|
2607
|
-
paidTo
|
|
2608
|
-
};
|
|
2609
|
-
}
|
|
2610
|
-
const a = app;
|
|
2611
|
-
return {
|
|
2612
|
-
kind: "attest",
|
|
2613
|
-
txid,
|
|
2614
|
-
proposalId: hexOf(a.proposalId, 32, "proposalId"),
|
|
2615
|
-
attester: who,
|
|
2616
|
-
score: u32Of(a.score, "score"),
|
|
2617
|
-
confidence: u32Of(a.confidence, "confidence"),
|
|
2618
|
-
height,
|
|
2619
|
-
pos: t.pos,
|
|
2620
|
-
paidTo
|
|
2621
|
-
};
|
|
2622
|
-
}
|
|
2623
|
-
function blockMembers(height, txs, sofar, rule = CAIRNX_EVENT_RULE) {
|
|
2624
|
-
checkRule(rule);
|
|
2625
|
-
if (!isIndex(height)) fail("height is not a safe integer >= 0");
|
|
2626
|
-
if (height < rule.activationHeight) return [];
|
|
2627
|
-
if (!Array.isArray(txs)) fail("txs is not a list");
|
|
2628
|
-
const ids = /* @__PURE__ */ new Set(), proposals = /* @__PURE__ */ new Set();
|
|
2629
|
-
const read = [];
|
|
2630
|
-
let last = -1;
|
|
2631
|
-
for (const item of txs) {
|
|
2632
|
-
if (!isIndex(item?.pos) || item.pos <= last) fail("positions are not safe integers >= 0 in strictly increasing order");
|
|
2633
|
-
last = item.pos;
|
|
2634
|
-
const id = hexOf(item.txid, 32, "txid");
|
|
2635
|
-
if (ids.has(id)) fail(`txid ${id} is repeated in the block`);
|
|
2636
|
-
ids.add(id);
|
|
2637
|
-
const kind = kindOf(item.tx?.app);
|
|
2638
|
-
if (kind === "none") continue;
|
|
2639
|
-
if (item.pos === 0) fail("position 0 is the coinbase, which carries no app payload");
|
|
2640
|
-
if (kind === "propose") {
|
|
2641
|
-
if (domainOf(item.tx.app) !== rule.domain || sofar.eventIds.has(id)) continue;
|
|
2642
|
-
proposals.add(id);
|
|
2643
|
-
read.push({ item, kind, id });
|
|
2644
|
-
} else read.push({ item, kind, id, proposal: hexOf(item.tx.app.proposalId, 32, "proposalId") });
|
|
2645
|
-
}
|
|
2646
|
-
return read.filter((r) => r.kind === "propose" || !sofar.eventIds.has(r.id) && (sofar.proposeIds.has(r.proposal) || proposals.has(r.proposal))).map((r) => ({ kind: r.kind, item: r.item }));
|
|
2647
|
-
}
|
|
2648
|
-
function blockEvents(height, txs, sofar, authors, rule = CAIRNX_EVENT_RULE) {
|
|
2649
|
-
const members = blockMembers(height, txs, sofar, rule);
|
|
2650
|
-
if (!Array.isArray(authors) || authors.length !== members.length) fail(`the block has ${members.length} members, not ${authors?.length} authors`);
|
|
2651
|
-
const events = members.map((m, i) => eventOfTx(m.item, height, authors[i], rule) ?? fail("a member mapped to no event"));
|
|
2652
|
-
return [...events.filter((e) => e.kind === "propose"), ...events.filter((e) => e.kind === "attest")];
|
|
2653
|
-
}
|
|
2654
|
-
|
|
2655
2797
|
// src/hashes.ts
|
|
2656
2798
|
var import_csd_codec6 = require("@inversealtruism/csd-codec");
|
|
2657
2799
|
var import_sha256 = require("@noble/hashes/sha256");
|
|
@@ -2971,6 +3113,7 @@ var import_csd_codec7 = require("@inversealtruism/csd-codec");
|
|
|
2971
3113
|
REG_FINALIZE_GRACE_BLOCKS,
|
|
2972
3114
|
RESERVED_NAMES,
|
|
2973
3115
|
RecordError,
|
|
3116
|
+
ReplayContextRefusal,
|
|
2974
3117
|
SALT_RE,
|
|
2975
3118
|
SCAN_MARGIN,
|
|
2976
3119
|
SCORE_CANCEL,
|
|
@@ -3034,6 +3177,7 @@ var import_csd_codec7 = require("@inversealtruism/csd-codec");
|
|
|
3034
3177
|
fillTargetId,
|
|
3035
3178
|
finalizeWinnerCheck,
|
|
3036
3179
|
hasLiveClaim,
|
|
3180
|
+
isChainHeight,
|
|
3037
3181
|
isName,
|
|
3038
3182
|
isNameGive,
|
|
3039
3183
|
isOpenClaimLane,
|
|
@@ -3051,6 +3195,7 @@ var import_csd_codec7 = require("@inversealtruism/csd-codec");
|
|
|
3051
3195
|
nameSet,
|
|
3052
3196
|
nameView,
|
|
3053
3197
|
nameXfer,
|
|
3198
|
+
newReplayContext,
|
|
3054
3199
|
offer,
|
|
3055
3200
|
offerCancelAll,
|
|
3056
3201
|
offerExpiryHeightOf,
|