Skip to content

Commit

Permalink
storing spend post time as the avergae of all measurements
Browse files Browse the repository at this point in the history
  • Loading branch information
patnir committed Mar 29, 2024
1 parent fc79724 commit 7091806
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
24 changes: 16 additions & 8 deletions ironfish-cli/src/utils/spendPostTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,23 @@ export async function updateSpendPostTimeInMs(
startTime: number,
endTime: number,
) {
const totalTime = endTime - startTime
const transactionSpendPostTime = Math.ceil(totalTime / raw.spends.length)
let spendPostTime = sdk.internal.get('spendPostTime')
if (transactionSpendPostTime > spendPostTime) {
spendPostTime = transactionSpendPostTime
sdk.internal.set('spendPostTime', spendPostTime)
sdk.internal.set('spendPostTimeAt', Date.now())
await sdk.internal.save()
const total = endTime - startTime
const current = Math.ceil(total / raw.spends.length)

const old = sdk.internal.get('spendPostTime')
const oldCount = sdk.internal.get('spendPostTimeMeasurements')

const newCount = oldCount + 1
let newSpendPostTime = current

if (oldCount > 1) {
newSpendPostTime = (old * oldCount + current) / newCount
}

sdk.internal.set('spendPostTime', newSpendPostTime)
sdk.internal.set('spendPostTimeAt', Date.now())
sdk.internal.set('spendPostTimeMeasurements', newCount)
await sdk.internal.save()
}

export function getSpendPostTimeInMs(sdk: IronfishSdk): number {
Expand Down
2 changes: 2 additions & 0 deletions ironfish/src/fileStores/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type InternalOptions = {
rpcAuthToken: string
networkId: number
spendPostTime: number // in milliseconds
spendPostTimeMeasurements: number // used to calculate the average spendPostTime
spendPostTimeAt: number // when the spend post time measurement was done
}

Expand All @@ -22,6 +23,7 @@ export const InternalOptionsDefaults: InternalOptions = {
rpcAuthToken: '',
networkId: DEFAULT_NETWORK_ID,
spendPostTime: 0,
spendPostTimeMeasurements: 0,
spendPostTimeAt: 0,
}

Expand Down

0 comments on commit 7091806

Please sign in to comment.