Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow cost models in single network mode #899

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/indexer-agent/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const SUGGESTED_SUBGRAPH_MAX_BLOCK_DISTANCE_ON_L2 =
50 + DEFAULT_SUBGRAPH_MAX_BLOCK_DISTANCE
const DEFAULT_SUBGRAPH_FRESHNESS_SLEEP_MILLISECONDS = 5_000

// NOTE: This is run only in single-network mode
export const start = {
command: 'start',
describe: 'Start the agent',
Expand Down Expand Up @@ -237,7 +238,7 @@ export const start = {
description:
'Inject the GRT to DAI/USDC conversion rate into cost model variables',
type: 'boolean',
default: true,
default: false,
group: 'Cost Models',
})
.option('address-book', {
Expand Down Expand Up @@ -439,6 +440,12 @@ export async function createNetworkSpecification(
}
}

if (chainId !== 1 && dai.inject) {
throw new Error(
`The DAI injection feature for cost models is only supported on Ethereum Mainnet`,
)
}

const tapAddressBook = loadFile(argv.tapAddressBook)

try {
Expand Down
4 changes: 2 additions & 2 deletions packages/indexer-cli/src/__tests__/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { cliTest, setup, teardown } from './util'
import { cliTest, setupMultiNetworks, teardown } from './util'
import path from 'path'

const baseDir = path.join(__dirname)

describe('Indexer cli tests', () => {
beforeEach(setup)
beforeEach(setupMultiNetworks)
afterEach(teardown)

describe('General', () => {
Expand Down
10 changes: 8 additions & 2 deletions packages/indexer-cli/src/__tests__/indexer/actions.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { cliTest, deleteFromAllTables, seedActions, setup, teardown } from '../util'
import {
cliTest,
deleteFromAllTables,
seedActions,
setupMultiNetworks,
teardown,
} from '../util'
import path from 'path'

const baseDir = path.join(__dirname, '..')
describe('Indexer actions tests', () => {
describe('With indexer management server', () => {
beforeAll(setup)
beforeAll(setupMultiNetworks)
afterAll(teardown)
beforeEach(seedActions)
afterEach(deleteFromAllTables)
Expand Down
51 changes: 48 additions & 3 deletions packages/indexer-cli/src/__tests__/indexer/cost.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import {
cliTest,
setup,
teardown,
connect,
deleteFromAllTables,
seedCostModels,
setupSingleNetwork,
setupMultiNetworks,
} from '../util'
import path from 'path'

const baseDir = path.join(__dirname, '..')

describe('Indexer cost tests', () => {
describe('Indexer cost tests singleNetwork', () => {
describe('With indexer management server', () => {
beforeAll(setup)
beforeAll(setupSingleNetwork)
afterAll(teardown)
beforeEach(seedCostModels)
afterEach(deleteFromAllTables)
Expand Down Expand Up @@ -231,3 +232,47 @@ describe('Indexer cost tests', () => {
)
})
})

describe('Indexer cost tests multiNetworks', () => {
beforeAll(setupMultiNetworks)
afterAll(teardown)
beforeEach(seedCostModels)
afterEach(deleteFromAllTables)

describe('Cost set...', () => {
cliTest(
'Indexer cost set model deployment id - reject multinetwork mode',
[
'indexer',
'cost',
'set',
'model',
'QmXRpJW3qBuYaiBYHdhv8DF4bHDZhXBmh91MtrnhJfQ5Lk',
'references/basic.agora',
],
'references/indexer-cost-model-deployment-multinetworks',
{
expectedExitCode: 1,
cwd: baseDir,
timeout: 10000,
},
)
cliTest(
'Indexer cost set variable deployment id - reject multinetwork mode',
[
'indexer',
'cost',
'set',
'variables',
'QmQ44hgrWWt3Qf2X9XEX2fPyTbmQbChxwNm5c1t4mhKpGt',
`'{"DAI": "0.5"}'`,
],
'references/indexer-cost-variables-deployment-multinetworks',
{
expectedExitCode: 1,
cwd: baseDir,
timeout: 10000,
},
)
})
})
4 changes: 2 additions & 2 deletions packages/indexer-cli/src/__tests__/indexer/rules.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import {
cliTest,
connect,
setup,
teardown,
deleteFromAllTables,
seedIndexingRules,
setupMultiNetworks,
} from '../util'
import path from 'path'

const baseDir = path.join(__dirname, '..')

describe('Indexer rules tests', () => {
describe('With indexer management server', () => {
beforeAll(setup)
beforeAll(setupMultiNetworks)
afterAll(teardown)
beforeEach(seedIndexingRules)
afterEach(deleteFromAllTables)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[GraphQL] Must be in single network mode to set cost models
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[GraphQL] Must be in single network mode to set cost models
20 changes: 15 additions & 5 deletions packages/indexer-cli/src/__tests__/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,15 @@ export const testNetworkSpecification = specification.NetworkSpecification.parse
},
})

export const setup = async () => {
export const setupMultiNetworks = async () => {
return await setup(true)
}

export const setupSingleNetwork = async () => {
return await setup(false)
}

export const setup = async (multiNetworksEnabled: boolean) => {
logger = createLogger({
name: 'Setup',
async: false,
Expand Down Expand Up @@ -141,10 +149,12 @@ export const setup = async () => {
const fakeMainnetNetwork = cloneDeep(network) as Network
fakeMainnetNetwork.specification.networkIdentifier = 'eip155:1'

const multiNetworks = new MultiNetworks(
[network, fakeMainnetNetwork],
(n: Network) => n.specification.networkIdentifier,
)
const multiNetworks = multiNetworksEnabled
? new MultiNetworks(
[network, fakeMainnetNetwork],
(n: Network) => n.specification.networkIdentifier,
)
: new MultiNetworks([network], (n: Network) => n.specification.networkIdentifier)

const defaults: IndexerManagementDefaults = {
globalIndexingRule: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,17 @@ export default {
{ models, multiNetworks, dai }: IndexerManagementResolverContext,
): Promise<object> => {
if (!multiNetworks) {
throw Error('IndexerManagementClient must be in `network` mode to set cost models')
throw new Error('No network configuration available')
}
if (Object.keys(multiNetworks.inner).length !== 1) {
throw Error('Must be in single network mode to set cost models')
}
const network = Object.values(multiNetworks.inner)[0]
const injectDai = network.specification.dai.inject
if (network.specification.networkIdentifier !== 'eip155:1' && injectDai) {
throw new Error(
`Can't set cost model: DAI injection enabled but not on Ethereum Mainnet`,
)
}

const update = parseGraphQLCostModel(costModel)
Expand All @@ -111,13 +121,6 @@ export default {
} catch (err) {
throw new Error(`Invalid cost model or variables: ${err.message}`)
}
const network = multiNetworks.inner['eip155:1']
if (!network) {
throw new Error(
`Can't set cost model: Indexer Agent does not have Ethereum Mainnet network configured.`,
)
}
const injectDai = !!network.specification.dai.inject
const [model] = await models.CostModel.findOrBuild({
where: { deployment: update.deployment },
})
Expand Down
Loading