Skip to content

Commit bb6d3e6

Browse files
authored
Merge pull request #84 from autonomys/add-step-size
Add step size in object mapping recovery
2 parents 8c663d5 + 0f8ab26 commit bb6d3e6

File tree

6 files changed

+39
-6
lines changed

6 files changed

+39
-6
lines changed

common/rpc-apis/src/objectMappingIndexer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const ObjectMappingIndexerRPCApi = createApiDefinition({
2121
subscribe_recover_object_mappings: {
2222
params: z.object({
2323
pieceIndex: z.number(),
24+
step: z.number().optional(),
2425
}),
2526
returns: z.object({
2627
subscriptionId: z.string(),

services/object-mapping-indexer/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const config = {
77
corsAllowOrigins: env('CORS_ALLOW_ORIGINS', ''),
88
nodeRpcUrl: env('NODE_RPC_URL'),
99
recoveryInterval: notNaN(Number(env('RECOVERY_INTERVAL', '1000'))),
10+
maxRecoveryStep: notNaN(Number(env('MAX_RECOVERY_STEP', '1000'))),
1011
logLevel: env(
1112
'LOG_LEVEL',
1213
process.env.NODE_ENV === 'production' ? 'info' : 'debug',

services/object-mapping-indexer/src/repositories/objectMapping.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,22 @@ const getByHash = async (hash: string) => {
6363
return result.rows.at(0)
6464
}
6565

66+
const getByPieceIndexRange = async (min: number, max: number) => {
67+
const db = await getDatabase()
68+
69+
const result = await db.query<DBObjectMapping>(
70+
'SELECT * FROM object_mappings WHERE "pieceIndex" >= $1 AND "pieceIndex" < $2',
71+
[min, max],
72+
)
73+
74+
return result.rows
75+
}
76+
6677
export const objectMappingRepository = {
6778
saveObjectMappings,
6879
getByBlockNumber,
6980
getLatestBlockNumber,
7081
getByPieceIndex,
7182
getByHash,
83+
getByPieceIndexRange,
7284
}

services/object-mapping-indexer/src/rpc/server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ const createObjectMappingsRPCServer = (app: Application) => {
2323
}
2424
},
2525
subscribe_recover_object_mappings: async (
26-
{ pieceIndex },
26+
{ pieceIndex, step },
2727
{ connection },
2828
) => {
2929
const subscriptionId =
3030
objectMappingRouter.subscribeRecoverObjectMappings(
3131
connection,
3232
pieceIndex,
33+
step,
3334
)
3435

3536
return { success: true, subscriptionId }

services/object-mapping-indexer/src/services/objectMappingRouter/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type RouterState = {
1111
objectMappingsSubscriptions: Map<string, Websocket.connection>
1212
recoverObjectMappingsSubscriptions: Map<
1313
string,
14-
{ connection: Websocket.connection; pieceIndex: number }
14+
{ connection: Websocket.connection; pieceIndex: number; step: number }
1515
>
1616
lastRealtimeBlockNumber: number
1717
}
@@ -71,6 +71,7 @@ const emitObjectMappings = (event: ObjectMappingListEntry) => {
7171
const subscribeRecoverObjectMappings = (
7272
connection: Websocket.connection,
7373
startingPieceIndex: number,
74+
step: number = 1,
7475
) => {
7576
logger.info(
7677
`IP (${connection.remoteAddress}) subscribing to recover object mappings`,
@@ -80,6 +81,7 @@ const subscribeRecoverObjectMappings = (
8081
state.recoverObjectMappingsSubscriptions.set(subscriptionId, {
8182
connection,
8283
pieceIndex,
84+
step,
8385
})
8486

8587
return subscriptionId
@@ -104,13 +106,16 @@ const emitRecoverObjectMappings = async () => {
104106
)
105107

106108
const promises = recovering.map(
107-
async ([subscriptionId, { connection, pieceIndex }]) => {
108-
const result =
109-
await objectMappingUseCase.getObjectByPieceIndex(pieceIndex)
109+
async ([subscriptionId, { connection, pieceIndex, step }]) => {
110+
const result = await objectMappingUseCase.getObjectByPieceIndexAndStep(
111+
pieceIndex,
112+
step,
113+
)
110114

111115
state.recoverObjectMappingsSubscriptions.set(subscriptionId, {
112116
connection,
113-
pieceIndex: pieceIndex + 1,
117+
pieceIndex: pieceIndex + step,
118+
step,
114119
})
115120

116121
if (pieceIndex >= state.lastRealtimeBlockNumber) {

services/object-mapping-indexer/src/useCases/objectMapping.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ const getObjectByPieceIndex = async (
5050
return objectMappings.map((e) => [e.hash, e.pieceIndex, e.pieceOffset])
5151
}
5252

53+
const getObjectByPieceIndexAndStep = async (
54+
pieceIndex: number,
55+
step: number,
56+
): Promise<ObjectMapping[]> => {
57+
const objectMappings = await objectMappingRepository.getByPieceIndexRange(
58+
pieceIndex,
59+
pieceIndex + step,
60+
)
61+
62+
return objectMappings.map((e) => [e.hash, e.pieceIndex, e.pieceOffset])
63+
}
64+
5365
const getObjectByBlock = async (
5466
blockNumber: number,
5567
): Promise<ObjectMapping[]> => {
@@ -64,4 +76,5 @@ export const objectMappingUseCase = {
6476
getObject,
6577
getObjectByPieceIndex,
6678
getObjectByBlock,
79+
getObjectByPieceIndexAndStep,
6780
}

0 commit comments

Comments
 (0)