@subsquid/batch-processor 1.0.0-portal-api.721f49 → 1.0.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/lib/database.d.ts +25 -8
- package/lib/database.d.ts.map +1 -1
- package/lib/run.d.ts +25 -11
- package/lib/run.d.ts.map +1 -1
- package/lib/run.js +183 -149
- package/lib/run.js.map +1 -1
- package/lib/util.d.ts +2 -2
- package/lib/util.d.ts.map +1 -1
- package/lib/util.js +1 -1
- package/lib/util.js.map +1 -1
- package/package.json +12 -9
- package/src/database.ts +28 -13
- package/src/find-rollback-index.test.ts +85 -0
- package/src/run.ts +219 -152
- package/src/test/processor.test.ts +651 -0
- package/src/util.ts +3 -3
- package/lib/metrics.d.ts +0 -21
- package/lib/metrics.d.ts.map +0 -1
- package/lib/metrics.js +0 -92
- package/lib/metrics.js.map +0 -1
- package/src/metrics.ts +0 -111
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@subsquid/batch-processor",
|
|
3
|
-
"version": "1.0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "ETL processor",
|
|
5
5
|
"license": "GPL-3.0-or-later",
|
|
6
|
-
"repository": "git@github.com:subsquid/squid.git",
|
|
6
|
+
"repository": "git@github.com:subsquid/squid-sdk.git",
|
|
7
7
|
"publishConfig": {
|
|
8
8
|
"access": "public",
|
|
9
9
|
"registry": "https://registry.npmjs.org/"
|
|
@@ -14,19 +14,22 @@
|
|
|
14
14
|
],
|
|
15
15
|
"main": "lib/index.js",
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@subsquid/logger": "^1.
|
|
18
|
-
"@subsquid/util-internal": "^3.
|
|
17
|
+
"@subsquid/logger": "^1.6.0",
|
|
18
|
+
"@subsquid/util-internal": "^3.3.0",
|
|
19
|
+
"@subsquid/util-internal-processor-tools": "^4.4.0",
|
|
19
20
|
"@subsquid/util-internal-counters": "^1.3.2",
|
|
20
21
|
"@subsquid/util-internal-prometheus-server": "^1.3.0",
|
|
21
22
|
"@subsquid/util-internal-range": "^0.3.0",
|
|
22
|
-
"
|
|
23
|
-
"@subsquid/util-internal-data-source": "0.0.1-portal-api.721f49"
|
|
23
|
+
"@subsquid/util-internal-data-source": "^0.0.0"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@
|
|
27
|
-
"
|
|
26
|
+
"@subsquid/util-internal-testing": "^0.0.0",
|
|
27
|
+
"@types/node": "^24.0.0",
|
|
28
|
+
"typescript": "5.5.4",
|
|
29
|
+
"vitest": "4.1.5"
|
|
28
30
|
},
|
|
29
31
|
"scripts": {
|
|
30
|
-
"build": "rm -rf lib && tsc"
|
|
32
|
+
"build": "rm -rf lib && tsc",
|
|
33
|
+
"test": "vitest --run"
|
|
31
34
|
}
|
|
32
35
|
}
|
package/src/database.ts
CHANGED
|
@@ -4,24 +4,27 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export type Database<S> = FinalDatabase<S> | HotDatabase<S>
|
|
6
6
|
|
|
7
|
-
|
|
8
7
|
export interface FinalTxInfo {
|
|
9
8
|
prevHead: HashAndHeight
|
|
10
9
|
nextHead: HashAndHeight
|
|
11
10
|
isOnTop: boolean
|
|
12
11
|
}
|
|
13
12
|
|
|
14
|
-
|
|
15
13
|
export interface FinalDatabase<S> {
|
|
16
14
|
supportsHotBlocks?: false
|
|
17
15
|
connect(): Promise<FinalDatabaseState>
|
|
18
|
-
|
|
19
|
-
transact(info: FinalTxInfo, cb: (store: S) => Promise<void>): Promise<void>
|
|
16
|
+
transact(info: FinalTxInfo, cb: (store: S) => Promise<DatabaseTransactResult | void>): Promise<void>
|
|
20
17
|
}
|
|
21
18
|
|
|
19
|
+
export interface DatabaseTransactResult {
|
|
20
|
+
templates?: TemplateMutation[]
|
|
21
|
+
}
|
|
22
22
|
|
|
23
|
-
export interface FinalDatabaseState
|
|
24
|
-
|
|
23
|
+
export interface FinalDatabaseState {
|
|
24
|
+
height: number
|
|
25
|
+
hash: string
|
|
26
|
+
templates?: TemplateMutation[]
|
|
27
|
+
}
|
|
25
28
|
|
|
26
29
|
export interface HotTxInfo {
|
|
27
30
|
finalizedHead: HashAndHeight
|
|
@@ -29,26 +32,38 @@ export interface HotTxInfo {
|
|
|
29
32
|
newBlocks: HashAndHeight[]
|
|
30
33
|
}
|
|
31
34
|
|
|
32
|
-
|
|
33
35
|
export interface HotDatabase<S> {
|
|
34
36
|
supportsHotBlocks: true
|
|
35
37
|
connect(): Promise<HotDatabaseState>
|
|
36
|
-
|
|
37
|
-
transact(info: FinalTxInfo, cb: (store: S) => Promise<void>): Promise<void>
|
|
38
|
+
transact(info: FinalTxInfo, cb: (store: S) => Promise<DatabaseTransactResult | void>): Promise<void>
|
|
38
39
|
|
|
39
40
|
transactHot2(
|
|
40
41
|
info: HotTxInfo,
|
|
41
|
-
cb: (store: S, blockSliceStart: number, blockSliceEnd: number) => Promise<void
|
|
42
|
+
cb: (store: S, blockSliceStart: number, blockSliceEnd: number) => Promise<DatabaseTransactResult | void>,
|
|
42
43
|
): Promise<void>
|
|
43
44
|
}
|
|
44
45
|
|
|
46
|
+
export interface HotBlock extends HashAndHeight {
|
|
47
|
+
templates?: TemplateMutation[]
|
|
48
|
+
}
|
|
45
49
|
|
|
46
50
|
export interface HotDatabaseState extends HashAndHeight {
|
|
47
|
-
top:
|
|
51
|
+
top: HotBlock[]
|
|
52
|
+
templates?: TemplateMutation[]
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface FinalDatabaseState extends HashAndHeight {
|
|
56
|
+
templates?: TemplateMutation[]
|
|
48
57
|
}
|
|
49
58
|
|
|
59
|
+
export interface TemplateMutation {
|
|
60
|
+
type: 'add' | 'delete'
|
|
61
|
+
key: string
|
|
62
|
+
value: string
|
|
63
|
+
blockNumber: number
|
|
64
|
+
}
|
|
50
65
|
|
|
51
66
|
export interface HashAndHeight {
|
|
52
|
-
height: number
|
|
53
67
|
hash: string
|
|
54
|
-
|
|
68
|
+
height: number
|
|
69
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type {BlockRef} from '@subsquid/util-internal-data-source'
|
|
2
|
+
import {describe, expect, it} from 'vitest'
|
|
3
|
+
import {findRollbackIndex} from './run'
|
|
4
|
+
|
|
5
|
+
function ref(number: number, hash: string): BlockRef {
|
|
6
|
+
return {number, hash}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
describe('findRollbackIndex', () => {
|
|
10
|
+
it('returns last index when chains are identical', () => {
|
|
11
|
+
const chain: BlockRef[] = [ref(0, '0x0'), ref(1, '0x1'), ref(2, '0x2')]
|
|
12
|
+
expect(findRollbackIndex(chain, chain)).toBe(2)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('returns the last common index when chains diverge mid-way', () => {
|
|
16
|
+
const current: BlockRef[] = [ref(0, '0x0'), ref(1, '0x1'), ref(2, '0x2'), ref(3, '0x3')]
|
|
17
|
+
const fork: BlockRef[] = [ref(0, '0x0'), ref(1, '0x1'), ref(2, '0x2alt'), ref(3, '0x3alt')]
|
|
18
|
+
// Common prefix is [0x0, 0x1]; divergence at index 2 (hash mismatch
|
|
19
|
+
// at number=2). Rollback target = last common index = 1.
|
|
20
|
+
expect(findRollbackIndex(current, fork)).toBe(1)
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('returns -1 when the two chains share no blocks at all', () => {
|
|
24
|
+
const current: BlockRef[] = [ref(0, '0xA0'), ref(1, '0xA1')]
|
|
25
|
+
const fork: BlockRef[] = [ref(0, '0xB0'), ref(1, '0xB1')]
|
|
26
|
+
// Same numbers, different hashes at index 0. No common prefix exists.
|
|
27
|
+
expect(findRollbackIndex(current, fork)).toBe(-1)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('returns -1 when the current chain is empty', () => {
|
|
31
|
+
const fork: BlockRef[] = [ref(0, '0x0'), ref(1, '0x1')]
|
|
32
|
+
expect(findRollbackIndex([], fork)).toBe(-1)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('returns -1 when the fork chain is empty', () => {
|
|
36
|
+
const current: BlockRef[] = [ref(0, '0x0'), ref(1, '0x1')]
|
|
37
|
+
expect(findRollbackIndex(current, [])).toBe(-1)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('skips non-overlapping block numbers on either side and finds the common suffix', () => {
|
|
41
|
+
// current has 0..5, fork has 3..7 — only numbers 3, 4, 5 overlap.
|
|
42
|
+
const current: BlockRef[] = [
|
|
43
|
+
ref(0, '0x0'),
|
|
44
|
+
ref(1, '0x1'),
|
|
45
|
+
ref(2, '0x2'),
|
|
46
|
+
ref(3, '0x3'),
|
|
47
|
+
ref(4, '0x4'),
|
|
48
|
+
ref(5, '0x5'),
|
|
49
|
+
]
|
|
50
|
+
const fork: BlockRef[] = [
|
|
51
|
+
ref(3, '0x3'),
|
|
52
|
+
ref(4, '0x4'),
|
|
53
|
+
ref(5, '0x5-alt'), // divergence starts here
|
|
54
|
+
ref(6, '0x6-alt'),
|
|
55
|
+
ref(7, '0x7-alt'),
|
|
56
|
+
]
|
|
57
|
+
// Common prefix inside overlap: 3, 4 match. Last common index in
|
|
58
|
+
// currentChain = 4 (block #4 has hash '0x4').
|
|
59
|
+
expect(findRollbackIndex(current, fork)).toBe(4)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it('returns -1 when all fork numbers are strictly above current chain range', () => {
|
|
63
|
+
const current: BlockRef[] = [ref(0, '0x0'), ref(1, '0x1')]
|
|
64
|
+
const fork: BlockRef[] = [ref(10, '0xA'), ref(11, '0xB')]
|
|
65
|
+
expect(findRollbackIndex(current, fork)).toBe(-1)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('returns -1 when all fork numbers are strictly below current chain range', () => {
|
|
69
|
+
const current: BlockRef[] = [ref(10, '0xA'), ref(11, '0xB')]
|
|
70
|
+
const fork: BlockRef[] = [ref(0, '0x0'), ref(1, '0x1')]
|
|
71
|
+
expect(findRollbackIndex(current, fork)).toBe(-1)
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('returns the last match when the fork is a strict prefix of the current chain', () => {
|
|
75
|
+
const current: BlockRef[] = [ref(0, '0x0'), ref(1, '0x1'), ref(2, '0x2'), ref(3, '0x3')]
|
|
76
|
+
const fork: BlockRef[] = [ref(0, '0x0'), ref(1, '0x1')]
|
|
77
|
+
expect(findRollbackIndex(current, fork)).toBe(1)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('returns the last match when the current chain is a strict prefix of the fork', () => {
|
|
81
|
+
const current: BlockRef[] = [ref(0, '0x0'), ref(1, '0x1')]
|
|
82
|
+
const fork: BlockRef[] = [ref(0, '0x0'), ref(1, '0x1'), ref(2, '0x2'), ref(3, '0x3')]
|
|
83
|
+
expect(findRollbackIndex(current, fork)).toBe(1)
|
|
84
|
+
})
|
|
85
|
+
})
|