Arweave AO Bootcamp 8/8 Ending & Appendix
KYOHEI ITO

KYOHEI ITO @kyohei_nft

About: The top contributor to Japan's web3 developer community / ex-CTO & VPoE at web3 startups in Japan

Location:
Tokyo, Japan
Joined:
Oct 14, 2021

Arweave AO Bootcamp 8/8 Ending & Appendix

Publish Date: Jun 4
3 3

This concludes the AO Bootcamp. Congratulations on making it through!

The following content serves as appendices.

We will continue to publish learning resources on Arweave AO.

Next up, we plan to introduce the latest practical dApp development flow leveraging Claude Code & ChatGPT.

Debugging and Troubleshooting

Common issues in AO development and their solutions are introduced below.

  1. Message sending fails:

    • Check that the wallet has sufficient balance.
    • Verify that the correct process ID is specified.
    • Confirm that the message format is correct.
  2. Process state does not update:

    • Ensure that handlers are properly configured.
    • Check that message tags and data match the handler’s conditions.
    • Verify that the Compute Unit (CU) correctly calculates the process state.
  3. Obtaining debug information:

    • Use the ao.log function to output debug information.
    • Use the ao.link explorer to inspect process states and messages.
    • Review process results in detail.
  4. Performance optimization:

    • Avoid using large data structures.
    • Avoid sending unnecessary messages.
    • Use dryrun for read-only operations.
  5. Network connectivity issues:

    • Verify that all units (MU/SU/CU) are running correctly.
    • Check network settings (URLs, ports, etc.) for accuracy.
    • Adjust timeout settings as needed.

An important tool for AO development is the ao.link explorer. This tool allows you to visually monitor process states and the flow of messages.

Complete Example of a Key-Value Store

Below is a full implementation example of a Key-Value store.

// kv-store.js
const { createDataItemSigner, connect } = require("@permaweb/aoconnect");
const { readFileSync } = require("fs");
const { resolve } = require("path");

// Load wallet
const jwk = JSON.parse(readFileSync(resolve(__dirname, "wallet.json")));
// Connect to AO units
const { result, message, spawn, dryrun } = connect({
  MU_URL: "http://localhost:4002",
  CU_URL: "http://localhost:4004",
  GATEWAY_URL: "http://localhost:4000",
});

// Wait helper
const wait = (ms) => new Promise((res) => setTimeout(() => res(), ms));

// Main function
async function main() {
  // Spawn a process
  const pid = await spawn({
    module: "YOUR_MODULE_ID", // AOS module ID
    scheduler: "YOUR_SCHEDULER_WALLET", // Scheduler wallet address
    signer: createDataItemSigner(jwk),
  });

  await wait(1000);
  console.log(`Spawned process ID: ${pid}`);

  // Define Lua handlers
  const lua = `
    local ao = require('ao')
    Store = Store or {}

    Handlers.add(
      "Get",
      Handlers.utils.hasMatchingTag("Action", "Get"),
      function (msg)
        assert(type(msg.Key) == 'string', 'Key is required!')
        ao.send({ Target = msg.From, Tags = { Value = Store[msg.Key] }})
      end
    )

    Handlers.add(
      "Set",
      Handlers.utils.hasMatchingTag("Action", "Set"),
      function (msg)
        assert(type(msg.Key) == 'string', 'Key is required!')
        assert(type(msg.Value) == 'string', 'Value is required!')
        Store[msg.Key] = msg.Value
        Handlers.utils.reply("Value stored!")(msg)
      end
    )
  `;

  // Register handlers
  const mid = await message({
    process: pid,
    signer: createDataItemSigner(jwk),
    tags: [{ name: "Action", value: "Eval" }],
    data: lua,
  });

  const res = await result({ process: pid, message: mid });
  console.log("Handler registration result:", res);

  // Set a value
  const mid1 = await message({
    process: pid,
    signer: createDataItemSigner(jwk),
    tags: [
      { name: "Action", value: "Set" },
      { name: "Key", value: "test" },
      { name: "Value", value: "abc" },
    ],
  });

  const res1 = await result({ process: pid, message: mid1 });
  console.log("Set value result:", res1.Messages[0]);

  // Get a value
  const res2 = await dryrun({
    process: pid,
    signer: createDataItemSigner(jwk),
    tags: [
      { name: "Action", value: "Get" },
      { name: "Key", value: "test" },
    ],
  });

  console.log("Get value result:", res2.Messages[0].Tags);
}

main().catch(console.error);
Enter fullscreen mode Exit fullscreen mode

Glossary

AO (Actor Oriented) A protocol for a decentralized, hyper-parallel supercomputer built on Arweave. It is based on the actor model and offers high scalability.

AOS (Actor Operating System) The first implementation of AO. A lightweight actor runtime environment using Lua, responsible for process management and message handling.

MU (Messenger Unit) A component of AO responsible for receiving, validating, and forwarding messages to the appropriate units.

SU (Scheduler Unit) The component that orders and persists messages, including storing them on Arweave.

CU (Compute Unit) The component that executes process logic and updates process state.

Eval (Evaluation) An action in AOS that dynamically registers or updates Lua code within a process.

dryrun An operation that simulates processing using the current state without actually writing transactions to Arweave.

JWK (JSON Web Key) A JSON format representing wallet private and public keys.

AR Token The native token of the Arweave network, used for permanent data storage.

AO Token The native token of the AO network, used for network security and incentives.

aoETH An asset bridged from Ethereum’s ETH into the AO network, eligible for AO token distribution.

Handlers A table in AOS Lua scripts that manages functions handling incoming messages.

Message The unit of data exchanged between processes, including tags and payload.

Arweave A blockchain network providing permanent storage. AO is built on top of it.

References

  1. Arweave Documentation
  2. AO Spec
  3. AO Cookbook
  4. aoconnect Documentation
  5. ANS-104 Specification
  6. ao-scheduler-utils
  7. ao-loader
  8. AO Whitepaper
  9. ao.link Explorer

By leveraging these resources, you can deepen your understanding of Arweave and AO development and accelerate your progress.

Comments 3 total

Add comment