@xyo-network/xl1-protocol-sdk 1.25.8 → 1.25.9

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.
@@ -4312,7 +4312,9 @@ import {
4312
4312
  ChainContractViewerMoniker as ChainContractViewerMoniker4,
4313
4313
  FinalizationViewerMoniker as FinalizationViewerMoniker4,
4314
4314
  isHydratedBlockWithHashMeta,
4315
+ isHydratedTransactionWithHashMeta,
4315
4316
  isSignedHydratedBlockWithHashMeta,
4317
+ isSignedHydratedTransactionWithHashMeta,
4316
4318
  MempoolRunnerMoniker
4317
4319
  } from "@xyo-network/xl1-protocol";
4318
4320
  var SimpleMempoolRunner = class extends AbstractCreatableProvider {
@@ -4363,7 +4365,7 @@ var SimpleMempoolRunner = class extends AbstractCreatableProvider {
4363
4365
  });
4364
4366
  this.logger?.info(`Starting prunePendingBlocks with batchSize=${batchSize}, maxPrune=${maxPrune}, maxCheck=${maxCheck}`);
4365
4367
  while (batch.length > 0 && pruned < maxPrune && total < maxCheck) {
4366
- const blocksAndBundles = await this.simpleValidationCheck(batch);
4368
+ const blocksAndBundles = await this.simpleBlockValidationCheck(batch);
4367
4369
  const blocks = blocksAndBundles.map(([b]) => b);
4368
4370
  const bundles = blocksAndBundles.map(([_, p]) => p);
4369
4371
  let valid = blocks.map((b) => !!b);
@@ -4405,8 +4407,62 @@ var SimpleMempoolRunner = class extends AbstractCreatableProvider {
4405
4407
  this.logger?.info(`prunePendingBlocks completed: pruned=${pruned}, totalChecked=${total}`);
4406
4408
  return [pruned, total];
4407
4409
  }
4408
- prunePendingTransactions(_options) {
4409
- throw new Error("Method not implemented.");
4410
+ async prunePendingTransactions({
4411
+ batchSize = 10,
4412
+ maxPrune = 1e3,
4413
+ maxCheck = 1e3
4414
+ } = {}) {
4415
+ let total = 0;
4416
+ let pruned = 0;
4417
+ let cursor;
4418
+ let batch = await this.pendingTransactionsArchivist.next({
4419
+ limit: batchSize,
4420
+ cursor,
4421
+ order: "desc"
4422
+ });
4423
+ this.logger?.info(`Starting prunePendingTransactions with batchSize=${batchSize}, maxPrune=${maxPrune}, maxCheck=${maxCheck}`);
4424
+ while (batch.length > 0 && pruned < maxPrune && total < maxCheck) {
4425
+ const transactionsAndBundles = await this.simpleTransactionValidationCheck(batch);
4426
+ const transactions = transactionsAndBundles.map(([t]) => t);
4427
+ const bundles = transactionsAndBundles.map(([_, p]) => p);
4428
+ let valid = transactions.map((t) => !!t);
4429
+ let remainingTransactionMap = [];
4430
+ let remainingTransactions = transactions.map((t, i) => {
4431
+ if (t) {
4432
+ remainingTransactionMap.push(i);
4433
+ return t;
4434
+ }
4435
+ }).filter(exists8);
4436
+ assertEx38(
4437
+ remainingTransactionMap.length === remainingTransactions.length,
4438
+ () => `remainingTransactionMap length should match remainingTransactions length [${remainingTransactionMap.length}/${remainingTransactions.length}]`
4439
+ );
4440
+ const validationResults = remainingTransactions.map((t) => t);
4441
+ for (const [i, r] of validationResults.entries()) {
4442
+ const validated = isHydratedTransactionWithHashMeta(r);
4443
+ if (!validated) {
4444
+ this.logger?.info(`Pruning transaction ${bundles[remainingTransactionMap[i]]._hash} during transaction validation`);
4445
+ this.logger?.info(` - validation result: ${JSON.stringify(r, null, 2)}`);
4446
+ }
4447
+ valid[remainingTransactionMap[i]] = validated;
4448
+ }
4449
+ const pruneHashes = bundles.map((p, i) => {
4450
+ if (!valid[i]) {
4451
+ return p._hash;
4452
+ }
4453
+ }).filter(exists8);
4454
+ pruned += pruneHashes.length;
4455
+ total += batch.length;
4456
+ await this.pendingTransactionsArchivist.delete(pruneHashes);
4457
+ cursor = batch.at(-1)?._sequence;
4458
+ batch = cursor ? await this.pendingTransactionsArchivist.next({
4459
+ limit: batchSize,
4460
+ cursor,
4461
+ order: "desc"
4462
+ }) : [];
4463
+ }
4464
+ this.logger?.info(`prunePendingTransactions completed: pruned=${pruned}, totalChecked=${total}`);
4465
+ return [pruned, total];
4410
4466
  }
4411
4467
  async submitBlocks(blocks) {
4412
4468
  const bundles = await Promise.all(blocks.map(async ([bw, payloads]) => {
@@ -4428,7 +4484,7 @@ var SimpleMempoolRunner = class extends AbstractCreatableProvider {
4428
4484
  const inserted = await this.pendingTransactionsArchivist.insert(bundles);
4429
4485
  return inserted.map((p) => p._hash);
4430
4486
  }
4431
- async simpleValidationCheck(payloads) {
4487
+ async simpleBlockValidationCheck(payloads) {
4432
4488
  const headNumber = await this.finalizationViewer.headNumber();
4433
4489
  const chainId = await this.chainContractViewer.chainId();
4434
4490
  const blockBundles = await Promise.all(payloads.map(async (p) => {
@@ -4451,6 +4507,38 @@ var SimpleMempoolRunner = class extends AbstractCreatableProvider {
4451
4507
  return [validatedBlock, bundle3];
4452
4508
  });
4453
4509
  }
4510
+ async simpleTransactionValidationCheck(payloads) {
4511
+ const headNumber = await this.finalizationViewer.headNumber();
4512
+ const chainId = await this.chainContractViewer.chainId();
4513
+ const transactionBundles = await Promise.all(payloads.map(async (p) => {
4514
+ return [isPayloadBundle(p) ? await bundledPayloadToHydratedTransaction(p) : void 0, p];
4515
+ }));
4516
+ return await Promise.all(transactionBundles.map(async ([transaction, bundle3]) => {
4517
+ const transactionCheckPassed = !!transaction;
4518
+ const chainIdPassed = transactionCheckPassed ? transaction[0].chain === chainId : false;
4519
+ const expPassed = transactionCheckPassed ? transaction[0].exp > headNumber : false;
4520
+ const typeCheckPassed = transactionCheckPassed ? isSignedHydratedTransactionWithHashMeta(transaction) : false;
4521
+ let opcodeCheckPassed = false;
4522
+ try {
4523
+ await validateTransactionsOpcodes(transaction ? [transaction] : []);
4524
+ opcodeCheckPassed = true;
4525
+ } catch (e) {
4526
+ this.logger?.info(`Opcode validation failed for transaction ${bundle3._hash} during simpleValidationCheck`);
4527
+ this.logger?.info(` - error: ${e.message}`);
4528
+ }
4529
+ const validationPassed = transactionCheckPassed && chainIdPassed && expPassed && typeCheckPassed && opcodeCheckPassed;
4530
+ const validatedTransaction = validationPassed ? transaction : void 0;
4531
+ if (!validationPassed) {
4532
+ this.logger?.info(`Pruning block bundle ${bundle3._hash} during simpleValidationCheck`);
4533
+ this.logger?.info(` - chainId match: ${chainIdPassed}`);
4534
+ this.logger?.info(` - exp check: ${expPassed}`);
4535
+ this.logger?.info(` - opcode check: ${opcodeCheckPassed}`);
4536
+ this.logger?.info(` - typeCheck: ${typeCheckPassed}`);
4537
+ this.logger?.info(` - bundle hash: ${bundle3._hash}`);
4538
+ }
4539
+ return [validatedTransaction, bundle3];
4540
+ }));
4541
+ }
4454
4542
  };
4455
4543
  __publicField(SimpleMempoolRunner, "defaultMoniker", MempoolRunnerMoniker);
4456
4544
  __publicField(SimpleMempoolRunner, "dependencies", [FinalizationViewerMoniker4, BlockValidationViewerMoniker2, ChainContractViewerMoniker4]);