@odatano/core 2.0.0-rc.35 → 2.0.0-rc.36
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/package.json +1 -1
- package/srv/cardano-service.js +115 -21
package/package.json
CHANGED
package/srv/cardano-service.js
CHANGED
|
@@ -92,6 +92,91 @@ function indexOnMissAction(actionName, entity, reqKeyField, validate, indexFn, o
|
|
|
92
92
|
});
|
|
93
93
|
};
|
|
94
94
|
}
|
|
95
|
+
/** Addresses one collection read may index on a miss; more is a 400. */
|
|
96
|
+
const MAX_ADDRESSES_PER_READ = 10;
|
|
97
|
+
const isRef = (t) => typeof t === 'object' && 'ref' in t;
|
|
98
|
+
const isVal = (t) => typeof t === 'object' && 'val' in t;
|
|
99
|
+
const isList = (t) => typeof t === 'object' && 'list' in t;
|
|
100
|
+
const isXpr = (t) => typeof t === 'object' && 'xpr' in t;
|
|
101
|
+
/** `address_address` or the association path `address/address`. */
|
|
102
|
+
function isAddressRef(t) {
|
|
103
|
+
if (!isRef(t))
|
|
104
|
+
return false;
|
|
105
|
+
const path = t.ref;
|
|
106
|
+
return (path.length === 1 && path[0] === 'address_address') || (path.length === 2 && path[0] === 'address' && path[1] === 'address');
|
|
107
|
+
}
|
|
108
|
+
const stringVal = (t) => t !== undefined && isVal(t) && typeof t.val === 'string' ? t.val : undefined;
|
|
109
|
+
/**
|
|
110
|
+
* Addresses a read of an address child set names: the parent of a navigation
|
|
111
|
+
* (`Addresses('…')/utxos`) or the `$filter` terms `address_address eq '…'` (either operand
|
|
112
|
+
* order) and `address_address in ('…', …)`.
|
|
113
|
+
*/
|
|
114
|
+
function addressesInReadQuery(req) {
|
|
115
|
+
const found = new Set();
|
|
116
|
+
const parent = req.data?.address_address;
|
|
117
|
+
if (typeof parent === 'string')
|
|
118
|
+
found.add(parent);
|
|
119
|
+
const walk = (tokens) => {
|
|
120
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
121
|
+
const token = tokens[i];
|
|
122
|
+
if (typeof token === 'string')
|
|
123
|
+
continue;
|
|
124
|
+
if (isXpr(token)) {
|
|
125
|
+
walk(token.xpr);
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const op = tokens[i + 1];
|
|
129
|
+
const right = tokens[i + 2];
|
|
130
|
+
if (isAddressRef(token) && op === '=') {
|
|
131
|
+
const value = stringVal(right);
|
|
132
|
+
if (value)
|
|
133
|
+
found.add(value);
|
|
134
|
+
}
|
|
135
|
+
else if (isAddressRef(token) && op === 'in' && right !== undefined && isList(right)) {
|
|
136
|
+
for (const item of right.list) {
|
|
137
|
+
const value = stringVal(item);
|
|
138
|
+
if (value)
|
|
139
|
+
found.add(value);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
else if (right !== undefined && isAddressRef(right) && op === '=') {
|
|
143
|
+
const value = stringVal(token);
|
|
144
|
+
if (value)
|
|
145
|
+
found.add(value);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
const query = req.query;
|
|
150
|
+
walk(('SELECT' in query && query.SELECT?.where) || []);
|
|
151
|
+
return Array.from(found);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Factory: READ handler for a child set of Addresses with index-on-miss. The address is taken
|
|
155
|
+
* from the navigation parent or the `$filter`; without one the read passes through.
|
|
156
|
+
* `isIndexed` says whether the address needs no fetch, `indexFn` fetches it.
|
|
157
|
+
*/
|
|
158
|
+
function addressChildRead(isIndexed, indexFn) {
|
|
159
|
+
return async (req) => {
|
|
160
|
+
const addresses = addressesInReadQuery(req);
|
|
161
|
+
if (addresses.length > MAX_ADDRESSES_PER_READ) {
|
|
162
|
+
(0, errors_1.rejectInvalid)(req, 'address_address', `A read may name at most ${MAX_ADDRESSES_PER_READ} addresses`, 'address_address');
|
|
163
|
+
}
|
|
164
|
+
for (const address of addresses) {
|
|
165
|
+
if (!(0, validators_1.isValidBech32Address)(address))
|
|
166
|
+
(0, errors_1.rejectInvalid)(req, 'address_address', 'Invalid bech32 address format', 'address_address');
|
|
167
|
+
}
|
|
168
|
+
return (0, backend_request_handler_1.handleRequest)(req, async (db) => {
|
|
169
|
+
if (addresses.length) {
|
|
170
|
+
widenTemporalWindow(req); // before the first DB statement
|
|
171
|
+
for (const address of addresses) {
|
|
172
|
+
if (!(await isIndexed(db, address)))
|
|
173
|
+
await indexFn(db, address);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return db.run(req.query);
|
|
177
|
+
});
|
|
178
|
+
};
|
|
179
|
+
}
|
|
95
180
|
// ---------------------------------------------------------------------------
|
|
96
181
|
// Service Registration
|
|
97
182
|
// ---------------------------------------------------------------------------
|
|
@@ -208,7 +293,28 @@ module.exports = (srv) => {
|
|
|
208
293
|
return latestSlices(assets, (asset) => asset.unit ?? '');
|
|
209
294
|
});
|
|
210
295
|
});
|
|
211
|
-
//
|
|
296
|
+
// Prefer the full address index; when no configured backend supports getAddress
|
|
297
|
+
// (AllBackendsFailedError with zero collected errors, e.g. Ogmios only), fall back to a
|
|
298
|
+
// UTxO-only index. getAddress is indexAddress's first call, so nothing is persisted before.
|
|
299
|
+
const indexAddressOrUtxos = async (db, address) => {
|
|
300
|
+
try {
|
|
301
|
+
await indexer().indexAddress(db, address);
|
|
302
|
+
}
|
|
303
|
+
catch (err) {
|
|
304
|
+
if (err instanceof errors_1.AllBackendsFailedError && err.errors.length === 0) {
|
|
305
|
+
await indexer().indexAddressUtxos(db, address);
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
throw err;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
// An address is indexed when its Addresses row is valid. The UTxO-only fallback writes no
|
|
313
|
+
// Addresses row, so for UTxOs a valid AddressUTxOs row counts as well.
|
|
314
|
+
const addressIndexed = async (db, address) => !!(await db.run(SELECT.one.from(Addresses).where({ address })));
|
|
315
|
+
const addressUtxosIndexed = async (db, address) => (await addressIndexed(db, address))
|
|
316
|
+
|| !!(await db.run(SELECT.one.from(AddressUTxOs).where({ address_address: address })));
|
|
317
|
+
// GetUTxOsByAddress — indexes the address if needed, then queries child UTxOs.
|
|
212
318
|
srv.on('GetUTxOsByAddress', async (req) => {
|
|
213
319
|
const { address } = req.data;
|
|
214
320
|
if (!address)
|
|
@@ -217,28 +323,16 @@ module.exports = (srv) => {
|
|
|
217
323
|
(0, errors_1.rejectInvalid)(req, 'GetUTxOsByAddress', 'Invalid bech32 address format', 'address');
|
|
218
324
|
return (0, backend_request_handler_1.handleRequest)(req, async (db) => {
|
|
219
325
|
widenTemporalWindow(req); // before the first DB statement
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
// UTxO-only index. getAddress is indexAddress's first call, so nothing is persisted before.
|
|
225
|
-
try {
|
|
226
|
-
await indexer().indexAddress(db, address);
|
|
227
|
-
}
|
|
228
|
-
catch (err) {
|
|
229
|
-
if (err instanceof errors_1.AllBackendsFailedError && err.errors.length === 0) {
|
|
230
|
-
await indexer().indexAddressUtxos(db, address);
|
|
231
|
-
}
|
|
232
|
-
else {
|
|
233
|
-
throw err;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
const fresh = await db.run(SELECT.from(AddressUTxOs).where({ address_address: address }));
|
|
237
|
-
return fresh;
|
|
238
|
-
}
|
|
239
|
-
return latestSlices(existing, (utxo) => `${utxo.hash}#${utxo.index}`);
|
|
326
|
+
if (!(await addressUtxosIndexed(db, address)))
|
|
327
|
+
await indexAddressOrUtxos(db, address);
|
|
328
|
+
const utxos = await db.run(SELECT.from(AddressUTxOs).where({ address_address: address }));
|
|
329
|
+
return latestSlices(utxos, (utxo) => `${utxo.hash}#${utxo.index}`);
|
|
240
330
|
});
|
|
241
331
|
});
|
|
332
|
+
// Collection reads of the address child sets fill like the parent: an address named in the
|
|
333
|
+
// navigation or the $filter is indexed on a miss, then the client's own query runs.
|
|
334
|
+
srv.on('READ', AddressUTxOs, addressChildRead(addressUtxosIndexed, indexAddressOrUtxos));
|
|
335
|
+
srv.on('READ', AddressAssets, addressChildRead(addressIndexed, (db, address) => indexer().indexAddress(db, address)));
|
|
242
336
|
// GetUTxOsByCredential — always-fresh credential-keyed UTxO query (dApp state reads need current
|
|
243
337
|
// data). Crawled UTxO set + node first, else Koios; ProviderUnavailableError without either.
|
|
244
338
|
srv.on('GetUTxOsByCredential', async (req) => {
|