Skip to content

Commit

Permalink
reflect feedback: adjust for naming convention
Browse files Browse the repository at this point in the history
  • Loading branch information
Intizar-T committed May 22, 2024
1 parent 5a70839 commit 9b1bd39
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 27 deletions.
10 changes: 5 additions & 5 deletions api/listener/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type ListenerInsertModel struct {
Chain string `db:"chain_name" json:"chain" validate:"required"`
}

type ListenerObservedBlockModel struct {
type ObservedBlockModel struct {
BlockKey string `db:"block_key" json:"blockKey" validate:"required"`
BlockNumber int64 `db:"block_number" json:"blockNumber" validate:"blockNumberValidator"`
}
Expand Down Expand Up @@ -158,7 +158,7 @@ func deleteById(c *fiber.Ctx) error {
}

func upsertObservedBlock(c *fiber.Ctx) error {
payload := new(ListenerObservedBlockModel)
payload := new(ObservedBlockModel)
if err := c.BodyParser(payload); err != nil {
return err
}
Expand All @@ -171,16 +171,16 @@ func upsertObservedBlock(c *fiber.Ctx) error {
return err
}

result, err := utils.QueryRow[ListenerObservedBlockModel](c, UpsertObservedBlock, map[string]any{"block_key": payload.BlockKey, "block_number": payload.BlockNumber})
result, err := utils.QueryRow[ObservedBlockModel](c, UpsertObservedBlock, map[string]any{"block_key": payload.BlockKey, "block_number": payload.BlockNumber})
if err != nil {
return err
}
return c.JSON(result)
}

func getObservedBlock(c *fiber.Ctx) error {
block_key := c.Query("blockKey")
result, err := utils.QueryRow[ListenerObservedBlockModel](c, GetObservedBlock, map[string]any{"block_key": block_key})
blockKey := c.Query("blockKey")
result, err := utils.QueryRow[ObservedBlockModel](c, GetObservedBlock, map[string]any{"block_key": blockKey})
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions api/listener/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ const (
`

GetObservedBlock = `
SELECT * FROM observed_block
SELECT * FROM observed_blocks
WHERE block_key = @block_key
LIMIT 1;
`

UpsertObservedBlock = `
INSERT INTO observed_block (block_key, block_number)
INSERT INTO observed_blocks (block_key, block_number)
VALUES (@block_key, @block_number)
ON CONFLICT (block_key)
DO UPDATE SET block_number = @block_number
Expand Down
2 changes: 1 addition & 1 deletion api/migrations/000002_add_observed_block.down.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DROP TABLE IF EXISTS "observed_block";
DROP TABLE IF EXISTS "observed_blocks";
4 changes: 2 additions & 2 deletions api/migrations/000002_add_observed_block.up.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE IF NOT EXISTS "observed_block" (
CREATE TABLE IF NOT EXISTS "observed_blocks" (
block_key TEXT NOT NULL,
block_number BIGINT NOT NULL,
CONSTRAINT "observed_block_key" UNIQUE ("block_key")
CONSTRAINT "observed_blocks_key" UNIQUE ("block_key")
)
4 changes: 2 additions & 2 deletions core/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ export enum OraklErrorCode {
AxiosNotSupported,
AxiosInvalidUrl,
FailedToConnectAPI,
UpsertListenerObservedBlockFailed,
GetListenerObservedBlockFailed
UpsertObservedBlockFailed,
GetObservedBlockFailed
}
24 changes: 16 additions & 8 deletions core/src/listener/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ export async function getListener({
}
}

export async function getListenerObservedBlock({
/**
* Get observed block number from the Orakl Network API for a given contract address
*
* @param {string} blockKey
* @param {pino.Logger} logger
* @return {Promise<IObservedBlock>}
* @exception {OraklErrorCode.GetObservedBlockFailed}
*/
export async function getObservedBlock({
blockKey,
logger
}: {
Expand All @@ -71,8 +79,8 @@ export async function getListenerObservedBlock({
const endpoint = buildUrl(ORAKL_NETWORK_API_URL, `listener/observed-block?blockKey=${blockKey}`)
return (await axios.get(endpoint))?.data
} catch (e) {
logger?.error({ name: 'getListenerObservedBlock', file: FILE_NAME, ...e }, 'error')
throw new OraklError(OraklErrorCode.GetListenerObservedBlockFailed)
logger?.error({ name: 'getObservedBlock', file: FILE_NAME, ...e }, 'error')
throw new OraklError(OraklErrorCode.GetObservedBlockFailed)
}
}

Expand All @@ -82,10 +90,10 @@ export async function getListenerObservedBlock({
* @param {string} blockKey
* @param {number} blockNumber
* @param {pino.Logger} logger
* @return {Promise<IListenerObservedBlock>}
* @exception {UpsertListenerObservedBlockFailed}
* @return {Promise<IObservedBlock>}
* @exception {OraklErrorCode.UpsertObservedBlockFailed}
*/
export async function upsertListenerObservedBlock({
export async function upsertObservedBlock({
blockKey,
blockNumber,
logger
Expand All @@ -98,7 +106,7 @@ export async function upsertListenerObservedBlock({
const endpoint = buildUrl(ORAKL_NETWORK_API_URL, 'listener/observed-block')
return (await axios.post(endpoint, { blockKey, blockNumber }))?.data
} catch (e) {
logger?.error({ name: 'upsertListenerObservedBlock', file: FILE_NAME, ...e }, 'error')
throw new OraklError(OraklErrorCode.UpsertListenerObservedBlockFailed)
logger?.error({ name: 'upsertObservedBlock', file: FILE_NAME, ...e }, 'error')
throw new OraklError(OraklErrorCode.UpsertObservedBlockFailed)
}
}
8 changes: 4 additions & 4 deletions core/src/listener/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Logger } from 'pino'
import type { RedisClientType } from 'redis'
import { BULLMQ_CONNECTION, getObservedBlockRedisKey, LISTENER_JOB_SETTINGS } from '../settings'
import { IListenerConfig } from '../types'
import { upsertListenerObservedBlock } from './api'
import { upsertObservedBlock } from './api'
import { State } from './state'
import { IHistoryListenerJob, ILatestListenerJob, ProcessEventOutputType } from './types'
import { watchman } from './watchman'
Expand Down Expand Up @@ -155,8 +155,8 @@ function latestJob({
}: {
state: State
historyListenerQueue: Queue
redisClient: RedisClientType
workerQueue: Queue
redisClient: RedisClientType
processFn: (log: ethers.Event) => Promise<ProcessEventOutputType | undefined>
logger: Logger
}) {
Expand Down Expand Up @@ -219,7 +219,7 @@ function latestJob({
logger.debug(`Listener submitted job [${jobId}] for [${jobName}]`)
}
}
await upsertListenerObservedBlock({
await upsertObservedBlock({
blockKey: observedBlockRedisKey,
blockNumber,
logger: this.logger
Expand Down Expand Up @@ -308,7 +308,7 @@ function historyJob({
}

if (blockNumber > observedBlock) {
await upsertListenerObservedBlock({
await upsertObservedBlock({
blockKey: observedBlockRedisKey,
blockNumber,
logger
Expand Down
6 changes: 3 additions & 3 deletions core/src/listener/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
PROVIDER_URL
} from '../settings'
import { IListenerConfig, IListenerRawConfig } from '../types'
import { getListenerObservedBlock, getListeners, upsertListenerObservedBlock } from './api'
import { getListeners, getObservedBlock, upsertObservedBlock } from './api'
import { IContracts, ILatestListenerJob } from './types'
import { postprocessListeners } from './utils'

Expand Down Expand Up @@ -175,7 +175,7 @@ export class State {
const contractAddress = toAddListener.address
const observedBlockRedisKey = getObservedBlockRedisKey(contractAddress)
const latestBlock = await this.latestBlockNumber()
const { blockKey: observedBlockKey } = await getListenerObservedBlock({
const { blockKey: observedBlockKey } = await getObservedBlock({
blockKey: observedBlockRedisKey,
logger: this.logger
})
Expand All @@ -188,7 +188,7 @@ export class State {
* it does not exist -> upsert latestBlock
*/
if (observedBlockKey === '') {
await upsertListenerObservedBlock({
await upsertObservedBlock({
blockKey: observedBlockRedisKey,
blockNumber: Math.max(latestBlock - 1, 0),
logger: this.logger
Expand Down

0 comments on commit 9b1bd39

Please sign in to comment.