@xyo-network/xl1-protocol-sdk 1.16.26 → 1.17.0
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/dist/neutral/ChainServiceCollectionV2.d.ts +5 -9
- package/dist/neutral/ChainServiceCollectionV2.d.ts.map +1 -1
- package/dist/neutral/index.mjs +242 -59
- package/dist/neutral/index.mjs.map +1 -1
- package/dist/neutral/primitives/index.d.ts +1 -0
- package/dist/neutral/primitives/index.d.ts.map +1 -1
- package/dist/neutral/primitives/state/findMostRecentBlock.d.ts +11 -0
- package/dist/neutral/primitives/state/findMostRecentBlock.d.ts.map +1 -0
- package/dist/neutral/primitives/state/hydratedBlockByNumber.d.ts +4 -0
- package/dist/neutral/primitives/state/hydratedBlockByNumber.d.ts.map +1 -0
- package/dist/neutral/primitives/state/index.d.ts +3 -0
- package/dist/neutral/primitives/state/index.d.ts.map +1 -0
- package/dist/neutral/provider/viewer/XyoViewer.d.ts +4 -4
- package/dist/neutral/provider/viewer/XyoViewer.d.ts.map +1 -1
- package/dist/neutral/provider/viewer/index.d.ts +0 -1
- package/dist/neutral/provider/viewer/index.d.ts.map +1 -1
- package/dist/neutral/services/Chain/BaseChainService.d.ts +0 -2
- package/dist/neutral/services/Chain/BaseChainService.d.ts.map +1 -1
- package/dist/neutral/services/Chain/ChainService.d.ts +2 -2
- package/dist/neutral/services/Chain/ChainService.d.ts.map +1 -1
- package/dist/neutral/services/Chain/interfaces/ChainStakeViewer.d.ts +2 -2
- package/dist/neutral/services/Chain/interfaces/ChainStakeViewer.d.ts.map +1 -1
- package/dist/neutral/simple/accountBalance/SimpleAccountBalanceViewer.d.ts +2 -2
- package/dist/neutral/simple/accountBalance/SimpleAccountBalanceViewer.d.ts.map +1 -1
- package/dist/neutral/simple/block/SimpleBlockViewer.d.ts +50 -0
- package/dist/neutral/simple/block/SimpleBlockViewer.d.ts.map +1 -0
- package/dist/neutral/simple/block/index.d.ts +2 -0
- package/dist/neutral/simple/block/index.d.ts.map +1 -0
- package/dist/neutral/simple/index.d.ts +1 -0
- package/dist/neutral/simple/index.d.ts.map +1 -1
- package/dist/neutral/utils/HydratedCache.d.ts +16 -0
- package/dist/neutral/utils/HydratedCache.d.ts.map +1 -0
- package/dist/neutral/utils/index.d.ts +1 -0
- package/dist/neutral/utils/index.d.ts.map +1 -1
- package/dist/neutral/viewers/Block.d.ts +6 -5
- package/dist/neutral/viewers/Block.d.ts.map +1 -1
- package/dist/neutral/viewers/Chain.d.ts +2 -0
- package/dist/neutral/viewers/Chain.d.ts.map +1 -1
- package/dist/neutral/{provider/viewer → viewers}/StepViewer.d.ts +2 -2
- package/dist/neutral/viewers/StepViewer.d.ts.map +1 -0
- package/dist/neutral/viewers/index.d.ts +1 -0
- package/dist/neutral/viewers/index.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/ChainServiceCollectionV2.ts +7 -10
- package/src/primitives/index.ts +1 -0
- package/src/primitives/state/findMostRecentBlock.ts +44 -0
- package/src/primitives/state/hydratedBlockByNumber.ts +19 -0
- package/src/primitives/state/index.ts +2 -0
- package/src/provider/viewer/XyoViewer.ts +6 -4
- package/src/provider/viewer/index.ts +0 -1
- package/src/services/Chain/BaseChainService.ts +1 -3
- package/src/services/Chain/ChainService.ts +2 -2
- package/src/services/Chain/interfaces/ChainStakeViewer.ts +4 -2
- package/src/simple/accountBalance/SimpleAccountBalanceViewer.ts +5 -5
- package/src/simple/block/SimpleBlockViewer.ts +133 -0
- package/src/simple/block/index.ts +1 -0
- package/src/simple/index.ts +1 -0
- package/src/utils/HydratedCache.ts +38 -0
- package/src/utils/index.ts +1 -0
- package/src/viewers/Block.ts +7 -6
- package/src/viewers/Chain.ts +2 -0
- package/src/{provider/viewer → viewers}/StepViewer.ts +2 -2
- package/src/viewers/index.ts +1 -0
- package/dist/neutral/provider/viewer/StepViewer.d.ts.map +0 -1
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import type { CreatableParams, Hash } from '@xylabs/sdk-js'
|
|
2
|
+
import {
|
|
3
|
+
AbstractCreatable,
|
|
4
|
+
assertEx,
|
|
5
|
+
spanRootAsync,
|
|
6
|
+
} from '@xylabs/sdk-js'
|
|
7
|
+
import type { ReadArchivist } from '@xyo-network/archivist-model'
|
|
8
|
+
import {
|
|
9
|
+
asSignedHydratedBlockWithHashMeta,
|
|
10
|
+
asXL1BlockNumber,
|
|
11
|
+
type SignedHydratedBlockWithHashMeta, type XL1BlockNumber,
|
|
12
|
+
} from '@xyo-network/xl1-protocol'
|
|
13
|
+
|
|
14
|
+
import { hydrateBlock } from '../../block/index.ts'
|
|
15
|
+
import type { ChainStoreRead, StakedChainContextRead } from '../../model/index.ts'
|
|
16
|
+
import { findMostRecentBlock, hydratedBlockByNumber } from '../../primitives/index.ts'
|
|
17
|
+
import { HydratedCache } from '../../utils/index.ts'
|
|
18
|
+
import type { BlockViewer } from '../../viewers/index.ts'
|
|
19
|
+
|
|
20
|
+
export interface SimpleBlockViewerParams extends CreatableParams {
|
|
21
|
+
context: StakedChainContextRead
|
|
22
|
+
finalizedArchivist?: ReadArchivist
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class SimpleBlockViewer extends AbstractCreatable<SimpleBlockViewerParams> implements BlockViewer {
|
|
26
|
+
private _signedHydratedBlockCache: HydratedCache<SignedHydratedBlockWithHashMeta> | undefined
|
|
27
|
+
|
|
28
|
+
get context(): StakedChainContextRead {
|
|
29
|
+
return this.params.context!
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
get finalizedArchivist(): ReadArchivist {
|
|
33
|
+
return this.params.finalizedArchivist!
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static override async paramsHandler(params: Partial<SimpleBlockViewerParams>) {
|
|
37
|
+
assertEx(params.context, () => 'context is required')
|
|
38
|
+
assertEx(params.finalizedArchivist, () => 'finalizedArchivist is required')
|
|
39
|
+
|
|
40
|
+
return { ...await super.paramsHandler(params) }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async blockByHash(hash: Hash): Promise<SignedHydratedBlockWithHashMeta | null> {
|
|
44
|
+
return await spanRootAsync('blockByHash', async () => {
|
|
45
|
+
const cache = this.getHydratedBlockCache()
|
|
46
|
+
return await cache.get(hash)
|
|
47
|
+
}, this.tracer)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async blockByNumber(blockNumber: XL1BlockNumber): Promise<SignedHydratedBlockWithHashMeta | null> {
|
|
51
|
+
return await spanRootAsync('blockByNumber', async () => {
|
|
52
|
+
return asSignedHydratedBlockWithHashMeta(await hydratedBlockByNumber(this.context, blockNumber)) ?? null
|
|
53
|
+
}, this.tracer)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async blocksByHash(hash: Hash, limit = 50): Promise<SignedHydratedBlockWithHashMeta[]> {
|
|
57
|
+
return await spanRootAsync('blocksByHash', async () => {
|
|
58
|
+
assertEx(limit > 0, () => 'limit must be greater than 0')
|
|
59
|
+
assertEx(limit <= 100, () => 'limit must be less than 100')
|
|
60
|
+
const blocks: SignedHydratedBlockWithHashMeta[] = []
|
|
61
|
+
let current = await this.blockByHash(hash)
|
|
62
|
+
while (current && blocks.length < limit) {
|
|
63
|
+
blocks.push(current)
|
|
64
|
+
const previousHash = current[0].previous
|
|
65
|
+
if (previousHash === null) break
|
|
66
|
+
current = await this.blockByHash(previousHash)
|
|
67
|
+
}
|
|
68
|
+
return blocks.map(b => asSignedHydratedBlockWithHashMeta(b, true))
|
|
69
|
+
}, this.tracer)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async blocksByNumber(blockNumber: XL1BlockNumber, limit = 50): Promise<SignedHydratedBlockWithHashMeta[]> {
|
|
73
|
+
return await spanRootAsync('blocksByHash', async () => {
|
|
74
|
+
assertEx(limit > 0, () => 'limit must be greater than 0')
|
|
75
|
+
assertEx(limit <= 100, () => 'limit must be less than 100')
|
|
76
|
+
const blocks: SignedHydratedBlockWithHashMeta[] = []
|
|
77
|
+
let current = await this.blockByNumber(blockNumber)
|
|
78
|
+
while (current && blocks.length < limit) {
|
|
79
|
+
blocks.push(current)
|
|
80
|
+
if (current[0].block === 0) break
|
|
81
|
+
const previousNumber = asXL1BlockNumber(current[0].block - 1, true)
|
|
82
|
+
current = await this.blockByNumber(previousNumber)
|
|
83
|
+
}
|
|
84
|
+
return blocks.map(b => asSignedHydratedBlockWithHashMeta(b, true))
|
|
85
|
+
}, this.tracer)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async currentBlock(): Promise<SignedHydratedBlockWithHashMeta> {
|
|
89
|
+
return await spanRootAsync('currentBlock', async () => {
|
|
90
|
+
const currentHead = assertEx(await this.getCurrentHead(), () => 'Could not find most recent block')
|
|
91
|
+
const cache = this.getHydratedBlockCache()
|
|
92
|
+
const block = await cache.get(currentHead._hash)
|
|
93
|
+
if (!block) {
|
|
94
|
+
console.log(`Could not find current block with hash ${currentHead!._hash}`)
|
|
95
|
+
}
|
|
96
|
+
return assertEx(block, () => 'Could not find current block')
|
|
97
|
+
}, this.tracer)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async currentBlockHash(): Promise<Hash> {
|
|
101
|
+
return await spanRootAsync('currentBlockHash', async () => {
|
|
102
|
+
const currentHead = assertEx(await this.getCurrentHead(), () => 'Could not find most recent block')
|
|
103
|
+
return currentHead._hash
|
|
104
|
+
}, this.tracer)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async currentBlockNumber(): Promise<XL1BlockNumber> {
|
|
108
|
+
return await spanRootAsync('currentBlockNumber', async () => {
|
|
109
|
+
const currentHead = assertEx(await this.getCurrentHead(), () => 'Could not find most recent block')
|
|
110
|
+
return asXL1BlockNumber(currentHead.block, { name: 'currentBlockNumber' })
|
|
111
|
+
}, this.tracer)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
protected async getCurrentHead() {
|
|
115
|
+
const chainArchivist = this.finalizedArchivist
|
|
116
|
+
return await findMostRecentBlock(chainArchivist)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
protected getHydratedBlockCache(): HydratedCache<SignedHydratedBlockWithHashMeta> {
|
|
120
|
+
if (this._signedHydratedBlockCache) return this._signedHydratedBlockCache
|
|
121
|
+
const chainMap = this.context.store.chainMap
|
|
122
|
+
this._signedHydratedBlockCache = new HydratedCache<SignedHydratedBlockWithHashMeta>(chainMap, async (
|
|
123
|
+
{ chainMap }: ChainStoreRead,
|
|
124
|
+
hash: Hash,
|
|
125
|
+
maxDepth?: number,
|
|
126
|
+
minDepth?: number,
|
|
127
|
+
) => {
|
|
128
|
+
const result = await hydrateBlock({ chainMap }, hash, maxDepth, minDepth)
|
|
129
|
+
return asSignedHydratedBlockWithHashMeta(result, true)
|
|
130
|
+
}, 200)
|
|
131
|
+
return this._signedHydratedBlockCache
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './SimpleBlockViewer.ts'
|
package/src/simple/index.ts
CHANGED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { Hash } from '@xylabs/sdk-js'
|
|
2
|
+
import type { Payload, WithStorageMeta } from '@xyo-network/payload-model'
|
|
3
|
+
import { LRUCache } from 'lru-cache'
|
|
4
|
+
|
|
5
|
+
import type { PayloadMapRead } from '../model/index.ts'
|
|
6
|
+
|
|
7
|
+
export type HydrateFunction<T> = (
|
|
8
|
+
context: { chainMap: PayloadMapRead<WithStorageMeta<Payload>> },
|
|
9
|
+
hash: Hash,
|
|
10
|
+
) => Promise<T | undefined>
|
|
11
|
+
|
|
12
|
+
export class HydratedCache<T extends {}> {
|
|
13
|
+
protected readonly cache: LRUCache<Hash, T>
|
|
14
|
+
protected readonly chainMap: PayloadMapRead<WithStorageMeta<Payload>>
|
|
15
|
+
protected readonly hydrateFunction: HydrateFunction<T>
|
|
16
|
+
|
|
17
|
+
constructor(
|
|
18
|
+
chainMap: PayloadMapRead<WithStorageMeta<Payload>>,
|
|
19
|
+
hydrateFunction: HydrateFunction<T>,
|
|
20
|
+
maxSize: number = 200,
|
|
21
|
+
) {
|
|
22
|
+
this.chainMap = chainMap
|
|
23
|
+
this.hydrateFunction = hydrateFunction
|
|
24
|
+
this.cache = new LRUCache<Hash, T>({ max: maxSize })
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async get(hash: Hash): Promise<T | null> {
|
|
28
|
+
const existing = this.cache.get(hash)
|
|
29
|
+
if (existing !== undefined) return existing
|
|
30
|
+
const block = (await this.hydrateFunction({ chainMap: this.chainMap }, hash)) ?? null
|
|
31
|
+
if (block !== null) this.cache.set(hash, block)
|
|
32
|
+
return block
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
has(hash: Hash): boolean {
|
|
36
|
+
return this.cache.has(hash)
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/utils/index.ts
CHANGED
package/src/viewers/Block.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import type { Hash, Promisable } from '@xylabs/sdk-js'
|
|
2
2
|
import type { SignedHydratedBlockWithHashMeta, XL1BlockNumber } from '@xyo-network/xl1-protocol'
|
|
3
3
|
|
|
4
|
-
export interface
|
|
5
|
-
blockByHash(hash: Hash): Promisable<SignedHydratedBlockWithHashMeta | null>
|
|
6
|
-
blockByNumber(block: XL1BlockNumber): Promisable<SignedHydratedBlockWithHashMeta | null>
|
|
4
|
+
export interface BlockViewerMethods {
|
|
7
5
|
blocksByHash(hash: Hash, limit?: number): Promisable<SignedHydratedBlockWithHashMeta[]>
|
|
8
|
-
|
|
6
|
+
blocksByNumber(block: XL1BlockNumber, limit?: number): Promisable<SignedHydratedBlockWithHashMeta[]>
|
|
9
7
|
currentBlock(): Promisable<SignedHydratedBlockWithHashMeta>
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface BlockViewer extends BlockViewerMethods {
|
|
11
|
+
blockByHash(hash: Hash): Promisable<SignedHydratedBlockWithHashMeta | null>
|
|
12
|
+
blockByNumber(block: XL1BlockNumber): Promisable<SignedHydratedBlockWithHashMeta | null>
|
|
10
13
|
currentBlockHash(): Promisable<Hash>
|
|
11
14
|
currentBlockNumber(): Promisable<XL1BlockNumber>
|
|
12
15
|
}
|
|
13
|
-
|
|
14
|
-
export interface BlockViewer extends BlockViewierMethods {}
|
package/src/viewers/Chain.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { Address, Promisable } from '@xylabs/sdk-js'
|
|
2
2
|
import type { PositionId, StepIdentity } from '@xyo-network/xl1-protocol'
|
|
3
3
|
|
|
4
|
-
import type { Position } from '
|
|
5
|
-
import type { NetworkStakeStepRewardsViewer } from '
|
|
4
|
+
import type { Position } from '../model/index.ts'
|
|
5
|
+
import type { NetworkStakeStepRewardsViewer } from '../provider/index.ts'
|
|
6
6
|
|
|
7
7
|
export interface PagedPositionsOptions {
|
|
8
8
|
cursor?: PositionId
|
package/src/viewers/index.ts
CHANGED
|
@@ -9,6 +9,7 @@ export * from './Stake.ts'
|
|
|
9
9
|
export * from './StakeEvents.ts'
|
|
10
10
|
export * from './StakeTotals.ts'
|
|
11
11
|
export * from './StepStake.ts'
|
|
12
|
+
export * from './StepViewer.ts'
|
|
12
13
|
export * from './TimeSync.ts'
|
|
13
14
|
export * from './Transaction.ts'
|
|
14
15
|
export * from './TransferBalance.ts'
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"StepViewer.d.ts","sourceRoot":"","sources":["../../../../src/provider/viewer/StepViewer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AACzD,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AAEzE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAA;AACpD,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,yBAAyB,CAAA;AAE5E,MAAM,WAAW,qBAAqB;IACpC,MAAM,CAAC,EAAE,UAAU,CAAA;IACnB,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAED,MAAM,WAAW,mBAAmB;IAClC,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,MAAM,CAAA;IACf,SAAS,EAAE,MAAM,CAAA;CAClB;AAED,MAAM,WAAW,iBAAiB;IAGhC,aAAa,CAAC,IAAI,EAAE,YAAY,GAAG,UAAU,CAAC,MAAM,CAAC,CAAA;IAErD,SAAS,CAAC,IAAI,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,qBAAqB,GAAG,UAAU,CAAC,QAAQ,EAAE,CAAC,CAAA;IAGtF,UAAU,CAAC,IAAI,EAAE,YAAY,GAAG,UAAU,CAAC,MAAM,CAAC,CAAA;IAGlD,KAAK,CAAC,IAAI,EAAE,YAAY,GAAG,UAAU,CAAC,MAAM,CAAC,CAAA;IAG7C,WAAW,CAAC,IAAI,EAAE,YAAY,GAAG,UAAU,CAAC,MAAM,CAAC,CAAA;IAEnD,OAAO,CAAC,IAAI,EAAE,YAAY,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,UAAU,CAAC,OAAO,EAAE,CAAC,CAAA;IAGjF,MAAM,CAAC,IAAI,EAAE,YAAY,EAAE,UAAU,CAAC,EAAE,UAAU,GAAG,UAAU,CAAC,MAAM,CAAC,CAAA;CACxE;AAED,MAAM,WAAW,UAAW,SAAQ,iBAAiB;IACnD,OAAO,CAAC,EAAE,6BAA6B,CAAA;CACxC"}
|