Calculate π
Use OVM to approximate the value of π.
Introduction
π, a mathematical constant, represents the ratio of a circle’s circumference to its diameter and is approximately equal to 3.14159. As an irrational number, π cannot be expressed precisely as a ratio of two integers. Its decimal expansion is infinite and non-repeating, with no discernible pattern. Its approximation is therefore a common challenge in computer science and mathematics, with many algorithms developed to calculate π to a high degree of precision.
OVM can be used to execute complex computations like AI training, scientific simulations, or any other compute-intensive tasks that are not feasible on an EVM blockchain. This includes calculating the value of π to a high degree of precision.
In this tutorial, you will learn how to write an OVM task to calculate the value of π.
Getting Started
Rest assured, this is not a math class, but a practical exercise to demonstrate the power of OVM.
Prerequisites
Before you start building, make sure you have the following prerequisites:
Installation
You should use Poetry to create a Python project.
Poetry will generate a pyproject.toml
file that includes the project dependencies.
We use Poetry to manage the dependencies and virtual environment for the project, so it will be easier to manage and containerize the project later.
We then use OVM Python SDK to interact with OVM Gateway to access verifiable and high-performance compute resources.
Python Code
Let's start by creating a new Python file, main.py
, and writing the following code:
The calculate_pi
function returns π using mpmath.
You may also implement your own code (Chudnovsky or others) to approximate π, but for this tutorial, we will use mpmath for simplicity.
We also set the precision to 100 decimal places, which is more than enough for most applications. Anything beyond 100 decimal places is considered an overkill, so we will raise an error when the input is greater than 100.
The rest of the code uses eth_abi to decode the input from hexidecimal to an integer, calculates π, and submits the result to the OVM Gateway.
Dockerfile
We containerize the Python code using Docker. This is to ensure that the code runs in a consistent environment and can be easily sprung up by any OVM Executor.
Smart Contract
Init a New Project
We now write a Solidity contract to enable others to interact with the code above, and making sure all interactions are recorded on-chain.
Add Dependencies
All OVM contracts depend on the OVM contracts library. We can install the library using Forge and update the remappings to point to the source code.
Implement Pi.sol
We begin by creating a new Pi.sol
in src
folder.
Common features are implemented in the OVMClient
contract, which can be imported and ready to use.
In this contract:
constructor
initializes the task with the required specifications. Updatespecification.repository
to point to your git repository.sendRequest
is used to monitor user requests and forward them to the OVM Gateway.setResponse
is used to receive the response from the OVM Gateway, decode the response, and store the result.
All results are recorded on-chain, ensuring transparency and verifiability.
Contract Deployment
Once the development is done, we deploy the contract to Open Chain Testnet using Forge.
Use your own private key with sufficient funds to deploy the contract.
0xbb2F7085Ad69653B8574121A549e247B24C64f25
is the OVM Gateway contract.
Upon deployment, the contract will be registered with the OVM Gateway, and users can start sending requests to calculate π.
Testing
Before testing your contract, make sure you have committed and pushed your code to a remote git repository.
We can now test the contract by sending a request to calculate π.
Replace 0xcontract
with the contract address that you just deployed.
And supply your private key to sign the transaction.
The calculation will be distributed to any available OVM Executor, the Executor will then pull the code from your git repository, execute the code, and submit the result on-chain via setResponse
.
Conclusion
Congratulations! You have successfully written an OVM task to calculate the value of π. This tutorial demonstrates the power of OVM in executing complex computations and recording the results on-chain for transparency and verifiability.
You can now develop your smart contract with OVM to access high-performance compute resources for other complex computations such as AI training and scientific simulations.