Things to Focus on while Working with TON Blockchain
In this article, we will review and discuss the elements to consider for those who want to develop TON applications.
Checklist
1. Name collisions
Func variables and functions may contain almost any legit character. I.e. var++
, ~bits
, foo-bar+baz
including commas are valid variables and functions names.
When writing and inspecting a Func code, Linter should be used.
2. Check the throw values
Each time the TVM execution stops normally, it stops with exit codes 0
or 1
. Although it is done automatically, TVM execution can be interrupted directly in an unexpected way if exit codes 0
and 1
are thrown directly by either throw(0)
or throw(1)
command.
3. Func is a strictly typed language with data structures holding exactly what they are supposed to store
It is crucial to keep track of what the code does and what it may return. Keep in mind that the compiler cares only about the code and only in its initial state. After certain operations stored values of some variables can change.
Reading unexpected variables values and calling methods on data types that are not supposed to have such methods (or their return values are not stored properly) are errors and are not skipped as "warnings" or "notices" but lead to unreachable code. Keep in mind that storing an unexpected value may be okay, however, reading it may cause problems e.g. error code 5 (integer out of expected range) may be thrown for an integer variable.
4. Messages have modes
It is essential to check the message mode, in particular its interaction with previous messages sent and fees. A possible failure is not accounting for storage fees, in which case contract may run out of TON leading to unexpected failures when sending outgoing messages. You can view the message modes here.
5. TON fully implements the actor model
It means the code of the contract can be changed. It can either be changed permanently, using SETCODE
TVM directive, or in runtime, setting the TVM code registry to a new cell value until the end of execution.
6. TON Blockchain has several transaction phases: computational phase, actions phase, and a bounce phase among them
The computational phase executes the code of smart contracts and only then the actions are performed (sending messages, code modification, changing libraries, and others). So, unlike on Ethereum-based blockchains, you won't see the computational phase exit code if you expected the sent message to fail, as it was performed not in the computational phase, but later, during the action phase.
7. TON contracts are autonomous
Contracts in the blockchain can reside in separate shards, processed by other set of validators, meaning that developer cannot pull data from other contracts on demand. Thus, any communication is asynchronous and done by sending messages.