Skip to content

Commit

Permalink
Take into account bank events when converting time parameters to blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Sep 26, 2023
1 parent b40b243 commit 7042434
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 47 deletions.
63 changes: 63 additions & 0 deletions src/core/utils/block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Op } from 'sequelize'

import { BankStateEvent, WasmStateEvent } from '@/db'

import { Block } from '../types'

export const getBlockForTime = async (
blockTimeUnixMs: bigint
): Promise<Block | undefined> => {
const [wasmEvent, bankEvent] = await Promise.all([
await WasmStateEvent.findOne({
where: {
blockTimeUnixMs: {
[Op.gt]: 0,
[Op.lte]: blockTimeUnixMs,
},
},
order: [['blockTimeUnixMs', 'DESC']],
}),
await BankStateEvent.findOne({
where: {
blockTimeUnixMs: {
[Op.gt]: 0,
[Op.lte]: blockTimeUnixMs,
},
},
order: [['blockTimeUnixMs', 'DESC']],
}),
])

// Choose latest block.
return [
...(wasmEvent ? [wasmEvent.block] : []),
...(bankEvent ? [bankEvent.block] : []),
].sort((a, b) => Number(b.height - a.height))[0]
}

export const getFirstBlock = async (): Promise<Block | undefined> => {
const [wasmEvent, bankEvent] = await Promise.all([
await WasmStateEvent.findOne({
where: {
blockTimeUnixMs: {
[Op.gt]: 0,
},
},
order: [['blockTimeUnixMs', 'ASC']],
}),
await BankStateEvent.findOne({
where: {
blockTimeUnixMs: {
[Op.gt]: 0,
},
},
order: [['blockTimeUnixMs', 'ASC']],
}),
])

// Choose latest block.
return [
...(wasmEvent ? [wasmEvent.block] : []),
...(bankEvent ? [bankEvent.block] : []),
].sort((a, b) => Number(b.height - a.height))[0]
}
1 change: 1 addition & 0 deletions src/core/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DependentKeyNamespace } from '@/db'

import { Block, FormulaType, SerializedBlock } from '../types'

export * from './block'
export * from './chain'
export * from './objectMatchesStructure'

Expand Down
56 changes: 9 additions & 47 deletions src/server/routes/indexer/computer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
FormulaTypeValues,
compute,
computeRange,
getBlockForTime,
getFirstBlock,
loadConfig,
typeIsFormulaType,
validateBlockString,
Expand All @@ -19,7 +21,6 @@ import {
Contract,
State,
Validator,
WasmStateEvent,
} from '@/db'

import { captureSentryException } from '../../sentry'
Expand Down Expand Up @@ -208,9 +209,9 @@ export const computer: Router.Middleware = async (ctx) => {

// TODO: Calculate start and end block with times some other way. Or don't use
// start and end blocks at all and use block or time depending on which is
// passed? Right now, the block is retrieved by checking `WasmEvent`s, but
// there are many times of events now and any formula can use any event type.
// This needs a better solution.
// passed? Right now, the block is retrieved by checking `WasmStateEvent`s,
// but there are many times of events now and any formula can use any event
// type. This needs a better solution.

// If times passed, validate that it's a range with either a start or a
// start/end pair.
Expand Down Expand Up @@ -336,17 +337,7 @@ export const computer: Router.Middleware = async (ctx) => {
time += BigInt(state.latestBlockTimeUnixMs)
}

block = (
await WasmStateEvent.findOne({
where: {
blockTimeUnixMs: {
[Op.gt]: 0,
[Op.lte]: time,
},
},
order: [['blockTimeUnixMs', 'DESC']],
})
)?.block
block = await getBlockForTime(time)
}

// If times passed, compute blocks that correlate with those times.
Expand All @@ -360,41 +351,12 @@ export const computer: Router.Middleware = async (ctx) => {
}

const startBlock =
(
await WasmStateEvent.findOne({
where: {
blockTimeUnixMs: {
[Op.gt]: 0,
[Op.lte]: times[0],
},
},
order: [['blockTimeUnixMs', 'DESC']],
})
)?.block ??
(await getBlockForTime(times[0])) ??
// Use first block if no event exists before start time.
(
await WasmStateEvent.findOne({
where: {
blockTimeUnixMs: {
[Op.gt]: 0,
},
},
order: [['blockTimeUnixMs', 'ASC']],
})
)?.block
(await getFirstBlock())
// Use latest block if no end time exists.
const endBlock = times[1]
? (
await WasmStateEvent.findOne({
where: {
blockTimeUnixMs: {
[Op.gt]: 0,
[Op.lte]: times[1],
},
},
order: [['blockTimeUnixMs', 'DESC']],
})
)?.block
? await getBlockForTime(times[1])
: state.latestBlock

if (startBlock && endBlock) {
Expand Down

0 comments on commit 7042434

Please sign in to comment.