@@ -267,3 +267,121 @@ pub fn get_gas_estimates(
267267 } )
268268 . collect ( )
269269}
270+
271+ #[ cfg( test) ]
272+ mod tests {
273+ use namada_sdk:: address:: PGF ;
274+ use namada_sdk:: ibc:: apps:: transfer:: types:: PrefixedCoin ;
275+ use namada_sdk:: ibc:: apps:: transfer:: types:: packet:: PacketData as Ics20PacketData ;
276+ use shared:: block_result:: {
277+ Event , EventKind , IbcCorePacketKind , IbcPacket ,
278+ } ;
279+ use shared:: ser:: { AccountsMap , TransferData } ;
280+ use shared:: token:: Token ;
281+ use shared:: transaction:: TransactionExitStatus ;
282+
283+ use super :: * ;
284+
285+ fn mock_block_result (
286+ inner_tx_hash : Option < & str > ,
287+ with_packet_sender : Option < String > ,
288+ ) -> BlockResult {
289+ BlockResult {
290+ end_events : vec ! [ Event {
291+ kind: EventKind :: IbcCore ( IbcCorePacketKind :: Send ) ,
292+ inner_tx_hash: inner_tx_hash
293+ . map( |hash| Id :: Hash ( hash. to_string( ) ) ) ,
294+ attributes: Some ( TxAttributesType :: SendPacket ( IbcPacket {
295+ source_port: "transfer" . to_string( ) ,
296+ dest_port: "transfer" . to_string( ) ,
297+ source_channel: "channel-0" . to_string( ) ,
298+ dest_channel: "channel-0" . to_string( ) ,
299+ timeout_timestamp: 0 ,
300+ timeout_height: String :: new( ) ,
301+ sequence: "1" . to_string( ) ,
302+ data: with_packet_sender
303+ . map( |sender| {
304+ serde_json:: to_string( & Ics20PacketData {
305+ token: PrefixedCoin {
306+ denom: "eatshit" . parse( ) . unwrap( ) ,
307+ amount: "1234" . parse( ) . unwrap( ) ,
308+ } ,
309+ sender: sender. into( ) ,
310+ receiver: "a1aaaa" . to_string( ) . into( ) ,
311+ memo: String :: new( ) . into( ) ,
312+ } )
313+ . unwrap( )
314+ } )
315+ . unwrap_or_default( ) ,
316+ } ) ) ,
317+ } ] ,
318+ ..Default :: default ( )
319+ }
320+ }
321+
322+ #[ test]
323+ fn test_get_ibc_packets ( ) {
324+ let expected_seq = |tx_id| IbcSequence {
325+ sequence_number : "1" . to_string ( ) ,
326+ source_port : "transfer" . to_string ( ) ,
327+ dest_port : "transfer" . to_string ( ) ,
328+ source_channel : "channel-0" . to_string ( ) ,
329+ dest_channel : "channel-0" . to_string ( ) ,
330+ timeout : 0 ,
331+ tx_id,
332+ } ;
333+
334+ // get ibc seq just from the events + inner tx hash
335+ let block_result = mock_block_result ( Some ( "deadbeef" ) , None ) ;
336+ assert_eq ! (
337+ get_ibc_packets( & block_result, & [ ] ) ,
338+ vec![ expected_seq( Id :: Hash ( "deadbeef" . to_string( ) ) ) ] ,
339+ ) ;
340+
341+ // protocol transfer, there is no inner tx hash
342+ let block_result = mock_block_result ( None , Some ( PGF . to_string ( ) ) ) ;
343+ assert_eq ! (
344+ get_ibc_packets( & block_result, & [ ] ) ,
345+ vec![ expected_seq( Id :: Hash (
346+ Hash :: sha256( "transfer/channel-0/transfer/channel-0/1" )
347+ . to_string( )
348+ . to_lowercase( )
349+ ) ) ] ,
350+ ) ;
351+
352+ // no inner tx hash in the event, get it from the provided tx slice
353+ let block_result = mock_block_result ( None , Some ( "a1aaaa" . to_string ( ) ) ) ;
354+ let wrapper = WrapperTransaction {
355+ exit_code : TransactionExitStatus :: Applied ,
356+ tx_id : Id :: Hash ( "eatshit" . to_string ( ) ) ,
357+ index : 0 ,
358+ fee : Default :: default ( ) ,
359+ atomic : false ,
360+ block_height : 0 ,
361+ total_signatures : 0 ,
362+ size : 0 ,
363+ } ;
364+ let inner = InnerTransaction {
365+ tx_id : Id :: Hash ( "deadbeef" . to_string ( ) ) ,
366+ wrapper_id : Id :: Hash ( "eatshit" . to_string ( ) ) ,
367+ index : 0 ,
368+ kind : TransactionKind :: IbcSendTrasparentTransfer ( (
369+ Token :: Native ( Id :: Hash ( "aabbcc" . to_string ( ) ) ) ,
370+ TransferData {
371+ sources : AccountsMap ( Default :: default ( ) ) ,
372+ targets : AccountsMap ( Default :: default ( ) ) ,
373+ shielded_section_hash : None ,
374+ } ,
375+ ) ) ,
376+ data : None ,
377+ extra_sections : Default :: default ( ) ,
378+ memo : None ,
379+ notes : 0 ,
380+ exit_code : TransactionExitStatus :: Applied ,
381+ } ;
382+ assert_eq ! (
383+ get_ibc_packets( & block_result, & [ ( wrapper, vec![ inner] ) ] ) ,
384+ vec![ expected_seq( Id :: Hash ( "deadbeef" . to_string( ) ) ) ] ,
385+ ) ;
386+ }
387+ }
0 commit comments