-
Notifications
You must be signed in to change notification settings - Fork 208
/
fake-bridge.js
383 lines (364 loc) · 11.5 KB
/
fake-bridge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// @ts-check
import { assert, Fail } from '@endo/errors';
import { makeTracer, VBankAccount } from '@agoric/internal';
import { E } from '@endo/far';
import { makeWhen } from '@agoric/vow/src/when.js';
import { Nat } from '@endo/nat';
/**
* @import {JsonSafe} from '@agoric/cosmic-proto';
* @import {MsgDelegateResponse, MsgUndelegateResponse} from '@agoric/cosmic-proto/cosmos/staking/v1beta1/tx.js';
* @import {MsgSendResponse} from '@agoric/cosmic-proto/cosmos/bank/v1beta1/tx.js';
* @import {BridgeHandler, ScopedBridgeManager} from '../src/types.js';
* @import {Remote} from '@agoric/vow';
*/
const trace = makeTracer('FakeBridge');
const when = makeWhen();
/** @typedef {{ [address: string]: { [denom: string]: bigint } }} Balances */
export const FAUCET_ADDRESS = 'faucet';
const INFINITE_AMOUNT = 99999999999n;
/**
* You can withdraw from the `faucet` address infinitely because its balance is
* always huge. When you withdraw, it's as if it is topped up again by a Cosmos
* transaction outside the Agoric VM. (Similarly for deposits.)
*
* @param {import('@agoric/zone').Zone} zone
* @param {object} opts
* @param {Balances} opts.balances initial balances
* @returns {ScopedBridgeManager<'bank'>}
* @see {makeFakeBankManagerKit} and its `pourPayment` for a helper
*/
export const makeFakeBankBridge = (zone, opts = { balances: {} }) => {
const { balances } = opts;
const currentBalance = ({ address, denom }) =>
address === FAUCET_ADDRESS
? INFINITE_AMOUNT
: Nat((balances[address] && balances[address][denom]) ?? 0n);
let lastNonce = 0n;
/** @type {Remote<BridgeHandler>} */
let hndlr;
return zone.exo('Fake Bank Bridge Manager', undefined, {
getBridgeId: () => 'bank',
toBridge: async obj => {
const { method, type, ...params } = obj;
trace('toBridge', type, method, params);
switch (obj.type) {
case 'VBANK_GET_MODULE_ACCOUNT_ADDRESS': {
const { moduleName } = obj;
const moduleDescriptor = Object.values(VBankAccount).find(
({ module }) => module === moduleName,
);
if (!moduleDescriptor) {
return 'undefined';
}
return moduleDescriptor.address;
}
// Observed message:
// address: 'agoric1megzytg65cyrgzs6fvzxgrcqvwwl7ugpt62346',
// denom: 'ibc/toyatom',
// type: 'VBANK_GET_BALANCE'
case 'VBANK_GET_BALANCE': {
return String(currentBalance(obj));
}
case 'VBANK_GRAB':
case 'VBANK_GIVE': {
const { amount, denom } = obj;
const address = type === 'VBANK_GRAB' ? obj.sender : obj.recipient;
(address && typeof address === 'string') ||
Fail`invalid address ${address}`;
balances[address] ||= {};
balances[address][denom] ||= 0n;
if (type === 'VBANK_GRAB') {
balances[address][denom] = Nat(
currentBalance({ address, denom }) - BigInt(amount),
);
} else {
balances[address][denom] = Nat(
currentBalance({ address, denom }) + BigInt(amount),
);
}
lastNonce += 1n;
// Also empty balances.
return harden({
type: 'VBANK_BALANCE_UPDATE',
nonce: `${lastNonce}`,
updated: [
{
address,
denom,
amount: String(currentBalance({ address, denom })),
},
],
});
}
default:
Fail`unknown type ${type}`;
}
},
fromBridge: async obj => {
if (!hndlr) throw Error('no handler!');
trace('fromBridge', obj);
return when(E(hndlr).fromBridge(obj));
},
initHandler: h => {
if (hndlr) throw Error('already init');
hndlr = h;
},
setHandler: h => {
if (!hndlr) throw Error('must init first');
hndlr = h;
},
});
};
/**
* @param {import('@agoric/zone').Zone} zone
* @param {(obj) => void} onToBridge
* @returns {ScopedBridgeManager<'dibc'>}
*/
export const makeFakeIbcBridge = (zone, onToBridge) => {
/** @type {Remote<BridgeHandler>} */
let hndlr;
return zone.exo('Fake IBC Bridge Manager', undefined, {
getBridgeId: () => 'dibc',
toBridge: async obj => {
onToBridge(obj);
const { method, type, ...params } = obj;
assert.equal(type, 'IBC_METHOD');
if (method === 'sendPacket') {
const { packet } = params;
return { ...packet, sequence: '39' };
} else if (method === 'startChannelCloseInit') {
const { packet } = params;
if (hndlr)
E(hndlr)
.fromBridge({
type: 'IBC_EVENT',
event: 'channelCloseConfirm',
packet,
})
.catch(e => console.error(e));
}
return undefined;
},
fromBridge: async obj => {
if (!hndlr) throw Error('no handler!');
return when(E(hndlr).fromBridge(obj));
},
initHandler: h => {
if (hndlr) throw Error('already init');
hndlr = h;
},
setHandler: h => {
if (!hndlr) throw Error('must init first');
hndlr = h;
},
});
};
export const LOCALCHAIN_DEFAULT_ADDRESS = 'agoric1fakeLCAAddress';
/**
* Constants that can be used to force an error state in a bridge transaction.
* Typically used for the LocalChainBridge which currently accepts all messages
* unless specified otherwise. Less useful for the DibcBridge which rejects all
* messages unless specified otherwise.
*/
export const SIMULATED_ERRORS = {
TIMEOUT: 504n,
BAD_REQUEST: 400n,
};
/**
* Used to mock responses from Cosmos Golang back to SwingSet for for
* E(lca).executeTx().
*
* Returns an empty object per message unless specified.
*
* @param {object} message
* @param {number} sequence
* @returns {unknown}
* @throws {Error} to simulate failures in certain cases
*/
export const fakeLocalChainBridgeTxMsgHandler = (message, sequence) => {
switch (message['@type']) {
// TODO #9402 reference bank to ensure caller has tokens they are transferring
case '/ibc.applications.transfer.v1.MsgTransfer': {
if (message.token.amount === String(SIMULATED_ERRORS.TIMEOUT)) {
throw Error('simulated unexpected MsgTransfer packet timeout');
}
// like `JsonSafe<MsgTransferResponse>`, but bigints are converted to numbers
// FIXME should vlocalchain return a string instead of number for bigint?
return {
sequence,
};
}
case '/cosmos.bank.v1beta1.MsgSend': {
if (message.amount[0].amount === String(SIMULATED_ERRORS.BAD_REQUEST)) {
throw Error('simulated error');
}
return /** @type {JsonSafe<MsgSendResponse>} */ ({});
}
case '/cosmos.staking.v1beta1.MsgDelegate': {
if (message.amount.amount === String(SIMULATED_ERRORS.TIMEOUT)) {
throw Error('simulated packet timeout');
}
return /** @type {JsonSafe<MsgDelegateResponse>} */ ({});
}
case '/cosmos.staking.v1beta1.MsgUndelegate': {
return /** @type {JsonSafe<MsgUndelegateResponse>} */ ({
// 5 seconds from unix epoch
completionTime: { seconds: '5', nanos: 0 },
});
}
// returns one empty object per message unless specified
default:
return {};
}
};
export const LOCALCHAIN_QUERY_ALL_BALANCES_RESPONSE = [
{
value: 10n,
denom: 'ubld',
},
{
value: 10n,
denom: 'uist',
},
];
/**
* Used to mock responses from Cosmos Golang back to SwingSet for for
* E(lca).query() and E(lca).queryMany().
*
* Returns an empty object per query message unless specified.
*
* @param {object} message
* @returns {unknown}
*/
export const fakeLocalChainBridgeQueryHandler = message => {
switch (message['@type']) {
case '/cosmos.bank.v1beta1.QueryAllBalancesRequest': {
return {
error: '',
height: '1',
reply: {
'@type': '/cosmos.bank.v1beta1.QueryAllBalancesResponse',
balances: LOCALCHAIN_QUERY_ALL_BALANCES_RESPONSE.map(x => ({
denom: x.denom,
amount: String(x.value),
})),
pagination: { nextKey: null, total: '2' },
},
};
}
case '/cosmos.bank.v1beta1.QueryBalanceRequest': {
return {
error: '',
height: '1',
reply: {
'@type': '/cosmos.bank.v1beta1.QueryBalanceResponse',
balance: {
denom: message.denom,
// return 10 for all denoms, somewhat arbitrarily.
// if a denom is not known to cosmos bank, we'd expect to see
// `{denom, amount: '0'}` returned
amount: '10',
},
},
};
}
// returns one empty object per message unless specified
default:
return {};
}
};
/**
* @param {import('@agoric/zone').Zone} zone
* @param {(obj) => void} [onToBridge]
* @returns {ScopedBridgeManager<'vlocalchain'>}
*/
export const makeFakeLocalchainBridge = (zone, onToBridge = () => {}) => {
/** @type {Remote<BridgeHandler>} */
let hndlr;
let lcaExecuteTxSequence = 0;
let accountsCreated = 0;
return zone.exo('Fake Localchain Bridge Manager', undefined, {
getBridgeId: () => 'vlocalchain',
toBridge: async obj => {
onToBridge(obj);
const { method, type, ...params } = obj;
trace('toBridge', type, method, params);
switch (type) {
case 'VLOCALCHAIN_ALLOCATE_ADDRESS': {
const address = `${LOCALCHAIN_DEFAULT_ADDRESS}${accountsCreated || ''}`;
accountsCreated += 1;
return address;
}
case 'VLOCALCHAIN_EXECUTE_TX': {
lcaExecuteTxSequence += 1;
return obj.messages.map(message =>
fakeLocalChainBridgeTxMsgHandler(message, lcaExecuteTxSequence),
);
}
case 'VLOCALCHAIN_QUERY_MANY': {
return obj.messages.map(message =>
fakeLocalChainBridgeQueryHandler(message),
);
}
default:
Fail`unknown type ${type}`;
}
return undefined;
},
fromBridge: async obj => {
if (!hndlr) throw Error('no handler!');
return when(E(hndlr).fromBridge(obj));
},
initHandler: h => {
if (hndlr) throw Error('already init');
hndlr = h;
},
setHandler: h => {
if (!hndlr) throw Error('must init first');
hndlr = h;
},
});
};
/**
* @param {import('@agoric/zone').Zone} zone
* @param {(obj) => void} [onToBridge]
* @returns {ScopedBridgeManager<'vtransfer'>}
*/
export const makeFakeTransferBridge = (zone, onToBridge = () => {}) => {
/** @type {Remote<BridgeHandler>} */
let hndlr;
const registered = zone.setStore('registered');
return zone.exo('Fake Transfer Bridge Manager', undefined, {
getBridgeId: () => 'vtransfer',
toBridge: async obj => {
onToBridge(obj);
const { type, ...params } = obj;
trace('toBridge', type, params);
switch (type) {
case 'BRIDGE_TARGET_REGISTER': {
registered.add(params.target);
return undefined;
}
case 'BRIDGE_TARGET_UNREGISTER': {
registered.delete(params.target);
return undefined;
}
default:
Fail`unknown type ${type}`;
}
return undefined;
},
fromBridge: async obj => {
if (!hndlr) throw Error('no handler!');
return when(E(hndlr).fromBridge(obj));
},
initHandler: h => {
if (hndlr) throw Error('already init');
hndlr = h;
},
setHandler: h => {
if (!hndlr) throw Error('must init first');
hndlr = h;
},
});
};