Skip to content

Commit ef78f76

Browse files
author
andraz maier
committed
Feat: add project versioning api point
1 parent bb3c7a6 commit ef78f76

File tree

11 files changed

+141
-42
lines changed

11 files changed

+141
-42
lines changed

Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ EXPOSE 3000
1212

1313
USER node
1414

15+
RUN git describe --tags --always > PROJECT_VERSION && \
16+
date +%s > PROJECT_BUILD_DATE && \
17+
git rev-parse HEAD > PROJECT_COMMIT_HASH && \
18+
rm -rf .git
19+
1520
ENV PATH="${PATH}:/app/verifier-indexer-api/docker/scripts"
1621
ENV NODE_ENV=production
1722

src/controllers/health.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
handleApiResponse,
1313
} from '../utils/api-models/ApiResponse';
1414
import { ApiResponseWrapperDec } from '../utils/open-api-utils';
15-
import { ApiDBVersion } from 'src/dtos/indexer/ApiDbVersion.dto';
15+
import { ApiDBVersion } from '../dtos/indexer/ApiDbVersion.dto';
1616

1717
abstract class BaseHealthController {
1818
protected abstract indexerEngine: IIndexerEngineService;
Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
/**
2-
* State table entry of the indexer database
2+
* Project version entry in the database
33
*/
4-
export class ApiDBVersion {
5-
/**
6-
* Version of connected underlying node
7-
*/
8-
nodeVersion: string;
9-
4+
export class Version {
105
/**
11-
* Git tag of the indexer client
6+
* Git tag of the project
127
*/
138
gitTag: string;
149

1510
/**
16-
* Git hash of the indexer client
11+
* Git hash of the project
1712
*/
1813
gitHash: string;
1914

2015
/**
21-
* Build date of the indexer client
16+
* Build date of the project
2217
*/
2318
buildDate: number;
19+
}
2420

21+
/**
22+
* Indexer version entry in the database
23+
*/
24+
export class IndexerVersion extends Version {
2525
/**
2626
* Number of confirmations setting for indexer client
2727
*/
@@ -30,5 +30,25 @@ export class ApiDBVersion {
3030
/**
3131
* History drop setting for indexer client
3232
*/
33-
historySeconds;
33+
historySeconds: number;
34+
}
35+
36+
/**
37+
* All version entries in the database
38+
*/
39+
export class ApiDBVersion {
40+
/**
41+
* Version of connected indexer
42+
*/
43+
indexer: IndexerVersion;
44+
45+
/**
46+
* Version of api server
47+
*/
48+
apiServer: Version;
49+
50+
/**
51+
* Version of connected underlying node
52+
*/
53+
nodeVersion: string;
3454
}

src/entity/utxo-entity-definitions.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@ import {
55
IUtxoVinTransactionPrevout,
66
IUtxoVoutTransaction,
77
} from '@flarenetwork/mcc';
8-
import { ApiDBBlock } from '../dtos/indexer/ApiDbBlock.dto';
9-
import { ApiDBTransaction } from '../dtos/indexer/ApiDbTransaction.dto';
10-
import {
11-
BlockResult,
12-
TransactionResult,
13-
} from '../indexed-query-manager/indexed-query-manager-types';
148
import {
159
Column,
1610
Entity,
@@ -19,7 +13,13 @@ import {
1913
OneToMany,
2014
PrimaryColumn,
2115
} from 'typeorm';
22-
import { ApiDBVersion } from 'src/dtos/indexer/ApiDbVersion.dto';
16+
import { ApiDBBlock } from '../dtos/indexer/ApiDbBlock.dto';
17+
import { ApiDBTransaction } from '../dtos/indexer/ApiDbTransaction.dto';
18+
import { ApiDBVersion, IndexerVersion } from '../dtos/indexer/ApiDbVersion.dto';
19+
import {
20+
BlockResult,
21+
TransactionResult,
22+
} from '../indexed-query-manager/indexed-query-manager-types';
2323

2424
// External Postgres Database Entities (Utxo (BTC and DOGE)) (read only)
2525

@@ -378,14 +378,18 @@ export class DBIndexerVersion {
378378
history_seconds: number;
379379

380380
toApiDBVersion(): ApiDBVersion {
381-
return {
382-
nodeVersion: this.node_version,
381+
const indexerVersion: IndexerVersion = {
383382
gitTag: this.git_tag,
384383
gitHash: this.git_hash,
385384
buildDate: this.build_date,
386385
numConfirmations: this.num_confirmations,
387386
historySeconds: this.history_seconds,
388387
};
388+
return {
389+
nodeVersion: this.node_version,
390+
indexer: indexerVersion,
391+
apiServer: null,
392+
};
389393
}
390394
}
391395

src/entity/xrp-entity-definitions.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
TransactionResult,
99
} from '../indexed-query-manager/indexed-query-manager-types';
1010
import { Column, Entity, PrimaryColumn } from 'typeorm';
11-
import { ApiDBVersion } from 'src/dtos/indexer/ApiDbVersion.dto';
11+
import { ApiDBVersion, IndexerVersion } from '../dtos/indexer/ApiDbVersion.dto';
1212

1313
@Entity('blocks')
1414
export class DBXrpIndexerBlock {
@@ -170,7 +170,6 @@ export class DBXrpState {
170170

171171
export type IDBXrpState = new () => DBXrpState;
172172

173-
174173
@Entity('versions')
175174
export class DBXrpIndexerVersion {
176175
@PrimaryColumn({ type: 'bigint' })
@@ -195,15 +194,19 @@ export class DBXrpIndexerVersion {
195194
history_seconds: number;
196195

197196
toApiDBVersion(): ApiDBVersion {
198-
return {
199-
nodeVersion: this.node_version,
197+
const indexerVersion: IndexerVersion = {
200198
gitTag: this.git_tag,
201199
gitHash: this.git_hash,
202200
buildDate: this.build_date,
203201
numConfirmations: this.num_confirmations,
204202
historySeconds: this.history_seconds,
205-
}
203+
};
204+
return {
205+
nodeVersion: this.node_version,
206+
indexer: indexerVersion,
207+
apiServer: null,
208+
};
206209
}
207210
}
208211

209-
export type IDBXrpIndexerVersion = new () => DBXrpIndexerVersion;
212+
export type IDBXrpIndexerVersion = new () => DBXrpIndexerVersion;

src/services/common/base-indexer-engine-service.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { ApiDBVersion } from 'src/dtos/indexer/ApiDbVersion.dto';
1+
import { readFile } from 'fs/promises';
2+
import { join } from 'path';
3+
import { ApiDBVersion } from '../../dtos/indexer/ApiDbVersion.dto';
24
import { ApiDBBlock } from '../../dtos/indexer/ApiDbBlock.dto';
35
import { ApiDBState } from '../../dtos/indexer/ApiDbState.dto';
46
import { ApiDBTransaction } from '../../dtos/indexer/ApiDbTransaction.dto';
@@ -10,7 +12,21 @@ import { PaginatedList } from '../../utils/api-models/PaginatedList';
1012
export abstract class IIndexerEngineService {
1113
public abstract getStateSetting(): Promise<ApiDBState | null>;
1214

13-
public abstract getIndexerServiceVersion(): Promise<ApiDBVersion>
15+
public abstract getIndexerServiceVersion(): Promise<ApiDBVersion>;
16+
17+
/**
18+
* Reads the version file from the file system.
19+
*/
20+
public static async readVersionFile(filePath: string): Promise<string> {
21+
return readFile(join(__dirname, filePath), 'utf-8')
22+
.then((data) => data)
23+
.catch((error) => {
24+
if (error.code === 'ENOENT') {
25+
return null;
26+
}
27+
throw error;
28+
});
29+
}
1430

1531
/**
1632
* Gets the range of available confirmed blocks in the indexer database.
@@ -45,7 +61,9 @@ export abstract class IIndexerEngineService {
4561
blockNumber: number,
4662
): Promise<ApiDBBlock | null>;
4763

48-
public abstract listBlock(props: QueryBlock): Promise<PaginatedList<ApiDBBlock>>;
64+
public abstract listBlock(
65+
props: QueryBlock,
66+
): Promise<PaginatedList<ApiDBBlock>>;
4967

5068
public abstract getBlock(blockHash: string): Promise<ApiDBBlock>;
5169

src/services/indexer-services/utxo-indexer.service.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
import { ChainType, unPrefix0x } from '@flarenetwork/mcc';
22
import { Injectable } from '@nestjs/common';
3+
import { Version } from '../../dtos/indexer/ApiDbVersion.dto';
34

45
import { EntityManager, SelectQueryBuilder } from 'typeorm';
56

67
import { ApiDBBlock } from '../../dtos/indexer/ApiDbBlock.dto';
78
import { ApiDBTransaction } from '../../dtos/indexer/ApiDbTransaction.dto';
89

910
import { ConfigService } from '@nestjs/config';
11+
import { ApiDBVersion } from '../../dtos/indexer/ApiDbVersion.dto';
1012
import { IConfig, VerifierServerConfig } from '../../config/configuration';
1113
import { ApiDBState } from '../../dtos/indexer/ApiDbState.dto';
1214
import { QueryBlock } from '../../dtos/indexer/QueryBlock.dto';
1315
import { QueryTransaction } from '../../dtos/indexer/QueryTransaction.dto';
1416
import {
17+
DBIndexerVersion,
18+
DBPruneSyncState,
19+
DBTipSyncState,
1520
DBUtxoIndexerBlock,
1621
DBUtxoTransaction,
17-
IDBUtxoIndexerBlock,
18-
IDBUtxoTransaction,
1922
IDBIndexerVersion,
20-
DBIndexerVersion,
2123
IDBPruneSyncState,
2224
IDBTipSyncState,
23-
DBPruneSyncState,
24-
DBTipSyncState,
25+
IDBUtxoIndexerBlock,
26+
IDBUtxoTransaction,
2527
} from '../../entity/utxo-entity-definitions';
2628
import { PaginatedList } from '../../utils/api-models/PaginatedList';
2729
import { IIndexerEngineService } from '../common/base-indexer-engine-service';
28-
import { ApiDBVersion } from 'src/dtos/indexer/ApiDbVersion.dto';
2930

3031
abstract class UtxoExternalIndexerEngineService extends IIndexerEngineService {
3132
// External utxo indexers specific tables
@@ -116,11 +117,33 @@ abstract class UtxoExternalIndexerEngineService extends IIndexerEngineService {
116117
this.versionTable,
117118
'version',
118119
);
120+
119121
const resVersion = await queryVersion.getOne();
120122
if (!resVersion) {
121123
throw new Error('No versions state found in the indexer database');
122124
}
123-
return resVersion.toApiDBVersion();
125+
126+
let versions = resVersion.toApiDBVersion();
127+
128+
const [gitTag, gitHash, buildDate] = await Promise.all([
129+
UtxoExternalIndexerEngineService.readVersionFile(
130+
'../../../PROJECT_VERSION',
131+
),
132+
UtxoExternalIndexerEngineService.readVersionFile(
133+
'../../../PROJECT_COMMIT_HASH',
134+
),
135+
UtxoExternalIndexerEngineService.readVersionFile(
136+
'../../../PROJECT_BUILD_DATE',
137+
),
138+
]);
139+
const apiServerVersion: Version = {
140+
gitTag: gitTag || 'local',
141+
gitHash: gitHash || 'local',
142+
buildDate: Number(buildDate) || Math.floor(Date.now() / 1000),
143+
};
144+
versions.apiServer = apiServerVersion;
145+
146+
return versions;
124147
}
125148

126149
/**

src/services/indexer-services/xrp-indexer.service.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { unPrefix0x } from '@flarenetwork/mcc';
22
import { Injectable } from '@nestjs/common';
33
import { ConfigService } from '@nestjs/config';
4+
import { ApiDBVersion } from '../../dtos/indexer/ApiDbVersion.dto';
45
import { EntityManager } from 'typeorm';
56
import { IConfig, VerifierServerConfig } from '../../config/configuration';
67
import { ApiDBBlock } from '../../dtos/indexer/ApiDbBlock.dto';
78
import { ApiDBState } from '../../dtos/indexer/ApiDbState.dto';
89
import { ApiDBTransaction } from '../../dtos/indexer/ApiDbTransaction.dto';
10+
import { Version } from '../../dtos/indexer/ApiDbVersion.dto';
911
import { QueryBlock } from '../../dtos/indexer/QueryBlock.dto';
1012
import { QueryTransaction } from '../../dtos/indexer/QueryTransaction.dto';
1113
import {
@@ -20,7 +22,6 @@ import {
2022
} from '../../entity/xrp-entity-definitions';
2123
import { PaginatedList } from '../../utils/api-models/PaginatedList';
2224
import { IIndexerEngineService } from '../common/base-indexer-engine-service';
23-
import { ApiDBVersion } from 'src/dtos/indexer/ApiDbVersion.dto';
2425

2526
@Injectable()
2627
export class XrpExternalIndexerEngineService extends IIndexerEngineService {
@@ -72,16 +73,41 @@ export class XrpExternalIndexerEngineService extends IIndexerEngineService {
7273
return response;
7374
}
7475

76+
/**
77+
* Gets the version of the indexer service.
78+
*/
7579
public async getIndexerServiceVersion(): Promise<ApiDBVersion> {
7680
const queryVersion = this.manager.createQueryBuilder(
7781
this.versionTable,
7882
'version',
7983
);
84+
8085
const resVersion = await queryVersion.getOne();
8186
if (!resVersion) {
8287
throw new Error('No versions state found in the indexer database');
8388
}
84-
return resVersion.toApiDBVersion();
89+
90+
let versions = resVersion.toApiDBVersion();
91+
92+
const [gitTag, gitHash, buildDate] = await Promise.all([
93+
XrpExternalIndexerEngineService.readVersionFile(
94+
'../../../PROJECT_VERSION',
95+
),
96+
XrpExternalIndexerEngineService.readVersionFile(
97+
'../../../PROJECT_COMMIT_HASH',
98+
),
99+
XrpExternalIndexerEngineService.readVersionFile(
100+
'../../../PROJECT_BUILD_DATE',
101+
),
102+
]);
103+
const apiServerVersion: Version = {
104+
gitTag: gitTag || 'local',
105+
gitHash: gitHash || 'local',
106+
buildDate: Number(buildDate) || Math.floor(Date.now() / 1000),
107+
};
108+
versions.apiServer = apiServerVersion;
109+
110+
return versions;
85111
}
86112

87113
public async confirmedBlockAt(

src/verifier-modules/btc-verifier-server.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { BTCConfirmedBlockHeightExistsVerifierService } from '../services/confir
1818
import { BtcExternalIndexerEngineService } from '../services/indexer-services/utxo-indexer.service';
1919
import { BTCPaymentVerifierService } from '../services/payment-verifier.service';
2020
import { BTCReferencedPaymentNonexistenceVerifierService } from '../services/referenced-payment-nonexistence-verifier.service';
21-
import { LoggerMiddleware } from 'src/middlware/LoggerMiddlwar';
21+
import { LoggerMiddleware } from '../middlware/LoggerMiddlwar';
2222

2323

2424
@Module({

src/verifier-modules/doge-verifier-server.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { DOGEConfirmedBlockHeightExistsVerifierService } from '../services/confi
1818
import { DogeExternalIndexerEngineService } from '../services/indexer-services/utxo-indexer.service';
1919
import { DOGEPaymentVerifierService } from '../services/payment-verifier.service';
2020
import { DOGEReferencedPaymentNonexistenceVerifierService } from '../services/referenced-payment-nonexistence-verifier.service';
21-
import { LoggerMiddleware } from 'src/middlware/LoggerMiddlwar';
21+
import { LoggerMiddleware } from '../middlware/LoggerMiddlwar';
2222

2323
@Module({
2424
imports: [

0 commit comments

Comments
 (0)