@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.js
CHANGED
|
@@ -3,6 +3,7 @@ import { EPOCH_LEN, MAX_URI_BYTES, epochOf } from "@inversealtruism/csd-codec";
|
|
|
3
3
|
var DOMAIN = "cairnx:v1";
|
|
4
4
|
var ACTIVATION_HEIGHT = 29860;
|
|
5
5
|
var beforeActivation = (height) => height < ACTIVATION_HEIGHT;
|
|
6
|
+
var isChainHeight = (height) => Number.isSafeInteger(height);
|
|
6
7
|
var V11_HEIGHT = 29960;
|
|
7
8
|
var V12_HEIGHT = 30300;
|
|
8
9
|
var V13_HEIGHT = 31100;
|
|
@@ -774,6 +775,10 @@ var MinQueue = class {
|
|
|
774
775
|
get size() {
|
|
775
776
|
return this.ids.length;
|
|
776
777
|
}
|
|
778
|
+
/** The smallest stored key, or undefined when the queue is empty. */
|
|
779
|
+
peekKey() {
|
|
780
|
+
return this.keys[0];
|
|
781
|
+
}
|
|
777
782
|
/** Adds an entry. A NaN key is not stored: no comparison with NaN is true, so such an item is never due. */
|
|
778
783
|
push(key, id) {
|
|
779
784
|
if (key !== key) return;
|
|
@@ -859,6 +864,7 @@ function createStore(opts = {}) {
|
|
|
859
864
|
s.add(v);
|
|
860
865
|
};
|
|
861
866
|
const regimeHeight = () => maxSweep >= V21_HEIGHT ? V21_HEIGHT : V21_HEIGHT - 1;
|
|
867
|
+
let splitSweep = true;
|
|
862
868
|
const addOffer = (o) => {
|
|
863
869
|
if (!offers.has(o.id)) offerAt.set(o.id, ++stamp);
|
|
864
870
|
offers.set(o.id, o);
|
|
@@ -999,11 +1005,27 @@ function createStore(opts = {}) {
|
|
|
999
1005
|
stats.visits += popped.length;
|
|
1000
1006
|
return [...new Set(popped)].filter(isDue).sort((a, b) => at.get(a) - at.get(b));
|
|
1001
1007
|
};
|
|
1008
|
+
const nextDue = () => {
|
|
1009
|
+
let d = maxSweep >= V21_HEIGHT ? Infinity : V21_HEIGHT;
|
|
1010
|
+
const ko = offerQ.peekKey(), kb = bidQ.peekKey(), kn = nameQ.peekKey(), kr = recaptureQ.peekKey();
|
|
1011
|
+
if (ko !== void 0) d = Math.min(d, (ko + 1) * EPOCH_LEN);
|
|
1012
|
+
if (kb !== void 0) d = Math.min(d, (kb + 1) * EPOCH_LEN);
|
|
1013
|
+
if (kn !== void 0) d = Math.min(d, kn + 1);
|
|
1014
|
+
if (kr !== void 0) d = Math.min(d, kr + 1);
|
|
1015
|
+
return d;
|
|
1016
|
+
};
|
|
1002
1017
|
const sweep = (height, expireOffer) => {
|
|
1003
1018
|
if (!(height >= maxSweep)) {
|
|
1004
1019
|
walkSweep(height, expireOffer);
|
|
1005
1020
|
return;
|
|
1006
1021
|
}
|
|
1022
|
+
if (splitSweep) for (let d = nextDue(); d < height; d = nextDue()) {
|
|
1023
|
+
if (!(d > maxSweep)) break;
|
|
1024
|
+
sweepAt(d, expireOffer);
|
|
1025
|
+
}
|
|
1026
|
+
sweepAt(height, expireOffer);
|
|
1027
|
+
};
|
|
1028
|
+
const sweepAt = (height, expireOffer) => {
|
|
1007
1029
|
if (!(maxSweep >= V21_HEIGHT) && height >= V21_HEIGHT) {
|
|
1008
1030
|
stats.visits += openOffers.size + openBids.size;
|
|
1009
1031
|
for (const id of openOffers) {
|
|
@@ -1030,6 +1052,13 @@ function createStore(opts = {}) {
|
|
|
1030
1052
|
same(`expired bids at ${height}`, dueBids, want.bids);
|
|
1031
1053
|
same(`expired reservations at ${height}`, dueNames, want.names);
|
|
1032
1054
|
same(`expired recaptures at ${height}`, dueRecaptures, want.recaptures);
|
|
1055
|
+
const early = !splitSweep ? [] : [
|
|
1056
|
+
...dueOffers.filter((id) => offerDue(offers.get(id), height - 1)),
|
|
1057
|
+
...dueBids.filter((id) => bidDue(bids.get(id), height - 1)),
|
|
1058
|
+
...dueNames.filter((nm) => nameDue(names.get(nm), height - 1)),
|
|
1059
|
+
...dueRecaptures.filter((nm) => height - 1 > recaptures.get(nm).finalizeBy)
|
|
1060
|
+
];
|
|
1061
|
+
if (early.length) throw new Error(`replay index check: expired at ${height}, due before it: ${early.join(", ")}`);
|
|
1033
1062
|
}
|
|
1034
1063
|
for (const id of dueOffers) {
|
|
1035
1064
|
const o = offers.get(id);
|
|
@@ -1179,6 +1208,9 @@ function createStore(opts = {}) {
|
|
|
1179
1208
|
putRecapture,
|
|
1180
1209
|
deleteRecapture,
|
|
1181
1210
|
sweep,
|
|
1211
|
+
endSplit: () => {
|
|
1212
|
+
splitSweep = false;
|
|
1213
|
+
},
|
|
1182
1214
|
pendingCount,
|
|
1183
1215
|
recaptureCount,
|
|
1184
1216
|
liveHoldCount,
|
|
@@ -1790,27 +1822,31 @@ function resolve(events, tipHeight) {
|
|
|
1790
1822
|
return replay(events, tipHeight);
|
|
1791
1823
|
}
|
|
1792
1824
|
function replay(events, tipHeight, opts = {}) {
|
|
1793
|
-
const
|
|
1825
|
+
const fold = createFold(opts);
|
|
1826
|
+
for (const e of [...events].sort(applyOrder)) fold.apply(e);
|
|
1827
|
+
return fold.finish(tipHeight);
|
|
1828
|
+
}
|
|
1829
|
+
function createFold(opts = {}) {
|
|
1794
1830
|
const seenIds = /* @__PURE__ */ new Set();
|
|
1795
|
-
const applied = [];
|
|
1796
|
-
for (const e of ordered) {
|
|
1797
|
-
if (e.height >= V29_HEIGHT) {
|
|
1798
|
-
const k = eventId(e);
|
|
1799
|
-
if (seenIds.has(k)) continue;
|
|
1800
|
-
seenIds.add(k);
|
|
1801
|
-
}
|
|
1802
|
-
applied.push(e);
|
|
1803
|
-
}
|
|
1804
1831
|
const store = createStore(opts);
|
|
1805
1832
|
const c = createContext(store);
|
|
1806
|
-
const {
|
|
1833
|
+
const { refused, sweepExpired } = c;
|
|
1807
1834
|
let pendingBlock = -1;
|
|
1808
1835
|
const applyPendingCancels = () => {
|
|
1809
1836
|
for (const f of c.pendingCancels) f();
|
|
1810
1837
|
c.pendingCancels = [];
|
|
1811
1838
|
};
|
|
1812
|
-
|
|
1813
|
-
if (
|
|
1839
|
+
const inDomain = (height) => {
|
|
1840
|
+
if (!isChainHeight(height)) store.endSplit();
|
|
1841
|
+
};
|
|
1842
|
+
const apply = (ev) => {
|
|
1843
|
+
inDomain(ev.height);
|
|
1844
|
+
if (ev.height >= V29_HEIGHT) {
|
|
1845
|
+
const k = eventId(ev);
|
|
1846
|
+
if (seenIds.has(k)) return;
|
|
1847
|
+
seenIds.add(k);
|
|
1848
|
+
}
|
|
1849
|
+
if (beforeActivation(ev.height)) return;
|
|
1814
1850
|
if (ev.height !== pendingBlock) {
|
|
1815
1851
|
applyPendingCancels();
|
|
1816
1852
|
pendingBlock = ev.height;
|
|
@@ -1822,7 +1858,7 @@ function replay(events, tipHeight, opts = {}) {
|
|
|
1822
1858
|
const parsed = explainRecord(ev.uri, ev.payloadHash);
|
|
1823
1859
|
if (!parsed.ok) {
|
|
1824
1860
|
refused(ev, ev.id, "invalid", parsed.code, "unparseable/non-canonical record");
|
|
1825
|
-
|
|
1861
|
+
return;
|
|
1826
1862
|
}
|
|
1827
1863
|
const rec = parsed.record;
|
|
1828
1864
|
const who = ev.proposer.toLowerCase();
|
|
@@ -1876,10 +1912,18 @@ function replay(events, tipHeight, opts = {}) {
|
|
|
1876
1912
|
} else {
|
|
1877
1913
|
applyAttest(c, ev);
|
|
1878
1914
|
}
|
|
1879
|
-
}
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1915
|
+
};
|
|
1916
|
+
const finish = (tipHeight) => {
|
|
1917
|
+
applyPendingCancels();
|
|
1918
|
+
inDomain(tipHeight);
|
|
1919
|
+
sweepExpired(tipHeight + 1);
|
|
1920
|
+
store.verify(tipHeight + 1, true);
|
|
1921
|
+
return materialize(c, tipHeight);
|
|
1922
|
+
};
|
|
1923
|
+
return { apply, finish };
|
|
1924
|
+
}
|
|
1925
|
+
function materialize(c, tipHeight) {
|
|
1926
|
+
const { tokens, balances, names, recaptures, offers, bids, fclaims, log } = c;
|
|
1883
1927
|
const tokensOut = {};
|
|
1884
1928
|
for (const [t, tok] of [...tokens.entries()].sort(([a], [b]) => ord(a, b))) tokensOut[t] = { ...tok.meta, minted: tok.minted.toString() };
|
|
1885
1929
|
const balancesOut = {};
|
|
@@ -1939,6 +1983,233 @@ function canonicalState(s) {
|
|
|
1939
1983
|
return JSON.stringify(sortKeys(data));
|
|
1940
1984
|
}
|
|
1941
1985
|
|
|
1986
|
+
// src/events.ts
|
|
1987
|
+
import { isWellFormedUtf16 as isWellFormedUtf162 } from "@inversealtruism/csd-codec";
|
|
1988
|
+
|
|
1989
|
+
// src/paidto.ts
|
|
1990
|
+
function paidToFromOutputs(outputs) {
|
|
1991
|
+
const m = /* @__PURE__ */ Object.create(null);
|
|
1992
|
+
for (const o of outputs) {
|
|
1993
|
+
if (typeof o.value !== "number" || !Number.isSafeInteger(o.value) || o.value < 0) continue;
|
|
1994
|
+
m[o.addr] = (BigInt(m[o.addr] ?? "0") + BigInt(o.value)).toString();
|
|
1995
|
+
}
|
|
1996
|
+
return m;
|
|
1997
|
+
}
|
|
1998
|
+
function burnedPayment(paidTo, payer, offer2) {
|
|
1999
|
+
const read = (addr) => addr === payer ? 0n : ptAmt(paidTo?.[addr]);
|
|
2000
|
+
let counterparty = 0n;
|
|
2001
|
+
if (offer2) {
|
|
2002
|
+
for (const addr of /* @__PURE__ */ new Set([offer2.seller, offer2.want.payto])) if (addr !== TREASURY_ADDR) counterparty += read(addr);
|
|
2003
|
+
}
|
|
2004
|
+
return { treasury: read(TREASURY_ADDR), counterparty };
|
|
2005
|
+
}
|
|
2006
|
+
|
|
2007
|
+
// src/events.ts
|
|
2008
|
+
var CAIRNX_EVENT_RULE = Object.freeze({ domain: DOMAIN, activationHeight: ACTIVATION_HEIGHT });
|
|
2009
|
+
var fail = (what) => {
|
|
2010
|
+
throw new Error(`events rule: ${what}`);
|
|
2011
|
+
};
|
|
2012
|
+
var isIndex = (v) => typeof v === "number" && Number.isSafeInteger(v) && v >= 0;
|
|
2013
|
+
function hexOf(v, bytes, what) {
|
|
2014
|
+
const h = typeof v === "string" && v.startsWith("0x") ? v.slice(2) : v;
|
|
2015
|
+
if (typeof h !== "string" || h.length !== bytes * 2 || !/^[0-9a-fA-F]*$/.test(h)) return fail(`${what} is not ${bytes}-byte hex`);
|
|
2016
|
+
return "0x" + h.toLowerCase();
|
|
2017
|
+
}
|
|
2018
|
+
function u64Of(v, what) {
|
|
2019
|
+
if (typeof v === "bigint" && v >= 0n && v < 1n << 64n) return v;
|
|
2020
|
+
if (typeof v === "number" && Number.isInteger(v) && v >= 0 && v <= 2 ** 53) return v;
|
|
2021
|
+
if (typeof v === "number" && Number.isInteger(v) && v > 2 ** 53) return fail(`${what} above 2^53 must be passed as a bigint`);
|
|
2022
|
+
return fail(`${what} is not a u64`);
|
|
2023
|
+
}
|
|
2024
|
+
function u32Of(v, what) {
|
|
2025
|
+
return typeof v === "number" && Number.isInteger(v) && v >= 0 && v <= 4294967295 ? v : fail(`${what} is not a u32`);
|
|
2026
|
+
}
|
|
2027
|
+
function checkRule(rule) {
|
|
2028
|
+
const d = rule?.domain;
|
|
2029
|
+
if (typeof d !== "string" || d === "" || !isWellFormedUtf162(d) || d.includes("\0") || d.includes(""))
|
|
2030
|
+
fail("the rule's domain must be non-empty, well-formed text without U+0000 or U+0001");
|
|
2031
|
+
if (!isIndex(rule.activationHeight)) fail("the rule's activation height must be a safe integer >= 0");
|
|
2032
|
+
}
|
|
2033
|
+
var kindOf = (app) => {
|
|
2034
|
+
const t = app?.type;
|
|
2035
|
+
return t === "None" ? "none" : t === "Propose" ? "propose" : t === "Attest" ? "attest" : fail(`unknown app type ${String(t)}`);
|
|
2036
|
+
};
|
|
2037
|
+
var domainOf = (app) => typeof app.domain === "string" ? app.domain : fail("a Propose domain is not text");
|
|
2038
|
+
function paidToOf(outputs) {
|
|
2039
|
+
if (!Array.isArray(outputs)) fail("outputs is not a list");
|
|
2040
|
+
const counted = [];
|
|
2041
|
+
for (const o of outputs) {
|
|
2042
|
+
const addr = hexOf(o?.scriptPubkey, 20, "script_pubkey"), value = u64Of(o?.value, "output value");
|
|
2043
|
+
if (value <= Number.MAX_SAFE_INTEGER) counted.push({ addr, value: Number(value) });
|
|
2044
|
+
}
|
|
2045
|
+
return paidToFromOutputs(counted);
|
|
2046
|
+
}
|
|
2047
|
+
function eventOfTx(t, height, author, rule = CAIRNX_EVENT_RULE) {
|
|
2048
|
+
checkRule(rule);
|
|
2049
|
+
if (!isIndex(height)) fail("height is not a safe integer >= 0");
|
|
2050
|
+
if (!isIndex(t?.pos)) fail("pos is not a safe integer >= 0");
|
|
2051
|
+
const txid = hexOf(t.txid, 32, "txid");
|
|
2052
|
+
const app = t.tx?.app;
|
|
2053
|
+
const kind = kindOf(app);
|
|
2054
|
+
if (kind === "none" || height < rule.activationHeight) return null;
|
|
2055
|
+
if (t.pos === 0) fail("position 0 is the coinbase, which carries no app payload");
|
|
2056
|
+
if (kind === "propose" && domainOf(app) !== rule.domain) return null;
|
|
2057
|
+
const paidTo = paidToOf(t.tx.outputs), who = hexOf(author, 20, "author");
|
|
2058
|
+
if (kind === "propose") {
|
|
2059
|
+
const p = app;
|
|
2060
|
+
const uri = p.uri;
|
|
2061
|
+
if (typeof uri !== "string" || !isWellFormedUtf162(uri)) return fail("uri is not well-formed text");
|
|
2062
|
+
const e = u64Of(p.expiresEpoch, "expiresEpoch");
|
|
2063
|
+
return {
|
|
2064
|
+
kind: "propose",
|
|
2065
|
+
id: txid,
|
|
2066
|
+
proposer: who,
|
|
2067
|
+
uri: uri.replaceAll("\0", ""),
|
|
2068
|
+
payloadHash: hexOf(p.payloadHash, 32, "payloadHash"),
|
|
2069
|
+
expiresEpoch: e <= Number.MAX_SAFE_INTEGER ? Number(e) : 2 ** 53,
|
|
2070
|
+
height,
|
|
2071
|
+
pos: t.pos,
|
|
2072
|
+
paidTo
|
|
2073
|
+
};
|
|
2074
|
+
}
|
|
2075
|
+
const a = app;
|
|
2076
|
+
return {
|
|
2077
|
+
kind: "attest",
|
|
2078
|
+
txid,
|
|
2079
|
+
proposalId: hexOf(a.proposalId, 32, "proposalId"),
|
|
2080
|
+
attester: who,
|
|
2081
|
+
score: u32Of(a.score, "score"),
|
|
2082
|
+
confidence: u32Of(a.confidence, "confidence"),
|
|
2083
|
+
height,
|
|
2084
|
+
pos: t.pos,
|
|
2085
|
+
paidTo
|
|
2086
|
+
};
|
|
2087
|
+
}
|
|
2088
|
+
function blockMembers(height, txs, sofar, rule = CAIRNX_EVENT_RULE) {
|
|
2089
|
+
checkRule(rule);
|
|
2090
|
+
if (!isIndex(height)) fail("height is not a safe integer >= 0");
|
|
2091
|
+
if (height < rule.activationHeight) return [];
|
|
2092
|
+
if (!Array.isArray(txs)) fail("txs is not a list");
|
|
2093
|
+
const ids = /* @__PURE__ */ new Set(), proposals = /* @__PURE__ */ new Set();
|
|
2094
|
+
const read = [];
|
|
2095
|
+
let last = -1;
|
|
2096
|
+
for (const item of txs) {
|
|
2097
|
+
if (!isIndex(item?.pos) || item.pos <= last) fail("positions are not safe integers >= 0 in strictly increasing order");
|
|
2098
|
+
last = item.pos;
|
|
2099
|
+
const id = hexOf(item.txid, 32, "txid");
|
|
2100
|
+
if (ids.has(id)) fail(`txid ${id} is repeated in the block`);
|
|
2101
|
+
ids.add(id);
|
|
2102
|
+
const kind = kindOf(item.tx?.app);
|
|
2103
|
+
if (kind === "none") continue;
|
|
2104
|
+
if (item.pos === 0) fail("position 0 is the coinbase, which carries no app payload");
|
|
2105
|
+
if (kind === "propose") {
|
|
2106
|
+
if (domainOf(item.tx.app) !== rule.domain || sofar.eventIds.has(id)) continue;
|
|
2107
|
+
proposals.add(id);
|
|
2108
|
+
read.push({ item, kind, id });
|
|
2109
|
+
} else read.push({ item, kind, id, proposal: hexOf(item.tx.app.proposalId, 32, "proposalId") });
|
|
2110
|
+
}
|
|
2111
|
+
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 }));
|
|
2112
|
+
}
|
|
2113
|
+
function blockEvents(height, txs, sofar, authors, rule = CAIRNX_EVENT_RULE) {
|
|
2114
|
+
const members = blockMembers(height, txs, sofar, rule);
|
|
2115
|
+
if (!Array.isArray(authors) || authors.length !== members.length) fail(`the block has ${members.length} members, not ${authors?.length} authors`);
|
|
2116
|
+
const events = members.map((m, i) => eventOfTx(m.item, height, authors[i], rule) ?? fail("a member mapped to no event"));
|
|
2117
|
+
return [...events.filter((e) => e.kind === "propose"), ...events.filter((e) => e.kind === "attest")];
|
|
2118
|
+
}
|
|
2119
|
+
|
|
2120
|
+
// src/replay-context.ts
|
|
2121
|
+
var ReplayContextRefusal = class extends Error {
|
|
2122
|
+
code;
|
|
2123
|
+
constructor(code, message) {
|
|
2124
|
+
super(message);
|
|
2125
|
+
this.name = "ReplayContextRefusal";
|
|
2126
|
+
this.code = code;
|
|
2127
|
+
}
|
|
2128
|
+
};
|
|
2129
|
+
var strictCode = /* @__PURE__ */ (function() {
|
|
2130
|
+
return this === void 0;
|
|
2131
|
+
})();
|
|
2132
|
+
function newReplayContext() {
|
|
2133
|
+
if (!strictCode) throw new ReplayContextRefusal("sloppy", "this build runs cairnx-core in sloppy mode, where a write to a frozen object is ignored");
|
|
2134
|
+
const fold = createFold();
|
|
2135
|
+
let height = -1;
|
|
2136
|
+
let broken = false;
|
|
2137
|
+
const live = () => {
|
|
2138
|
+
if (broken) throw new ReplayContextRefusal("broken", "the replay threw inside advance or stateAt; replay with resolve()");
|
|
2139
|
+
};
|
|
2140
|
+
return {
|
|
2141
|
+
get height() {
|
|
2142
|
+
return height;
|
|
2143
|
+
},
|
|
2144
|
+
advance(toHeight, events) {
|
|
2145
|
+
live();
|
|
2146
|
+
if (!isChainHeight(toHeight)) throw new ReplayContextRefusal("domain", `target height ${String(toHeight)} is not a safe integer`);
|
|
2147
|
+
const batch = [...events].map((e) => ({ ...e }));
|
|
2148
|
+
for (const e of batch) {
|
|
2149
|
+
if (!isChainHeight(e.height)) throw new ReplayContextRefusal("domain", `event height ${String(e.height)} is not a safe integer`);
|
|
2150
|
+
if (!isIndex(e.pos)) throw new ReplayContextRefusal("domain", `event position ${String(e.pos)} is not a safe integer >= 0`);
|
|
2151
|
+
if (typeof eventId(e) !== "string") throw new ReplayContextRefusal("domain", `an event id of type ${typeof eventId(e)}`);
|
|
2152
|
+
}
|
|
2153
|
+
if (toHeight < height) throw new ReplayContextRefusal("order", `target ${toHeight} is below the context's height ${height}`);
|
|
2154
|
+
for (const e of batch) {
|
|
2155
|
+
if (e.height <= height || e.height > toHeight) throw new ReplayContextRefusal("order", `event height ${e.height} is outside (${height}, ${toHeight}]`);
|
|
2156
|
+
}
|
|
2157
|
+
batch.sort(applyOrder);
|
|
2158
|
+
try {
|
|
2159
|
+
for (const e of batch) fold.apply(e);
|
|
2160
|
+
} catch (err) {
|
|
2161
|
+
broken = true;
|
|
2162
|
+
throw err;
|
|
2163
|
+
}
|
|
2164
|
+
height = toHeight;
|
|
2165
|
+
},
|
|
2166
|
+
stateAt(tipHeight) {
|
|
2167
|
+
live();
|
|
2168
|
+
if (tipHeight !== height) throw new ReplayContextRefusal("tip", `an answer at ${String(tipHeight)} from a context at ${height}`);
|
|
2169
|
+
try {
|
|
2170
|
+
return detach(fold.finish(tipHeight));
|
|
2171
|
+
} catch (err) {
|
|
2172
|
+
broken = true;
|
|
2173
|
+
throw err;
|
|
2174
|
+
}
|
|
2175
|
+
}
|
|
2176
|
+
};
|
|
2177
|
+
}
|
|
2178
|
+
var freezeDeep = (v) => {
|
|
2179
|
+
if (v === null || typeof v !== "object" || Object.isFrozen(v)) return;
|
|
2180
|
+
Object.freeze(v);
|
|
2181
|
+
for (const x of Object.values(v)) freezeDeep(x);
|
|
2182
|
+
};
|
|
2183
|
+
var copyRecord = (r) => {
|
|
2184
|
+
const out = {};
|
|
2185
|
+
for (const [k, v] of Object.entries(r)) {
|
|
2186
|
+
if (Array.isArray(v)) {
|
|
2187
|
+
for (const x of v) freezeDeep(x);
|
|
2188
|
+
out[k] = [...v];
|
|
2189
|
+
} else {
|
|
2190
|
+
freezeDeep(v);
|
|
2191
|
+
out[k] = v;
|
|
2192
|
+
}
|
|
2193
|
+
}
|
|
2194
|
+
return out;
|
|
2195
|
+
};
|
|
2196
|
+
function detach(st) {
|
|
2197
|
+
const offers = {};
|
|
2198
|
+
for (const [id, o] of Object.entries(st.offers)) {
|
|
2199
|
+
if (o.status === "open") offers[id] = copyRecord(o);
|
|
2200
|
+
else {
|
|
2201
|
+
freezeDeep(o);
|
|
2202
|
+
offers[id] = o;
|
|
2203
|
+
}
|
|
2204
|
+
}
|
|
2205
|
+
const bids = {};
|
|
2206
|
+
for (const [id, b] of Object.entries(st.bids)) bids[id] = copyRecord(b);
|
|
2207
|
+
for (const n of Object.values(st.names)) freezeDeep(n.profile);
|
|
2208
|
+
const events = st.events.slice();
|
|
2209
|
+
for (const e of events) freezeDeep(e);
|
|
2210
|
+
return { ...st, offers, bids, events };
|
|
2211
|
+
}
|
|
2212
|
+
|
|
1942
2213
|
// src/client.ts
|
|
1943
2214
|
import { blockReward, INITIAL_REWARD } from "@inversealtruism/csd-codec";
|
|
1944
2215
|
var CLAIM_ATTACKER_Q = 0.2;
|
|
@@ -2127,7 +2398,7 @@ function nameView(names, name) {
|
|
|
2127
2398
|
}
|
|
2128
2399
|
|
|
2129
2400
|
// src/version.ts
|
|
2130
|
-
var CAIRNX_CORE_VERSION = "0.2.
|
|
2401
|
+
var CAIRNX_CORE_VERSION = "0.2.10";
|
|
2131
2402
|
|
|
2132
2403
|
// src/gates.ts
|
|
2133
2404
|
var GATE_HEIGHTS = Object.freeze({
|
|
@@ -2317,138 +2588,6 @@ function bindProvenOffer(offerEv) {
|
|
|
2317
2588
|
return { payto, seller, terms: provenOfferTerms(rec, offerEv.height) };
|
|
2318
2589
|
}
|
|
2319
2590
|
|
|
2320
|
-
// src/paidto.ts
|
|
2321
|
-
function paidToFromOutputs(outputs) {
|
|
2322
|
-
const m = /* @__PURE__ */ Object.create(null);
|
|
2323
|
-
for (const o of outputs) {
|
|
2324
|
-
if (typeof o.value !== "number" || !Number.isSafeInteger(o.value) || o.value < 0) continue;
|
|
2325
|
-
m[o.addr] = (BigInt(m[o.addr] ?? "0") + BigInt(o.value)).toString();
|
|
2326
|
-
}
|
|
2327
|
-
return m;
|
|
2328
|
-
}
|
|
2329
|
-
function burnedPayment(paidTo, payer, offer2) {
|
|
2330
|
-
const read = (addr) => addr === payer ? 0n : ptAmt(paidTo?.[addr]);
|
|
2331
|
-
let counterparty = 0n;
|
|
2332
|
-
if (offer2) {
|
|
2333
|
-
for (const addr of /* @__PURE__ */ new Set([offer2.seller, offer2.want.payto])) if (addr !== TREASURY_ADDR) counterparty += read(addr);
|
|
2334
|
-
}
|
|
2335
|
-
return { treasury: read(TREASURY_ADDR), counterparty };
|
|
2336
|
-
}
|
|
2337
|
-
|
|
2338
|
-
// src/events.ts
|
|
2339
|
-
import { isWellFormedUtf16 as isWellFormedUtf162 } from "@inversealtruism/csd-codec";
|
|
2340
|
-
var CAIRNX_EVENT_RULE = Object.freeze({ domain: DOMAIN, activationHeight: ACTIVATION_HEIGHT });
|
|
2341
|
-
var fail = (what) => {
|
|
2342
|
-
throw new Error(`events rule: ${what}`);
|
|
2343
|
-
};
|
|
2344
|
-
var isIndex = (v) => typeof v === "number" && Number.isSafeInteger(v) && v >= 0;
|
|
2345
|
-
function hexOf(v, bytes, what) {
|
|
2346
|
-
const h = typeof v === "string" && v.startsWith("0x") ? v.slice(2) : v;
|
|
2347
|
-
if (typeof h !== "string" || h.length !== bytes * 2 || !/^[0-9a-fA-F]*$/.test(h)) return fail(`${what} is not ${bytes}-byte hex`);
|
|
2348
|
-
return "0x" + h.toLowerCase();
|
|
2349
|
-
}
|
|
2350
|
-
function u64Of(v, what) {
|
|
2351
|
-
if (typeof v === "bigint" && v >= 0n && v < 1n << 64n) return v;
|
|
2352
|
-
if (typeof v === "number" && Number.isInteger(v) && v >= 0 && v <= 2 ** 53) return v;
|
|
2353
|
-
if (typeof v === "number" && Number.isInteger(v) && v > 2 ** 53) return fail(`${what} above 2^53 must be passed as a bigint`);
|
|
2354
|
-
return fail(`${what} is not a u64`);
|
|
2355
|
-
}
|
|
2356
|
-
function u32Of(v, what) {
|
|
2357
|
-
return typeof v === "number" && Number.isInteger(v) && v >= 0 && v <= 4294967295 ? v : fail(`${what} is not a u32`);
|
|
2358
|
-
}
|
|
2359
|
-
function checkRule(rule) {
|
|
2360
|
-
const d = rule?.domain;
|
|
2361
|
-
if (typeof d !== "string" || d === "" || !isWellFormedUtf162(d) || d.includes("\0") || d.includes(""))
|
|
2362
|
-
fail("the rule's domain must be non-empty, well-formed text without U+0000 or U+0001");
|
|
2363
|
-
if (!isIndex(rule.activationHeight)) fail("the rule's activation height must be a safe integer >= 0");
|
|
2364
|
-
}
|
|
2365
|
-
var kindOf = (app) => {
|
|
2366
|
-
const t = app?.type;
|
|
2367
|
-
return t === "None" ? "none" : t === "Propose" ? "propose" : t === "Attest" ? "attest" : fail(`unknown app type ${String(t)}`);
|
|
2368
|
-
};
|
|
2369
|
-
var domainOf = (app) => typeof app.domain === "string" ? app.domain : fail("a Propose domain is not text");
|
|
2370
|
-
function paidToOf(outputs) {
|
|
2371
|
-
if (!Array.isArray(outputs)) fail("outputs is not a list");
|
|
2372
|
-
const counted = [];
|
|
2373
|
-
for (const o of outputs) {
|
|
2374
|
-
const addr = hexOf(o?.scriptPubkey, 20, "script_pubkey"), value = u64Of(o?.value, "output value");
|
|
2375
|
-
if (value <= Number.MAX_SAFE_INTEGER) counted.push({ addr, value: Number(value) });
|
|
2376
|
-
}
|
|
2377
|
-
return paidToFromOutputs(counted);
|
|
2378
|
-
}
|
|
2379
|
-
function eventOfTx(t, height, author, rule = CAIRNX_EVENT_RULE) {
|
|
2380
|
-
checkRule(rule);
|
|
2381
|
-
if (!isIndex(height)) fail("height is not a safe integer >= 0");
|
|
2382
|
-
if (!isIndex(t?.pos)) fail("pos is not a safe integer >= 0");
|
|
2383
|
-
const txid = hexOf(t.txid, 32, "txid");
|
|
2384
|
-
const app = t.tx?.app;
|
|
2385
|
-
const kind = kindOf(app);
|
|
2386
|
-
if (kind === "none" || height < rule.activationHeight) return null;
|
|
2387
|
-
if (t.pos === 0) fail("position 0 is the coinbase, which carries no app payload");
|
|
2388
|
-
if (kind === "propose" && domainOf(app) !== rule.domain) return null;
|
|
2389
|
-
const paidTo = paidToOf(t.tx.outputs), who = hexOf(author, 20, "author");
|
|
2390
|
-
if (kind === "propose") {
|
|
2391
|
-
const p = app;
|
|
2392
|
-
const uri = p.uri;
|
|
2393
|
-
if (typeof uri !== "string" || !isWellFormedUtf162(uri)) return fail("uri is not well-formed text");
|
|
2394
|
-
const e = u64Of(p.expiresEpoch, "expiresEpoch");
|
|
2395
|
-
return {
|
|
2396
|
-
kind: "propose",
|
|
2397
|
-
id: txid,
|
|
2398
|
-
proposer: who,
|
|
2399
|
-
uri: uri.replaceAll("\0", ""),
|
|
2400
|
-
payloadHash: hexOf(p.payloadHash, 32, "payloadHash"),
|
|
2401
|
-
expiresEpoch: e <= Number.MAX_SAFE_INTEGER ? Number(e) : 2 ** 53,
|
|
2402
|
-
height,
|
|
2403
|
-
pos: t.pos,
|
|
2404
|
-
paidTo
|
|
2405
|
-
};
|
|
2406
|
-
}
|
|
2407
|
-
const a = app;
|
|
2408
|
-
return {
|
|
2409
|
-
kind: "attest",
|
|
2410
|
-
txid,
|
|
2411
|
-
proposalId: hexOf(a.proposalId, 32, "proposalId"),
|
|
2412
|
-
attester: who,
|
|
2413
|
-
score: u32Of(a.score, "score"),
|
|
2414
|
-
confidence: u32Of(a.confidence, "confidence"),
|
|
2415
|
-
height,
|
|
2416
|
-
pos: t.pos,
|
|
2417
|
-
paidTo
|
|
2418
|
-
};
|
|
2419
|
-
}
|
|
2420
|
-
function blockMembers(height, txs, sofar, rule = CAIRNX_EVENT_RULE) {
|
|
2421
|
-
checkRule(rule);
|
|
2422
|
-
if (!isIndex(height)) fail("height is not a safe integer >= 0");
|
|
2423
|
-
if (height < rule.activationHeight) return [];
|
|
2424
|
-
if (!Array.isArray(txs)) fail("txs is not a list");
|
|
2425
|
-
const ids = /* @__PURE__ */ new Set(), proposals = /* @__PURE__ */ new Set();
|
|
2426
|
-
const read = [];
|
|
2427
|
-
let last = -1;
|
|
2428
|
-
for (const item of txs) {
|
|
2429
|
-
if (!isIndex(item?.pos) || item.pos <= last) fail("positions are not safe integers >= 0 in strictly increasing order");
|
|
2430
|
-
last = item.pos;
|
|
2431
|
-
const id = hexOf(item.txid, 32, "txid");
|
|
2432
|
-
if (ids.has(id)) fail(`txid ${id} is repeated in the block`);
|
|
2433
|
-
ids.add(id);
|
|
2434
|
-
const kind = kindOf(item.tx?.app);
|
|
2435
|
-
if (kind === "none") continue;
|
|
2436
|
-
if (item.pos === 0) fail("position 0 is the coinbase, which carries no app payload");
|
|
2437
|
-
if (kind === "propose") {
|
|
2438
|
-
if (domainOf(item.tx.app) !== rule.domain || sofar.eventIds.has(id)) continue;
|
|
2439
|
-
proposals.add(id);
|
|
2440
|
-
read.push({ item, kind, id });
|
|
2441
|
-
} else read.push({ item, kind, id, proposal: hexOf(item.tx.app.proposalId, 32, "proposalId") });
|
|
2442
|
-
}
|
|
2443
|
-
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 }));
|
|
2444
|
-
}
|
|
2445
|
-
function blockEvents(height, txs, sofar, authors, rule = CAIRNX_EVENT_RULE) {
|
|
2446
|
-
const members = blockMembers(height, txs, sofar, rule);
|
|
2447
|
-
if (!Array.isArray(authors) || authors.length !== members.length) fail(`the block has ${members.length} members, not ${authors?.length} authors`);
|
|
2448
|
-
const events = members.map((m, i) => eventOfTx(m.item, height, authors[i], rule) ?? fail("a member mapped to no event"));
|
|
2449
|
-
return [...events.filter((e) => e.kind === "propose"), ...events.filter((e) => e.kind === "attest")];
|
|
2450
|
-
}
|
|
2451
|
-
|
|
2452
2591
|
// src/hashes.ts
|
|
2453
2592
|
import { bytesToHex, concatBytes, hexToBytes, utf8ToBytes } from "@inversealtruism/csd-codec";
|
|
2454
2593
|
import { sha256 } from "@noble/hashes/sha256";
|
|
@@ -2776,6 +2915,7 @@ export {
|
|
|
2776
2915
|
REG_FINALIZE_GRACE_BLOCKS,
|
|
2777
2916
|
RESERVED_NAMES,
|
|
2778
2917
|
RecordError,
|
|
2918
|
+
ReplayContextRefusal,
|
|
2779
2919
|
SALT_RE,
|
|
2780
2920
|
SCAN_MARGIN,
|
|
2781
2921
|
SCORE_CANCEL,
|
|
@@ -2839,6 +2979,7 @@ export {
|
|
|
2839
2979
|
fillTargetId,
|
|
2840
2980
|
finalizeWinnerCheck,
|
|
2841
2981
|
hasLiveClaim,
|
|
2982
|
+
isChainHeight,
|
|
2842
2983
|
isName,
|
|
2843
2984
|
isNameGive,
|
|
2844
2985
|
isOpenClaimLane,
|
|
@@ -2856,6 +2997,7 @@ export {
|
|
|
2856
2997
|
nameSet,
|
|
2857
2998
|
nameView,
|
|
2858
2999
|
nameXfer,
|
|
3000
|
+
newReplayContext,
|
|
2859
3001
|
offer,
|
|
2860
3002
|
offerCancelAll,
|
|
2861
3003
|
offerExpiryHeightOf,
|