Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(validate): draw out initial validate function #50

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/common/main.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local ant = {}
local ant = { _version = "0.0.1" }

function ant.init()
-- main.lua
Expand All @@ -13,6 +13,7 @@ function ant.init()
local initialize = require(".common.initialize")
local records = require(".common.records")
local controllers = require(".common.controllers")
local validate = require(".common.validate")

---@alias Owner string
---@description The owner of the ANT
Expand Down Expand Up @@ -70,7 +71,7 @@ function ant.init()
Record = "Record",
Records = "Records",
State = "State",
Evolve = "Evolve",
ValidateHandlers = "Validate-Handlers",
-- IO Network Contract Handlers
ReleaseName = "Release-Name",
ReassignName = "Reassign-Name",
Expand Down Expand Up @@ -313,6 +314,10 @@ function ant.init()
Names = msg.Tags.Names,
})
end)

createActionHandler(ActionMap.ValidateHandlers, function(msg)
return validate.validateHandlers(msg, ao)
end)
end

return ant
16 changes: 13 additions & 3 deletions src/common/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ function utils.hasMatchingTag(tag, value)
return Handlers.utils.hasMatchingTag(tag, value)
end

--- Deep copies a table
--- Deep copies a table, handling circular references
--- @param original table The table to copy
--- @param seen table|nil Internal table to track already copied references
--- @return table|nil The deep copy of the table or nil if the original is nil
function utils.deepCopy(original)
function utils.deepCopy(original, seen)
if not original then
return nil
end
Expand All @@ -31,14 +32,23 @@ function utils.deepCopy(original)
return original
end

-- Use the `seen` table to track circular references
seen = seen or {}
if seen[original] then
return seen[original] -- Return the already copied table for circular references
end

local copy = {}
seen[original] = copy -- Track the copy of this table

for key, value in pairs(original) do
if type(value) == "table" then
copy[key] = utils.deepCopy(value) -- Recursively copy the nested table
copy[key] = utils.deepCopy(value, seen) -- Recursively copy the nested table
else
copy[key] = value
end
end

return copy
end

Expand Down
30 changes: 30 additions & 0 deletions src/common/validate.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
local utils = require(".common.utils")
local json = require(".common.json")
local validate = {}

---@param msg AoMessage AO messages to evaluate
---@param env table the AO env object
function validate.validateHandlers(msg, env)
utils.validateOwner(msg.From)
local messages = json.decode(msg.Data)

local results = {}

local stateBackup = json.decode(json.encode(utils.getState()))

for name, message in pairs(messages) do
local process = package.loaded[".process"]
local _, res = pcall(process.handle, message, env)
ao.clearOutbox()
results[name] = { message = message, result = res }

--- reset state
for k, v in pairs(stateBackup) do
_G[k] = v
end
end

return results
end

return validate
2 changes: 1 addition & 1 deletion test/info.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('aos Info', async () => {
assert(processInfo.Handlers);
assert.deepStrictEqual(processInfo.Handlers, [
'_eval',
'_default',
'_default',
'transfer',
'balance',
'balances',
Expand Down
50 changes: 50 additions & 0 deletions test/validation.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { createAntAosLoader } from './utils.mjs';
import { describe, it } from 'node:test';
import assert from 'node:assert';
import {
AO_LOADER_HANDLER_ENV,
DEFAULT_HANDLE_OPTIONS,
STUB_ADDRESS,
} from '../tools/constants.mjs';

describe('aos Validate', async () => {
const { handle: originalHandle, memory: startMemory } =
await createAntAosLoader();

async function handle(options = {}, mem = startMemory) {
return originalHandle(
mem,
{
...DEFAULT_HANDLE_OPTIONS,
...options,
},
AO_LOADER_HANDLER_ENV,
);
}

it('should validate Transfer', async () => {
const messages = {
Transfer: {
...DEFAULT_HANDLE_OPTIONS,
Tags: [
{ name: 'Action', value: 'Transfer' },
{ name: 'Recipient', value: 'STUB_ADDRESS'.padEnd(43, '1') },
],
},
Transfer2: {
...DEFAULT_HANDLE_OPTIONS,
Tags: [
{ name: 'Action', value: 'Transfer' },
{ name: 'Recipient', value: 'STUB_ADDRESS'.padEnd(43, '1') },
],
},
};

const res = await handle({
Tags: [{ name: 'Action', value: 'Validate-Handlers' }],
Data: JSON.stringify(messages),
});

console.dir(JSON.parse(res.Messages[0].Data), { depth: null });
});
});
Loading