Skip to content

Commit 5c76981

Browse files
committed
fix: full traversal of sendPacket from end-to-end
1 parent cab6be8 commit 5c76981

File tree

2 files changed

+50
-55
lines changed

2 files changed

+50
-55
lines changed

packages/cosmic-swingset/lib/ag-solo/html/main.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,19 @@ function run() {
9999
if (histnum >= nextHistNum) {
100100
nextHistNum = histnum + 1;
101101
}
102-
const c = document.getElementById(`command-${histnum}`);
103-
if (c) {
102+
const m1 = document.getElementById(`msg-command-${histnum}`);
103+
const m2 = document.getElementById(`msg-history-${histnum}`);
104+
if (m1 || m2) {
105+
const c = document.getElementById(`command-${histnum}`);
104106
const h1 = document.getElementById(`history-${histnum}`);
105-
const m1 = document.getElementById(`msg-command-${histnum}`);
106-
const m2 = document.getElementById(`msg-history-${histnum}`);
107107
c.innerHTML = linesToHTML(`${command}`);
108-
m1.innerHTML = linesToHTML(`${consoles.command}`);
108+
if (m1) {
109+
m1.innerHTML = linesToHTML(`${consoles.command}`);
110+
}
109111
h1.innerHTML = linesToHTML(`${result}`);
110-
m2.innerHTML = linesToHTML(`${consoles.display}`);
112+
if (m2) {
113+
m2.innerHTML = linesToHTML(`${consoles.display}`);
114+
}
111115
} else {
112116
addHistoryEntry(histnum, command, result, consoles);
113117
}

packages/cosmic-swingset/lib/ag-solo/vats/ibc.js

+40-49
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
rethrowUnlessMissing,
55
dataToBase64,
66
base64ToBytes,
7-
toBytes,
87
getPrefixes,
98
} from '@agoric/swingset-vat/src/vats/network';
109
import makeStore from '@agoric/store';
@@ -143,6 +142,31 @@ export function makeIBCProtocolHandler(
143142
*/
144143
const channelKeyToSeqAck = makeStore('CHANNEL:PORT');
145144

145+
/**
146+
* Send a packet out via the IBC device.
147+
* @param {IBCPacket} packet
148+
* @param {Store<number, PromiseRecord<Bytes, any>>} seqToAck
149+
*/
150+
async function ibcSendPacket(packet, seqToAck) {
151+
// Make a kernel call to do the send.
152+
const fullPacket = await callIBCDevice('sendPacket', {
153+
packet,
154+
relativeTimeout: DEFAULT_PACKET_TIMEOUT,
155+
});
156+
157+
// Extract the actual sequence number from the return.
158+
const { sequence } = fullPacket;
159+
160+
/**
161+
* @type {PromiseRecord<Bytes, any>}
162+
*/
163+
const ackDeferred = producePromise();
164+
165+
// Register the ack resolver/rejector with this sequence number.
166+
seqToAck.init(sequence, ackDeferred);
167+
return ackDeferred.promise;
168+
}
169+
146170
/**
147171
* @param {string} channelID
148172
* @param {string} portID
@@ -161,60 +185,39 @@ export function makeIBCProtocolHandler(
161185
const channelKey = `${channelID}:${portID}`;
162186
const seqToAck = makeStore('SEQUENCE');
163187
channelKeyToSeqAck.init(channelKey, seqToAck);
188+
164189
/**
165190
* @param {Connection} _conn
166191
* @param {Bytes} packetBytes
167192
* @param {ConnectionHandler} _handler
168193
* @returns {Promise<Bytes>} Acknowledgement data
169194
*/
170195
let onReceive = async (_conn, packetBytes, _handler) => {
196+
// console.error(`Remote IBC Handler ${portID} ${channelID}`);
171197
const packet = {
172198
source_port: portID,
173199
source_channel: channelID,
174200
destination_port: rPortID,
175201
destination_channel: rChannelID,
176202
data: dataToBase64(packetBytes),
177203
};
178-
const fullPacket = await callIBCDevice('sendPacket', {
179-
packet,
180-
relativeTimeout: DEFAULT_PACKET_TIMEOUT,
181-
});
182-
const { sequence } = fullPacket;
183-
/**
184-
* @type {PromiseRecord<Bytes, any>}
185-
*/
186-
const ackDeferred = producePromise();
187-
seqToAck.init(sequence, ackDeferred);
188-
return ackDeferred.promise;
204+
return ibcSendPacket(packet, seqToAck);
189205
};
190206

191-
if (ordered) {
207+
// FIXME: We may want a queue sometime to sequence
208+
// our packets, but it doesn't currently work (received
209+
// packets don't arrive).
210+
if (false && ordered) {
192211
// Set up a queue on the receiver.
193212
const withChannelReceiveQueue = makeWithQueue();
194213
onReceive = withChannelReceiveQueue(onReceive);
195214
}
196215

197216
return harden({
198-
async onOpen(conn, handler) {
217+
async onOpen(conn, _handler) {
199218
console.info('onOpen Remote IBC Connection', channelID, portID);
200-
/**
201-
* @param {Data} data
202-
* @returns {Promise<Bytes>} acknowledgement
203-
*/
204-
let sender = data =>
205-
/** @type {Promise<Bytes>} */
206-
(E(handler)
207-
.onReceive(conn, toBytes(data), handler)
208-
.catch(rethrowUnlessMissing));
209-
if (ordered) {
210-
// Set up a queue on the sender.
211-
const withChannelSendQueue = makeWithQueue();
212-
sender = withChannelSendQueue(sender);
213-
}
214-
const boundSender = sender;
215-
sender = data => {
216-
return boundSender(data);
217-
};
219+
const connP = /** @type {Promise<Connection, any>} */ (E.when(conn));
220+
channelKeyToConnP.init(channelKey, connP);
218221
},
219222
onReceive,
220223
async onClose(_conn, _reason, _handler) {
@@ -623,11 +626,7 @@ paths:
623626

624627
// Actually connect.
625628
// eslint-disable-next-line prettier/prettier
626-
const pr = E(protocolImpl).inbound(listenSearch, localAddr, remoteAddr, rchandler);
627-
const connP = /** @type {Promise<Connection>} */ (pr);
628-
629-
/* Stash it for later use. */
630-
channelKeyToConnP.init(channelKey, connP);
629+
E(protocolImpl).inbound(listenSearch, localAddr, remoteAddr, rchandler);
631630
break;
632631
}
633632

@@ -640,6 +639,7 @@ paths:
640639
} = packet;
641640
const channelKey = `${channelID}:${portID}`;
642641

642+
console.log(`Received with:`, channelKey, channelKeyToConnP.keys());
643643
const connP = channelKeyToConnP.get(channelKey);
644644
const data = base64ToBytes(data64);
645645

@@ -709,20 +709,11 @@ paths:
709709
}
710710

711711
const { source_port: portID, source_channel: channelID } = packet;
712-
713-
const fullPacket = await callIBCDevice('sendPacket', { packet });
714-
715-
const { sequence } = fullPacket;
716-
/**
717-
* @type {PromiseRecord<Bytes, any>}
718-
*/
719-
const ackDeferred = producePromise();
720712
const channelKey = `${channelID}:${portID}`;
721713
const seqToAck = channelKeyToSeqAck.get(channelKey);
722-
seqToAck.init(sequence, ackDeferred);
723-
ackDeferred.promise.then(
724-
ack => console.info('Manual packet', fullPacket, 'acked:', ack),
725-
e => console.warn('Manual packet', fullPacket, 'timed out:', e),
714+
ibcSendPacket(packet, seqToAck).then(
715+
ack => console.info('Manual packet', packet, 'acked:', ack),
716+
e => console.warn('Manual packet', packet, 'failed:', e),
726717
);
727718
break;
728719
}

0 commit comments

Comments
 (0)