Skip to content

Commit

Permalink
don't show measuring messages when no notes exist
Browse files Browse the repository at this point in the history
  • Loading branch information
patnir committed Mar 21, 2024
1 parent a1b97ea commit 8497a4f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
36 changes: 23 additions & 13 deletions ironfish-cli/src/utils/spendPostTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,29 @@ export async function getSpendPostTimeInMs(
account: string,
forceBenchmark: boolean,
): Promise<number> {
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<number> {
Expand All @@ -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()
Expand Down
18 changes: 17 additions & 1 deletion ironfish-cli/src/utils/timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -52,6 +64,10 @@ export class TransactionTimer {
}

end() {
if (this.estimateInMs === 0) {
return
}

if (!this.progressBar || !this.startTime || !this.timer) {
return
}
Expand Down

0 comments on commit 8497a4f

Please sign in to comment.