@bsv/overlay 0.1.12 → 0.1.13
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/dist/cjs/package.json +2 -2
- package/dist/cjs/src/Engine.js +33 -410
- package/dist/cjs/src/Engine.js.map +1 -1
- package/dist/cjs/src/GASP/OverlayGASPRemote.js +6 -4
- package/dist/cjs/src/GASP/OverlayGASPRemote.js.map +1 -1
- package/dist/cjs/src/GASP/OverlayGASPStorage.js +152 -77
- package/dist/cjs/src/GASP/OverlayGASPStorage.js.map +1 -1
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/src/Engine.js +33 -410
- package/dist/esm/src/Engine.js.map +1 -1
- package/dist/esm/src/GASP/OverlayGASPRemote.js +7 -4
- package/dist/esm/src/GASP/OverlayGASPRemote.js.map +1 -1
- package/dist/esm/src/GASP/OverlayGASPStorage.js +151 -77
- package/dist/esm/src/GASP/OverlayGASPStorage.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/types/src/Engine.d.ts +6 -118
- package/dist/types/src/Engine.d.ts.map +1 -1
- package/dist/types/src/GASP/OverlayGASPRemote.d.ts +3 -2
- package/dist/types/src/GASP/OverlayGASPRemote.d.ts.map +1 -1
- package/dist/types/src/GASP/OverlayGASPStorage.d.ts +29 -3
- package/dist/types/src/GASP/OverlayGASPStorage.d.ts.map +1 -1
- package/dist/types/tsconfig.types.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/Engine.ts +12 -8
- package/dist/cjs/src/SHIPAdvertisement.js +0 -3
- package/dist/cjs/src/SHIPAdvertisement.js.map +0 -1
- package/dist/cjs/src/SLAPAdvertisement.js +0 -3
- package/dist/cjs/src/SLAPAdvertisement.js.map +0 -1
- package/dist/esm/src/SHIPAdvertisement.js +0 -2
- package/dist/esm/src/SHIPAdvertisement.js.map +0 -1
- package/dist/esm/src/SLAPAdvertisement.js +0 -2
- package/dist/esm/src/SLAPAdvertisement.js.map +0 -1
- package/dist/types/src/SHIPAdvertisement.d.ts +0 -9
- package/dist/types/src/SHIPAdvertisement.d.ts.map +0 -1
- package/dist/types/src/SLAPAdvertisement.d.ts +0 -9
- package/dist/types/src/SLAPAdvertisement.d.ts.map +0 -1
package/dist/cjs/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bsv/overlay",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.13",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"description": "BSV Blockchain Overlay Services Engine",
|
|
6
6
|
"files": [
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"@bsv/gasp": "^0.1.2",
|
|
51
|
-
"@bsv/sdk": "^1.1.
|
|
51
|
+
"@bsv/sdk": "^1.1.24",
|
|
52
52
|
"knex": "^3.1.0"
|
|
53
53
|
}
|
|
54
54
|
}
|
package/dist/cjs/src/Engine.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.Engine = void 0;
|
|
4
4
|
const sdk_1 = require("@bsv/sdk");
|
|
5
5
|
const gasp_1 = require("@bsv/gasp");
|
|
6
|
+
const OverlayGASPRemote_js_1 = require("./GASP/OverlayGASPRemote.js");
|
|
7
|
+
const OverlayGASPStorage_js_1 = require("./GASP/OverlayGASPStorage.js");
|
|
6
8
|
/**
|
|
7
9
|
* Am engine for running BSV Overlay Services (topic managers and lookup services).
|
|
8
10
|
*/
|
|
@@ -12,7 +14,7 @@ class Engine {
|
|
|
12
14
|
* @param {[key: string]: TopicManager} managers - manages topic admittance
|
|
13
15
|
* @param {[key: string]: LookupService} lookupServices - manages UTXO lookups
|
|
14
16
|
* @param {Storage} storage - for interacting with internally-managed persistent data
|
|
15
|
-
* @param {ChainTracker} chainTracker - Verifies SPV data associated with transactions
|
|
17
|
+
* @param {ChainTracker | 'scripts only'} chainTracker - Verifies SPV data associated with transactions
|
|
16
18
|
* @param {string} [hostingURL] - The URL this engine is hosted at. Required if going to support peer-discovery with an advertiser.
|
|
17
19
|
* @param {Broadcaster} [Broadcaster] - broadcaster used for broadcasting the incoming transaction
|
|
18
20
|
* @param {Advertiser} [Advertiser] - handles SHIP and SLAP advertisements for peer-discovery
|
|
@@ -21,8 +23,9 @@ class Engine {
|
|
|
21
23
|
* @param {SyncConfiguration} syncConfiguration — Configuration object describing historical synchronization of topics.
|
|
22
24
|
* @param {boolean} logTime - Enables / disables the timing logs for various operations in the Overlay submit route.
|
|
23
25
|
* @param {string} logPrefix - Supports overriding the log prefix with a custom string.
|
|
26
|
+
* @param {boolean} throwOnBroadcastFailure - Enables / disables throwing an error when a transaction broadcast failure is detected.
|
|
24
27
|
*/
|
|
25
|
-
constructor(managers, lookupServices, storage, chainTracker, hostingURL, shipTrackers, slapTrackers, broadcaster, advertiser, syncConfiguration, logTime = false, logPrefix = '[OVERLAY_ENGINE] ') {
|
|
28
|
+
constructor(managers, lookupServices, storage, chainTracker, hostingURL, shipTrackers, slapTrackers, broadcaster, advertiser, syncConfiguration, logTime = false, logPrefix = '[OVERLAY_ENGINE] ', throwOnBroadcastFailure = false) {
|
|
26
29
|
this.managers = managers;
|
|
27
30
|
this.lookupServices = lookupServices;
|
|
28
31
|
this.storage = storage;
|
|
@@ -35,6 +38,7 @@ class Engine {
|
|
|
35
38
|
this.syncConfiguration = syncConfiguration;
|
|
36
39
|
this.logTime = logTime;
|
|
37
40
|
this.logPrefix = logPrefix;
|
|
41
|
+
this.throwOnBroadcastFailure = throwOnBroadcastFailure;
|
|
38
42
|
// To encourage synchronization of overlay services, the SHIP sync strategy is used by default for all overlay topics, except for 'tm_ship' and 'tm_slap'.
|
|
39
43
|
// For these two topics, any existing trackers are combined with the provided shipTrackers and slapTrackers omitting any duplicates.
|
|
40
44
|
if (syncConfiguration === undefined) {
|
|
@@ -84,7 +88,6 @@ class Engine {
|
|
|
84
88
|
* @param {TaggedBEEF} taggedBEEF - The transaction to process
|
|
85
89
|
* @param {function(STEAK): void} [onSTEAKReady] - Optional callback function invoked when the STEAK is ready.
|
|
86
90
|
* @param {string} mode — Indicates the submission behavior, whether historical or current. Historical transactions are not broadcast or propagated.
|
|
87
|
-
* @param {boolean} log
|
|
88
91
|
*
|
|
89
92
|
* The optional callback function should be used to get STEAK when ready, and avoid waiting for broadcast and transaction propagation to complete.
|
|
90
93
|
*
|
|
@@ -103,11 +106,11 @@ class Engine {
|
|
|
103
106
|
this.startTime(`submit_${txid}`);
|
|
104
107
|
this.startTime(`chainTracker_${txid.substring(0, 10)}`);
|
|
105
108
|
const txValid = await tx.verify(this.chainTracker);
|
|
106
|
-
this.endTime(`chainTracker_${txid.substring(0, 10)}`);
|
|
107
109
|
if (!txValid)
|
|
108
110
|
throw new Error('Unable to verify SPV information.');
|
|
111
|
+
this.endTime(`chainTracker_${txid.substring(0, 10)}`);
|
|
109
112
|
const steak = {};
|
|
110
|
-
let
|
|
113
|
+
let admissibleOutputs = { outputsToAdmit: [], coinsToRetain: [] };
|
|
111
114
|
const previousCoins = [];
|
|
112
115
|
// Parallelize the topic processing
|
|
113
116
|
const topicPromises = taggedBEEF.topics.map(async (topic) => {
|
|
@@ -123,20 +126,22 @@ class Engine {
|
|
|
123
126
|
return;
|
|
124
127
|
}
|
|
125
128
|
// Check if any input of this transaction is a previous UTXO
|
|
126
|
-
const outputPromises = tx.inputs.map(input => {
|
|
129
|
+
const outputPromises = tx.inputs.map(async (input, i) => {
|
|
127
130
|
var _a;
|
|
128
|
-
const previousTXID = input.sourceTXID
|
|
129
|
-
|
|
131
|
+
const previousTXID = input.sourceTXID !== undefined ? input.sourceTXID : (_a = input.sourceTransaction) === null || _a === void 0 ? void 0 : _a.id('hex');
|
|
132
|
+
if (previousTXID !== undefined) {
|
|
133
|
+
const output = this.storage.findOutput(previousTXID, input.sourceOutputIndex, topic);
|
|
134
|
+
if (output !== undefined && output !== null) {
|
|
135
|
+
previousCoins.push(i);
|
|
136
|
+
return await Promise.resolve(output);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return await Promise.resolve(null);
|
|
130
140
|
});
|
|
131
141
|
this.startTime(`previousOutputQuery_${txid.substring(0, 10)}`);
|
|
132
142
|
const outputs = await Promise.all(outputPromises);
|
|
133
143
|
this.endTime(`previousOutputQuery_${txid.substring(0, 10)}`);
|
|
134
|
-
outputs.
|
|
135
|
-
if (output !== undefined && output !== null) {
|
|
136
|
-
previousCoins.push(i);
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
const markSpentPromises = outputs.map(async (output, i) => {
|
|
144
|
+
const markSpentPromises = outputs.map(async (output) => {
|
|
140
145
|
if (output !== undefined && output !== null) {
|
|
141
146
|
try {
|
|
142
147
|
await this.storage.markUTXOAsSpent(output.txid, output.outputIndex, topic);
|
|
@@ -159,7 +164,7 @@ class Engine {
|
|
|
159
164
|
const admissibleOutputsPromise = (async () => {
|
|
160
165
|
try {
|
|
161
166
|
this.startTime(`identifyAdmissibleOutputs_${txid.substring(0, 10)}`);
|
|
162
|
-
|
|
167
|
+
admissibleOutputs = await this.managers[topic].identifyAdmissibleOutputs(taggedBEEF.beef, previousCoins);
|
|
163
168
|
this.endTime(`identifyAdmissibleOutputs_${txid.substring(0, 10)}`);
|
|
164
169
|
}
|
|
165
170
|
catch (_) {
|
|
@@ -169,15 +174,17 @@ class Engine {
|
|
|
169
174
|
// Wait for both tasks to complete
|
|
170
175
|
await Promise.all([markSpentPromises, admissibleOutputsPromise]);
|
|
171
176
|
// Keep track of what outputs were admitted for what topic
|
|
172
|
-
steak[topic] =
|
|
177
|
+
steak[topic] = admissibleOutputs;
|
|
173
178
|
});
|
|
174
179
|
await Promise.all(topicPromises);
|
|
175
180
|
// Broadcast the transaction if not historical and broadcaster is configured
|
|
176
181
|
this.startTime(`broadcast_${txid.substring(0, 10)}`);
|
|
177
182
|
if (mode !== 'historical-tx' && this.broadcaster !== undefined) {
|
|
178
183
|
const response = await this.broadcaster.broadcast(tx);
|
|
179
|
-
if ((0, sdk_1.isBroadcastFailure)(response)) {
|
|
180
|
-
|
|
184
|
+
if ((0, sdk_1.isBroadcastFailure)(response) && this.throwOnBroadcastFailure) {
|
|
185
|
+
const e = new Error(`Failed to broadcast transaction! Error: ${response.description}`);
|
|
186
|
+
e.more = response.more;
|
|
187
|
+
throw e;
|
|
181
188
|
}
|
|
182
189
|
}
|
|
183
190
|
this.endTime(`broadcast_${txid.substring(0, 10)}`);
|
|
@@ -188,7 +195,7 @@ class Engine {
|
|
|
188
195
|
}
|
|
189
196
|
for (const topic of taggedBEEF.topics) {
|
|
190
197
|
// Keep track of which outputs to admit, mark as stale, or retain
|
|
191
|
-
const outputsToAdmit =
|
|
198
|
+
const outputsToAdmit = admissibleOutputs.outputsToAdmit;
|
|
192
199
|
const staleCoins = [];
|
|
193
200
|
const outputsConsumed = [];
|
|
194
201
|
// Find which outputs should not be retained and mark them as stale
|
|
@@ -196,7 +203,7 @@ class Engine {
|
|
|
196
203
|
for (const inputIndex of previousCoins) {
|
|
197
204
|
const previousTXID = tx.inputs[inputIndex].sourceTXID || ((_a = tx.inputs[inputIndex].sourceTransaction) === null || _a === void 0 ? void 0 : _a.id('hex'));
|
|
198
205
|
const previousOutputIndex = tx.inputs[inputIndex].sourceOutputIndex;
|
|
199
|
-
if (!
|
|
206
|
+
if (!admissibleOutputs.coinsToRetain.includes(inputIndex)) {
|
|
200
207
|
staleCoins.push({
|
|
201
208
|
txid: previousTXID,
|
|
202
209
|
outputIndex: previousOutputIndex
|
|
@@ -236,7 +243,7 @@ class Engine {
|
|
|
236
243
|
this.endTime(`insertNewOutput_${txid.substring(0, 10)}`);
|
|
237
244
|
newUTXOs.push({ txid, outputIndex });
|
|
238
245
|
this.startTime(`notifyLookupService${txid.substring(0, 10)}`);
|
|
239
|
-
await Promise.all(Object.values(this.lookupServices).map(l => { var _a; return (_a = l.outputAdded) === null || _a === void 0 ? void 0 : _a.call(l, txid, outputIndex, tx.outputs[outputIndex].lockingScript, topic); }));
|
|
246
|
+
await Promise.all(Object.values(this.lookupServices).map(async (l) => { var _a; return await ((_a = l.outputAdded) === null || _a === void 0 ? void 0 : _a.call(l, txid, outputIndex, tx.outputs[outputIndex].lockingScript, topic)); }));
|
|
240
247
|
this.endTime(`notifyLookupService${txid.substring(0, 10)}`);
|
|
241
248
|
}));
|
|
242
249
|
this.startTime(`outputConsumed_${txid.substring(0, 10)}`);
|
|
@@ -260,10 +267,10 @@ class Engine {
|
|
|
260
267
|
if (this.advertiser === undefined || mode === 'historical-tx') {
|
|
261
268
|
return steak;
|
|
262
269
|
}
|
|
263
|
-
this.startTime(`
|
|
270
|
+
this.startTime(`transactionPropagation_${txid.substring(0, 10)}`);
|
|
264
271
|
// Propagate transaction to other nodes according to synchronization agreements
|
|
265
272
|
// 1. Find nodes that host the topics associated with admissable outputs
|
|
266
|
-
// We want to figure out which topics we actually care about(because their associated outputs were admitted)
|
|
273
|
+
// We want to figure out which topics we actually care about (because their associated outputs were admitted)
|
|
267
274
|
// AND if the topic was not admitted we want to remove it from the list of topics we care about.
|
|
268
275
|
const relevantTopics = taggedBEEF.topics.filter(topic => steak[topic] !== undefined && steak[topic].outputsToAdmit.length !== 0);
|
|
269
276
|
// TODO: Cache ship/slap lookup with expiry (every 5min)
|
|
@@ -518,7 +525,7 @@ class Engine {
|
|
|
518
525
|
if (Array.isArray(syncEndpoints)) {
|
|
519
526
|
await Promise.all(syncEndpoints.map(async (endpoint) => {
|
|
520
527
|
// Sync to each host that is associated with this topic
|
|
521
|
-
const gasp = new gasp_1.GASP(new OverlayGASPStorage(topic, this), new OverlayGASPRemote(endpoint, topic), 0, `[GASP Sync of ${topic} with ${endpoint}]`, true);
|
|
528
|
+
const gasp = new gasp_1.GASP(new OverlayGASPStorage_js_1.OverlayGASPStorage(topic, this), new OverlayGASPRemote_js_1.OverlayGASPRemote(endpoint, topic), 0, `[GASP Sync of ${topic} with ${endpoint}]`, true);
|
|
522
529
|
await gasp.sync();
|
|
523
530
|
}));
|
|
524
531
|
}
|
|
@@ -864,389 +871,5 @@ There is currently a bug with the test runner that prevents importing and using
|
|
|
864
871
|
Thus, all non-type exports have been moved to Engine.
|
|
865
872
|
*/
|
|
866
873
|
// TODO: fix bug with imports that break tests. -----[GASP/OverlayGASPRemote.ts]-----
|
|
867
|
-
|
|
868
|
-
constructor(endpointURL, topic) {
|
|
869
|
-
this.endpointURL = endpointURL;
|
|
870
|
-
this.topic = topic;
|
|
871
|
-
}
|
|
872
|
-
/**
|
|
873
|
-
* Given an outgoing initial request, sends the request to the foreign instance and obtains their initial response.
|
|
874
|
-
* @param request
|
|
875
|
-
* @returns
|
|
876
|
-
*/
|
|
877
|
-
async getInitialResponse(request) {
|
|
878
|
-
// Send out an HTTP request to the URL (current host for topic)
|
|
879
|
-
// Include the topic in the request
|
|
880
|
-
// Parse out response and return correct format
|
|
881
|
-
const url = `${this.endpointURL}/requestSyncResponse`;
|
|
882
|
-
const response = await fetch(url, {
|
|
883
|
-
method: 'POST',
|
|
884
|
-
headers: {
|
|
885
|
-
'Content-Type': 'application/json',
|
|
886
|
-
'X-BSV-Topic': this.topic
|
|
887
|
-
},
|
|
888
|
-
body: JSON.stringify(request)
|
|
889
|
-
});
|
|
890
|
-
if (!response.ok) {
|
|
891
|
-
throw new Error(`HTTP error! Status: ${response.status}`);
|
|
892
|
-
}
|
|
893
|
-
const result = await response.json();
|
|
894
|
-
// Validate and return the response in the correct format
|
|
895
|
-
if (!Array.isArray(result.UTXOList) || typeof result.since !== 'number') {
|
|
896
|
-
throw new Error('Invalid response format');
|
|
897
|
-
}
|
|
898
|
-
return {
|
|
899
|
-
UTXOList: result.UTXOList.map((utxo) => ({
|
|
900
|
-
txid: utxo.txid,
|
|
901
|
-
outputIndex: utxo.outputIndex
|
|
902
|
-
})),
|
|
903
|
-
since: result.since
|
|
904
|
-
};
|
|
905
|
-
}
|
|
906
|
-
/**
|
|
907
|
-
* Given an outgoing txid, outputIndex and optional metadata, request the associated GASP node from the foreign instance.
|
|
908
|
-
* @param graphID
|
|
909
|
-
* @param txid
|
|
910
|
-
* @param outputIndex
|
|
911
|
-
* @param metadata
|
|
912
|
-
* @returns
|
|
913
|
-
*/
|
|
914
|
-
async requestNode(graphID, txid, outputIndex, metadata) {
|
|
915
|
-
// Send an HTTP request with the provided info and get back a gaspNode
|
|
916
|
-
const url = `${this.endpointURL}/requestForeignGASPNode`;
|
|
917
|
-
const body = {
|
|
918
|
-
graphID,
|
|
919
|
-
txid,
|
|
920
|
-
outputIndex,
|
|
921
|
-
metadata
|
|
922
|
-
};
|
|
923
|
-
const response = await fetch(url, {
|
|
924
|
-
method: 'POST',
|
|
925
|
-
headers: {
|
|
926
|
-
'Content-Type': 'application/json'
|
|
927
|
-
},
|
|
928
|
-
body: JSON.stringify(body)
|
|
929
|
-
});
|
|
930
|
-
if (!response.ok) {
|
|
931
|
-
throw new Error(`HTTP error! Status: ${response.status}`);
|
|
932
|
-
}
|
|
933
|
-
const result = await response.json();
|
|
934
|
-
// Validate and return the response in the correct format
|
|
935
|
-
if (typeof result.graphID !== 'string' || typeof result.rawTx !== 'string' || typeof result.outputIndex !== 'number') {
|
|
936
|
-
throw new Error('Invalid response format');
|
|
937
|
-
}
|
|
938
|
-
const gaspNode = {
|
|
939
|
-
graphID: result.graphID,
|
|
940
|
-
rawTx: result.rawTx,
|
|
941
|
-
outputIndex: result.outputIndex,
|
|
942
|
-
proof: result.proof,
|
|
943
|
-
txMetadata: result.txMetadata,
|
|
944
|
-
outputMetadata: result.outputMetadata,
|
|
945
|
-
inputs: result.inputs
|
|
946
|
-
};
|
|
947
|
-
return gaspNode;
|
|
948
|
-
}
|
|
949
|
-
// ---- Now optional methods ----
|
|
950
|
-
// When are only syncing to them
|
|
951
|
-
async getInitialReply(response) {
|
|
952
|
-
throw new Error('Function not supported!');
|
|
953
|
-
}
|
|
954
|
-
// Only used when supporting bidirectional sync.
|
|
955
|
-
// Overlay services does not support this.
|
|
956
|
-
async submitNode(node) {
|
|
957
|
-
throw new Error('Node submission not supported!');
|
|
958
|
-
}
|
|
959
|
-
}
|
|
960
|
-
exports.OverlayGASPRemote = OverlayGASPRemote;
|
|
961
|
-
class OverlayGASPStorage {
|
|
962
|
-
constructor(topic, engine, maxNodesInGraph) {
|
|
963
|
-
this.topic = topic;
|
|
964
|
-
this.engine = engine;
|
|
965
|
-
this.maxNodesInGraph = maxNodesInGraph;
|
|
966
|
-
this.temporaryGraphNodeRefs = {};
|
|
967
|
-
}
|
|
968
|
-
/**
|
|
969
|
-
*
|
|
970
|
-
* @param since
|
|
971
|
-
* @returns
|
|
972
|
-
*/
|
|
973
|
-
async findKnownUTXOs(since) {
|
|
974
|
-
const UTXOs = await this.engine.storage.findUTXOsForTopic(this.topic, since);
|
|
975
|
-
return UTXOs.map(output => ({
|
|
976
|
-
txid: output.txid,
|
|
977
|
-
outputIndex: output.outputIndex
|
|
978
|
-
}));
|
|
979
|
-
}
|
|
980
|
-
/**
|
|
981
|
-
* For a given txid and output index, returns the associated transaction, a merkle proof if the transaction is in a block, and metadata if if requested. If no metadata is requested, metadata hashes on inputs are not returned.
|
|
982
|
-
* @param graphID
|
|
983
|
-
* @param txid
|
|
984
|
-
* @param outputIndex
|
|
985
|
-
* @param metadata
|
|
986
|
-
* @returns
|
|
987
|
-
*/
|
|
988
|
-
async hydrateGASPNode(graphID, txid, outputIndex, metadata) {
|
|
989
|
-
const output = await this.engine.storage.findOutput(txid, outputIndex, undefined, undefined, true);
|
|
990
|
-
if ((output === null || output === void 0 ? void 0 : output.beef) === undefined) {
|
|
991
|
-
throw new Error('No matching output found!');
|
|
992
|
-
}
|
|
993
|
-
const tx = sdk_1.Transaction.fromBEEF(output.beef);
|
|
994
|
-
const rawTx = tx.toHex();
|
|
995
|
-
const node = {
|
|
996
|
-
rawTx,
|
|
997
|
-
graphID,
|
|
998
|
-
outputIndex
|
|
999
|
-
};
|
|
1000
|
-
if (tx.merklePath !== undefined) {
|
|
1001
|
-
node.proof = tx.merklePath.toHex();
|
|
1002
|
-
}
|
|
1003
|
-
return node;
|
|
1004
|
-
}
|
|
1005
|
-
/**
|
|
1006
|
-
* For a given node, returns the inputs needed to complete the graph, including whether updated metadata is requested for those inputs.
|
|
1007
|
-
* @param tx The node for which needed inputs should be found.
|
|
1008
|
-
* @returns A promise for a mapping of requested input transactions and whether metadata should be provided for each.
|
|
1009
|
-
*/
|
|
1010
|
-
async findNeededInputs(tx) {
|
|
1011
|
-
var _a, _b, _c;
|
|
1012
|
-
// If there is no Merkle proof, we always need the inputs
|
|
1013
|
-
const response = {
|
|
1014
|
-
requestedInputs: {}
|
|
1015
|
-
};
|
|
1016
|
-
const parsedTx = sdk_1.Transaction.fromHex(tx.rawTx);
|
|
1017
|
-
if (tx.proof === undefined) {
|
|
1018
|
-
for (const input of parsedTx.inputs) {
|
|
1019
|
-
response.requestedInputs[`${input.sourceTXID}.${input.sourceOutputIndex}`] = {
|
|
1020
|
-
metadata: false
|
|
1021
|
-
};
|
|
1022
|
-
}
|
|
1023
|
-
return await this.stripAlreadyKnownInputs(response);
|
|
1024
|
-
}
|
|
1025
|
-
// Attempt to check if the current transaction is admissible
|
|
1026
|
-
parsedTx.merklePath = sdk_1.MerklePath.fromHex(tx.proof);
|
|
1027
|
-
const admittanceResult = await this.engine.managers[this.topic].identifyAdmissibleOutputs(parsedTx.toBEEF(), []);
|
|
1028
|
-
if (admittanceResult.outputsToAdmit.includes(tx.outputIndex)) {
|
|
1029
|
-
// The transaction is admissible, no further inputs are needed
|
|
1030
|
-
}
|
|
1031
|
-
else {
|
|
1032
|
-
// The transaction is not admissible, get inputs needed for further verification
|
|
1033
|
-
// TopicManagers should implement a function to identify which inputs are needed.
|
|
1034
|
-
if (this.engine.managers[this.topic] !== undefined && typeof this.engine.managers[this.topic].identifyNeededInputs === 'function') {
|
|
1035
|
-
try {
|
|
1036
|
-
const neededInputs = (_c = await ((_b = (_a = this.engine.managers[this.topic]).identifyNeededInputs) === null || _b === void 0 ? void 0 : _b.call(_a, parsedTx.toBEEF()))) !== null && _c !== void 0 ? _c : [];
|
|
1037
|
-
for (const input of neededInputs) {
|
|
1038
|
-
response.requestedInputs[`${input.txid}.${input.outputIndex}`] = {
|
|
1039
|
-
metadata: false
|
|
1040
|
-
};
|
|
1041
|
-
}
|
|
1042
|
-
return await this.stripAlreadyKnownInputs(response);
|
|
1043
|
-
}
|
|
1044
|
-
catch (e) {
|
|
1045
|
-
console.error(`An error occurred when identifying needed inputs for transaction: ${parsedTx.id('hex')}.${tx.outputIndex}!`);
|
|
1046
|
-
// Cut off the graph in case of an error here.
|
|
1047
|
-
}
|
|
1048
|
-
}
|
|
1049
|
-
// By default, if the topic manager isn't able to stipulate needed inputs, only the inputs necessary for SPV are requested.
|
|
1050
|
-
}
|
|
1051
|
-
// Everything else falls through to returning undefined/void, which will terminate the synchronization at this point.
|
|
1052
|
-
}
|
|
1053
|
-
/**
|
|
1054
|
-
* Ensures that no inputs are requested from foreign nodes before sending any GASP response
|
|
1055
|
-
* Also terminates graphs if the response would be empty.
|
|
1056
|
-
*/
|
|
1057
|
-
async stripAlreadyKnownInputs(response) {
|
|
1058
|
-
if (typeof response === 'undefined') {
|
|
1059
|
-
return response;
|
|
1060
|
-
}
|
|
1061
|
-
for (const inputNodeId of Object.keys(response.requestedInputs)) {
|
|
1062
|
-
const [txid, outputIndex] = inputNodeId.split('.');
|
|
1063
|
-
const found = await this.engine.storage.findOutput(txid, Number(outputIndex), this.topic);
|
|
1064
|
-
if (found !== null && found !== undefined) {
|
|
1065
|
-
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
1066
|
-
delete response.requestedInputs[inputNodeId];
|
|
1067
|
-
}
|
|
1068
|
-
}
|
|
1069
|
-
if (Object.keys(response.requestedInputs).length === 0) {
|
|
1070
|
-
return undefined;
|
|
1071
|
-
}
|
|
1072
|
-
return response;
|
|
1073
|
-
}
|
|
1074
|
-
/**
|
|
1075
|
-
* Appends a new node to a temporary graph.
|
|
1076
|
-
* @param tx The node to append to this graph.
|
|
1077
|
-
* @param spentBy Unless this is the same node identified by the graph ID, denotes the TXID and input index for the node which spent this one, in 36-byte format.
|
|
1078
|
-
* @throws If the node cannot be appended to the graph, either because the graph ID is for a graph the recipient does not want or because the graph has grown to be too large before being finalized.
|
|
1079
|
-
*/
|
|
1080
|
-
async appendToGraph(tx, spentBy) {
|
|
1081
|
-
if (this.maxNodesInGraph !== undefined && Object.keys(this.temporaryGraphNodeRefs).length >= this.maxNodesInGraph) {
|
|
1082
|
-
throw new Error('The max number of nodes in transaction graph has been reached!');
|
|
1083
|
-
}
|
|
1084
|
-
const parsedTx = sdk_1.Transaction.fromHex(tx.rawTx);
|
|
1085
|
-
const txid = parsedTx.id('hex');
|
|
1086
|
-
if (tx.proof !== undefined) {
|
|
1087
|
-
parsedTx.merklePath = sdk_1.MerklePath.fromHex(tx.proof);
|
|
1088
|
-
}
|
|
1089
|
-
// Given the passed in node, append to the temp graph
|
|
1090
|
-
// Use the spentBy param which should be a txid.inputIndex for the node which spent this one in 36-byte format
|
|
1091
|
-
const newGraphNode = {
|
|
1092
|
-
txid,
|
|
1093
|
-
graphID: tx.graphID,
|
|
1094
|
-
rawTx: tx.rawTx,
|
|
1095
|
-
outputIndex: tx.outputIndex,
|
|
1096
|
-
proof: tx.proof,
|
|
1097
|
-
txMetadata: tx.txMetadata,
|
|
1098
|
-
outputMetadata: tx.outputMetadata,
|
|
1099
|
-
inputs: tx.inputs,
|
|
1100
|
-
children: []
|
|
1101
|
-
};
|
|
1102
|
-
// If spentBy is undefined, then we know it's the root node.
|
|
1103
|
-
if (spentBy === undefined) {
|
|
1104
|
-
this.temporaryGraphNodeRefs[tx.graphID] = newGraphNode;
|
|
1105
|
-
}
|
|
1106
|
-
else {
|
|
1107
|
-
// Find the parent node based on spentBy
|
|
1108
|
-
const parentNode = this.temporaryGraphNodeRefs[spentBy];
|
|
1109
|
-
if (parentNode !== undefined) {
|
|
1110
|
-
// Set parent-child relationship
|
|
1111
|
-
parentNode.children.push(newGraphNode);
|
|
1112
|
-
newGraphNode.parent = parentNode;
|
|
1113
|
-
this.temporaryGraphNodeRefs[`${newGraphNode.txid}.${newGraphNode.outputIndex}`] = newGraphNode;
|
|
1114
|
-
}
|
|
1115
|
-
else {
|
|
1116
|
-
throw new Error(`Parent node with GraphID ${spentBy} not found`);
|
|
1117
|
-
}
|
|
1118
|
-
}
|
|
1119
|
-
}
|
|
1120
|
-
/**
|
|
1121
|
-
* Checks whether the given graph, in its current state, makes reference only to transactions that are proven in the blockchain, or already known by the recipient to be valid.
|
|
1122
|
-
* Additionally, in a breadth-first manner (ensuring that all inputs for any given node are processed before nodes that spend them), it ensures that the root node remains valid according to the rules of the overlay's topic manager,
|
|
1123
|
-
* while considering any coins which the Manager had previously indicated were either valid or invalid.
|
|
1124
|
-
* @param graphID The TXID and output index (in 36-byte format) for the UTXO at the tip of this graph.
|
|
1125
|
-
* @throws If the graph is not well-anchored, according to the rules of Bitcoin or the rules of the Overlay Topic Manager.
|
|
1126
|
-
*/
|
|
1127
|
-
async validateGraphAnchor(graphID) {
|
|
1128
|
-
var _a;
|
|
1129
|
-
const rootNode = this.temporaryGraphNodeRefs[graphID];
|
|
1130
|
-
if (rootNode === undefined) {
|
|
1131
|
-
throw new Error(`Graph node with ID ${graphID} not found`);
|
|
1132
|
-
}
|
|
1133
|
-
// Check that the root node is Bitcoin-valid.
|
|
1134
|
-
const beef = this.getBEEFForNode(rootNode);
|
|
1135
|
-
const spvTx = sdk_1.Transaction.fromBEEF(beef);
|
|
1136
|
-
const isBitcoinValid = await spvTx.verify(this.engine.chainTracker);
|
|
1137
|
-
if (!isBitcoinValid) {
|
|
1138
|
-
throw new Error('The graph is not well-anchored according to the rules of Bitcoin.');
|
|
1139
|
-
}
|
|
1140
|
-
// Then, ensure the node is Overlay-valid.
|
|
1141
|
-
const beefs = this.computeOrderedBEEFsForGraph(graphID);
|
|
1142
|
-
// coins: a Set of all historical coins to retain (no need to remove them), used to emulate topical admittance of previous inputs over time.
|
|
1143
|
-
const coins = new Set();
|
|
1144
|
-
// Submit all historical BEEFs in order through the topic manager, tracking what would be retained until we submit the root node last.
|
|
1145
|
-
// If, at the end, the root node is admitted, we have a valid overlay-specific graph.
|
|
1146
|
-
for (const beef of beefs) {
|
|
1147
|
-
// For any input to this transaction, see if it's a valid coin that's admitted. If so, it's a previous coin.
|
|
1148
|
-
const previousCoins = [];
|
|
1149
|
-
const tx = sdk_1.Transaction.fromBEEF(beef);
|
|
1150
|
-
for (const inputIndex in tx.inputs) {
|
|
1151
|
-
const input = tx.inputs[inputIndex];
|
|
1152
|
-
const sourceTXID = input.sourceTXID || ((_a = input.sourceTransaction) === null || _a === void 0 ? void 0 : _a.id('hex'));
|
|
1153
|
-
const coin = `${sourceTXID}.${input.sourceOutputIndex}`;
|
|
1154
|
-
if (coins.has(coin)) {
|
|
1155
|
-
previousCoins.push(Number(inputIndex));
|
|
1156
|
-
}
|
|
1157
|
-
}
|
|
1158
|
-
const admittanceInstructions = await this.engine.managers[this.topic].identifyAdmissibleOutputs(beef, previousCoins);
|
|
1159
|
-
// Every admitted output is now a coin.
|
|
1160
|
-
for (const outputIndex of admittanceInstructions.outputsToAdmit) {
|
|
1161
|
-
coins.add(`${tx.id('hex')}.${outputIndex}`);
|
|
1162
|
-
}
|
|
1163
|
-
}
|
|
1164
|
-
// After sending through all the graph's BEEFs...
|
|
1165
|
-
// If the root node is now a coin, we have acceptance by the overlay.
|
|
1166
|
-
// Otherwise, throw.
|
|
1167
|
-
if (!coins.has(graphID)) {
|
|
1168
|
-
throw new Error('This graph did not result in topical admittance of the root node. Rejecting.');
|
|
1169
|
-
}
|
|
1170
|
-
}
|
|
1171
|
-
/**
|
|
1172
|
-
* Deletes all data associated with a temporary graph that has failed to sync, if the graph exists.
|
|
1173
|
-
* @param graphID The TXID and output index (in 36-byte format) for the UTXO at the tip of this graph.
|
|
1174
|
-
*/
|
|
1175
|
-
async discardGraph(graphID) {
|
|
1176
|
-
for (const [nodeId, graphRef] of Object.entries(this.temporaryGraphNodeRefs)) {
|
|
1177
|
-
if (graphRef.graphID === graphID) {
|
|
1178
|
-
// Delete child node
|
|
1179
|
-
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
1180
|
-
delete this.temporaryGraphNodeRefs[nodeId];
|
|
1181
|
-
}
|
|
1182
|
-
}
|
|
1183
|
-
}
|
|
1184
|
-
/**
|
|
1185
|
-
* Finalizes a graph, solidifying the new UTXO and its ancestors so that it will appear in the list of known UTXOs.
|
|
1186
|
-
* @param graphID The TXID and output index (in 36-byte format) for the UTXO at the root of this graph.
|
|
1187
|
-
*/
|
|
1188
|
-
async finalizeGraph(graphID) {
|
|
1189
|
-
const beefs = this.computeOrderedBEEFsForGraph(graphID);
|
|
1190
|
-
// Submit all historical BEEFs in order, finalizing the graph for the current UTXO
|
|
1191
|
-
for (const beef of beefs) {
|
|
1192
|
-
await this.engine.submit({
|
|
1193
|
-
beef,
|
|
1194
|
-
topics: [this.topic]
|
|
1195
|
-
}, () => { }, 'historical-tx');
|
|
1196
|
-
}
|
|
1197
|
-
}
|
|
1198
|
-
/**
|
|
1199
|
-
* Computes an ordered set of BEEFs for the graph with the given graph IDs
|
|
1200
|
-
* @param {string} graphID — The ID of the graph for which BEEFs are required
|
|
1201
|
-
* @returns Ordered BEEFs for the graph
|
|
1202
|
-
*/
|
|
1203
|
-
computeOrderedBEEFsForGraph(graphID) {
|
|
1204
|
-
const beefs = [];
|
|
1205
|
-
const hydrator = (node) => {
|
|
1206
|
-
const currentBEEF = this.getBEEFForNode(node);
|
|
1207
|
-
if (beefs.indexOf(currentBEEF) === -1) {
|
|
1208
|
-
beefs.unshift(currentBEEF);
|
|
1209
|
-
}
|
|
1210
|
-
for (const child of node.children) {
|
|
1211
|
-
// Continue backwards to the earliest nodes, adding them onto the beginning
|
|
1212
|
-
hydrator(child);
|
|
1213
|
-
}
|
|
1214
|
-
};
|
|
1215
|
-
// Start the hydrator with the root node
|
|
1216
|
-
const foundRoot = this.temporaryGraphNodeRefs[graphID];
|
|
1217
|
-
if (!foundRoot) {
|
|
1218
|
-
throw new Error('Unable to find root node in graph for finalization!');
|
|
1219
|
-
}
|
|
1220
|
-
hydrator(foundRoot);
|
|
1221
|
-
return beefs;
|
|
1222
|
-
}
|
|
1223
|
-
/**
|
|
1224
|
-
* Computes a full BEEF for a given graph node, based on the temporary graph store.
|
|
1225
|
-
* @param node Graph node for which BEEF is needed.
|
|
1226
|
-
* @returns BEEF array, including all proofs on inputs.
|
|
1227
|
-
*/
|
|
1228
|
-
getBEEFForNode(node) {
|
|
1229
|
-
// Given a node, hydrate its merkle proof or all inputs, returning a reference to the hydrated node's Transaction object
|
|
1230
|
-
const hydrator = (node) => {
|
|
1231
|
-
const tx = sdk_1.Transaction.fromHex(node.rawTx);
|
|
1232
|
-
if (node.proof) {
|
|
1233
|
-
tx.merklePath = sdk_1.MerklePath.fromHex(node.proof);
|
|
1234
|
-
return tx; // Transaction with proof, end of the line.
|
|
1235
|
-
}
|
|
1236
|
-
// For each input, look it up and recurse.
|
|
1237
|
-
for (const inputIndex in tx.inputs) {
|
|
1238
|
-
const input = tx.inputs[inputIndex];
|
|
1239
|
-
const foundNode = this.temporaryGraphNodeRefs[`${input.sourceTXID}.${input.sourceOutputIndex}`];
|
|
1240
|
-
if (!foundNode) {
|
|
1241
|
-
throw new Error('Required input node for unproven parent not found in temporary graph store. Ensure, for every parent of any given already-proven node (kept for Overlay-specific historical reasons), that a proof is also provided on those inputs. While implicitly they are valid by virtue of their descendents being proven in the blockchain, BEEF serialization will still fail when winding forward the topical UTXO set histories during sync.');
|
|
1242
|
-
}
|
|
1243
|
-
tx.inputs[inputIndex].sourceTransaction = hydrator(foundNode);
|
|
1244
|
-
}
|
|
1245
|
-
return tx;
|
|
1246
|
-
};
|
|
1247
|
-
const finalTX = hydrator(node);
|
|
1248
|
-
return finalTX.toBEEF();
|
|
1249
|
-
}
|
|
1250
|
-
}
|
|
1251
|
-
exports.OverlayGASPStorage = OverlayGASPStorage;
|
|
874
|
+
// TODO: fix bug with imports that break tests. -----[GASP/OverlayGASPStorage.ts]-----
|
|
1252
875
|
//# sourceMappingURL=Engine.js.map
|