Skip to content

Commit

Permalink
use latest value from redis
Browse files Browse the repository at this point in the history
  • Loading branch information
plusminushalf committed Nov 25, 2024
1 parent 12de6ac commit 9e2f16d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/handlers/gasPriceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ export class GasPriceManager {
return await this.updateBaseFee()
}

let baseFee = this.baseFeePerGasQueue.getLatestValue()
let baseFee = await this.baseFeePerGasQueue.getLatestValue()
if (!baseFee) {
baseFee = await this.updateBaseFee()
}
Expand All @@ -407,9 +407,9 @@ export class GasPriceManager {
return await this.updateGasPrice()
}

const maxFeePerGas = this.maxFeePerGasQueue.getLatestValue()
const maxFeePerGas = await this.maxFeePerGasQueue.getLatestValue()
const maxPriorityFeePerGas =
this.maxPriorityFeePerGasQueue.getLatestValue()
await this.maxPriorityFeePerGasQueue.getLatestValue()

if (!maxFeePerGas || !maxPriorityFeePerGas) {
throw new RpcError("No gas price available")
Expand Down
35 changes: 26 additions & 9 deletions src/utils/timedQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Logger } from "pino"

export interface TimedQueue {
saveValue(value: bigint): Promise<void>
getLatestValue(): bigint | null
getLatestValue(): Promise<bigint | null>
getMinValue(): Promise<bigint | undefined>
getMaxValue(): Promise<bigint | undefined>
isEmpty(): Promise<boolean>
Expand All @@ -15,7 +15,6 @@ export class RedisTimedQueue implements TimedQueue {
private redisClient: Redis
private queueKey: string
private queueValidity: number
private latestValue: bigint | null
private logger: Logger
private tag: string

Expand All @@ -33,7 +32,6 @@ export class RedisTimedQueue implements TimedQueue {
this.tag = `${tag}-${config.publicClient.chain.id}`
this.queueKey = `${config.redisGasPriceQueueName}-${this.tag}`
this.queueValidity = queueValidity
this.latestValue = null

this.logger = config.getLogger(
{ module: "RedisTimedQueue" },
Expand Down Expand Up @@ -80,11 +78,30 @@ export class RedisTimedQueue implements TimedQueue {
timestamp.toString(),
value.toString()
)
this.latestValue = value
}

public getLatestValue(): bigint | null {
return this.latestValue
public async getLatestValue(): Promise<bigint | null> {
const allEntries = await this.redisClient.zrange(
this.queueKey,
0,
-1,
"WITHSCORES"
)

// sort all entries by timestamp
let latestValue: bigint | null = null
let latestTimestamp = 0

for (let i = 0; i < allEntries.length; i += 2) {
const value = BigInt(allEntries[i])
const timestamp = Number.parseInt(allEntries[i + 1])
if (timestamp > latestTimestamp) {
latestTimestamp = timestamp
latestValue = value
}
}

return latestValue
}

public async getMinValue(): Promise<bigint | undefined> {
Expand Down Expand Up @@ -159,17 +176,17 @@ export class MemoryTimedQueue implements TimedQueue {
return Promise.resolve()
}

public getLatestValue(): bigint | null {
public getLatestValue(): Promise<bigint | null> {
if (this.queue.length === 0) {
return null
return Promise.resolve(null)
}

this.logger.info(
{ value: this.queue[this.queue.length - 1].value },
"[MemoryTimedQueue] Getting latest value"
)

return this.queue[this.queue.length - 1].value
return Promise.resolve(this.queue[this.queue.length - 1].value)
}

public getMinValue(): Promise<bigint | undefined> {
Expand Down

0 comments on commit 9e2f16d

Please sign in to comment.