@xyo-network/xl1-protocol-sdk 1.26.1 → 1.26.3
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/block/primitives/blockFromBlockNumber.d.ts.map +1 -1
- package/dist/neutral/index.mjs +64 -59
- package/dist/neutral/index.mjs.map +1 -1
- package/dist/neutral/primitives/state/hydratedBlockByNumber.d.ts.map +1 -1
- package/dist/neutral/test/index.mjs +64 -59
- package/dist/neutral/test/index.mjs.map +1 -1
- package/package.json +10 -10
- package/src/block/primitives/blockFromBlockNumber.ts +45 -42
- package/src/primitives/state/hydratedBlockByNumber.ts +14 -12
- package/src/simple/block/SimpleBlockViewer.ts +4 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xyo-network/xl1-protocol-sdk",
|
|
3
|
-
"version": "1.26.
|
|
3
|
+
"version": "1.26.3",
|
|
4
4
|
"description": "XYO Layer One SDK Protocol",
|
|
5
5
|
"homepage": "https://xylabs.com",
|
|
6
6
|
"bugs": {
|
|
@@ -48,25 +48,25 @@
|
|
|
48
48
|
"!**/*.test.*"
|
|
49
49
|
],
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@xyo-network/xl1-protocol-lib": "~1.26.
|
|
52
|
-
"@xyo-network/xl1-schema": "~1.26.
|
|
51
|
+
"@xyo-network/xl1-protocol-lib": "~1.26.3",
|
|
52
|
+
"@xyo-network/xl1-schema": "~1.26.3",
|
|
53
53
|
"async-mutex": "~0.5.0",
|
|
54
54
|
"cosmiconfig": "^9.0.1",
|
|
55
55
|
"lru-cache": "^11.2.7"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
-
"@opentelemetry/api": "^1.9.
|
|
58
|
+
"@opentelemetry/api": "^1.9.1",
|
|
59
59
|
"@types/node": "^25.5.0",
|
|
60
|
-
"@xylabs/sdk-js": "^5.0.
|
|
61
|
-
"@xylabs/ts-scripts-yarn3": "~7.4.
|
|
62
|
-
"@xylabs/tsconfig": "~7.4.
|
|
63
|
-
"@xylabs/vitest-extended": "~5.0.
|
|
60
|
+
"@xylabs/sdk-js": "^5.0.87",
|
|
61
|
+
"@xylabs/ts-scripts-yarn3": "~7.4.27",
|
|
62
|
+
"@xylabs/tsconfig": "~7.4.27",
|
|
63
|
+
"@xylabs/vitest-extended": "~5.0.87",
|
|
64
64
|
"@xyo-network/sdk-js": "^5.3.16",
|
|
65
|
-
"@xyo-network/xl1-network-model": "~1.26.
|
|
65
|
+
"@xyo-network/xl1-network-model": "~1.26.3",
|
|
66
66
|
"axios": "^1.13.6",
|
|
67
67
|
"ethers": "^6.16.0",
|
|
68
68
|
"ky": "^1.14.3",
|
|
69
|
-
"mongodb": "~7.1.
|
|
69
|
+
"mongodb": "~7.1.1",
|
|
70
70
|
"tslib": "^2.8.1",
|
|
71
71
|
"typescript": "~5.9.3",
|
|
72
72
|
"vitest": "~4.1.1",
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
asHash, type Hash, isDefined,
|
|
3
|
+
spanAsync,
|
|
3
4
|
} from '@xylabs/sdk-js'
|
|
4
5
|
import { toSafeJsonString } from '@xylabs/sdk-js'
|
|
5
6
|
import type { Signed, WithHashMeta } from '@xyo-network/sdk-js'
|
|
@@ -17,50 +18,52 @@ export async function blockFromBlockNumber(
|
|
|
17
18
|
context: ChainContextRead,
|
|
18
19
|
blockNumber: XL1BlockNumber,
|
|
19
20
|
): Promise<WithHashMeta<Signed<BlockBoundWitness>>> {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
let currentBlock = asSignedBlockBoundWitnessWithStorageMeta(
|
|
28
|
-
result,
|
|
29
|
-
() => `Found Payload is not a Signed<BlockBoundWitness>: ${JSON.stringify(result, null, 2)}`,
|
|
30
|
-
)
|
|
31
|
-
if (currentBlock.block < blockNumber) {
|
|
32
|
-
throw new Error(`Block number ${blockNumber} is greater than head ${currentBlock.block}.`)
|
|
33
|
-
}
|
|
34
|
-
while (currentBlock.block > blockNumber) {
|
|
35
|
-
let jumpHash: Hash | null = currentBlock.previous
|
|
36
|
-
let jumpBlockNumber = currentBlock.block - 1
|
|
37
|
-
for (const [step, stepSize] of StepSizes.entries()) {
|
|
38
|
-
const possibleJumpBlockNumber = currentBlock.block - (currentBlock.block % stepSize) - 1
|
|
39
|
-
if (possibleJumpBlockNumber >= blockNumber && possibleJumpBlockNumber <= jumpBlockNumber) {
|
|
40
|
-
jumpBlockNumber = possibleJumpBlockNumber
|
|
41
|
-
jumpHash = asHash(currentBlock.step_hashes?.at(step), () => `Step hash not found for step ${step} in block ${currentBlock.block}`)
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
const [newBlock] = await chainMap.get([
|
|
45
|
-
asHash(jumpHash, () => `Jump hash not found for block number [${blockNumber}]: ${jumpBlockNumber} ${toSafeJsonString(currentBlock, 10)}`),
|
|
46
|
-
])
|
|
47
|
-
if (!isDefined(newBlock)) {
|
|
48
|
-
throw new Error(`Block not found for jump hash: ${jumpHash}`)
|
|
21
|
+
return await spanAsync('blockFromBlockNumber', async () => {
|
|
22
|
+
const cacheKey = `${blockNumber}`
|
|
23
|
+
const { chainMap, head } = context
|
|
24
|
+
return await withContextCacheResponse(context, 'blockFromBlockNumber', cacheKey, async () => {
|
|
25
|
+
const [result] = await chainMap.get([head._hash])
|
|
26
|
+
if (!isDefined(result)) {
|
|
27
|
+
throw new Error(`Head block not found for hash: ${head._hash}`)
|
|
49
28
|
}
|
|
50
|
-
currentBlock = asSignedBlockBoundWitnessWithStorageMeta(
|
|
51
|
-
|
|
52
|
-
() => {
|
|
53
|
-
const result = SignedBlockBoundWitnessWithHashMetaZod.safeParse(newBlock)
|
|
54
|
-
return `Found Payload [jump hash] is not a Signed<BlockBoundWitness>: ${result.error}`
|
|
55
|
-
},
|
|
29
|
+
let currentBlock = asSignedBlockBoundWitnessWithStorageMeta(
|
|
30
|
+
result,
|
|
31
|
+
() => `Found Payload is not a Signed<BlockBoundWitness>: ${JSON.stringify(result, null, 2)}`,
|
|
56
32
|
)
|
|
57
|
-
if (currentBlock.block === blockNumber) {
|
|
58
|
-
break
|
|
59
|
-
}
|
|
60
33
|
if (currentBlock.block < blockNumber) {
|
|
61
|
-
throw new Error(`Block number ${blockNumber} is
|
|
34
|
+
throw new Error(`Block number ${blockNumber} is greater than head ${currentBlock.block}.`)
|
|
35
|
+
}
|
|
36
|
+
while (currentBlock.block > blockNumber) {
|
|
37
|
+
let jumpHash: Hash | null = currentBlock.previous
|
|
38
|
+
let jumpBlockNumber = currentBlock.block - 1
|
|
39
|
+
for (const [step, stepSize] of StepSizes.entries()) {
|
|
40
|
+
const possibleJumpBlockNumber = currentBlock.block - (currentBlock.block % stepSize) - 1
|
|
41
|
+
if (possibleJumpBlockNumber >= blockNumber && possibleJumpBlockNumber <= jumpBlockNumber) {
|
|
42
|
+
jumpBlockNumber = possibleJumpBlockNumber
|
|
43
|
+
jumpHash = asHash(currentBlock.step_hashes?.at(step), () => `Step hash not found for step ${step} in block ${currentBlock.block}`)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const [newBlock] = await chainMap.get([
|
|
47
|
+
asHash(jumpHash, () => `Jump hash not found for block number [${blockNumber}]: ${jumpBlockNumber} ${toSafeJsonString(currentBlock, 10)}`),
|
|
48
|
+
])
|
|
49
|
+
if (!isDefined(newBlock)) {
|
|
50
|
+
throw new Error(`Block not found for jump hash: ${jumpHash}`)
|
|
51
|
+
}
|
|
52
|
+
currentBlock = asSignedBlockBoundWitnessWithStorageMeta(
|
|
53
|
+
newBlock,
|
|
54
|
+
() => {
|
|
55
|
+
const result = SignedBlockBoundWitnessWithHashMetaZod.safeParse(newBlock)
|
|
56
|
+
return `Found Payload [jump hash] is not a Signed<BlockBoundWitness>: ${result.error}`
|
|
57
|
+
},
|
|
58
|
+
)
|
|
59
|
+
if (currentBlock.block === blockNumber) {
|
|
60
|
+
break
|
|
61
|
+
}
|
|
62
|
+
if (currentBlock.block < blockNumber) {
|
|
63
|
+
throw new Error(`Block number ${blockNumber} is not a valid step block number for block ${head._hash}.`)
|
|
64
|
+
}
|
|
62
65
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
})
|
|
66
|
+
return currentBlock
|
|
67
|
+
})
|
|
68
|
+
}, { ...context, timeBudgetLimit: 500 })
|
|
66
69
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { assertEx } from '@xylabs/sdk-js'
|
|
1
|
+
import { assertEx, spanAsync } from '@xylabs/sdk-js'
|
|
2
2
|
import type {
|
|
3
3
|
ChainContextRead,
|
|
4
4
|
HydratedBlockWithHashMeta, XL1BlockNumber,
|
|
@@ -9,15 +9,17 @@ import { withContextCacheResponse } from '../../ChainContextHelpers.ts'
|
|
|
9
9
|
|
|
10
10
|
/** HydratedBlockByNumber helper function. */
|
|
11
11
|
export async function hydratedBlockByNumber(context: ChainContextRead, blockNumber: XL1BlockNumber): Promise<HydratedBlockWithHashMeta | null> {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
12
|
+
return await spanAsync('hydratedBlockByNumber', async () => {
|
|
13
|
+
if (blockNumber < 0) throw new Error(`Block number ${blockNumber} is less than 0`)
|
|
14
|
+
if (blockNumber > Number.MAX_SAFE_INTEGER) throw new Error(`Block number ${blockNumber} is greater than the maximum safe integer`)
|
|
15
|
+
if (blockNumber % 1 !== 0) throw new Error(`Block number ${blockNumber} is not an integer`)
|
|
16
|
+
const cacheKey = `${blockNumber}`
|
|
17
|
+
return await withContextCacheResponse(context, 'hydratedBlockByNumber', cacheKey, async () => {
|
|
18
|
+
const block = assertEx(
|
|
19
|
+
await blockFromBlockNumber(context, blockNumber),
|
|
20
|
+
() => `Could not find block for block number ${blockNumber}`,
|
|
21
|
+
)
|
|
22
|
+
return await hydrateBlock(context, block._hash)
|
|
23
|
+
}, { max: 20_000 })
|
|
24
|
+
}, { ...context, timeBudgetLimit: 500 })
|
|
23
25
|
}
|
|
@@ -61,7 +61,7 @@ export class SimpleBlockViewer extends AbstractCreatableProvider<SimpleBlockView
|
|
|
61
61
|
) => {
|
|
62
62
|
const result = await hydrateBlock(context, hash, maxDepth, minDepth)
|
|
63
63
|
return asSignedHydratedBlockWithStorageMeta(result, true)
|
|
64
|
-
},
|
|
64
|
+
}, 20_000)
|
|
65
65
|
return this._signedHydratedBlockCache
|
|
66
66
|
}
|
|
67
67
|
|
|
@@ -90,7 +90,7 @@ export class SimpleBlockViewer extends AbstractCreatableProvider<SimpleBlockView
|
|
|
90
90
|
this.signedHydratedBlockWithDataLakePayloadsCache.set(hash, result)
|
|
91
91
|
}
|
|
92
92
|
return result
|
|
93
|
-
}, this.context)
|
|
93
|
+
}, { ...this.context, timeBudgetLimit: 100 })
|
|
94
94
|
}
|
|
95
95
|
|
|
96
96
|
async blockByNumber(blockNumber: XL1BlockNumber): Promise<SignedHydratedBlockWithHashMeta | null> {
|
|
@@ -102,7 +102,7 @@ export class SimpleBlockViewer extends AbstractCreatableProvider<SimpleBlockView
|
|
|
102
102
|
const block = asSignedHydratedBlockWithHashMeta(await hydratedBlockByNumber(await this.getChainContextRead(), blockNumber)) ?? null
|
|
103
103
|
const [result] = block ? await addDataLakePayloads(block, this.dataLakeViewer) : [null, []]
|
|
104
104
|
return result
|
|
105
|
-
}, this.context)
|
|
105
|
+
}, { ...this.context, timeBudgetLimit: 100 })
|
|
106
106
|
}
|
|
107
107
|
|
|
108
108
|
async blocksByHash(hash: Hash, limit = 50): Promise<SignedHydratedBlockWithHashMeta[]> {
|
|
@@ -118,7 +118,7 @@ export class SimpleBlockViewer extends AbstractCreatableProvider<SimpleBlockView
|
|
|
118
118
|
current = await this.blockByHash(previousHash)
|
|
119
119
|
}
|
|
120
120
|
return blocks.map(b => asSignedHydratedBlockWithHashMeta(b, true))
|
|
121
|
-
}, this.context)
|
|
121
|
+
}, { ...this.context, timeBudgetLimit: 300 })
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
async blocksByNumber(blockNumber: XL1BlockNumber, limit = 50): Promise<SignedHydratedBlockWithHashMeta[]> {
|