-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlocal_state.py
287 lines (251 loc) · 10 KB
/
local_state.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import abc
import typing
from collections.abc import Sequence
import mypy.nodes
from puya import log
from puya.awst import wtypes
from puya.awst.nodes import (
AppAccountStateExpression,
Expression,
ExpressionStatement,
IntegerConstant,
StateDelete,
StateExists,
StateGet,
StateGetEx,
Statement,
)
from puya.errors import CodeError
from puya.parse import SourceLocation
from puyapy.awst_build import pytypes
from puyapy.awst_build.eb import _expect as expect
from puyapy.awst_build.eb._base import FunctionBuilder, GenericTypeBuilder
from puyapy.awst_build.eb._bytes_backed import BytesBackedInstanceExpressionBuilder
from puyapy.awst_build.eb._utils import dummy_value
from puyapy.awst_build.eb.bool import BoolExpressionBuilder
from puyapy.awst_build.eb.factories import builder_for_instance
from puyapy.awst_build.eb.interface import (
InstanceBuilder,
NodeBuilder,
StorageProxyConstructorArgs,
StorageProxyConstructorResult,
TypeBuilder,
)
from puyapy.awst_build.eb.storage._storage import (
StorageProxyDefinitionBuilder,
parse_storage_proxy_constructor_args,
)
from puyapy.awst_build.eb.storage._value_proxy import ValueProxyExpressionBuilder
from puyapy.awst_build.eb.tuple import TupleExpressionBuilder
from puyapy.awst_build.utils import get_arg_mapping
logger = log.get_logger(__name__)
class LocalStateTypeBuilder(TypeBuilder[pytypes.StorageProxyType]):
def __init__(self, typ: pytypes.PyType, location: SourceLocation) -> None:
assert isinstance(typ, pytypes.StorageProxyType)
assert typ.generic == pytypes.GenericLocalStateType
self._typ = typ
super().__init__(typ, location)
@typing.override
def call(
self,
args: Sequence[NodeBuilder],
arg_kinds: list[mypy.nodes.ArgKind],
arg_names: list[str | None],
location: SourceLocation,
) -> InstanceBuilder:
return _init(args, arg_names, location, result_type=self._typ)
class LocalStateGenericTypeBuilder(GenericTypeBuilder):
@typing.override
def call(
self,
args: Sequence[NodeBuilder],
arg_kinds: list[mypy.nodes.ArgKind],
arg_names: list[str | None],
location: SourceLocation,
) -> InstanceBuilder:
return _init(args, arg_names, location, result_type=None)
def _init(
args: Sequence[NodeBuilder],
arg_names: list[str | None],
location: SourceLocation,
*,
result_type: pytypes.StorageProxyType | None,
) -> InstanceBuilder:
type_arg_name = "type_"
key_arg_name = "key"
descr_arg_name = "description"
arg_mapping, _ = get_arg_mapping(
required_positional_names=[type_arg_name],
optional_kw_only=[key_arg_name, descr_arg_name],
args=args,
arg_names=arg_names,
call_location=location,
raise_on_missing=True,
)
match arg_mapping[type_arg_name].pytype:
case pytypes.TypeType(typ=content):
pass
case _:
raise CodeError("first argument must be a type reference", location)
if result_type is None:
result_type = pytypes.GenericLocalStateType.parameterise([content], location)
elif result_type.content != content:
logger.error(
"explicit type annotation does not match first argument"
" - suggest to remove the explicit type annotation, it shouldn't be required",
location=location,
)
typed_args = parse_storage_proxy_constructor_args(
arg_mapping,
key_wtype=wtypes.state_key,
key_arg_name=key_arg_name,
descr_arg_name=descr_arg_name,
location=location,
)
if typed_args.key is None:
return StorageProxyDefinitionBuilder(typed_args, result_type, location)
return _LocalStateExpressionBuilderFromConstructor(typed_args, result_type)
class LocalStateExpressionBuilder(
BytesBackedInstanceExpressionBuilder[pytypes.StorageProxyType], bytes_member="key"
):
def __init__(self, expr: Expression, typ: pytypes.PyType, member_name: str | None = None):
assert isinstance(typ, pytypes.StorageProxyType)
assert typ.generic == pytypes.GenericLocalStateType
self.member_name: typing.Final = member_name
super().__init__(typ, expr)
@typing.override
def index(self, index: InstanceBuilder, location: SourceLocation) -> InstanceBuilder:
expr = _build_field(self, index, location)
return _Value(self.pytype.content, expr)
@typing.override
def contains(self, item: InstanceBuilder, location: SourceLocation) -> InstanceBuilder:
exists_expr = StateExists(
field=_build_field(self, item, location), source_location=location
)
return BoolExpressionBuilder(exists_expr)
@typing.override
def member_access(self, name: str, location: SourceLocation) -> NodeBuilder:
match name:
case "get":
return _Get(self, location)
case "maybe":
return _Maybe(self, location)
return super().member_access(name, location)
@typing.override
def iterate(self) -> typing.Never:
raise CodeError("cannot iterate account states", self.source_location)
@typing.override
def iterable_item_type(self) -> typing.Never:
self.iterate()
@typing.override
def slice_index(
self,
begin_index: InstanceBuilder | None,
end_index: InstanceBuilder | None,
stride: InstanceBuilder | None,
location: SourceLocation,
) -> InstanceBuilder:
raise CodeError("cannot slice account states", self.source_location)
@typing.override
def bool_eval(self, location: SourceLocation, *, negate: bool = False) -> InstanceBuilder:
raise CodeError("cannot determine if a LocalState is empty or not", location)
def _build_field(
self: LocalStateExpressionBuilder,
index: NodeBuilder,
location: SourceLocation,
) -> AppAccountStateExpression:
# TODO: maybe resolve literal should allow functions, so we can validate
# constant values inside e.g. conditional expressions, not just plain constants
# like we check below with matching on IntegerConstant
index_expr = expect.argument_of_type_else_dummy(
index,
# UInt64 comes first since we want to resolve int literals,
# str literals for account not currently supported
pytypes.UInt64Type,
pytypes.AccountType,
resolve_literal=True,
).resolve()
# https://developer.algorand.org/docs/get-details/dapps/smart-contracts/apps/#resource-availability
# Note that the sender address is implicitly included in the array,
# but doesn't count towards the limit of 4, so the <= 4 below is correct
# and intended
if isinstance(index_expr, IntegerConstant) and not (0 <= index_expr.value <= 4):
logger.error(
"account index should be between 0 and 4 inclusive",
location=index.source_location,
)
if self.member_name:
exists_assertion_message = f"check self.{self.member_name} exists for account"
else:
exists_assertion_message = "check LocalState exists for account"
return AppAccountStateExpression(
key=self.resolve(),
account=index_expr,
wtype=self.pytype.content_wtype,
exists_assertion_message=exists_assertion_message,
source_location=location,
)
class _LocalStateExpressionBuilderFromConstructor(
LocalStateExpressionBuilder, StorageProxyConstructorResult
):
def __init__(self, args: StorageProxyConstructorArgs, typ: pytypes.StorageProxyType):
assert args.key is not None
super().__init__(args.key, typ, member_name=None)
self._args = args
@typing.override
@property
def args(self) -> StorageProxyConstructorArgs:
return self._args
class _MemberFunction(FunctionBuilder, abc.ABC):
def __init__(self, base: LocalStateExpressionBuilder, location: SourceLocation):
super().__init__(location)
self._base = base
class _Get(_MemberFunction):
@typing.override
def call(
self,
args: Sequence[NodeBuilder],
arg_kinds: list[mypy.nodes.ArgKind],
arg_names: list[str | None],
location: SourceLocation,
) -> InstanceBuilder:
content_typ = self._base.pytype.content
key_arg_name = "key"
default_arg_name = "default"
args_map, any_missing = get_arg_mapping(
required_positional_names=[key_arg_name, default_arg_name],
args=args,
arg_names=arg_names,
call_location=location,
raise_on_missing=False,
)
if any_missing:
return dummy_value(content_typ, location)
item = args_map[key_arg_name]
default_arg = expect.argument_of_type_else_dummy(args_map[default_arg_name], content_typ)
key = _build_field(self._base, item, location)
default = default_arg.resolve()
expr = StateGet(field=key, default=default, source_location=location)
return builder_for_instance(content_typ, expr)
class _Maybe(_MemberFunction):
@typing.override
def call(
self,
args: Sequence[NodeBuilder],
arg_kinds: list[mypy.nodes.ArgKind],
arg_names: list[str | None],
location: SourceLocation,
) -> InstanceBuilder:
content_typ = self._base.pytype.content
result_typ = pytypes.GenericTupleType.parameterise(
[content_typ, pytypes.BoolType], location
)
arg = expect.exactly_one_arg(args, location, default=expect.default_fixed_value(None))
if arg is None:
return dummy_value(result_typ, location)
field = _build_field(self._base, arg, location)
app_local_get_ex = StateGetEx(field=field, source_location=location)
return TupleExpressionBuilder(app_local_get_ex, result_typ)
class _Value(ValueProxyExpressionBuilder[pytypes.PyType, AppAccountStateExpression]):
def delete(self, location: SourceLocation) -> Statement:
return ExpressionStatement(StateDelete(field=self.resolve(), source_location=location))