Want to know the contract operation mechanism of the application on AO? Then grab the contract code to find out. Use a python applet to easily get the contract of the specified process.

Author: txohyeah

Review: outprog

Source: Content Guild - News

About AO

What is AO?

The AO testnet has been released for half a year, and there are a lot of contents in AO. This article mainly focuses on Process and smart contracts, including what AO is, what Process is, and the life cycle of Process. Finally, we will focus on how to view the contract code of Process.

AO is a computing function that Arweave has further developed based on the improvement of distributed storage, aiming to achieve comprehensive decentralized application support, which corresponds to the development path of Ethereum's computing first and storage later. Simply put, AO represents the smart contract or computing power on the Arweave platform, and is a logical layer extension on top of its permanent storage solution.

AO consists of three units, MU / SU / CU

MU: Receives messages sent by users to ensure they are signed.

SU: Timestamps and sorts messages, then bundles and publishes them to Arweave. SU is also the protagonist we will use today, and it will play an important role when reviewing the contract later.

CU: Processes messages and calculates results.

What is the Process running on AO?

AO is essentially a super parallel computer built on a Data Protocol, where Data exists in the form of Message, a basic element defined in AO. Process plays an important role in processing Message and is the basic unit for processing messages on AO. Process runs on CU and can be regarded as a virtual machine on CU. Therefore, the basic ability of Process is to receive and send messages within the network. Then the developer builds the message processing capability for Process by adding a handler to Process. The contract to be discussed later is actually deployed by sending a message to Process and then using the built-in _eval handler. The added handler can be understood as the message processing function of the contract.

Process Lifecycle

If we want to find an analogy in our existing world, I think the container in Docker can be a good comparison for people to understand the process. Below I will use the life cycle of the container in Docker to analogize the life cycle of the process.

We all know that the creation of a container in docker depends on an image. For example, you can create a mysql container based on the mysql image, and then you can call the mysql service on this container. The same is true for the process in AO. The creation of the process depends on the module, and there will be modules with different functions, including the simplest module, the module that supports scheduled tasks, and the module that includes sqlite. There are already hundreds of modules for developers to choose to create their own processes. In addition, you can also make your own custom modules. I believe that in the near future, there will also be modules that support GPUs and modules that support various AI capabilities.

The process in AO is also different from the container in Docker. The container in Docker supports various actions such as starting, stopping, and deleting. Currently, there are no such operations in the process of AO. After all, in the world of decentralized applications, these functions controlled by centralized individuals are not harmonious. In AO, the operation of the process depends entirely on the value of the process itself. If it has enough value, then there will definitely be more CUs willing to run it. On the contrary, if a process has no value, no CU is willing to run it, and then the process will disappear silently.

Eval function and AO contract deployment

What is an AO contract? Compared with Ethereum’s smart contract, the Lua code in the Process running on AO can be roughly regarded as a smart contract.

When we create a Process, each loaded Module will have two default handlers (handler can be understood as a function open to other Processes), one of which is the _eval handler. The main function of this handler is to run Lua code. For example, if you enter 1+1 in aos and return 2, it is processed by this handler. So deploying a contract is actually sending a message to the Process, and adding a custom Handler that can handle business logic to the Process through the _eval handler.

There is one thing to note, see the code below. This is the code section in process.lua in the official open source AO project. The _eval handler can only be executed when the sender of the message is the same as the Owner of the Process (the Owner is the wallet address when the contract is created). In other words, if the Owner is set to nil (that is, the Process is set to have no Owner), then the contract in this Process becomes a contract that no one can modify.

In addition, because AO is actually built on the Storage-based Consensus Paradigm (SCP), the running contract must be found on the "Storage Consensus", that is, all messages running in Process, including the contract code deployed through the _eval handler, are stored on Arweave. Therefore, anyone can find the contract code on this "Storage Consensus".

View Contract

So, let's move on to today's topic: how do you find the contract code? Below I will introduce two methods and give you a Python program to grab the contract.

Arweave

The first method is of course to query directly on Arweave (graphql https://arweave.net/graphql can be used). This method requires that the data is packaged in Arweave before it can be useful.

SU - Browser

The second method is the one we will mainly introduce today. Since all data will be uploaded to the chain by SU, it can also be queried on SU. I personally think that each SU will have a cache locally, caching the data uploaded by itself, so the corresponding contract code can be found by querying SU. First of all, you can directly query by entering the address in the browser. However, the disadvantages of this method are also obvious: 1. Some processes send and receive a huge amount of messages, so the amount of data uploaded to the chain is also huge. However, the data that the browser can load is limited, and the browser often crashes. 2. It is difficult to filter out the contract data you want from the massive amount of data.

As shown in the figure below, all messages of the process (AO token process) with process id m3PaWzK4PTG9lAaqYQPaPdOcXdO8hYqi5Fe9NWqXd0w within two timestamps are shown.

SU - sdk

I wrote a small program here, using an ao SDK written by a big man in the industry based on Python to filter out messages with Action as Eval. (Messages with Action as Eval are all messages processed by the _eval handler, including loaded contract codes.) The following figure shows the message I captured from SU, where the content in the data field is a contract code with character escapes. Of course, the captured message will also contain content that is not a deployed contract code, such as executing 1 + 1, etc. However, the number of messages after filtering is not much, and the contract code can be manually selected.

The program has been open sourced. Here is the github address: https://github.com/txohyeah/ao-sc

AO token contract

So let's take a look at the contract code of ao token. (The contract code will also be placed in the open source code repository)

First, when initializing the state, TotalSupply = "21000000000000000000" is defined, which is consistent with the issuance amount of Bitcoin.

Denomination = Denomination or 12 also defines the decimal point to be 12 places.

It is defined that if the transfer function is executed before 100,000 blocks are generated, "Transfer is locked" will be returned directly.

Since one block is generated every five minutes, 100,000 blocks will be generated around February of next year.

There is more relevant information, so I will not go into details. You can enjoy browsing this classic AO contract.

Program Instructions

I introduced the captured contract code above, and now I will briefly introduce this program. It is very simple, and I believe that people who don’t understand the code can also use it easily.

The first step is to install Python 3.12. The program is written in Python, so installing Python is a must.

The second step is to install the required packages. This program relies on the SDK of the industry leader and everpay.

Step 3: Modify start_time / end_time / process in fetch_sc_record.py and execute fetch_sc_record.py.

start_time and end_time are the time periods you need to capture. process is the id of the process of the contract you need to capture.

In the fourth step, the corresponding Eval message will appear in the msg_eval.json file. Browse the message again and you can find the contract code you need!

PS: You also need to set HTTPS_PROXY in the same way as running aos.

🏆 "Catch Bugs" to Get Rewards: If you find typos, incorrect sentences, or incorrect descriptions in this article, click here to report them and you will get rewards.

Disclaimer: This article does not represent the views or positions of PermaDAO. PermaDAO does not provide investment advice and does not endorse any project. Please abide by the laws of your country and conduct Web3 activities in compliance with regulations.

🔗 关于 PermaDAO:Website | Twitter | Telegram | Discord | Medium | YouTube