Skip to content

Commit 23cc4bd

Browse files
committed
chore(sui): enable multi-send in zkgm
Signed-off-by: aeryz <[email protected]>
1 parent c80a583 commit 23cc4bd

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

sui/ucs03_zkgm/sources/zkgm.move

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,12 @@ module zkgm::zkgm {
211211
salt: vector<u8>,
212212
}
213213

214+
public struct SendCtx {
215+
channel_id: u32,
216+
salt: vector<u8>,
217+
instructions: vector<Instruction>
218+
}
219+
214220
public struct AckCtx {
215221
packet_ctxs: vector<ZkgmPacketAckCtx>,
216222
packets: vector<Packet>,
@@ -333,41 +339,71 @@ module zkgm::zkgm {
333339
);
334340
}
335341

342+
public fun begin_send(
343+
channel_id: u32,
344+
salt: vector<u8>,
345+
): SendCtx {
346+
SendCtx {
347+
channel_id,
348+
salt,
349+
instructions: vector::empty()
350+
}
351+
}
352+
336353
public fun send_with_coin<T>(
337354
zkgm: &mut RelayStore,
338355
ibc_store: &mut ibc::IBCStore,
339-
clock: &Clock,
340356
coin: Coin<T>,
341-
channel_id: u32,
342-
timeout_height: u64,
343-
timeout_timestamp: u64,
344-
salt: vector<u8>,
345357
version: u8,
346358
opcode: u8,
347359
operand: vector<u8>,
360+
mut send_ctx: SendCtx,
348361
ctx: &mut TxContext
349-
) {
362+
): SendCtx {
350363
let instruction = instruction::new(version, opcode, operand);
351364
let sender = ctx.sender();
352365
let coin = zkgm.verify_internal<T>(
353366
ibc_store,
354367
option::some(coin),
355368
sender,
356-
channel_id,
369+
send_ctx.channel_id,
357370
0,
358371
instruction,
359372
ctx
360373
);
361374
// This guarantees that the coin is used by some instruction
362375
coin.destroy_none();
363376

364-
let zkgm_pack = zkgm_packet::new(salt, 0, instruction);
377+
send_ctx.instructions.push_back(instruction);
378+
379+
send_ctx
380+
}
381+
382+
public fun end_send(
383+
ibc_store: &mut ibc::IBCStore,
384+
clock: &Clock,
385+
timeout_height: u64,
386+
timeout_timestamp: u64,
387+
send_ctx: SendCtx,
388+
ctx: &mut TxContext,
389+
) {
390+
let SendCtx { channel_id, salt, instructions } = send_ctx;
391+
392+
let instruction = if (instructions.length() > 1) {
393+
instructions.do_ref!(|instr| {
394+
assert!(helper::is_allowed_batch_instruction(instr.opcode()), E_INVALID_BATCH_INSTRUCTION);
395+
});
396+
instruction::new(INSTR_VERSION_0, OP_BATCH, batch::new(instructions).encode())
397+
} else {
398+
instructions[0]
399+
};
400+
365401
ibc_store.send_packet(
366402
clock,
367403
channel_id,
368404
timeout_height,
369405
timeout_timestamp,
370-
zkgm_packet::encode(&zkgm_pack),
406+
zkgm_packet::encode(&zkgm_packet::new(salt, 0, instruction)),
371407
IbcAppWitness {},
372408
ctx
373409
);

0 commit comments

Comments
 (0)