[Has anyone successfully tested it?] Optimizing TVM Linker for TON Blockchain: A Technical Deep Dive πŸš€

The TVM Linker is a crucial utility for the TON Blockchain (The Open Network), responsible for compiling and linking TVM (TON Virtual Machine) assembly source code of TON smart contracts. It transforms the assembly code into a binary TVC file, adds standard selectors and runtime, and can even emulate the computing phase of a TON transaction.

Prerequisites :hammer_and_wrench:

Before diving into the TVM Linker, ensure you have the latest version of Rust and the Cargo tool installed. You can find the installation guide here.

Building the TVM Linker :wrench:

To build the TVM Linker, execute the following command:

cargo update && cargo build --release -j8

Usage Modes of TVM Linker :open_book:

1. Generating a Ready-to-Deploy Contract

To compile a TVM assembly source file into a deployable contract, use the following command:

tvm_linker compile [--lib <lib_file>] [--abi-json <abi_file>] [-w <workchain_id>] [--debug] [--print_code] [--silent] [--debug-map <debug_info_path>] <source>
  • <source>: TVM assembly source file name.
  • --lib <lib_file>: Runtime library file. Multiple libraries can be specified.
  • --abi-json <abi_file>: Contract ABI file to generate function IDs according to ABI signatures.
  • -w <workchain_id>: Workchain ID for generating user-friendly addresses. Default is -1.
  • --debug-map <debug_info_path>: Generates a debug info file for contract debugging.
  • --print_code: Prints the generated code without creating a TVC file.
  • --silent: Mutes extra notifications.

2. Decoding .boc Messages

To decode externally prepared .boc messages, use:

tvm_linker decode [--tvc] <boc-file>

3. Preparing External Inbound Messages

First, generate a contract as described in step 1. Then, create an external inbound message in boc format using:

tvm_linker message <contract-address> [--init] [--data] [-w]
tvm_linker message <contract-address> [--init] --abi-json <abi_file> --abi-method <method_name> --abi-params {json_with_params} [-w]
  • --init: Creates a constructor message with contract’s code and data.
  • --data <data_string>: Adds a raw body to the message in hex format.
  • --abi-json, --abi-method, --abi-params: Specifies ABI details for the message.

4. Emulating Contract Execution

For contract debugging, the linker can emulate the compute phase of a blockchain transaction:

tvm_linker test <contract-address> --body XXXX... [--sign key-file] [--trace] [--decode-c6] [--internal <value>] [--src address] [--now unixtime] [-s source-file] [--balance <value>]
  • --body XXXX...: Hex string representing the message body.
  • --sign key-file: Signs the body with a private key.
  • --trace: Traces VM execution.
  • --decode-c6: Outputs actions in a user-friendly format.
  • --internal <value>: Sends an internal message with specified nanograms.
  • --src address: Source address for the internal message.
  • --now unixtime: Sets transaction creation time.

5. Disassembler Tools

The TVM Linker also provides disassembler tools for analyzing TVC files:

  • dump: Outputs a pseudo-graphical representation of a cell tree.
  • text: Disassembles TVC files produced by Solidity and FunC compilers.
  • graphviz: Generates a graphical DAG representation in dot format.

For more detailed information on all options, flags, and subcommands, use tvm_linker --help.

Support and Resources :books:

For further documentation and tutorials, visit docs.ton.dev and check out the TON Labs YouTube Channel.

By optimizing the TVM Linker for TON Blockchain, developers can streamline the process of deploying and testing smart contracts on this robust and scalable platform.