Skip to content

Commit 09ce090

Browse files
committed
cloud_function: makeCache to build from sdk chains
Signed-off-by: bingyuyap <bingyu.yap.21@gmail.com>
1 parent b19e05f commit 09ce090

File tree

2 files changed

+31
-285
lines changed

2 files changed

+31
-285
lines changed

cloud_functions/src/getMessages.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
ObservedMessageResponse,
88
MessagesByChain,
99
makeCache,
10+
makeCacheEntry,
1011
} from './types';
1112
import { ChainId } from '@wormhole-foundation/sdk-base';
1213
import { stringToChainId } from '@wormhole-foundation/wormhole-monitor-common';
@@ -155,15 +156,15 @@ const REFRESH_TIME_INTERVAL =
155156
const NUM_ROWS = Number(process.env.CLOUD_FUNCTIONS_NUM_ROWS) || 100;
156157
const BLOCK_INCREMENT = Number(process.env.CLOUD_FUNCTIONS_BLOCK_INCREMENT) || 10_000;
157158
/*
158-
The getMessages endpoint has 1 path param: chain id - required and multiple query params:
159+
The getMessages endpoint has 1 path param: chain id - required and multiple query params:
159160
(1) missingVaa - true/false
160161
(2) fromId - rowKey or prefix to start at for getRows()
161162
(3) reloadCache - true/false to reload the noVaaCache
162163
163164
if only chain is provided, the last numMessages will be returned regardless of hasSignedVaa status in descending order of timestamp
164165
if chain and missingVaa=true are used, then the rows will be filtered for hasSignedVaa=1
165166
166-
There is one cache: noVaaCache. It is keyed by ChainId and contains messages, last updated timestamp, and first missing vaa row key.
167+
There is one cache: noVaaCache. It is keyed by ChainId and contains messages, last updated timestamp, and first missing vaa row key.
167168
The cache is refreshed when the difference of current timestamp and last update timestamp is greater than refreshTimeInterval.
168169
When the cache is intially loaded, all of that chain's rows are retrieved and the latest "numMessages" messages returned. Each subsequent
169170
refresh first finds the most recent rows until first missing vaa row key.
@@ -219,10 +220,10 @@ export async function getMessages(req: any, res: any) {
219220
strict: false,
220221
});
221222
var pathVars = re.exec(req.path);
223+
222224
let chain: string | undefined = undefined;
223225
if (pathVars) {
224226
chain = pathVars[1];
225-
console.log(chain);
226227
const chainId: ChainId | undefined = stringToChainId(chain);
227228
if (chainId) {
228229
let messageResponse: ObservedMessageResponse = {
@@ -234,26 +235,29 @@ export async function getMessages(req: any, res: any) {
234235
let lastRowKey: string = '';
235236

236237
if (missingVaa === 'true' || missingVaa === '1') {
237-
if (
238-
noVaaCache[chainId]['messages'].length === 0 ||
239-
Date.now() - noVaaCache[chainId]['lastUpdated'] > REFRESH_TIME_INTERVAL
240-
) {
241-
if (noVaaCache[chainId]['messages'].length === 0) {
242-
console.log(`noVaaCache is empty, setting cache['messages] ${new Date()}`);
238+
if (!noVaaCache[chainId]) {
239+
noVaaCache[chainId] = makeCacheEntry();
240+
}
241+
242+
const cache = noVaaCache[chainId]!;
243+
244+
if (cache.messages.length === 0 || Date.now() - cache.lastUpdated > REFRESH_TIME_INTERVAL) {
245+
if (cache.messages.length === 0) {
246+
console.log(`noVaaCache is empty, setting cache['messages'] ${new Date()}`);
243247
} else {
244248
console.log(
245249
`noVaaCache is older than ${REFRESH_TIME_INTERVAL} ms, refreshing ${new Date()}`
246250
);
247251
}
248252
let prevDate = Date.now();
249-
lastRowKey = noVaaCache[chainId]['lastRowKey'];
253+
lastRowKey = cache.lastRowKey;
250254
messageResponse = await getMessagesNoSignedVaa_(chainId, NUM_ROWS, lastRowKey);
251255
console.log('In noVaaCache, after getMessages_=', Date.now() - prevDate);
252256
messages = messageResponse['messages'];
253257
noVaaCache[chainId] = messageResponse;
254258
} else {
255259
// console.log(`noVaaCache is still valid, not refreshing ${new Date()}`);
256-
messages = noVaaCache[chainId]['messages'];
260+
messages = cache.messages;
257261
}
258262
} else {
259263
let prevDate = Date.now();

cloud_functions/src/types.ts

+16-274
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChainId } from '@wormhole-foundation/sdk-base';
1+
import { ChainId, chainIds } from '@wormhole-foundation/sdk-base';
22

33
export type ObservedEventInfo = {
44
info: {
@@ -40,282 +40,24 @@ export type ObservedMessageResponse = {
4040
};
4141

4242
export type MessagesByChain = {
43-
[chain in ChainId]: ObservedMessageResponse;
43+
[chain in ChainId]?: ObservedMessageResponse;
4444
};
4545

46-
export function makeCache() {
47-
const cache: MessagesByChain = {
48-
1: {
49-
messages: [],
50-
lastUpdated: 0,
51-
lastRowKey: '',
52-
},
53-
2: {
54-
messages: [],
55-
lastUpdated: 0,
56-
lastRowKey: '',
57-
},
58-
3: {
59-
messages: [],
60-
lastUpdated: 0,
61-
lastRowKey: '',
62-
},
63-
4: {
64-
messages: [],
65-
lastUpdated: 0,
66-
lastRowKey: '',
67-
},
68-
5: {
69-
messages: [],
70-
lastUpdated: 0,
71-
lastRowKey: '',
72-
},
73-
6: {
74-
messages: [],
75-
lastUpdated: 0,
76-
lastRowKey: '',
77-
},
78-
7: {
79-
messages: [],
80-
lastUpdated: 0,
81-
lastRowKey: '',
82-
},
83-
8: {
84-
messages: [],
85-
lastUpdated: 0,
86-
lastRowKey: '',
87-
},
88-
9: {
89-
messages: [],
90-
lastUpdated: 0,
91-
lastRowKey: '',
92-
},
93-
10: {
94-
messages: [],
95-
lastUpdated: 0,
96-
lastRowKey: '',
97-
},
98-
11: {
99-
messages: [],
100-
lastUpdated: 0,
101-
lastRowKey: '',
102-
},
103-
12: {
104-
messages: [],
105-
lastUpdated: 0,
106-
lastRowKey: '',
107-
},
108-
13: {
109-
messages: [],
110-
lastUpdated: 0,
111-
lastRowKey: '',
112-
},
113-
14: {
114-
messages: [],
115-
lastUpdated: 0,
116-
lastRowKey: '',
117-
},
118-
15: {
119-
messages: [],
120-
lastUpdated: 0,
121-
lastRowKey: '',
122-
},
123-
16: {
124-
messages: [],
125-
lastUpdated: 0,
126-
lastRowKey: '',
127-
},
128-
17: {
129-
messages: [],
130-
lastUpdated: 0,
131-
lastRowKey: '',
132-
},
133-
18: {
134-
messages: [],
135-
lastUpdated: 0,
136-
lastRowKey: '',
137-
},
138-
19: {
139-
messages: [],
140-
lastUpdated: 0,
141-
lastRowKey: '',
142-
},
143-
20: {
144-
messages: [],
145-
lastUpdated: 0,
146-
lastRowKey: '',
147-
},
148-
21: {
149-
messages: [],
150-
lastUpdated: 0,
151-
lastRowKey: '',
152-
},
153-
22: {
154-
messages: [],
155-
lastUpdated: 0,
156-
lastRowKey: '',
157-
},
158-
23: {
159-
messages: [],
160-
lastUpdated: 0,
161-
lastRowKey: '',
162-
},
163-
24: {
164-
messages: [],
165-
lastUpdated: 0,
166-
lastRowKey: '',
167-
},
168-
25: {
169-
messages: [],
170-
lastUpdated: 0,
171-
lastRowKey: '',
172-
},
173-
26: {
174-
messages: [],
175-
lastUpdated: 0,
176-
lastRowKey: '',
177-
},
178-
28: {
179-
messages: [],
180-
lastUpdated: 0,
181-
lastRowKey: '',
182-
},
183-
29: {
184-
messages: [],
185-
lastUpdated: 0,
186-
lastRowKey: '',
187-
},
188-
30: {
189-
messages: [],
190-
lastUpdated: 0,
191-
lastRowKey: '',
192-
},
193-
32: {
194-
messages: [],
195-
lastUpdated: 0,
196-
lastRowKey: '',
197-
},
198-
33: {
199-
messages: [],
200-
lastUpdated: 0,
201-
lastRowKey: '',
202-
},
203-
34: {
204-
messages: [],
205-
lastUpdated: 0,
206-
lastRowKey: '',
207-
},
208-
35: {
209-
messages: [],
210-
lastUpdated: 0,
211-
lastRowKey: '',
212-
},
213-
36: {
214-
messages: [],
215-
lastUpdated: 0,
216-
lastRowKey: '',
217-
},
218-
37: {
219-
messages: [],
220-
lastUpdated: 0,
221-
lastRowKey: '',
222-
},
223-
38: {
224-
messages: [],
225-
lastUpdated: 0,
226-
lastRowKey: '',
227-
},
228-
39: {
229-
messages: [],
230-
lastUpdated: 0,
231-
lastRowKey: '',
232-
},
233-
40: {
234-
messages: [],
235-
lastUpdated: 0,
236-
lastRowKey: '',
237-
},
238-
3104: {
239-
messages: [],
240-
lastUpdated: 0,
241-
lastRowKey: '',
242-
},
243-
4000: {
244-
messages: [],
245-
lastUpdated: 0,
246-
lastRowKey: '',
247-
},
248-
4001: {
249-
messages: [],
250-
lastUpdated: 0,
251-
lastRowKey: '',
252-
},
253-
4002: {
254-
messages: [],
255-
lastUpdated: 0,
256-
lastRowKey: '',
257-
},
258-
4003: {
259-
messages: [],
260-
lastUpdated: 0,
261-
lastRowKey: '',
262-
},
263-
4004: {
264-
messages: [],
265-
lastUpdated: 0,
266-
lastRowKey: '',
267-
},
268-
4005: {
269-
messages: [],
270-
lastUpdated: 0,
271-
lastRowKey: '',
272-
},
273-
4006: {
274-
messages: [],
275-
lastUpdated: 0,
276-
lastRowKey: '',
277-
},
278-
4007: {
279-
messages: [],
280-
lastUpdated: 0,
281-
lastRowKey: '',
282-
},
283-
4008: {
284-
messages: [],
285-
lastUpdated: 0,
286-
lastRowKey: '',
287-
},
288-
10002: {
289-
messages: [],
290-
lastUpdated: 0,
291-
lastRowKey: '',
292-
},
293-
10003: {
294-
messages: [],
295-
lastUpdated: 0,
296-
lastRowKey: '',
297-
},
298-
10004: {
299-
messages: [],
300-
lastUpdated: 0,
301-
lastRowKey: '',
302-
},
303-
10005: {
304-
messages: [],
305-
lastUpdated: 0,
306-
lastRowKey: '',
307-
},
308-
10006: {
309-
messages: [],
310-
lastUpdated: 0,
311-
lastRowKey: '',
312-
},
313-
10007: {
314-
messages: [],
315-
lastUpdated: 0,
316-
lastRowKey: '',
317-
},
46+
export function makeCacheEntry(): ObservedMessageResponse {
47+
return {
48+
messages: [],
49+
lastUpdated: 0,
50+
lastRowKey: '',
31851
};
52+
}
53+
54+
export function makeCache(): MessagesByChain {
55+
const cache: MessagesByChain = {};
56+
57+
chainIds.forEach((chainId) => {
58+
cache[chainId] = makeCacheEntry();
59+
});
60+
31961
return cache;
32062
}
32163

0 commit comments

Comments
 (0)