Skip to content

Commit

Permalink
Disable using computations for range queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Dec 13, 2023
1 parent a2a037d commit 77d7a85
Showing 1 changed file with 98 additions and 98 deletions.
196 changes: 98 additions & 98 deletions src/server/routes/indexer/computer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Router from '@koa/router'
import { Op } from 'sequelize'

import {
Block,
Expand Down Expand Up @@ -324,11 +323,11 @@ export const computer: Router.Middleware = async (ctx) => {

let computation

const computationWhere = {
targetAddress: address,
formula: formulaName,
args: JSON.stringify(args),
}
// const computationWhere = {
// targetAddress: address,
// formula: formulaName,
// args: JSON.stringify(args),
// }

// If time passed, compute block that correlates with that time.
if (time) {
Expand Down Expand Up @@ -399,101 +398,102 @@ export const computer: Router.Middleware = async (ctx) => {
// between. If not, compute range.
let existingUsed = false

const existingStartComputation = await Computation.findOne({
where: {
...computationWhere,
blockHeight: {
[Op.lte]: blocks[0].height,
},
},
order: [['blockHeight', 'DESC']],
})
// If start computation exists, check the rest.
if (existingStartComputation) {
const existingRestComputations = await Computation.findAll({
where: {
...computationWhere,
blockHeight: {
[Op.gt]: blocks[0].height,
[Op.lte]: blocks[1].height,
},
},
order: [['blockHeight', 'ASC']],
})
// TODO(computations): Re-enable computations when they are invalidated in the background.
// const existingStartComputation = await Computation.findOne({
// where: {
// ...computationWhere,
// blockHeight: {
// [Op.lte]: blocks[0].height,
// },
// },
// order: [['blockHeight', 'DESC']],
// })
// // If start computation exists, check the rest.
// if (existingStartComputation) {
// const existingRestComputations = await Computation.findAll({
// where: {
// ...computationWhere,
// blockHeight: {
// [Op.gt]: blocks[0].height,
// [Op.lte]: blocks[1].height,
// },
// },
// order: [['blockHeight', 'ASC']],
// })

// // Ensure entire range is covered by checking if validations are
// // chained. In other words, check that each computation is valid up
// // until the block just before the next computation starts.
// let existingComputations = [
// existingStartComputation,
// ...existingRestComputations,
// ]
// const isRangeCoveredBeforeEnd = existingComputations.every(
// (computation, i) =>
// i === existingComputations.length - 1 ||
// BigInt(computation.latestBlockHeightValid) ===
// BigInt(existingComputations[i + 1].blockHeight) - 1n
// )

// Ensure entire range is covered by checking if validations are
// chained. In other words, check that each computation is valid up
// until the block just before the next computation starts.
let existingComputations = [
existingStartComputation,
...existingRestComputations,
]
const isRangeCoveredBeforeEnd = existingComputations.every(
(computation, i) =>
i === existingComputations.length - 1 ||
BigInt(computation.latestBlockHeightValid) ===
BigInt(existingComputations[i + 1].blockHeight) - 1n
)
// // If range is covered, ensure that the end computation is valid at the
// // end block.
// let entireRangeValid =
// isRangeCoveredBeforeEnd &&
// (await existingComputations[
// existingComputations.length - 1
// ].updateValidityUpToBlockHeight(blocks[1].height))

// // If range is covered until the end, we are dealing with an incomplete
// // but continuous range. Load just the rest.
// if (isRangeCoveredBeforeEnd && !entireRangeValid) {
// const missingComputations = await computeRange({
// ...typedFormula,
// chainId: state.chainId,
// targetAddress: address,
// args,
// // Start at the block of the last existing computation, since we
// // need the block time to perform computations but cannot retrieve
// // that information with just `latestBlockHeightValid`.
// blockStart:
// existingComputations[existingComputations.length - 1].block,
// blockEnd: blocks[1],
// })

// If range is covered, ensure that the end computation is valid at the
// end block.
let entireRangeValid =
isRangeCoveredBeforeEnd &&
(await existingComputations[
existingComputations.length - 1
].updateValidityUpToBlockHeight(blocks[1].height))

// If range is covered until the end, we are dealing with an incomplete
// but continuous range. Load just the rest.
if (isRangeCoveredBeforeEnd && !entireRangeValid) {
const missingComputations = await computeRange({
...typedFormula,
chainId: state.chainId,
targetAddress: address,
args,
// Start at the block of the last existing computation, since we
// need the block time to perform computations but cannot retrieve
// that information with just `latestBlockHeightValid`.
blockStart:
existingComputations[existingComputations.length - 1].block,
blockEnd: blocks[1],
})

// Ignore first computation since it's equivalent to the last existing
// computation.
missingComputations.shift()

// Cache computations for future queries.
const createdMissingComputations =
await Computation.createFromComputationOutputs(
address,
typedFormula,
args,
missingComputations
)

// Avoid using push(...items) since there is a limit to the number of
// arguments that can be put on the stack, and the number of
// computations may be very large.
existingComputations = [
...existingComputations,
...createdMissingComputations,
]

entireRangeValid = await existingComputations[
existingComputations.length - 1
].updateValidityUpToBlockHeight(blocks[1].height)
}
// // Ignore first computation since it's equivalent to the last existing
// // computation.
// missingComputations.shift()

// // Cache computations for future queries.
// const createdMissingComputations =
// await Computation.createFromComputationOutputs(
// address,
// typedFormula,
// args,
// missingComputations
// )

// // Avoid using push(...items) since there is a limit to the number of
// // arguments that can be put on the stack, and the number of
// // computations may be very large.
// existingComputations = [
// ...existingComputations,
// ...createdMissingComputations,
// ]

if (entireRangeValid) {
outputs = existingComputations.map(({ block, output }) => ({
value: output && JSON.parse(output),
blockHeight: block.height ?? -1n,
blockTimeUnixMs: block.timeUnixMs ?? -1n,
}))
existingUsed = true
}
}
// entireRangeValid = await existingComputations[
// existingComputations.length - 1
// ].updateValidityUpToBlockHeight(blocks[1].height)
// }

// if (entireRangeValid) {
// outputs = existingComputations.map(({ block, output }) => ({
// value: output && JSON.parse(output),
// blockHeight: block.height ?? -1n,
// blockTimeUnixMs: block.timeUnixMs ?? -1n,
// }))
// existingUsed = true
// }
// }

// If could not find existing range, compute.
if (!existingUsed) {
Expand Down

0 comments on commit 77d7a85

Please sign in to comment.