Skip to content

Commit 85fafe6

Browse files
authored
fix: Revert/decoder service integration (#2589)
This PR reverts the decoder service integration.
1 parent f41d223 commit 85fafe6

File tree

47 files changed

+826
-1817
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+826
-1817
lines changed

src/datasources/cache/cache.first.data.source.spec.ts

Lines changed: 229 additions & 529 deletions
Large diffs are not rendered by default.

src/datasources/cache/cache.first.data.source.ts

Lines changed: 9 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -71,71 +71,12 @@ export class CacheFirstDataSource {
7171
notFoundExpireTimeSeconds: number;
7272
networkRequest?: NetworkRequest;
7373
expireTimeSeconds?: number;
74-
}): Promise<Raw<T>> {
75-
return await this.tryCache({
76-
...args,
77-
queryFn: () => {
78-
return this._getFromNetworkAndWriteCache({
79-
...args,
80-
method: 'get',
81-
});
82-
},
83-
});
84-
}
85-
86-
/**
87-
* Gets the cached value behind {@link CacheDir}.
88-
* If the value is not present, it tries to get the respective JSON
89-
* payload from {@link url}.
90-
* 404 errors are cached with {@link notFoundExpireTimeSeconds} seconds expiration time.
91-
*
92-
* @param args.cacheDir - {@link CacheDir} containing the key and field to be used to retrieve from cache
93-
* @param args.url - the HTTP endpoint to retrieve the JSON payload
94-
* @param args.networkRequest - the HTTP request to be used if there is a cache miss
95-
* @param args.expireTimeSeconds - the time to live in seconds for the payload behind {@link CacheDir}
96-
* @param args.notFoundExpireTimeSeconds - the time to live in seconds for the error when the item is not found
97-
* @param args.data - the data to be sent in the body of the request
98-
*/
99-
async post<T>(args: {
100-
cacheDir: CacheDir;
101-
url: string;
102-
notFoundExpireTimeSeconds: number;
103-
networkRequest?: NetworkRequest;
104-
expireTimeSeconds?: number;
105-
data: object;
106-
}): Promise<Raw<T>> {
107-
return await this.tryCache({
108-
...args,
109-
queryFn: () => {
110-
return this._getFromNetworkAndWriteCache({
111-
...args,
112-
method: 'post',
113-
});
114-
},
115-
});
116-
}
117-
118-
/**
119-
* Gets the cached value behind {@link CacheDir}.
120-
* If the value is not present, it tries to get the respective JSON
121-
* payload from {@link queryFn}.
122-
* 404 errors are cached with {@link notFoundExpireTimeSeconds} seconds expiration time.
123-
*
124-
* @param args.cacheDir - {@link CacheDir} containing the key and field to be used to retrieve from cache
125-
* @param args.notFoundExpireTimeSeconds - the time to live in seconds for the error when the item is not found
126-
* @param args.fn - the function to be executed if the cache entry is not found
127-
* @returns the cached value or the result of the function
128-
*/
129-
private async tryCache<T>(args: {
130-
cacheDir: CacheDir;
131-
notFoundExpireTimeSeconds: number;
132-
queryFn: () => Promise<Raw<T>>;
13374
}): Promise<Raw<T>> {
13475
const cached = await this.cacheService.hGet(args.cacheDir);
13576
if (cached != null) return this._getFromCachedData(args.cacheDir, cached);
13677

13778
try {
138-
return await args.queryFn();
79+
return await this._getFromNetworkAndWriteCache(args);
13980
} catch (error) {
14081
if (
14182
error instanceof NetworkResponseError &&
@@ -170,34 +111,20 @@ export class CacheFirstDataSource {
170111
}
171112

172113
/**
173-
* Gets/posts the data from the network and caches the result.
114+
* Gets the data from the network and caches the result.
174115
*/
175-
private async _getFromNetworkAndWriteCache<T>(
176-
args:
177-
| {
178-
cacheDir: CacheDir;
179-
url: string;
180-
networkRequest?: NetworkRequest;
181-
expireTimeSeconds?: number;
182-
method: 'get';
183-
data?: never;
184-
}
185-
| {
186-
cacheDir: CacheDir;
187-
url: string;
188-
networkRequest?: NetworkRequest;
189-
expireTimeSeconds?: number;
190-
method: 'post';
191-
data: object;
192-
},
193-
): Promise<Raw<T>> {
116+
private async _getFromNetworkAndWriteCache<T>(args: {
117+
cacheDir: CacheDir;
118+
url: string;
119+
networkRequest?: NetworkRequest;
120+
expireTimeSeconds?: number;
121+
}): Promise<Raw<T>> {
194122
const { key, field } = args.cacheDir;
195123
this.loggingService.debug({ type: LogType.CacheMiss, key, field });
196124
const startTimeMs = Date.now();
197-
const { data } = await this.networkService[args.method]<T>({
125+
const { data } = await this.networkService.get<T>({
198126
url: args.url,
199127
networkRequest: args.networkRequest,
200-
data: args.data,
201128
});
202129

203130
const shouldBeCached = await this._shouldBeCached(key, startTimeMs);

src/datasources/cache/cache.router.ts

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ export class CacheRouter {
1616
private static readonly COUNTERFACTUAL_SAFE_KEY = 'counterfactual_safe';
1717
private static readonly COUNTERFACTUAL_SAFES_KEY = 'counterfactual_safes';
1818
private static readonly CREATION_TRANSACTION_KEY = 'creation_transaction';
19-
private static readonly DECODED_DATA_KEY = 'decoded_data';
20-
private static readonly DECODED_DATA_CONTRACTS_KEY = 'decoded_data_contracts';
2119
private static readonly DELEGATES_KEY = 'delegates';
2220
private static readonly FIREBASE_OAUTH2_TOKEN_KEY = 'firebase_oauth2_token';
2321
private static readonly INCOMING_TRANSFERS_KEY = 'incoming_transfers';
@@ -389,43 +387,6 @@ export class CacheRouter {
389387
);
390388
}
391389

392-
static getDecodedDataCacheKey(args: {
393-
chainId: string;
394-
data: `0x${string}`;
395-
to: `0x${string}`;
396-
}): string {
397-
return `${args.chainId}_${CacheRouter.DECODED_DATA_KEY}_${args.data}_${args.to}`;
398-
}
399-
400-
static getDecodedDataCacheDir(args: {
401-
chainId: string;
402-
data: `0x${string}`;
403-
to: `0x${string}`;
404-
}): CacheDir {
405-
return new CacheDir(CacheRouter.getDecodedDataCacheKey(args), '');
406-
}
407-
408-
static getDecodedDataContractsCacheKey(args: {
409-
chainIds: Array<string>;
410-
address: `0x${string}`;
411-
limit?: number;
412-
offset?: number;
413-
}): string {
414-
return `${args.chainIds.sort().join('_')}_${CacheRouter.DECODED_DATA_CONTRACTS_KEY}_${args.address}`;
415-
}
416-
417-
static getDecodedDataContractsCacheDir(args: {
418-
chainIds: Array<string>;
419-
address: `0x${string}`;
420-
limit?: number;
421-
offset?: number;
422-
}): CacheDir {
423-
return new CacheDir(
424-
CacheRouter.getDecodedDataContractsCacheKey(args),
425-
`${args.limit}_${args.offset}`,
426-
);
427-
}
428-
429390
static getAllTransactionsCacheDir(args: {
430391
chainId: string;
431392
safeAddress: `0x${string}`;

src/datasources/data-decoder-api/data-decoder-api.module.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ import { Module } from '@nestjs/common';
22
import { DataDecoderApi } from '@/datasources/data-decoder-api/data-decoder-api.service';
33
import { HttpErrorFactory } from '@/datasources/errors/http-error-factory';
44
import { IDataDecoderApi } from '@/domain/interfaces/data-decoder-api.interface';
5-
import { CacheFirstDataSourceModule } from '@/datasources/cache/cache.first.data.source.module';
65

76
@Module({
8-
imports: [CacheFirstDataSourceModule],
97
providers: [
108
HttpErrorFactory,
119
{ provide: IDataDecoderApi, useClass: DataDecoderApi },

src/datasources/data-decoder-api/data-decoder-api.service.spec.ts

Lines changed: 22 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,19 @@ import { DataSourceError } from '@/domain/errors/data-source.error';
99
import { NetworkResponseError } from '@/datasources/network/entities/network.error.entity';
1010
import { rawify } from '@/validation/entities/raw.entity';
1111
import type { IConfigurationService } from '@/config/configuration.service.interface';
12-
import type { CacheFirstDataSource } from '@/datasources/cache/cache.first.data.source';
12+
import type { INetworkService } from '@/datasources/network/network.service.interface';
1313

1414
const mockConfigurationService = jest.mocked({
1515
getOrThrow: jest.fn(),
1616
} as jest.MockedObjectDeep<IConfigurationService>);
1717

18-
const mockCacheFirstDataSource = jest.mocked({
18+
const mockNetworkService = jest.mocked({
1919
get: jest.fn(),
2020
post: jest.fn(),
21-
} as jest.MockedObjectDeep<CacheFirstDataSource>);
21+
} as jest.MockedObjectDeep<INetworkService>);
2222

2323
describe('DataDecoderApi', () => {
2424
const baseUrl = faker.internet.url({ appendSlash: false });
25-
const notFoundExpireTimeSeconds = faker.number.int();
2625
let target: DataDecoderApi;
2726

2827
beforeEach(() => {
@@ -32,15 +31,12 @@ describe('DataDecoderApi', () => {
3231
if (key === 'safeDataDecoder.baseUri') {
3332
return baseUrl;
3433
}
35-
if (key === 'expirationTimeInSeconds.notFound.default') {
36-
return notFoundExpireTimeSeconds;
37-
}
3834
throw new Error('Unexpected key');
3935
});
4036
const httpErrorFactory = new HttpErrorFactory();
4137
target = new DataDecoderApi(
4238
mockConfigurationService,
43-
mockCacheFirstDataSource,
39+
mockNetworkService,
4440
httpErrorFactory,
4541
);
4642
});
@@ -52,24 +48,19 @@ describe('DataDecoderApi', () => {
5248
const chainId = faker.string.numeric();
5349
const data = faker.string.hexadecimal() as `0x${string}`;
5450
const getDataDecodedUrl = `${baseUrl}/api/v1/data-decoder`;
55-
mockCacheFirstDataSource.post.mockImplementation(({ url }) => {
51+
mockNetworkService.post.mockImplementation(({ url }) => {
5652
if (url === getDataDecodedUrl) {
57-
return Promise.resolve(rawify(dataDecoded));
53+
return Promise.resolve({ status: 200, data: rawify(dataDecoded) });
5854
}
5955
throw new Error('Unexpected URL');
6056
});
6157

6258
const actual = await target.getDecodedData({ data, to, chainId });
6359

6460
expect(actual).toStrictEqual(dataDecoded);
65-
expect(mockCacheFirstDataSource.post).toHaveBeenCalledTimes(1);
66-
expect(mockCacheFirstDataSource.post).toHaveBeenCalledWith({
67-
cacheDir: {
68-
field: '',
69-
key: `${chainId}_decoded_data_${data}_${to}`,
70-
},
61+
expect(mockNetworkService.post).toHaveBeenCalledTimes(1);
62+
expect(mockNetworkService.post).toHaveBeenCalledWith({
7163
url: getDataDecodedUrl,
72-
notFoundExpireTimeSeconds,
7364
data: { chainId, to, data },
7465
});
7566
});
@@ -84,7 +75,7 @@ describe('DataDecoderApi', () => {
8475
});
8576
const expected = new DataSourceError(errorMessage, statusCode);
8677
const getDataDecodedUrl = `${baseUrl}/api/v1/data-decoder`;
87-
mockCacheFirstDataSource.post.mockImplementation(({ url }) => {
78+
mockNetworkService.post.mockImplementation(({ url }) => {
8879
if (url === getDataDecodedUrl) {
8980
return Promise.reject(
9081
new NetworkResponseError(
@@ -103,14 +94,9 @@ describe('DataDecoderApi', () => {
10394
target.getDecodedData({ data, to, chainId }),
10495
).rejects.toThrow(expected);
10596

106-
expect(mockCacheFirstDataSource.post).toHaveBeenCalledTimes(1);
107-
expect(mockCacheFirstDataSource.post).toHaveBeenCalledWith({
108-
cacheDir: {
109-
field: '',
110-
key: `${chainId}_decoded_data_${data}_${to}`,
111-
},
97+
expect(mockNetworkService.post).toHaveBeenCalledTimes(1);
98+
expect(mockNetworkService.post).toHaveBeenCalledWith({
11299
url: getDataDecodedUrl,
113-
notFoundExpireTimeSeconds,
114100
data: { chainId, to, data },
115101
});
116102
});
@@ -121,9 +107,9 @@ describe('DataDecoderApi', () => {
121107
const contract = contractBuilder().build();
122108
const contractPage = pageBuilder().with('results', [contract]).build();
123109
const getContractsUrl = `${baseUrl}/api/v1/contracts/${contract.address}`;
124-
mockCacheFirstDataSource.get.mockImplementation(({ url }) => {
110+
mockNetworkService.get.mockImplementation(({ url }) => {
125111
if (url === getContractsUrl) {
126-
return Promise.resolve(rawify(contractPage));
112+
return Promise.resolve({ status: 200, data: rawify(contractPage) });
127113
}
128114
throw new Error('Unexpected URL');
129115
});
@@ -134,14 +120,9 @@ describe('DataDecoderApi', () => {
134120
});
135121

136122
expect(actual).toStrictEqual(contractPage);
137-
expect(mockCacheFirstDataSource.get).toHaveBeenCalledTimes(1);
138-
expect(mockCacheFirstDataSource.get).toHaveBeenCalledWith({
139-
cacheDir: {
140-
field: 'undefined_undefined',
141-
key: `${contract.chainId}_decoded_data_contracts_${contract.address}`,
142-
},
123+
expect(mockNetworkService.get).toHaveBeenCalledTimes(1);
124+
expect(mockNetworkService.get).toHaveBeenCalledWith({
143125
url: getContractsUrl,
144-
notFoundExpireTimeSeconds,
145126
networkRequest: {
146127
params: {
147128
chain_ids: contract.chainId.toString(),
@@ -161,9 +142,9 @@ describe('DataDecoderApi', () => {
161142
];
162143
const contractPage = pageBuilder().with('results', [contract]).build();
163144
const getContractsUrl = `${baseUrl}/api/v1/contracts/${contract.address}`;
164-
mockCacheFirstDataSource.get.mockImplementation(({ url }) => {
145+
mockNetworkService.get.mockImplementation(({ url }) => {
165146
if (url === getContractsUrl) {
166-
return Promise.resolve(rawify(contractPage));
147+
return Promise.resolve({ status: 200, data: rawify(contractPage) });
167148
}
168149
throw new Error('Unexpected URL');
169150
});
@@ -174,14 +155,9 @@ describe('DataDecoderApi', () => {
174155
});
175156

176157
expect(actual).toStrictEqual(contractPage);
177-
expect(mockCacheFirstDataSource.get).toHaveBeenCalledTimes(1);
178-
expect(mockCacheFirstDataSource.get).toHaveBeenCalledWith({
179-
cacheDir: {
180-
field: 'undefined_undefined',
181-
key: `${chainIds.sort().join('_')}_decoded_data_contracts_${contract.address}`,
182-
},
158+
expect(mockNetworkService.get).toHaveBeenCalledTimes(1);
159+
expect(mockNetworkService.get).toHaveBeenCalledWith({
183160
url: getContractsUrl,
184-
notFoundExpireTimeSeconds,
185161
networkRequest: {
186162
params: {
187163
chain_ids: `${chainIds[0]}&chain_ids=${chainIds[1]}&chain_ids=${chainIds[2]}`,
@@ -200,7 +176,7 @@ describe('DataDecoderApi', () => {
200176
});
201177
const expected = new DataSourceError(errorMessage, statusCode);
202178
const getContractsUrl = `${baseUrl}/api/v1/contracts/${contract.address}`;
203-
mockCacheFirstDataSource.get.mockImplementation(({ url }) => {
179+
mockNetworkService.get.mockImplementation(({ url }) => {
204180
if (url === getContractsUrl) {
205181
return Promise.reject(
206182
new NetworkResponseError(
@@ -222,14 +198,9 @@ describe('DataDecoderApi', () => {
222198
}),
223199
).rejects.toThrow(expected);
224200

225-
expect(mockCacheFirstDataSource.get).toHaveBeenCalledTimes(1);
226-
expect(mockCacheFirstDataSource.get).toHaveBeenCalledWith({
227-
cacheDir: {
228-
field: 'undefined_undefined',
229-
key: `${contract.chainId}_decoded_data_contracts_${contract.address}`,
230-
},
201+
expect(mockNetworkService.get).toHaveBeenCalledTimes(1);
202+
expect(mockNetworkService.get).toHaveBeenCalledWith({
231203
url: getContractsUrl,
232-
notFoundExpireTimeSeconds,
233204
networkRequest: {
234205
params: {
235206
chain_ids: contract.chainId.toString(),

0 commit comments

Comments
 (0)