diff --git a/packages/api-contract/src/base/Base.ts b/packages/api-contract/src/base/Base.ts index e2c93754df..71dd0c296d 100644 --- a/packages/api-contract/src/base/Base.ts +++ b/packages/api-contract/src/base/Base.ts @@ -23,7 +23,8 @@ export abstract class Base { this.api = api; 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'); } diff --git a/packages/rpc-provider/src/lru.spec.ts b/packages/rpc-provider/src/lru.spec.ts index 6d675884fa..c49c3b25d8 100644 --- a/packages/rpc-provider/src/lru.spec.ts +++ b/packages/rpc-provider/src/lru.spec.ts @@ -11,6 +11,7 @@ describe('LRUCache', (): void => { keys.forEach((k) => lru.set(k, `${k}${k}${k}`)); 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}`)); }); @@ -22,6 +23,7 @@ describe('LRUCache', (): void => { keys.forEach((k) => lru.set(k, `${k}${k}${k}`)); 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}`)); }); @@ -33,17 +35,21 @@ describe('LRUCache', (): void => { keys.forEach((k) => lru.set(k, `${k}${k}${k}`)); 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'); 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'); 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'); expect(lru.entries()).toEqual([['6', '666'], ['4', '4433'], ['3', '333'], ['5', '555']]); + expect(lru.length === lru.lengthData && lru.length === lru.lengthRefs).toBe(true); }); }); diff --git a/packages/rpc-provider/src/lru.ts b/packages/rpc-provider/src/lru.ts index 3a1935e2f7..ccf18bab2a 100644 --- a/packages/rpc-provider/src/lru.ts +++ b/packages/rpc-provider/src/lru.ts @@ -22,17 +22,26 @@ export class LRUCache { readonly #refs: Map = new Map(); #length = 0; - #head: LRUNode = new LRUNode('head'); - #tail: LRUNode = new LRUNode('tail'); + #head: LRUNode; + #tail: LRUNode; constructor (capacity = DEFAULT_CAPACITY) { this.capacity = capacity; + this.#head = this.#tail = new LRUNode(''); } get length (): number { return this.#length; } + get lengthData (): number { + return this.#data.size; + } + + get lengthRefs (): number { + return this.#refs.size; + } + entries (): [string, unknown][] { const keys = this.keys(); const entries = new Array<[string, unknown]>(keys.length); @@ -49,7 +58,7 @@ export class LRUCache { keys (): string[] { const keys: string[] = []; - if (this.length) { + if (this.#length) { let curr = this.#head; while (curr !== this.#tail) { @@ -67,7 +76,7 @@ export class LRUCache { const data = this.#data.get(key); if (data) { - this.#toFront(key); + this.#toHead(key); return data as T; } @@ -77,7 +86,7 @@ export class LRUCache { set (key: string, value: T): void { if (this.#data.has(key)) { - this.#toFront(key); + this.#toHead(key); } else { const node = new LRUNode(key); @@ -96,7 +105,6 @@ export class LRUCache { this.#refs.delete(this.#tail.key); this.#tail = this.#tail.prev; - this.#tail.next = this.#head; } else { this.#length += 1; } @@ -105,7 +113,7 @@ export class LRUCache { this.#data.set(key, value); } - #toFront (key: string): void { + #toHead (key: string): void { const ref = this.#refs.get(key); if (ref && ref !== this.#head) {