Skip to content

Commit

Permalink
error system updated and tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
arietrouw committed Jul 28, 2023
1 parent dc7a52a commit 1290fc0
Show file tree
Hide file tree
Showing 49 changed files with 477 additions and 364 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"--forceExit",
"--runInBand",
"--testTimeout=120000",
"packages/modules/packages/bridge/packages/http/src/spec/HttpBridge.spec.ts"
"packages/node/packages/express/packages/test/src/routes/hash/get/get.spec.ts"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"bbox",
"bignumber",
"boundwitness",
"boundwitnesses",
"bowser",
"chlor",
"codeql",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { assertEx } from '@xylabs/assert'
import { QueryBoundWitness, QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-builder'
import { PayloadHasher } from '@xyo-network/core'
import {
CustomDivinerModule,
DivinerConfigSchema,
DivinerDivineQuerySchema,
DivinerModule,
Expand All @@ -10,16 +11,17 @@ import {
DivinerQuery,
DivinerQueryBase,
} from '@xyo-network/diviner-model'
import { AbstractModuleInstance, ModuleConfig, ModuleQueryHandlerResult } from '@xyo-network/module'
import { AbstractModuleInstance } from '@xyo-network/module-abstract'
import { ModuleConfig, ModuleQueryHandlerResult } from '@xyo-network/module-model'
import { Payload } from '@xyo-network/payload-model'
import { Promisable } from '@xyo-network/promise'

export abstract class AbstractDiviner<
TParams extends DivinerParams = DivinerParams,
TEventData extends DivinerModuleEventData = DivinerModuleEventData,
TEventData extends DivinerModuleEventData<DivinerModule<TParams>> = DivinerModuleEventData<DivinerModule<TParams>>,
>
extends AbstractModuleInstance<TParams, TEventData>
implements DivinerModule<TParams>
implements CustomDivinerModule<TParams, TEventData>
{
static override readonly configSchemas: string[] = [DivinerConfigSchema]
static targetSchema: string
Expand All @@ -34,13 +36,18 @@ export abstract class AbstractDiviner<
}
}

/** @function divine The main entry point for a diviner. Do not override this function. Implement/override divineHandler for custom functionality */
divine(payloads?: Payload[]): Promise<Payload[]> {
return this.busy(async () => {
await this.started('throw')
return await this.divineHandler(payloads)
await this.emit('divineStart', { inPayloads: payloads, module: this })
const resultPayloads = await this.divineHandler(payloads)
await this.emit('divineStart', { inPayloads: payloads, module: this, outPayloads: resultPayloads })
return resultPayloads
})
}

/** @function queryHandler Calls divine for a divine query. Override to support additional queries. */
protected override async queryHandler<T extends QueryBoundWitness = QueryBoundWitness, TConfig extends ModuleConfig = ModuleConfig>(
query: T,
payloads?: Payload[],
Expand All @@ -54,15 +61,14 @@ export abstract class AbstractDiviner<
const resultPayloads: Payload[] = []
switch (queryPayload.schema) {
case DivinerDivineQuerySchema:
await this.emit('reportStart', { inPayloads: payloads, module: this })
resultPayloads.push(...(await this.divineHandler(cleanPayloads)))
await this.emit('reportEnd', { inPayloads: payloads, module: this, outPayloads: resultPayloads })
resultPayloads.push(...(await this.divine(cleanPayloads)))
break
default:
return super.queryHandler(query, payloads)
}
return resultPayloads
}

/** @function divineHandler Implement or override to add custom functionality to a diviner */
protected abstract divineHandler(payloads?: Payload[]): Promisable<Payload[]>
}
55 changes: 0 additions & 55 deletions packages/modules/packages/diviner/packages/model/src/Diviner.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Payload } from '@xyo-network/payload-model'
import { Promisable } from '@xyo-network/promise'

export interface DivinerQueryFunctions {
divine: (payloads?: Payload[]) => Promisable<Payload[]>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Module, ModuleEventData } from '@xyo-network/module-model'

import { DivineEndEventData, DivineStartEventData } from './Events'

export interface DivinerModuleEventData<T extends Module = Module> extends DivineEndEventData<T>, DivineStartEventData<T>, ModuleEventData<T> {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EventData } from '@xyo-network/module'
import { Module, ModuleEventArgs } from '@xyo-network/module-model'
import { Payload } from '@xyo-network/payload-model'

export type WitnessReportEndEventArgs<T extends Module = Module> = ModuleEventArgs<
export type DivineEndEventArgs<T extends Module = Module> = ModuleEventArgs<
T,
{
errors?: Error[]
Expand All @@ -11,6 +11,6 @@ export type WitnessReportEndEventArgs<T extends Module = Module> = ModuleEventAr
}
>

export interface WitnessReportEndEventData<T extends Module = Module> extends EventData {
reportEnd: WitnessReportEndEventArgs<T>
export interface DivineEndEventData<T extends Module = Module> extends EventData {
observeEnd: DivineEndEventArgs<T>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { EventData } from '@xyo-network/module'
import { Module, ModuleEventArgs } from '@xyo-network/module-model'
import { Payload } from '@xyo-network/payload-model'

export type DivineStartEventArgs<T extends Module = Module> = ModuleEventArgs<
T,
{
inPayloads?: Payload[]
}
>

export interface DivineStartEventData<T extends Module = Module> extends EventData {
divineStart: DivineStartEventArgs<T>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './DivineEnd'
export * from './DivineStart'
16 changes: 16 additions & 0 deletions packages/modules/packages/diviner/packages/model/src/Instance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ModuleInstance } from '@xyo-network/module-model'

import { DivinerQueryFunctions } from './DivinerQueryFunctions'
import { DivinerModuleEventData } from './EventData'
import { CustomDivinerModule, DivinerModule } from './Module'
import { DivinerParams } from './Params'

export type DivinerInstance<TParams extends DivinerParams = DivinerParams> = DivinerModule<TParams> & DivinerQueryFunctions & ModuleInstance

export type CustomDivinerInstance<
TParams extends DivinerParams = DivinerParams,
TEvents extends DivinerModuleEventData<DivinerInstance<TParams>> = DivinerModuleEventData<DivinerInstance<TParams>>,
> = CustomDivinerModule<TParams, TEvents> & DivinerQueryFunctions & ModuleInstance

/** @deprecated use DivinerInstance instead */
export type Diviner = DivinerInstance
11 changes: 11 additions & 0 deletions packages/modules/packages/diviner/packages/model/src/Module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@xyo-network/module-model'

import { DivinerModuleEventData } from './EventData'
import { DivinerParams } from './Params'

export type DivinerModule<TParams extends DivinerParams = DivinerParams> = Module<TParams, DivinerModuleEventData<DivinerModule>>

export type CustomDivinerModule<
TParams extends DivinerParams = DivinerParams,
TEvents extends DivinerModuleEventData<DivinerModule<TParams>> = DivinerModuleEventData<DivinerModule<TParams>>,
> = Module<TParams, TEvents>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { AnyObject } from '@xyo-network/core'
import { AnyConfigSchema, ModuleParams } from '@xyo-network/module-model'

import { DivinerConfig } from './Config'

export type DivinerParams<
TConfig extends AnyConfigSchema<DivinerConfig> = AnyConfigSchema<DivinerConfig>,
TAdditional extends AnyObject | undefined = undefined,
> = ModuleParams<TConfig, TAdditional>
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export * from './Config'
export * from './Diviner'
export * from './DivinerQueryFunctions'
export * from './EventData'
export * from './Events'
export * from './Instance'
export * from './Module'
export * from './Params'
export * from './Queries'
export * from './typeChecks'
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { IsInstanceFactory, IsModuleFactory, isModuleInstance, WithFactory } from '@xyo-network/module-model'
import { AsObjectFactory } from '@xyo-network/object-identity'

import { DivinerInstance, DivinerModule } from './Diviner'
import { DivinerInstance } from './Instance'
import { DivinerModule } from './Module'
import { DivinerDivineQuerySchema } from './Queries'

export const isDivinerInstance = new IsInstanceFactory<DivinerInstance>().create({ divine: 'function' }, [isModuleInstance])
Expand Down
31 changes: 20 additions & 11 deletions packages/modules/packages/sentinel/src/AbstractSentinel.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import { assertEx } from '@xylabs/assert'
import { AbstractArchivingModule, ArchivistInstance, asArchivistInstance } from '@xyo-network/archivist'
import { QueryBoundWitness, QueryBoundWitnessWrapper } from '@xyo-network/boundwitness-builder'
import { BoundWitness } from '@xyo-network/boundwitness-model'
import { AnyConfigSchema, ModuleConfig, ModuleQueryHandlerResult } from '@xyo-network/module'
import { BoundWitness, isBoundWitness, notBoundWitness } from '@xyo-network/boundwitness-model'
import { ModuleConfig, ModuleQueryHandlerResult } from '@xyo-network/module'
import { Payload } from '@xyo-network/payload-model'
import { isWitnessInstance, WitnessInstance } from '@xyo-network/witness'
import uniq from 'lodash/uniq'

import { SentinelConfig, SentinelConfigSchema } from './Config'
import { SentinelQueryBase, SentinelReportQuerySchema } from './Queries'
import { SentinelInstance, SentinelModuleEventData, SentinelParams } from './SentinelModel'
import {
CustomSentinelInstance,
SentinelConfigSchema,
SentinelInstance,
SentinelModuleEventData,
SentinelParams,
SentinelQueryBase,
SentinelReportQuerySchema,
} from './model'

export abstract class AbstractSentinel<
TParams extends SentinelParams<AnyConfigSchema<SentinelConfig>> = SentinelParams<SentinelConfig>,
TEventData extends SentinelModuleEventData = SentinelModuleEventData,
TParams extends SentinelParams = SentinelParams,
TEventData extends SentinelModuleEventData<SentinelInstance<TParams>> = SentinelModuleEventData<SentinelInstance<TParams>>,
>
extends AbstractArchivingModule<TParams, TEventData>
implements SentinelInstance<TParams, TEventData>
implements CustomSentinelInstance<TParams, TEventData>
{
static override readonly configSchemas: string[] = [SentinelConfigSchema]

Expand Down Expand Up @@ -82,9 +88,12 @@ export abstract class AbstractSentinel<

async report(inPayloads?: Payload[]): Promise<Payload[]> {
await this.emit('reportStart', { inPayloads, module: this })
const outPayloads = await this.reportHandler(inPayloads)
await this.emit('reportEnd', { inPayloads, module: this, outPayloads })
return outPayloads
const payloads = await this.reportHandler(inPayloads)
const outPayloads = payloads.filter(notBoundWitness)
const boundwitnesses = payloads.filter(isBoundWitness)
const boundwitness = boundwitnesses.find((bw) => bw.addresses.includes(this.address))
await this.emit('reportEnd', { boundwitness, inPayloads, module: this, outPayloads })
return payloads
}

protected override async queryHandler<T extends QueryBoundWitness = QueryBoundWitness, TConfig extends ModuleConfig = ModuleConfig>(
Expand Down
15 changes: 4 additions & 11 deletions packages/modules/packages/sentinel/src/MemorySentinel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,21 @@ import { Payload } from '@xyo-network/payload-model'
import { WitnessInstance } from '@xyo-network/witness'

import { AbstractSentinel } from './AbstractSentinel'
import { SentinelConfig, SentinelConfigSchema } from './Config'
import { SentinelReportQuerySchema } from './Queries'
import { SentinelModule, SentinelModuleEventData, SentinelParams } from './SentinelModel'
import { SentinelConfig, SentinelConfigSchema, SentinelInstance, SentinelModuleEventData, SentinelParams, SentinelReportQuerySchema } from './model'

export type MemorySentinelParams<TConfig extends AnyConfigSchema<SentinelConfig> = AnyConfigSchema<SentinelConfig>> = SentinelParams<
AnyConfigSchema<TConfig>
>

export class MemorySentinel<
TParams extends MemorySentinelParams = MemorySentinelParams,
TEventData extends SentinelModuleEventData = SentinelModuleEventData,
>
extends AbstractSentinel<TParams, TEventData>
implements SentinelModule<TParams, TEventData>
{
TParams extends MemorySentinelParams = MemorySentinelParams,
TEventData extends SentinelModuleEventData<SentinelInstance<TParams>> = SentinelModuleEventData<SentinelInstance<TParams>>,
> extends AbstractSentinel<TParams, TEventData> {
static override configSchemas = [SentinelConfigSchema]

async reportHandler(payloads: Payload[] = []): Promise<Payload[]> {
await this.started('throw')
const errors: Error[] = []
await this.emit('reportStart', { inPayloads: payloads, module: this as SentinelModule })
const allWitnesses = [...(await this.getWitnesses())]
const resultPayloads: Payload[] = []

Expand All @@ -43,7 +37,6 @@ export class MemorySentinel<

const [boundWitness] = await this.bindQueryResult({ schema: SentinelReportQuerySchema }, resultPayloads)
this.history.push(assertEx(boundWitness))
await this.emit('reportEnd', { boundWitness, errors, inPayloads: payloads, module: this as SentinelModule, outPayloads: resultPayloads })
return [boundWitness, ...resultPayloads]
}

Expand Down
53 changes: 0 additions & 53 deletions packages/modules/packages/sentinel/src/SentinelModel.ts

This file was deleted.

Loading

0 comments on commit 1290fc0

Please sign in to comment.