-
Notifications
You must be signed in to change notification settings - Fork 11
/
app_deployer.py
608 lines (513 loc) · 22.5 KB
/
app_deployer.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
import base64
import dataclasses
import json
from dataclasses import asdict, dataclass
from enum import Enum
from typing import Literal
from algosdk.logic import get_application_address
from algosdk.v2client.indexer import IndexerClient
from algokit_utils.applications.app_manager import AppManager
from algokit_utils.config import config
from algokit_utils.models.state import TealTemplateParams
from algokit_utils.transactions.transaction_composer import (
AppCreateMethodCallParams,
AppCreateParams,
AppDeleteMethodCallParams,
AppDeleteParams,
AppUpdateMethodCallParams,
AppUpdateParams,
)
from algokit_utils.transactions.transaction_sender import (
AlgorandClientTransactionSender,
SendAppCreateTransactionResult,
SendAppTransactionResult,
SendAppUpdateTransactionResult,
)
__all__ = [
"APP_DEPLOY_NOTE_DAPP",
"AppDeployMetaData",
"AppDeployParams",
"AppDeployResponse",
"AppDeployer",
"AppLookup",
"AppMetaData",
"AppReference",
"OnSchemaBreak",
"OnUpdate",
"OperationPerformed",
]
APP_DEPLOY_NOTE_DAPP: str = "ALGOKIT_DEPLOYER"
logger = config.logger
@dataclasses.dataclass
class AppReference:
"""Information about an Algorand app"""
app_id: int
app_address: str
@dataclasses.dataclass
class AppDeployMetaData:
"""Metadata about an application stored in a transaction note during creation.
The note is serialized as JSON and prefixed with {py:data}`NOTE_PREFIX` and stored in the transaction note field
as part of {py:meth}`ApplicationClient.deploy`
"""
name: str
version: str
deletable: bool | None
updatable: bool | None
@dataclasses.dataclass
class AppMetaData(AppReference, AppDeployMetaData):
"""Metadata about a deployed app"""
created_round: int
updated_round: int
created_metadata: AppDeployMetaData
deleted: bool
@dataclasses.dataclass
class AppLookup:
"""Cache of {py:class}`AppMetaData` for a specific `creator`
Can be used as an argument to {py:class}`ApplicationClient` to reduce the number of calls when deploying multiple
apps or discovering multiple app_ids
"""
creator: str
apps: dict[str, AppMetaData] = dataclasses.field(default_factory=dict)
class OnSchemaBreak(str, Enum):
"""Action to take if an Application's schema has breaking changes"""
Fail = "fail"
"""Fail the deployment"""
ReplaceApp = "replace_app"
"""Create a new Application and delete the old Application in a single transaction"""
AppendApp = "append_app"
"""Create a new Application"""
class OnUpdate(str, Enum):
"""Action to take if an Application has been updated"""
Fail = "fail"
"""Fail the deployment"""
UpdateApp = "update_app"
"""Update the Application with the new approval and clear programs"""
ReplaceApp = "replace_app"
"""Create a new Application and delete the old Application in a single transaction"""
AppendApp = "append_app"
"""Create a new application"""
class OperationPerformed(str, Enum):
"""Describes the actions taken during deployment"""
Nothing = "nothing"
"""An existing Application was found"""
Create = "create"
"""No existing Application was found, created a new Application"""
Update = "update"
"""An existing Application was found, but was out of date, updated to latest version"""
Replace = "replace"
"""An existing Application was found, but was out of date, created a new Application and deleted the original"""
@dataclass(kw_only=True)
class AppDeployParams:
"""Parameters for deploying an app"""
metadata: AppDeployMetaData
deploy_time_params: TealTemplateParams | None = None
on_schema_break: Literal["replace", "fail", "append"] | OnSchemaBreak = OnSchemaBreak.Fail
on_update: Literal["update", "replace", "fail", "append"] | OnUpdate = OnUpdate.Fail
create_params: AppCreateParams | AppCreateMethodCallParams
update_params: AppUpdateParams | AppUpdateMethodCallParams
delete_params: AppDeleteParams | AppDeleteMethodCallParams
existing_deployments: AppLookup | None = None
ignore_cache: bool = False
max_fee: int | None = None
max_rounds_to_wait: int | None = None
suppress_log: bool = False
populate_app_call_resources: bool = False
# Union type for all possible deploy results
@dataclass(frozen=True)
class AppDeployResponse:
app: AppMetaData
operation_performed: OperationPerformed
create_response: SendAppCreateTransactionResult | None = None
update_response: SendAppUpdateTransactionResult | None = None
delete_response: SendAppTransactionResult | None = None
class AppDeployer:
"""Manages deployment and deployment metadata of applications"""
def __init__(
self,
app_manager: AppManager,
transaction_sender: AlgorandClientTransactionSender,
indexer: IndexerClient | None = None,
):
self._app_manager = app_manager
self._transaction_sender = transaction_sender
self._indexer = indexer
self._app_lookups: dict[str, AppLookup] = {}
def _create_deploy_note(self, metadata: AppDeployMetaData) -> bytes:
note = {
"dapp_name": APP_DEPLOY_NOTE_DAPP,
"format": "j",
"data": metadata.__dict__,
}
return json.dumps(note).encode()
def deploy(self, deployment: AppDeployParams) -> AppDeployResponse:
# Create new instances with updated notes
logger.info(
f"Idempotently deploying app \"{deployment.metadata.name}\" from creator "
f"{deployment.create_params.sender} using {len(deployment.create_params.approval_program)} bytes of "
f"{'teal code' if isinstance(deployment.create_params.approval_program, str) else 'AVM bytecode'} and "
f"{len(deployment.create_params.clear_state_program)} bytes of "
f"{'teal code' if isinstance(deployment.create_params.clear_state_program, str) else 'AVM bytecode'}",
suppress_log=deployment.suppress_log,
)
note = self._create_deploy_note(deployment.metadata)
create_params = dataclasses.replace(deployment.create_params, note=note)
update_params = dataclasses.replace(deployment.update_params, note=note)
deployment = dataclasses.replace(
deployment,
create_params=create_params,
update_params=update_params,
)
# Validate inputs
if (
deployment.existing_deployments
and deployment.existing_deployments.creator != deployment.create_params.sender
):
raise ValueError(
f"Received invalid existingDeployments value for creator "
f"{deployment.existing_deployments.creator} when attempting to deploy "
f"for creator {deployment.create_params.sender}"
)
if not deployment.existing_deployments and not self._indexer:
raise ValueError(
"Didn't receive an indexer client when this AppManager was created, "
"but also didn't receive an existingDeployments cache - one of them must be provided"
)
# Compile code if needed
approval_program = deployment.create_params.approval_program
clear_program = deployment.create_params.clear_state_program
if isinstance(approval_program, str):
compiled_approval = self._app_manager.compile_teal_template(
approval_program,
deployment.deploy_time_params,
deployment.metadata.__dict__,
)
approval_program = compiled_approval.compiled_base64_to_bytes
if isinstance(clear_program, str):
compiled_clear = self._app_manager.compile_teal_template(
clear_program,
deployment.deploy_time_params,
)
clear_program = compiled_clear.compiled_base64_to_bytes
# Get existing app metadata
apps = deployment.existing_deployments or self.get_creator_apps_by_name(
creator_address=deployment.create_params.sender,
ignore_cache=deployment.ignore_cache,
)
existing_app = apps.apps.get(deployment.metadata.name)
if not existing_app or existing_app.deleted:
return self._create_app(
deployment=deployment,
approval_program=approval_program,
clear_program=clear_program,
)
# Check for changes
existing_app_record = self._app_manager.get_by_id(existing_app.app_id)
existing_approval = base64.b64encode(existing_app_record.approval_program).decode()
existing_clear = base64.b64encode(existing_app_record.clear_state_program).decode()
new_approval = base64.b64encode(approval_program).decode()
new_clear = base64.b64encode(clear_program).decode()
is_update = new_approval != existing_approval or new_clear != existing_clear
is_schema_break = (
existing_app_record.local_ints
< (deployment.create_params.schema.get("local_ints", 0) if deployment.create_params.schema else 0)
or existing_app_record.global_ints
< (deployment.create_params.schema.get("global_ints", 0) if deployment.create_params.schema else 0)
or existing_app_record.local_byte_slices
< (deployment.create_params.schema.get("local_byte_slices", 0) if deployment.create_params.schema else 0)
or existing_app_record.global_byte_slices
< (deployment.create_params.schema.get("global_byte_slices", 0) if deployment.create_params.schema else 0)
)
if is_schema_break:
logger.warning(
f"Detected a breaking app schema change in app {existing_app.app_id}:",
extra={
"from": {
"global_ints": existing_app_record.global_ints,
"global_byte_slices": existing_app_record.global_byte_slices,
"local_ints": existing_app_record.local_ints,
"local_byte_slices": existing_app_record.local_byte_slices,
},
"to": deployment.create_params.schema,
},
suppress_log=deployment.suppress_log,
)
return self._handle_schema_break(
deployment=deployment,
existing_app=existing_app,
approval_program=approval_program,
clear_program=clear_program,
)
if is_update:
return self._handle_update(
deployment=deployment,
existing_app=existing_app,
approval_program=approval_program,
clear_program=clear_program,
)
logger.debug("No detected changes in app, nothing to do.", suppress_log=deployment.suppress_log)
return AppDeployResponse(
app=existing_app,
operation_performed=OperationPerformed.Nothing,
)
def _create_app(
self,
deployment: AppDeployParams,
approval_program: bytes,
clear_program: bytes,
) -> AppDeployResponse:
"""Create a new application"""
if isinstance(deployment.create_params, AppCreateMethodCallParams):
create_response = self._transaction_sender.app_create_method_call(
AppCreateMethodCallParams(
**{
**asdict(deployment.create_params),
"approval_program": approval_program,
"clear_state_program": clear_program,
}
)
)
else:
create_response = self._transaction_sender.app_create(
AppCreateParams(
**{
**asdict(deployment.create_params),
"approval_program": approval_program,
"clear_state_program": clear_program,
}
)
)
app_metadata = AppMetaData(
app_id=create_response.app_id,
app_address=get_application_address(create_response.app_id),
**asdict(deployment.metadata),
created_metadata=deployment.metadata,
created_round=create_response.confirmation.get("confirmed-round", 0)
if isinstance(create_response.confirmation, dict)
else 0,
updated_round=create_response.confirmation.get("confirmed-round", 0)
if isinstance(create_response.confirmation, dict)
else 0,
deleted=False,
)
self._update_app_lookup(deployment.create_params.sender, app_metadata)
return AppDeployResponse(
app=app_metadata,
operation_performed=OperationPerformed.Create,
create_response=create_response,
)
def _replace_app(
self,
deployment: AppDeployParams,
existing_app: AppMetaData,
approval_program: bytes,
clear_program: bytes,
) -> AppDeployResponse:
composer = self._transaction_sender.new_group()
# Add create transaction
if isinstance(deployment.create_params, AppCreateMethodCallParams):
composer.add_app_create_method_call(
AppCreateMethodCallParams(
**{
**deployment.create_params.__dict__,
"approval_program": approval_program,
"clear_state_program": clear_program,
}
)
)
else:
composer.add_app_create(
AppCreateParams(
**{
**deployment.create_params.__dict__,
"approval_program": approval_program,
"clear_state_program": clear_program,
}
)
)
create_txn_index = composer.count() - 1
# Add delete transaction
if isinstance(deployment.delete_params, AppDeleteMethodCallParams):
delete_call_params = AppDeleteMethodCallParams(
**{
**deployment.delete_params.__dict__,
"app_id": existing_app.app_id,
}
)
composer.add_app_delete_method_call(delete_call_params)
else:
delete_params = AppDeleteParams(
**{
**deployment.delete_params.__dict__,
"app_id": existing_app.app_id,
}
)
composer.add_app_delete(delete_params)
delete_txn_index = composer.count() - 1
result = composer.send()
create_response = SendAppCreateTransactionResult.from_composer_result(result, create_txn_index)
delete_response = SendAppTransactionResult.from_composer_result(result, delete_txn_index)
app_id = int(result.confirmations[0]["application-index"]) # type: ignore[call-overload]
app_metadata = AppMetaData(
app_id=app_id,
app_address=get_application_address(app_id),
**deployment.metadata.__dict__,
created_metadata=deployment.metadata,
created_round=result.confirmations[0]["confirmed-round"], # type: ignore[call-overload]
updated_round=result.confirmations[0]["confirmed-round"], # type: ignore[call-overload]
deleted=False,
)
self._update_app_lookup(deployment.create_params.sender, app_metadata)
return AppDeployResponse(
app=app_metadata,
operation_performed=OperationPerformed.Replace,
create_response=create_response,
update_response=None,
delete_response=delete_response,
)
def _update_app(
self,
deployment: AppDeployParams,
existing_app: AppMetaData,
approval_program: bytes,
clear_program: bytes,
) -> AppDeployResponse:
"""Update an existing application"""
if isinstance(deployment.update_params, AppUpdateMethodCallParams):
result = self._transaction_sender.app_update_method_call(
AppUpdateMethodCallParams(
**{
**deployment.update_params.__dict__,
"app_id": existing_app.app_id,
"approval_program": approval_program,
"clear_state_program": clear_program,
}
)
)
else:
result = self._transaction_sender.app_update(
AppUpdateParams(
**{
**deployment.update_params.__dict__,
"app_id": existing_app.app_id,
"approval_program": approval_program,
"clear_state_program": clear_program,
}
)
)
app_metadata = AppMetaData(
app_id=existing_app.app_id,
app_address=existing_app.app_address,
created_metadata=existing_app.created_metadata,
created_round=existing_app.created_round,
updated_round=result.confirmation.get("confirmed-round", 0) if isinstance(result.confirmation, dict) else 0,
**deployment.metadata.__dict__,
deleted=False,
)
self._update_app_lookup(deployment.create_params.sender, app_metadata)
return AppDeployResponse(
app=app_metadata,
operation_performed=OperationPerformed.Update,
update_response=result,
)
def _handle_schema_break(
self,
deployment: AppDeployParams,
existing_app: AppMetaData,
approval_program: bytes,
clear_program: bytes,
) -> AppDeployResponse:
if deployment.on_schema_break in (OnSchemaBreak.Fail, "fail"):
raise ValueError(
"Schema break detected and onSchemaBreak=OnSchemaBreak.Fail, stopping deployment. "
"If you want to try deleting and recreating the app then "
"re-run with onSchemaBreak=OnSchemaBreak.ReplaceApp"
)
if deployment.on_schema_break in (OnSchemaBreak.AppendApp, "append"):
return self._create_app(deployment, approval_program, clear_program)
if existing_app.deletable:
return self._replace_app(deployment, existing_app, approval_program, clear_program)
else:
raise ValueError("App is not deletable but onSchemaBreak=ReplaceApp, " "cannot delete and recreate app")
def _handle_update(
self,
deployment: AppDeployParams,
existing_app: AppMetaData,
approval_program: bytes,
clear_program: bytes,
) -> AppDeployResponse:
if deployment.on_update in (OnUpdate.Fail, "fail"):
raise ValueError(
"Update detected and onUpdate=Fail, stopping deployment. " "Try a different onUpdate value to not fail."
)
if deployment.on_update in (OnUpdate.AppendApp, "append"):
return self._create_app(deployment, approval_program, clear_program)
if deployment.on_update in (OnUpdate.UpdateApp, "update"):
if existing_app.updatable:
return self._update_app(deployment, existing_app, approval_program, clear_program)
else:
raise ValueError("App is not updatable but onUpdate=UpdateApp, cannot update app")
if deployment.on_update in (OnUpdate.ReplaceApp, "replace"):
if existing_app.deletable:
return self._replace_app(deployment, existing_app, approval_program, clear_program)
else:
raise ValueError("App is not deletable but onUpdate=ReplaceApp, " "cannot delete and recreate app")
raise ValueError(f"Unsupported onUpdate value: {deployment.on_update}")
def _update_app_lookup(self, sender: str, app_metadata: AppMetaData) -> None:
"""Update the app lookup cache"""
lookup = self._app_lookups.get(sender)
if not lookup:
self._app_lookups[sender] = AppLookup(
creator=sender,
apps={app_metadata.name: app_metadata},
)
else:
lookup.apps[app_metadata.name] = app_metadata
def get_creator_apps_by_name(self, *, creator_address: str, ignore_cache: bool = False) -> AppLookup:
"""Get apps created by an account"""
if not ignore_cache and creator_address in self._app_lookups:
return self._app_lookups[creator_address]
if not self._indexer:
raise ValueError(
"Didn't receive an indexer client when this AppManager was created, "
"but received a call to get_creator_apps"
)
app_lookup: dict[str, AppMetaData] = {}
# Get all apps created by account
created_apps = self._indexer.search_applications(creator=creator_address)
for app in created_apps["applications"]:
app_id = app["id"]
# Get creation transaction
creation_txns = self._indexer.search_transactions(
application_id=app_id,
min_round=app["created-at-round"],
address=creator_address,
address_role="sender",
note_prefix=base64.b64encode(APP_DEPLOY_NOTE_DAPP.encode()),
limit=1,
)
if not creation_txns["transactions"]:
continue
creation_txn = creation_txns["transactions"][0]
try:
note = base64.b64decode(creation_txn["note"]).decode()
if not note.startswith(f"{APP_DEPLOY_NOTE_DAPP}:j"):
continue
metadata = json.loads(note[len(APP_DEPLOY_NOTE_DAPP) + 2 :])
if metadata.get("name"):
app_lookup[metadata["name"]] = AppMetaData(
app_id=app_id,
app_address=get_application_address(app_id),
created_metadata=metadata,
created_round=creation_txn["confirmed-round"],
**metadata,
updated_round=creation_txn["confirmed-round"],
deleted=app.get("deleted", False),
)
except Exception as e:
logger.warning(
f"Error processing app {app_id} for creator {creator_address}: {e}",
)
continue
lookup = AppLookup(creator=creator_address, apps=app_lookup)
self._app_lookups[creator_address] = lookup
return lookup