Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #994 from LiskHQ/876-pending_transactions
Browse files Browse the repository at this point in the history
Add functionality for handling pending transactions - Closes #876
  • Loading branch information
shuse2 authored Jan 21, 2019
2 parents 826b124 + 8fd46f3 commit 2c507d4
Show file tree
Hide file tree
Showing 7 changed files with 471 additions and 106 deletions.
104 changes: 104 additions & 0 deletions packages/lisk-transaction-pool/fixtures/transactions.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,109 @@
},
"signature":"faaf3fcfe58e6435b47716fa89b322d855e062ffa52d6e01fe569628c48a2310d6390736d2ff47efd2543c26df309c16f8e789a406da03b1e4dd2511176bb408",
"id":"927268910456751599"
},
{
"type": 0,
"amount": "10008298357",
"fee": 0,
"timestamp": 0,
"recipientId": "17033820735302139166L",
"senderId": "6566229458323231555L",
"senderPublicKey":
"d121d3abf5425fdc0f161d9ddb32f89b7750b4bdb0bff7d18b191d4b4bafa6d4",
"signature":
"72c9b2aa734ec1b97549718ddf0d4737fd38a7f0fd105ea28486f2d989e9b3e399238d81a93aa45c27309d91ce604a5db9d25c9c90a138821f2011bc6636c60a",
"id": "5449806225917864483"
},
{
"type": 0,
"amount": "100181268334",
"fee": 0,
"timestamp": 0,
"recipientId": "6346132659289875504L",
"senderId": "6566229458323231555L",
"senderPublicKey":
"d121d3abf5425fdc0f161d9ddb32f89b7750b4bdb0bff7d18b191d4b4bafa6d4",
"signature":
"9b09dcdb61db43bcc7b789177c415b1d78b4033a783577d905f12568cc3b9434fb5999ecacf9a027eaa5444fce374681010dcce3ff87e6eca2f28a4ef9c83b03",
"id": "12166312224816013186"
},
{
"type": 0,
"amount": "100362463",
"fee": 0,
"timestamp": 0,
"recipientId": "11464849422097900507L",
"senderId": "6566229458323231555L",
"senderPublicKey":
"d121d3abf5425fdc0f161d9ddb32f89b7750b4bdb0bff7d18b191d4b4bafa6d4",
"signature":
"ad2ed14b42899c8b4a827e7e8afd081162c1b7de1fccf46c9a0ec68e382f467d775a6eecd560244bc3da6beba15fe386955a77e8176aec405844d199520be009",
"id": "4164649425208084448"
},
{
"type": 0,
"amount": "100369372455",
"fee": 0,
"timestamp": 0,
"recipientId": "4236366971618433722L",
"senderId": "6566229458323231555L",
"senderPublicKey":
"d121d3abf5425fdc0f161d9ddb32f89b7750b4bdb0bff7d18b191d4b4bafa6d4",
"signature":
"f9ff9ee3b8312ff3825b4d37322779e1edb2ef50844f09f1935a03e9075d7eae93a71cb1181bf48a45148450ecce7a44aa91741a501c1ee42debdef2da377500",
"id": "16794796872512157174"
},
{
"type": 0,
"amount": "100381478227",
"fee": 0,
"timestamp": 0,
"recipientId": "2072845667742629181L",
"senderId": "6566229458323231555L",
"senderPublicKey":
"d121d3abf5425fdc0f161d9ddb32f89b7750b4bdb0bff7d18b191d4b4bafa6d4",
"signature":
"d648d8d5173e42a4e7cac208b4e53eb54656b1cd98ac342c3e6888338d3660c1891f18300e63b7e777ddb355c619c792cce895bf08d7fd6bbb2ce3fdcebc1e0a",
"id": "12945087750653182687"
},
{
"type": 0,
"amount": "1005469534",
"fee": 0,
"timestamp": 0,
"recipientId": "4433488237980614411L",
"senderId": "6566229458323231555L",
"senderPublicKey":
"d121d3abf5425fdc0f161d9ddb32f89b7750b4bdb0bff7d18b191d4b4bafa6d4",
"signature":
"dcb362ae9393ef69ac2b5c50d9cb605b126defd7623e9eaa22bd3d377feaeae226463cd3326fade770d94c280b86d47c18ccee33a35bbaafdd3e31cf0b910d07",
"id": "771831816220047333"
},
{
"type": 0,
"amount": "100590879667",
"fee": 0,
"timestamp": 0,
"recipientId": "4050335756210208828L",
"senderId": "6566229458323231555L",
"senderPublicKey":
"d121d3abf5425fdc0f161d9ddb32f89b7750b4bdb0bff7d18b191d4b4bafa6d4",
"signature":
"bb503ca5a7cea4330bdc10c13ef6619d6609936b181003c3b0eb76521750ab8088840a2e5fdc8bedcb08a37bb58f916f643a2e5f9d7ff13de6a764f39e591d08",
"id": "8226427494934824916"
},
{
"type": 0,
"amount": "10074039329",
"fee": 0,
"timestamp": 0,
"recipientId": "11034244441535906697L",
"senderId": "6566229458323231555L",
"senderPublicKey":
"d121d3abf5425fdc0f161d9ddb32f89b7750b4bdb0bff7d18b191d4b4bafa6d4",
"signature":
"db4eec2efab2f15e8a1b2cc514ce31ef26b4c444bc4f5a7b0b9a64742d423486a0c162d953af214a63eec108c9a1d460cc1145dfb02f0fad8648991db1bab40e",
"id": "1522947044527641725"
}
]
78 changes: 64 additions & 14 deletions packages/lisk-transaction-pool/src/check_transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,88 @@ export interface TransactionResponse {
}

export enum Status {
FAIL = 0,
OK = 1,
FAIL = 2,
PENDING = 2,
}

export interface CheckTransactionsResponse {
export interface CheckTransactionsResponseWithPassAndFail {
failedTransactions: ReadonlyArray<Transaction>;
passedTransactions: ReadonlyArray<Transaction>;
}

export const checkTransactions = async (
export interface CheckTransactionsResponseWithPassFailAndPending {
failedTransactions: ReadonlyArray<Transaction>;
passedTransactions: ReadonlyArray<Transaction>;
pendingTransactions: ReadonlyArray<Transaction>;
}

const getTransactionByStatus = (
transactions: ReadonlyArray<Transaction>,
responses: ReadonlyArray<TransactionResponse>,
status: Status,
): ReadonlyArray<Transaction> => {
const transactionIdsByStatus = responses
.filter(transactionResponse => transactionResponse.status === status)
.map(transactionStatus => transactionStatus.id);

const transactionsByStatus = transactions.filter(transaction =>
transactionIdsByStatus.includes(transaction.id),
);

return transactionsByStatus;
};

export const checkTransactionsWithPassAndFail = async (
transactions: ReadonlyArray<Transaction>,
checkerFunction: CheckerFunction,
): Promise<CheckTransactionsResponse> => {
): Promise<CheckTransactionsResponseWithPassAndFail> => {
// Process transactions and check their validity
const { transactionsResponses } = await checkerFunction(transactions);

// Get ids of failed transactions from the response
const failedTransactionIds = transactionsResponses
.filter(transactionResponse => transactionResponse.status === Status.FAIL)
.map(transactionStatus => transactionStatus.id);
const failedTransactions = getTransactionByStatus(
transactions,
transactionsResponses,
Status.FAIL,
);
const passedTransactions = getTransactionByStatus(
transactions,
transactionsResponses,
Status.OK,
);

return {
failedTransactions,
passedTransactions,
};
};

// Filter transactions which were failed
const failedTransactions = transactions.filter(transaction =>
failedTransactionIds.includes(transaction.id),
export const checkTransactionsWithPassFailAndPending = async (
transactions: ReadonlyArray<Transaction>,
checkerFunction: CheckerFunction,
): Promise<CheckTransactionsResponseWithPassFailAndPending> => {
// Process transactions and check their validity
const { transactionsResponses } = await checkerFunction(transactions);

const failedTransactions = getTransactionByStatus(
transactions,
transactionsResponses,
Status.FAIL,
);
const passedTransactions = getTransactionByStatus(
transactions,
transactionsResponses,
Status.OK,
);
// Filter transactions which were ok
const passedTransactions = transactions.filter(
transaction => !failedTransactionIds.includes(transaction.id),
const pendingTransactions = getTransactionByStatus(
transactions,
transactionsResponses,
Status.PENDING,
);

return {
failedTransactions,
passedTransactions,
pendingTransactions,
};
};
12 changes: 11 additions & 1 deletion packages/lisk-transaction-pool/src/queue.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Transaction } from './transaction_pool';

interface QueueIndex {
[index: string]: Transaction;
[index: string]: Transaction | undefined;
}

interface RemoveForReduceObject {
Expand Down Expand Up @@ -88,6 +88,12 @@ export class Queue {
return !!this._index[transaction.id];
}

public filter(
condition: (transaction: Transaction) => boolean,
): ReadonlyArray<Transaction> {
return this._transactions.filter(condition);
}

public peekUntil(
condition: (transaction: Transaction) => boolean,
): ReadonlyArray<Transaction> {
Expand Down Expand Up @@ -146,4 +152,8 @@ export class Queue {
public size(): number {
return this._transactions.length;
}

public sizeBy(condition: (transaction: Transaction) => boolean): number {
return this._transactions.filter(condition).length;
}
}
Loading

0 comments on commit 2c507d4

Please sign in to comment.