Skip to Content
DevelopersExamplesComputational Squaring

Computational Squaring Example

Off-chain computation with on-chain verification for squaring a number. Here, we implement a simple squaring function (n ^ 2) for demonstration purposes—in a real-world scenario, you would implement a more complex function.

This example demonstrates how operators perform off-chain computation of squaring a number with on-chain objectively verifiable results when a fault occurs.

Lifecycle Overview:

  • Requests are sent to the contract without the need for heavy on-chain computation.
  • The off-chain service, ran by the operators, computes the square of a given number and submits the result to the contract.
  • Slashing is triggered if the operator submits an incorrect result.

Getting started

Prerequisites for this example:

  1. Rust and Cargo: https://www.rust-lang.org/tools/install
  2. Basic understanding of CosmWasm Contract
  3. Node.js for development of the off-chain service: https://nodejs.org/en/download/
  4. Docker to run off-chain node: https://docs.docker.com/get-docker/

Project structure:

squaring/ ├── contract/ <- CosmWasm contract ├── service/ <- Off-chain service ├── compose.yml <- Docker Compose file for running the service └── README.md <- This file

How to run

Clone and copy contents of ./examples/squaring to your local machine. Install and npm run test to set up the environment.

run.sh
npm run test

How to build the contract

build.sh
docker run --rm -v "$(pwd)":/code \ --mount type=volume,source="$(basename "$(pwd)")_cache",target=/target \ --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \ cosmwasm/optimizer:0.16.0

Project overview

This project demonstrates a Bitcoin Validated Service (BVS) using a simple squaring function as an example. It consists of two main components the on-chain contract and the off-chain service.

We use squaring as a simple example to demonstrate the concept of off-chain computation with on-chain verification. Squaring is n^2, where n is the input number. While this example is simple and not meant for production, it illustrates the principles of off-chain computation and on-chain verification.

The general request respond lifecycle is as follows:

  1. Users submit computation requests to the contract
  2. Operators (running the off-chain service) detect these requests
  3. Operators perform the computation off-chain
  4. Operators submit the results back to the contract
  5. The contract verifies the results and slashes operators who submit incorrect answers

On-chain Contract (CosmWasm)

Located in the contract/ directory, this Rust-based smart contract:

  • Accepts computation requests from users
  • Verifies responses from operators to prove fault
  • Manages operator registration and slashing mechanisms
  • Provides query endpoints for retrieving computation results

Off-chain Service (TypeScript)

Located in the service/ directory, this Node.js service:

  • Continuously monitors the blockchain for new computation requests
  • Performs the actual computation (squaring numbers) off-chain
  • Submits results back to the on-chain contract
Last updated on