@activeledger/activehybrid 5.0.2 → 5.1.1
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/README.md +13 -46
- package/es/api.d.ts +53 -0
- package/es/api.d.ts.map +1 -0
- package/es/api.js +365 -0
- package/es/api.js.map +1 -0
- package/es/bootstrap.d.ts +36 -0
- package/es/bootstrap.d.ts.map +1 -0
- package/es/bootstrap.js +106 -0
- package/es/bootstrap.js.map +1 -0
- package/es/commands.d.ts +25 -0
- package/es/commands.d.ts.map +1 -0
- package/es/commands.js +185 -0
- package/es/commands.js.map +1 -0
- package/es/config.d.ts +17 -0
- package/es/config.d.ts.map +1 -0
- package/es/config.js +395 -0
- package/es/config.js.map +1 -0
- package/es/contracts.d.ts +31 -0
- package/es/contracts.d.ts.map +1 -0
- package/es/contracts.js +171 -0
- package/es/contracts.js.map +1 -0
- package/es/divergence.d.ts +8 -0
- package/es/divergence.d.ts.map +1 -0
- package/es/divergence.js +40 -0
- package/es/divergence.js.map +1 -0
- package/es/events.d.ts +38 -0
- package/es/events.d.ts.map +1 -0
- package/es/events.js +144 -0
- package/es/events.js.map +1 -0
- package/es/feed.d.ts +42 -0
- package/es/feed.d.ts.map +1 -0
- package/es/feed.js +167 -0
- package/es/feed.js.map +1 -0
- package/es/http-upstream.d.ts +83 -0
- package/es/http-upstream.d.ts.map +1 -0
- package/es/http-upstream.js +360 -0
- package/es/http-upstream.js.map +1 -0
- package/es/index.js +211 -43
- package/es/index.js.map +1 -1
- package/es/lookup.d.ts +34 -0
- package/es/lookup.d.ts.map +1 -0
- package/es/lookup.js +194 -0
- package/es/lookup.js.map +1 -0
- package/es/node.d.ts +42 -0
- package/es/node.d.ts.map +1 -0
- package/es/node.js +219 -0
- package/es/node.js.map +1 -0
- package/es/orderer.d.ts +30 -0
- package/es/orderer.d.ts.map +1 -0
- package/es/orderer.js +134 -0
- package/es/orderer.js.map +1 -0
- package/es/p2p-upstream.d.ts +47 -0
- package/es/p2p-upstream.d.ts.map +1 -0
- package/es/p2p-upstream.js +218 -0
- package/es/p2p-upstream.js.map +1 -0
- package/es/processor.d.ts +61 -0
- package/es/processor.d.ts.map +1 -0
- package/es/processor.js +640 -0
- package/es/processor.js.map +1 -0
- package/es/program.d.ts +3 -0
- package/es/program.d.ts.map +1 -0
- package/es/program.js +57 -0
- package/es/program.js.map +1 -0
- package/es/pruner.d.ts +47 -0
- package/es/pruner.d.ts.map +1 -0
- package/es/pruner.js +143 -0
- package/es/pruner.js.map +1 -0
- package/es/registry.d.ts +43 -0
- package/es/registry.d.ts.map +1 -0
- package/es/registry.js +49 -0
- package/es/registry.js.map +1 -0
- package/es/run.d.ts +36 -0
- package/es/run.d.ts.map +1 -0
- package/es/run.js +158 -0
- package/es/run.js.map +1 -0
- package/es/submit.d.ts +50 -0
- package/es/submit.d.ts.map +1 -0
- package/es/submit.js +186 -0
- package/es/submit.js.map +1 -0
- package/es/types.d.ts +67 -0
- package/es/types.d.ts.map +1 -0
- package/es/types.js +2 -0
- package/es/types.js.map +1 -0
- package/es/upstream.d.ts +36 -0
- package/es/upstream.d.ts.map +1 -0
- package/es/upstream.js +2 -0
- package/es/upstream.js.map +1 -0
- package/es/verify.d.ts +59 -0
- package/es/verify.d.ts.map +1 -0
- package/es/verify.js +203 -0
- package/es/verify.js.map +1 -0
- package/es/workspace.d.ts +4 -0
- package/es/workspace.d.ts.map +1 -0
- package/es/workspace.js +40 -0
- package/es/workspace.js.map +1 -0
- package/lib/api.d.ts +53 -0
- package/lib/api.d.ts.map +1 -0
- package/lib/api.js +402 -0
- package/lib/api.js.map +1 -0
- package/lib/bootstrap.d.ts +36 -0
- package/lib/bootstrap.d.ts.map +1 -0
- package/lib/bootstrap.js +116 -0
- package/lib/bootstrap.js.map +1 -0
- package/lib/commands.d.ts +25 -0
- package/lib/commands.d.ts.map +1 -0
- package/lib/commands.js +229 -0
- package/lib/commands.js.map +1 -0
- package/lib/config.d.ts +17 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +437 -0
- package/lib/config.js.map +1 -0
- package/lib/contracts.d.ts +31 -0
- package/lib/contracts.d.ts.map +1 -0
- package/lib/contracts.js +210 -0
- package/lib/contracts.js.map +1 -0
- package/lib/divergence.d.ts +8 -0
- package/lib/divergence.d.ts.map +1 -0
- package/lib/divergence.js +46 -0
- package/lib/divergence.js.map +1 -0
- package/lib/events.d.ts +38 -0
- package/lib/events.d.ts.map +1 -0
- package/lib/events.js +152 -0
- package/lib/events.js.map +1 -0
- package/lib/feed.d.ts +42 -0
- package/lib/feed.d.ts.map +1 -0
- package/lib/feed.js +173 -0
- package/lib/feed.js.map +1 -0
- package/lib/http-upstream.d.ts +83 -0
- package/lib/http-upstream.d.ts.map +1 -0
- package/lib/http-upstream.js +401 -0
- package/lib/http-upstream.js.map +1 -0
- package/lib/index.js +210 -42
- package/lib/index.js.map +1 -1
- package/lib/lookup.d.ts +34 -0
- package/lib/lookup.d.ts.map +1 -0
- package/lib/lookup.js +201 -0
- package/lib/lookup.js.map +1 -0
- package/lib/node.d.ts +42 -0
- package/lib/node.d.ts.map +1 -0
- package/lib/node.js +257 -0
- package/lib/node.js.map +1 -0
- package/lib/orderer.d.ts +30 -0
- package/lib/orderer.d.ts.map +1 -0
- package/lib/orderer.js +138 -0
- package/lib/orderer.js.map +1 -0
- package/lib/p2p-upstream.d.ts +47 -0
- package/lib/p2p-upstream.d.ts.map +1 -0
- package/lib/p2p-upstream.js +222 -0
- package/lib/p2p-upstream.js.map +1 -0
- package/lib/processor.d.ts +61 -0
- package/lib/processor.d.ts.map +1 -0
- package/lib/processor.js +644 -0
- package/lib/processor.js.map +1 -0
- package/lib/program.d.ts +3 -0
- package/lib/program.d.ts.map +1 -0
- package/lib/program.js +61 -0
- package/lib/program.js.map +1 -0
- package/lib/pruner.d.ts +47 -0
- package/lib/pruner.d.ts.map +1 -0
- package/lib/pruner.js +147 -0
- package/lib/pruner.js.map +1 -0
- package/lib/registry.d.ts +43 -0
- package/lib/registry.d.ts.map +1 -0
- package/lib/registry.js +58 -0
- package/lib/registry.js.map +1 -0
- package/lib/run.d.ts +36 -0
- package/lib/run.d.ts.map +1 -0
- package/lib/run.js +206 -0
- package/lib/run.js.map +1 -0
- package/lib/submit.d.ts +50 -0
- package/lib/submit.d.ts.map +1 -0
- package/lib/submit.js +226 -0
- package/lib/submit.js.map +1 -0
- package/lib/types.d.ts +67 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/{interfaces/hybrid.interface.js → types.js} +1 -1
- package/lib/types.js.map +1 -0
- package/lib/upstream.d.ts +36 -0
- package/lib/upstream.d.ts.map +1 -0
- package/lib/upstream.js +3 -0
- package/lib/upstream.js.map +1 -0
- package/lib/verify.d.ts +59 -0
- package/lib/verify.d.ts.map +1 -0
- package/lib/verify.js +218 -0
- package/lib/verify.js.map +1 -0
- package/lib/workspace.d.ts +4 -0
- package/lib/workspace.d.ts.map +1 -0
- package/lib/workspace.js +78 -0
- package/lib/workspace.js.map +1 -0
- package/package.json +14 -14
- package/es/contract.d.ts +0 -5
- package/es/contract.d.ts.map +0 -1
- package/es/contract.js +0 -33
- package/es/contract.js.map +0 -1
- package/es/contracts/default/contract.d.ts +0 -37
- package/es/contracts/default/contract.d.ts.map +0 -1
- package/es/contracts/default/contract.js +0 -848
- package/es/contracts/default/contract.js.map +0 -1
- package/es/contracts/default/fund.d.ts +0 -14
- package/es/contracts/default/fund.d.ts.map +0 -1
- package/es/contracts/default/fund.js +0 -90
- package/es/contracts/default/fund.js.map +0 -1
- package/es/contracts/default/namespace.d.ts +0 -10
- package/es/contracts/default/namespace.d.ts.map +0 -1
- package/es/contracts/default/namespace.js +0 -47
- package/es/contracts/default/namespace.js.map +0 -1
- package/es/contracts/default/onboard.d.ts +0 -7
- package/es/contracts/default/onboard.d.ts.map +0 -1
- package/es/contracts/default/onboard.js +0 -45
- package/es/contracts/default/onboard.js.map +0 -1
- package/es/contracts/default/setup.d.ts +0 -33
- package/es/contracts/default/setup.d.ts.map +0 -1
- package/es/contracts/default/setup.js +0 -413
- package/es/contracts/default/setup.js.map +0 -1
- package/es/default.config.json +0 -40
- package/es/interfaces/hybrid.interface.d.ts +0 -7
- package/es/interfaces/hybrid.interface.d.ts.map +0 -1
- package/es/interfaces/hybrid.interface.js +0 -2
- package/es/interfaces/hybrid.interface.js.map +0 -1
- package/es/server.d.ts +0 -23
- package/es/server.d.ts.map +0 -1
- package/es/server.js +0 -232
- package/es/server.js.map +0 -1
- package/lib/contract.d.ts +0 -5
- package/lib/contract.d.ts.map +0 -1
- package/lib/contract.js +0 -70
- package/lib/contract.js.map +0 -1
- package/lib/contracts/default/contract.d.ts +0 -37
- package/lib/contracts/default/contract.d.ts.map +0 -1
- package/lib/contracts/default/contract.js +0 -884
- package/lib/contracts/default/contract.js.map +0 -1
- package/lib/contracts/default/fund.d.ts +0 -14
- package/lib/contracts/default/fund.d.ts.map +0 -1
- package/lib/contracts/default/fund.js +0 -93
- package/lib/contracts/default/fund.js.map +0 -1
- package/lib/contracts/default/namespace.d.ts +0 -10
- package/lib/contracts/default/namespace.d.ts.map +0 -1
- package/lib/contracts/default/namespace.js +0 -83
- package/lib/contracts/default/namespace.js.map +0 -1
- package/lib/contracts/default/onboard.d.ts +0 -7
- package/lib/contracts/default/onboard.d.ts.map +0 -1
- package/lib/contracts/default/onboard.js +0 -48
- package/lib/contracts/default/onboard.js.map +0 -1
- package/lib/contracts/default/setup.d.ts +0 -33
- package/lib/contracts/default/setup.d.ts.map +0 -1
- package/lib/contracts/default/setup.js +0 -449
- package/lib/contracts/default/setup.js.map +0 -1
- package/lib/default.config.json +0 -40
- package/lib/interfaces/hybrid.interface.d.ts +0 -7
- package/lib/interfaces/hybrid.interface.d.ts.map +0 -1
- package/lib/interfaces/hybrid.interface.js.map +0 -1
- package/lib/server.d.ts +0 -23
- package/lib/server.d.ts.map +0 -1
- package/lib/server.js +0 -236
- package/lib/server.js.map +0 -1
package/es/processor.js
ADDED
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
import { ActiveCrypto } from "@activeledger/activecrypto";
|
|
2
|
+
import { ActiveLogger } from "@activeledger/activelogger";
|
|
3
|
+
import { ActiveProtocol } from "@activeledger/activeprotocol";
|
|
4
|
+
import { revPosition, spiConsensus } from "@activeledger/activesync";
|
|
5
|
+
import { ContractBuilder, applyExecutionOptions } from "./contracts";
|
|
6
|
+
import { writeDivergence } from "./divergence";
|
|
7
|
+
import { readDoc, splitRev, writeAtRev } from "./lookup";
|
|
8
|
+
import { docHash, quorum, reaches, verifyFeedItem } from "./verify";
|
|
9
|
+
const canonical = (v) => ActiveCrypto.canonicalJson(v);
|
|
10
|
+
const errorText = (e) => e?.message || String(e);
|
|
11
|
+
const clone = (v) => JSON.parse(JSON.stringify(v));
|
|
12
|
+
const baseId = (id) => id.split(":")[0];
|
|
13
|
+
export class Processor {
|
|
14
|
+
options;
|
|
15
|
+
execute;
|
|
16
|
+
contracts;
|
|
17
|
+
done = new Set();
|
|
18
|
+
constructor(options) {
|
|
19
|
+
this.options = options;
|
|
20
|
+
this.execute =
|
|
21
|
+
options.execute ??
|
|
22
|
+
((entry, db, opts) => ActiveProtocol.Process.dryRunHybrid(entry, db, opts));
|
|
23
|
+
this.contracts =
|
|
24
|
+
options.contracts === undefined
|
|
25
|
+
? new ContractBuilder({
|
|
26
|
+
store: options.store,
|
|
27
|
+
sources: options.sources,
|
|
28
|
+
lookup: options.lookup,
|
|
29
|
+
verify: (state, meta) => this.streamAttested(state, meta),
|
|
30
|
+
})
|
|
31
|
+
: options.contracts;
|
|
32
|
+
}
|
|
33
|
+
async streamAttested(state, meta) {
|
|
34
|
+
const ctx = this.options.context();
|
|
35
|
+
const umid = meta?.umid;
|
|
36
|
+
if (!ctx || typeof umid !== "string" || !state?._id)
|
|
37
|
+
return false;
|
|
38
|
+
const { supernodes, attesters = [] } = this.sources();
|
|
39
|
+
let umidDoc;
|
|
40
|
+
const attestations = [];
|
|
41
|
+
for (const up of [...supernodes, ...attesters]) {
|
|
42
|
+
try {
|
|
43
|
+
umidDoc ??= await up.umid(umid);
|
|
44
|
+
const a = await up.attest(umid);
|
|
45
|
+
if (a)
|
|
46
|
+
attestations.push(a);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (!umidDoc?.umid?.$revs)
|
|
52
|
+
return false;
|
|
53
|
+
const q = quorum(attestations, umidDoc.umid.$revs, ctx.keys, ctx.reachedPercent, ctx.networkSize);
|
|
54
|
+
if (!q.reached)
|
|
55
|
+
return false;
|
|
56
|
+
const agreed = q.agreed[0];
|
|
57
|
+
return (docHash(umidDoc) === agreed.umidDocHash &&
|
|
58
|
+
agreed.revsOut?.[state._id]?.sha256 === docHash(state) &&
|
|
59
|
+
agreed.revsOut?.[`${state._id}:stream`]?.sha256 === docHash(meta));
|
|
60
|
+
}
|
|
61
|
+
get contractBuilder() {
|
|
62
|
+
return this.contracts;
|
|
63
|
+
}
|
|
64
|
+
sources() {
|
|
65
|
+
const s = this.options.sources;
|
|
66
|
+
return typeof s === "function" ? s() : s;
|
|
67
|
+
}
|
|
68
|
+
get data() {
|
|
69
|
+
return this.options.store.data;
|
|
70
|
+
}
|
|
71
|
+
async apply(item, from) {
|
|
72
|
+
try {
|
|
73
|
+
return await this.applyItem(item, from);
|
|
74
|
+
}
|
|
75
|
+
catch (e) {
|
|
76
|
+
ActiveLogger.error(e, `Hybrid apply ${item?.umid} failed`);
|
|
77
|
+
return transient(item?.umid, [`apply failed: ${errorText(e)}`]);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
async applyItem(item, from) {
|
|
81
|
+
const ctx = this.options.context();
|
|
82
|
+
if (!ctx)
|
|
83
|
+
return outcome(item?.umid, "dropped", "no", ["not bootstrapped"]);
|
|
84
|
+
const check = verifyFeedItem(item, ctx);
|
|
85
|
+
if (!check.ok) {
|
|
86
|
+
ActiveLogger.warn(`Dropped feed item ${item?.umid} from ${from?.id}: ${check.reasons.join("; ")}`);
|
|
87
|
+
return outcome(item?.umid, "dropped", "no", check.reasons);
|
|
88
|
+
}
|
|
89
|
+
if (this.done.has(`${item.outcome}:${item.umid}`)) {
|
|
90
|
+
return outcome(item.umid, "settled", item.outcome === "committed" ? "yes" : "no", ["already applied"]);
|
|
91
|
+
}
|
|
92
|
+
const tallied = { yes: check.yes, no: check.no };
|
|
93
|
+
const result = item.outcome === "committed"
|
|
94
|
+
? await this.committed(item, ctx, tallied)
|
|
95
|
+
: await this.failed(item, ctx, tallied, from);
|
|
96
|
+
if (result.action !== "dropped")
|
|
97
|
+
this.remember(`${item.outcome}:${item.umid}`);
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
remember(key) {
|
|
101
|
+
this.done.add(key);
|
|
102
|
+
if (this.done.size > 10000) {
|
|
103
|
+
const first = this.done.values().next().value;
|
|
104
|
+
if (first !== undefined)
|
|
105
|
+
this.done.delete(first);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
async committed(item, ctx, tallied) {
|
|
109
|
+
const umid = item.umid;
|
|
110
|
+
const att = item.attestation;
|
|
111
|
+
const revsOut = att.revsOut || {};
|
|
112
|
+
if (docHash(item.umidDoc) !== att.umidDocHash) {
|
|
113
|
+
return outcome(umid, "dropped", "no", ["umid doc does not match the attestation's umidDocHash"]);
|
|
114
|
+
}
|
|
115
|
+
if (await readDoc(this.data, `${umid}:umid`)) {
|
|
116
|
+
return outcome(umid, "settled", "yes", ["already applied"]);
|
|
117
|
+
}
|
|
118
|
+
if (Object.keys(revsOut).length && (await this.mismatched(revsOut, [])).length === 0) {
|
|
119
|
+
await this.writeUmidDoc(item);
|
|
120
|
+
return outcome(umid, "settled", "yes", ["outputs already at the attested revisions"]);
|
|
121
|
+
}
|
|
122
|
+
const missing = await this.prepare(item);
|
|
123
|
+
if (missing.length) {
|
|
124
|
+
const covered = await this.coveredByNewerState(item, revsOut);
|
|
125
|
+
if (covered)
|
|
126
|
+
return outcome(umid, "settled", "no", ["superseded: the streams are already past this item"]);
|
|
127
|
+
return transient(umid, [`streams not available at $revs: ${missing.join(", ")}`]);
|
|
128
|
+
}
|
|
129
|
+
const before = await this.snapshot(item, revsOut);
|
|
130
|
+
const entry = item.umidDoc.umid;
|
|
131
|
+
let result;
|
|
132
|
+
let execError = "";
|
|
133
|
+
try {
|
|
134
|
+
result = await this.run(entry);
|
|
135
|
+
}
|
|
136
|
+
catch (e1) {
|
|
137
|
+
ActiveLogger.warn(`Execution of ${umid} failed, retrying: ${errorText(e1)}`);
|
|
138
|
+
try {
|
|
139
|
+
result = await this.run(entry);
|
|
140
|
+
}
|
|
141
|
+
catch (e2) {
|
|
142
|
+
execError = errorText(e2);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (!result) {
|
|
146
|
+
return this.repair(item, ctx, tallied, before, [], "execution-error", { error: execError });
|
|
147
|
+
}
|
|
148
|
+
const touched = result.committed ? await this.saveLocal(result) : [];
|
|
149
|
+
const wrong = result.committed ? await this.mismatched(revsOut, touched) : ["not committed locally"];
|
|
150
|
+
const localSummary = await this.summary(result, touched);
|
|
151
|
+
if (!wrong.length) {
|
|
152
|
+
const reasons = [];
|
|
153
|
+
let divergence;
|
|
154
|
+
if (result.umidDoc &&
|
|
155
|
+
!result.supernodeDependent &&
|
|
156
|
+
canonical(executionParts(result.umidDoc)) !== canonical(executionParts(item.umidDoc))) {
|
|
157
|
+
reasons.push("local :umid doc streams/events differ from the received doc");
|
|
158
|
+
divergence = await this.diverge(item, "umid-doc-mismatch", tallied, localSummary.result, localSummary.revs);
|
|
159
|
+
}
|
|
160
|
+
await this.finishCommitted(item, revsOut, result);
|
|
161
|
+
return { ...outcome(umid, "saved", "yes", reasons), ...(divergence ? { divergence } : {}) };
|
|
162
|
+
}
|
|
163
|
+
if (result.supernodeDependent) {
|
|
164
|
+
return this.adopt(item, ctx, tallied, result, before, touched, localSummary);
|
|
165
|
+
}
|
|
166
|
+
return this.repair(item, ctx, tallied, before, touched, "output-mismatch", {
|
|
167
|
+
...localSummary.result,
|
|
168
|
+
mismatched: wrong,
|
|
169
|
+
}, localSummary.revs);
|
|
170
|
+
}
|
|
171
|
+
async adopt(item, ctx, tallied, result, before, touched, local) {
|
|
172
|
+
const umid = item.umid;
|
|
173
|
+
const revsOut = item.attestation.revsOut;
|
|
174
|
+
const q = await this.gatherQuorum(item, ctx);
|
|
175
|
+
if (!q.reached) {
|
|
176
|
+
await this.rollback(before, touched);
|
|
177
|
+
return transient(umid, [
|
|
178
|
+
`adopt needs ${ctx.reachedPercent}% matching commit attestations, have ${q.count}/${ctx.networkSize}`,
|
|
179
|
+
]);
|
|
180
|
+
}
|
|
181
|
+
const fetched = await this.fetchSigned(revsOut);
|
|
182
|
+
if (fetched.missing.length) {
|
|
183
|
+
await this.rollback(before, touched);
|
|
184
|
+
return transient(umid, [`attested outputs not available: ${fetched.missing.join(", ")}`]);
|
|
185
|
+
}
|
|
186
|
+
const verdict = ActiveProtocol.checkAdopt({
|
|
187
|
+
umid,
|
|
188
|
+
signed: fetched.docs,
|
|
189
|
+
local: result.documents,
|
|
190
|
+
before,
|
|
191
|
+
declarations: result.adopt || [],
|
|
192
|
+
supernodeDependent: true,
|
|
193
|
+
});
|
|
194
|
+
if (!(await this.writeDocs(fetched.docs, before, touched))) {
|
|
195
|
+
await this.rollback(before, touched);
|
|
196
|
+
return transient(umid, ["writing the attested outputs failed"]);
|
|
197
|
+
}
|
|
198
|
+
if (verdict.ok) {
|
|
199
|
+
const warnings = [];
|
|
200
|
+
if (verdict.mode === "undeclared") {
|
|
201
|
+
warnings.push("adopt.undeclared");
|
|
202
|
+
await this.emit({
|
|
203
|
+
type: "adopt.undeclared",
|
|
204
|
+
contract: entryOf(item)?.$tx?.$contract,
|
|
205
|
+
umid,
|
|
206
|
+
payload: {
|
|
207
|
+
contract: entryOf(item)?.$tx?.$contract,
|
|
208
|
+
entry: entryOf(item)?.$tx?.$entry ?? null,
|
|
209
|
+
dependence: result.dependence || [],
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
await this.finishCommitted(item, revsOut, result, fetched.docs);
|
|
214
|
+
return { ...outcome(umid, "adopted", "yes", []), warnings };
|
|
215
|
+
}
|
|
216
|
+
const divergence = await this.diverge(item, "adopt-rejected", tallied, { ...local.result, adoptReasons: verdict.reasons }, local.revs);
|
|
217
|
+
await this.finishCommitted(item, revsOut, undefined, fetched.docs);
|
|
218
|
+
return { ...outcome(umid, "repaired", "yes", verdict.reasons), divergence };
|
|
219
|
+
}
|
|
220
|
+
async repair(item, ctx, tallied, before, touched, kind, localResult, localRevs) {
|
|
221
|
+
const umid = item.umid;
|
|
222
|
+
const revsOut = item.attestation.revsOut;
|
|
223
|
+
const q = await this.gatherQuorum(item, ctx);
|
|
224
|
+
const spi = await this.spi(revsOut, q.reached);
|
|
225
|
+
if (spi.missing.length || !(await this.writeDocs(spi.docs, before, touched))) {
|
|
226
|
+
await this.rollback(before, touched);
|
|
227
|
+
const reasons = [`SPI could not repair: ${spi.missing.join(", ") || "write failed"}`];
|
|
228
|
+
const divergence = await this.diverge(item, kind, tallied, { ...localResult, spi: reasons }, localRevs);
|
|
229
|
+
return { ...outcome(umid, "dropped", "no", reasons), divergence };
|
|
230
|
+
}
|
|
231
|
+
const divergence = await this.diverge(item, kind, tallied, localResult, localRevs);
|
|
232
|
+
await this.finishCommitted(item, revsOut, undefined, spi.docs);
|
|
233
|
+
return { ...outcome(umid, "repaired", "yes", [`repaired by SPI (${kind})`]), divergence };
|
|
234
|
+
}
|
|
235
|
+
async finishCommitted(item, revsOut, result, written) {
|
|
236
|
+
await this.writeUmidDoc(item);
|
|
237
|
+
if (result && result.volatile && Object.keys(result.volatile).length) {
|
|
238
|
+
await this.data.bulkDocs(Object.values(clone(result.volatile)), { new_edits: true });
|
|
239
|
+
}
|
|
240
|
+
if (this.contracts) {
|
|
241
|
+
const ids = Object.keys(written ?? revsOut).filter((id) => !id.includes(":"));
|
|
242
|
+
const docs = [];
|
|
243
|
+
for (const id of ids) {
|
|
244
|
+
const doc = await readDoc(this.data, id);
|
|
245
|
+
if (doc)
|
|
246
|
+
docs.push(doc);
|
|
247
|
+
}
|
|
248
|
+
await this.contracts.onWritten(docs);
|
|
249
|
+
}
|
|
250
|
+
const events = Array.isArray(item.umidDoc?.events) ? item.umidDoc.events : result?.events || [];
|
|
251
|
+
await this.emitContractEvents(item, events);
|
|
252
|
+
await this.emit({
|
|
253
|
+
type: "tx.committed",
|
|
254
|
+
umid: item.umid,
|
|
255
|
+
contract: entryOf(item)?.$tx?.$contract,
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
async writeUmidDoc(item) {
|
|
259
|
+
const doc = item.umidDoc;
|
|
260
|
+
if (!doc || docHash(doc) !== item.attestation?.umidDocHash) {
|
|
261
|
+
throw new Error(`${item.umid}: umid doc does not match umidDocHash`);
|
|
262
|
+
}
|
|
263
|
+
if (!(await writeAtRev(this.data, clone(doc)))) {
|
|
264
|
+
throw new Error(`${item.umid}: writing the :umid doc failed`);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
async failed(item, ctx, tallied, from) {
|
|
268
|
+
const umid = item.umid;
|
|
269
|
+
if (!item.tx || typeof item.tx !== "object" || !item.tx.$tx) {
|
|
270
|
+
return outcome(umid, "dropped", "no", ["failed item carries no transaction"]);
|
|
271
|
+
}
|
|
272
|
+
if (reaches(tallied.yes, ctx.reachedPercent, ctx.networkSize)) {
|
|
273
|
+
const { supernodes, peers } = this.sources();
|
|
274
|
+
let asked = false;
|
|
275
|
+
for (const source of [...supernodes, ...peers]) {
|
|
276
|
+
if (source === from || source.id === from?.id)
|
|
277
|
+
continue;
|
|
278
|
+
try {
|
|
279
|
+
const doc = await source.umid(umid);
|
|
280
|
+
asked = true;
|
|
281
|
+
if (doc?.umid?.$umid === umid) {
|
|
282
|
+
return outcome(umid, "dropped", "no", [
|
|
283
|
+
`failed item reaches consensus and ${source.id} shows it committed`,
|
|
284
|
+
]);
|
|
285
|
+
}
|
|
286
|
+
break;
|
|
287
|
+
}
|
|
288
|
+
catch {
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
if (!asked) {
|
|
292
|
+
return transient(umid, ["failed item reaches consensus and no other upstream could confirm it"]);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
const missing = await this.prepare(item);
|
|
296
|
+
if (missing.length) {
|
|
297
|
+
return transient(umid, [`streams not available at $revs: ${missing.join(", ")}`]);
|
|
298
|
+
}
|
|
299
|
+
const entry = { ...item.tx, $revs: item.$revs };
|
|
300
|
+
let result;
|
|
301
|
+
let execError = "";
|
|
302
|
+
try {
|
|
303
|
+
result = await this.run(entry);
|
|
304
|
+
}
|
|
305
|
+
catch (e1) {
|
|
306
|
+
try {
|
|
307
|
+
result = await this.run(entry);
|
|
308
|
+
}
|
|
309
|
+
catch (e2) {
|
|
310
|
+
execError = errorText(e2);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
await this.emit({ type: "tx.failed", umid, contract: item.tx.$tx.$contract });
|
|
314
|
+
if (!result) {
|
|
315
|
+
const divergence = await this.diverge(item, "execution-error", tallied, { error: execError });
|
|
316
|
+
return { ...outcome(umid, "recorded", "no", [`execution error: ${execError}`]), divergence };
|
|
317
|
+
}
|
|
318
|
+
const passed = result.vote && result.committed;
|
|
319
|
+
if (passed && !result.supernodeDependent) {
|
|
320
|
+
const divergence = await this.diverge(item, "failed-but-passed", tallied, (await this.summary(result, [])).result);
|
|
321
|
+
return { ...outcome(umid, "recorded", "yes", ["passed locally"]), divergence };
|
|
322
|
+
}
|
|
323
|
+
return outcome(umid, "recorded", result.supernodeDependent ? "conditional" : "no", result.supernodeDependent ? ["supernode-dependent; not judged"] : []);
|
|
324
|
+
}
|
|
325
|
+
async prepare(item) {
|
|
326
|
+
const wanted = [];
|
|
327
|
+
for (const side of [item.$revs?.$i, item.$revs?.$o]) {
|
|
328
|
+
for (const id of Object.keys(side || {}))
|
|
329
|
+
wanted.push({ id, rev: side[id] });
|
|
330
|
+
}
|
|
331
|
+
if (!wanted.length)
|
|
332
|
+
return [];
|
|
333
|
+
const { missing } = await this.options.lookup.ensure(wanted);
|
|
334
|
+
const real = /^[1-9]\d*-[0-9a-f]{8,}/;
|
|
335
|
+
const named = (id) => item.$revs?.$i?.[id] ?? item.$revs?.$o?.[id];
|
|
336
|
+
const dataIds = missing.filter((id) => id.endsWith(":data") && !real.test(splitRev(named(id)).state ?? ""));
|
|
337
|
+
if (!dataIds.length)
|
|
338
|
+
return missing;
|
|
339
|
+
const absent = new Set((await this.options.lookup.refreshToTip(dataIds)).absent ?? []);
|
|
340
|
+
return missing.filter((id) => !(dataIds.includes(id) && absent.has(id)));
|
|
341
|
+
}
|
|
342
|
+
async coveredByNewerState(item, revsOut) {
|
|
343
|
+
const ids = Object.keys(revsOut);
|
|
344
|
+
if (!ids.length)
|
|
345
|
+
return false;
|
|
346
|
+
await this.options.lookup.refreshToTip(ids.filter((id) => !id.includes(":")));
|
|
347
|
+
for (const id of ids) {
|
|
348
|
+
const local = await readDoc(this.data, id);
|
|
349
|
+
if (!local?._rev)
|
|
350
|
+
return false;
|
|
351
|
+
const past = revPosition(local._rev) > revPosition(revsOut[id].rev);
|
|
352
|
+
if (!past && local._rev !== revsOut[id].rev)
|
|
353
|
+
return false;
|
|
354
|
+
}
|
|
355
|
+
return true;
|
|
356
|
+
}
|
|
357
|
+
async snapshot(item, revsOut) {
|
|
358
|
+
const ids = new Set(Object.keys(revsOut));
|
|
359
|
+
for (const side of [item.$revs?.$i, item.$revs?.$o]) {
|
|
360
|
+
for (const id of Object.keys(side || {})) {
|
|
361
|
+
ids.add(id);
|
|
362
|
+
ids.add(`${id}:stream`);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
const out = {};
|
|
366
|
+
for (const id of ids) {
|
|
367
|
+
const doc = await readDoc(this.data, id);
|
|
368
|
+
if (doc)
|
|
369
|
+
out[id] = doc;
|
|
370
|
+
}
|
|
371
|
+
return out;
|
|
372
|
+
}
|
|
373
|
+
async run(entry) {
|
|
374
|
+
if (this.options.setup)
|
|
375
|
+
applyExecutionOptions(this.options.setup());
|
|
376
|
+
if (this.contracts)
|
|
377
|
+
await this.contracts.ensureFor(entry);
|
|
378
|
+
return this.execute(entry, this.data, {});
|
|
379
|
+
}
|
|
380
|
+
async saveLocal(result) {
|
|
381
|
+
const docs = Object.values(clone(result.documents || {})).filter((d) => d && d._id);
|
|
382
|
+
if (!docs.length)
|
|
383
|
+
return [];
|
|
384
|
+
const r = await this.data.bulkDocs(docs, { new_edits: true });
|
|
385
|
+
if (r?.ok !== true)
|
|
386
|
+
ActiveLogger.warn(`Saving local result failed`);
|
|
387
|
+
return docs.map((d) => d._id);
|
|
388
|
+
}
|
|
389
|
+
async mismatched(revsOut, written) {
|
|
390
|
+
const wrong = [];
|
|
391
|
+
for (const id of Object.keys(revsOut)) {
|
|
392
|
+
const stored = await readDoc(this.data, id);
|
|
393
|
+
if (!ActiveProtocol.matchesRevOut(stored, revsOut[id]))
|
|
394
|
+
wrong.push(id);
|
|
395
|
+
}
|
|
396
|
+
for (const id of written)
|
|
397
|
+
if (!revsOut[id])
|
|
398
|
+
wrong.push(id);
|
|
399
|
+
return wrong;
|
|
400
|
+
}
|
|
401
|
+
async summary(result, touched) {
|
|
402
|
+
const outputs = {};
|
|
403
|
+
const $o = {};
|
|
404
|
+
for (const id of touched) {
|
|
405
|
+
const doc = await readDoc(this.data, id);
|
|
406
|
+
if (doc) {
|
|
407
|
+
outputs[id] = { rev: doc._rev, sha256: docHash(doc) };
|
|
408
|
+
$o[id] = doc._rev;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
return {
|
|
412
|
+
result: {
|
|
413
|
+
vote: result.vote,
|
|
414
|
+
committed: result.committed,
|
|
415
|
+
...(result.error ? { error: result.error } : {}),
|
|
416
|
+
supernodeDependent: result.supernodeDependent,
|
|
417
|
+
dependence: result.dependence || [],
|
|
418
|
+
outputs,
|
|
419
|
+
},
|
|
420
|
+
revs: { $i: {}, $o },
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
async rollback(before, touched) {
|
|
424
|
+
for (const id of touched) {
|
|
425
|
+
try {
|
|
426
|
+
if (before[id])
|
|
427
|
+
await writeAtRev(this.data, before[id]);
|
|
428
|
+
else if (await readDoc(this.data, id))
|
|
429
|
+
await this.data.purge({ _id: id });
|
|
430
|
+
}
|
|
431
|
+
catch (e) {
|
|
432
|
+
ActiveLogger.error(e, `Rollback of ${id} failed`);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
async writeDocs(docs, before, touched) {
|
|
437
|
+
let ok = true;
|
|
438
|
+
const ids = Object.keys(docs).sort((a, b) => Number(b.includes(":")) - Number(a.includes(":")));
|
|
439
|
+
for (const id of ids) {
|
|
440
|
+
if (!(await writeAtRev(this.data, clone(docs[id]))))
|
|
441
|
+
ok = false;
|
|
442
|
+
}
|
|
443
|
+
await this.rollback(before, touched.filter((id) => !docs[id]));
|
|
444
|
+
return ok;
|
|
445
|
+
}
|
|
446
|
+
async gatherQuorum(item, ctx) {
|
|
447
|
+
const att = item.attestation;
|
|
448
|
+
const all = [att, ...(Array.isArray(item.attestations) ? item.attestations : [])];
|
|
449
|
+
const agrees = () => {
|
|
450
|
+
const q = quorum(all, item.$revs, ctx.keys, ctx.reachedPercent, ctx.networkSize);
|
|
451
|
+
const same = q.agreed.length > 0 &&
|
|
452
|
+
canonical(q.agreed[0].revsOut) === canonical(att.revsOut) &&
|
|
453
|
+
q.agreed[0].umidDocHash === att.umidDocHash &&
|
|
454
|
+
q.agreed[0].outcome === "committed";
|
|
455
|
+
return { reached: q.reached && same, count: same ? q.count : 0 };
|
|
456
|
+
};
|
|
457
|
+
let q = agrees();
|
|
458
|
+
const asked = this.sources();
|
|
459
|
+
for (const source of [...asked.supernodes, ...(asked.attesters ?? [])]) {
|
|
460
|
+
if (q.reached)
|
|
461
|
+
break;
|
|
462
|
+
try {
|
|
463
|
+
const a = await source.attest(item.umid);
|
|
464
|
+
if (a) {
|
|
465
|
+
all.push(a);
|
|
466
|
+
q = agrees();
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
catch {
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
return q;
|
|
473
|
+
}
|
|
474
|
+
static requests(revsOut, atRev) {
|
|
475
|
+
const reqs = new Map();
|
|
476
|
+
for (const id of Object.keys(revsOut)) {
|
|
477
|
+
const base = baseId(id);
|
|
478
|
+
if (reqs.has(base))
|
|
479
|
+
continue;
|
|
480
|
+
reqs.set(base, atRev && revsOut[base] ? { id: base, rev: revsOut[base].rev } : { id: base });
|
|
481
|
+
}
|
|
482
|
+
return [...reqs.values()];
|
|
483
|
+
}
|
|
484
|
+
static index(docs) {
|
|
485
|
+
const out = new Map();
|
|
486
|
+
for (const d of Array.isArray(docs) ? docs : []) {
|
|
487
|
+
if (d && typeof d._id === "string")
|
|
488
|
+
out.set(d._id, [...(out.get(d._id) ?? []), d]);
|
|
489
|
+
}
|
|
490
|
+
return out;
|
|
491
|
+
}
|
|
492
|
+
async fetchSigned(revsOut) {
|
|
493
|
+
const docs = {};
|
|
494
|
+
const { peers, supernodes } = this.sources();
|
|
495
|
+
for (const source of [...peers, ...supernodes]) {
|
|
496
|
+
const left = Object.keys(revsOut).filter((id) => !docs[id]);
|
|
497
|
+
if (!left.length)
|
|
498
|
+
break;
|
|
499
|
+
const want = {};
|
|
500
|
+
for (const id of left)
|
|
501
|
+
want[id] = revsOut[id];
|
|
502
|
+
try {
|
|
503
|
+
const r = await source.streams(Processor.requests(want, true));
|
|
504
|
+
const byId = Processor.index(r?.docs);
|
|
505
|
+
for (const id of left) {
|
|
506
|
+
const doc = (byId.get(id) ?? []).find((d) => ActiveProtocol.matchesRevOut(d, revsOut[id]));
|
|
507
|
+
if (doc)
|
|
508
|
+
docs[id] = doc;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
catch (e) {
|
|
512
|
+
ActiveLogger.warn(`Fetch of attested outputs from ${source.id} failed: ${errorText(e)}`);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
return { docs, missing: Object.keys(revsOut).filter((id) => !docs[id]) };
|
|
516
|
+
}
|
|
517
|
+
async spi(revsOut, quorumSigned) {
|
|
518
|
+
const docs = {};
|
|
519
|
+
const ids = Object.keys(revsOut);
|
|
520
|
+
const { peers, supernodes } = this.sources();
|
|
521
|
+
const ask = (sources, atRev) => Promise.all(sources.map((s) => s
|
|
522
|
+
.streams(Processor.requests(revsOut, atRev))
|
|
523
|
+
.then((r) => (Array.isArray(r?.docs) ? r.docs : undefined))
|
|
524
|
+
.catch(() => undefined))).then((all) => all.filter((a) => Array.isArray(a)));
|
|
525
|
+
const asked = peers.slice(0, this.options.spiPeers ?? 5);
|
|
526
|
+
if (asked.length) {
|
|
527
|
+
const answers = (await ask(asked, true)).map((a) => Processor.index(a));
|
|
528
|
+
for (const id of ids) {
|
|
529
|
+
const matching = answers.filter((a) => (a.get(id) ?? []).some((d) => ActiveProtocol.matchesRevOut(d, revsOut[id])));
|
|
530
|
+
if (matching.length && (quorumSigned || matching.length > answers.length / 2)) {
|
|
531
|
+
docs[id] = matching[0].get(id).find((d) => ActiveProtocol.matchesRevOut(d, revsOut[id]));
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
let left = ids.filter((id) => !docs[id]);
|
|
536
|
+
if (left.length && supernodes.length) {
|
|
537
|
+
const answers = await ask(supernodes, false);
|
|
538
|
+
const indexed = answers.map((a) => Processor.index(a));
|
|
539
|
+
if (quorumSigned) {
|
|
540
|
+
for (const id of left) {
|
|
541
|
+
for (const a of indexed) {
|
|
542
|
+
const d = (a.get(id) ?? []).find((x) => ActiveProtocol.matchesRevOut(x, revsOut[id]));
|
|
543
|
+
if (d) {
|
|
544
|
+
docs[id] = d;
|
|
545
|
+
break;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
left = left.filter((id) => !docs[id]);
|
|
550
|
+
}
|
|
551
|
+
if (left.length && answers.length) {
|
|
552
|
+
const { winners } = spiConsensus(answers, Math.floor(answers.length / 2) + 1);
|
|
553
|
+
for (const id of left) {
|
|
554
|
+
const w = winners[id];
|
|
555
|
+
if (!w)
|
|
556
|
+
continue;
|
|
557
|
+
if (w.rev === revsOut[id].rev) {
|
|
558
|
+
for (const a of indexed) {
|
|
559
|
+
const d = (a.get(id) ?? []).find((x) => ActiveProtocol.matchesRevOut(x, revsOut[id]));
|
|
560
|
+
if (d) {
|
|
561
|
+
docs[id] = d;
|
|
562
|
+
break;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
else if (revPosition(w.rev) > revPosition(revsOut[id].rev)) {
|
|
567
|
+
docs[id] = w.doc;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
return { docs, missing: ids.filter((id) => !docs[id]) };
|
|
573
|
+
}
|
|
574
|
+
async diverge(item, kind, tallied, localResult, localRevs) {
|
|
575
|
+
const record = {
|
|
576
|
+
umid: item.umid,
|
|
577
|
+
kind,
|
|
578
|
+
supernodeTally: tallied,
|
|
579
|
+
localResult,
|
|
580
|
+
$revs: item.$revs,
|
|
581
|
+
...(item.attestation?.revsOut ? { revsOut: item.attestation.revsOut } : {}),
|
|
582
|
+
...(localRevs ? { localRevs } : {}),
|
|
583
|
+
at: (this.options.now ? this.options.now() : new Date()).toISOString(),
|
|
584
|
+
};
|
|
585
|
+
try {
|
|
586
|
+
await writeDivergence(this.options.store, record);
|
|
587
|
+
}
|
|
588
|
+
catch (e) {
|
|
589
|
+
ActiveLogger.error(e, `Divergence record for ${item.umid} not written`);
|
|
590
|
+
}
|
|
591
|
+
await this.emit({
|
|
592
|
+
type: "divergence",
|
|
593
|
+
umid: item.umid,
|
|
594
|
+
contract: entryOf(item)?.$tx?.$contract,
|
|
595
|
+
payload: { kind },
|
|
596
|
+
});
|
|
597
|
+
return record;
|
|
598
|
+
}
|
|
599
|
+
async emitContractEvents(item, events) {
|
|
600
|
+
for (const ev of events) {
|
|
601
|
+
if (!ev || typeof ev.name !== "string")
|
|
602
|
+
continue;
|
|
603
|
+
await this.emit({
|
|
604
|
+
type: ev.name,
|
|
605
|
+
contract: ev.contract ?? entryOf(item)?.$tx?.$contract,
|
|
606
|
+
umid: item.umid,
|
|
607
|
+
payload: ev.data && typeof ev.data === "object" && !Array.isArray(ev.data)
|
|
608
|
+
? ev.data
|
|
609
|
+
: { data: ev.data },
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
async emit(event) {
|
|
614
|
+
try {
|
|
615
|
+
await this.options.events.emit(event);
|
|
616
|
+
}
|
|
617
|
+
catch (e) {
|
|
618
|
+
ActiveLogger.error(e, `Event ${event.type} for ${event.umid} not emitted`);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
const executionParts = (umidDoc) => ({
|
|
623
|
+
streams: umidDoc?.streams,
|
|
624
|
+
events: Array.isArray(umidDoc?.events)
|
|
625
|
+
? umidDoc.events.map((e) => {
|
|
626
|
+
if (!e || typeof e !== "object")
|
|
627
|
+
return e;
|
|
628
|
+
const { _id, ...rest } = e;
|
|
629
|
+
return rest;
|
|
630
|
+
})
|
|
631
|
+
: umidDoc?.events,
|
|
632
|
+
});
|
|
633
|
+
const entryOf = (item) => item?.umidDoc?.umid ?? item?.tx;
|
|
634
|
+
function outcome(umid, action, vote, reasons) {
|
|
635
|
+
return { umid, action, vote, warnings: [], reasons };
|
|
636
|
+
}
|
|
637
|
+
function transient(umid, reasons) {
|
|
638
|
+
return { ...outcome(umid, "dropped", "no", reasons), retry: true };
|
|
639
|
+
}
|
|
640
|
+
//# sourceMappingURL=processor.js.map
|