From 8497a4feb2f548dde2c8eeda55b5245ab7182809 Mon Sep 17 00:00:00 2001 From: Rahul Patni Date: Tue, 19 Mar 2024 10:46:05 -0700 Subject: [PATCH] don't show measuring messages when no notes exist --- ironfish-cli/src/utils/spendPostTime.ts | 36 ++++++++++++++++--------- ironfish-cli/src/utils/timer.ts | 18 ++++++++++++- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/ironfish-cli/src/utils/spendPostTime.ts b/ironfish-cli/src/utils/spendPostTime.ts index 8f9964ac6b..903b75cf6d 100644 --- a/ironfish-cli/src/utils/spendPostTime.ts +++ b/ironfish-cli/src/utils/spendPostTime.ts @@ -21,24 +21,29 @@ export async function getSpendPostTimeInMs( account: string, forceBenchmark: boolean, ): Promise { - let spendPostTime = sdk.internal.get('spendPostTime') + try { + let spendPostTime = sdk.internal.get('spendPostTime') - const spendPostTimeAt = sdk.internal.get('spendPostTimeAt') + const spendPostTimeAt = sdk.internal.get('spendPostTimeAt') - const shouldbenchmark = - forceBenchmark || - spendPostTime <= 0 || - Date.now() - spendPostTimeAt > 1000 * 60 * 60 * 24 * 30 // 1 month + const shouldbenchmark = + forceBenchmark || + spendPostTime <= 0 || + Date.now() - spendPostTimeAt > 1000 * 60 * 60 * 24 * 30 // 1 month - if (shouldbenchmark) { - spendPostTime = await benchmarkSpendPostTime(client, account) + if (shouldbenchmark) { + spendPostTime = await benchmarkSpendPostTime(client, account) - sdk.internal.set('spendPostTime', spendPostTime) - sdk.internal.set('spendPostTimeAt', Date.now()) - await sdk.internal.save() - } + sdk.internal.set('spendPostTime', spendPostTime) + sdk.internal.set('spendPostTimeAt', Date.now()) + await sdk.internal.save() + } - return spendPostTime + return spendPostTime + } catch (e) { + // if benchmarking fails, return 0. The consumer of this function should not show an estimate + return 0 + } } async function benchmarkSpendPostTime(client: RpcClient, account: string): Promise { @@ -50,6 +55,11 @@ async function benchmarkSpendPostTime(client: RpcClient, account: string): Promi const notes = await fetchNotes(client, account, 10) + // Not enough notes in the account to measure the time to combine a note + if (notes.length < 3) { + return 0 + } + CliUx.ux.action.start('Measuring time to combine 1 note') const feeRates = await client.wallet.estimateFeeRates() diff --git a/ironfish-cli/src/utils/timer.ts b/ironfish-cli/src/utils/timer.ts index 8c5a9c1f5e..b916a16707 100644 --- a/ironfish-cli/src/utils/timer.ts +++ b/ironfish-cli/src/utils/timer.ts @@ -14,10 +14,18 @@ export class TransactionTimer { constructor(spendPostTime: number, raw: RawTransaction, logger?: Logger) { this.logger = logger ?? createRootLogger() - this.estimateInMs = Math.max(Math.ceil(spendPostTime * raw.spends.length), 1000) + + // if spendPostTime is 0, there means that there was an issue measuring the spendPostTime + // we will not show the progress bar in this case + + this.estimateInMs = + spendPostTime > 0 ? Math.max(Math.ceil(spendPostTime * raw.spends.length), 1000) : 0 } displayEstimate() { + if (this.estimateInMs === 0) { + return + } this.logger.log( `Time to send: ${TimeUtils.renderSpan(this.estimateInMs, { hideMilliseconds: true, @@ -26,6 +34,10 @@ export class TransactionTimer { } start() { + if (this.estimateInMs === 0) { + return + } + this.progressBar = CliUx.ux.progress({ format: '{title}: [{bar}] {percentage}% | {estimate}', }) as ProgressBar @@ -52,6 +64,10 @@ export class TransactionTimer { } end() { + if (this.estimateInMs === 0) { + return + } + if (!this.progressBar || !this.startTime || !this.timer) { return }