@xyo-network/chain-validation 1.22.0 → 1.23.2
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/neutral/index.mjs
CHANGED
|
@@ -1,44 +1,49 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
-
|
|
4
1
|
// src/block/validateBlock.ts
|
|
5
2
|
import { ZERO_HASH as ZERO_HASH6 } from "@xylabs/sdk-js";
|
|
6
3
|
import { BlockValidationError as BlockValidationError6 } from "@xyo-network/xl1-sdk";
|
|
7
4
|
|
|
8
5
|
// src/block/validators/AllowedPayloadSchemas.ts
|
|
9
6
|
import { ZERO_HASH } from "@xylabs/sdk-js";
|
|
10
|
-
import {
|
|
11
|
-
|
|
7
|
+
import {
|
|
8
|
+
BlockValidationError,
|
|
9
|
+
isAllowedBlockPayloadSchema
|
|
10
|
+
} from "@xyo-network/xl1-sdk";
|
|
11
|
+
var BlockAllowedPayloadSchemasValidator = (context, block) => {
|
|
12
12
|
const errors = [];
|
|
13
13
|
try {
|
|
14
14
|
for (const schema of block.payload_schemas) {
|
|
15
15
|
if (!isAllowedBlockPayloadSchema(schema)) {
|
|
16
|
-
errors.push(new BlockValidationError(
|
|
16
|
+
errors.push(new BlockValidationError(
|
|
17
|
+
block?._hash ?? ZERO_HASH,
|
|
18
|
+
block,
|
|
19
|
+
`payload schema not allowed in block: ${schema}`
|
|
20
|
+
));
|
|
17
21
|
}
|
|
18
22
|
}
|
|
19
23
|
} catch (ex) {
|
|
20
|
-
errors.push(new BlockValidationError(
|
|
24
|
+
errors.push(new BlockValidationError(
|
|
25
|
+
block?._hash ?? ZERO_HASH,
|
|
26
|
+
block,
|
|
27
|
+
`validation excepted: ${ex}`,
|
|
28
|
+
ex
|
|
29
|
+
));
|
|
21
30
|
}
|
|
22
31
|
return errors;
|
|
23
|
-
}
|
|
32
|
+
};
|
|
24
33
|
|
|
25
34
|
// src/block/validators/BoundWitness.ts
|
|
26
35
|
import { ZERO_HASH as ZERO_HASH2 } from "@xylabs/sdk-js";
|
|
27
36
|
import { BoundWitnessValidator } from "@xyo-network/sdk-js";
|
|
28
37
|
import { BlockValidationError as BlockValidationError2 } from "@xyo-network/xl1-sdk";
|
|
29
|
-
var error =
|
|
30
|
-
var checkSignaturesShape =
|
|
38
|
+
var error = (block, message) => new BlockValidationError2(block?._hash ?? ZERO_HASH2, block, message);
|
|
39
|
+
var checkSignaturesShape = (block) => {
|
|
31
40
|
const bw = block;
|
|
32
41
|
if (bw.$signatures === void 0) {
|
|
33
|
-
return Array.isArray(bw.signatures) ? [
|
|
34
|
-
error(block, "BoundWitness has `signatures` but expected `$signatures` (signatures must be in the meta-prefixed `$signatures` key)")
|
|
35
|
-
] : [
|
|
36
|
-
error(block, "BoundWitness is missing `$signatures`")
|
|
37
|
-
];
|
|
42
|
+
return Array.isArray(bw.signatures) ? [error(block, "BoundWitness has `signatures` but expected `$signatures` (signatures must be in the meta-prefixed `$signatures` key)")] : [error(block, "BoundWitness is missing `$signatures`")];
|
|
38
43
|
}
|
|
39
44
|
return [];
|
|
40
|
-
}
|
|
41
|
-
var BlockBoundWitnessValidator =
|
|
45
|
+
};
|
|
46
|
+
var BlockBoundWitnessValidator = async (_context, block) => {
|
|
42
47
|
try {
|
|
43
48
|
const shapeErrors = checkSignaturesShape(block);
|
|
44
49
|
if (shapeErrors.length > 0) return shapeErrors;
|
|
@@ -46,17 +51,15 @@ var BlockBoundWitnessValidator = /* @__PURE__ */ __name(async (_context, block)
|
|
|
46
51
|
const bwErrors = await bwValidator.validate();
|
|
47
52
|
return bwErrors.map((e) => error(block, `BoundWitness validation: ${e.message}`));
|
|
48
53
|
} catch (ex) {
|
|
49
|
-
return [
|
|
50
|
-
error(block, `Failed BlockBoundWitnessValidator: ${String(ex)}`)
|
|
51
|
-
];
|
|
54
|
+
return [error(block, `Failed BlockBoundWitnessValidator: ${String(ex)}`)];
|
|
52
55
|
}
|
|
53
|
-
}
|
|
56
|
+
};
|
|
54
57
|
|
|
55
58
|
// src/block/validators/Fields.ts
|
|
56
59
|
import { isDefined, ZERO_HASH as ZERO_HASH3 } from "@xylabs/sdk-js";
|
|
57
60
|
import { BoundWitnessSchema } from "@xyo-network/boundwitness-model";
|
|
58
61
|
import { BlockValidationError as BlockValidationError3 } from "@xyo-network/xl1-sdk";
|
|
59
|
-
var BlockFieldsValidator =
|
|
62
|
+
var BlockFieldsValidator = (context, block, chainId) => {
|
|
60
63
|
const errors = [];
|
|
61
64
|
try {
|
|
62
65
|
if (isDefined(chainId) && block.chain !== chainId.toLowerCase()) {
|
|
@@ -69,48 +72,60 @@ var BlockFieldsValidator = /* @__PURE__ */ __name((context, block, chainId) => {
|
|
|
69
72
|
}
|
|
70
73
|
}
|
|
71
74
|
if (new Set(txHashes).size < txHashes.length) {
|
|
72
|
-
errors.push(new BlockValidationError3(
|
|
75
|
+
errors.push(new BlockValidationError3(
|
|
76
|
+
block?._hash ?? ZERO_HASH3,
|
|
77
|
+
block,
|
|
78
|
+
`Duplicate Transaction Hashes: ${txHashes}`
|
|
79
|
+
));
|
|
73
80
|
}
|
|
74
81
|
} catch (ex) {
|
|
75
|
-
errors.push(new BlockValidationError3(
|
|
82
|
+
errors.push(new BlockValidationError3(
|
|
83
|
+
block?._hash ?? ZERO_HASH3,
|
|
84
|
+
block,
|
|
85
|
+
`validation excepted: ${ex}`,
|
|
86
|
+
ex
|
|
87
|
+
));
|
|
76
88
|
}
|
|
77
89
|
return errors;
|
|
78
|
-
}
|
|
90
|
+
};
|
|
79
91
|
|
|
80
92
|
// src/block/validators/JsonSchema.ts
|
|
81
93
|
import { ZERO_HASH as ZERO_HASH4 } from "@xylabs/sdk-js";
|
|
82
94
|
import { BlockBoundWitnessWithStorageMetaJsonSchema, BlockValidationError as BlockValidationError4 } from "@xyo-network/xl1-sdk";
|
|
83
95
|
import { Ajv } from "ajv";
|
|
84
|
-
var BlockJsonSchemaValidator =
|
|
85
|
-
const ajv = new Ajv({
|
|
86
|
-
allErrors: true,
|
|
87
|
-
strict: true
|
|
88
|
-
});
|
|
96
|
+
var BlockJsonSchemaValidator = (jsonSchema = BlockBoundWitnessWithStorageMetaJsonSchema) => {
|
|
97
|
+
const ajv = new Ajv({ allErrors: true, strict: true });
|
|
89
98
|
const validate = ajv.compile(jsonSchema);
|
|
90
99
|
return async (context, block) => {
|
|
91
100
|
const errors = [];
|
|
92
101
|
try {
|
|
93
102
|
await validate(block);
|
|
94
103
|
if ((validate.errors ?? []).length > 0) {
|
|
95
|
-
const error2 = new BlockValidationError4(
|
|
96
|
-
|
|
97
|
-
|
|
104
|
+
const error2 = new BlockValidationError4(
|
|
105
|
+
block?._hash ?? ZERO_HASH4,
|
|
106
|
+
block,
|
|
107
|
+
`failed JSON schema validation: ${ajv.errorsText(validate.errors, { separator: "\n" })}`
|
|
108
|
+
);
|
|
98
109
|
error2.cause = validate.errors;
|
|
99
110
|
errors.push(error2);
|
|
100
111
|
}
|
|
101
112
|
} catch (ex) {
|
|
102
|
-
const error2 = new BlockValidationError4(
|
|
113
|
+
const error2 = new BlockValidationError4(
|
|
114
|
+
block?._hash ?? ZERO_HASH4,
|
|
115
|
+
block,
|
|
116
|
+
`validation excepted: ${ex}`
|
|
117
|
+
);
|
|
103
118
|
error2.cause = ex;
|
|
104
119
|
errors.push(error2);
|
|
105
120
|
}
|
|
106
121
|
return errors;
|
|
107
122
|
};
|
|
108
|
-
}
|
|
123
|
+
};
|
|
109
124
|
|
|
110
125
|
// src/block/validators/PreviousHash.ts
|
|
111
126
|
import { isHash, ZERO_HASH as ZERO_HASH5 } from "@xylabs/sdk-js";
|
|
112
127
|
import { BlockValidationError as BlockValidationError5 } from "@xyo-network/xl1-sdk";
|
|
113
|
-
var BlockPreviousHashValidator =
|
|
128
|
+
var BlockPreviousHashValidator = (context, block) => {
|
|
114
129
|
const errors = [];
|
|
115
130
|
try {
|
|
116
131
|
const blockNumber = block.block;
|
|
@@ -126,15 +141,19 @@ var BlockPreviousHashValidator = /* @__PURE__ */ __name((context, block) => {
|
|
|
126
141
|
errors.push(new BlockValidationError5(block?._hash ?? ZERO_HASH5, block, "invalid block number"));
|
|
127
142
|
}
|
|
128
143
|
} catch (ex) {
|
|
129
|
-
const error2 = new BlockValidationError5(
|
|
144
|
+
const error2 = new BlockValidationError5(
|
|
145
|
+
block?._hash ?? ZERO_HASH5,
|
|
146
|
+
block,
|
|
147
|
+
`Failed BlockPreviousHashValidator: ${ex}`
|
|
148
|
+
);
|
|
130
149
|
error2.cause = ex;
|
|
131
150
|
errors.push(error2);
|
|
132
151
|
}
|
|
133
152
|
return errors;
|
|
134
|
-
}
|
|
153
|
+
};
|
|
135
154
|
|
|
136
155
|
// src/block/validateBlock.ts
|
|
137
|
-
var validateBlock =
|
|
156
|
+
var validateBlock = async (context, block, chainId, additionalValidators = []) => {
|
|
138
157
|
const errors = [];
|
|
139
158
|
try {
|
|
140
159
|
const validators = [
|
|
@@ -144,12 +163,14 @@ var validateBlock = /* @__PURE__ */ __name(async (context, block, chainId, addit
|
|
|
144
163
|
BlockAllowedPayloadSchemasValidator,
|
|
145
164
|
...additionalValidators
|
|
146
165
|
];
|
|
147
|
-
errors.push(...(await Promise.all(
|
|
166
|
+
errors.push(...(await Promise.all(
|
|
167
|
+
validators.map((validator) => validator(context, block, chainId))
|
|
168
|
+
)).flat());
|
|
148
169
|
} catch (ex) {
|
|
149
170
|
errors.push(new BlockValidationError6(block?._hash ?? ZERO_HASH6, block, "validation excepted", ex));
|
|
150
171
|
}
|
|
151
172
|
return errors;
|
|
152
|
-
}
|
|
173
|
+
};
|
|
153
174
|
|
|
154
175
|
// src/hydratedBlock/validateHydratedBlock.ts
|
|
155
176
|
import { ZERO_HASH as ZERO_HASH18 } from "@xylabs/sdk-js";
|
|
@@ -163,11 +184,20 @@ import { HydratedBlockValidationError } from "@xyo-network/xl1-sdk";
|
|
|
163
184
|
import { ZERO_HASH as ZERO_HASH16 } from "@xylabs/sdk-js";
|
|
164
185
|
import { BoundWitnessSchema as BoundWitnessSchema2 } from "@xyo-network/boundwitness-model";
|
|
165
186
|
import { SchemaSchema } from "@xyo-network/sdk-js";
|
|
166
|
-
import {
|
|
187
|
+
import {
|
|
188
|
+
BridgeDestinationObservationSchema,
|
|
189
|
+
BridgeIntentSchema,
|
|
190
|
+
BridgeSourceObservationSchema,
|
|
191
|
+
ChainStakeIntentSchema,
|
|
192
|
+
HashSchema,
|
|
193
|
+
InBlockPayloadValidationError as InBlockPayloadValidationError12,
|
|
194
|
+
TimeSchema,
|
|
195
|
+
TransferSchema
|
|
196
|
+
} from "@xyo-network/xl1-sdk";
|
|
167
197
|
|
|
168
198
|
// src/elevatedPayload/lib/isElevatedFromBlock.ts
|
|
169
199
|
import { isTransactionBoundWitness } from "@xyo-network/xl1-sdk";
|
|
170
|
-
var isElevatedFromBlock =
|
|
200
|
+
var isElevatedFromBlock = (payload, [, payloads]) => {
|
|
171
201
|
const txs = payloads.filter((p) => isTransactionBoundWitness(p));
|
|
172
202
|
for (const tx of txs) {
|
|
173
203
|
if (tx.payload_hashes.includes(payload._hash)) {
|
|
@@ -175,27 +205,32 @@ var isElevatedFromBlock = /* @__PURE__ */ __name((payload, [, payloads]) => {
|
|
|
175
205
|
}
|
|
176
206
|
}
|
|
177
207
|
return true;
|
|
178
|
-
}
|
|
208
|
+
};
|
|
179
209
|
|
|
180
210
|
// src/elevatedPayload/lib/validateElevatedFromBlock.ts
|
|
181
211
|
import { InBlockPayloadValidationError, transactionsFromHydratedBlock } from "@xyo-network/xl1-sdk";
|
|
182
|
-
var validateElevatedFromBlock =
|
|
212
|
+
var validateElevatedFromBlock = (context, payload, block) => {
|
|
183
213
|
const errors = [];
|
|
184
214
|
try {
|
|
185
215
|
const txs = transactionsFromHydratedBlock(block);
|
|
186
216
|
const allTxPayloadHashes = new Set(txs.flatMap((tx) => tx.payload_hashes));
|
|
187
217
|
if (allTxPayloadHashes.has(payload._hash)) {
|
|
188
|
-
errors.push(new InBlockPayloadValidationError(
|
|
218
|
+
errors.push(new InBlockPayloadValidationError(
|
|
219
|
+
block[0]?._hash,
|
|
220
|
+
block,
|
|
221
|
+
payload,
|
|
222
|
+
`Transaction may not include block payload hash [${payload.schema}]: ${payload._hash}`
|
|
223
|
+
));
|
|
189
224
|
}
|
|
190
225
|
} catch (ex) {
|
|
191
226
|
errors.push(new InBlockPayloadValidationError(block[0]?._hash, block, payload, `validation excepted: ${ex}`, ex));
|
|
192
227
|
}
|
|
193
228
|
return errors;
|
|
194
|
-
}
|
|
229
|
+
};
|
|
195
230
|
|
|
196
231
|
// src/elevatedPayload/lib/validateElevatedFromTransaction.ts
|
|
197
232
|
import { InBlockPayloadValidationError as InBlockPayloadValidationError2, transactionsFromHydratedBlock as transactionsFromHydratedBlock2 } from "@xyo-network/xl1-sdk";
|
|
198
|
-
var validateElevatedFromTransaction =
|
|
233
|
+
var validateElevatedFromTransaction = (context, payload, block) => {
|
|
199
234
|
const errors = [];
|
|
200
235
|
try {
|
|
201
236
|
const txs = transactionsFromHydratedBlock2(block);
|
|
@@ -208,32 +243,49 @@ var validateElevatedFromTransaction = /* @__PURE__ */ __name((context, payload,
|
|
|
208
243
|
errors.push(new InBlockPayloadValidationError2(payload._hash, block, payload, "No Transactions in block"));
|
|
209
244
|
}
|
|
210
245
|
} catch (ex) {
|
|
211
|
-
errors.push(new InBlockPayloadValidationError2(
|
|
246
|
+
errors.push(new InBlockPayloadValidationError2(
|
|
247
|
+
payload._hash,
|
|
248
|
+
block,
|
|
249
|
+
payload,
|
|
250
|
+
`Failed validateElevatedFromTransaction: ${ex}`,
|
|
251
|
+
ex
|
|
252
|
+
));
|
|
212
253
|
}
|
|
213
254
|
return errors;
|
|
214
|
-
}
|
|
255
|
+
};
|
|
215
256
|
|
|
216
257
|
// src/elevatedPayload/lib/validateTransactionInBlock.ts
|
|
217
258
|
import { ZERO_HASH as ZERO_HASH7 } from "@xylabs/sdk-js";
|
|
218
259
|
import { isHashMeta } from "@xyo-network/sdk-js";
|
|
219
|
-
import {
|
|
220
|
-
|
|
260
|
+
import {
|
|
261
|
+
BoundWitnessSignaturesValidator,
|
|
262
|
+
InBlockPayloadValidationError as InBlockPayloadValidationError3,
|
|
263
|
+
isTransactionBoundWitness as isTransactionBoundWitness2,
|
|
264
|
+
validateTransaction
|
|
265
|
+
} from "@xyo-network/xl1-sdk";
|
|
266
|
+
var validateTransactionInBlock = async (context, payload, block) => {
|
|
221
267
|
const errors = [];
|
|
222
268
|
try {
|
|
223
269
|
if (isTransactionBoundWitness2(payload) && isHashMeta(payload)) {
|
|
224
|
-
const txErrors = await validateTransaction({
|
|
225
|
-
...context,
|
|
226
|
-
chainId: block[0].chain
|
|
227
|
-
}, [
|
|
228
|
-
payload,
|
|
229
|
-
block[1]
|
|
230
|
-
]);
|
|
270
|
+
const txErrors = await validateTransaction({ ...context, chainId: block[0].chain }, [payload, block[1]]);
|
|
231
271
|
for (const txError of txErrors) {
|
|
232
|
-
errors.push(new InBlockPayloadValidationError3(
|
|
272
|
+
errors.push(new InBlockPayloadValidationError3(
|
|
273
|
+
payload._hash,
|
|
274
|
+
block,
|
|
275
|
+
payload,
|
|
276
|
+
`TransactionValidation error: ${txError}`,
|
|
277
|
+
txError
|
|
278
|
+
));
|
|
233
279
|
}
|
|
234
280
|
const txSignatureErrors = await BoundWitnessSignaturesValidator(payload);
|
|
235
281
|
for (const txSignatureError of txSignatureErrors) {
|
|
236
|
-
errors.push(new InBlockPayloadValidationError3(
|
|
282
|
+
errors.push(new InBlockPayloadValidationError3(
|
|
283
|
+
payload._hash,
|
|
284
|
+
block,
|
|
285
|
+
payload,
|
|
286
|
+
`BoundWitnessSignaturesValidator error: ${txSignatureError}`,
|
|
287
|
+
txSignatureError
|
|
288
|
+
));
|
|
237
289
|
}
|
|
238
290
|
} else {
|
|
239
291
|
errors.push(new InBlockPayloadValidationError3(payload._hash, block, payload, "Payload failed isTransactionBoundWitness or isHashMeta"));
|
|
@@ -242,11 +294,11 @@ var validateTransactionInBlock = /* @__PURE__ */ __name(async (context, payload,
|
|
|
242
294
|
errors.push(new InBlockPayloadValidationError3(payload._hash ?? ZERO_HASH7, block, payload, `validation excepted: ${ex}`, ex));
|
|
243
295
|
}
|
|
244
296
|
return errors;
|
|
245
|
-
}
|
|
297
|
+
};
|
|
246
298
|
|
|
247
299
|
// src/elevatedPayload/lib/validateTypedPayloadInBlock.ts
|
|
248
300
|
import { isHashMeta as isHashMeta2 } from "@xyo-network/sdk-js";
|
|
249
|
-
var validateTypedPayloadInBlock =
|
|
301
|
+
var validateTypedPayloadInBlock = async (context, payload, block, identityFunction) => {
|
|
250
302
|
const errors = [];
|
|
251
303
|
try {
|
|
252
304
|
if (identityFunction(payload) && isHashMeta2(payload)) {
|
|
@@ -262,12 +314,12 @@ var validateTypedPayloadInBlock = /* @__PURE__ */ __name(async (context, payload
|
|
|
262
314
|
errors.push(new Error(`Failed validateTypedPayloadInBlock: ${ex}`));
|
|
263
315
|
}
|
|
264
316
|
return errors;
|
|
265
|
-
}
|
|
317
|
+
};
|
|
266
318
|
|
|
267
319
|
// src/elevatedPayload/payloads/validateBridgeDestinationObservationInBlock.ts
|
|
268
320
|
import { ZERO_HASH as ZERO_HASH8 } from "@xylabs/sdk-js";
|
|
269
321
|
import { InBlockPayloadValidationError as InBlockPayloadValidationError4, isBridgeDestinationObservation } from "@xyo-network/xl1-sdk";
|
|
270
|
-
var validateBridgeDestinationObservationInBlock =
|
|
322
|
+
var validateBridgeDestinationObservationInBlock = async (context, payload, block) => {
|
|
271
323
|
const errors = [];
|
|
272
324
|
try {
|
|
273
325
|
const typedErrors = await validateTypedPayloadInBlock(context, payload, block, isBridgeDestinationObservation);
|
|
@@ -275,15 +327,21 @@ var validateBridgeDestinationObservationInBlock = /* @__PURE__ */ __name(async (
|
|
|
275
327
|
errors.push(new InBlockPayloadValidationError4(payload._hash, block, payload, `validateTypedPayloadInBlock error: ${typedError}`, typedError));
|
|
276
328
|
}
|
|
277
329
|
} catch (ex) {
|
|
278
|
-
errors.push(new InBlockPayloadValidationError4(
|
|
330
|
+
errors.push(new InBlockPayloadValidationError4(
|
|
331
|
+
block?.[0]?._hash ?? ZERO_HASH8,
|
|
332
|
+
block,
|
|
333
|
+
payload,
|
|
334
|
+
`Failed validateBridgeObservationInBlock: ${ex}`,
|
|
335
|
+
ex
|
|
336
|
+
));
|
|
279
337
|
}
|
|
280
338
|
return errors;
|
|
281
|
-
}
|
|
339
|
+
};
|
|
282
340
|
|
|
283
341
|
// src/elevatedPayload/payloads/validateBridgeIntentInBlock.ts
|
|
284
342
|
import { ZERO_HASH as ZERO_HASH9 } from "@xylabs/sdk-js";
|
|
285
343
|
import { InBlockPayloadValidationError as InBlockPayloadValidationError5, isBridgeIntent } from "@xyo-network/xl1-sdk";
|
|
286
|
-
var validateBridgeIntentInBlock =
|
|
344
|
+
var validateBridgeIntentInBlock = async (context, payload, block) => {
|
|
287
345
|
const errors = [];
|
|
288
346
|
try {
|
|
289
347
|
const typedErrors = await validateTypedPayloadInBlock(context, payload, block, isBridgeIntent);
|
|
@@ -291,15 +349,21 @@ var validateBridgeIntentInBlock = /* @__PURE__ */ __name(async (context, payload
|
|
|
291
349
|
errors.push(new InBlockPayloadValidationError5(payload._hash, block, payload, `validateTypedPayloadInBlock error: ${typedError}`, typedError));
|
|
292
350
|
}
|
|
293
351
|
} catch (ex) {
|
|
294
|
-
errors.push(new InBlockPayloadValidationError5(
|
|
352
|
+
errors.push(new InBlockPayloadValidationError5(
|
|
353
|
+
block?.[0]?._hash ?? ZERO_HASH9,
|
|
354
|
+
block,
|
|
355
|
+
payload,
|
|
356
|
+
`Failed validateBridgeIntentInBlock: ${ex}`,
|
|
357
|
+
ex
|
|
358
|
+
));
|
|
295
359
|
}
|
|
296
360
|
return errors;
|
|
297
|
-
}
|
|
361
|
+
};
|
|
298
362
|
|
|
299
363
|
// src/elevatedPayload/payloads/validateBridgeSourceObservationInBlock.ts
|
|
300
364
|
import { ZERO_HASH as ZERO_HASH10 } from "@xylabs/sdk-js";
|
|
301
365
|
import { InBlockPayloadValidationError as InBlockPayloadValidationError6, isBridgeSourceObservation } from "@xyo-network/xl1-sdk";
|
|
302
|
-
var validateBridgeSourceObservationInBlock =
|
|
366
|
+
var validateBridgeSourceObservationInBlock = async (context, payload, block) => {
|
|
303
367
|
const errors = [];
|
|
304
368
|
try {
|
|
305
369
|
const typedErrors = await validateTypedPayloadInBlock(context, payload, block, isBridgeSourceObservation);
|
|
@@ -307,15 +371,21 @@ var validateBridgeSourceObservationInBlock = /* @__PURE__ */ __name(async (conte
|
|
|
307
371
|
errors.push(new InBlockPayloadValidationError6(payload._hash, block, payload, `validateTypedPayloadInBlock error: ${typedError}`, typedError));
|
|
308
372
|
}
|
|
309
373
|
} catch (ex) {
|
|
310
|
-
errors.push(new InBlockPayloadValidationError6(
|
|
374
|
+
errors.push(new InBlockPayloadValidationError6(
|
|
375
|
+
block?.[0]?._hash ?? ZERO_HASH10,
|
|
376
|
+
block,
|
|
377
|
+
payload,
|
|
378
|
+
`Failed validateBridgeObservationInBlock: ${ex}`,
|
|
379
|
+
ex
|
|
380
|
+
));
|
|
311
381
|
}
|
|
312
382
|
return errors;
|
|
313
|
-
}
|
|
383
|
+
};
|
|
314
384
|
|
|
315
385
|
// src/elevatedPayload/payloads/validateChainStakeIntentInBlock.ts
|
|
316
386
|
import { ZERO_HASH as ZERO_HASH11 } from "@xylabs/sdk-js";
|
|
317
387
|
import { InBlockPayloadValidationError as InBlockPayloadValidationError7, isChainStakeIntent } from "@xyo-network/xl1-sdk";
|
|
318
|
-
var validateChainStakeIntentInBlock =
|
|
388
|
+
var validateChainStakeIntentInBlock = async (context, payload, block) => {
|
|
319
389
|
const errors = [];
|
|
320
390
|
try {
|
|
321
391
|
const typedErrors = await validateTypedPayloadInBlock(context, payload, block, isChainStakeIntent);
|
|
@@ -323,15 +393,21 @@ var validateChainStakeIntentInBlock = /* @__PURE__ */ __name(async (context, pay
|
|
|
323
393
|
errors.push(new InBlockPayloadValidationError7(payload._hash, block, payload, `validateTypedPayloadInBlock error: ${typedError}`, typedError));
|
|
324
394
|
}
|
|
325
395
|
} catch (ex) {
|
|
326
|
-
errors.push(new InBlockPayloadValidationError7(
|
|
396
|
+
errors.push(new InBlockPayloadValidationError7(
|
|
397
|
+
block[0]?._hash ?? ZERO_HASH11,
|
|
398
|
+
block,
|
|
399
|
+
payload,
|
|
400
|
+
`validation excepted: ${ex}`,
|
|
401
|
+
ex
|
|
402
|
+
));
|
|
327
403
|
}
|
|
328
404
|
return errors;
|
|
329
|
-
}
|
|
405
|
+
};
|
|
330
406
|
|
|
331
407
|
// src/elevatedPayload/payloads/validateHashInBlock.ts
|
|
332
408
|
import { ZERO_HASH as ZERO_HASH12 } from "@xylabs/sdk-js";
|
|
333
409
|
import { InBlockPayloadValidationError as InBlockPayloadValidationError8, isHashPayload } from "@xyo-network/xl1-sdk";
|
|
334
|
-
var validateHashInBlock =
|
|
410
|
+
var validateHashInBlock = async (context, payload, block) => {
|
|
335
411
|
const errors = [];
|
|
336
412
|
try {
|
|
337
413
|
const typedErrors = await validateTypedPayloadInBlock(context, payload, block, isHashPayload);
|
|
@@ -339,16 +415,22 @@ var validateHashInBlock = /* @__PURE__ */ __name(async (context, payload, block)
|
|
|
339
415
|
errors.push(new InBlockPayloadValidationError8(payload._hash, block, payload, `validateTypedPayloadInBlock error: ${typedError}`, typedError));
|
|
340
416
|
}
|
|
341
417
|
} catch (ex) {
|
|
342
|
-
errors.push(new InBlockPayloadValidationError8(
|
|
418
|
+
errors.push(new InBlockPayloadValidationError8(
|
|
419
|
+
block?.[0]?._hash ?? ZERO_HASH12,
|
|
420
|
+
block,
|
|
421
|
+
payload,
|
|
422
|
+
`validation excepted: ${ex}`,
|
|
423
|
+
ex
|
|
424
|
+
));
|
|
343
425
|
}
|
|
344
426
|
return errors;
|
|
345
|
-
}
|
|
427
|
+
};
|
|
346
428
|
|
|
347
429
|
// src/elevatedPayload/payloads/validateSchemaInBlock.ts
|
|
348
430
|
import { ZERO_HASH as ZERO_HASH13 } from "@xylabs/sdk-js";
|
|
349
431
|
import { isSchemaPayload } from "@xyo-network/sdk-js";
|
|
350
432
|
import { InBlockPayloadValidationError as InBlockPayloadValidationError9 } from "@xyo-network/xl1-sdk";
|
|
351
|
-
var validateSchemaInBlock =
|
|
433
|
+
var validateSchemaInBlock = async (context, payload, block) => {
|
|
352
434
|
const errors = [];
|
|
353
435
|
try {
|
|
354
436
|
const typedErrors = await validateTypedPayloadInBlock(context, payload, block, isSchemaPayload);
|
|
@@ -356,15 +438,21 @@ var validateSchemaInBlock = /* @__PURE__ */ __name(async (context, payload, bloc
|
|
|
356
438
|
errors.push(new InBlockPayloadValidationError9(payload._hash, block, payload, `validateTypedPayloadInBlock error: ${typedError}`, typedError));
|
|
357
439
|
}
|
|
358
440
|
} catch (ex) {
|
|
359
|
-
errors.push(new InBlockPayloadValidationError9(
|
|
441
|
+
errors.push(new InBlockPayloadValidationError9(
|
|
442
|
+
block?.[0]?._hash ?? ZERO_HASH13,
|
|
443
|
+
block,
|
|
444
|
+
payload,
|
|
445
|
+
`Failed validateSchemaInBlock: ${ex}`,
|
|
446
|
+
ex
|
|
447
|
+
));
|
|
360
448
|
}
|
|
361
449
|
return errors;
|
|
362
|
-
}
|
|
450
|
+
};
|
|
363
451
|
|
|
364
452
|
// src/elevatedPayload/payloads/validateTimeInBlock.ts
|
|
365
453
|
import { ZERO_HASH as ZERO_HASH14 } from "@xylabs/sdk-js";
|
|
366
454
|
import { InBlockPayloadValidationError as InBlockPayloadValidationError10, isTimePayload } from "@xyo-network/xl1-sdk";
|
|
367
|
-
var validateTimeInBlock =
|
|
455
|
+
var validateTimeInBlock = async (context, payload, block) => {
|
|
368
456
|
const errors = [];
|
|
369
457
|
try {
|
|
370
458
|
const typedErrors = await validateTypedPayloadInBlock(context, payload, block, isTimePayload);
|
|
@@ -372,15 +460,21 @@ var validateTimeInBlock = /* @__PURE__ */ __name(async (context, payload, block)
|
|
|
372
460
|
errors.push(new InBlockPayloadValidationError10(payload._hash, block, payload, `validateTypedPayloadInBlock error: ${typedError}`, typedError));
|
|
373
461
|
}
|
|
374
462
|
} catch (ex) {
|
|
375
|
-
errors.push(new InBlockPayloadValidationError10(
|
|
463
|
+
errors.push(new InBlockPayloadValidationError10(
|
|
464
|
+
block?.[0]?._hash ?? ZERO_HASH14,
|
|
465
|
+
block,
|
|
466
|
+
payload,
|
|
467
|
+
`Failed validateTimeInBlock: ${ex}`,
|
|
468
|
+
ex
|
|
469
|
+
));
|
|
376
470
|
}
|
|
377
471
|
return errors;
|
|
378
|
-
}
|
|
472
|
+
};
|
|
379
473
|
|
|
380
474
|
// src/elevatedPayload/payloads/validateTransferInBlock.ts
|
|
381
475
|
import { ZERO_HASH as ZERO_HASH15 } from "@xylabs/sdk-js";
|
|
382
476
|
import { InBlockPayloadValidationError as InBlockPayloadValidationError11, isTransfer } from "@xyo-network/xl1-sdk";
|
|
383
|
-
var validateTransferInBlock =
|
|
477
|
+
var validateTransferInBlock = async (context, payload, block) => {
|
|
384
478
|
const errors = [];
|
|
385
479
|
try {
|
|
386
480
|
const typedErrors = await validateTypedPayloadInBlock(context, payload, block, isTransfer);
|
|
@@ -388,10 +482,16 @@ var validateTransferInBlock = /* @__PURE__ */ __name(async (context, payload, bl
|
|
|
388
482
|
errors.push(new InBlockPayloadValidationError11(payload._hash, block, payload, `validateTypedPayloadInBlock error: ${typedError}`, typedError));
|
|
389
483
|
}
|
|
390
484
|
} catch (ex) {
|
|
391
|
-
errors.push(new InBlockPayloadValidationError11(
|
|
485
|
+
errors.push(new InBlockPayloadValidationError11(
|
|
486
|
+
block?.[0]?._hash ?? ZERO_HASH15,
|
|
487
|
+
block,
|
|
488
|
+
payload,
|
|
489
|
+
`Failed validateTransferInBlock: ${ex}`,
|
|
490
|
+
ex
|
|
491
|
+
));
|
|
392
492
|
}
|
|
393
493
|
return errors;
|
|
394
|
-
}
|
|
494
|
+
};
|
|
395
495
|
|
|
396
496
|
// src/elevatedPayload/validatePayloadInBlock.ts
|
|
397
497
|
var payloadValidators = {
|
|
@@ -405,7 +505,7 @@ var payloadValidators = {
|
|
|
405
505
|
[TimeSchema]: validateTimeInBlock,
|
|
406
506
|
[TransferSchema]: validateTransferInBlock
|
|
407
507
|
};
|
|
408
|
-
var validatePayloadInBlock =
|
|
508
|
+
var validatePayloadInBlock = async (context, payload, block) => {
|
|
409
509
|
const errors = [];
|
|
410
510
|
try {
|
|
411
511
|
const validator = payloadValidators[payload.schema];
|
|
@@ -415,66 +515,70 @@ var validatePayloadInBlock = /* @__PURE__ */ __name(async (context, payload, blo
|
|
|
415
515
|
errors.push(new InBlockPayloadValidationError12(block?.[0]?._hash ?? ZERO_HASH16, block, payload, `Unsupported payload schema: ${payload.schema}`));
|
|
416
516
|
}
|
|
417
517
|
} catch (ex) {
|
|
418
|
-
const error2 = new InBlockPayloadValidationError12(
|
|
518
|
+
const error2 = new InBlockPayloadValidationError12(
|
|
519
|
+
block?.[0]?._hash ?? ZERO_HASH16,
|
|
520
|
+
block,
|
|
521
|
+
payload,
|
|
522
|
+
`validation excepted: ${ex}`,
|
|
523
|
+
ex
|
|
524
|
+
);
|
|
419
525
|
errors.push(error2);
|
|
420
526
|
}
|
|
421
527
|
return errors;
|
|
422
|
-
}
|
|
528
|
+
};
|
|
423
529
|
|
|
424
530
|
// src/hydratedBlock/validators/Payloads.ts
|
|
425
|
-
var PayloadsInBlockValidator =
|
|
531
|
+
var PayloadsInBlockValidator = async (context, [block, payloads]) => {
|
|
426
532
|
const errors = [];
|
|
427
533
|
try {
|
|
428
534
|
const payloadMap = {};
|
|
429
535
|
for (const payload of payloads) {
|
|
430
536
|
payloadMap[payload._hash] = payload;
|
|
431
537
|
}
|
|
432
|
-
const remainingPayloads = {
|
|
433
|
-
...payloadMap
|
|
434
|
-
};
|
|
538
|
+
const remainingPayloads = { ...payloadMap };
|
|
435
539
|
for (let i = 0; i < block.payload_hashes.length; i++) {
|
|
436
540
|
const hash = block.payload_hashes[i];
|
|
437
541
|
const schema = block.payload_schemas[i];
|
|
438
542
|
const payload = payloadMap[hash];
|
|
439
543
|
if (payload) {
|
|
440
|
-
const payloadInBlockErrors = await validatePayloadInBlock(context, payload, [
|
|
441
|
-
block,
|
|
442
|
-
payloads
|
|
443
|
-
]);
|
|
544
|
+
const payloadInBlockErrors = await validatePayloadInBlock(context, payload, [block, payloads]);
|
|
444
545
|
for (const payloadInBlockError of payloadInBlockErrors) {
|
|
445
|
-
errors.push(new HydratedBlockValidationError(
|
|
446
|
-
block,
|
|
447
|
-
payloads
|
|
448
|
-
|
|
546
|
+
errors.push(new HydratedBlockValidationError(
|
|
547
|
+
block?._hash ?? ZERO_HASH17,
|
|
548
|
+
[block, payloads],
|
|
549
|
+
`validatePayloadInBlock error: ${payloadInBlockError}`,
|
|
550
|
+
payloadInBlockError
|
|
551
|
+
));
|
|
449
552
|
}
|
|
450
553
|
delete remainingPayloads[hash];
|
|
451
554
|
} else {
|
|
452
|
-
errors.push(new HydratedBlockValidationError(block?._hash ?? ZERO_HASH17, [
|
|
453
|
-
block,
|
|
454
|
-
payloads
|
|
455
|
-
], `missing payload ${hash} ${schema}`));
|
|
555
|
+
errors.push(new HydratedBlockValidationError(block?._hash ?? ZERO_HASH17, [block, payloads], `missing payload ${hash} ${schema}`));
|
|
456
556
|
}
|
|
457
557
|
}
|
|
458
558
|
if (Object.keys(remainingPayloads).length > 0) {
|
|
459
|
-
errors.push(new HydratedBlockValidationError(block?._hash ?? ZERO_HASH17, [
|
|
460
|
-
block,
|
|
461
|
-
payloads
|
|
462
|
-
], `extra payloads ${Object.keys(payloadMap).join(", ")}`));
|
|
559
|
+
errors.push(new HydratedBlockValidationError(block?._hash ?? ZERO_HASH17, [block, payloads], `extra payloads ${Object.keys(payloadMap).join(", ")}`));
|
|
463
560
|
}
|
|
464
561
|
} catch (ex) {
|
|
465
|
-
errors.push(new HydratedBlockValidationError(
|
|
466
|
-
block,
|
|
467
|
-
payloads
|
|
468
|
-
|
|
562
|
+
errors.push(new HydratedBlockValidationError(
|
|
563
|
+
block?._hash ?? ZERO_HASH17,
|
|
564
|
+
[block, payloads],
|
|
565
|
+
`Failed PayloadsInBlockValidator: ${ex}`,
|
|
566
|
+
ex
|
|
567
|
+
));
|
|
469
568
|
}
|
|
470
569
|
return errors;
|
|
471
|
-
}
|
|
570
|
+
};
|
|
472
571
|
|
|
473
572
|
// src/hydratedBlock/validateHydratedBlock.ts
|
|
474
|
-
var validateHydratedBlock =
|
|
573
|
+
var validateHydratedBlock = async (context, hydratedBlock, chainIdAtBlockNumber, additionalValidators = [], additionalBlockValidators = []) => {
|
|
475
574
|
const errors = [];
|
|
476
575
|
try {
|
|
477
|
-
const validateBlockErrors = await validateBlock(
|
|
576
|
+
const validateBlockErrors = await validateBlock(
|
|
577
|
+
context,
|
|
578
|
+
hydratedBlock[0],
|
|
579
|
+
chainIdAtBlockNumber ? await chainIdAtBlockNumber(hydratedBlock[0].block) : void 0,
|
|
580
|
+
additionalBlockValidators
|
|
581
|
+
);
|
|
478
582
|
for (const validateBlockError of validateBlockErrors) {
|
|
479
583
|
errors.push(new HydratedBlockValidationError2(hydratedBlock[0]._hash, hydratedBlock, `validateBlock error: ${validateBlockError}`, validateBlockError));
|
|
480
584
|
}
|
|
@@ -488,10 +592,15 @@ var validateHydratedBlock = /* @__PURE__ */ __name(async (context, hydratedBlock
|
|
|
488
592
|
];
|
|
489
593
|
errors.push(...(await Promise.all(validators.map((v) => v(context, hydratedBlock, chainIdAtBlockNumber)))).flat());
|
|
490
594
|
} catch (ex) {
|
|
491
|
-
errors.push(new HydratedBlockValidationError2(
|
|
595
|
+
errors.push(new HydratedBlockValidationError2(
|
|
596
|
+
hydratedBlock?.[0]?._hash ?? ZERO_HASH18,
|
|
597
|
+
hydratedBlock,
|
|
598
|
+
`Failed validateHydratedBlock: ${ex}`,
|
|
599
|
+
ex
|
|
600
|
+
));
|
|
492
601
|
}
|
|
493
602
|
return errors;
|
|
494
|
-
}
|
|
603
|
+
};
|
|
495
604
|
|
|
496
605
|
// src/hydratedBlockState/validateHydratedBlockState.ts
|
|
497
606
|
import { spanRootAsync as spanRootAsync2, ZERO_HASH as ZERO_HASH20 } from "@xylabs/sdk-js";
|
|
@@ -499,15 +608,28 @@ import { HydratedBlockStateValidationError as HydratedBlockStateValidationError2
|
|
|
499
608
|
|
|
500
609
|
// src/hydratedBlockState/validators/RequiredBalance.ts
|
|
501
610
|
import { spanRootAsync, ZERO_HASH as ZERO_HASH19 } from "@xylabs/sdk-js";
|
|
502
|
-
import {
|
|
503
|
-
|
|
611
|
+
import {
|
|
612
|
+
AttoXL1,
|
|
613
|
+
HydratedBlockStateValidationError,
|
|
614
|
+
isSignedTransactionBoundWitnessWithHashMeta,
|
|
615
|
+
netBalancesForPayloads,
|
|
616
|
+
XYO_ZERO_ADDRESS
|
|
617
|
+
} from "@xyo-network/xl1-sdk";
|
|
618
|
+
function offendingTransactionHashesForAddress(block, address) {
|
|
619
|
+
const hashes = [];
|
|
620
|
+
for (const payload of block[1]) {
|
|
621
|
+
if (isSignedTransactionBoundWitnessWithHashMeta(payload) && payload.from === address) {
|
|
622
|
+
hashes.push(payload._hash);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
return hashes;
|
|
626
|
+
}
|
|
627
|
+
var RequiredBalanceBlockStateValidator = async (context, block) => {
|
|
504
628
|
return await spanRootAsync("RequiredBalanceBlockStateValidator", async () => {
|
|
505
629
|
const errors = [];
|
|
506
630
|
let chainId = "00";
|
|
507
631
|
try {
|
|
508
|
-
const netBalances = netBalancesForPayloads({
|
|
509
|
-
singletons: {}
|
|
510
|
-
}, block[1]);
|
|
632
|
+
const netBalances = netBalancesForPayloads({ singletons: {} }, block[1]);
|
|
511
633
|
const netBalanceAddresses = Object.keys(netBalances);
|
|
512
634
|
const requiredBalances = {};
|
|
513
635
|
for (const address of netBalanceAddresses) {
|
|
@@ -516,32 +638,45 @@ var RequiredBalanceBlockStateValidator = /* @__PURE__ */ __name(async (context,
|
|
|
516
638
|
}
|
|
517
639
|
}
|
|
518
640
|
const previous = block[0].previous;
|
|
519
|
-
if (previous === null) return [
|
|
520
|
-
|
|
521
|
-
|
|
641
|
+
if (previous === null) return [new HydratedBlockStateValidationError(
|
|
642
|
+
block?.[0]?._hash ?? ZERO_HASH19,
|
|
643
|
+
"00",
|
|
644
|
+
block,
|
|
645
|
+
"Insufficient funds because first block"
|
|
646
|
+
)];
|
|
522
647
|
chainId = await context.chainIdAtBlockNumber(block[0].block);
|
|
523
648
|
await spanRootAsync("RequiredBalanceBlockStateValidator|balancesLoop", async () => {
|
|
524
649
|
for (const [address, reqBalance] of Object.entries(requiredBalances)) {
|
|
525
|
-
const result = await context.accountBalance.accountBalances([
|
|
526
|
-
address
|
|
527
|
-
], {
|
|
528
|
-
head: previous
|
|
529
|
-
});
|
|
650
|
+
const result = await context.accountBalance.accountBalances([address], { head: previous });
|
|
530
651
|
const balance = result[address] ?? AttoXL1(0n);
|
|
531
652
|
if (address !== XYO_ZERO_ADDRESS && reqBalance > balance) {
|
|
532
|
-
|
|
653
|
+
const offendingTransactionHashes = offendingTransactionHashesForAddress(block, address);
|
|
654
|
+
errors.push(new HydratedBlockStateValidationError(
|
|
655
|
+
block?.[0]?._hash ?? ZERO_HASH19,
|
|
656
|
+
chainId,
|
|
657
|
+
block,
|
|
658
|
+
`insufficient balance for ${address} ${balance} < ${requiredBalances[address]}`,
|
|
659
|
+
void 0,
|
|
660
|
+
offendingTransactionHashes.length > 0 ? offendingTransactionHashes : void 0
|
|
661
|
+
));
|
|
533
662
|
}
|
|
534
663
|
}
|
|
535
664
|
}, context);
|
|
536
665
|
} catch (ex) {
|
|
537
|
-
errors.push(new HydratedBlockStateValidationError(
|
|
666
|
+
errors.push(new HydratedBlockStateValidationError(
|
|
667
|
+
block?.[0]?._hash ?? ZERO_HASH19,
|
|
668
|
+
chainId,
|
|
669
|
+
block,
|
|
670
|
+
`Failed RequiredBalanceBlockStateValidator: ${ex}`,
|
|
671
|
+
ex
|
|
672
|
+
));
|
|
538
673
|
}
|
|
539
674
|
return await Promise.resolve(errors);
|
|
540
675
|
}, context);
|
|
541
|
-
}
|
|
676
|
+
};
|
|
542
677
|
|
|
543
678
|
// src/hydratedBlockState/validateHydratedBlockState.ts
|
|
544
|
-
var validateHydratedBlockState =
|
|
679
|
+
var validateHydratedBlockState = async (context, hydratedBlock, additionalValidators = []) => {
|
|
545
680
|
return await spanRootAsync2("validateHydratedBlockState", async () => {
|
|
546
681
|
const errors = [];
|
|
547
682
|
let chainId;
|
|
@@ -549,7 +684,13 @@ var validateHydratedBlockState = /* @__PURE__ */ __name(async (context, hydrated
|
|
|
549
684
|
chainId = await context.chainIdAtBlockNumber(hydratedBlock[0].block);
|
|
550
685
|
const validateHydratedBlockErrors = await validateHydratedBlock(context, hydratedBlock, context.chainIdAtBlockNumber);
|
|
551
686
|
for (const validateHydratedBlockError of validateHydratedBlockErrors) {
|
|
552
|
-
errors.push(new HydratedBlockStateValidationError2(
|
|
687
|
+
errors.push(new HydratedBlockStateValidationError2(
|
|
688
|
+
hydratedBlock[0]._hash,
|
|
689
|
+
chainId,
|
|
690
|
+
hydratedBlock,
|
|
691
|
+
`validateBlock error: ${validateHydratedBlockError}`,
|
|
692
|
+
validateHydratedBlockError
|
|
693
|
+
));
|
|
553
694
|
}
|
|
554
695
|
const validators = [
|
|
555
696
|
RequiredBalanceBlockStateValidator,
|
|
@@ -557,11 +698,17 @@ var validateHydratedBlockState = /* @__PURE__ */ __name(async (context, hydrated
|
|
|
557
698
|
];
|
|
558
699
|
errors.push(...(await Promise.all(validators.map((v) => v(context, hydratedBlock)))).flat());
|
|
559
700
|
} catch (ex) {
|
|
560
|
-
errors.push(new HydratedBlockStateValidationError2(
|
|
701
|
+
errors.push(new HydratedBlockStateValidationError2(
|
|
702
|
+
hydratedBlock?.[0]?._hash ?? ZERO_HASH20,
|
|
703
|
+
chainId ?? "00",
|
|
704
|
+
hydratedBlock,
|
|
705
|
+
`Failed validateHydratedBlockState: ${ex}`,
|
|
706
|
+
ex
|
|
707
|
+
));
|
|
561
708
|
}
|
|
562
709
|
return errors;
|
|
563
710
|
}, context);
|
|
564
|
-
}
|
|
711
|
+
};
|
|
565
712
|
|
|
566
713
|
// src/hydratedTransactionState/validateHydratedTransactionState.ts
|
|
567
714
|
import { spanRootAsync as spanRootAsync4, ZERO_HASH as ZERO_HASH22 } from "@xylabs/sdk-js";
|
|
@@ -569,45 +716,73 @@ import { HydratedTransactionValidationError as HydratedTransactionValidationErro
|
|
|
569
716
|
|
|
570
717
|
// src/hydratedTransactionState/validators/RequiredBalance.ts
|
|
571
718
|
import { spanRootAsync as spanRootAsync3, ZERO_HASH as ZERO_HASH21 } from "@xylabs/sdk-js";
|
|
572
|
-
import {
|
|
573
|
-
|
|
719
|
+
import {
|
|
720
|
+
AttoXL1 as AttoXL12,
|
|
721
|
+
HydratedTransactionValidationError,
|
|
722
|
+
HydratedTransactionWrapper,
|
|
723
|
+
netBalancesForPayloads as netBalancesForPayloads2,
|
|
724
|
+
transactionRequiredGas,
|
|
725
|
+
XYO_ZERO_ADDRESS as XYO_ZERO_ADDRESS2
|
|
726
|
+
} from "@xyo-network/xl1-sdk";
|
|
727
|
+
async function balanceShortfallErrors(context, tx, requiredBalances) {
|
|
728
|
+
const errors = [];
|
|
729
|
+
const requiredAddresses = Object.keys(requiredBalances);
|
|
730
|
+
if (requiredAddresses.length === 0) return errors;
|
|
731
|
+
const headBlock = (await context.blockViewer.currentBlock())[0];
|
|
732
|
+
const balances = await context.accountBalanceViewer.accountBalances(
|
|
733
|
+
requiredAddresses,
|
|
734
|
+
{ head: headBlock._hash }
|
|
735
|
+
);
|
|
736
|
+
for (const address of requiredAddresses) {
|
|
737
|
+
const reqBalance = requiredBalances[address];
|
|
738
|
+
const balance = balances[address] ?? AttoXL12(0n);
|
|
739
|
+
if (address !== XYO_ZERO_ADDRESS2 && reqBalance > balance) {
|
|
740
|
+
errors.push(new HydratedTransactionValidationError(
|
|
741
|
+
tx?.[0]?._hash ?? ZERO_HASH21,
|
|
742
|
+
tx,
|
|
743
|
+
`insufficient balance for ${address} ${balance} < ${reqBalance}`
|
|
744
|
+
));
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
return errors;
|
|
748
|
+
}
|
|
749
|
+
var RequiredBalanceTransactionStateValidator = async (context, tx) => {
|
|
574
750
|
return await spanRootAsync3("RequiredBalanceTransactionStateValidator", async () => {
|
|
575
751
|
const errors = [];
|
|
576
752
|
try {
|
|
577
|
-
const netBalances = netBalancesForPayloads2({
|
|
578
|
-
singletons: {}
|
|
579
|
-
}, tx[1]);
|
|
753
|
+
const netBalances = netBalancesForPayloads2({ singletons: {} }, tx[1]);
|
|
580
754
|
const wrapper = await HydratedTransactionWrapper.parse(tx);
|
|
581
755
|
const from = wrapper.boundWitness.from;
|
|
582
|
-
const
|
|
756
|
+
const gasRequired = transactionRequiredGas(tx);
|
|
757
|
+
if (wrapper.fees.gasLimit < gasRequired) {
|
|
758
|
+
errors.push(new HydratedTransactionValidationError(
|
|
759
|
+
tx?.[0]?._hash ?? ZERO_HASH21,
|
|
760
|
+
tx,
|
|
761
|
+
`fees.gasLimit ${wrapper.fees.gasLimit} < required gas ${gasRequired}`
|
|
762
|
+
));
|
|
763
|
+
}
|
|
764
|
+
const gasCost = gasRequired * wrapper.fees.gasPrice;
|
|
583
765
|
const baseCost = wrapper.fees.base;
|
|
584
766
|
netBalances[from] = (netBalances[from] ?? 0n) - gasCost - baseCost;
|
|
585
767
|
const requiredBalances = {};
|
|
586
768
|
for (const [address, net] of Object.entries(netBalances)) {
|
|
587
769
|
if (net < 0n) requiredBalances[address] = -net;
|
|
588
770
|
}
|
|
589
|
-
|
|
590
|
-
if (requiredAddresses.length === 0) return errors;
|
|
591
|
-
const headBlock = (await context.blockViewer.currentBlock())[0];
|
|
592
|
-
const balances = await context.accountBalanceViewer.accountBalances(requiredAddresses, {
|
|
593
|
-
head: headBlock._hash
|
|
594
|
-
});
|
|
595
|
-
for (const address of requiredAddresses) {
|
|
596
|
-
const reqBalance = requiredBalances[address];
|
|
597
|
-
const balance = balances[address] ?? AttoXL12(0n);
|
|
598
|
-
if (address !== XYO_ZERO_ADDRESS2 && reqBalance > balance) {
|
|
599
|
-
errors.push(new HydratedTransactionValidationError(tx?.[0]?._hash ?? ZERO_HASH21, tx, `insufficient balance for ${address} ${balance} < ${reqBalance}`));
|
|
600
|
-
}
|
|
601
|
-
}
|
|
771
|
+
errors.push(...await balanceShortfallErrors(context, tx, requiredBalances));
|
|
602
772
|
} catch (ex) {
|
|
603
|
-
errors.push(new HydratedTransactionValidationError(
|
|
773
|
+
errors.push(new HydratedTransactionValidationError(
|
|
774
|
+
tx?.[0]?._hash ?? ZERO_HASH21,
|
|
775
|
+
tx,
|
|
776
|
+
`Failed RequiredBalanceTransactionStateValidator: ${ex}`,
|
|
777
|
+
ex
|
|
778
|
+
));
|
|
604
779
|
}
|
|
605
780
|
return errors;
|
|
606
781
|
}, context);
|
|
607
|
-
}
|
|
782
|
+
};
|
|
608
783
|
|
|
609
784
|
// src/hydratedTransactionState/validateHydratedTransactionState.ts
|
|
610
|
-
var validateHydratedTransactionState =
|
|
785
|
+
var validateHydratedTransactionState = async (context, tx) => {
|
|
611
786
|
return await spanRootAsync4("validateHydratedTransactionState", async () => {
|
|
612
787
|
const errors = [];
|
|
613
788
|
try {
|
|
@@ -616,11 +791,16 @@ var validateHydratedTransactionState = /* @__PURE__ */ __name(async (context, tx
|
|
|
616
791
|
];
|
|
617
792
|
errors.push(...(await Promise.all(validators.map((v) => v(context, tx)))).flat());
|
|
618
793
|
} catch (ex) {
|
|
619
|
-
errors.push(new HydratedTransactionValidationError2(
|
|
794
|
+
errors.push(new HydratedTransactionValidationError2(
|
|
795
|
+
tx?.[0]?._hash ?? ZERO_HASH22,
|
|
796
|
+
tx,
|
|
797
|
+
`Failed validateHydratedTransactionState: ${ex}`,
|
|
798
|
+
ex
|
|
799
|
+
));
|
|
620
800
|
}
|
|
621
801
|
return errors;
|
|
622
802
|
}, context);
|
|
623
|
-
}
|
|
803
|
+
};
|
|
624
804
|
export {
|
|
625
805
|
BlockAllowedPayloadSchemasValidator,
|
|
626
806
|
BlockBoundWitnessValidator,
|
|
@@ -635,4 +815,4 @@ export {
|
|
|
635
815
|
validateHydratedBlockState,
|
|
636
816
|
validateHydratedTransactionState
|
|
637
817
|
};
|
|
638
|
-
//# sourceMappingURL=index.mjs.map
|
|
818
|
+
//# sourceMappingURL=index.mjs.map
|