Skip to content

Commit

Permalink
Merge branch 'main' into notifications-database
Browse files Browse the repository at this point in the history
  • Loading branch information
iamacook committed Jul 30, 2024
2 parents 2d2ab0d + 1519f67 commit 700e552
Show file tree
Hide file tree
Showing 26 changed files with 1,398 additions and 195 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ jobs:
- run: |
BUILD_NUMBER=${{ github.sha }}
echo "BUILD_NUMBER=${BUILD_NUMBER::7}" >> "$GITHUB_ENV"
- uses: docker/setup-qemu-action@v3.1.0
- uses: docker/setup-qemu-action@v3.2.0
with:
platforms: arm64
- uses: docker/setup-buildx-action@v3
Expand Down Expand Up @@ -149,7 +149,7 @@ jobs:
- run: |
BUILD_NUMBER=${{ github.sha }}
echo "BUILD_NUMBER=${BUILD_NUMBER::7}" >> "$GITHUB_ENV"
- uses: docker/setup-qemu-action@v3.1.0
- uses: docker/setup-qemu-action@v3.2.0
with:
platforms: arm64
- uses: docker/setup-buildx-action@v3
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@
"@nestjs/core": "^10.3.10",
"@nestjs/platform-express": "^10.3.10",
"@nestjs/serve-static": "^4.0.2",
"@nestjs/swagger": "^7.3.1",
"@nestjs/swagger": "^7.4.0",
"@safe-global/safe-deployments": "^1.37.1",
"amqp-connection-manager": "^4.1.14",
"amqplib": "^0.10.4",
"cookie-parser": "^1.4.6",
"jsonwebtoken": "^9.0.2",
"lodash": "^4.17.21",
"nestjs-cls": "^4.3.0",
"nestjs-cls": "^4.4.0",
"postgres": "^3.4.4",
"redis": "^4.6.15",
"redis": "^4.7.0",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1",
"semver": "^7.6.2",
"viem": "^2.17.4",
"winston": "^3.13.0",
"semver": "^7.6.3",
"viem": "^2.18.4",
"winston": "^3.13.1",
"zod": "^3.23.8"
},
"devDependencies": {
Expand All @@ -57,18 +57,18 @@
"@types/express": "^4.17.21",
"@types/jest": "29.5.12",
"@types/jsonwebtoken": "^9",
"@types/lodash": "^4.17.6",
"@types/lodash": "^4.17.7",
"@types/node": "^20.14.8",
"@types/semver": "^7.5.8",
"@types/supertest": "^6.0.2",
"eslint": "^9.7.0",
"eslint-config-prettier": "^9.1.0",
"husky": "^9.0.11",
"husky": "^9.1.4",
"jest": "29.7.0",
"prettier": "^3.3.2",
"source-map-support": "^0.5.20",
"supertest": "^7.0.0",
"ts-jest": "29.2.2",
"ts-jest": "29.2.3",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"tsconfig-paths": "4.2.0",
Expand Down
1 change: 1 addition & 0 deletions src/config/entities/__tests__/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export default (): ReturnType<typeof configuration> => ({
swapsDecoding: true,
twapsDecoding: true,
debugLogs: false,
configHooksDebugLogs: false,
imitationMapping: false,
auth: false,
confirmationView: false,
Expand Down
2 changes: 2 additions & 0 deletions src/config/entities/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ export default () => ({
swapsDecoding: process.env.FF_SWAPS_DECODING?.toLowerCase() === 'true',
twapsDecoding: process.env.FF_TWAPS_DECODING?.toLowerCase() === 'true',
debugLogs: process.env.FF_DEBUG_LOGS?.toLowerCase() === 'true',
configHooksDebugLogs:
process.env.FF_CONFIG_HOOKS_DEBUG_LOGS?.toLowerCase() === 'true',
imitationMapping:
process.env.FF_IMITATION_MAPPING?.toLowerCase() === 'true',
auth: process.env.FF_AUTH?.toLowerCase() === 'true',
Expand Down
2 changes: 2 additions & 0 deletions src/datasources/accounts/accounts.datasource.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AccountsDatasource } from '@/datasources/accounts/accounts.datasource';
import { FakeCacheService } from '@/datasources/cache/__tests__/fake.cache.service';
import { MAX_TTL } from '@/datasources/cache/constants';
import { CacheDir } from '@/datasources/cache/entities/cache-dir.entity';
import { CachedQueryResolver } from '@/datasources/db/cached-query-resolver';
import { PostgresDatabaseMigrator } from '@/datasources/db/postgres-database.migrator';
import { accountDataTypeBuilder } from '@/domain/accounts/entities/__tests__/account-data-type.builder';
import { upsertAccountDataSettingsDtoBuilder } from '@/domain/accounts/entities/__tests__/upsert-account-data-settings.dto.entity.builder';
Expand Down Expand Up @@ -43,6 +44,7 @@ describe('AccountsDatasource tests', () => {
target = new AccountsDatasource(
fakeCacheService,
sql,
new CachedQueryResolver(mockLoggingService, fakeCacheService),
mockLoggingService,
mockConfigurationService,
);
Expand Down
39 changes: 18 additions & 21 deletions src/datasources/accounts/accounts.datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
ICacheService,
} from '@/datasources/cache/cache.service.interface';
import { MAX_TTL } from '@/datasources/cache/constants';
import { getFromCacheOrExecuteAndCache } from '@/datasources/db/utils';
import { CachedQueryResolver } from '@/datasources/db/cached-query-resolver';
import { ICachedQueryResolver } from '@/datasources/db/cached-query-resolver.interface';
import { AccountDataSetting } from '@/domain/accounts/entities/account-data-setting.entity';
import { AccountDataType } from '@/domain/accounts/entities/account-data-type.entity';
import { Account } from '@/domain/accounts/entities/account.entity';
Expand All @@ -29,6 +30,8 @@ export class AccountsDatasource implements IAccountsDatasource, OnModuleInit {
constructor(
@Inject(CacheService) private readonly cacheService: ICacheService,
@Inject('DB_INSTANCE') private readonly sql: postgres.Sql,
@Inject(ICachedQueryResolver)
private readonly cachedQueryResolver: CachedQueryResolver,
@Inject(LoggingService) private readonly loggingService: ILoggingService,
@Inject(IConfigurationService)
private readonly configurationService: IConfigurationService,
Expand Down Expand Up @@ -70,13 +73,11 @@ export class AccountsDatasource implements IAccountsDatasource, OnModuleInit {

async getAccount(address: `0x${string}`): Promise<Account> {
const cacheDir = CacheRouter.getAccountCacheDir(address);
const [account] = await getFromCacheOrExecuteAndCache<Account[]>(
this.loggingService,
this.cacheService,
const [account] = await this.cachedQueryResolver.get<Account[]>({
cacheDir,
this.sql<Account[]>`SELECT * FROM accounts WHERE address = ${address}`,
this.defaultExpirationTimeInSeconds,
);
query: this.sql`SELECT * FROM accounts WHERE address = ${address}`,
ttl: this.defaultExpirationTimeInSeconds,
});

if (!account) {
throw new NotFoundException('Error getting account.');
Expand Down Expand Up @@ -106,30 +107,26 @@ export class AccountsDatasource implements IAccountsDatasource, OnModuleInit {

async getDataTypes(): Promise<AccountDataType[]> {
const cacheDir = CacheRouter.getAccountDataTypesCacheDir();
return getFromCacheOrExecuteAndCache<AccountDataType[]>(
this.loggingService,
this.cacheService,
return this.cachedQueryResolver.get<AccountDataType[]>({
cacheDir,
this.sql<AccountDataType[]>`SELECT * FROM account_data_types`,
MAX_TTL,
);
query: this.sql`SELECT * FROM account_data_types`,
ttl: MAX_TTL,
});
}

async getAccountDataSettings(
address: `0x${string}`,
): Promise<AccountDataSetting[]> {
const account = await this.getAccount(address);
const cacheDir = CacheRouter.getAccountDataSettingsCacheDir(address);
return getFromCacheOrExecuteAndCache<AccountDataSetting[]>(
this.loggingService,
this.cacheService,
return this.cachedQueryResolver.get<AccountDataSetting[]>({
cacheDir,
this.sql<AccountDataSetting[]>`
SELECT ads.* FROM account_data_settings ads INNER JOIN account_data_types adt
ON ads.account_data_type_id = adt.id
query: this.sql`
SELECT ads.* FROM account_data_settings ads
INNER JOIN account_data_types adt ON ads.account_data_type_id = adt.id
WHERE ads.account_id = ${account.id} AND adt.is_active IS TRUE;`,
this.defaultExpirationTimeInSeconds,
);
ttl: this.defaultExpirationTimeInSeconds,
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IConfigurationService } from '@/config/configuration.service.interface'
import { CounterfactualSafesDatasource } from '@/datasources/accounts/counterfactual-safes/counterfactual-safes.datasource';
import { FakeCacheService } from '@/datasources/cache/__tests__/fake.cache.service';
import { CacheDir } from '@/datasources/cache/entities/cache-dir.entity';
import { CachedQueryResolver } from '@/datasources/db/cached-query-resolver';
import { PostgresDatabaseMigrator } from '@/datasources/db/postgres-database.migrator';
import { createCounterfactualSafeDtoBuilder } from '@/domain/accounts/counterfactual-safes/entities/__tests__/create-counterfactual-safe.dto.entity.builder';
import { accountBuilder } from '@/domain/accounts/entities/__tests__/account.builder';
Expand Down Expand Up @@ -42,6 +43,7 @@ describe('CounterfactualSafesDatasource tests', () => {
target = new CounterfactualSafesDatasource(
fakeCacheService,
sql,
new CachedQueryResolver(mockLoggingService, fakeCacheService),
mockLoggingService,
mockConfigurationService,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
CacheService,
ICacheService,
} from '@/datasources/cache/cache.service.interface';
import { getFromCacheOrExecuteAndCache } from '@/datasources/db/utils';
import { CachedQueryResolver } from '@/datasources/db/cached-query-resolver';
import { ICachedQueryResolver } from '@/datasources/db/cached-query-resolver.interface';
import { CounterfactualSafe } from '@/domain/accounts/counterfactual-safes/entities/counterfactual-safe.entity';
import { CreateCounterfactualSafeDto } from '@/domain/accounts/counterfactual-safes/entities/create-counterfactual-safe.dto.entity';
import { Account } from '@/domain/accounts/entities/account.entity';
Expand All @@ -22,6 +23,8 @@ export class CounterfactualSafesDatasource
constructor(
@Inject(CacheService) private readonly cacheService: ICacheService,
@Inject('DB_INSTANCE') private readonly sql: postgres.Sql,
@Inject(ICachedQueryResolver)
private readonly cachedQueryResolver: CachedQueryResolver,
@Inject(LoggingService) private readonly loggingService: ILoggingService,
@Inject(IConfigurationService)
private readonly configurationService: IConfigurationService,
Expand Down Expand Up @@ -58,16 +61,14 @@ export class CounterfactualSafesDatasource
args.chainId,
args.predictedAddress,
);
const [counterfactualSafe] = await getFromCacheOrExecuteAndCache<
const [counterfactualSafe] = await this.cachedQueryResolver.get<
CounterfactualSafe[]
>(
this.loggingService,
this.cacheService,
>({
cacheDir,
this.sql<CounterfactualSafe[]>`
query: this.sql<CounterfactualSafe[]>`
SELECT * FROM counterfactual_safes WHERE chain_id = ${args.chainId} AND predicted_address = ${args.predictedAddress}`,
this.defaultExpirationTimeInSeconds,
);
ttl: this.defaultExpirationTimeInSeconds,
});

if (!counterfactualSafe) {
throw new NotFoundException('Error getting Counterfactual Safe.');
Expand All @@ -82,14 +83,12 @@ export class CounterfactualSafesDatasource
const cacheDir = CacheRouter.getCounterfactualSafesCacheDir(
account.address,
);
return getFromCacheOrExecuteAndCache<CounterfactualSafe[]>(
this.loggingService,
this.cacheService,
return this.cachedQueryResolver.get<CounterfactualSafe[]>({
cacheDir,
this.sql<CounterfactualSafe[]>`
query: this.sql<CounterfactualSafe[]>`
SELECT * FROM counterfactual_safes WHERE account_id = ${account.id}`,
this.defaultExpirationTimeInSeconds,
);
ttl: this.defaultExpirationTimeInSeconds,
});
}

async deleteCounterfactualSafe(args: {
Expand Down
1 change: 1 addition & 0 deletions src/datasources/cache/cache.first.data.source.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('CacheFirstDataSource', () => {
fakeCacheService = new FakeCacheService();
fakeConfigurationService = new FakeConfigurationService();
fakeConfigurationService.set('features.debugLogs', true);
fakeConfigurationService.set('features.configHooksDebugLogs', false);
cacheFirstDataSource = new CacheFirstDataSource(
fakeCacheService,
mockNetworkService,
Expand Down
32 changes: 32 additions & 0 deletions src/datasources/cache/cache.first.data.source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { Safe } from '@/domain/safe/entities/safe.entity';
@Injectable()
export class CacheFirstDataSource {
private readonly areDebugLogsEnabled: boolean;
private readonly areConfigHooksDebugLogsEnabled: boolean;

constructor(
@Inject(CacheService) private readonly cacheService: ICacheService,
Expand All @@ -45,6 +46,10 @@ export class CacheFirstDataSource {
) {
this.areDebugLogsEnabled =
this.configurationService.getOrThrow<boolean>('features.debugLogs');
this.areConfigHooksDebugLogsEnabled =
this.configurationService.getOrThrow<boolean>(
'features.configHooksDebugLogs',
);
}

/**
Expand Down Expand Up @@ -149,6 +154,13 @@ export class CacheFirstDataSource {
data as Safe,
);
}

if (
this.areConfigHooksDebugLogsEnabled &&
args.cacheDir.key.includes('chain')
) {
this.logChainUpdateCacheWrite(startTimeMs, args.cacheDir, data);
}
}
return data;
}
Expand Down Expand Up @@ -267,4 +279,24 @@ export class CacheFirstDataSource {
safe,
});
}

/**
* Logs the chain/chains retrieved.
* NOTE: this is a debugging-only function.
* TODO: remove this function after debugging.
*/
private logChainUpdateCacheWrite(
requestStartTime: number,
cacheDir: CacheDir,
data: unknown,
): void {
this.loggingService.info({
type: 'cache_write',
cacheKey: cacheDir.key,
cacheField: cacheDir.field,
cacheWriteTime: new Date(),
requestStartTime: new Date(requestStartTime),
data,
});
}
}
8 changes: 8 additions & 0 deletions src/datasources/config-api/config-api.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { HttpErrorFactory } from '@/datasources/errors/http-error-factory';
import { chainBuilder } from '@/domain/chains/entities/__tests__/chain.builder';
import { DataSourceError } from '@/domain/errors/data-source.error';
import { safeAppBuilder } from '@/domain/safe-apps/entities/__tests__/safe-app.builder';
import { ILoggingService } from '@/logging/logging.interface';
import { faker } from '@faker-js/faker';

const dataSource = {
Expand All @@ -25,6 +26,10 @@ const httpErrorFactory = {
} as jest.MockedObjectDeep<HttpErrorFactory>;
const mockHttpErrorFactory = jest.mocked(httpErrorFactory);

const mockLoggingService = {
info: jest.fn(),
} as jest.MockedObjectDeep<ILoggingService>;

describe('ConfigApi', () => {
const baseUri = faker.internet.url({ appendSlash: false });
const expirationTimeInSeconds = faker.number.int();
Expand All @@ -43,6 +48,7 @@ describe('ConfigApi', () => {
'expirationTimeInSeconds.notFound.default',
notFoundExpirationTimeInSeconds,
);
fakeConfigurationService.set('features.configHooksDebugLogs', false);
});

beforeEach(() => {
Expand All @@ -52,6 +58,7 @@ describe('ConfigApi', () => {
mockCacheService,
fakeConfigurationService,
mockHttpErrorFactory,
mockLoggingService,
);
});

Expand All @@ -65,6 +72,7 @@ describe('ConfigApi', () => {
mockCacheService,
fakeConfigurationService,
mockHttpErrorFactory,
mockLoggingService,
),
).toThrow();
});
Expand Down
Loading

0 comments on commit 700e552

Please sign in to comment.