What are Programs?
Programs are accounts that contain executable code in their data. They can be invoked by anyone, and they run on the Thru blockchain virtual machine. They can read data from any account. They can also create new accounts that they own, and write to them. To enable potentially complex calculations, the Thru network provides a virtual machine (VM). This is like a shared, simulated computer that everyone can trust to run programs exactly as written. It is able to perform ordinary computations, as well as update the state of the network. The Thru VM is based on the 64-bit RISC-V architecture, which is a simplified instruction set typically used for physical CPUs in microcontrollers. Because of its simplicity, it can run programs efficiently. For more information, see Virtual Machine.Transactions
When someone wants to run a program, we call this a transaction. The transaction contains the program to run, the accounts that the program should operate on, and the instructions to pass to the program. More precisely, the transaction format is specified in the transaction specification.Accounts
A transaction must include a list of the addresses of the accounts that the program should operate on. Typically, the instruction data then specifies to the program the semantics of the accounts.The Fee Payer
Computing power isnβt free, and so someone needs to pay for each transaction. The fee payer of the transaction is the account that requests the transaction to be executed, and pays for the transaction to be run. The fee is charged in the native token of the Thru network. Notably, the fee payer can choose the fee they wish to pay for the transaction, which may even be zero. The value of a transaction is determined based on market value; the fee payer should determine the fee they are willing to pay for the transaction.Each transaction has a maximum number of compute units (CUs) that
it is allowed to consume. The number of compute units consumed is
conmputed based on the number of instructions executed, the amount of
memory used, and the number of additional bytes in account storage that
are consumed by the program. See resources for more information.The maximum compute units of a transaction can be used in the calculation
of the fair fee for the transaction.
Instructions
Note that the instruction data for a program is just a sequence of bytes, which is left up to the interpretation of the program.Calling Other Programs
Programs can call other programs using theinvoke syscall. This mechanism is more important
for more than just reusing code. Because each program is responsible for its
own accounts, having programs be able to call other programs provides a
mechanism for encapsulating state.
Success and Failure
Once a program has finished executing, it should use theexit syscall. It produces an exit code, and
can either succeed or revert the transaction. If it reverts, the execution
of the transaction is halted and all changes made to accounts are rolled back.
If the top-level program succeeds, the transaction is committed and the changes
made to accounts are persisted.
Writing Programs
Since the Thru VM uses the popular RISC-V architecture, it is easy to write programs using popular languages like C, C++, and Rust, using existing compilers. You can learn how to write a C program here.Memory Model
To make writing programs easy, all the information made available to the program can be accessed at certain memory addresses. For example, the data of the accounts passed into the program are each located at an address0x03____000000, where the blanks are the index of the account.
To read and write account data, the program can simply read and write to
the appropriate address. See Memory Layout
for more information.
System Calls (Syscalls)
To perform special operations, such as creating an account and transferring account balances, the virtual machine provides a number of syscalls. These are special operations which are triggered by theecall instruction, and are handled by the VM. The Thru SDKs provide
functions such as tsys_account_create
which wrap the syscalls and make them easy to call from high-level
languages. Learn more about syscalls here.