Slight adjustment to rpc-provider LRU (#4198)
This commit is contained in:
@@ -23,7 +23,8 @@ export abstract class Base<ApiType extends ApiTypes> {
|
|||||||
this.api = api;
|
this.api = api;
|
||||||
this._decorateMethod = decorateMethod;
|
this._decorateMethod = decorateMethod;
|
||||||
|
|
||||||
assert(!!(api && api.isConnected && api.tx && api.tx.contracts && Object.keys(api.tx.contracts).length), 'Your API has not been initialized correctly and it not decorated with the runtime interfaces for contracts as retrieved from the on-chain runtime');
|
assert(!!(api && api.isConnected && api.tx), 'Your API has not been initialized correctly and is not connected to a chain');
|
||||||
|
assert(!!(api.tx.contracts && Object.keys(api.tx.contracts).length), 'You need to connect to a chain with a runtime that supports contracts');
|
||||||
assert(isFunction(api.tx.contracts.instantiateWithCode), 'You need to connect to a chain with a runtime with a V3 contracts module. The runtime does not expose api.tx.contracts.instantiateWithCode');
|
assert(isFunction(api.tx.contracts.instantiateWithCode), 'You need to connect to a chain with a runtime with a V3 contracts module. The runtime does not expose api.tx.contracts.instantiateWithCode');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ describe('LRUCache', (): void => {
|
|||||||
keys.forEach((k) => lru.set(k, `${k}${k}${k}`));
|
keys.forEach((k) => lru.set(k, `${k}${k}${k}`));
|
||||||
|
|
||||||
expect(lru.keys().join(', ')).toEqual(keys.reverse().join(', '));
|
expect(lru.keys().join(', ')).toEqual(keys.reverse().join(', '));
|
||||||
|
expect(lru.length === lru.lengthData && lru.length === lru.lengthRefs).toBe(true);
|
||||||
|
|
||||||
keys.forEach((k) => expect(lru.get(k)).toEqual(`${k}${k}${k}`));
|
keys.forEach((k) => expect(lru.get(k)).toEqual(`${k}${k}${k}`));
|
||||||
});
|
});
|
||||||
@@ -22,6 +23,7 @@ describe('LRUCache', (): void => {
|
|||||||
keys.forEach((k) => lru.set(k, `${k}${k}${k}`));
|
keys.forEach((k) => lru.set(k, `${k}${k}${k}`));
|
||||||
|
|
||||||
expect(lru.keys().join(', ')).toEqual(keys.slice(2).reverse().join(', '));
|
expect(lru.keys().join(', ')).toEqual(keys.slice(2).reverse().join(', '));
|
||||||
|
expect(lru.length === lru.lengthData && lru.length === lru.lengthRefs).toBe(true);
|
||||||
|
|
||||||
keys.slice(2).forEach((k) => expect(lru.get(k)).toEqual(`${k}${k}${k}`));
|
keys.slice(2).forEach((k) => expect(lru.get(k)).toEqual(`${k}${k}${k}`));
|
||||||
});
|
});
|
||||||
@@ -33,17 +35,21 @@ describe('LRUCache', (): void => {
|
|||||||
keys.forEach((k) => lru.set(k, `${k}${k}${k}`));
|
keys.forEach((k) => lru.set(k, `${k}${k}${k}`));
|
||||||
|
|
||||||
expect(lru.entries()).toEqual([['5', '555'], ['4', '444'], ['3', '333'], ['2', '222']]);
|
expect(lru.entries()).toEqual([['5', '555'], ['4', '444'], ['3', '333'], ['2', '222']]);
|
||||||
|
expect(lru.length === lru.lengthData && lru.length === lru.lengthRefs).toBe(true);
|
||||||
|
|
||||||
lru.get('3');
|
lru.get('3');
|
||||||
|
|
||||||
expect(lru.entries()).toEqual([['3', '333'], ['5', '555'], ['4', '444'], ['2', '222']]);
|
expect(lru.entries()).toEqual([['3', '333'], ['5', '555'], ['4', '444'], ['2', '222']]);
|
||||||
|
expect(lru.length === lru.lengthData && lru.length === lru.lengthRefs).toBe(true);
|
||||||
|
|
||||||
lru.set('4', '4433');
|
lru.set('4', '4433');
|
||||||
|
|
||||||
expect(lru.entries()).toEqual([['4', '4433'], ['3', '333'], ['5', '555'], ['2', '222']]);
|
expect(lru.entries()).toEqual([['4', '4433'], ['3', '333'], ['5', '555'], ['2', '222']]);
|
||||||
|
expect(lru.length === lru.lengthData && lru.length === lru.lengthRefs).toBe(true);
|
||||||
|
|
||||||
lru.set('6', '666');
|
lru.set('6', '666');
|
||||||
|
|
||||||
expect(lru.entries()).toEqual([['6', '666'], ['4', '4433'], ['3', '333'], ['5', '555']]);
|
expect(lru.entries()).toEqual([['6', '666'], ['4', '4433'], ['3', '333'], ['5', '555']]);
|
||||||
|
expect(lru.length === lru.lengthData && lru.length === lru.lengthRefs).toBe(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -22,17 +22,26 @@ export class LRUCache {
|
|||||||
readonly #refs: Map<string, LRUNode> = new Map();
|
readonly #refs: Map<string, LRUNode> = new Map();
|
||||||
|
|
||||||
#length = 0;
|
#length = 0;
|
||||||
#head: LRUNode = new LRUNode('head');
|
#head: LRUNode;
|
||||||
#tail: LRUNode = new LRUNode('tail');
|
#tail: LRUNode;
|
||||||
|
|
||||||
constructor (capacity = DEFAULT_CAPACITY) {
|
constructor (capacity = DEFAULT_CAPACITY) {
|
||||||
this.capacity = capacity;
|
this.capacity = capacity;
|
||||||
|
this.#head = this.#tail = new LRUNode('<empty>');
|
||||||
}
|
}
|
||||||
|
|
||||||
get length (): number {
|
get length (): number {
|
||||||
return this.#length;
|
return this.#length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get lengthData (): number {
|
||||||
|
return this.#data.size;
|
||||||
|
}
|
||||||
|
|
||||||
|
get lengthRefs (): number {
|
||||||
|
return this.#refs.size;
|
||||||
|
}
|
||||||
|
|
||||||
entries (): [string, unknown][] {
|
entries (): [string, unknown][] {
|
||||||
const keys = this.keys();
|
const keys = this.keys();
|
||||||
const entries = new Array<[string, unknown]>(keys.length);
|
const entries = new Array<[string, unknown]>(keys.length);
|
||||||
@@ -49,7 +58,7 @@ export class LRUCache {
|
|||||||
keys (): string[] {
|
keys (): string[] {
|
||||||
const keys: string[] = [];
|
const keys: string[] = [];
|
||||||
|
|
||||||
if (this.length) {
|
if (this.#length) {
|
||||||
let curr = this.#head;
|
let curr = this.#head;
|
||||||
|
|
||||||
while (curr !== this.#tail) {
|
while (curr !== this.#tail) {
|
||||||
@@ -67,7 +76,7 @@ export class LRUCache {
|
|||||||
const data = this.#data.get(key);
|
const data = this.#data.get(key);
|
||||||
|
|
||||||
if (data) {
|
if (data) {
|
||||||
this.#toFront(key);
|
this.#toHead(key);
|
||||||
|
|
||||||
return data as T;
|
return data as T;
|
||||||
}
|
}
|
||||||
@@ -77,7 +86,7 @@ export class LRUCache {
|
|||||||
|
|
||||||
set <T> (key: string, value: T): void {
|
set <T> (key: string, value: T): void {
|
||||||
if (this.#data.has(key)) {
|
if (this.#data.has(key)) {
|
||||||
this.#toFront(key);
|
this.#toHead(key);
|
||||||
} else {
|
} else {
|
||||||
const node = new LRUNode(key);
|
const node = new LRUNode(key);
|
||||||
|
|
||||||
@@ -96,7 +105,6 @@ export class LRUCache {
|
|||||||
this.#refs.delete(this.#tail.key);
|
this.#refs.delete(this.#tail.key);
|
||||||
|
|
||||||
this.#tail = this.#tail.prev;
|
this.#tail = this.#tail.prev;
|
||||||
this.#tail.next = this.#head;
|
|
||||||
} else {
|
} else {
|
||||||
this.#length += 1;
|
this.#length += 1;
|
||||||
}
|
}
|
||||||
@@ -105,7 +113,7 @@ export class LRUCache {
|
|||||||
this.#data.set(key, value);
|
this.#data.set(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
#toFront (key: string): void {
|
#toHead (key: string): void {
|
||||||
const ref = this.#refs.get(key);
|
const ref = this.#refs.get(key);
|
||||||
|
|
||||||
if (ref && ref !== this.#head) {
|
if (ref && ref !== this.#head) {
|
||||||
|
|||||||
Reference in New Issue
Block a user