Repository: stellar/rs-soroban-env
Pull request #1419 (2024-06-05 20:55:36)

Emulate maximum possible nonce expiration in recording auth mode.
### What

Emulate maximum possible nonce expiration in recording auth mode.

### Why

Currently we don't really know what's the intended signature expiration during the auth simulation. Since there is always a potential option for the user to want to customize their signature expiration at the time of signing the auth (even though that option might not currently exist in the wallets), there is really no way to do better than assuming maximum possible expiration and thus maximum possible rent fee. Since the rent fee is refundable, the end user impact is just a slightly higher fee locked before applying the transaction and refunding the unused rent fee portion.

### Known limitations

N/A



Patch:
diff --git a/soroban-env-host/src/auth.rs b/soroban-env-host/src/auth.rs
index 67766a6b..c1ddaa19 100644
--- a/soroban-env-host/src/auth.rs
+++ b/soroban-env-host/src/auth.rs
@@ -1794,7 +1794,13 @@ impl AccountAuthorizationTracker {
         let nonce = if !is_transaction_source_account {
             let random_nonce: i64 =
                 host.with_recording_auth_nonce_prng(|p| Ok(p.gen_range(0..=i64::MAX)))?;
-            host.consume_nonce(address, random_nonce, 0)?;
+            // We use the `max_live_until_ledger` as the nonce lifetime here
+            // in order to account for a maximum possible rent fee (given maximum
+            // possible signature expiration). However, we don't want to actually
+            // store that as nonce expiration ledger in the recording tracker,
+            // as users are able (and encouraged) to customize the signature
+            // expiration after simulation and before signing the auth payload.
+            host.consume_nonce(address, random_nonce, host.max_live_until_ledger()?)?;
             Some((random_nonce, 0))
         } else {
             None


Test patch:
diff --git a/soroban-env-host/observations/20/test__stellar_asset_contract__test_recording_auth_for_stellar_asset_contract.json b/soroban-env-host/observations/20/test__stellar_asset_contract__test_recording_auth_for_stellar_asset_contract.json
index fe014330..1713bfb7 100644
--- a/soroban-env-host/observations/20/test__stellar_asset_contract__test_recording_auth_for_stellar_asset_contract.json
+++ b/soroban-env-host/observations/20/test__stellar_asset_contract__test_recording_auth_for_stellar_asset_contract.json
@@ -125,19 +125,19 @@
   " 123 ret obj_cmp -> Ok(0)": "cpu:262339",
   " 124 ret get_contract_data -> Ok(Address(obj#57))": "cpu:262445, store:3@5a4a3e01/4@f6cc7366, stk:1@6c9072ac",
   " 125 call require_auth(Address(obj#57))": "",
-  " 126 ret require_auth -> Ok(Void)": "cpu:268919, mem:20740, store:3@5a4a3e01/5@2f1e056f, foot:5@b91f63c9, auth:1@88392f86/1@693fa1c6",
+  " 126 ret require_auth -> Ok(Void)": "cpu:268919, mem:20740, store:3@5a4a3e01/5@380c0bfa, foot:5@b91f63c9, auth:1@88392f86/1@693fa1c6",
   " 127 call extend_current_contract_instance_and_code_ttl(U32(103680), U32(120960))": "",
   " 128 call get_ledger_sequence()": "cpu:270081, mem:20876, store:3@5a4a3e01/-, foot:-",
   " 129 ret get_ledger_sequence -> Ok(U32(123))": "",
-  " 130 ret extend_current_contract_instance_and_code_ttl -> Ok(Void)": "cpu:281105, mem:22981, store:3@5a4a3e01/5@268c2d9, foot:5@b91f63c9",
+  " 130 ret extend_current_contract_instance_and_code_ttl -> Ok(Void)": "cpu:281105, mem:22981, store:3@5a4a3e01/5@82ee3aa, foot:5@b91f63c9",
   " 131 call vec_new_from_slice(1)": "cpu:281300",
   " 132 ret vec_new_from_slice -> Ok(Vec(obj#75))": "cpu:282263, mem:23069, objs:-/38@1622152d",
   " 133 call get_contract_data(Vec(obj#75), Instance)": "",
-  " 134 call obj_cmp(Vec(obj#55), Vec(obj#75))": "cpu:282375, store:-/5@268c2d9, stk:-",
+  " 134 call obj_cmp(Vec(obj#55), Vec(obj#75))": "cpu:282375, store:-/5@82ee3aa, stk:-",
   " 135 ret obj_cmp -> Ok(-1)": "cpu:282662",
   " 136 call obj_cmp(Vec(obj#59), Vec(obj#75))": "",
   " 137 ret obj_cmp -> Ok(0)": "cpu:282949",
-  " 138 ret get_contract_data -> Ok(Vec(obj#69))": "cpu:283055, store:3@5a4a3e01/5@268c2d9, stk:1@6c9072ac",
+  " 138 ret get_contract_data -> Ok(Vec(obj#69))": "cpu:283055, store:3@5a4a3e01/5@82ee3aa, stk:1@6c9072ac",
   " 139 call vec_get(Vec(obj#69), U32(0))": "",
   " 140 ret vec_get -> Ok(Symbol(AlphaNum4))": "cpu:283220",
   " 141 call symbol_index_in_strs(Symbol(AlphaNum4), 3)": "",
@@ -155,11 +155,11 @@
   " 153 call vec_new_from_slice(1)": "cpu:285447, mem:23205",
   " 154 ret vec_new_from_slice -> Ok(Vec(obj#77))": "cpu:286410, mem:23293, objs:-/39@f0253e55",
   " 155 call get_contract_data(Vec(obj#77), Instance)": "",
-  " 156 call obj_cmp(Vec(obj#55), Vec(obj#77))": "cpu:286522, store:-/5@268c2d9, stk:-",
+  " 156 call obj_cmp(Vec(obj#55), Vec(obj#77))": "cpu:286522, store:-/5@82ee3aa, stk:-",
   " 157 ret obj_cmp -> Ok(-1)": "cpu:286809",
   " 158 call obj_cmp(Vec(obj#59), Vec(obj#77))": "",
   " 159 ret obj_cmp -> Ok(0)": "cpu:287096",
-  " 160 ret get_contract_data -> Ok(Vec(obj#69))": "cpu:287202, store:3@5a4a3e01/5@268c2d9, stk:1@6c9072ac",
+  " 160 ret get_contract_data -> Ok(Vec(obj#69))": "cpu:287202, store:3@5a4a3e01/5@82ee3aa, stk:1@6c9072ac",
   " 161 call vec_get(Vec(obj#69), U32(0))": "",
   " 162 ret vec_get -> Ok(Symbol(AlphaNum4))": "cpu:287367",
   " 163 call symbol_index_in_strs(Symbol(AlphaNum4), 3)": "",
@@ -174,10 +174,10 @@
   " 172 ret string_len -> Ok(U32(4))": "cpu:288429",
   " 173 call string_copy_to_slice(String(obj#63), U32(0), 4)": "",
   " 174 ret string_copy_to_slice -> Ok(())": "cpu:288532",
-  " 175 call get_contract_data(Symbol(METADATA), Instance)": "cpu:291173, mem:24057, store:3@5a4a3e01/5@8290ce63",
-  " 176 call obj_cmp(Vec(obj#55), Symbol(METADATA))": "cpu:291224, store:-/5@8290ce63, stk:-",
+  " 175 call get_contract_data(Symbol(METADATA), Instance)": "cpu:291173, mem:24057, store:3@5a4a3e01/5@f08cc07",
+  " 176 call obj_cmp(Vec(obj#55), Symbol(METADATA))": "cpu:291224, store:-/5@f08cc07, stk:-",
   " 177 ret obj_cmp -> Ok(1)": "cpu:291346",
-  " 178 ret get_contract_data -> Ok(Map(obj#53))": "cpu:291452, store:3@5a4a3e01/5@8290ce63, stk:1@6c9072ac",
+  " 178 ret get_contract_data -> Ok(Map(obj#53))": "cpu:291452, store:3@5a4a3e01/5@f08cc07, stk:1@6c9072ac",
   " 179 call map_unpack_to_slice(Map(obj#53), 3)": "",
   " 180 ret map_unpack_to_slice -> Ok(Void)": "cpu:291792",
   " 181 call vec_new_from_slice(4)": "",
@@ -185,7 +185,7 @@
   " 183 call contract_event(Vec(obj#79), I128(100))": "",
   " 184 ret contract_event -> Ok(Void)": "cpu:294553, mem:24346, objs:-/41@3b2e302e, evt:1@6c5d4658",
   " 185 pop SAC:2e378f80:mint -> Ok(Void)": "",
-  " 186 call bytes_new_from_slice(32)": "cpu:294660, store:-/5@8290ce63, stk:-, auth:1@88392f86/-",
+  " 186 call bytes_new_from_slice(32)": "cpu:294660, store:-/5@f08cc07, stk:-, auth:1@88392f86/-",
   " 187 ret bytes_new_from_slice -> Ok(Bytes(obj#83))": "cpu:295629, mem:24458, objs:-/42@261e3923",
   " 188 call bytes_new_from_slice(64)": "",
   " 189 ret bytes_new_from_slice -> Ok(Bytes(obj#85))": "cpu:296606, mem:24602, objs:-/43@4e711e4c",
@@ -212,5 +212,5 @@
   " 210 call verify_sig_ed25519(Bytes(obj#83), Bytes(obj#93), Bytes(obj#85))": "",
   " 211 ret verify_sig_ed25519 -> Err(Error(Crypto, InvalidInput))": "cpu:741706",
   " 212 ret call -> Ok(Void)": "cpu:741752, auth:-/-",
-  " 213 end": "cpu:744338, mem:30680, prngs:-/9b4a753, objs:-/50@ed74790e, vm:-/-, evt:1@6c5d4658, store:-/5@8290ce63, foot:5@b91f63c9, stk:-, auth:-/-"
+  " 213 end": "cpu:744338, mem:30680, prngs:-/9b4a753, objs:-/50@ed74790e, vm:-/-, evt:1@6c5d4658, store:-/5@f08cc07, foot:5@b91f63c9, stk:-, auth:-/-"
 }
\ No newline at end of file
diff --git a/soroban-env-host/observations/21/test__stellar_asset_contract__test_recording_auth_for_stellar_asset_contract.json b/soroban-env-host/observations/21/test__stellar_asset_contract__test_recording_auth_for_stellar_asset_contract.json
index fe014330..1713bfb7 100644
--- a/soroban-env-host/observations/21/test__stellar_asset_contract__test_recording_auth_for_stellar_asset_contract.json
+++ b/soroban-env-host/observations/21/test__stellar_asset_contract__test_recording_auth_for_stellar_asset_contract.json
@@ -125,19 +125,19 @@
   " 123 ret obj_cmp -> Ok(0)": "cpu:262339",
   " 124 ret get_contract_data -> Ok(Address(obj#57))": "cpu:262445, store:3@5a4a3e01/4@f6cc7366, stk:1@6c9072ac",
   " 125 call require_auth(Address(obj#57))": "",
-  " 126 ret require_auth -> Ok(Void)": "cpu:268919, mem:20740, store:3@5a4a3e01/5@2f1e056f, foot:5@b91f63c9, auth:1@88392f86/1@693fa1c6",
+  " 126 ret require_auth -> Ok(Void)": "cpu:268919, mem:20740, store:3@5a4a3e01/5@380c0bfa, foot:5@b91f63c9, auth:1@88392f86/1@693fa1c6",
   " 127 call extend_current_contract_instance_and_code_ttl(U32(103680), U32(120960))": "",
   " 128 call get_ledger_sequence()": "cpu:270081, mem:20876, store:3@5a4a3e01/-, foot:-",
   " 129 ret get_ledger_sequence -> Ok(U32(123))": "",
-  " 130 ret extend_current_contract_instance_and_code_ttl -> Ok(Void)": "cpu:281105, mem:22981, store:3@5a4a3e01/5@268c2d9, foot:5@b91f63c9",
+  " 130 ret extend_current_contract_instance_and_code_ttl -> Ok(Void)": "cpu:281105, mem:22981, store:3@5a4a3e01/5@82ee3aa, foot:5@b91f63c9",
   " 131 call vec_new_from_slice(1)": "cpu:281300",
   " 132 ret vec_new_from_slice -> Ok(Vec(obj#75))": "cpu:282263, mem:23069, objs:-/38@1622152d",
   " 133 call get_contract_data(Vec(obj#75), Instance)": "",
-  " 134 call obj_cmp(Vec(obj#55), Vec(obj#75))": "cpu:282375, store:-/5@268c2d9, stk:-",
+  " 134 call obj_cmp(Vec(obj#55), Vec(obj#75))": "cpu:282375, store:-/5@82ee3aa, stk:-",
   " 135 ret obj_cmp -> Ok(-1)": "cpu:282662",
   " 136 call obj_cmp(Vec(obj#59), Vec(obj#75))": "",
   " 137 ret obj_cmp -> Ok(0)": "cpu:282949",
-  " 138 ret get_contract_data -> Ok(Vec(obj#69))": "cpu:283055, store:3@5a4a3e01/5@268c2d9, stk:1@6c9072ac",
+  " 138 ret get_contract_data -> Ok(Vec(obj#69))": "cpu:283055, store:3@5a4a3e01/5@82ee3aa, stk:1@6c9072ac",
   " 139 call vec_get(Vec(obj#69), U32(0))": "",
   " 140 ret vec_get -> Ok(Symbol(AlphaNum4))": "cpu:283220",
   " 141 call symbol_index_in_strs(Symbol(AlphaNum4), 3)": "",
@@ -155,11 +155,11 @@
   " 153 call vec_new_from_slice(1)": "cpu:285447, mem:23205",
   " 154 ret vec_new_from_slice -> Ok(Vec(obj#77))": "cpu:286410, mem:23293, objs:-/39@f0253e55",
   " 155 call get_contract_data(Vec(obj#77), Instance)": "",
-  " 156 call obj_cmp(Vec(obj#55), Vec(obj#77))": "cpu:286522, store:-/5@268c2d9, stk:-",
+  " 156 call obj_cmp(Vec(obj#55), Vec(obj#77))": "cpu:286522, store:-/5@82ee3aa, stk:-",
   " 157 ret obj_cmp -> Ok(-1)": "cpu:286809",
   " 158 call obj_cmp(Vec(obj#59), Vec(obj#77))": "",
   " 159 ret obj_cmp -> Ok(0)": "cpu:287096",
-  " 160 ret get_contract_data -> Ok(Vec(obj#69))": "cpu:287202, store:3@5a4a3e01/5@268c2d9, stk:1@6c9072ac",
+  " 160 ret get_contract_data -> Ok(Vec(obj#69))": "cpu:287202, store:3@5a4a3e01/5@82ee3aa, stk:1@6c9072ac",
   " 161 call vec_get(Vec(obj#69), U32(0))": "",
   " 162 ret vec_get -> Ok(Symbol(AlphaNum4))": "cpu:287367",
   " 163 call symbol_index_in_strs(Symbol(AlphaNum4), 3)": "",
@@ -174,10 +174,10 @@
   " 172 ret string_len -> Ok(U32(4))": "cpu:288429",
   " 173 call string_copy_to_slice(String(obj#63), U32(0), 4)": "",
   " 174 ret string_copy_to_slice -> Ok(())": "cpu:288532",
-  " 175 call get_contract_data(Symbol(METADATA), Instance)": "cpu:291173, mem:24057, store:3@5a4a3e01/5@8290ce63",
-  " 176 call obj_cmp(Vec(obj#55), Symbol(METADATA))": "cpu:291224, store:-/5@8290ce63, stk:-",
+  " 175 call get_contract_data(Symbol(METADATA), Instance)": "cpu:291173, mem:24057, store:3@5a4a3e01/5@f08cc07",
+  " 176 call obj_cmp(Vec(obj#55), Symbol(METADATA))": "cpu:291224, store:-/5@f08cc07, stk:-",
   " 177 ret obj_cmp -> Ok(1)": "cpu:291346",
-  " 178 ret get_contract_data -> Ok(Map(obj#53))": "cpu:291452, store:3@5a4a3e01/5@8290ce63, stk:1@6c9072ac",
+  " 178 ret get_contract_data -> Ok(Map(obj#53))": "cpu:291452, store:3@5a4a3e01/5@f08cc07, stk:1@6c9072ac",
   " 179 call map_unpack_to_slice(Map(obj#53), 3)": "",
   " 180 ret map_unpack_to_slice -> Ok(Void)": "cpu:291792",
   " 181 call vec_new_from_slice(4)": "",
@@ -185,7 +185,7 @@
   " 183 call contract_event(Vec(obj#79), I128(100))": "",
   " 184 ret contract_event -> Ok(Void)": "cpu:294553, mem:24346, objs:-/41@3b2e302e, evt:1@6c5d4658",
   " 185 pop SAC:2e378f80:mint -> Ok(Void)": "",
-  " 186 call bytes_new_from_slice(32)": "cpu:294660, store:-/5@8290ce63, stk:-, auth:1@88392f86/-",
+  " 186 call bytes_new_from_slice(32)": "cpu:294660, store:-/5@f08cc07, stk:-, auth:1@88392f86/-",
   " 187 ret bytes_new_from_slice -> Ok(Bytes(obj#83))": "cpu:295629, mem:24458, objs:-/42@261e3923",
   " 188 call bytes_new_from_slice(64)": "",
   " 189 ret bytes_new_from_slice -> Ok(Bytes(obj#85))": "cpu:296606, mem:24602, objs:-/43@4e711e4c",
@@ -212,5 +212,5 @@
   " 210 call verify_sig_ed25519(Bytes(obj#83), Bytes(obj#93), Bytes(obj#85))": "",
   " 211 ret verify_sig_ed25519 -> Err(Error(Crypto, InvalidInput))": "cpu:741706",
   " 212 ret call -> Ok(Void)": "cpu:741752, auth:-/-",
-  " 213 end": "cpu:744338, mem:30680, prngs:-/9b4a753, objs:-/50@ed74790e, vm:-/-, evt:1@6c5d4658, store:-/5@8290ce63, foot:5@b91f63c9, stk:-, auth:-/-"
+  " 213 end": "cpu:744338, mem:30680, prngs:-/9b4a753, objs:-/50@ed74790e, vm:-/-, evt:1@6c5d4658, store:-/5@f08cc07, foot:5@b91f63c9, stk:-, auth:-/-"
 }
\ No newline at end of file
diff --git a/soroban-env-host/src/test/e2e_tests.rs b/soroban-env-host/src/test/e2e_tests.rs
index 2fd2c823..a11dc671 100644
--- a/soroban-env-host/src/test/e2e_tests.rs
+++ b/soroban-env-host/src/test/e2e_tests.rs
@@ -115,7 +115,7 @@ fn sign_auth_entry(
                     invocation: out.root_invocation.clone(),
                     nonce: creds.nonce,
                     signature_expiration_ledger: ledger_info.sequence_number
-                        + ledger_info.min_temp_entry_ttl
+                        + ledger_info.max_entry_ttl
                         - 1,
                 });
             let signature_payload: [u8; 32] =
@@ -126,7 +126,7 @@ fn sign_auth_entry(
                 .unwrap();
             creds.signature = signer.sign(&dummy_host, &signature_payload);
             creds.signature_expiration_ledger =
-                ledger_info.sequence_number + ledger_info.min_temp_entry_ttl - 1;
+                ledger_info.sequence_number + ledger_info.max_entry_ttl - 1;
         }
     }
     out
diff --git a/soroban-simulation/src/test/simulation.rs b/soroban-simulation/src/test/simulation.rs
index 7f5cb3bb..ba82612c 100644
--- a/soroban-simulation/src/test/simulation.rs
+++ b/soroban-simulation/src/test/simulation.rs
@@ -479,9 +479,9 @@ fn test_simulate_invoke_contract_with_auth() {
 
     let (expected_instructions, expected_read_bytes, expected_resource_fee, expected_mem_bytes) =
         if ledger_info.protocol_version == CURRENT_PROTOCOL_VERSION {
-            (38210172, 7492, 39363, 19104958)
+            (38210172, 7492, 76472, 19104958)
         } else {
-            (41268119, 7540, 42423, 20633919)
+            (41268119, 7540, 79532, 20633919)
         };
 
     assert_eq!(