Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 | 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 23x 23x 23x 23x 23x 10001x 10001x 10001x 3x 3x 4x 4x 4x 4x 4x 4x 4x 4x 4x 10027x 50135x 1220x 2x 3x 3x 3x 12x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 10014x 3x 10011x 1x 10010x 10010x 10010x 10010x 5x 10010x 41x 41x 41x 30x 30x 30x 30x 20x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 89x 401x 401x 27x 27x 27x 27x 27x 27x 27x 29x 10x 19x 19x 19x 19x 19x 19x 19x 180x 19x 153x 19x 19x | /* * Copyright © 2019 Lisk Foundation * * See the LICENSE file at the top-level directory of this distribution * for licensing information. * * Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, * no part of this software, including this file, may be copied, modified, * propagated, or distributed except according to the terms contained in the * LICENSE file. * * Removal or modification of this copyright notice is prohibited. * */ import { EventEmitter } from 'events'; import { CheckerFunction, CheckTransactionsResponseWithPassAndFail, CheckTransactionsResponseWithPassFailAndPending, checkTransactionsWithPassAndFail, checkTransactionsWithPassFailAndPending, Status, TransactionResponse, } from './check_transactions'; import { Job } from './job'; import { Queue } from './queue'; import * as queueCheckers from './queue_checkers'; export interface TransactionObject { readonly id: string; receivedAt?: Date; readonly asset: { [key: string]: string | number | ReadonlyArray<string> | undefined; }; readonly senderPublicKey: string; signatures?: ReadonlyArray<string>; readonly type: number; containsUniqueData?: boolean; verifiedOnce?: boolean; } export interface SignatureObject { transactionId: string; signature: string; publicKey: string; } export interface TransactionFunctions { isExpired(date: Date): boolean; verifyAgainstOtherTransactions( otherTransactions: ReadonlyArray<Transaction>, ): boolean; addVerifiedSignature(signature: string): TransactionResponse; isReady(): boolean; } export interface TransactionPoolConfiguration { readonly expireTransactionsInterval: number; readonly maxTransactionsPerQueue: number; readonly receivedTransactionsLimitPerProcessing: number; readonly receivedTransactionsProcessingInterval: number; readonly validatedTransactionsLimitPerProcessing: number; readonly validatedTransactionsProcessingInterval: number; readonly verifiedTransactionsLimitPerProcessing: number; readonly verifiedTransactionsProcessingInterval: number; readonly pendingTransactionsProcessingLimit: number; } export interface AddTransactionResult { readonly alreadyExists: boolean; readonly isFull: boolean; readonly queueName: QueueNames; } interface TransactionPoolDependencies { processTransactions: CheckerFunction; validateTransactions: CheckerFunction; verifyTransactions: CheckerFunction; } type TransactionPoolOptions = TransactionPoolConfiguration & TransactionPoolDependencies; export type Transaction = TransactionObject & TransactionFunctions; export type QueueNames = | 'received' | 'validated' | 'verified' | 'pending' | 'ready'; interface Queues { readonly [queue: string]: Queue; } const DEFAULT_PENDING_TRANSACTIONS_PROCESSING_LIMIT = 5; const DEFAULT_EXPIRE_TRANSACTION_INTERVAL = 30000; const DEFAULT_MAX_TRANSACTIONS_PER_QUEUE = 1000; const DEFAULT_RECEIVED_TRANSACTIONS_PROCESSING_INTERVAL = 30000; const DEFAULT_RECEIVED_TRANSACTIONS_LIMIT_PER_PROCESSING = 100; const DEFAULT_VALIDATED_TRANSACTIONS_PROCESSING_INTERVAL = 30000; const DEFAULT_VALIDATED_TRANSACTIONS_LIMIT_PER_PROCESSING = 100; const DEFAULT_VERIFIED_TRANSACTIONS_PROCESSING_INTERVAL = 30000; const DEFAULT_VERIFIED_TRANSACTIONS_LIMIT_PER_PROCESSING = 100; export const EVENT_ADDED_TRANSACTIONS = 'transactionsAdded'; export const EVENT_REMOVED_TRANSACTIONS = 'transactionsRemoved'; export const EVENT_VERIFIED_TRANSACTION_ONCE = 'transactionVerifiedOnce'; export const ACTION_ADD_VERIFIED_REMOVED_TRANSACTIONS = 'addVerifiedRemovedTransactions'; export const ACTION_REMOVE_CONFIRMED_TRANSACTIONS = 'removeConfirmedTransactions'; export const ACTION_ADD_TRANSACTIONS = 'addTransactions'; export const ACTION_EXPIRE_TRANSACTIONS = 'expireTransactions'; export const ACTION_PROCESS_VERIFIED_TRANSACTIONS = 'processVerifiedTransactions'; export const ACTION_VALIDATE_RECEIVED_TRANSACTIONS = 'validateReceivedTransactions'; export const ACTION_VERIFY_VALIDATED_TRANSACTIONS = 'verifyValidatedTransactions'; export const ACTION_ADD_VERIFIED_TRANSACTIONS = 'addVerifiedTransactions'; export const ACTION_ADD_PENDING_TRANSACTIONS = 'addPendingTransactions'; export class TransactionPool extends EventEmitter { private readonly _pendingTransactionsProcessingLimit: number; private readonly _expireTransactionsInterval: number; private readonly _expireTransactionsJob: Job<ReadonlyArray<Transaction>>; private readonly _maxTransactionsPerQueue: number; private readonly _queues: Queues; private readonly _receivedTransactionsProcessingInterval: number; private readonly _receivedTransactionsProcessingLimitPerInterval: number; private readonly _validatedTransactionsProcessingInterval: number; private readonly _validatedTransactionsProcessingLimitPerInterval: number; private readonly _verifiedTransactionsProcessingInterval: number; private readonly _verifiedTransactionsProcessingLimitPerInterval: number; private readonly _validateTransactions: CheckerFunction; private readonly _validateTransactionsJob: Job< CheckTransactionsResponseWithPassAndFail >; private readonly _verifyTransactions: CheckerFunction; private readonly _verifyTransactionsJob: Job< CheckTransactionsResponseWithPassAndFail >; private readonly _processTransactions: CheckerFunction; private readonly _processTransactionsJob: Job< CheckTransactionsResponseWithPassAndFail >; public constructor({ expireTransactionsInterval = DEFAULT_EXPIRE_TRANSACTION_INTERVAL, maxTransactionsPerQueue = DEFAULT_MAX_TRANSACTIONS_PER_QUEUE, receivedTransactionsProcessingInterval = DEFAULT_RECEIVED_TRANSACTIONS_PROCESSING_INTERVAL, receivedTransactionsLimitPerProcessing = DEFAULT_RECEIVED_TRANSACTIONS_LIMIT_PER_PROCESSING, validatedTransactionsProcessingInterval = DEFAULT_VALIDATED_TRANSACTIONS_PROCESSING_INTERVAL, validatedTransactionsLimitPerProcessing = DEFAULT_VALIDATED_TRANSACTIONS_LIMIT_PER_PROCESSING, verifiedTransactionsProcessingInterval = DEFAULT_VERIFIED_TRANSACTIONS_PROCESSING_INTERVAL, verifiedTransactionsLimitPerProcessing = DEFAULT_VERIFIED_TRANSACTIONS_LIMIT_PER_PROCESSING, pendingTransactionsProcessingLimit = DEFAULT_PENDING_TRANSACTIONS_PROCESSING_LIMIT, validateTransactions, verifyTransactions, processTransactions, }: TransactionPoolOptions) { super(); this._maxTransactionsPerQueue = maxTransactionsPerQueue; this._pendingTransactionsProcessingLimit = pendingTransactionsProcessingLimit; this._queues = { received: new Queue(), validated: new Queue(), verified: new Queue(), pending: new Queue(), ready: new Queue(), }; this._expireTransactionsInterval = expireTransactionsInterval; this._expireTransactionsJob = new Job( this.expireTransactions.bind(this), this._expireTransactionsInterval, ); // tslint:disable-next-line:no-floating-promises this._expireTransactionsJob.start(); this._receivedTransactionsProcessingInterval = receivedTransactionsProcessingInterval; this._receivedTransactionsProcessingLimitPerInterval = receivedTransactionsLimitPerProcessing; this._validateTransactions = validateTransactions; this._validateTransactionsJob = new Job( this.validateReceivedTransactions.bind(this), this._receivedTransactionsProcessingInterval, ); // tslint:disable-next-line:no-floating-promises this._validateTransactionsJob.start(); this._validatedTransactionsProcessingInterval = validatedTransactionsProcessingInterval; this._validatedTransactionsProcessingLimitPerInterval = validatedTransactionsLimitPerProcessing; this._verifyTransactions = verifyTransactions; this._verifyTransactionsJob = new Job( this.verifyValidatedTransactions.bind(this), this._validatedTransactionsProcessingInterval, ); // tslint:disable-next-line:no-floating-promises this._verifyTransactionsJob.start(); this._verifiedTransactionsProcessingInterval = verifiedTransactionsProcessingInterval; this._verifiedTransactionsProcessingLimitPerInterval = verifiedTransactionsLimitPerProcessing; this._processTransactions = processTransactions; this._processTransactionsJob = new Job( this.processVerifiedTransactions.bind(this), this._verifiedTransactionsProcessingInterval, ); // tslint:disable-next-line:no-floating-promises this._processTransactionsJob.start(); } public cleanup(): void { this.removeTransactionsFromQueues( Object.keys(this.queues), queueCheckers.returnTrueUntilLimit(this._maxTransactionsPerQueue), ); this._expireTransactionsJob.stop(); this._validateTransactionsJob.stop(); this._verifyTransactionsJob.stop(); this._processTransactionsJob.stop(); } public addTransaction(transaction: Transaction): AddTransactionResult { const receivedQueue: QueueNames = 'received'; // Transactions which are added to the received queue should fire the event "EVENT_VERIFIED_TRANSACTION_ONCE" // When they are verified for the first time. VerifiedOnce flag is primarily used for it. transaction.verifiedOnce = false; return this.addTransactionToQueue(receivedQueue, transaction); } public addPendingTransaction(transaction: Transaction): AddTransactionResult { const pendingQueue: QueueNames = 'pending'; return this.addTransactionToQueue(pendingQueue, transaction); } public addVerifiedTransaction( transaction: Transaction, ): AddTransactionResult { const verifiedQueue: QueueNames = 'verified'; return this.addTransactionToQueue(verifiedQueue, transaction); } public addVerifiedRemovedTransactions( transactions: ReadonlyArray<Transaction>, ): void { const { received, validated, ...otherQueues } = this._queues; // Move transactions from the validated queue to the received queue where account was a receipient in the verified removed transactions // Rationale is explained in issue #963 const removedTransactionsByRecipientIdFromValidatedQueue = this._queues.validated.removeFor( queueCheckers.checkTransactionForSenderIdWithRecipientIds(transactions), ); this._queues.received.enqueueMany( removedTransactionsByRecipientIdFromValidatedQueue, ); // Move transactions from the verified, pending and ready queues to the validated queue where account was a receipient in the verified removed transactions const removedTransactionsByRecipientIdFromOtherQueues = this.removeTransactionsFromQueues( Object.keys(otherQueues), queueCheckers.checkTransactionForSenderIdWithRecipientIds(transactions), ); this._queues.validated.enqueueMany( removedTransactionsByRecipientIdFromOtherQueues, ); // Add transactions to the verified queue which were included in the verified removed transactions this._queues.verified.enqueueMany(transactions); this.emit(EVENT_ADDED_TRANSACTIONS, { action: ACTION_ADD_VERIFIED_REMOVED_TRANSACTIONS, to: 'verified', payload: transactions, }); } // It is assumed that signature is verified for this transaction before this function is called public addVerifiedSignature( signatureObject: SignatureObject, ): TransactionResponse { const transaction = this.findInTransactionPool( signatureObject.transactionId, ); if (transaction) { return transaction.addVerifiedSignature(signatureObject.signature); } return { id: signatureObject.transactionId, status: Status.FAIL, errors: [new Error('Could not find transaction in transaction pool')], }; } public existsInTransactionPool(id: string): boolean { return Object.keys(this._queues).reduce( (previousValue: boolean, queueName: string) => previousValue || this._queues[queueName].exists(id), false, ); } public findInTransactionPool(id: string): Transaction | undefined { return Object.keys(this._queues).reduce( (previousValue: Transaction | undefined, queueName) => previousValue || this._queues[queueName].index[id], undefined, ); } public get queues(): Queues { return this._queues; } public getProcessableTransactions(limit: number): ReadonlyArray<Transaction> { return this._queues.ready.peekUntil( queueCheckers.returnTrueUntilLimit(limit), ); } public removeConfirmedTransactions( transactions: ReadonlyArray<Transaction>, ): void { // Remove transactions in the transaction pool which were included in the confirmed transactions const removedTransactions = this.removeTransactionsFromQueues( Object.keys(this._queues), queueCheckers.checkTransactionForId(transactions), ); const { received, validated, ...otherQueues } = this._queues; const confirmedTransactionsWithUniqueData = transactions.filter( (transaction: Transaction) => transaction.containsUniqueData, ); // Remove transactions from the validated queue which were sent from the accounts in the confirmed transactions const removedTransactionsBySenderPublicKeysFromValidatedQueue = this._queues.validated.removeFor( queueCheckers.checkTransactionForSenderPublicKey(transactions), ); // Remove transactions from the validated queue if they are of a type which includes unique data and that type is included in the confirmed transactions const removedTransactionsByTypesFromValidatedQueue = this._queues.validated.removeFor( queueCheckers.checkTransactionForTypes( confirmedTransactionsWithUniqueData, ), ); // Add removed transactions from the validated queue to the received queue // Rationale is explained in issue #963 this._queues.received.enqueueMany([ ...removedTransactionsBySenderPublicKeysFromValidatedQueue, ...removedTransactionsByTypesFromValidatedQueue, ]); // Remove transactions from the verified, pending and ready queues which were sent from the accounts in the confirmed transactions const removedTransactionsBySenderPublicKeysFromOtherQueues = this.removeTransactionsFromQueues( Object.keys(otherQueues), queueCheckers.checkTransactionForSenderPublicKey(transactions), ); // Remove all transactions from the verified, pending and ready queues if they are of a type which includes unique data and that type is included in the confirmed transactions const removedTransactionsByTypesFromOtherQueues = this.removeTransactionsFromQueues( Object.keys(otherQueues), queueCheckers.checkTransactionForTypes( confirmedTransactionsWithUniqueData, ), ); this.emit(EVENT_REMOVED_TRANSACTIONS, { action: ACTION_REMOVE_CONFIRMED_TRANSACTIONS, payload: removedTransactions, }); // Add transactions which need to be reverified to the validated queue this._queues.validated.enqueueMany([ ...removedTransactionsBySenderPublicKeysFromOtherQueues, ...removedTransactionsByTypesFromOtherQueues, ]); } public reverifyTransactionsFromSenders( senderPublicKeys: ReadonlyArray<string>, ): void { const { received, validated, ...otherQueues } = this._queues; const senderProperty: queueCheckers.TransactionFilterableKeys = 'senderPublicKey'; // Move transactions from the validated queue to the received queue which were sent from sender accounts // Rationale is explained in issue #963 const removedTransactionsBySenderPublicKeysFromValidatedQueue = this._queues.validated.removeFor( queueCheckers.checkTransactionPropertyForValues( senderPublicKeys, senderProperty, ), ); this._queues.received.enqueueMany( removedTransactionsBySenderPublicKeysFromValidatedQueue, ); // Move transactions from the verified, pending and ready queues to the validated queue which were sent from sender accounts const removedTransactionsBySenderPublicKeysFromOtherQueues = this.removeTransactionsFromQueues( Object.keys(otherQueues), queueCheckers.checkTransactionPropertyForValues( senderPublicKeys, senderProperty, ), ); this._queues.validated.enqueueMany( removedTransactionsBySenderPublicKeysFromOtherQueues, ); } // This function is currently unused, the usability of this function will be decided after performance tests public validateTransactionAgainstTransactionsInPool( transaction: Transaction, ): boolean { return transaction.verifyAgainstOtherTransactions([ ...this.queues.ready.transactions, ...this.queues.pending.transactions, ...this.queues.verified.transactions, ]); } private addTransactionToQueue( queueName: QueueNames, transaction: Transaction, ): AddTransactionResult { if (this.existsInTransactionPool(transaction.id)) { return { isFull: false, alreadyExists: true, queueName, }; } if (this._queues[queueName].size() >= this._maxTransactionsPerQueue) { return { isFull: true, alreadyExists: false, queueName, }; } // Add receivedAt property for the transaction transaction.receivedAt = new Date(); this._queues[queueName].enqueueOne(transaction); this.emit(EVENT_ADDED_TRANSACTIONS, { action: ACTION_ADD_TRANSACTIONS, to: queueName, payload: [transaction], }); // If transaction is added to one of the queues which semantically mean that transactions are verified, then fire the event. if (queueName === 'verified' || queueName === 'pending') { this.emit(EVENT_VERIFIED_TRANSACTION_ONCE, { action: queueName === 'verified' ? ACTION_ADD_VERIFIED_TRANSACTIONS : ACTION_ADD_PENDING_TRANSACTIONS, payload: [transaction], }); } return { isFull: false, alreadyExists: false, queueName, }; } private async expireTransactions(): Promise<ReadonlyArray<Transaction>> { const expiredTransactions = this.removeTransactionsFromQueues( Object.keys(this._queues), queueCheckers.checkTransactionForExpiry(), ); this.emit(EVENT_REMOVED_TRANSACTIONS, { action: ACTION_EXPIRE_TRANSACTIONS, payload: expiredTransactions, }); return expiredTransactions; } private async processVerifiedTransactions(): Promise< CheckTransactionsResponseWithPassAndFail > { const transactionsInReadyQueue = this._queues.ready.size(); const transactionsInVerifiedQueue = this._queues.verified.size(); const processableTransactionsInPendingQueue = this._queues.pending.sizeBy( transaction => transaction.isReady(), ); if ( transactionsInReadyQueue >= this._verifiedTransactionsProcessingLimitPerInterval || (transactionsInVerifiedQueue === 0 && processableTransactionsInPendingQueue === 0) ) { return { passedTransactions: [], failedTransactions: [], }; } const additionalTransactionsToProcessLimit = this._verifiedTransactionsProcessingLimitPerInterval - transactionsInReadyQueue; const transactionsFromPendingQueueLimit = Math.min( additionalTransactionsToProcessLimit, this._pendingTransactionsProcessingLimit, ); // Filter at max transactionsFromPendingQueueLimit from the pending queue which are also ready const transactionsFromPendingQueue = this._queues.pending .filter(transaction => transaction.isReady()) .slice(0, transactionsFromPendingQueueLimit); const additionalVerifiedTransactionsToProcessLimit = additionalTransactionsToProcessLimit - transactionsFromPendingQueue.length; const transactionsFromVerifiedQueue = this._queues.verified.peekUntil( queueCheckers.returnTrueUntilLimit( additionalVerifiedTransactionsToProcessLimit, ), ); const transactionsFromReadyQueue = this._queues.ready.peekUntil( queueCheckers.returnTrueUntilLimit(transactionsInReadyQueue), ); const toProcessTransactions = [ ...transactionsFromReadyQueue, ...transactionsFromPendingQueue, ...transactionsFromVerifiedQueue, ]; const { passedTransactions, failedTransactions, } = await checkTransactionsWithPassAndFail( toProcessTransactions, this._processTransactions, ); const { received, validated, ...otherQueues } = this._queues; // Remove invalid transactions from verified, pending and ready queues const removedTransactions = this.removeTransactionsFromQueues( Object.keys(otherQueues), queueCheckers.checkTransactionForId(failedTransactions), ); // Move all passed transactions to the ready queue // Keep transactions in the ready queue which still exist this._queues.ready.enqueueMany( this._queues.ready.removeFor( queueCheckers.checkTransactionForId(passedTransactions), ), ); // Move processeable transactions from the verified queue to the ready queue this._queues.ready.enqueueMany( this._queues.verified.removeFor( queueCheckers.checkTransactionForId(passedTransactions), ), ); // Move processable transactions from the pending queue to the ready queue this._queues.ready.enqueueMany( this._queues.pending.removeFor( queueCheckers.checkTransactionForId(passedTransactions), ), ); this.emit(EVENT_REMOVED_TRANSACTIONS, { action: ACTION_PROCESS_VERIFIED_TRANSACTIONS, payload: removedTransactions, }); return { passedTransactions, failedTransactions, }; } private removeTransactionsFromQueues( queueNames: ReadonlyArray<string>, condition: (transaction: Transaction) => boolean, ): ReadonlyArray<Transaction> { return queueNames .map(queueName => this._queues[queueName].removeFor(condition)) .reduce( ( transactionsAccumulatedFromQueues: ReadonlyArray<Transaction>, transactionsFromCurrentQueue: ReadonlyArray<Transaction>, ) => transactionsAccumulatedFromQueues.concat( transactionsFromCurrentQueue, ), [], ); } private async validateReceivedTransactions(): Promise< CheckTransactionsResponseWithPassAndFail > { Iif ( this.queues.validated.size() >= this._maxTransactionsPerQueue || this.queues.received.size() === 0 ) { return { passedTransactions: [], failedTransactions: [], }; } const toValidateTransactions = this._queues.received.peekUntil( queueCheckers.returnTrueUntilLimit( this._receivedTransactionsProcessingLimitPerInterval, ), ); const { passedTransactions, failedTransactions, } = await checkTransactionsWithPassAndFail( toValidateTransactions, this._validateTransactions, ); // Remove invalid transactions const removedTransactions = this._queues.received.removeFor( queueCheckers.checkTransactionForId(failedTransactions), ); // Move valid transactions from the received queue to the validated queue this._queues.validated.enqueueMany( this._queues.received.removeFor( queueCheckers.checkTransactionForId(passedTransactions), ), ); this.emit(EVENT_REMOVED_TRANSACTIONS, { action: ACTION_VALIDATE_RECEIVED_TRANSACTIONS, payload: removedTransactions, }); return { passedTransactions, failedTransactions, }; } private async verifyValidatedTransactions(): Promise< CheckTransactionsResponseWithPassFailAndPending > { if ( this.queues.verified.size() >= this._maxTransactionsPerQueue || this.queues.validated.size() === 0 ) { return { passedTransactions: [], failedTransactions: [], pendingTransactions: [], }; } const toVerifyTransactions = this._queues.validated.peekUntil( queueCheckers.returnTrueUntilLimit( this._validatedTransactionsProcessingLimitPerInterval, ), ); const { failedTransactions, pendingTransactions, passedTransactions, } = await checkTransactionsWithPassFailAndPending( toVerifyTransactions, this._verifyTransactions, ); // Remove invalid transactions const removedTransactions = this._queues.validated.removeFor( queueCheckers.checkTransactionForId(failedTransactions), ); // Move verified transactions from the validated queue to the verified queue this._queues.verified.enqueueMany( this._queues.validated.removeFor( queueCheckers.checkTransactionForId(passedTransactions), ), ); // Move verified pending transactions from the validated queue to the pending queue this._queues.pending.enqueueMany( this._queues.validated.removeFor( queueCheckers.checkTransactionForId(pendingTransactions), ), ); this.emit(EVENT_REMOVED_TRANSACTIONS, { action: ACTION_VERIFY_VALIDATED_TRANSACTIONS, payload: removedTransactions, }); // Checking which transactions were verified for the first time, filtering them and firing an event for those transactions const transactionsVerifiedForFirstTime = [ ...pendingTransactions, ...passedTransactions, ].filter(transaction => transaction.verifiedOnce === false); transactionsVerifiedForFirstTime.forEach( transaction => delete transaction.verifiedOnce, ); this.emit(EVENT_VERIFIED_TRANSACTION_ONCE, { action: ACTION_VERIFY_VALIDATED_TRANSACTIONS, payload: transactionsVerifiedForFirstTime, }); return { passedTransactions, failedTransactions, pendingTransactions, }; } } |