-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for compiling programs to AVM bytecode, and referen…
…cing those programs within other contracts. In particular: * support getting compiled programs from contracts using `compile_contract` and address from logic signatures using `compile_logicsig` * added `algopy.arc4.arc4_create`, and `algopy.arc4.arc4_update` for creating and updating ARC4 applications, automatically providing the required parameters * new CLI options for controlling AVM bytecode output `--output-bytecode` enables outputting AVM bytecode to a `.bin` file `--match-algod-bytecode` outputs bytecode that will match algod output, if this is not specified bytecode output will be smaller where possible, but functionally equivalent * new CLI options for providing template values when outputting to bytecode `--template-var` and `--template-vars-path` allows specifying template values to use during bytecode generation `--template-vars-prefix` allows overriding the default prefix (TMPL_) used for template variable expansion
- Loading branch information
1 parent
1b24cd7
commit 7d7a4fd
Showing
327 changed files
with
39,752 additions
and
9,367 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ exclude_also = | |
if .*TYPE_CHECKING: | ||
@(abc\.)?abstractmethod | ||
raise InternalError | ||
typing.assert_never |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
docs/architecture-decisions/2024-05-22_teal-compilation.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Compilation of Algorand TEAL Programs into AVM Bytecode | ||
|
||
- **Status**: Proposed | ||
- **Owner**: Daniel McGregor (MakerX) | ||
- **Deciders**: Rob Moore (MakerX), Adam Chidlow (MakerX), Alessandro Cappellato (Algorand Foundation) | ||
- **Date created**: 2024-05-22 | ||
- **Date decided**: | ||
- **Date updated**: | ||
|
||
## Context | ||
|
||
The Puya compiler needs to assemble valid Algorand TEAL (Transaction Execution Approval Language) programs into AVM (Algorand Virtual Machine) bytecode | ||
so the bytecode can be used within inner transactions that need to create applications. Additionally, the ability to produce compiled bytecode without | ||
an algod node is also desriable for other use cases, e.g. producing an [ARC-56 app description](https://github.com/algorandfoundation/ARCs/pull/258). | ||
|
||
There are several potential solutions to achieve this compilation, each with its own advantages and trade-offs. | ||
|
||
## Requirements | ||
|
||
* The solution works cross-platform (i.e. works on Windows, Mac and Linux) | ||
* The solution is fast | ||
* The solution is correct | ||
|
||
## Options | ||
|
||
### Option 1. Use Algod Compile Endpoint | ||
|
||
Utilize the Algorand algod `/v2/teal/compile` endpoint to compile TEAL programs into AVM bytecode. | ||
|
||
**Pros** | ||
|
||
* Ensures compatibility with the latest Algorand protocol updates. | ||
* Existing solution | ||
|
||
**Cons** | ||
|
||
* Requires configuration and connectivity to an Algorand algod node | ||
* A compiler requiring a network call to produce output is unusual, and may be problematic in environments adhering to the "principle of least privilege" | ||
* Potential latency due to network requests. | ||
|
||
### Option 2. Use Goal CLI to Compile | ||
|
||
Utilize the Algorand goal command-line interface to compile TEAL programs locally. | ||
|
||
**Pros** | ||
|
||
* Local compilation without the need for network connectivity. | ||
* Directly supported by Algorand, ensuring compatibility. | ||
|
||
**Cons** | ||
|
||
* Requires installation of the goal CLI, and configuration to locate it | ||
* No native windows binary of goal is available | ||
* Increased complexities due co-ordinating inputs and outputs between the compiler and goal | ||
|
||
### Option 3: Integrate TEAL to AVM Bytecode Assembly into Puya Compiler | ||
|
||
Extend the existing Puya compiler to directly convert TEAL programs into AVM bytecode. | ||
|
||
**Pros** | ||
|
||
* Full Control Over Compilation Process: By integrating the assembly within the Puya compiler, we maintain full control and can tailor the process to fit specific use cases and workflows. | ||
* No External Dependencies: This approach eliminates reliance on and configuration of external tools or services, ensuring the compilation process is self-contained and not affected by network issues or external updates. | ||
* Additional optimizations: We can introduce additional optimizations, if desired these could be upstreamed to the algod implementation | ||
* Simpler Implementation: Given that Puya already handles well-structured TEAL (as one of the IR layers in the compiler), extending it to assemble bytecode is relatively straightforward and does not require complex parsing. The conversion from a logical TEAL op to the underlying bytecode is based on information in the AVM langspec providing a high degree of confidence in the output. | ||
|
||
**Cons** | ||
|
||
* Potential for Bugs and Inconsistencies: There is a risk of introducing bugs or deviations from Algorand's official compiler, which could lead to compatibility issues. This risk can be offset by including tests that verify the compiler output matches the algod output for a suite of test programs. | ||
* Ongoing Maintenance: We will need to continuously update our compiler to ensure it remains aligned with any changes or updates to the Algorand protocol. This is mitigated to a large degree by utilizing the AVM langspec as the source of truth for op code values. | ||
|
||
## Preferred option | ||
|
||
Option 3: Implement Assembly of TEAL to Bytecode in the Puya Compiler | ||
|
||
The preferred solution is to extend the existing Puya compiler to directly convert TEAL programs into AVM bytecode. | ||
This approach offers several key advantages, including full control over the compilation process and independence from external tools or services. | ||
|
||
To provide consistency with algod, we will introduce a flag that enables the generation of bytecode matching the results produced by algod. | ||
Additionally, to mitigate any potential risks, we will implement comprehensive tests to verify that our output consistently | ||
aligns with algod compilation results. | ||
|
||
## Selected option |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.