Skip to content

Commit

Permalink
Improve logical time comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Aug 1, 2022
1 parent ec488d3 commit 3d78c72
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
11 changes: 6 additions & 5 deletions src/contract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Address,
UniqueArray,
LT_COLLATOR,
} from './utils';
import {
AbiParam,
Expand Down Expand Up @@ -278,9 +279,9 @@ export class Contract<Abi> {
: subscriber.transactions(this.address)
).flatMap(item => item.transactions)
.takeWhile(item => range == null ||
(range.fromLt == null || item.id.lt > range.fromLt) &&
(range.fromLt == null || LT_COLLATOR.compare(item.id.lt, range.fromLt) > 0) &&
(range.fromUtime == null || item.createdAt > range.fromUtime) &&
(range.toLt == null || item.id.lt < range.toLt) &&
(range.toLt == null || LT_COLLATOR.compare(item.id.lt, range.toLt) < 0) &&
(range.toUtime == null || item.createdAt < range.toUtime),
)
.flatMap(tx => this.decodeTransactionEvents({ transaction: tx })
Expand Down Expand Up @@ -318,10 +319,10 @@ export class Contract<Abi> {
}

const filteredTransactions = transactions.filter((item) => (
(range?.fromLt == null || item.id.lt > range.fromLt) &&
(range?.fromLt == null || LT_COLLATOR.compare(item.id.lt, range.fromLt) > 0) &&
(range?.fromUtime == null || item.createdAt > range.fromUtime) &&
(range?.toLt == null || item.id.lt < range?.toLt) &&
(range?.toUtime == null || item.createdAt < range?.toUtime)
(range?.toLt == null || LT_COLLATOR.compare(item.id.lt, range.toLt) < 0) &&
(range?.toUtime == null || item.createdAt < range.toUtime)
));

if (filteredTransactions.length > 0) {
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export * from './api';
export * from './models';
export * from './contract';
export { Stream, Subscriber } from './stream';
export { Address, CheckAddress, AddressLiteral, UniqueArray, mergeTransactions } from './utils';
export { Address, CheckAddress, AddressLiteral, UniqueArray, mergeTransactions, LT_COLLATOR } from './utils';

/**
* @category Provider
Expand Down Expand Up @@ -858,6 +858,11 @@ export class ProviderRpcClient {
address: new Address(data.address),
state: data.state,
}),
'messageStatusUpdated': (data) => ({
address: new Address(data.address),
hash: data.hash,
transaction: data.transaction != null ? parseTransaction(data.transaction) : undefined,
}),
'networkChanged': data => data,
'permissionsChanged': (data) => ({
permissions: parsePermissions(data.permissions),
Expand Down
49 changes: 20 additions & 29 deletions src/stream.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ProviderEvent, ProviderEventData } from './api';
import { Address, getUniqueId } from './utils';
import { Address, getUniqueId, LT_COLLATOR } from './utils';
import { TransactionId, Transaction, TransactionsBatchInfo, TransactionWithAccount } from './models';
import { ProviderRpcClient, Subscription } from './index';

Expand Down Expand Up @@ -788,8 +788,6 @@ type UnorderedTransactionsScannerParams = {
onEnd: (eof: boolean) => void;
fromLt?: string;
fromUtime?: number;
toLt?: string;
toUtime?: number;
};

class UnorderedTransactionsScanner implements Scanner {
Expand Down Expand Up @@ -829,40 +827,33 @@ class UnorderedTransactionsScanner implements Scanner {
break;
}

const fromFilteredTransactions = transactions.filter((item) => (
(params.fromLt == null || item.id.lt > params.fromLt) &&
const filteredTransactions = transactions.filter((item) => (
(params.fromLt == null || LT_COLLATOR.compare(item.id.lt, params.fromLt) > 0) &&
(params.fromUtime == null || item.createdAt > params.fromUtime)
));

if (fromFilteredTransactions.length == 0) {
if (filteredTransactions.length == 0) {
state.complete = true;
break;
}

const toFilteredTransactions = fromFilteredTransactions.filter((item) => (
(params.toLt == null || item.id.lt < params.toLt) &&
(params.toUtime == null || item.createdAt < params.toUtime)
));

if (toFilteredTransactions.length > 0) {
const info = {
maxLt: toFilteredTransactions[0].id.lt,
minLt: toFilteredTransactions[toFilteredTransactions.length - 1].id.lt,
batchType: 'old',
} as TransactionsBatchInfo;

this.queue.enqueue(async () => {
const isRunning = this.params.onData({
address: this.params.address,
transactions: toFilteredTransactions,
info,
});
if (!isRunning) {
state.complete = true;
this.isRunning = false;
}
const info = {
maxLt: filteredTransactions[0].id.lt,
minLt: filteredTransactions[filteredTransactions.length - 1].id.lt,
batchType: 'old',
} as TransactionsBatchInfo;

this.queue.enqueue(async () => {
const isRunning = this.params.onData({
address: this.params.address,
transactions: filteredTransactions,
info,
});
}
if (!isRunning) {
state.complete = true;
this.isRunning = false;
}
});

if (continuation != null) {
this.continuation = continuation;
Expand Down
7 changes: 6 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ type IsHexString<T extends string, L extends readonly number[]> =
? IsHexString<Tail, [...L, 0]>
: T extends '' ? L['length'] extends 32 ? true : never : never

/**
* @category Utils
*/
export const LT_COLLATOR: Intl.Collator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' });

/**
* Modifies knownTransactions array, merging it with new transactions.
* All arrays are assumed to be sorted by descending logical time.
Expand Down Expand Up @@ -113,7 +118,7 @@ export function mergeTransactions<Addr>(
let i = 0;
while (
i < knownTransactions.length &&
knownTransactions[i].id.lt.localeCompare(info.maxLt) >= 0
LT_COLLATOR.compare(knownTransactions[i].id.lt, info.maxLt) >= 0
) {
++i;
}
Expand Down

0 comments on commit 3d78c72

Please sign in to comment.