Skip to content

Commit 3423e93

Browse files
Fix try_ client ignoring allow_non_root_auth flag (#1761)
### What Generated `try_` client methods now check `allow_non_root_auth` and call `mock_all_auths_allowing_non_root_auth()` when set, matching the behavior of their non-`try_` counterparts. A test is added to `token_client.rs` covering this path. ### Why `try_` methods always called `mock_all_auths()`, ignoring the `allow_non_root_auth` flag set via `mock_all_auths_allowing_non_root_auth()`. This caused `try_` variants to silently use different authorization semantics, making non-root auth unexpectedly fail when invoked through `try_` methods. Fixes #1737. Close #1737
1 parent 54b94fa commit 3423e93

39 files changed

+1251
-224
lines changed

soroban-sdk-macros/src/derive_client.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,11 @@ pub fn derive_client_impl(crate_path: &Path, name: &str, fns: &[syn_ext::Fn]) ->
304304
self.env.mock_auths(mock_auths);
305305
}
306306
if self.mock_all_auths {
307-
self.env.mock_all_auths();
307+
if self.allow_non_root_auth {
308+
self.env.mock_all_auths_allowing_non_root_auth();
309+
} else {
310+
self.env.mock_all_auths();
311+
}
308312
}
309313
}
310314
use #crate_path::{IntoVal,FromVal};

soroban-sdk/src/tests/token_client.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,31 @@ fn test_mock_all_auth() {
110110
);
111111

112112
assert_eq!(client.allowance(&from, &spender), 20);
113+
114+
// Also test `try_` with `mock_all_auths_allowing_non_root_auth` succeeds.
115+
let from2 = Address::generate(&env);
116+
assert!(client
117+
.mock_all_auths_allowing_non_root_auth()
118+
.try_approve(&from2, &spender, &30, &200)
119+
.is_ok());
120+
121+
let auths = env.auths();
122+
assert_eq!(
123+
auths,
124+
std::vec![(
125+
from2.clone(),
126+
AuthorizedInvocation {
127+
function: AuthorizedFunction::Contract((
128+
token_contract_id.clone(),
129+
Symbol::new(&env, "approve"),
130+
(&from2, &spender, 30_i128, 200_u32).into_val(&env)
131+
)),
132+
sub_invocations: std::vec![]
133+
}
134+
)]
135+
);
136+
137+
assert_eq!(client.allowance(&from2, &spender), 30);
113138
}
114139

115140
#[test]

soroban-sdk/test_snapshots/tests/token_client/test_mock_all_auth.1.json

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"generators": {
3-
"address": 5,
3+
"address": 6,
44
"nonce": 0,
55
"mux_id": 0
66
},
@@ -57,6 +57,35 @@
5757
}
5858
]
5959
],
60+
[],
61+
[
62+
[
63+
"CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMDR4",
64+
{
65+
"function": {
66+
"contract_fn": {
67+
"contract_address": "CBEPDNVYXQGWB5YUBXKJWYJA7OXTZW5LFLNO5JRRGE6Z6C5OSUZPCCEL",
68+
"function_name": "approve",
69+
"args": [
70+
{
71+
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMDR4"
72+
},
73+
{
74+
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAK3IM"
75+
},
76+
{
77+
"i128": "30"
78+
},
79+
{
80+
"u32": 200
81+
}
82+
]
83+
}
84+
},
85+
"sub_invocations": []
86+
}
87+
]
88+
],
6089
[]
6190
],
6291
"ledger": {
@@ -177,6 +206,26 @@
177206
},
178207
"live_until": 6311999
179208
},
209+
{
210+
"entry": {
211+
"last_modified_ledger_seq": 0,
212+
"data": {
213+
"contract_data": {
214+
"ext": "v0",
215+
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMDR4",
216+
"key": {
217+
"ledger_key_nonce": {
218+
"nonce": "1033654523790656264"
219+
}
220+
},
221+
"durability": "temporary",
222+
"val": "void"
223+
}
224+
},
225+
"ext": "v0"
226+
},
227+
"live_until": 6311999
228+
},
180229
{
181230
"entry": {
182231
"last_modified_ledger_seq": 0,
@@ -238,6 +287,67 @@
238287
},
239288
"live_until": 200
240289
},
290+
{
291+
"entry": {
292+
"last_modified_ledger_seq": 0,
293+
"data": {
294+
"contract_data": {
295+
"ext": "v0",
296+
"contract": "CBEPDNVYXQGWB5YUBXKJWYJA7OXTZW5LFLNO5JRRGE6Z6C5OSUZPCCEL",
297+
"key": {
298+
"vec": [
299+
{
300+
"symbol": "Allowance"
301+
},
302+
{
303+
"map": [
304+
{
305+
"key": {
306+
"symbol": "from"
307+
},
308+
"val": {
309+
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMDR4"
310+
}
311+
},
312+
{
313+
"key": {
314+
"symbol": "spender"
315+
},
316+
"val": {
317+
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAK3IM"
318+
}
319+
}
320+
]
321+
}
322+
]
323+
},
324+
"durability": "temporary",
325+
"val": {
326+
"map": [
327+
{
328+
"key": {
329+
"symbol": "amount"
330+
},
331+
"val": {
332+
"i128": "30"
333+
}
334+
},
335+
{
336+
"key": {
337+
"symbol": "live_until_ledger"
338+
},
339+
"val": {
340+
"u32": 200
341+
}
342+
}
343+
]
344+
}
345+
}
346+
},
347+
"ext": "v0"
348+
},
349+
"live_until": 200
350+
},
241351
{
242352
"entry": {
243353
"last_modified_ledger_seq": 0,

tests-expanded/test_add_i128_tests.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,11 @@ impl<'a> ContractClient<'a> {
219219
self.env.mock_auths(mock_auths);
220220
}
221221
if self.mock_all_auths {
222-
self.env.mock_all_auths();
222+
if self.allow_non_root_auth {
223+
self.env.mock_all_auths_allowing_non_root_auth();
224+
} else {
225+
self.env.mock_all_auths();
226+
}
223227
}
224228
}
225229
use soroban_sdk::{FromVal, IntoVal};

tests-expanded/test_add_u128_tests.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,11 @@ impl<'a> ContractClient<'a> {
219219
self.env.mock_auths(mock_auths);
220220
}
221221
if self.mock_all_auths {
222-
self.env.mock_all_auths();
222+
if self.allow_non_root_auth {
223+
self.env.mock_all_auths_allowing_non_root_auth();
224+
} else {
225+
self.env.mock_all_auths();
226+
}
223227
}
224228
}
225229
use soroban_sdk::{FromVal, IntoVal};

tests-expanded/test_add_u64_tests.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,11 @@ impl<'a> ContractClient<'a> {
219219
self.env.mock_auths(mock_auths);
220220
}
221221
if self.mock_all_auths {
222-
self.env.mock_all_auths();
222+
if self.allow_non_root_auth {
223+
self.env.mock_all_auths_allowing_non_root_auth();
224+
} else {
225+
self.env.mock_all_auths();
226+
}
223227
}
224228
}
225229
use soroban_sdk::{FromVal, IntoVal};

tests-expanded/test_associated_types_contracttrait_tests.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,11 @@ impl<'a> TraitClient<'a> {
301301
self.env.mock_auths(mock_auths);
302302
}
303303
if self.mock_all_auths {
304-
self.env.mock_all_auths();
304+
if self.allow_non_root_auth {
305+
self.env.mock_all_auths_allowing_non_root_auth();
306+
} else {
307+
self.env.mock_all_auths();
308+
}
305309
}
306310
}
307311
use soroban_sdk::{FromVal, IntoVal};
@@ -457,7 +461,11 @@ impl<'a> ContractClient<'a> {
457461
self.env.mock_auths(mock_auths);
458462
}
459463
if self.mock_all_auths {
460-
self.env.mock_all_auths();
464+
if self.allow_non_root_auth {
465+
self.env.mock_all_auths_allowing_non_root_auth();
466+
} else {
467+
self.env.mock_all_auths();
468+
}
461469
}
462470
}
463471
use soroban_sdk::{FromVal, IntoVal};
@@ -630,7 +638,11 @@ impl<'a> ContractClient<'a> {
630638
self.env.mock_auths(mock_auths);
631639
}
632640
if self.mock_all_auths {
633-
self.env.mock_all_auths();
641+
if self.allow_non_root_auth {
642+
self.env.mock_all_auths_allowing_non_root_auth();
643+
} else {
644+
self.env.mock_all_auths();
645+
}
634646
}
635647
}
636648
use soroban_sdk::{FromVal, IntoVal};
@@ -955,7 +967,11 @@ mod test_with_wasm {
955967
self.env.mock_auths(mock_auths);
956968
}
957969
if self.mock_all_auths {
958-
self.env.mock_all_auths();
970+
if self.allow_non_root_auth {
971+
self.env.mock_all_auths_allowing_non_root_auth();
972+
} else {
973+
self.env.mock_all_auths();
974+
}
959975
}
960976
}
961977
use soroban_sdk::{FromVal, IntoVal};
@@ -1036,7 +1052,11 @@ mod test_with_wasm {
10361052
self.env.mock_auths(mock_auths);
10371053
}
10381054
if self.mock_all_auths {
1039-
self.env.mock_all_auths();
1055+
if self.allow_non_root_auth {
1056+
self.env.mock_all_auths_allowing_non_root_auth();
1057+
} else {
1058+
self.env.mock_all_auths();
1059+
}
10401060
}
10411061
}
10421062
use soroban_sdk::{FromVal, IntoVal};

tests-expanded/test_associated_types_tests.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,11 @@ impl<'a> ContractClient<'a> {
231231
self.env.mock_auths(mock_auths);
232232
}
233233
if self.mock_all_auths {
234-
self.env.mock_all_auths();
234+
if self.allow_non_root_auth {
235+
self.env.mock_all_auths_allowing_non_root_auth();
236+
} else {
237+
self.env.mock_all_auths();
238+
}
235239
}
236240
}
237241
use soroban_sdk::{FromVal, IntoVal};

tests-expanded/test_auth_tests.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,11 @@ impl<'a> ContractAClient<'a> {
216216
self.env.mock_auths(mock_auths);
217217
}
218218
if self.mock_all_auths {
219-
self.env.mock_all_auths();
219+
if self.allow_non_root_auth {
220+
self.env.mock_all_auths_allowing_non_root_auth();
221+
} else {
222+
self.env.mock_all_auths();
223+
}
220224
}
221225
}
222226
use soroban_sdk::{FromVal, IntoVal};
@@ -1596,7 +1600,11 @@ impl<'a> ContractBClient<'a> {
15961600
self.env.mock_auths(mock_auths);
15971601
}
15981602
if self.mock_all_auths {
1599-
self.env.mock_all_auths();
1603+
if self.allow_non_root_auth {
1604+
self.env.mock_all_auths_allowing_non_root_auth();
1605+
} else {
1606+
self.env.mock_all_auths();
1607+
}
16001608
}
16011609
}
16021610
use soroban_sdk::{FromVal, IntoVal};

tests-expanded/test_bls_tests.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,11 @@ impl<'a> ContractClient<'a> {
856856
self.env.mock_auths(mock_auths);
857857
}
858858
if self.mock_all_auths {
859-
self.env.mock_all_auths();
859+
if self.allow_non_root_auth {
860+
self.env.mock_all_auths_allowing_non_root_auth();
861+
} else {
862+
self.env.mock_all_auths();
863+
}
860864
}
861865
}
862866
use soroban_sdk::{FromVal, IntoVal};
@@ -945,7 +949,11 @@ impl<'a> ContractClient<'a> {
945949
self.env.mock_auths(mock_auths);
946950
}
947951
if self.mock_all_auths {
948-
self.env.mock_all_auths();
952+
if self.allow_non_root_auth {
953+
self.env.mock_all_auths_allowing_non_root_auth();
954+
} else {
955+
self.env.mock_all_auths();
956+
}
949957
}
950958
}
951959
use soroban_sdk::{FromVal, IntoVal};
@@ -1020,7 +1028,11 @@ impl<'a> ContractClient<'a> {
10201028
self.env.mock_auths(mock_auths);
10211029
}
10221030
if self.mock_all_auths {
1023-
self.env.mock_all_auths();
1031+
if self.allow_non_root_auth {
1032+
self.env.mock_all_auths_allowing_non_root_auth();
1033+
} else {
1034+
self.env.mock_all_auths();
1035+
}
10241036
}
10251037
}
10261038
use soroban_sdk::{FromVal, IntoVal};
@@ -1092,7 +1104,11 @@ impl<'a> ContractClient<'a> {
10921104
self.env.mock_auths(mock_auths);
10931105
}
10941106
if self.mock_all_auths {
1095-
self.env.mock_all_auths();
1107+
if self.allow_non_root_auth {
1108+
self.env.mock_all_auths_allowing_non_root_auth();
1109+
} else {
1110+
self.env.mock_all_auths();
1111+
}
10961112
}
10971113
}
10981114
use soroban_sdk::{FromVal, IntoVal};

0 commit comments

Comments
 (0)