Skip to content

Commit

Permalink
using getSpendPostTime in notes combine
Browse files Browse the repository at this point in the history
  • Loading branch information
patnir committed Mar 21, 2024
1 parent 9598d69 commit d8019dd
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 121 deletions.
121 changes: 2 additions & 119 deletions ironfish-cli/src/commands/wallet/notes/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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,
Expand Down Expand Up @@ -74,121 +72,6 @@ export class CombineNotesCommand extends IronfishCommand {
}),
}

private async getSpendPostTimeInMs(
client: RpcClient,
account: string,
forceBenchmark: boolean,
): Promise<number> {
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<number> {
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<EstimateFeeRatesResponse>,
) {
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<number> {
const spendsPerMinute = Math.max(Math.floor(60000 / spendPostTimeMs), 2) // minimum of 2 notes per minute in case the spentPostTime is very high

Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions ironfish-cli/src/utils/spendPostTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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')

Expand Down

0 comments on commit d8019dd

Please sign in to comment.