Skip to content

Feat recover object mappings #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions services/object-mapping-indexer/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ export const config = {
process.env.NODE_ENV === 'production' ? 'info' : 'debug',
),
databaseUrl: env('DATABASE_URL'),
recoverObjectMappings: {
active: env('RECOVER_OBJECT_MAPPINGS', 'false'),
endpoint:
env('RECOVER_OBJECT_MAPPINGS', 'false') === 'true'
? env('RECOVER_OBJECT_MAPPINGS_ENDPOINT')
: undefined,
},
}
2 changes: 2 additions & 0 deletions services/object-mapping-indexer/src/listeners.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createObjectMappingListener } from './services/objectMappingListener/index.js'
import { createObjectMappingRetriever } from './services/objectMappingRetriever/index.js'

createObjectMappingListener().start()
createObjectMappingRetriever().start()
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,19 @@ const getByHash = async (hash: string) => {
return result.rows.at(0)
}

const getLastPieceIndex = async () => {
const db = await getDatabase()
const result = await db.query<DBObjectMapping>(
'SELECT MAX("pieceIndex") as "pieceIndex" FROM object_mappings',
)
return result.rows[0]?.pieceIndex ?? 0
}

export const objectMappingRepository = {
saveObjectMappings,
getByBlockNumber,
getLatestBlockNumber,
getByPieceIndex,
getByHash,
getLastPieceIndex,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { ObjectMappingIndexerRPCApi } from '@auto-files/rpc-apis'
import { config } from '../../config'
import { logger } from '../../drivers/logger'
import { objectMappingRepository } from '../../repositories/objectMapping'

export const createObjectMappingRetriever = () => {
if (
!config.recoverObjectMappings.active ||
!config.recoverObjectMappings.endpoint
) {
logger.warn('Recover object mappings are not active')
return
}

const start = async () => {
const pieceIndex = await objectMappingRepository.getLastPieceIndex()
await objectMappingListener.api.subscribe_recover_object_mappings({
pieceIndex,
})

objectMappingListener.onNotification('object_mapping_list', (params) => {
objectMappingRepository.saveObjectMappings(
params.map((e) => ({
hash: e[0],
pieceIndex: e[1],
pieceOffset: e[2],
// TODO: get block number
blockNumber: 0,
})),
)
})
}

const objectMappingListener = ObjectMappingIndexerRPCApi.createClient({
endpoint: config.recoverObjectMappings.endpoint,
callbacks: {
onOpen: start,
onReconnection: start,
},
})

return { start }
}