Skip to content

Commit 9d58314

Browse files
committed
fix(solo): only subscribe to one copy of mailbox events
1 parent 35e9c87 commit 9d58314

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

packages/solo/src/chain-cosmos-sdk.js

+18-17
Original file line numberDiff line numberDiff line change
@@ -347,14 +347,8 @@ ${chainID} chain does not yet know of address ${clientAddr}${adviseEgress(
347347
// We want to subscribe.
348348
method: 'subscribe',
349349
params: {
350-
// Here is the Tendermint event for new blocks.
351-
// query: `tm.event = 'NewBlockHeader'`,
352-
353-
// This is the Cosmos event for activityhash changes.
354-
// query: `storage.path = 'activityhash'`,
355-
356-
// This is the Cosmos event for our mailbox changes.
357-
query: `storage.path = '${mailboxPath}'`,
350+
// This is the minimal query for mailbox changes.
351+
query: `tm.event = 'NewBlockHeader' AND storage.path = '${mailboxPath}'`,
358352
},
359353
};
360354
// Send that message, and wait for the subscription.
@@ -363,7 +357,7 @@ ${chainID} chain does not yet know of address ${clientAddr}${adviseEgress(
363357
// Poll to establish our initial mailbox.
364358
const pollMailboxWhileFirstUpdate = async () => {
365359
if (!firstUpdate) {
366-
// Already got the subscription.
360+
// We're too late.
367361
return;
368362
}
369363
const mb = await getMailbox().then(
@@ -373,12 +367,12 @@ ${chainID} chain does not yet know of address ${clientAddr}${adviseEgress(
373367
},
374368
);
375369
if (!firstUpdate) {
376-
// Already got the subscription.
370+
// We're too late.
377371
return;
378372
}
379-
console.error('got first mb', mb);
373+
// console.error('got first mb', mb);
380374
if (mb !== undefined) {
381-
// Found the first mailbox.
375+
// Found the initial mailbox.
382376
// console.error('Updating in pollMailboxWhileFirstUpdate');
383377
updater.updateState(mb);
384378
firstUpdate = false;
@@ -412,6 +406,12 @@ ${chainID} chain does not yet know of address ${clientAddr}${adviseEgress(
412406
return;
413407
}
414408

409+
if (obj.error) {
410+
console.error(`Error subscribing to events`, obj.error);
411+
ws.close();
412+
return;
413+
}
414+
415415
// It matches our subscription, so maybe notify.
416416
const events = obj.result.events;
417417
if (!events) {
@@ -420,18 +420,19 @@ ${chainID} chain does not yet know of address ${clientAddr}${adviseEgress(
420420
const paths = events['storage.path'];
421421
const values = events['storage.value'];
422422

423-
// Find only the latest value.
424-
let latestValue;
423+
// Find only the latest value in the events.
424+
let latestMailboxValue;
425425
paths.forEach((key, i) => {
426426
if (key === mailboxPath) {
427-
latestValue = values[i];
427+
latestMailboxValue = values[i];
428428
}
429429
});
430-
if (latestValue === undefined) {
430+
if (latestMailboxValue === undefined) {
431+
// No matching events found.
431432
return;
432433
}
433434

434-
const mb = JSON.parse(latestValue);
435+
const mb = JSON.parse(latestMailboxValue);
435436

436437
// Update our notifier.
437438
// console.error('Updating in ws.message');

0 commit comments

Comments
 (0)