Programs
The point of a blockchain is to run programs that manipulate accounts, also known as smart contracts. When running a program on the Thru network, it is executed simultaneously on physical validator nodes. However, with the abstraction of the network, you can think of it as the program just executing once on the network and modifying the state of the accounts on the network.
What are Programs?
Section titled “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
Section titled “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
Section titled “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
Section titled “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.
Additionally, because the fee payer must authorize the transaction, programs can use this to see that the fee payer has given consent to certain operations.
Instructions
Section titled “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
Section titled “Calling Other Programs”Programs can call other programs using the
invoke 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
Section titled “Success and Failure”Once a program has finished executing, it should use the
exit 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
Section titled “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
Section titled “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
address 0x03____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)
Section titled “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 the ecall 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.
Software Development Kits (SDKs)
Section titled “Software Development Kits (SDKs)”To make it easy to develop programs for the Thru VM, software development kits (SDKs) are available for C, C++, and Rust. They provide build scripts to help you compile programs, as well as a variety of useful functions including syscall wrappers.