Skip to content

Commit

Permalink
Fix/trusted algorithms (#68)
Browse files Browse the repository at this point in the history
* refactor: move checkDidFiles to provider utils

* fix: correctly continue resolving

* test: add aquarius and provider mocks

* test: add trusted algorithms unit test

* chore: remove console log

* chore: remove only from unit test
  • Loading branch information
moritzkirstein authored Mar 5, 2024
1 parent 94c0fbc commit ee97319
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 25 deletions.
25 changes: 4 additions & 21 deletions src/utils/helpers/trusted-algorithms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@oceanprotocol/lib'
import { getAsset } from '../aquarius'
import { FileTypes, NautilusService, ServiceTypes } from '../../Nautilus'
import { checkDidFiles } from '../provider'

// TODO replace hardcoded service index 0 with service id once supported by the stack
async function getPublisherTrustedAlgorithms(
Expand All @@ -30,7 +31,7 @@ async function getPublisherTrustedAlgorithms(
throw new Error(`Asset ${asset.id} is not of type algorithm`)
if (!asset.services?.[0]) throw new Error(`No service in ${asset.id}`)

const filesChecksum = await getFileDidInfo(
const filesChecksum = await checkDidFiles(
asset?.id,
asset?.services?.[0]?.id,
asset?.services?.[0]?.serviceEndpoint
Expand All @@ -53,30 +54,12 @@ async function getPublisherTrustedAlgorithms(
return trustedAlgorithms
}

async function getFileDidInfo(
did: string,
serviceId: string,
providerUrl: string
): Promise<FileInfo[]> {
try {
const response = await ProviderInstance.checkDidFiles(
did,
serviceId,
providerUrl,
true
)
return response
} catch (error) {
throw new Error(`[Initialize check file did] Error:' ${error}`)
}
}

export async function resolvePublisherTrustedAlgorithms(
nautilusDDOServices: NautilusService<ServiceTypes, FileTypes>[],
metadataCacheUri: string
) {
for (const service of nautilusDDOServices) {
if (service.addedPublisherTrustedAlgorithms?.length) continue
if (service.addedPublisherTrustedAlgorithms?.length < 1) continue

const dids = service.addedPublisherTrustedAlgorithms.map(
(asset) => asset.did
Expand All @@ -88,7 +71,7 @@ export async function resolvePublisherTrustedAlgorithms(

if (service.compute?.publisherTrustedAlgorithms?.length === 0) {
service.compute.publisherTrustedAlgorithms = newPublisherTrustedAlgorithms
return
continue
}

newPublisherTrustedAlgorithms.forEach((algorithm) => {
Expand Down
18 changes: 18 additions & 0 deletions src/utils/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ export async function getEncryptedFiles(
}
}

export async function checkDidFiles(
did: string,
serviceId: string,
providerUrl: string
): Promise<FileInfo[]> {
try {
const response = await ProviderInstance.checkDidFiles(
did,
serviceId,
providerUrl,
true
)
return response
} catch (error) {
throw new Error(`[Initialize check file did] Error:' ${error}`)
}
}

export async function initializeProvider(
asset: AssetWithAccessDetails,
accountId: string,
Expand Down
17 changes: 17 additions & 0 deletions test/mocks/aquarius.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as aquarius from '../../src/utils/aquarius'
import * as AquariusAsset from '../fixtures/AquariusAsset.json'
import sinon from 'sinon'

const RETURNED_ASSET = AquariusAsset
const RETURNED_ASSET_ARRAY = [AquariusAsset, AquariusAsset]

export function mockAquarius(): sinon.SinonMock {
const aquariusMock = sinon.mock(aquarius)

aquariusMock.expects('getAsset').returns(Promise.resolve(RETURNED_ASSET))
aquariusMock
.expects('getAssets')
.returns(Promise.resolve(RETURNED_ASSET_ARRAY))

return aquariusMock
}
8 changes: 8 additions & 0 deletions test/mocks/provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import sinon from 'sinon'
import * as provider from '../../src/utils/provider'

export function mockProvider(): sinon.SinonMock {
const providerMock = sinon.mock(provider)

return providerMock
}
8 changes: 4 additions & 4 deletions test/unit/NautilusService.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { expect } from 'chai'
import sinon from 'sinon'
import { FileTypes, NautilusService, ServiceTypes, UrlFile } from '../../src'
import * as provider from '../../src/utils/provider'
import { datasetService } from '../fixtures/AssetConfig'
import { getConsumerParameters } from '../fixtures/ConsumerParameters'
import { expectThrowsAsync } from '../utils.test'
import { mockProvider } from '../mocks/provider'

describe('NautilusService', () => {
let providerMock: sinon.SinonMock
Expand All @@ -14,11 +14,11 @@ describe('NautilusService', () => {
const nftAddress = '0x1234NFT'
const datatokenAddress = '0x1234DATATOKEN'

beforeEach(() => {
providerMock = sinon.mock(provider)
before(() => {
providerMock = mockProvider()
})

afterEach(() => {
after(() => {
providerMock.restore()
})

Expand Down
72 changes: 72 additions & 0 deletions test/unit/Utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Asset, LogLevel, LoggerInstance, getHash } from '@oceanprotocol/lib'
import { ServiceBuilder } from '../../src'
import { resolvePublisherTrustedAlgorithms } from '../../src/utils/helpers/trusted-algorithms'
import * as AquariusAsset from '../fixtures/AquariusAsset.json'
import { expect } from 'chai'
import sinon from 'sinon'
import { mockAquarius } from '../mocks/aquarius'
import { mockProvider } from '../mocks/provider'

describe('Utils', () => {
let aquariusMock: sinon.SinonMock
let providerMock: sinon.SinonMock

before(() => {
LoggerInstance.setLevel(LogLevel.Verbose)
aquariusMock = mockAquarius()
providerMock = mockProvider()
})

after(() => {
aquariusMock.restore()
providerMock.restore()
})

describe('Trusted Algorithms', async () => {
const containerChecksum = getHash(
AquariusAsset.metadata.algorithm.container.entrypoint +
AquariusAsset.metadata.algorithm.container.checksum
)
const trustedAlgorithms = {
did: AquariusAsset.id,
filesChecksum: 'filesChecksum',
containerSectionChecksum: containerChecksum
}

beforeEach(() => {
providerMock
.expects('checkDidFiles')
.returns(
Promise.resolve([
{ type: '', valid: true, checksum: trustedAlgorithms.filesChecksum }
])
)
})

it('should resolve publisherTrustedAlgorithms correctly', async () => {
const serviceBuilder = new ServiceBuilder({
aquariusAsset: AquariusAsset as Asset,
serviceId: AquariusAsset.services[0].id
})

const service = serviceBuilder
.addTrustedAlgorithms([
{
did: AquariusAsset.id
}
])
.build()

await resolvePublisherTrustedAlgorithms(
[service],
'https://dummy.metadatacache'
)

expect(service)
.to.have.property('compute')
.to.have.property('publisherTrustedAlgorithms')
.to.have.lengthOf.above(0)
.to.deep.include(trustedAlgorithms)
})
})
})

0 comments on commit ee97319

Please sign in to comment.