Skip to content

Legacynet AOS on HyperBEAM

Currently, testing legacynet aos processes works much better without HyperBEAM.

You can check out Legacynet AOS guide to test with legacy AO units.

Legacynet AOS on HyperBEAM uses genesis-wasm@1.0 device and an external CU for computation. The standalone WAO server works as a local CU.

spawnLegacy, schedule, and computeLegacy manages the process for you.

import assert from "assert"
import { describe, it, before, after, beforeEach } from "node:test"
import { HyperBEAM } from "wao/test"
 
const cwd = "../HyperBEAM" // HyperBEAM directory
 
const lua = `
local count = 0
Handlers.add("Inc", "Inc", function (msg)
  count = count + 1
  msg.reply({ Data = "Count: "..tostring(count) })
end)
 
Handlers.add("Get", "Get", function (msg)
  msg.reply({ Data = "Count: "..tostring(count) })
end)`
 
describe("Hyperbeam Legacynet", function () {
  let hbeam, hb
  before(async () => {
    hbeam = await new HyperBEAM({
      cwd,
      reset: true,
      as: ["genesis_wasm"],
    }).ready()
  })
 
  beforeEach(async () => (hb = hbeam.hb))
  after(async () => hbeam.kill())
 
  it("should run a legacynet AOS process with a local CU", async () => {
    const { pid } = await hb.spawnLegacy()
    const { slot } = await hb.schedule({
      pid,
      data: lua,
      tags: { Action: "Eval" },
    })
    const res = await hb.computeLegacy({ pid, slot })
    
    let i = 0
    while (i < 10) {
      const { slot } = await hb.schedule({
        pid,
        tags: { Action: "Inc" },
      })
      const res2 = await hb.computeLegacy({ pid, slot })
      assert.equal(res2.Messages[0].Data, `Count: ${++i}`)
    }
    
    const res3 = await hb.dryrun({ pid, action: "Get" })
    assert.equal(res3.Messages[0].Data, `Count: ${i}`)
 
    const res4 = await hb.messages({ pid, from: 0 })
    assert.equal(res4.edges.length, i + 2)
  })
})

Using WAO SDK

The AO class wraps the low-level HB methods with the same API you use for in-memory testing:

import assert from "assert"
import { describe, it, before, after } from "node:test"
import { HyperBEAM } from "wao/test"
import { AO } from "wao"
 
const src_data = `
local count = 0
Handlers.add("Inc", "Inc", function (msg)
  count = count + 1
  msg.reply({ Data = "Count: "..tostring(count) })
end)
Handlers.add("Get", "Get", function (msg)
  msg.reply({ Data = "Count: "..tostring(count) })
end)`
 
describe("Legacynet AOS with WAO SDK", function () {
  let hbeam, ao
  before(async () => {
    hbeam = await new HyperBEAM({ reset: true, genesis_wasm: true }).ready()
    ao = await new AO({ hb: hbeam.url }).init(hbeam.jwk)
  })
  after(async () => hbeam.kill())
 
  it("should deploy and interact", async () => {
    const { p } = await ao.deploy({ src_data })
    await p.m("Inc")
    assert.equal(await p.d("Get"), "Count: 1")
  })
})

Remote Deployment

Deploy to remote push nodes for production. Use push-1 through push-10 for full compute support:

import { AO } from "wao"
import fs from "fs"
 
const jwk = JSON.parse(fs.readFileSync(".wallet.json", "utf8"))
const ao = await new AO({ hb: "https://push-1.forward.computer" }).init(jwk)
 
const { p, pid } = await ao.deploy({ src_data })
console.log("Process ID:", pid)
 
await p.m("Inc")
const { out } = await p.msg("Get")
console.log(out) // "Count: 1"