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

use the testnet api url for asset verification when on testnet network #5384

Merged
merged 1 commit into from
Sep 24, 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
28 changes: 27 additions & 1 deletion ironfish/src/assets/assetsVerificationApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import nock from 'nock'
import { NodeFileProvider } from '../fileSystems'
import { AssetsVerificationApi } from './assetsVerificationApi'
import {
AssetsVerificationApi,
getDefaultAssetVerificationEndpoint,
} from './assetsVerificationApi'

const assetData1 = {
identifier: '0123',
Expand Down Expand Up @@ -239,3 +242,26 @@ describe('Assets Verification API Client', () => {
})
})
})

describe('getDefaultAssetVerificationEndpoint', () => {
it('returns the testnet url with the testnet id', () => {
expect(getDefaultAssetVerificationEndpoint(0)).toEqual(
'https://testnet.api.ironfish.network/assets/verified_metadata',
)
})

it('returns the regular url with any other id', () => {
expect(getDefaultAssetVerificationEndpoint(1)).toEqual(
'https://api.ironfish.network/assets/verified_metadata',
)
expect(getDefaultAssetVerificationEndpoint(10)).toEqual(
'https://api.ironfish.network/assets/verified_metadata',
)
})

it('returns the regular url with no id', () => {
expect(getDefaultAssetVerificationEndpoint()).toEqual(
'https://api.ironfish.network/assets/verified_metadata',
)
})
})
12 changes: 10 additions & 2 deletions ironfish/src/assets/assetsVerificationApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ export class AssetsVerificationApi {

readonly url: string

constructor(options: { files: FileSystem; url?: string; timeout?: number }) {
this.url = options?.url || 'https://api.ironfish.network/assets/verified_metadata'
constructor(options: { files: FileSystem; url: string; timeout?: number }) {
this.url = options.url
this.timeout = options?.timeout ?? 30 * 1000 // 30 seconds
this.adapter = isFileUrl(this.url)
? axiosFileAdapter(options.files)
Expand Down Expand Up @@ -130,6 +130,14 @@ export class AssetsVerificationApi {
}
}

export function getDefaultAssetVerificationEndpoint(networkId?: number): string {
if (networkId === 0) {
return 'https://testnet.api.ironfish.network/assets/verified_metadata'
}

return 'https://api.ironfish.network/assets/verified_metadata'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it might be more correct to only return this for ID 1, I wanted to get this working for testnet primarily. Changing this would require some more thinking and refactoring on what to do otherwise

}

const isFileUrl = (url: string): boolean => {
const parsedUrl = new URL(url)
return parsedUrl.protocol === 'file:'
Expand Down
6 changes: 4 additions & 2 deletions ironfish/src/assets/assetsVerifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { VerifiedAssetsCacheStore } from '../fileStores/verifiedAssets'
import { NodeFileProvider } from '../fileSystems'
import { AssetsVerifier } from './assetsVerifier'

const apiUrl = 'https://example.com/endpoint'

/* eslint-disable jest/no-standalone-expect */
/* eslint-disable @typescript-eslint/no-explicit-any */
const assetData1 = {
Expand Down Expand Up @@ -58,7 +60,7 @@ describe('AssetsVerifier', () => {
})

it('does not refresh when not started', () => {
const assetsVerifier = new AssetsVerifier({ files })
const assetsVerifier = new AssetsVerifier({ files, apiUrl })
const refresh = jest.spyOn(assetsVerifier as any, 'refresh')

jest.runOnlyPendingTimers()
Expand Down Expand Up @@ -191,7 +193,7 @@ describe('AssetsVerifier', () => {

describe('verify', () => {
it("returns 'unknown' when not started", () => {
const assetsVerifier = new AssetsVerifier({ files })
const assetsVerifier = new AssetsVerifier({ files, apiUrl })

expect(assetsVerifier.verify('0123')).toStrictEqual({ status: 'unknown' })
expect(assetsVerifier.verify('4567')).toStrictEqual({ status: 'unknown' })
Expand Down
8 changes: 4 additions & 4 deletions ironfish/src/assets/assetsVerifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ export class AssetsVerifier {

constructor(options: {
files: FileSystem
apiUrl?: string
apiUrl: string
cache?: VerifiedAssetsCacheStore
logger?: Logger
}) {
this.logger = options?.logger ?? createRootLogger()
this.api = new AssetsVerificationApi({ url: options?.apiUrl, files: options.files })
this.cache = options?.cache
this.logger = options.logger ?? createRootLogger()
this.api = new AssetsVerificationApi({ url: options.apiUrl, files: options.files })
this.cache = options.cache
this.started = false

if (this.cache?.config?.apiUrl === this.api.url) {
Expand Down
23 changes: 12 additions & 11 deletions ironfish/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { BoxKeyPair, FishHashContext } from '@ironfish/rust-nodejs'
import { v4 as uuid } from 'uuid'
import { AssetsVerifier } from './assets'
import { AssetsVerifier, getDefaultAssetVerificationEndpoint } from './assets'
import { Blockchain } from './blockchain'
import { BlockHasher } from './blockHasher'
import {
Expand Down Expand Up @@ -223,16 +223,6 @@ export class FullNode {
const peerStore = new PeerStore(files, dataDir)
await peerStore.load()

const verifiedAssetsCache = new VerifiedAssetsCacheStore(files, dataDir)
await verifiedAssetsCache.load()

const assetsVerifier = new AssetsVerifier({
files,
apiUrl: config.get('assetVerificationApi'),
cache: verifiedAssetsCache,
logger,
})

const numWorkers = calculateWorkers(config.get('nodeWorkers'), config.get('nodeWorkersMax'))

const workerPool = new WorkerPool({ logger, metrics, numWorkers })
Expand All @@ -249,6 +239,17 @@ export class FullNode {

const network = new Network(networkDefinition)

const verifiedAssetsCache = new VerifiedAssetsCacheStore(files, dataDir)
await verifiedAssetsCache.load()

const assetsVerifier = new AssetsVerifier({
files,
apiUrl:
config.get('assetVerificationApi') || getDefaultAssetVerificationEndpoint(network.id),
cache: verifiedAssetsCache,
logger,
})

if (!config.isSet('bootstrapNodes')) {
config.setOverride('bootstrapNodes', network.bootstrapNodes)
}
Expand Down