@bsv/overlay 0.1.12 → 0.1.14
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 +82 -411
- 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 +82 -411
- 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 +14 -115
- 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 +68 -9
- package/src/__tests/Engine.test.ts +95 -1
- 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.14",
|
|
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,11 @@
|
|
|
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");
|
|
8
|
+
const url_1 = require("url");
|
|
6
9
|
/**
|
|
7
10
|
* Am engine for running BSV Overlay Services (topic managers and lookup services).
|
|
8
11
|
*/
|
|
@@ -12,7 +15,7 @@ class Engine {
|
|
|
12
15
|
* @param {[key: string]: TopicManager} managers - manages topic admittance
|
|
13
16
|
* @param {[key: string]: LookupService} lookupServices - manages UTXO lookups
|
|
14
17
|
* @param {Storage} storage - for interacting with internally-managed persistent data
|
|
15
|
-
* @param {ChainTracker} chainTracker - Verifies SPV data associated with transactions
|
|
18
|
+
* @param {ChainTracker | 'scripts only'} chainTracker - Verifies SPV data associated with transactions
|
|
16
19
|
* @param {string} [hostingURL] - The URL this engine is hosted at. Required if going to support peer-discovery with an advertiser.
|
|
17
20
|
* @param {Broadcaster} [Broadcaster] - broadcaster used for broadcasting the incoming transaction
|
|
18
21
|
* @param {Advertiser} [Advertiser] - handles SHIP and SLAP advertisements for peer-discovery
|
|
@@ -21,8 +24,9 @@ class Engine {
|
|
|
21
24
|
* @param {SyncConfiguration} syncConfiguration — Configuration object describing historical synchronization of topics.
|
|
22
25
|
* @param {boolean} logTime - Enables / disables the timing logs for various operations in the Overlay submit route.
|
|
23
26
|
* @param {string} logPrefix - Supports overriding the log prefix with a custom string.
|
|
27
|
+
* @param {boolean} throwOnBroadcastFailure - Enables / disables throwing an error when a transaction broadcast failure is detected.
|
|
24
28
|
*/
|
|
25
|
-
constructor(managers, lookupServices, storage, chainTracker, hostingURL, shipTrackers, slapTrackers, broadcaster, advertiser, syncConfiguration, logTime = false, logPrefix = '[OVERLAY_ENGINE] ') {
|
|
29
|
+
constructor(managers, lookupServices, storage, chainTracker, hostingURL, shipTrackers, slapTrackers, broadcaster, advertiser, syncConfiguration, logTime = false, logPrefix = '[OVERLAY_ENGINE] ', throwOnBroadcastFailure = false) {
|
|
26
30
|
this.managers = managers;
|
|
27
31
|
this.lookupServices = lookupServices;
|
|
28
32
|
this.storage = storage;
|
|
@@ -35,6 +39,7 @@ class Engine {
|
|
|
35
39
|
this.syncConfiguration = syncConfiguration;
|
|
36
40
|
this.logTime = logTime;
|
|
37
41
|
this.logPrefix = logPrefix;
|
|
42
|
+
this.throwOnBroadcastFailure = throwOnBroadcastFailure;
|
|
38
43
|
// 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
44
|
// For these two topics, any existing trackers are combined with the provided shipTrackers and slapTrackers omitting any duplicates.
|
|
40
45
|
if (syncConfiguration === undefined) {
|
|
@@ -84,7 +89,6 @@ class Engine {
|
|
|
84
89
|
* @param {TaggedBEEF} taggedBEEF - The transaction to process
|
|
85
90
|
* @param {function(STEAK): void} [onSTEAKReady] - Optional callback function invoked when the STEAK is ready.
|
|
86
91
|
* @param {string} mode — Indicates the submission behavior, whether historical or current. Historical transactions are not broadcast or propagated.
|
|
87
|
-
* @param {boolean} log
|
|
88
92
|
*
|
|
89
93
|
* The optional callback function should be used to get STEAK when ready, and avoid waiting for broadcast and transaction propagation to complete.
|
|
90
94
|
*
|
|
@@ -103,11 +107,11 @@ class Engine {
|
|
|
103
107
|
this.startTime(`submit_${txid}`);
|
|
104
108
|
this.startTime(`chainTracker_${txid.substring(0, 10)}`);
|
|
105
109
|
const txValid = await tx.verify(this.chainTracker);
|
|
106
|
-
this.endTime(`chainTracker_${txid.substring(0, 10)}`);
|
|
107
110
|
if (!txValid)
|
|
108
111
|
throw new Error('Unable to verify SPV information.');
|
|
112
|
+
this.endTime(`chainTracker_${txid.substring(0, 10)}`);
|
|
109
113
|
const steak = {};
|
|
110
|
-
let
|
|
114
|
+
let admissibleOutputs = { outputsToAdmit: [], coinsToRetain: [] };
|
|
111
115
|
const previousCoins = [];
|
|
112
116
|
// Parallelize the topic processing
|
|
113
117
|
const topicPromises = taggedBEEF.topics.map(async (topic) => {
|
|
@@ -123,20 +127,22 @@ class Engine {
|
|
|
123
127
|
return;
|
|
124
128
|
}
|
|
125
129
|
// Check if any input of this transaction is a previous UTXO
|
|
126
|
-
const outputPromises = tx.inputs.map(input => {
|
|
130
|
+
const outputPromises = tx.inputs.map(async (input, i) => {
|
|
127
131
|
var _a;
|
|
128
|
-
const previousTXID = input.sourceTXID
|
|
129
|
-
|
|
132
|
+
const previousTXID = input.sourceTXID !== undefined ? input.sourceTXID : (_a = input.sourceTransaction) === null || _a === void 0 ? void 0 : _a.id('hex');
|
|
133
|
+
if (previousTXID !== undefined) {
|
|
134
|
+
const output = this.storage.findOutput(previousTXID, input.sourceOutputIndex, topic);
|
|
135
|
+
if (output !== undefined && output !== null) {
|
|
136
|
+
previousCoins.push(i);
|
|
137
|
+
return await Promise.resolve(output);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return await Promise.resolve(null);
|
|
130
141
|
});
|
|
131
142
|
this.startTime(`previousOutputQuery_${txid.substring(0, 10)}`);
|
|
132
143
|
const outputs = await Promise.all(outputPromises);
|
|
133
144
|
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) => {
|
|
145
|
+
const markSpentPromises = outputs.map(async (output) => {
|
|
140
146
|
if (output !== undefined && output !== null) {
|
|
141
147
|
try {
|
|
142
148
|
await this.storage.markUTXOAsSpent(output.txid, output.outputIndex, topic);
|
|
@@ -159,7 +165,7 @@ class Engine {
|
|
|
159
165
|
const admissibleOutputsPromise = (async () => {
|
|
160
166
|
try {
|
|
161
167
|
this.startTime(`identifyAdmissibleOutputs_${txid.substring(0, 10)}`);
|
|
162
|
-
|
|
168
|
+
admissibleOutputs = await this.managers[topic].identifyAdmissibleOutputs(taggedBEEF.beef, previousCoins);
|
|
163
169
|
this.endTime(`identifyAdmissibleOutputs_${txid.substring(0, 10)}`);
|
|
164
170
|
}
|
|
165
171
|
catch (_) {
|
|
@@ -169,15 +175,17 @@ class Engine {
|
|
|
169
175
|
// Wait for both tasks to complete
|
|
170
176
|
await Promise.all([markSpentPromises, admissibleOutputsPromise]);
|
|
171
177
|
// Keep track of what outputs were admitted for what topic
|
|
172
|
-
steak[topic] =
|
|
178
|
+
steak[topic] = admissibleOutputs;
|
|
173
179
|
});
|
|
174
180
|
await Promise.all(topicPromises);
|
|
175
181
|
// Broadcast the transaction if not historical and broadcaster is configured
|
|
176
182
|
this.startTime(`broadcast_${txid.substring(0, 10)}`);
|
|
177
183
|
if (mode !== 'historical-tx' && this.broadcaster !== undefined) {
|
|
178
184
|
const response = await this.broadcaster.broadcast(tx);
|
|
179
|
-
if ((0, sdk_1.isBroadcastFailure)(response)) {
|
|
180
|
-
|
|
185
|
+
if ((0, sdk_1.isBroadcastFailure)(response) && this.throwOnBroadcastFailure) {
|
|
186
|
+
const e = new Error(`Failed to broadcast transaction! Error: ${response.description}`);
|
|
187
|
+
e.more = response.more;
|
|
188
|
+
throw e;
|
|
181
189
|
}
|
|
182
190
|
}
|
|
183
191
|
this.endTime(`broadcast_${txid.substring(0, 10)}`);
|
|
@@ -188,7 +196,7 @@ class Engine {
|
|
|
188
196
|
}
|
|
189
197
|
for (const topic of taggedBEEF.topics) {
|
|
190
198
|
// Keep track of which outputs to admit, mark as stale, or retain
|
|
191
|
-
const outputsToAdmit =
|
|
199
|
+
const outputsToAdmit = admissibleOutputs.outputsToAdmit;
|
|
192
200
|
const staleCoins = [];
|
|
193
201
|
const outputsConsumed = [];
|
|
194
202
|
// Find which outputs should not be retained and mark them as stale
|
|
@@ -196,7 +204,7 @@ class Engine {
|
|
|
196
204
|
for (const inputIndex of previousCoins) {
|
|
197
205
|
const previousTXID = tx.inputs[inputIndex].sourceTXID || ((_a = tx.inputs[inputIndex].sourceTransaction) === null || _a === void 0 ? void 0 : _a.id('hex'));
|
|
198
206
|
const previousOutputIndex = tx.inputs[inputIndex].sourceOutputIndex;
|
|
199
|
-
if (!
|
|
207
|
+
if (!admissibleOutputs.coinsToRetain.includes(inputIndex)) {
|
|
200
208
|
staleCoins.push({
|
|
201
209
|
txid: previousTXID,
|
|
202
210
|
outputIndex: previousOutputIndex
|
|
@@ -236,7 +244,7 @@ class Engine {
|
|
|
236
244
|
this.endTime(`insertNewOutput_${txid.substring(0, 10)}`);
|
|
237
245
|
newUTXOs.push({ txid, outputIndex });
|
|
238
246
|
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); }));
|
|
247
|
+
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
248
|
this.endTime(`notifyLookupService${txid.substring(0, 10)}`);
|
|
241
249
|
}));
|
|
242
250
|
this.startTime(`outputConsumed_${txid.substring(0, 10)}`);
|
|
@@ -260,10 +268,10 @@ class Engine {
|
|
|
260
268
|
if (this.advertiser === undefined || mode === 'historical-tx') {
|
|
261
269
|
return steak;
|
|
262
270
|
}
|
|
263
|
-
this.startTime(`
|
|
271
|
+
this.startTime(`transactionPropagation_${txid.substring(0, 10)}`);
|
|
264
272
|
// Propagate transaction to other nodes according to synchronization agreements
|
|
265
273
|
// 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)
|
|
274
|
+
// We want to figure out which topics we actually care about (because their associated outputs were admitted)
|
|
267
275
|
// AND if the topic was not admitted we want to remove it from the list of topics we care about.
|
|
268
276
|
const relevantTopics = taggedBEEF.topics.filter(topic => steak[topic] !== undefined && steak[topic].outputsToAdmit.length !== 0);
|
|
269
277
|
// TODO: Cache ship/slap lookup with expiry (every 5min)
|
|
@@ -417,7 +425,7 @@ class Engine {
|
|
|
417
425
|
* @returns {Promise<void>} A promise that resolves when the synchronization process is complete.
|
|
418
426
|
*/
|
|
419
427
|
async syncAdvertisements() {
|
|
420
|
-
if (this.advertiser === undefined) {
|
|
428
|
+
if (this.advertiser === undefined || !this.hostingURL || !this.isValidUrl(this.hostingURL)) {
|
|
421
429
|
return;
|
|
422
430
|
}
|
|
423
431
|
const advertiser = this.advertiser;
|
|
@@ -518,7 +526,7 @@ class Engine {
|
|
|
518
526
|
if (Array.isArray(syncEndpoints)) {
|
|
519
527
|
await Promise.all(syncEndpoints.map(async (endpoint) => {
|
|
520
528
|
// 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);
|
|
529
|
+
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
530
|
await gasp.sync();
|
|
523
531
|
}));
|
|
524
532
|
}
|
|
@@ -854,6 +862,53 @@ class Engine {
|
|
|
854
862
|
const documentation = await ((_b = (_a = this.lookupServices[provider]) === null || _a === void 0 ? void 0 : _a.getDocumentation) === null || _b === void 0 ? void 0 : _b.call(_a));
|
|
855
863
|
return documentation !== undefined ? documentation : 'No documentation found!';
|
|
856
864
|
}
|
|
865
|
+
/**
|
|
866
|
+
* Validates a URL to ensure it does not match disallowed patterns:
|
|
867
|
+
* - Contains "http:" protocol
|
|
868
|
+
* - Contains "localhost" (with or without a port)
|
|
869
|
+
* - Internal or non-routable IP addresses (e.g., 192.168.x.x, 10.x.x.x, 172.16.x.x to 172.31.x.x)
|
|
870
|
+
* - Non-routable IPs like 127.x.x.x, 0.0.0.0, or IPv6 loopback (::1)
|
|
871
|
+
*
|
|
872
|
+
* @param url - The URL string to validate
|
|
873
|
+
* @returns {boolean} - Returns `false` if the URL violates any of the conditions `true` otherwise
|
|
874
|
+
*/
|
|
875
|
+
isValidUrl(url) {
|
|
876
|
+
try {
|
|
877
|
+
const parsedUrl = new url_1.URL(url);
|
|
878
|
+
// Disallow http:
|
|
879
|
+
if (parsedUrl.protocol === "http:") {
|
|
880
|
+
return false;
|
|
881
|
+
}
|
|
882
|
+
// Disallow localhost with or without a port
|
|
883
|
+
if (/^localhost(:\d+)?$/i.test(parsedUrl.hostname)) {
|
|
884
|
+
return false;
|
|
885
|
+
}
|
|
886
|
+
// Disallow internal and non-routable IP addresses
|
|
887
|
+
const ipAddress = parsedUrl.hostname;
|
|
888
|
+
// Regex for non-routable IPv4 IPs
|
|
889
|
+
const nonRoutableIpv4Patterns = [
|
|
890
|
+
/^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/, // Loopback IPs
|
|
891
|
+
/^10\.\d{1,3}\.\d{1,3}\.\d{1,3}$/, // 10.x.x.x private IPs
|
|
892
|
+
/^192\.168\.\d{1,3}\.\d{1,3}$/, // 192.168.x.x private IPs
|
|
893
|
+
/^172\.(1[6-9]|2[0-9]|3[0-1])\.\d{1,3}\.\d{1,3}$/, // 172.16.x.x to 172.31.x.x private IPs
|
|
894
|
+
/^0\.0\.0\.0$/ // Non-routable address
|
|
895
|
+
];
|
|
896
|
+
// Check for IPv4 matches
|
|
897
|
+
if (nonRoutableIpv4Patterns.some((pattern) => pattern.test(ipAddress))) {
|
|
898
|
+
return false;
|
|
899
|
+
}
|
|
900
|
+
// Check for non-routable IPv6 addresses explicitly
|
|
901
|
+
if (ipAddress === "[::1]") {
|
|
902
|
+
return false;
|
|
903
|
+
}
|
|
904
|
+
// If none of the disallowed conditions matched, the URL is valid
|
|
905
|
+
return true;
|
|
906
|
+
}
|
|
907
|
+
catch (error) {
|
|
908
|
+
// If the input is not a valid URL, return false
|
|
909
|
+
return false;
|
|
910
|
+
}
|
|
911
|
+
}
|
|
857
912
|
}
|
|
858
913
|
exports.Engine = Engine;
|
|
859
914
|
//////////
|
|
@@ -864,389 +919,5 @@ There is currently a bug with the test runner that prevents importing and using
|
|
|
864
919
|
Thus, all non-type exports have been moved to Engine.
|
|
865
920
|
*/
|
|
866
921
|
// 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;
|
|
922
|
+
// TODO: fix bug with imports that break tests. -----[GASP/OverlayGASPStorage.ts]-----
|
|
1252
923
|
//# sourceMappingURL=Engine.js.map
|