-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Sync assets using chain-sync protocol
Implements the Ogmios TypeScript client to follow the chain, removing the heavy-handed solution of diffing the projected data from `cardano-db-sync`. - Implements a persistent queue, backed by the PostgreSQL database - Replaces `DataSyncController` with `ChainFollower` and `Worker` - Asset table is no-longer dropped in the migration routine BREAKING CHANGE: Configuration of the asset metadata fetching is now a single value. `POLLING_INTERVAL_METADATA_SYNC_INITIAL` and `POLLING_INTERVAL_METADATA_SYNC_ONGOING` are replaced by 'ASSET_METADATA_UPDATE_INTERVAL', which is the number of seconds before the service checks the registry for updates.
- Loading branch information
Showing
46 changed files
with
660 additions
and
613 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[submodule "cardano-ogmios"] | ||
path = cardano-ogmios | ||
url = https://github.com/KtorZ/cardano-ogmios | ||
[submodule "ogmios"] | ||
path = ogmios | ||
url = https://github.com/CardanoSolutions/ogmios.git |
Submodule cardano-ogmios
deleted from
6096ae
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
export interface Signature { | ||
signature: string | ||
publicKey: string | ||
} | ||
|
||
export interface AssetMetadata { | ||
description?: { | ||
value: string | ||
anSignatures: Signature[] | ||
} | ||
logo?: { | ||
value: string | ||
anSignatures: Signature[] | ||
} | ||
name?: { | ||
value: string | ||
anSignatures: Signature[] | ||
} | ||
owner?: Signature | ||
preImage?: { | ||
value: string | ||
hashFn: string | ||
} | ||
subject: string | ||
ticker?: { | ||
value: string | ||
anSignatures: Signature[] | ||
} | ||
url?: { | ||
value: string | ||
anSignatures: Signature[] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import AssetFingerprint from '@emurgo/cip14-js' | ||
import { | ||
ChainSyncClient, | ||
createChainSyncClient, | ||
isMaryBlock, | ||
Schema | ||
} from '@cardano-ogmios/client' | ||
import { Config } from './Config' | ||
import { Asset } from './graphql_types' | ||
import { HasuraClient } from './HasuraClient' | ||
import PgBoss from 'pg-boss' | ||
import { dummyLogger, Logger } from 'ts-log' | ||
|
||
export const assetFingerprint = (policyId: Asset['policyId'], assetName?: Asset['assetName']) => | ||
new AssetFingerprint( | ||
Buffer.from(policyId, 'hex'), | ||
assetName !== undefined ? Buffer.from(assetName, 'hex') : undefined) | ||
.fingerprint() | ||
|
||
export class ChainFollower { | ||
private chainSyncClient: ChainSyncClient | ||
private queue: PgBoss | ||
|
||
constructor ( | ||
readonly hasuraClient: HasuraClient, | ||
private logger: Logger = dummyLogger, | ||
queueConfig: Config['db'] | ||
) { | ||
this.queue = new PgBoss({ | ||
application_name: 'cardano-graphql', | ||
...queueConfig | ||
}) | ||
} | ||
|
||
public async initialize (ogmiosConfig: Config['ogmios']) { | ||
this.logger.info({ module: 'ChainFollower' }, 'Initializing') | ||
this.chainSyncClient = await createChainSyncClient({ | ||
rollBackward: async ({ point, tip }, requestNext) => { | ||
if (point !== 'origin') { | ||
this.logger.info( | ||
{ module: 'ChainFollower', tip, rollbackPoint: point }, 'Rolling back' | ||
) | ||
const deleteResult = await this.hasuraClient.deleteAssetsAfterSlot(point.slot) | ||
this.logger.info({ module: 'ChainFollower' }, `Deleted ${deleteResult} assets`) | ||
} else { | ||
this.logger.info({ module: 'ChainFollower' }, 'Rolling back to genesis') | ||
const deleteResult = await this.hasuraClient.deleteAssetsAfterSlot(0) | ||
this.logger.info({ module: 'ChainFollower' }, `Deleted ${deleteResult} assets`) | ||
} | ||
requestNext() | ||
}, | ||
rollForward: async ({ block }, requestNext) => { | ||
let b: Schema.BlockMary | ||
if (isMaryBlock(block)) { | ||
b = block.mary as Schema.BlockMary | ||
} | ||
if (b !== undefined) { | ||
for (const tx of b.body) { | ||
for (const entry of Object.entries(tx.body.mint.assets)) { | ||
const [policyId, assetName] = entry[0].split('.') | ||
const assetId = policyId.concat(assetName) | ||
if (!(await this.hasuraClient.hasAsset(assetId))) { | ||
const asset = { | ||
assetId, | ||
assetName, | ||
firstAppearedInSlot: b.header.slot, | ||
fingerprint: assetFingerprint(policyId, assetName), | ||
policyId: `\\x${policyId}` | ||
} | ||
await this.hasuraClient.insertAssets([asset]) | ||
const SIX_HOURS = 21600 | ||
const THREE_MONTHS = 365 | ||
await this.queue.publish('asset-metadata-fetch-initial', { assetId }, { | ||
retryDelay: SIX_HOURS, | ||
retryLimit: THREE_MONTHS | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
requestNext() | ||
} | ||
}, { connection: ogmiosConfig }) | ||
this.logger.info({ module: 'ChainFollower' }, 'Initialized') | ||
} | ||
|
||
public async start (points: Schema.Point[]) { | ||
this.logger.info({ module: 'ChainFollower' }, 'Starting') | ||
await this.queue.start() | ||
await this.chainSyncClient.startSync(points) | ||
} | ||
|
||
public async shutdown () { | ||
this.logger.info({ module: 'ChainFollower' }, 'Shutting down') | ||
await this.chainSyncClient.shutdown() | ||
await this.queue.stop() | ||
this.logger.info( | ||
{ module: 'ChainFollower' }, | ||
'Shutdown complete') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.