๐ ๏ธWork in progress๐ ๏ธ
KAVM leverages the K Framework to empower Algorand smart contracts' developers with property-based testing and formal verification.
For now, KAVM can only be installed from source. We intend to provide a Docker image and standalone packages for popular operating systems at a later stage. To install KAVM from source, please refer to Working on KAVM.
The semantics can be used either via the kavm command-line interface or programmaticaly with the kavm Python package.
The kavm Python tool provides a command-line interface for the semantics:
- compiling the semantics with the LLVM and Haskell backends with
kavm kompile - executing concrete simulations and tests are run via
krunand the K LLVM Backend withkavm run - Not yet implemented proving properties by symbolic execution via
kproveand the K Haskell Backend withkavm prove - parsing TEAL programs via
kastwithkavm kast
See kavm --help for more information.
Note: make sure to activate the Python virtual environment build by KAVM with . .build/venv/bin/activate for the kavm script to be available.
kavm provides Python bindings to KAVM
kavm.algodintegrates KAVM withpy-algorand-sdkandalgodโ the Algorand node client, making it possible to run existing deployment and testing scripts on top of KAVM.kavm.__main__is the implementation of thekavmcommand-line tool
The tests are located in tests/.
Run make test-avm-semantics -j8 to execute the test suite. Adjust the -jX option as needed to run X tests in parallel.
Each test has two components:
- a test scenario,
.avm-simulationintests/scenarios/that defines the initial network state, the input transaction group and declares which TEAL programs it uses. - a TEAL program
.teal, or several programs, intests/teal-sources/;
Currently, a test is considered passing if the kavm runner script returns 0. The final state network state is not examined.
The negative test scenarios are expected to fail and have the .fail.avm-simulation file extension.
NOTE: the specs have not yet been fully ported to the current semantics and are failing.
They are not checked on CI and are not called by make test.
The specifications are located in tests/specs/.
Run make test-avm-prove to verify the specifications.
The verification.md module must be compiled with the Haskell backend and included in every spec file.
The Makefile target test-avm-prove ensures that the verification module is compiled properly before checking the specs.
The AVM semantics source files are located in lib/include/kframework/avm/:
avm-configuration.mddefines the top-level K configuration of the AVM:- AVM execution state
- TEAL interpreter state
blockchain.mddefines the Algorand network state: accounts, apps and assets.txn.mddefines the representation of Algorand transactions.avm-execution.mddefines the execution flow of AVM:- transaction evaluation and execution process, depending on its type;
- starting/stopping TEAL execution;
- AVM-level panic behaviors;
- basic syntax of simulation scenarios.
avm-initialization.mddefines rules that initialize AVM with a specific initial network state and the supplied transaction group;avm-limits.mddefines the constants that govern AVM execution: max transaction group size, max TEAL stack depth, etc.avm-txn-deque.mddefines an internal data structure that handles transaction execution schedule.args.mddefines the representation of Logic Signature transaction arguments.
Transaction Execution Approval Language (TEAL) is the language that governs approval of Algorand transactions and serves as the execution layer for Algorand Smart Contracts.
The K modules describing syntax and semantics of TEAL are located in lib/include/kframework/avm/teal/:
teal-types.mddefines basic types representing values in TEAL and various operations involving them.teal-constants.mddefines a set of integer constants TEAL uses, including transaction types and on-completion types.teal-fields.mddefines sets of fields that may be used as arguments to some specific opcodes in TEAL, such astxn/txnafields andglobalfields.teal-syntax.mddefines the syntax of (textual) TEAL and the structure of its programs.teal-stack.mddefines the K representation of TEAL stack and the associated operations.teal-execution.mddefines the execution flow of the interpreter:teal-driver.mddefines the semantics of the various TEAL opcodes and specifies how a TEAL program is interpreted.
Not all TEAL opcodes are supported by the semantics as of yet. See the relevant wiki page for the table of supported opcodes and their execution costs. The semantics does not yet support contract-to-contract calls.
kavm[./kavm] is a Python package that enables interoperability between KAVM and algod โ the Algorand node daemon. kavm also provides a drop-in replacement for py-algorand-sdk, making it possible to run existing deployment and testing scripts on top of KAVM.
kavm uses two auxiliary scripts located in lib/include/kframework/avm/scripts/:
parse-avm-simulation.shcallskparseto parse a simulation scenario from a.avm-simulationfile;parse-teal-programs.shcallskparseto parse a;-separated list of TEAL source codes.
make deps: build K and other dependencies.make -C kavm poetry-install: set up a virtual environment using Poetry.make build: compile KAVM K modules and thekavmtool. By default,kompileis called with the LLVM backend. To compile the semantics with the Haskell backend, executeK_BACKEND=haskell make build.make test -j8: run tests. Adjust the-jXoption as needed to runXtests in parallel.
When developing, make sure to activate the Python virtual environment build by KAVM with . .build/venv/bin/activate for the kavm script to be available. The Makefile activates the environment itself when necessary.
We use direnv to manage the environment variables such as PATH, see .envrc.
After installing direnv, run direnv allow to activate the settings for the project's directory.
Feel free to ignore direnv if you prefer your global installation of K.
This project contains facilities to generate coverage metrics for K rewrite rules that were executed by kavm run. This is helpful in ensuring that the test suite contains input programs that exercise all rewrite rules in the semantics.
To generate the coverage report for the AVM simulation scenarious squite (tests/scenarios/), run the following command:
make clean-coverage && make test-kavm-avm-simulation && make coverageThis command generates a .build/coverage.xml file. This file contains information about the K rewrite rules that have been exercised for all tests in the (tests/scenarios/) directory.