@@ -189,7 +189,7 @@ export async function connectToChain(
189
189
fullArgs ,
190
190
{ maxBuffer : MAX_BUFFER_SIZE } ,
191
191
( _error , stdout , stderr ) => {
192
- return resolve ( { stdout, stderr } ) ;
192
+ resolve ( { stdout, stderr } ) ;
193
193
} ,
194
194
) ;
195
195
if ( stdin ) {
@@ -332,6 +332,10 @@ ${chainID} chain does not yet know of address ${clientAddr}${adviseEgress(
332
332
} ;
333
333
// Send that message, and wait for the subscription.
334
334
ws . send ( JSON . stringify ( obj ) ) ;
335
+
336
+ // Ensure our sender wakes up again.
337
+ // eslint-disable-next-line no-use-before-define
338
+ sendUpdater . updateState ( true ) ;
335
339
} ) ;
336
340
ws . addEventListener ( 'message' , ev => {
337
341
// We received a message.
@@ -359,7 +363,11 @@ ${chainID} chain does not yet know of address ${clientAddr}${adviseEgress(
359
363
360
364
const { notifier : sendNotifier , updater : sendUpdater } = makeNotifierKit ( ) ;
361
365
362
- // An array of [seqnum, message] ordered by unique seqnum.
366
+ /**
367
+ * @typedef {bigint } SeqNum
368
+ * @type {Array<[SeqNum, any]> }
369
+ * Ordered by seqnum
370
+ */
363
371
let messagePool = [ ] ;
364
372
365
373
// Atomically add to the message pool, ensuring the pool is sorted by unique
@@ -384,15 +392,18 @@ ${chainID} chain does not yet know of address ${clientAddr}${adviseEgress(
384
392
messagePool = uniquePool ;
385
393
} ;
386
394
395
+ const removeAckedFromMessagePool = ack => {
396
+ // Remove all messages sent at earlier acks.
397
+ messagePool = messagePool . filter ( m => m [ 0 ] > ack ) ;
398
+ } ;
399
+
387
400
let totalDeliveries = 0 ;
388
401
let highestAck = - 1 ;
389
402
let sequenceNumber = 0n ;
390
403
const sendFromMessagePool = async ( ) => {
391
404
let tmpInfo ;
392
405
393
- // We atomically drain the message pool.
394
406
const messages = messagePool ;
395
- messagePool = [ ] ;
396
407
397
408
try {
398
409
totalDeliveries += 1 ;
@@ -478,15 +489,12 @@ ${chainID} chain does not yet know of address ${clientAddr}${adviseEgress(
478
489
X `Unexpected output: ${ stdout . trimRight ( ) } ` ,
479
490
) ;
480
491
481
- // We submitted the transaction successfully.
492
+ // We submitted the transaction to the mempool successfully.
493
+ // Preemptively increment our sequence number to avoid needing to
494
+ // retry next time.
482
495
sequenceNumber += 1n ;
483
496
}
484
497
}
485
- } catch ( e ) {
486
- // Put back the deliveries we tried to make.
487
- messagePool = messages . concat ( messagePool ) ;
488
- messagePool . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ;
489
- throw e ;
490
498
} finally {
491
499
if ( tmpInfo ) {
492
500
await fs . promises . unlink ( tmpInfo . path ) ;
@@ -503,11 +511,9 @@ ${chainID} chain does not yet know of address ${clientAddr}${adviseEgress(
503
511
* @param {number= } lastBlockUpdate
504
512
*/
505
513
const recurseEachNewBlock = async ( lastBlockUpdate = undefined ) => {
506
- const { updateCount, value } = await blockNotifier . getUpdateSince (
507
- lastBlockUpdate ,
508
- ) ;
509
- assert ( value , X `${ GCI } unexpectedly finished!` ) ;
514
+ const { updateCount } = await blockNotifier . getUpdateSince ( lastBlockUpdate ) ;
510
515
console . debug ( `new block on ${ GCI } , fetching mailbox` ) ;
516
+ assert ( updateCount , X `${ GCI } unexpectedly finished!` ) ;
511
517
await getMailbox ( )
512
518
. then ( ret => {
513
519
if ( ! ret ) {
@@ -516,6 +522,7 @@ ${chainID} chain does not yet know of address ${clientAddr}${adviseEgress(
516
522
const { outbox, ack } = ret ;
517
523
// console.debug('have outbox', outbox, ack);
518
524
inbound ( GCI , outbox , ack ) ;
525
+ removeAckedFromMessagePool ( ack ) ;
519
526
} )
520
527
. catch ( e => console . error ( `Failed to fetch ${ GCI } mailbox:` , e ) ) ;
521
528
recurseEachNewBlock ( updateCount ) ;
@@ -543,10 +550,8 @@ ${chainID} chain does not yet know of address ${clientAddr}${adviseEgress(
543
550
// This function ensures we only have one outgoing send operation at a time.
544
551
const recurseEachSend = async ( lastSendUpdate = undefined ) => {
545
552
// See when there is another requested send since our last time.
546
- const { updateCount, value } = await sendNotifier . getUpdateSince (
547
- lastSendUpdate ,
548
- ) ;
549
- assert ( value , X `Sending unexpectedly finished!` ) ;
553
+ const { updateCount } = await sendNotifier . getUpdateSince ( lastSendUpdate ) ;
554
+ assert ( updateCount , X `Sending unexpectedly finished!` ) ;
550
555
551
556
await sendFromMessagePool ( ) . catch ( retrySend ) ;
552
557
recurseEachSend ( updateCount ) ;
0 commit comments