Allow processing of BigInt strings within context variable values.

Add tests.
pull/3585/head
derekpierre 2025-03-21 16:20:18 -04:00
parent 46372dde1b
commit 4198aed384
No known key found for this signature in database
3 changed files with 38 additions and 4 deletions

View File

@ -11,7 +11,10 @@ from nucypher.policy.conditions.exceptions import (
InvalidContextVariableData,
RequiredContextVariable,
)
from nucypher.policy.conditions.utils import ConditionProviderManager
from nucypher.policy.conditions.utils import (
ConditionProviderManager,
check_and_convert_big_int_string_to_int,
)
USER_ADDRESS_CONTEXT = ":userAddress"
USER_ADDRESS_EIP4361_EXTERNAL_CONTEXT = ":userAddressExternalEIP4361"
@ -114,6 +117,7 @@ def get_context_value(
try:
# DIRECTIVES are special context vars that will pre-processed by ursula
func = _DIRECTIVES[context_variable]
value = func(providers=providers, **context) # required inputs here
except KeyError:
# fallback for context variable without directive - assume key,value pair
# handles the case for user customized context variables
@ -122,8 +126,9 @@ def get_context_value(
raise RequiredContextVariable(
f'No value provided for unrecognized context variable "{context_variable}"'
)
else:
value = func(providers=providers, **context) # required inputs here
elif isinstance(value, str):
# possible big int value
value = check_and_convert_big_int_string_to_int(value)
return value

View File

@ -19,6 +19,7 @@ from nucypher.policy.conditions.exceptions import (
from nucypher.policy.conditions.lingo import (
ReturnValueTest,
)
from tests.constants import INT256_MIN, UINT256_MAX
INVALID_CONTEXT_PARAM_NAMES = [
":",
@ -89,6 +90,34 @@ def test_resolve_any_context_variables():
assert resolved_return_value.value == resolved_value
@pytest.mark.parametrize(
"value, expected_resolved_value",
[
(":foo", UINT256_MAX),
(":bar", INT256_MIN),
(
[":foo", 12, ":bar", "5555555555", "endWith_n"],
[UINT256_MAX, 12, INT256_MIN, "5555555555", "endWith_n"],
),
(
[":foo", ":foo", 5, [99, [":bar"]]],
[UINT256_MAX, UINT256_MAX, 5, [99, [INT256_MIN]]],
),
],
)
def test_resolve_big_int_context_variables(value, expected_resolved_value):
# bigints have the 'n' suffix
context = {":foo": f"{UINT256_MAX}n", ":bar": f"{INT256_MIN}n"}
# use with parameters
resolved_value = resolve_any_context_variables(value, **context)
assert resolved_value == expected_resolved_value
return_value_test = ReturnValueTest(comparator="==", value=value)
resolved_return_value = return_value_test.with_resolved_context(**context)
assert resolved_return_value.value == resolved_value
@pytest.mark.parametrize(
"value, expected_resolution",
[

View File

@ -1362,7 +1362,7 @@ def test_big_int_inputs_and_outputs(
comparator_value=return_value_test_value, # value set in return value test
comparator="==",
expected_outcome=True,
context_var_testing=ContextVarTest.NO_CONTEXT_VAR_ONLY,
context_var_testing=ContextVarTest.WITH_AND_WITHOUT_CONTEXT_VAR,
)
execute_spy.assert_called_with(