From 3b28ce3fb0a1468ef3499dd30ba038019189f126 Mon Sep 17 00:00:00 2001 From: Ermal Kaleci Date: Sat, 1 Jun 2024 10:25:14 +0200 Subject: [PATCH] remove fetch status --- packages/chopsticks/src/context.ts | 4 -- packages/chopsticks/src/logger.ts | 17 --------- packages/core/src/api.ts | 59 +++++++++--------------------- packages/core/src/setup.ts | 7 ---- 4 files changed, 18 insertions(+), 69 deletions(-) diff --git a/packages/chopsticks/src/context.ts b/packages/chopsticks/src/context.ts index eaf76425..0f8b33ce 100644 --- a/packages/chopsticks/src/context.ts +++ b/packages/chopsticks/src/context.ts @@ -4,7 +4,6 @@ import { Config } from './schema/index.js' import { HexString } from '@polkadot/util/types' import { SqliteDatabase } from '@acala-network/chopsticks-db' import { overrideStorage, overrideWasm } from './utils/override.js' -import { statusFetching } from './logger.js' import axios from 'axios' const logger = defaultLogger.child({ name: 'setup-context' }) @@ -48,9 +47,6 @@ export const setupContext = async (argv: Config, overrideParent = false) => { offchainWorker: argv['offchain-worker'], maxMemoryBlockCount: argv['max-memory-block-count'], processQueuedMessages: argv['process-queued-messages'], - hooks: { - apiFetching: statusFetching, - }, }) // load block from db diff --git a/packages/chopsticks/src/logger.ts b/packages/chopsticks/src/logger.ts index c3f2d1fe..16046098 100644 --- a/packages/chopsticks/src/logger.ts +++ b/packages/chopsticks/src/logger.ts @@ -1,18 +1 @@ -import _ from 'lodash' - export { defaultLogger, truncate } from '@acala-network/chopsticks-core' - -export const spinnerFrames = - process.platform === 'win32' ? ['-', '\\', '|', '/'] : ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'] -let index = 0 - -const clearStatus = _.debounce(() => process.stdout.clearLine(1), 500, { trailing: true }) - -export const statusFetching = () => { - if (!process.stdout.clearLine) return - if (process.env['CI'] || process.env['VITEST'] || process.env['TEST']) return - process.stdout.write(` ${spinnerFrames[index++]} Fetching`) - process.stdout.cursorTo(0) - index = ++index % spinnerFrames.length - clearStatus() -} diff --git a/packages/core/src/api.ts b/packages/core/src/api.ts index cbb8c8a1..36a290d3 100644 --- a/packages/core/src/api.ts +++ b/packages/core/src/api.ts @@ -26,10 +26,6 @@ export class Api { readonly signedExtensions: ExtDef - #apiHooks: { - fetching?: () => void - } = {} - constructor(provider: ProviderInterface, signedExtensions?: ExtDef) { this.#provider = provider this.signedExtensions = signedExtensions || {} @@ -72,39 +68,20 @@ export class Api { return this.#chainProperties } - // Set the hook function to be called when api fetch endpoint. - // This hook is throttled and won't be called more than once in 50ms timeframe. - onFetching(fetching?: () => void) { - this.#apiHooks.fetching = fetching - } - - fetching = _.throttle( - () => { - this.#apiHooks.fetching?.() - }, - 50, - { leading: true }, - ) - - async send(method: string, params: unknown[], isCacheable?: boolean): Promise { - this.fetching() - return this.#provider.send(method, params, isCacheable) - } - async getSystemName() { - return this.send('system_name', []) + return this.#provider.send('system_name', []) } async getSystemProperties() { - return this.send('system_properties', []) + return this.#provider.send('system_properties', []) } async getSystemChain() { - return this.send('system_chain', []) + return this.#provider.send('system_chain', []) } async getBlockHash(blockNumber?: number) { - return this.send( + return this.#provider.send( 'chain_getBlockHash', Number.isInteger(blockNumber) ? [blockNumber] : [], !!blockNumber, @@ -112,11 +89,11 @@ export class Api { } async getHeader(hash?: string) { - return this.send
('chain_getHeader', hash ? [hash] : [], !!hash) + return this.#provider.send
('chain_getHeader', hash ? [hash] : [], !!hash) } async getBlock(hash?: string) { - return this.send('chain_getBlock', hash ? [hash] : [], !!hash) + return this.#provider.send('chain_getBlock', hash ? [hash] : [], !!hash) } async getStorage(key: string, hash?: string) { @@ -125,12 +102,12 @@ export class Api { // child storage key, use childstate_getStorage const params = [child, storageKey] if (hash) params.push(hash as HexString) - return this.send('childstate_getStorage', params, !!hash) + return this.#provider.send('childstate_getStorage', params, !!hash) } else { // main storage key, use state_getStorage const params = [key] if (hash) params.push(hash) - return this.send('state_getStorage', params, !!hash) + return this.#provider.send('state_getStorage', params, !!hash) } } @@ -141,14 +118,14 @@ export class Api { // strip child prefix from startKey const params = [child, storageKey, pageSize, stripChildPrefix(startKey as HexString)] if (hash) params.push(hash as HexString) - return this.send('childstate_getKeysPaged', params, !!hash).then((keys) => - keys.map((key) => prefixedChildKey(child, key)), - ) + return this.#provider + .send('childstate_getKeysPaged', params, !!hash) + .then((keys) => keys.map((key) => prefixedChildKey(child, key))) } else { // main storage key, use state_getKeysPaged const params = [prefix, pageSize, startKey] if (hash) params.push(hash) - return this.send('state_getKeysPaged', params, !!hash) + return this.#provider.send('state_getKeysPaged', params, !!hash) } } @@ -159,16 +136,16 @@ export class Api { // strip child prefix from keys const params: any[] = [child, keys.map((key) => stripChildPrefix(key))] if (hash) params.push(hash) - return this.send('childstate_getStorageEntries', params, !!hash).then( - (values) => _.zip(keys, values) as [HexString, HexString | null][], - ) + return this.#provider + .send('childstate_getStorageEntries', params, !!hash) + .then((values) => _.zip(keys, values) as [HexString, HexString | null][]) } else { // main storage key, use state_getStorageAt const params: any[] = [keys] if (hash) params.push(hash) - return this.send('state_queryStorageAt', params, !!hash).then( - (result) => (result[0]?.['changes'] as [HexString, HexString | null][]) || [], - ) + return this.#provider + .send('state_queryStorageAt', params, !!hash) + .then((result) => (result[0]?.['changes'] as [HexString, HexString | null][]) || []) } } diff --git a/packages/core/src/setup.ts b/packages/core/src/setup.ts index bf4cf088..5b9a499d 100644 --- a/packages/core/src/setup.ts +++ b/packages/core/src/setup.ts @@ -25,9 +25,6 @@ export type SetupOptions = { offchainWorker?: boolean maxMemoryBlockCount?: number processQueuedMessages?: boolean - hooks?: { - apiFetching?: () => void - } } export const processOptions = async (options: SetupOptions) => { @@ -42,10 +39,6 @@ export const processOptions = async (options: SetupOptions) => { provider = new WsProvider(options.endpoint, 3_000) } const api = new Api(provider) - - // setup api hooks - api.onFetching(options.hooks?.apiFetching) - await api.isReady let blockHash: string