Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cache overwrite #182

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions zp-relayer/services/fee/FeeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface IUserFeeOptions {
denominate(denominator: BN): this
convert(priceFeed: IPriceFeed): Promise<this>
getObject(): Record<string, string>
clone(): this
}

export class DefaultUserFeeOptions implements IUserFeeOptions {
Expand All @@ -38,6 +39,10 @@ export class DefaultUserFeeOptions implements IUserFeeOptions {
return this
}

clone() {
return new DefaultUserFeeOptions(this.fee.clone()) as this
}

getObject() {
return {
fee: this.fee.toString(10),
Expand Down Expand Up @@ -108,7 +113,7 @@ export abstract class FeeManager {
}

async getFeeOptions(params: IGetFeesParams, useCached = true): Promise<IUserFeeOptions> {
if (useCached && this.cachedFeeOptions) return this.cachedFeeOptions
if (useCached && this.cachedFeeOptions) return this.cachedFeeOptions.clone()
let feeOptions: IUserFeeOptions
try {
feeOptions = await this.fetchFeeOptions(params)
Expand All @@ -117,7 +122,7 @@ export abstract class FeeManager {
logger.error('Failed to fetch fee options', e)
if (!this.cachedFeeOptions) throw e
logger.debug('Fallback to cache fee options')
feeOptions = this.cachedFeeOptions
feeOptions = this.cachedFeeOptions.clone()
}
return feeOptions
}
Expand Down
5 changes: 5 additions & 0 deletions zp-relayer/services/fee/OptimismFeeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class OptimismUserFeeOptions implements IUserFeeOptions {
return this
}

clone(): this {
return new OptimismUserFeeOptions(this.baseFee.clone(), this.oneByteFee.clone()) as this
}

getObject() {
return {
fee: this.baseFee.toString(10),
Expand Down Expand Up @@ -98,6 +102,7 @@ export class OptimismFeeManager extends FeeManager {
}

async _fetchFeeOptions({ gasLimit }: IGetFeesParams): Promise<OptimismUserFeeOptions> {
// TODO: add RLP encoding overhead to baseFee
const baseFee = await FeeManager.estimateExecutionFee(this.gasPrice, gasLimit)

const l1BaseFee = await contractCallRetry(this.oracle, 'l1BaseFee').then(toBN)
Expand Down