-
Notifications
You must be signed in to change notification settings - Fork 17
/
utils.ts
317 lines (277 loc) · 7.83 KB
/
utils.ts
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
import { Transaction, TransactionsBatchInfo } from './models';
/**
* @category Utils
*/
export type UniqueArray<T> = T extends readonly [infer X, ...infer Rest]
? InArray<Rest, X> extends true
? ['Encountered value with duplicates:', X]
: readonly [X, ...UniqueArray<Rest>]
: T
/**
* @category Utils
*/
export type InArray<T, X> = T extends readonly [X, ...infer _Rest]
? true
: T extends readonly [X]
? true
: T extends readonly [infer _, ...infer Rest]
? InArray<Rest, X>
: false
/**
* @category Utils
*/
export type ArrayItemType<T extends readonly unknown[]> = T extends readonly (infer Ts)[] ? Ts : never;
/**
* @category Utils
*/
export class Address {
private readonly _address: string;
constructor(address: string) {
this._address = address;
}
public toString(): string {
return this._address;
}
public toJSON(): string {
return this._address;
}
public equals = (other: Address | string): boolean => this._equals(other);
private _equals(other: Address | string): boolean {
if (typeof other === 'string') {
return this._address === other;
} else {
return this._address === other?._address;
}
}
}
/**
* Check whether the provider object is instance of `Address`,
* handling the case of duplicated dependencies.
*/
export function isAddressObject(address: object | null): boolean {
return (
address != null &&
(address instanceof Address ||
Object.getPrototypeOf(address).constructor.name === Address.prototype.constructor.name)
);
}
/**
* @category Utils
*/
export class AddressLiteral<T extends string> extends Address {
constructor(address: CheckAddress<T>) {
super(address);
}
}
/**
* @category Utils
*/
export type CheckAddress<T extends string> = AddressImpl<T, Lowercase<T>>;
type AddressPrefix = '0:' | '-1:'
type AddressImpl<T, Tl extends string> = Tl extends `${AddressPrefix}${infer Hash}`
? true extends IsHexString<Hash, []>
? T : never
: never;
type HexSymbol = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f'
type HexByte = `${HexSymbol}${HexSymbol}`
type IsHexString<T extends string, L extends readonly number[]> =
T extends `${HexByte}${infer Tail}`
? IsHexString<Tail, [...L, 0]>
: T extends '' ? L['length'] extends 32 ? true : never : never
/**
* @category Utils
*/
export class MessageExpiredException extends Error {
constructor(public readonly address: Address, public readonly hash: string) {
super('Message expired');
}
}
export class DelayedTransactions {
private readonly transactions: Map<string, {
promise: Promise<Transaction | undefined>,
resolve: (transaction?: Transaction) => void,
reject: () => void,
}> = new Map();
public async waitTransaction(address: Address, hash: string): Promise<Transaction> {
let transaction = this.transactions.get(hash)?.promise;
if (transaction == null) {
let resolve: (transaction?: Transaction) => void;
let reject: () => void;
transaction = new Promise<Transaction | undefined>((promiseResolve, promiseReject) => {
resolve = (tx) => promiseResolve(tx);
reject = () => promiseReject();
});
this.transactions.set(hash, {
promise: transaction,
resolve: resolve!,
reject: reject!,
});
}
const tx = await transaction;
if (tx == null) {
throw new MessageExpiredException(address, hash);
}
return tx;
}
public fillTransaction(hash: string, transaction?: Transaction) {
const pendingTransaction = this.transactions.get(hash);
if (pendingTransaction != null) {
pendingTransaction.resolve(transaction);
} else {
this.transactions.set(hash, {
promise: Promise.resolve(transaction),
resolve: () => { /* do nothing */
},
reject: () => { /* do nothing */
},
});
}
}
}
/**
* @category Utils
*/
export class Semaphore {
private tasks: (() => void)[] = [];
count: number;
constructor(count: number) {
this.count = count;
}
public acquire(): Promise<() => void> {
return new Promise<() => void>((res, _rej) => {
this.tasks.push(() => {
let released = false;
res(() => {
if (!released) {
released = true;
this.count++;
this.sched();
}
});
});
nextTick(this.sched);
});
}
public releaseAll() {
while (this.tasks.length > 0) {
this.tasks.shift()?.();
}
}
sched = () => {
if (this.count > 0 && this.tasks.length > 0) {
this.count--;
this.tasks.shift()?.();
}
};
}
function byObserver(Observer: any) {
const node = document.createTextNode('');
let queue: any, currentQueue: any, bit = 0, i = 0;
new Observer(function() {
let callback;
if (!queue) {
if (!currentQueue) return;
queue = currentQueue;
} else if (currentQueue) {
queue = currentQueue.slice(i).concat(queue);
}
currentQueue = queue;
queue = null;
i = 0;
if (typeof currentQueue === 'function') {
callback = currentQueue;
currentQueue = null;
callback();
return;
}
node.data = (bit = ++bit % 2) as any;
while (i < currentQueue.length) {
callback = currentQueue[i];
i++;
if (i === currentQueue.length) currentQueue = null;
callback();
}
}).observe(node, { characterData: true });
return function(fn: any) {
if (queue) {
if (typeof queue === 'function') queue = [queue, fn];
else queue.push(fn);
return;
}
queue = fn;
node.data = (bit = ++bit % 2) as any;
};
}
const nextTick = (function() {
// queueMicrotask
if (typeof queueMicrotask === 'function') {
return queueMicrotask;
}
// MutationObserver
if ((typeof document === 'object') && document) {
if (typeof MutationObserver === 'function') return byObserver(MutationObserver);
if (typeof (window as any).WebKitMutationObserver === 'function') return byObserver((window as any).WebKitMutationObserver);
}
/* @ts-ignore */
if (typeof setImmediate === 'function') {
/* @ts-ignore */
return setImmediate;
}
if ((typeof setTimeout === 'function') || (typeof setTimeout === 'object')) {
return function(cb: any) {
setTimeout(cb, 0);
};
}
throw new Error('No `nextTick` implementation found');
}());
/**
* @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.
*
* > Note! This method does not remove duplicates.
*
* @param knownTransactions
* @param newTransactions
* @param info
*
* @category Utils
*/
export function mergeTransactions<Addr>(
knownTransactions: Transaction<Addr>[],
newTransactions: Transaction<Addr>[],
info: TransactionsBatchInfo,
): Transaction<Addr>[] {
if (info.batchType === 'old') {
knownTransactions.push(...newTransactions);
return knownTransactions;
}
if (knownTransactions.length === 0) {
knownTransactions.push(...newTransactions);
return knownTransactions;
}
// Example:
// known lts: [N, N-1, N-2, N-3, (!) N-10,...]
// new lts: [N-4, N-5]
// batch info: { minLt: N-5, maxLt: N-4, batchType: 'new' }
// 1. Skip indices until known transaction lt is greater than the biggest in the batch
let i = 0;
while (
i < knownTransactions.length &&
LT_COLLATOR.compare(knownTransactions[i].id.lt, info.maxLt) >= 0
) {
++i;
}
// 2. Insert new transactions
knownTransactions.splice(i, 0, ...newTransactions);
return knownTransactions;
}
const MAX = 4294967295;
let idCounter = Math.floor(Math.random() * MAX);
export function getUniqueId(): number {
idCounter = (idCounter + 1) % MAX;
return idCounter;
}