-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy patharc4.py
332 lines (276 loc) · 11.9 KB
/
arc4.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
from algopy import (
Bytes,
UInt64,
subroutine,
urange,
)
from algopy.op import (
btoi,
bzero,
extract,
extract_uint16,
getbit,
itob,
replace,
select_uint64,
setbit_bytes,
substring,
)
UINT16_SIZE = 2
UINT64_SIZE = 8
UINT16_OFFSET = UINT64_SIZE - UINT16_SIZE
@subroutine
def dynamic_array_pop_bit(array: Bytes) -> tuple[Bytes, Bytes]:
"""
Pop the last item from an arc4 dynamic array of arc4 encoded boolean items
array: The bytes for the source array
returns: tuple of (The popped item, The updated bytes for the source array)
"""
array_length = extract_uint16(array, 0)
length_minus_1 = array_length - 1
result = replace(array, 0, extract(itob(length_minus_1), UINT16_OFFSET, 0))
popped_location = length_minus_1 + UINT16_SIZE * 8
popped = setbit_bytes(b"\x00", 0, getbit(result, popped_location))
result = setbit_bytes(result, popped_location, 0)
result = substring(result, 0, UINT16_SIZE + ((length_minus_1 + 7) // 8))
return popped, result
@subroutine
def dynamic_array_pop_fixed_size(array: Bytes, fixed_byte_size: UInt64) -> tuple[Bytes, Bytes]:
"""
Pop the last item from an arc4 dynamic array of fixed sized items
array: The bytes for the source array
returns: tuple of (The popped item, The updated bytes for the source array)
"""
array_length = extract_uint16(array, 0)
length_minus_1 = array_length - 1
result = replace(array, 0, extract(itob(length_minus_1), UINT16_OFFSET, 0))
item_location = result.length - fixed_byte_size
popped = extract(result, item_location, fixed_byte_size)
result = substring(result, 0, item_location)
return popped, result
@subroutine
def dynamic_array_pop_byte_length_head(array: Bytes) -> tuple[Bytes, Bytes]:
"""
Pop the last item from an arc4 dynamic array of items that are prefixed with their length in
bytes, e.g. arc4.String, arc4.DynamicBytes
source: The bytes for the source array
returns: tuple of (The popped item, The updated bytes for the source array)
"""
array_length = extract_uint16(array, 0)
length_minus_1 = array_length - 1
popped_header_offset = length_minus_1 * UINT16_SIZE
head_and_tail = extract(array, UINT16_SIZE, 0)
popped_offset = extract_uint16(head_and_tail, popped_header_offset)
popped = substring(head_and_tail, popped_offset, head_and_tail.length)
head_and_tail = substring(head_and_tail, 0, popped_header_offset) + substring(
head_and_tail, popped_header_offset + 2, popped_offset
)
updated = extract(
itob(length_minus_1), UINT16_OFFSET, UINT16_SIZE
) + recalculate_head_for_elements_with_byte_length_head(
array_head_and_tail=head_and_tail, length=length_minus_1, start_at_index=UInt64(0)
)
return popped, updated
@subroutine
def dynamic_array_pop_dynamic_element(array: Bytes) -> tuple[Bytes, Bytes]:
"""
Pop the last item from an arc4 dynamic array of dynamically sized items
array: The bytes for the source array
returns: tuple of (The popped item, The updated bytes for the source array)
"""
array_length = extract_uint16(array, 0)
length_minus_1 = array_length - 1
popped_header_offset = length_minus_1 * UINT16_SIZE
head_and_tail = extract(array, UINT16_SIZE, 0)
popped_offset = extract_uint16(head_and_tail, popped_header_offset)
popped = substring(head_and_tail, popped_offset, head_and_tail.length)
new_head = Bytes()
for head_offset in urange(0, length_minus_1 * UINT16_SIZE, UINT16_SIZE):
item_offset = extract_uint16(head_and_tail, head_offset)
item_offset -= UINT16_SIZE
new_head += extract(itob(item_offset), UINT16_OFFSET, UINT16_SIZE)
updated = (
extract(itob(length_minus_1), UINT16_OFFSET, UINT16_SIZE)
+ new_head
+ substring(head_and_tail, popped_header_offset + UINT16_SIZE, popped_offset)
)
return popped, updated
@subroutine
def dynamic_array_concat_bits(
*, array: Bytes, new_items_bytes: Bytes, new_items_count: UInt64, is_packed: bool
) -> Bytes:
"""
Concat data to an arc4 dynamic array of arc4 encoded boolean values
array: The bytes for the source array
new_items_bytes: Either the data portion of an arc4 packed array of booleans
or
a sparse array of concatenated arc4 booleans
new_items_count: The count of new items being added
is_packed: True if new_items_bytes represents a packed array, else False
returns: The updated bytes for the source array
"""
array_length = extract_uint16(array, 0)
new_array_length = array_length + new_items_count
new_array_length_b = extract(itob(new_array_length), UINT16_OFFSET, 0)
result = replace(array, 0, new_array_length_b)
current_bytes = (array_length + 7) // 8
required_bytes = (new_array_length + 7) // 8
if current_bytes < required_bytes:
result += bzero(required_bytes - current_bytes)
write_offset = array_length + 8 * UINT16_SIZE
for i in urange(0, new_items_count, UInt64(1) if is_packed else UInt64(8)):
result = setbit_bytes(result, write_offset, getbit(new_items_bytes, i))
write_offset += 1
return result
@subroutine
def dynamic_array_concat_byte_length_head(
array: Bytes, new_items_bytes: Bytes, new_items_count: UInt64
) -> Bytes:
"""
Replace a single item in an arc4 dynamic array of items that are prefixed with
their byte length
array: The bytes of the source array
new_items_bytes: The bytes for all new items, concatenated
new_items_counts: The count of new items being added
returns: The updated bytes for the source array
"""
array_length = extract_uint16(array, 0)
new_length = array_length + new_items_count
header_end = array_length * UINT16_SIZE + 2
return extract(
itob(new_length), UINT16_OFFSET, UINT16_SIZE
) + recalculate_head_for_elements_with_byte_length_head(
array_head_and_tail=(
substring(array, 2, header_end)
+ bzero(new_items_count * UINT16_SIZE)
+ substring(array, header_end, array.length)
+ new_items_bytes
),
length=new_length,
start_at_index=UInt64(0),
)
@subroutine
def dynamic_array_concat_dynamic_element(
*,
array_items_count: UInt64,
array_head_and_tail: Bytes,
new_items_count: UInt64,
new_head_and_tail: Bytes,
) -> Bytes:
new_head = Bytes()
item_offset_adjustment = new_items_count * UINT16_SIZE
for head_offset in urange(0, array_items_count * UINT16_SIZE, UINT16_SIZE):
item_offset = extract_uint16(array_head_and_tail, head_offset)
new_head += extract(itob(item_offset_adjustment + item_offset), UINT16_OFFSET, UINT16_SIZE)
item_offset_adjustment = array_head_and_tail.length
for head_offset in urange(0, new_items_count * UINT16_SIZE, UINT16_SIZE):
item_offset = extract_uint16(new_head_and_tail, head_offset)
new_head += extract(itob(item_offset_adjustment + item_offset), UINT16_OFFSET, UINT16_SIZE)
return (
extract(itob(array_items_count + new_items_count), UINT16_OFFSET, UINT16_SIZE)
+ new_head
+ substring(
array_head_and_tail, array_items_count * UINT16_SIZE, array_head_and_tail.length
)
+ substring(new_head_and_tail, new_items_count * UINT16_SIZE, new_head_and_tail.length)
)
@subroutine
def dynamic_array_replace_byte_length_head(array: Bytes, new_item: Bytes, index: UInt64) -> Bytes:
"""
Replace a single item in an arc4 dynamic array of items that are prefixed with
their byte length
array: The bytes of the source array
new_item: The bytes of the new item to be inserted
index: The index to insert the new item at
array_length: The length of the array
returns: The updated bytes for the source array
"""
size_b = substring(array, 0, UINT16_SIZE)
array_length = btoi(size_b)
return size_b + static_array_replace_byte_length_head(
array_head_and_tail=extract(array, UINT16_SIZE, 0),
new_item=new_item,
index=index,
array_length=array_length,
)
@subroutine
def dynamic_array_replace_dynamic_element(source: Bytes, new_item: Bytes, index: UInt64) -> Bytes:
size_b = substring(source, 0, UINT16_SIZE)
array_length = btoi(size_b)
return size_b + static_array_replace_dynamic_element(
array_head_and_tail=extract(source, UINT16_SIZE, 0),
new_item=new_item,
index=index,
array_length=array_length,
)
@subroutine
def static_array_replace_dynamic_element(
*, array_head_and_tail: Bytes, new_item: Bytes, index: UInt64, array_length: UInt64
) -> Bytes:
original_offset = extract_uint16(array_head_and_tail, index * 2)
next_item_offset = extract_uint16(array_head_and_tail, (index + 1) * 2)
end_of_tail = array_head_and_tail.length
is_before_end = array_length - index - 1
end_offset = select_uint64(end_of_tail, next_item_offset, is_before_end)
original_item_length = end_offset - original_offset
new_item_length = new_item.length
new_head_and_tail = (
substring(array_head_and_tail, 0, original_offset)
+ new_item
+ substring(array_head_and_tail, end_offset, end_of_tail)
)
for head_offset in urange((index + 1) * 2, array_length * 2, 2):
tail_offset = extract_uint16(new_head_and_tail, head_offset)
tail_offset += new_item_length
tail_offset -= original_item_length
tail_offset_bytes = extract(itob(tail_offset), UINT16_OFFSET, UINT16_SIZE)
new_head_and_tail = replace(new_head_and_tail, head_offset, tail_offset_bytes)
return new_head_and_tail
@subroutine
def static_array_replace_byte_length_head(
array_head_and_tail: Bytes, new_item: Bytes, index: UInt64, array_length: UInt64
) -> Bytes:
"""
Replace a single item in an arc4 dynamic array of items that are prefixed with
their byte length
array_head_and_tail: The head and tail bytes of the source array
new_item: The bytes of the new item to be inserted
index: The index to insert the new item at
array_length: The length of the array
returns: The updated bytes for the source array
"""
assert index < array_length, "Index out of bounds"
offset_for_index = extract_uint16(array_head_and_tail, index * UINT16_SIZE)
old_item_length = extract_uint16(array_head_and_tail, offset_for_index)
old_item_end = offset_for_index + old_item_length + UINT16_SIZE
return recalculate_head_for_elements_with_byte_length_head(
array_head_and_tail=substring(array_head_and_tail, 0, offset_for_index)
+ new_item
+ substring(array_head_and_tail, old_item_end, array_head_and_tail.length),
length=array_length,
start_at_index=index,
)
@subroutine
def recalculate_head_for_elements_with_byte_length_head(
array_head_and_tail: Bytes, length: UInt64, start_at_index: UInt64
) -> Bytes:
"""
Recalculates the offset values of an arc4 static array, where each item's head consists of
its length in bytes as uint16
array_data: The static array data
length: The length of the static array
start_at_index: Optionally start at a non-zero index for performance optimisation. The offset
at this index is assumed to be correct if start_at_index is not 0
returns: The updated bytes for the source array
"""
tail_offset = select_uint64(
length * UINT16_SIZE,
extract_uint16(array_head_and_tail, start_at_index * UINT16_SIZE),
start_at_index, # use length * UINT16_SIZE if 0 otherwise inspect head
)
for head_offset in urange(start_at_index * UINT16_SIZE, length * UINT16_SIZE, UINT16_SIZE):
tail_offset_bytes = extract(itob(tail_offset), UINT16_OFFSET, UINT16_SIZE)
array_head_and_tail = replace(array_head_and_tail, head_offset, tail_offset_bytes)
tail_offset += extract_uint16(array_head_and_tail, tail_offset) + UINT16_SIZE
head_offset += UINT16_SIZE
return array_head_and_tail