@xyo-network/diviner-stateful 5.3.22 → 5.3.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xyo-network/diviner-stateful",
3
- "version": "5.3.22",
3
+ "version": "5.3.24",
4
4
  "description": "Primary SDK for using XYO Protocol 2.0",
5
5
  "homepage": "https://xyo.network",
6
6
  "bugs": {
@@ -30,63 +30,59 @@
30
30
  "types": "dist/neutral/index.d.ts",
31
31
  "files": [
32
32
  "dist",
33
- "src",
34
33
  "!**/*.bench.*",
35
34
  "!**/*.spec.*",
36
35
  "!**/*.test.*",
37
36
  "README.md"
38
37
  ],
39
38
  "dependencies": {
40
- "@xyo-network/archivist-model": "~5.3.22",
41
- "@xyo-network/archivist-wrapper": "~5.3.22",
42
- "@xyo-network/boundwitness-builder": "~5.3.22",
43
- "@xyo-network/boundwitness-model": "~5.3.22",
44
- "@xyo-network/diviner-abstract": "~5.3.22",
45
- "@xyo-network/diviner-boundwitness-model": "~5.3.22",
46
- "@xyo-network/diviner-model": "~5.3.22",
47
- "@xyo-network/diviner-wrapper": "~5.3.22",
48
- "@xyo-network/module-model": "~5.3.22",
49
- "@xyo-network/payload-builder": "~5.3.22",
50
- "@xyo-network/payload-model": "~5.3.22"
39
+ "@xyo-network/archivist-model": "~5.3.24",
40
+ "@xyo-network/archivist-wrapper": "~5.3.24",
41
+ "@xyo-network/boundwitness-builder": "~5.3.24",
42
+ "@xyo-network/boundwitness-model": "~5.3.24",
43
+ "@xyo-network/diviner-model": "~5.3.24",
44
+ "@xyo-network/diviner-abstract": "~5.3.24",
45
+ "@xyo-network/module-model": "~5.3.24",
46
+ "@xyo-network/payload-model": "~5.3.24",
47
+ "@xyo-network/payload-builder": "~5.3.24",
48
+ "@xyo-network/diviner-wrapper": "~5.3.24",
49
+ "@xyo-network/diviner-boundwitness-model": "~5.3.24"
51
50
  },
52
51
  "devDependencies": {
53
52
  "@opentelemetry/api": "^1.9.1",
54
53
  "@types/node": "^25.5.0",
55
- "@xylabs/sdk-js": "^5.0.91",
56
- "@xylabs/ts-scripts-common": "~7.6.8",
57
- "@xylabs/ts-scripts-yarn3": "~7.6.8",
58
- "@xylabs/tsconfig": "~7.6.8",
59
- "@xylabs/vitest-extended": "~5.0.91",
60
- "@xyo-network/archivist-memory": "~5.3.22",
61
- "@xyo-network/archivist-model": "~5.3.22",
62
- "@xyo-network/archivist-wrapper": "~5.3.22",
63
- "@xyo-network/boundwitness-builder": "~5.3.22",
64
- "@xyo-network/boundwitness-model": "~5.3.22",
65
- "@xyo-network/diviner-abstract": "~5.3.22",
66
- "@xyo-network/diviner-boundwitness-memory": "~5.3.22",
67
- "@xyo-network/diviner-boundwitness-model": "~5.3.22",
68
- "@xyo-network/diviner-model": "~5.3.22",
69
- "@xyo-network/diviner-payload-generic": "~5.3.22",
70
- "@xyo-network/diviner-wrapper": "~5.3.22",
71
- "@xyo-network/manifest": "~5.3.22",
72
- "@xyo-network/module-factory-locator": "~5.3.22",
73
- "@xyo-network/module-model": "~5.3.22",
74
- "@xyo-network/node-memory": "~5.3.22",
75
- "@xyo-network/payload-builder": "~5.3.22",
76
- "@xyo-network/payload-model": "~5.3.22",
77
- "@xyo-network/wallet": "~5.3.22",
54
+ "@xylabs/sdk-js": "^5.0.93",
55
+ "@xylabs/ts-scripts-common": "~7.6.16",
56
+ "@xylabs/ts-scripts-pnpm": "~7.6.16",
57
+ "@xylabs/tsconfig": "~7.6.16",
58
+ "@xylabs/vitest-extended": "~5.0.93",
78
59
  "acorn": "^8.16.0",
79
60
  "axios": "^1.14.0",
80
- "cosmiconfig": "^9.0.1",
81
- "esbuild": "^0.27.4",
82
- "eslint": "^10.1.0",
61
+ "esbuild": "^0.28.0",
83
62
  "ethers": "^6.16.0",
84
- "rollup": "^4.60.1",
85
63
  "tslib": "^2.8.1",
86
64
  "typescript": "~5.9.3",
87
65
  "vite": "^8.0.3",
88
66
  "vitest": "~4.1.2",
89
- "zod": "^4.3.6"
67
+ "zod": "^4.3.6",
68
+ "@xyo-network/archivist-memory": "~5.3.24",
69
+ "@xyo-network/diviner-abstract": "~5.3.24",
70
+ "@xyo-network/archivist-model": "~5.3.24",
71
+ "@xyo-network/archivist-wrapper": "~5.3.24",
72
+ "@xyo-network/boundwitness-builder": "~5.3.24",
73
+ "@xyo-network/boundwitness-model": "~5.3.24",
74
+ "@xyo-network/diviner-model": "~5.3.24",
75
+ "@xyo-network/diviner-payload-generic": "~5.3.24",
76
+ "@xyo-network/module-factory-locator": "~5.3.24",
77
+ "@xyo-network/diviner-wrapper": "~5.3.24",
78
+ "@xyo-network/module-model": "~5.3.24",
79
+ "@xyo-network/payload-model": "~5.3.24",
80
+ "@xyo-network/diviner-boundwitness-memory": "~5.3.24",
81
+ "@xyo-network/manifest": "~5.3.24",
82
+ "@xyo-network/payload-builder": "~5.3.24",
83
+ "@xyo-network/diviner-boundwitness-model": "~5.3.24",
84
+ "@xyo-network/node-memory": "~5.3.24",
85
+ "@xyo-network/wallet": "~5.3.24"
90
86
  },
91
87
  "peerDependencies": {
92
88
  "@xylabs/sdk-js": "^5",
@@ -97,4 +93,4 @@
97
93
  "publishConfig": {
98
94
  "access": "public"
99
95
  }
100
- }
96
+ }
package/src/Config.ts DELETED
@@ -1,26 +0,0 @@
1
- import type { DivinerConfig } from '@xyo-network/diviner-model'
2
- import type { ModuleIdentifier } from '@xyo-network/module-model'
3
- import { asSchema } from '@xyo-network/payload-model'
4
-
5
- import { StatefulDivinerSchema } from './Schema.ts'
6
-
7
- /**
8
- * The schema for a Stateful Diviner config
9
- */
10
- export const StatefulDivinerConfigSchema = asSchema(`${StatefulDivinerSchema}.config`, true)
11
- /**
12
- * The schema for a Stateful Diviner config
13
- */
14
- export type StatefulDivinerConfigSchema = typeof StatefulDivinerConfigSchema
15
-
16
- /**
17
- * The config for a Stateful Diviner
18
- */
19
- export type StatefulDivinerConfig = DivinerConfig<{
20
- schema: StatefulDivinerConfigSchema
21
- stateStore: {
22
- archivist: ModuleIdentifier
23
- boundWitnessDiviner: ModuleIdentifier
24
- payloadDiviner: ModuleIdentifier
25
- }
26
- }>
package/src/Diviner.ts DELETED
@@ -1,140 +0,0 @@
1
- import type { Hash } from '@xylabs/sdk-js'
2
- import { assertEx, toSafeJson } from '@xylabs/sdk-js'
3
- import { ArchivistWrapper } from '@xyo-network/archivist-wrapper'
4
- import { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'
5
- import { isBoundWitness } from '@xyo-network/boundwitness-model'
6
- import { AbstractDiviner } from '@xyo-network/diviner-abstract'
7
- import type { BoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'
8
- import { BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'
9
- import type { DivinerInstance, DivinerModuleEventData } from '@xyo-network/diviner-model'
10
- import { DivinerWrapper } from '@xyo-network/diviner-wrapper'
11
- import type { ModuleState, StateDictionary } from '@xyo-network/module-model'
12
- import { isModuleState, ModuleStateSchema } from '@xyo-network/module-model'
13
- import { PayloadBuilder } from '@xyo-network/payload-builder'
14
- import type {
15
- Payload, Schema,
16
- WithStorageMeta,
17
- } from '@xyo-network/payload-model'
18
- import { SequenceConstants } from '@xyo-network/payload-model'
19
-
20
- import { StatefulDivinerConfigSchema } from './Config.ts'
21
- import type { StatefulDivinerParams } from './Params.ts'
22
-
23
- const moduleName = 'StatefulDiviner'
24
-
25
- /**
26
- * A Diviner that maintains state
27
- */
28
- export abstract class StatefulDiviner<
29
- TParams extends StatefulDivinerParams = StatefulDivinerParams,
30
- TIn extends Payload = Payload,
31
- TOut extends Payload = Payload,
32
- TEventData extends DivinerModuleEventData<DivinerInstance<TParams, TIn, TOut>, TIn, TOut> = DivinerModuleEventData<
33
- DivinerInstance<TParams, TIn, TOut>,
34
- TIn,
35
- TOut
36
- >,
37
- TState extends StateDictionary = StateDictionary,
38
- > extends AbstractDiviner<TParams, TIn, TOut, TEventData> {
39
- static override readonly configSchemas: Schema[] = [...super.configSchemas, StatefulDivinerConfigSchema]
40
- static override readonly defaultConfigSchema: Schema = StatefulDivinerConfigSchema
41
-
42
- /**
43
- * The last state
44
- */
45
- protected _lastState?: ModuleState<TState>
46
-
47
- /**
48
- * Commit the internal state of the Diviner process. This is similar
49
- * to a transaction completion in a database and should only be called
50
- * when results have been successfully persisted to the appropriate
51
- * external stores.
52
- * @param nextState The state to commit
53
- */
54
- protected async commitState(nextState: ModuleState<TState>) {
55
- // Don't commit state if no state has changed
56
- if (toSafeJson(nextState.state) === toSafeJson(this._lastState?.state)) return
57
- this._lastState = nextState
58
- const archivist = await this.getArchivistForStateStore()
59
- const [bw] = await new BoundWitnessBuilder().payload(nextState).signer(this.account).build()
60
- await archivist.insert([bw, nextState])
61
- }
62
-
63
- /**
64
- * Retrieves the archivist for the specified store
65
- * @param store The store to retrieve the archivist for
66
- * @returns The archivist for the specified store
67
- */
68
- protected async getArchivistForStateStore() {
69
- const name = assertEx(this.config?.stateStore?.archivist, () => `${moduleName}: Config for stateStore.archivist not specified`)
70
- const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.archivist`)
71
- return ArchivistWrapper.wrap(mod, this.account)
72
- }
73
-
74
- /**
75
- * Retrieves the BoundWitness Diviner for the specified store
76
- * @param store The store to retrieve the BoundWitness Diviner for
77
- * @returns The BoundWitness Diviner for the specified store
78
- */
79
- protected async getBoundWitnessDivinerForStateStore() {
80
- const name = assertEx(this.config?.stateStore?.boundWitnessDiviner, () => `${moduleName}: Config for stateStore.boundWitnessDiviner not specified`)
81
- const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.boundWitnessDiviner`)
82
- return DivinerWrapper.wrap(mod, this.account)
83
- }
84
-
85
- /**
86
- * Retrieves the Payload Diviner for the specified store
87
- * @param store The store to retrieve the Payload Diviner for
88
- * @returns The Payload Diviner for the specified store
89
- */
90
- protected async getPayloadDivinerForStateStore() {
91
- const name = assertEx(this.config?.stateStore?.payloadDiviner, () => `${moduleName}: Config for stateStore.payloadDiviner not specified`)
92
- const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.payloadDiviner`)
93
- return DivinerWrapper.wrap(mod, this.account)
94
- }
95
-
96
- /**
97
- * Retrieves the last state of the Diviner process. Used to recover state after
98
- * preemptions, reboots, etc.
99
- */
100
- protected async retrieveState(): Promise<ModuleState<TState> | undefined> {
101
- if (this._lastState) return this._lastState
102
- let hash: Hash | null = null
103
- const diviner = await this.getBoundWitnessDivinerForStateStore()
104
- const query = new PayloadBuilder<BoundWitnessDivinerQueryPayload>({ schema: BoundWitnessDivinerQuerySchema })
105
- .fields({
106
- address: this.account.address,
107
- limit: 1,
108
- cursor: SequenceConstants.minLocalSequence,
109
- order: 'desc',
110
- payload_schemas: [ModuleStateSchema],
111
- })
112
- .build()
113
- const boundWitnesses = await diviner.divine([query])
114
- if (boundWitnesses.length > 0) {
115
- const boundWitness = boundWitnesses[0]
116
- if (isBoundWitness(boundWitness)) {
117
- // Find the index for this address in the BoundWitness that is a ModuleState
118
- hash = boundWitness.addresses
119
- .map((address, index) => ({ address, index }))
120
- .filter(({ address }) => address === this.account.address)
121
- // eslint-disable-next-line unicorn/no-array-reduce
122
- .reduce(
123
- (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),
124
- null as Hash | null,
125
- )
126
- }
127
- }
128
-
129
- // If we able to located the last state
130
- if (hash !== null) {
131
- // Get last state
132
- const archivist = await this.getArchivistForStateStore()
133
- const payload = (await archivist.get([hash])).find(isModuleState<TState>)
134
- if (payload) {
135
- return payload as WithStorageMeta<ModuleState<TState>>
136
- }
137
- }
138
- return undefined
139
- }
140
- }
@@ -1,154 +0,0 @@
1
- import type { Hash } from '@xylabs/sdk-js'
2
- import { assertEx, toSafeJson } from '@xylabs/sdk-js'
3
- import { asArchivistInstance } from '@xyo-network/archivist-model'
4
- import { BoundWitnessBuilder } from '@xyo-network/boundwitness-builder'
5
- import { isBoundWitness } from '@xyo-network/boundwitness-model'
6
- import type { BoundWitnessDivinerQueryPayload } from '@xyo-network/diviner-boundwitness-model'
7
- import { BoundWitnessDivinerQuerySchema } from '@xyo-network/diviner-boundwitness-model'
8
- import { asDivinerInstance } from '@xyo-network/diviner-model'
9
- import type {
10
- AnyConfigSchema,
11
- ModuleInstance,
12
- ModuleState,
13
- QueryableModuleParams,
14
- StateDictionary,
15
- } from '@xyo-network/module-model'
16
- import {
17
- isModuleState,
18
- ModuleStateSchema,
19
- } from '@xyo-network/module-model'
20
- import { PayloadBuilder } from '@xyo-network/payload-builder'
21
- import type { WithStorageMeta } from '@xyo-network/payload-model'
22
- import { SequenceConstants } from '@xyo-network/payload-model'
23
-
24
- import type { StatefulDivinerConfig } from './Config.ts'
25
-
26
- export type StatefulModuleParams = QueryableModuleParams<AnyConfigSchema<StatefulDivinerConfig>>
27
-
28
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
29
- export type AnyModule<TParams extends StatefulModuleParams = StatefulModuleParams> = new (...args: any[]) => ModuleInstance<TParams>
30
-
31
- const moduleName = 'StatefulModuleMixin'
32
-
33
- /**
34
- * @internal
35
- * @ignore Inherit from StatefulDiviner instead
36
- * @param ModuleBase
37
- * @returns
38
- */
39
- export const StatefulModuleMixin = <
40
- TParams extends StatefulModuleParams = StatefulModuleParams,
41
- TModule extends AnyModule<TParams> = AnyModule<TParams>,
42
- TState extends StateDictionary = StateDictionary,
43
- >(
44
- ModuleBase: TModule,
45
- ) => {
46
- abstract class StatefulModuleBase extends ModuleBase {
47
- _lastState?: ModuleState<TState>
48
-
49
- /**
50
- * Commit the internal state of the Diviner process. This is similar
51
- * to a transaction completion in a database and should only be called
52
- * when results have been successfully persisted to the appropriate
53
- * external stores.
54
- * @param nextState The state to commit
55
- */
56
- async commitState(nextState: ModuleState<TState>) {
57
- // Don't commit state if no state has changed
58
- if (toSafeJson(nextState.state) === toSafeJson(this._lastState?.state)) return
59
- this._lastState = nextState
60
- const archivist = await this.getArchivistForStore()
61
- // const [bw] = await new BoundWitnessBuilder().payload(nextState).signer(this.account).build()
62
- const [bw] = await new BoundWitnessBuilder().payload(nextState).build()
63
- await archivist.insert([bw, nextState])
64
- }
65
-
66
- /**
67
- * Retrieves the archivist for the specified store
68
- * @param store The store to retrieve the archivist for
69
- * @returns The archivist for the specified store
70
- */
71
- async getArchivistForStore() {
72
- const name = assertEx(this.config?.stateStore?.archivist, () => `${moduleName}: Config for stateStore.archivist not specified`)
73
- const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.archivist`)
74
- // return ArchivistWrapper.wrap(mod, this.account)
75
- const instance = asArchivistInstance(mod)
76
- return assertEx(instance, () => `${moduleName}: Failed to wrap archivist instance`)
77
- }
78
-
79
- /**
80
- * Retrieves the BoundWitness Diviner for the specified store
81
- * @param store The store to retrieve the BoundWitness Diviner for
82
- * @returns The BoundWitness Diviner for the specified store
83
- */
84
- async getBoundWitnessDivinerForStore() {
85
- const name = assertEx(
86
- this.config?.stateStore?.boundWitnessDiviner,
87
- () => `${moduleName}: Config for stateStore.boundWitnessDiviner not specified`,
88
- )
89
- const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.boundWitnessDiviner`)
90
- // return DivinerWrapper.wrap(mod, this.account)
91
- const instance = asDivinerInstance(mod)
92
- return assertEx(instance, () => `${moduleName}: Failed to wrap diviner instance`)
93
- }
94
-
95
- /**
96
- * Retrieves the Payload Diviner for the specified store
97
- * @param store The store to retrieve the Payload Diviner for
98
- * @returns The Payload Diviner for the specified store
99
- */
100
- async getPayloadDivinerForStateStore() {
101
- const name = assertEx(this.config?.stateStore?.payloadDiviner, () => `${moduleName}: Config for stateStore.payloadDiviner not specified`)
102
- const mod = assertEx(await this.resolve(name), () => `${moduleName}: Failed to resolve stateStore.payloadDiviner`)
103
- // return DivinerWrapper.wrap(mod, this.account)
104
- const instance = asDivinerInstance(mod)
105
- return assertEx(instance, () => `${moduleName}: Failed to wrap diviner instance`)
106
- }
107
-
108
- /**
109
- * Retrieves the last state of the Diviner process. Used to recover state after
110
- * preemptions, reboots, etc.
111
- */
112
- async retrieveState(): Promise<ModuleState<TState> | undefined> {
113
- if (this._lastState) return this._lastState
114
- let hash: Hash | null = null
115
- const diviner = await this.getBoundWitnessDivinerForStore()
116
- const query = new PayloadBuilder<BoundWitnessDivinerQueryPayload>({ schema: BoundWitnessDivinerQuerySchema })
117
- .fields({
118
- // address: this.account.address,
119
- limit: 1,
120
- cursor: SequenceConstants.minLocalSequence,
121
- order: 'desc',
122
- payload_schemas: [ModuleStateSchema],
123
- })
124
- .build()
125
- const boundWitnesses = await diviner.divine([query])
126
- if (boundWitnesses.length > 0) {
127
- const boundWitness = boundWitnesses[0]
128
- if (isBoundWitness(boundWitness)) {
129
- // Find the index for this address in the BoundWitness that is a ModuleState
130
- hash = boundWitness.addresses
131
- .map((address, index) => ({ address, index }))
132
- // .filter(({ address }) => address === this.account.address)
133
- // eslint-disable-next-line unicorn/no-array-reduce
134
- .reduce(
135
- (prev, curr) => (boundWitness.payload_schemas?.[curr?.index] === ModuleStateSchema ? boundWitness.payload_hashes[curr?.index] : prev),
136
- null as Hash | null,
137
- )
138
- }
139
- }
140
-
141
- // If we able to located the last state
142
- if (hash !== null) {
143
- // Get last state
144
- const archivist = await this.getArchivistForStore()
145
- const payload = (await archivist.get([hash])).find(isModuleState<TState>)
146
- if (payload) {
147
- return payload as WithStorageMeta<ModuleState<TState>>
148
- }
149
- }
150
- return undefined
151
- }
152
- }
153
- return StatefulModuleBase
154
- }
package/src/Params.ts DELETED
@@ -1,9 +0,0 @@
1
- import type { DivinerParams } from '@xyo-network/diviner-model'
2
- import type { AnyConfigSchema } from '@xyo-network/module-model'
3
-
4
- import type { StatefulDivinerConfig } from './Config.ts'
5
-
6
- /**
7
- * The parameters for a Stateful Diviner
8
- */
9
- export type StatefulDivinerParams = DivinerParams<AnyConfigSchema<StatefulDivinerConfig>>
package/src/Schema.ts DELETED
@@ -1,4 +0,0 @@
1
- import { asSchema } from '@xyo-network/payload-model'
2
-
3
- export const StatefulDivinerSchema = asSchema('network.xyo.diviner.stateful', true)
4
- export type StatefulDivinerSchema = typeof StatefulDivinerSchema
package/src/index.ts DELETED
@@ -1,5 +0,0 @@
1
- export * from './Config.ts'
2
- export * from './Diviner.ts'
3
- export * from './DivinerMixin.ts'
4
- export * from './Params.ts'
5
- export * from './Schema.ts'
@@ -1,51 +0,0 @@
1
- {
2
- "$schema": "https://raw.githubusercontent.com/XYOracleNetwork/sdk-xyo-client-js/main/packages/manifest/src/schema.json",
3
- "nodes": [
4
- {
5
- "config": {
6
- "name": "DivinerTest",
7
- "schema": "network.xyo.node.config"
8
- },
9
- "modules": {
10
- "public": [
11
- {
12
- "config": {
13
- "language": "javascript",
14
- "name": "AddressStateArchivist",
15
- "schema": "network.xyo.archivist.config"
16
- }
17
- },
18
- {
19
- "config": {
20
- "archivist": "AddressStateArchivist",
21
- "language": "javascript",
22
- "name": "AddressStateBoundWitnessDiviner",
23
- "schema": "network.xyo.diviner.boundwitness.config"
24
- }
25
- },
26
- {
27
- "config": {
28
- "archivist": "AddressStateArchivist",
29
- "language": "javascript",
30
- "name": "AddressStatePayloadDiviner",
31
- "schema": "network.xyo.diviner.payload.config"
32
- }
33
- },
34
- {
35
- "config": {
36
- "language": "javascript",
37
- "name": "Diviner",
38
- "schema": "network.xyo.diviner.stateful.config",
39
- "stateStore": {
40
- "archivist": "AddressStateArchivist",
41
- "boundWitnessDiviner": "AddressStateBoundWitnessDiviner",
42
- "payloadDiviner": "AddressStatePayloadDiviner"
43
- }
44
- }
45
- }
46
- ]
47
- }
48
- }
49
- ],
50
- "schema": "network.xyo.manifest"
51
- }