Skip to content

Commit

Permalink
sourceQuery for BW
Browse files Browse the repository at this point in the history
  • Loading branch information
arietrouw committed Oct 10, 2023
1 parent 947e643 commit bf00e08
Show file tree
Hide file tree
Showing 47 changed files with 135 additions and 89 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"test-esm": "node $(yarn jest --no-cache --forceExit)",
"test": "jest --no-cache --forceExit",
"test:no-slow": "jest . --passWithNoTests --forceExit -- --group=-slow",
"test:thumbnail": "jest . --passWithNoTests --forceExit -- --group=thumbnail",
"test:no-mongo": "jest . --passWithNoTests --forceExit -c ./jest.config.no-mongo.ts -- --group=-mongo",
"test:ci": "jest --runInBand --coverage --forceExit --passWithNoTests",
"xyo": "node ./packages/cli/dist/cjs/index.js",
Expand All @@ -125,4 +126,4 @@
"yarn": "1.22.19"
},
"type": "module"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isQueryBoundWitness, QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-builder'
import { isBoundWitness } from '@xyo-network/boundwitness-model'
import { QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-builder'
import { isBoundWitness, isQueryBoundWitness } from '@xyo-network/boundwitness-model'
import { BoundWitnessWrapper } from '@xyo-network/boundwitness-wrapper'
import { Payload } from '@xyo-network/payload-model'
import { PayloadWrapper } from '@xyo-network/payload-wrapper'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import {
asArchivistInstance,
isArchivistInstance,
} from '@xyo-network/archivist-model'
import { QueryBoundWitness, QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-builder'
import { BoundWitness, BoundWitnessSchema } from '@xyo-network/boundwitness-model'
import { QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-builder'
import { BoundWitness, BoundWitnessSchema, QueryBoundWitness } from '@xyo-network/boundwitness-model'
import { PayloadHasher } from '@xyo-network/core'
import { AbstractModuleInstance } from '@xyo-network/module-abstract'
import { duplicateModules, ModuleConfig, ModuleQueryHandlerResult } from '@xyo-network/module-model'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { QueryBoundWitness, QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-builder'
import { QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-builder'
import { QueryBoundWitness } from '@xyo-network/boundwitness-model'
import {
BridgeConfigSchema,
BridgeConnectedQuerySchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { compact } from '@xylabs/lodash'
import { AbstractBridge } from '@xyo-network/abstract-bridge'
import { ApiEnvelope } from '@xyo-network/api-models'
import { AxiosError, AxiosJson } from '@xyo-network/axios'
import { QueryBoundWitness } from '@xyo-network/boundwitness-builder'
import { QueryBoundWitness } from '@xyo-network/boundwitness-model'
import { BridgeModule, CacheConfig } from '@xyo-network/bridge-model'
import { ConfigPayload, ConfigSchema } from '@xyo-network/config-payload-plugin'
import { ManifestPayload, ManifestPayloadSchema } from '@xyo-network/manifest-model'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QueryBoundWitness } from '@xyo-network/boundwitness-builder'
import { QueryBoundWitness } from '@xyo-network/boundwitness-model'
import { ManifestPayload } from '@xyo-network/manifest-model'
import {
AnyConfigSchema,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assertEx } from '@xylabs/assert'
import { compact } from '@xylabs/lodash'
import { AddressPayload, AddressSchema } from '@xyo-network/address-payload-plugin'
import { QueryBoundWitness } from '@xyo-network/boundwitness-builder'
import { QueryBoundWitness } from '@xyo-network/boundwitness-model'
import { BridgeModule } from '@xyo-network/bridge-model'
import { ManifestPayloadSchema, ModuleManifestPayload } from '@xyo-network/manifest-model'
import { BaseEmitter } from '@xyo-network/module-abstract'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assertEx } from '@xylabs/assert'
import { delay } from '@xylabs/delay'
import { compact } from '@xylabs/lodash'
import { AbstractBridge } from '@xyo-network/abstract-bridge'
import { QueryBoundWitness } from '@xyo-network/boundwitness-builder'
import { QueryBoundWitness } from '@xyo-network/boundwitness-model'
import { BridgeModule, CacheConfig } from '@xyo-network/bridge-model'
import { ConfigPayload, ConfigSchema } from '@xyo-network/config-payload-plugin'
import { ManifestPayload, ManifestPayloadSchema } from '@xyo-network/manifest-model'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertEx } from '@xylabs/assert'
import { QueryBoundWitness } from '@xyo-network/boundwitness-builder'
import { QueryBoundWitness } from '@xyo-network/boundwitness-model'
import {
BridgeConnectQuerySchema,
BridgeDisconnectQuerySchema,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { assertEx } from '@xylabs/assert'
import { QueryBoundWitness, QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-builder'
import { QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-builder'
import { QueryBoundWitness } from '@xyo-network/boundwitness-model'
import { PayloadHasher } from '@xyo-network/core'
import {
CustomDivinerModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { HDWallet } from '@xyo-network/account'
import { AccountInstance } from '@xyo-network/account-model'
import { AddressPayload, AddressSchema } from '@xyo-network/address-payload-plugin'
import { ArchivistInstance, asArchivistInstance } from '@xyo-network/archivist-model'
import { BoundWitnessBuilder, QueryBoundWitness, QueryBoundWitnessBuilder, QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-builder'
import { BoundWitness } from '@xyo-network/boundwitness-model'
import { BoundWitnessBuilder, QueryBoundWitnessBuilder, QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-builder'
import { BoundWitness, QueryBoundWitness } from '@xyo-network/boundwitness-model'
import { ConfigPayload, ConfigSchema } from '@xyo-network/config-payload-plugin'
import { PayloadHasher } from '@xyo-network/core'
import { handleError, handleErrorAsync } from '@xyo-network/error'
Expand Down Expand Up @@ -52,6 +52,7 @@ import { IdLogger } from '@xyo-network/shared'
import { WalletInstance } from '@xyo-network/wallet-model'

import { BaseEmitter } from './BaseEmitter'
import { determineAccount } from './determineAccount'
import { ModuleErrorBuilder } from './Error'
import { ModuleConfigQueryValidator, Queryable, SupportedQueryValidator } from './QueryValidator'

Expand Down Expand Up @@ -203,26 +204,12 @@ export abstract class AbstractModule<TParams extends ModuleParams = ModuleParams
return newModule
}

static async determineAccount({
account,
accountDerivationPath,
wallet,
}: {
static async determineAccount(params: {
account?: AccountInstance | 'random'
accountDerivationPath?: string
wallet?: WalletInstance
}): Promise<AccountInstance> {
if (wallet) {
return assertEx(accountDerivationPath ? await wallet.derivePath(accountDerivationPath) : wallet, 'Failed to derive account from path')
} else if (account === 'random') {
return await HDWallet.random()
} else if (account) {
return account
} else {
//this should eventually be removed/thrown
console.warn('AbstractModule.determineAccount: No account provided - Creating Random account')
return await HDWallet.random()
}
return await determineAccount(params)
}

static factory<TModule extends ModuleInstance>(
Expand Down Expand Up @@ -285,6 +272,7 @@ export abstract class AbstractModule<TParams extends ModuleParams = ModuleParams
queryConfig?: TConfig,
): Promise<ModuleQueryResult> {
this._noOverride('query')
const sourceQuery = await PayloadHasher.hashAsync(query)
return await this.busy(async () => {
const resultPayloads: Payload[] = []
const errorPayloads: ModuleError[] = []
Expand Down Expand Up @@ -313,7 +301,7 @@ export abstract class AbstractModule<TParams extends ModuleParams = ModuleParams
const timestamp = { schema: 'network.xyo.timestamp', timestamp: Date.now() }
resultPayloads.push(timestamp)
}
const result = await this.bindQueryResult(query, resultPayloads, queryAccount ? [queryAccount] : [], errorPayloads)
const result = await this.bindQueryResult(query, resultPayloads, sourceQuery, queryAccount ? [queryAccount] : [], errorPayloads)
const args: ModuleQueriedEventArgs = { module: this, payloads, query, result }
await this.emit('moduleQueried', args)
return result
Expand Down Expand Up @@ -462,10 +450,11 @@ export abstract class AbstractModule<TParams extends ModuleParams = ModuleParams
protected async bindQueryResult<T extends Query>(
query: T,
payloads: Payload[],
sourceQuery?: string,
additionalWitnesses: AccountInstance[] = [],
errors?: ModuleError[],
): Promise<ModuleQueryResult> {
const builder = new BoundWitnessBuilder().payloads(payloads).errors(errors)
const builder = new BoundWitnessBuilder().payloads(payloads).errors(errors).sourceQuery(sourceQuery)
const queryWitnessAccount = this.queryAccounts[query.schema as ModuleQueryBase['schema']]
const witnesses = [this.account, queryWitnessAccount, ...additionalWitnesses].filter(exists)
builder.witnesses(witnesses)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QueryBoundWitness } from '@xyo-network/boundwitness-builder'
import { QueryBoundWitness } from '@xyo-network/boundwitness-model'
import { Payload } from '@xyo-network/payload-model'
import { Promisable } from '@xyo-network/promise'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { QueryBoundWitness, QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-builder'
import { QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-builder'
import { QueryBoundWitness } from '@xyo-network/boundwitness-model'
import { Module, ModuleQuery } from '@xyo-network/module-model'
import { Payload } from '@xyo-network/payload-model'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable max-lines */
import { assertEx } from '@xylabs/assert'
import { HDWallet } from '@xyo-network/account'
import { AccountInstance } from '@xyo-network/account-model'
import { WalletInstance } from '@xyo-network/wallet-model'

export const determineAccount = async ({
account,
accountDerivationPath,
wallet,
}: {
account?: AccountInstance | 'random'
accountDerivationPath?: string
wallet?: WalletInstance
}): Promise<AccountInstance> => {
if (wallet) {
return assertEx(accountDerivationPath ? await wallet.derivePath(accountDerivationPath) : wallet, 'Failed to derive account from path')
} else if (account === 'random') {
return await HDWallet.random()
} else if (account) {
return account
} else {
//this should eventually be removed/thrown
console.warn('AbstractModule.determineAccount: No account provided - Creating Random account')
return await HDWallet.random()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QueryBoundWitness } from '@xyo-network/boundwitness-builder'
import { QueryBoundWitness } from '@xyo-network/boundwitness-model'
import { EventData } from '@xyo-network/module-events'
import { Payload } from '@xyo-network/payload-model'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QueryBoundWitness } from '@xyo-network/boundwitness-builder'
import { QueryBoundWitness } from '@xyo-network/boundwitness-model'
import { Payload } from '@xyo-network/payload-model'
import { Promisable } from '@xyo-network/promise'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { assertEx } from '@xylabs/assert'
import { compact } from '@xylabs/lodash'
import { Account } from '@xyo-network/account'
import { AccountInstance } from '@xyo-network/account-model'
import { QueryBoundWitness, QueryBoundWitnessBuilder } from '@xyo-network/boundwitness-builder'
import { QueryBoundWitnessBuilder } from '@xyo-network/boundwitness-builder'
import { QueryBoundWitness } from '@xyo-network/boundwitness-model'
import { BoundWitnessWrapper } from '@xyo-network/boundwitness-wrapper'
import { Base } from '@xyo-network/core'
import { ModuleManifestPayload } from '@xyo-network/manifest-model'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { assertEx } from '@xylabs/assert'
import { AddressPayload, AddressSchema } from '@xyo-network/address-payload-plugin'
import { QueryBoundWitness, QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-builder'
import { QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-builder'
import { QueryBoundWitness } from '@xyo-network/boundwitness-model'
import { NodeManifestPayload, NodeManifestPayloadSchema } from '@xyo-network/manifest-model'
import { AbstractModuleInstance } from '@xyo-network/module-abstract'
import {
Expand Down
4 changes: 2 additions & 2 deletions packages/modules/packages/sentinel/src/AbstractSentinel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assertEx } from '@xylabs/assert'
import { uniq } from '@xylabs/lodash'
import { QueryBoundWitness, QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-builder'
import { BoundWitness, isBoundWitness, notBoundWitness } from '@xyo-network/boundwitness-model'
import { QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-builder'
import { BoundWitness, isBoundWitness, notBoundWitness, QueryBoundWitness } from '@xyo-network/boundwitness-model'
import { AbstractModuleInstance } from '@xyo-network/module-abstract'
import { ModuleConfig, ModuleQueryHandlerResult } from '@xyo-network/module-model'
import { Payload } from '@xyo-network/payload-model'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { assertEx } from '@xylabs/assert'
import { ArchivistInstance } from '@xyo-network/archivist-model'
import { QueryBoundWitness, QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-builder'
import { QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-builder'
import { QueryBoundWitness } from '@xyo-network/boundwitness-model'
import { PayloadHasher } from '@xyo-network/core'
import { AbstractModuleInstance } from '@xyo-network/module-abstract'
import { creatableModule, ModuleConfig, ModuleQueryHandlerResult } from '@xyo-network/module-model'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { QueryBoundWitness } from '@xyo-network/boundwitness-builder'
import { BoundWitness, BoundWitnessSchema } from '@xyo-network/boundwitness-model'
import { BoundWitness, BoundWitnessSchema, QueryBoundWitness } from '@xyo-network/boundwitness-model'
import { AbstractModule } from '@xyo-network/module-abstract'
import { Module, ModuleConfig, ModuleConfigSchema } from '@xyo-network/module-model'
import { ArchiveModuleConfig } from '@xyo-network/node-core-model'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertEx } from '@xylabs/assert'
import { asyncHandler } from '@xylabs/sdk-api-express-ecs'
import { QueryBoundWitness } from '@xyo-network/boundwitness-builder'
import { QueryBoundWitness } from '@xyo-network/boundwitness-model'
import { Module, ModuleQueryResult } from '@xyo-network/module-model'
import { trimAddressPrefix } from '@xyo-network/node-core-lib'
import { Payload } from '@xyo-network/payload-model'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Account } from '@xyo-network/account'
import { AddressPayload, AddressSchema } from '@xyo-network/address-payload-plugin'
import { QueryBoundWitness, QueryBoundWitnessBuilder } from '@xyo-network/boundwitness-builder'
import { BoundWitness, BoundWitnessSchema } from '@xyo-network/boundwitness-model'
import { QueryBoundWitnessBuilder } from '@xyo-network/boundwitness-builder'
import { BoundWitness, BoundWitnessSchema, QueryBoundWitness } from '@xyo-network/boundwitness-model'
import { ModuleDiscoverQuerySchema } from '@xyo-network/module-model'
import { PayloadBuilder } from '@xyo-network/payload-builder'
import { Payload } from '@xyo-network/payload-model'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { testIf } from '@xylabs/jest-helpers'

/**
* @group crypto
* @group slow
*/

describe('EthereumGasEthersWitness', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { ImageThumbnailWitness } from '../Witness'
// eslint-disable-next-line import/no-named-as-default-member
const testIfHasBin = (bin: string) => (hasbin.sync(bin) ? it : it.skip)

/**
* @group thumbnail
*/

describe('ImageThumbnailWitness', () => {
testIfHasBin('magick')('DATA [medium/png]', async () => {
const witness = await ImageThumbnailWitness.create({ account: await HDWallet.random() })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { ImageThumbnailWitness } from '../Witness'
// eslint-disable-next-line import/no-named-as-default-member
const describeIfHasBin = (bin: string) => (hasbin.sync(bin) ? describe : describe.skip)

/**
* @group thumbnail
*/

describeIfHasBin('magick')('ImageThumbnailWitness', () => {
let witness: ImageThumbnailWitness
beforeAll(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { ImageThumbnailWitness } from '../Witness'
// eslint-disable-next-line import/no-named-as-default-member
const testIfHasBin = (bin: string) => (hasbin.sync(bin) ? it : it.skip)

/**
* @group thumbnail
*/

describe('ImageThumbnailWitness', () => {
let witness: ImageThumbnailWitness
beforeAll(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { mock } from 'jest-mock-extended'

import { ImageThumbnailWitness } from '../Witness'

/**
* @group thumbnail
*/

describe('Witness', () => {
describe('when behind sentinel', () => {
const archivistName = 'archivist'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { mock } from 'jest-mock-extended'

import { ImageThumbnailWitness } from '../Witness'

/**
* @group thumbnail
*/

describe('Witness', () => {
describe('checkIpfsUrl', () => {
const logger = mock<Console>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const testVideoFormat = async (witness: ImageThumbnailWitness, url: string) => {
expect(fileType?.mime).toBe('image/png')
}

/**
* @group thumbnail
*/

describeIfHasBin('magick')('ImageThumbnailWitness', () => {
describe('with video type', () => {
let witness: ImageThumbnailWitness
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class BoundWitnessBuilder<TBoundWitness extends BoundWitness<{ schema: st
private _payloadHashes: string[] | undefined
private _payloadSchemas: string[] | undefined
private _payloads: TPayload[] = []
private _sourceQuery: string | undefined
private _timestamp = Date.now()

constructor(
Expand Down Expand Up @@ -110,6 +111,10 @@ export class BoundWitnessBuilder<TBoundWitness extends BoundWitness<{ schema: st
result.timestamp = this._timestamp
}

if (this._sourceQuery) {
result.sourceQuery = this._sourceQuery
}

return result
}

Expand Down Expand Up @@ -138,6 +143,11 @@ export class BoundWitnessBuilder<TBoundWitness extends BoundWitness<{ schema: st
return this
}

sourceQuery(hash?: string) {
this._sourceQuery = hash
return this
}

witness(account: AccountInstance) {
this._accounts?.push(account)
return this
Expand Down
Loading

0 comments on commit bf00e08

Please sign in to comment.