Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Jun 21, 2024
1 parent a9a106e commit 7b55ede
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 130 deletions.
90 changes: 90 additions & 0 deletions src/db/block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { ModelStatic, Op } from 'sequelize'

import { Block, DependableEventModel } from '@/types'

import { getDependableEventModels } from './dependable'

/**
* Get the latest block before or equal to the requested block height.
*/
export const getBlockForHeight = async (
blockHeight: bigint,
// Optionally set a minimum.
after = 0n
): Promise<Block | undefined> => {
const events = await Promise.all(
getDependableEventModels().map((DependableEventModel) =>
(
DependableEventModel as unknown as ModelStatic<DependableEventModel>
).findOne({
where: {
[DependableEventModel.blockHeightKey]: {
[Op.gt]: after,
[Op.lte]: blockHeight,
},
},
order: [[DependableEventModel.blockHeightKey, 'DESC']],
})
)
)

// Choose latest block.
return events
.flatMap((event) => event?.block || [])
.sort((a, b) => Number(b.height - a.height))[0]
}

/**
* Get the latest block before or equal to the requested block time.
*/
export const getBlockForTime = async (
blockTimeUnixMs: bigint,
// Optionally set a minimum.
after = 0n
): Promise<Block | undefined> => {
const events = await Promise.all(
getDependableEventModels().map((DependableEventModel) =>
(
DependableEventModel as unknown as ModelStatic<DependableEventModel>
).findOne({
where: {
[DependableEventModel.blockTimeUnixMsKey]: {
[Op.gt]: after,
[Op.lte]: blockTimeUnixMs,
},
},
order: [[DependableEventModel.blockTimeUnixMsKey, 'DESC']],
})
)
)

// Choose latest block.
return events
.flatMap((event) => event?.block || [])
.sort((a, b) => Number(b.height - a.height))[0]
}

/**
* Get the first block.
*/
export const getFirstBlock = async (): Promise<Block | undefined> => {
const events = await Promise.all(
getDependableEventModels().map((DependableEventModel) =>
(
DependableEventModel as unknown as ModelStatic<DependableEventModel>
).findOne({
where: {
[DependableEventModel.blockHeightKey]: {
[Op.gt]: 0,
},
},
order: [[DependableEventModel.blockHeightKey, 'ASC']],
})
)
)

// Choose first block.
return events
.flatMap((event) => event?.block || [])
.sort((a, b) => Number(a.height - b.height))[0]
}
1 change: 1 addition & 0 deletions src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './computation'
export * from './connection'
export * from './dependable'
export * from './setup'
export * from './block'
2 changes: 1 addition & 1 deletion src/formulas/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ContractFormula,
FormulaType,
FormulaTypeValues,
GenericFormula,
NestedFormulaMap,
TypedFormula,
Expand Down Expand Up @@ -85,6 +86,5 @@ export const getTypedFormula = (
} as TypedFormula
}

export const FormulaTypeValues = Object.values(FormulaType)
export const typeIsFormulaType = (type: string): type is FormulaType =>
FormulaTypeValues.includes(type as FormulaType)
16 changes: 9 additions & 7 deletions src/scripts/preCompute.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { Command } from 'commander'

import { loadConfig } from '@/config'
import { Computation, Contract, State, loadDb } from '@/db'
import { computeRange, getTypedFormula } from '@/formulas'
import { WasmCodeService } from '@/services/wasm-codes'
import { Block } from '@/types'
import {
bigIntMin,
Computation,
Contract,
State,
getBlockForHeight,
getBlockForTime,
getFirstBlock,
validateBlockString,
} from '@/utils'
loadDb,
} from '@/db'
import { computeRange, getTypedFormula } from '@/formulas'
import { WasmCodeService } from '@/services/wasm-codes'
import { Block } from '@/types'
import { bigIntMin, validateBlockString } from '@/utils'

export const main = async () => {
// Parse arguments.
Expand Down
35 changes: 20 additions & 15 deletions src/server/routes/indexer/computer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import {
Contract,
State,
Validator,
getBlockForTime,
getFirstBlock,
} from '@/db'
import {
FormulaTypeValues,
compute,
computeRange,
getTypedFormula,
typeIsFormulaType,
} from '@/formulas'
import { WasmCodeService } from '@/services/wasm-codes'
import { Block, FormulaType } from '@/types'
import { getBlockForTime, getFirstBlock, validateBlockString } from '@/utils'
import { Block, FormulaType, FormulaTypeValues } from '@/types'
import { validateBlockString } from '@/utils'

import { captureSentryException } from '../../sentry'

Expand Down Expand Up @@ -259,20 +260,26 @@ export const computer: Router.Middleware = async (ctx) => {
let typedFormula
try {
typedFormula = getTypedFormula(type, formulaName)
} catch {
ctx.status = 404
ctx.body = 'formula not found'
return
}

const state = await State.getSingleton()
if (!state) {
ctx.status = 500
ctx.body = 'state not found'
} catch (err) {
if (err instanceof Error && err.message.includes('Formula not found')) {
ctx.status = 404
ctx.body = 'formula not found'
} else {
console.error(err)
ctx.status = 500
ctx.body = 'internal server error'
}
return
}

try {
const state = await State.getSingleton()
if (!state) {
ctx.status = 500
ctx.body = 'state not found'
return
}

// If type is "contract"...
if (typedFormula.type === FormulaType.Contract) {
const contract = await Contract.findByPk(address)
Expand Down Expand Up @@ -723,8 +730,6 @@ export const computer: Router.Middleware = async (ctx) => {

captureSentryException(ctx, err, {
tags: {
blockHeight: state?.latestBlockHeight,
blockTimeUnixMs: state?.latestBlockTimeUnixMs,
key,
type,
address,
Expand Down
2 changes: 1 addition & 1 deletion src/server/test/indexer/computer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { ContractFormula } from '@/types'
export type ComputerTestOptions = {
apiKey: string
credit: AccountKeyCredit
mockFormula: (formula?: Partial<ContractFormula>) => jest.Mock
mockFormula: (formula?: Partial<ContractFormula>) => jest.SpyInstance
unmockFormula: () => void
}
3 changes: 1 addition & 2 deletions src/server/test/indexer/computer/validations.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import request from 'supertest'

import { Contract, Validator } from '@/db'
import { FormulaTypeValues } from '@/formulas'
import { FormulaType } from '@/types'
import { FormulaType, FormulaTypeValues } from '@/types'

import { app } from '../app'
import { ComputerTestOptions } from './types'
Expand Down
17 changes: 3 additions & 14 deletions src/test/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
export const getTypedFormula = jest.fn()
import * as utils from '@/formulas/utils'

const mocks = {
'@/formulas': {
getTypedFormula,
},
}
export const getTypedFormula = jest.spyOn(utils, 'getTypedFormula')

// Creates mocks with default implementations.
export const restoreOriginalMocks = () => {
Object.entries(mocks).forEach(([module, mocks]) => {
jest.mock(module, () => mocks)

// Mock with default implementation unless overridden.
Object.entries(mocks).forEach(([name, mock]) => {
mock.mockImplementation(jest.requireActual(module)[name])
})
})
getTypedFormula.mockReset()
}

restoreOriginalMocks()
2 changes: 1 addition & 1 deletion src/test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { DbType } from '@/types'
loadConfig()

// Don't log errors during tests.
jest.spyOn(console, 'error').mockImplementation()
// jest.spyOn(console, 'error').mockImplementation()

// Wipe databases before each test.
beforeEach(async () => {
Expand Down
2 changes: 2 additions & 0 deletions src/types/formulas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ export enum FormulaType {
Wallet = 'wallet',
}

export const FormulaTypeValues = Object.values(FormulaType)

export type TypedFormula = { name: string } & (
| {
type: FormulaType.Contract
Expand Down
90 changes: 1 addition & 89 deletions src/utils/block.ts
Original file line number Diff line number Diff line change
@@ -1,92 +1,4 @@
import { ModelStatic, Op } from 'sequelize'

import { getDependableEventModels } from '@/db'
import { Block, DependableEventModel, SerializedBlock } from '@/types'

/**
* Get the latest block before or equal to the requested block height.
*/
export const getBlockForHeight = async (
blockHeight: bigint,
// Optionally set a minimum.
after = 0n
): Promise<Block | undefined> => {
const events = await Promise.all(
getDependableEventModels().map((DependableEventModel) =>
(
DependableEventModel as unknown as ModelStatic<DependableEventModel>
).findOne({
where: {
[DependableEventModel.blockHeightKey]: {
[Op.gt]: after,
[Op.lte]: blockHeight,
},
},
order: [[DependableEventModel.blockHeightKey, 'DESC']],
})
)
)

// Choose latest block.
return events
.flatMap((event) => event?.block || [])
.sort((a, b) => Number(b.height - a.height))[0]
}

/**
* Get the latest block before or equal to the requested block time.
*/
export const getBlockForTime = async (
blockTimeUnixMs: bigint,
// Optionally set a minimum.
after = 0n
): Promise<Block | undefined> => {
const events = await Promise.all(
getDependableEventModels().map((DependableEventModel) =>
(
DependableEventModel as unknown as ModelStatic<DependableEventModel>
).findOne({
where: {
[DependableEventModel.blockTimeUnixMsKey]: {
[Op.gt]: after,
[Op.lte]: blockTimeUnixMs,
},
},
order: [[DependableEventModel.blockTimeUnixMsKey, 'DESC']],
})
)
)

// Choose latest block.
return events
.flatMap((event) => event?.block || [])
.sort((a, b) => Number(b.height - a.height))[0]
}

/**
* Get the first block.
*/
export const getFirstBlock = async (): Promise<Block | undefined> => {
const events = await Promise.all(
getDependableEventModels().map((DependableEventModel) =>
(
DependableEventModel as unknown as ModelStatic<DependableEventModel>
).findOne({
where: {
[DependableEventModel.blockHeightKey]: {
[Op.gt]: 0,
},
},
order: [[DependableEventModel.blockHeightKey, 'ASC']],
})
)
)

// Choose first block.
return events
.flatMap((event) => event?.block || [])
.sort((a, b) => Number(a.height - b.height))[0]
}
import { Block, SerializedBlock } from '@/types'

export const validateBlockString = (block: string, subject: string): Block => {
let parsedBlock
Expand Down

0 comments on commit 7b55ede

Please sign in to comment.