Skip to content

Commit 64fba7f

Browse files
authored
feat: ignore input-added events for non-existing apps. (#61)
1 parent 95767af commit 64fba7f

File tree

4 files changed

+328
-250
lines changed

4 files changed

+328
-250
lines changed

src/handlers/InputAdded.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -245,17 +245,13 @@ export default class InputAdded implements Handler {
245245
let application =
246246
this.applicationStorage.get(dappId) ??
247247
(await ctx.store.get(Application, dappId));
248+
248249
if (!application) {
249250
ctx.log.warn(`${dappId} (Application) not found`);
250-
application = new Application({
251-
id: dappId,
252-
timestamp: timestampInSeconds,
253-
chain: chain,
254-
address: dappAddress,
255-
rollupVersion: RollupVersion.v1,
256-
});
257-
this.applicationStorage.set(dappId, application);
258-
ctx.log.info(`${dappId} (Application) stored`);
251+
ctx.log.warn(
252+
`Ignoring event(InputAdded v1) [index: ${event.inboxInputIndex}]`,
253+
);
254+
return false;
259255
}
260256

261257
const inputId = generateIDFrom([dappId, event.inboxInputIndex]);

src/handlers/v2/InputAdded.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,11 @@ export default class InputAdded implements Handler {
251251
(await ctx.store.get(Application, appId));
252252
if (!application) {
253253
ctx.log.warn(`${appId} (Application v2) not found`);
254-
application = new Application({
255-
id: appId,
256-
timestamp: timestampInSeconds,
257-
chain: chain,
258-
address: appAddress,
259-
rollupVersion: RollupVersion.v2,
260-
});
261-
this.applicationStorage.set(appId, application);
262-
ctx.log.info(`${appId} (Application v2) stored`);
254+
ctx.log.warn(
255+
`Ignoring event(InputAdded v2) [index: ${event.index}]`,
256+
);
257+
258+
return false;
263259
}
264260

265261
const inputId = generateIDFrom([appId, index]);

tests/handlers/InputAdded.test.ts

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,38 @@ describe('InputAdded', () => {
8585
const mockChainStorage = new Map<string, Chain>();
8686
const expectedChain = { id: sepolia.id.toString() };
8787

88+
const buildValidApplication = (address: string) => ({
89+
id: `${sepolia.id}-${address}-v1`,
90+
address: `${address}`,
91+
timestamp: BigInt(logs[0].block.timestamp) / 1000n,
92+
chain: expectedChain,
93+
rollupVersion: 'v1',
94+
factory: {
95+
id: '11155111-0x7122cd1221c20892234186facfe8615e6743ab02',
96+
address: '0x7122cd1221c20892234186facfe8615e6743ab02',
97+
chain: {
98+
id: '11155111',
99+
},
100+
},
101+
});
102+
88103
beforeEach(() => {
104+
mockTokenStorage.clear();
105+
mockDepositStorage.clear();
106+
mockApplicationStorage.clear();
107+
mockInputStorage.clear();
108+
mockNftStorage.clear();
109+
mockErc721DepositStorage.clear();
110+
mockMultiTokenStorage.clear();
111+
mockErc1155DepositStorage.clear();
112+
113+
// add pre created valid apps
114+
const appOne = buildValidApplication(
115+
'0x0be010fa7e70d74fa8b6729fe1ae268787298f54',
116+
);
117+
118+
mockApplicationStorage.set(appOne.id, appOne);
119+
89120
inputAdded = new InputAdded(
90121
mockTokenStorage,
91122
mockDepositStorage,
@@ -97,15 +128,6 @@ describe('InputAdded', () => {
97128
mockErc1155DepositStorage,
98129
mockChainStorage,
99130
);
100-
101-
mockTokenStorage.clear();
102-
mockDepositStorage.clear();
103-
mockApplicationStorage.clear();
104-
mockInputStorage.clear();
105-
mockNftStorage.clear();
106-
mockErc721DepositStorage.clear();
107-
mockMultiTokenStorage.clear();
108-
mockErc1155DepositStorage.clear();
109131
});
110132

111133
afterEach(() => {
@@ -114,6 +136,7 @@ describe('InputAdded', () => {
114136

115137
describe('handle', async () => {
116138
test('should ignore events other than InputAdded', async () => {
139+
mockApplicationStorage.clear();
117140
await inputAdded.handle(logs[1], block, ctx);
118141
expect(mockInputStorage.size).toBe(0);
119142
expect(mockApplicationStorage.size).toBe(0);
@@ -126,19 +149,21 @@ describe('InputAdded', () => {
126149
expect(mockInputStorage.size).toBe(1);
127150
});
128151

129-
test('when creating a non-existing app it should also set the timestamp in seconds', async () => {
152+
test('should ignore inputs to non-existing applications', async () => {
153+
mockApplicationStorage.clear();
130154
await inputAdded.handle(logs[0], block, ctx);
131155

132-
const timestamp = BigInt(logs[0].block.timestamp) / 1000n;
133-
134156
const [application] = mockApplicationStorage.values();
135-
expect(application).toEqual({
136-
id: `${sepolia.id}-0x0be010fa7e70d74fa8b6729fe1ae268787298f54-v1`,
137-
address: '0x0be010fa7e70d74fa8b6729fe1ae268787298f54',
138-
timestamp,
139-
chain: expectedChain,
140-
rollupVersion: 'v1',
141-
});
157+
expect(application).toEqual(undefined);
158+
159+
expect(ctx.log.warn).toHaveBeenCalledTimes(2);
160+
expect(ctx.log.warn).toHaveBeenCalledWith(
161+
'11155111-0x0be010fa7e70d74fa8b6729fe1ae268787298f54-v1 (Application) not found',
162+
);
163+
164+
expect(ctx.log.warn).toHaveBeenCalledWith(
165+
'Ignoring event(InputAdded v1) [index: 1]',
166+
);
142167
});
143168

144169
test('should throw error when chain-id information is not available in the Log ', async () => {
@@ -332,13 +357,21 @@ describe('InputAdded', () => {
332357
describe('ERC-1155 deposits', () => {
333358
const tokenAddress = `${sepolia.id}-0x2960f4db2b0993ae5b59bc4a0f5ec7a1767e905e`;
334359

360+
beforeEach(() => {
361+
// add pre created valid apps
362+
const appOne = buildValidApplication(
363+
'0x4ca2f6935200b9a782a78f408f640f17b29809d8',
364+
);
365+
366+
mockApplicationStorage.set(appOne.id, appOne);
367+
});
368+
335369
afterEach(() => {
336370
vi.clearAllMocks();
337371
});
338372

339373
test('should store the token information', async () => {
340374
expect(mockMultiTokenStorage.size).toBe(0);
341-
342375
await inputAdded.handle(logErc1155SingleTransfer, block, ctx);
343376

344377
expect(mockMultiTokenStorage.size).toBe(1);

0 commit comments

Comments
 (0)