diff --git a/ironfish-cli/src/utils/spendPostTime.ts b/ironfish-cli/src/utils/spendPostTime.ts index cf43bb5067..f766c29b66 100644 --- a/ironfish-cli/src/utils/spendPostTime.ts +++ b/ironfish-cli/src/utils/spendPostTime.ts @@ -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 { diff --git a/ironfish/src/fileStores/internal.ts b/ironfish/src/fileStores/internal.ts index 1ce7251f42..0b2a62f030 100644 --- a/ironfish/src/fileStores/internal.ts +++ b/ironfish/src/fileStores/internal.ts @@ -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 } @@ -22,6 +23,7 @@ export const InternalOptionsDefaults: InternalOptions = { rpcAuthToken: '', networkId: DEFAULT_NETWORK_ID, spendPostTime: 0, + spendPostTimeMeasurements: 0, spendPostTimeAt: 0, }