From d8019dda30e6578a5cf08eef10817f2ff21169df Mon Sep 17 00:00:00 2001 From: Rahul Patni Date: Thu, 21 Mar 2024 10:39:43 -0700 Subject: [PATCH] using getSpendPostTime in notes combine --- .../src/commands/wallet/notes/combine.ts | 121 +----------------- ironfish-cli/src/utils/spendPostTime.ts | 4 +- 2 files changed, 4 insertions(+), 121 deletions(-) diff --git a/ironfish-cli/src/commands/wallet/notes/combine.ts b/ironfish-cli/src/commands/wallet/notes/combine.ts index 32cb6738c2..8925ee88a1 100644 --- a/ironfish-cli/src/commands/wallet/notes/combine.ts +++ b/ironfish-cli/src/commands/wallet/notes/combine.ts @@ -3,14 +3,11 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ import { Asset } from '@ironfish/rust-nodejs' import { - BenchUtils, CreateTransactionRequest, CurrencyUtils, - EstimateFeeRatesResponse, RawTransaction, RawTransactionSerde, RpcClient, - RpcResponseEnded, TimeUtils, Transaction, } from '@ironfish/sdk' @@ -21,6 +18,7 @@ import { IronFlag, RemoteFlags } from '../../../flags' import { getExplorer } from '../../../utils/explorer' import { selectFee } from '../../../utils/fees' import { fetchNotes } from '../../../utils/note' +import { getSpendPostTimeInMs } from '../../../utils/spendPostTime' import { displayTransactionSummary, TransactionTimer, @@ -74,121 +72,6 @@ export class CombineNotesCommand extends IronfishCommand { }), } - private async getSpendPostTimeInMs( - client: RpcClient, - account: string, - forceBenchmark: boolean, - ): Promise { - let spendPostTime = this.sdk.internal.get('spendPostTime') - - const spendPostTimeAt = this.sdk.internal.get('spendPostTimeAt') - - const shouldbenchmark = - forceBenchmark || - spendPostTime <= 0 || - Date.now() - spendPostTimeAt > 1000 * 60 * 60 * 24 * 30 // 1 month - - if (shouldbenchmark) { - spendPostTime = await this.benchmarkSpendPostTime(client, account) - - this.sdk.internal.set('spendPostTime', spendPostTime) - this.sdk.internal.set('spendPostTimeAt', Date.now()) - await this.sdk.internal.save() - } - - return spendPostTime - } - - private async benchmarkSpendPostTime(client: RpcClient, account: string): Promise { - const publicKey = ( - await client.wallet.getAccountPublicKey({ - account: account, - }) - ).content.publicKey - - const notes = await fetchNotes(client, account, 10) - - CliUx.ux.action.start('Measuring time to combine 1 note') - - const feeRates = await client.wallet.estimateFeeRates() - - /** Transaction 1: selects 1 note */ - - const txn1Params: CreateTransactionRequest = { - account: account, - outputs: [ - { - publicAddress: publicKey, - amount: CurrencyUtils.encode(BigInt(notes[0].value)), - memo: '', - }, - ], - fee: null, - feeRate: null, - notes: [notes[0].noteHash], - } - - /** Transaction 2: selects two notes */ - - const txn2Params: CreateTransactionRequest = { - account: account, - outputs: [ - { - publicAddress: publicKey, - amount: CurrencyUtils.encode(BigInt(notes[0].value) + BigInt(notes[1].value)), - memo: '', - }, - ], - fee: null, - feeRate: null, - notes: [notes[0].noteHash, notes[1].noteHash], - } - - const promisesTxn1 = [] - const promisesTxn2 = [] - - for (let i = 0; i < 3; i++) { - promisesTxn1.push(this.measureTransactionPostTime(client, txn1Params, feeRates)) - promisesTxn2.push(this.measureTransactionPostTime(client, txn2Params, feeRates)) - } - - const resultTxn1 = await Promise.all(promisesTxn1) - const resultTxn2 = await Promise.all(promisesTxn2) - - const delta = Math.ceil( - (resultTxn2.reduce((acc, curr) => acc + curr, 0) - - resultTxn1.reduce((acc, curr) => acc + curr, 0)) / - 3, - ) - - CliUx.ux.action.stop(TimeUtils.renderSpan(delta)) - - return delta - } - - private async measureTransactionPostTime( - client: RpcClient, - params: CreateTransactionRequest, - feeRates: RpcResponseEnded, - ) { - const response = await client.wallet.createTransaction({ - ...params, - feeRate: feeRates.content.fast, - }) - - const bytes = Buffer.from(response.content.transaction, 'hex') - const raw = RawTransactionSerde.deserialize(bytes) - - const start = BenchUtils.start() - - await client.wallet.postTransaction({ - transaction: RawTransactionSerde.serialize(raw).toString('hex'), - broadcast: false, - }) - - return BenchUtils.end(start) - } - private async selectNotesToCombine(spendPostTimeMs: number): Promise { const spendsPerMinute = Math.max(Math.floor(60000 / spendPostTimeMs), 2) // minimum of 2 notes per minute in case the spentPostTime is very high @@ -349,7 +232,7 @@ export class CombineNotesCommand extends IronfishCommand { await this.ensureUserHasEnoughNotesToCombine(client, from) - const spendPostTime = await this.getSpendPostTimeInMs(client, from, flags.benchmark) + const spendPostTime = await getSpendPostTimeInMs(this.sdk, client, from, flags.benchmark) let numberOfNotes = flags.notes diff --git a/ironfish-cli/src/utils/spendPostTime.ts b/ironfish-cli/src/utils/spendPostTime.ts index 8d12e5b37b..8f9964ac6b 100644 --- a/ironfish-cli/src/utils/spendPostTime.ts +++ b/ironfish-cli/src/utils/spendPostTime.ts @@ -13,7 +13,7 @@ import { TimeUtils, } from '@ironfish/sdk' import { CliUx } from '@oclif/core' -import { CombineNotesCommand } from '../commands/wallet/notes/combine' +import { fetchNotes } from './note' export async function getSpendPostTimeInMs( sdk: IronfishSdk, @@ -48,7 +48,7 @@ async function benchmarkSpendPostTime(client: RpcClient, account: string): Promi }) ).content.publicKey - const notes = await CombineNotesCommand.fetchNotes(client, account, 10) + const notes = await fetchNotes(client, account, 10) CliUx.ux.action.start('Measuring time to combine 1 note')