Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | 40x 40x 40x 40x 40x 40x 40x 40x 2x 2x 40x 2x 2x 40x 2x 2x 2x 2x 2x 8x 2x 2x 2x 2x 54x 2x 14x 3x 2x | /* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable prefer-spread */
import {
Bootstrap,
ObservableCardanoNode,
ProjectionEvent,
logProjectionProgress,
requestNext
} from '@cardano-sdk/projection';
import { Cardano } from '@cardano-sdk/core';
import { Logger } from 'ts-log';
import { Observable, concat, defer, groupBy, mergeMap, take, takeWhile } from 'rxjs';
import {
PgConnectionConfig,
TypeormDevOptions,
TypeormOptions,
TypeormStabilityWindowBuffer,
WithTypeormContext,
createObservableConnection,
createTypeormTipTracker,
isRecoverableTypeormError,
typeormTransactionCommit,
withTypeormTransaction
} from '@cardano-sdk/projection-typeorm';
import {
PreparedProjection,
ProjectionName,
ProjectionOptions,
prepareTypeormProjection
} from './prepareTypeormProjection';
import { ReconnectionConfig, passthrough, shareRetryBackoff, toEmpty } from '@cardano-sdk/util-rxjs';
import { migrations } from './migrations';
const reconnectionConfig: ReconnectionConfig = {
initialInterval: 50,
maxInterval: 5000
};
export interface CreateTypeormProjectionProps {
projections: ProjectionName[];
blocksBufferLength: number;
connectionConfig$: Observable<PgConnectionConfig>;
devOptions?: TypeormDevOptions;
exitAtBlockNo?: Cardano.BlockNo;
logger: Logger;
cardanoNode: ObservableCardanoNode;
projectionOptions?: ProjectionOptions;
}
const applyMappers =
<T = {}>(selectedMappers: PreparedProjection['mappers']) =>
(evt$: Observable<ProjectionEvent>) =>
evt$.pipe.apply(evt$, selectedMappers as any) as Observable<ProjectionEvent<T>>;
const applyStores =
<T extends WithTypeormContext>(selectedStores: PreparedProjection['stores']) =>
(evt$: Observable<T>) =>
evt$.pipe.apply(evt$, selectedStores as any) as Observable<T>;
/**
* Creates a projection observable that applies a sequence of operators
* required to project requested `projections` into a postgres database.
*
* Uses TypeORM entities and operators defined in 'projection-typeorm' package.
* Dependencies of each projection are defined in ./prepareTypeormProjection.ts
*/
export const createTypeormProjection = ({
blocksBufferLength,
projections,
connectionConfig$,
logger,
devOptions: requestedDevOptions,
cardanoNode,
exitAtBlockNo,
projectionOptions
}: CreateTypeormProjectionProps) => {
const { handlePolicyIds } = { handlePolicyIds: [], ...projectionOptions };
logger.debug(`Creating projection with policyIds ${JSON.stringify(handlePolicyIds)}`);
logger.debug(`Using a ${blocksBufferLength} blocks buffer`);
const { mappers, entities, stores, extensions, willStore } = prepareTypeormProjection(
{
options: projectionOptions,
projections
},
{ logger }
);
const connect = (options?: TypeormOptions, devOptions?: TypeormDevOptions) =>
createObservableConnection({
connectionConfig$,
devOptions,
entities,
extensions,
logger,
options
});
const tipTracker = createTypeormTipTracker({
connection$: connect(),
reconnectionConfig
});
const buffer = new TypeormStabilityWindowBuffer({
connection$: connect(),
logger,
reconnectionConfig
});
const projectionSource$ = Bootstrap.fromCardanoNode({
blocksBufferLength,
buffer,
cardanoNode,
logger,
projectedTip$: tipTracker.tip$
});
return concat(
// initialize database before starting the projector
connect(
{
installExtensions: true,
migrations: migrations.filter(({ entity }) => entities.includes(entity as any)),
migrationsRun: !requestedDevOptions?.synchronize
},
requestedDevOptions
).pipe(take(1), toEmpty),
defer(() =>
projectionSource$.pipe(
applyMappers(mappers),
// if there are any relevant data to write into db
groupBy((evt) => willStore(evt)),
mergeMap((group$) =>
group$.key
? group$.pipe(
shareRetryBackoff(
(evt$) =>
evt$.pipe(
withTypeormTransaction({ connection$: connect() }),
applyStores(stores),
buffer.storeBlockData(),
typeormTransactionCommit()
),
{ shouldRetry: isRecoverableTypeormError }
)
)
: group$
),
tipTracker.trackProjectedTip(),
requestNext(),
logProjectionProgress(logger),
exitAtBlockNo ? takeWhile((event) => event.block.header.blockNo < exitAtBlockNo) : passthrough()
)
)
);
};
|