@@ -3,7 +3,9 @@ use std::io::Write;
33
44use borsh:: BorshDeserialize ;
55use borsh_ext:: BorshSerializeExt ;
6+ use color_eyre:: owo_colors:: OwoColorize ;
67use ledger_namada_rs:: { BIP44Path , NamadaApp } ;
8+ use namada_core:: masp:: MaspTransaction ;
79use namada_sdk:: address:: { Address , ImplicitAddress } ;
810use namada_sdk:: args:: TxBecomeValidator ;
911use namada_sdk:: collections:: HashSet ;
@@ -829,13 +831,33 @@ pub async fn submit_shielded_transfer(
829831 namada : & impl Namada ,
830832 args : args:: TxShieldedTransfer ,
831833) -> Result < ( ) , error:: Error > {
834+ display_line ! (
835+ namada. io( ) ,
836+ "{}: {}\n " ,
837+ "WARNING" . bold( ) . underline( ) . yellow( ) ,
838+ "Some information might be leaked if your shielded wallet is not up \
839+ to date, make sure to run `namadac shielded-sync` before running \
840+ this command.",
841+ ) ;
842+
832843 let ( mut tx, signing_data) = args. clone ( ) . build ( namada) . await ?;
833844
845+ let masp_section = tx
846+ . sections
847+ . iter ( )
848+ . find_map ( |section| section. masp_tx ( ) )
849+ . ok_or_else ( || {
850+ error:: Error :: Other (
851+ "Missing MASP section in shielded transaction" . to_string ( ) ,
852+ )
853+ } ) ?;
834854 if args. tx . dump_tx || args. tx . dump_wrapper_tx {
835855 tx:: dump_tx ( namada. io ( ) , & args. tx , tx) ?;
856+ pre_cache_masp_data ( namada, & masp_section) . await ;
836857 } else {
837858 sign ( namada, & mut tx, & args. tx , signing_data) . await ?;
838- namada. submit ( tx, & args. tx ) . await ?;
859+ let res = namada. submit ( tx, & args. tx ) . await ?;
860+ pre_cache_masp_data_on_tx_result ( namada, & res, & masp_section) . await ;
839861 }
840862 Ok ( ( ) )
841863}
@@ -900,13 +922,33 @@ pub async fn submit_unshielding_transfer(
900922 namada : & impl Namada ,
901923 args : args:: TxUnshieldingTransfer ,
902924) -> Result < ( ) , error:: Error > {
925+ display_line ! (
926+ namada. io( ) ,
927+ "{}: {}\n " ,
928+ "WARNING" . bold( ) . underline( ) . yellow( ) ,
929+ "Some information might be leaked if your shielded wallet is not up \
930+ to date, make sure to run `namadac shielded-sync` before running \
931+ this command.",
932+ ) ;
933+
903934 let ( mut tx, signing_data) = args. clone ( ) . build ( namada) . await ?;
904935
936+ let masp_section = tx
937+ . sections
938+ . iter ( )
939+ . find_map ( |section| section. masp_tx ( ) )
940+ . ok_or_else ( || {
941+ error:: Error :: Other (
942+ "Missing MASP section in shielded transaction" . to_string ( ) ,
943+ )
944+ } ) ?;
905945 if args. tx . dump_tx || args. tx . dump_wrapper_tx {
906946 tx:: dump_tx ( namada. io ( ) , & args. tx , tx) ?;
947+ pre_cache_masp_data ( namada, & masp_section) . await ;
907948 } else {
908949 sign ( namada, & mut tx, & args. tx , signing_data) . await ?;
909- namada. submit ( tx, & args. tx ) . await ?;
950+ let res = namada. submit ( tx, & args. tx ) . await ?;
951+ pre_cache_masp_data_on_tx_result ( namada, & res, & masp_section) . await ;
910952 }
911953 Ok ( ( ) )
912954}
@@ -920,16 +962,25 @@ where
920962{
921963 let ( tx, signing_data, _) = args. build ( namada) . await ?;
922964
965+ let opt_masp_section =
966+ tx. sections . iter ( ) . find_map ( |section| section. masp_tx ( ) ) ;
923967 if args. tx . dump_tx || args. tx . dump_wrapper_tx {
924968 tx:: dump_tx ( namada. io ( ) , & args. tx , tx) ?;
969+ if let Some ( masp_section) = opt_masp_section {
970+ pre_cache_masp_data ( namada, & masp_section) . await ;
971+ }
925972 } else {
926- batch_opt_reveal_pk_and_submit (
973+ let res = batch_opt_reveal_pk_and_submit (
927974 namada,
928975 & args. tx ,
929976 & [ & args. source . effective_address ( ) ] ,
930977 ( tx, signing_data) ,
931978 )
932979 . await ?;
980+
981+ if let Some ( masp_section) = opt_masp_section {
982+ pre_cache_masp_data_on_tx_result ( namada, & res, & masp_section) . await ;
983+ }
933984 }
934985 // NOTE that the tx could fail when its submission epoch doesn't match
935986 // construction epoch
@@ -1482,3 +1533,42 @@ pub async fn gen_ibc_shielding_transfer(
14821533 }
14831534 Ok ( ( ) )
14841535}
1536+
1537+ // Pre-cache the data for the provided MASP transaction. Log an error on
1538+ // failure.
1539+ async fn pre_cache_masp_data ( namada : & impl Namada , masp_tx : & MaspTransaction ) {
1540+ if let Err ( e) = namada
1541+ . shielded_mut ( )
1542+ . await
1543+ . pre_cache_transaction ( masp_tx)
1544+ . await
1545+ {
1546+ // Just display the error but do not propagate it
1547+ edisplay_line ! ( namada. io( ) , "Failed to pre-cache masp data: {}." , e) ;
1548+ }
1549+ }
1550+
1551+ // Check the result of a transaction and pre-cache the masp data accordingly
1552+ async fn pre_cache_masp_data_on_tx_result (
1553+ namada : & impl Namada ,
1554+ tx_result : & ProcessTxResponse ,
1555+ masp_tx : & MaspTransaction ,
1556+ ) {
1557+ match tx_result {
1558+ ProcessTxResponse :: Applied ( resp) => {
1559+ if let Some ( InnerTxResult :: Success ( _) ) =
1560+ // If we have the masp data in an ibc transfer it
1561+ // means we are unshielding, so there's no reveal pk
1562+ // tx in the batch which contains only the ibc tx
1563+ resp. batch_result ( ) . first ( ) . map ( |( _, res) | res)
1564+ {
1565+ pre_cache_masp_data ( namada, masp_tx) . await ;
1566+ }
1567+ }
1568+ ProcessTxResponse :: Broadcast ( _) => {
1569+ pre_cache_masp_data ( namada, masp_tx) . await ;
1570+ }
1571+ // Do not pre-cache when dry-running
1572+ ProcessTxResponse :: DryRun ( _) => { }
1573+ }
1574+ }
0 commit comments