Skip to content

Commit

Permalink
Merge pull request #23 from ar-io/PE-6656-release-name
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler authored Oct 16, 2024
2 parents d3b0598 + b07e874 commit a6b3506
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 2 deletions.
33 changes: 33 additions & 0 deletions src/common/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ function ant.init()
Records = "Records",
State = "State",
Evolve = "Evolve",
-- IO Network Contract Handlers
ReleaseName = "Release-Name",
}

local TokenSpecActionMap = {
Expand Down Expand Up @@ -450,6 +452,37 @@ function ant.init()
SourceCodeTxId = srcCodeTxId
end
)

-- IO Network Contract Handlers
Handlers.add(camel(ActionMap.ReleaseName), utils.hasMatchingTag("Action", ActionMap.ReleaseName), function(msg)
local assertHasPermission, permissionErr = pcall(utils.validateOwner, msg.From)
if assertHasPermission == false then
return ao.send({
Target = msg.From,
Action = "Invalid-Release-Name-Notice",
Data = permissionErr,
Error = "Release-Name-Error",
})
end

local name = string.lower(msg.Tags["Name"])
local ioProcess = msg.Tags["IO-Process-Id"]

-- send the release message to the provided IO Process Id
ao.send({
Target = ioProcess,
Action = "Release-Name-Notice",
Initiator = msg.From,
Name = name,
})

ao.send({
Target = msg.From,
Action = "Release-Name-Notice",
Initiator = msg.From,
Name = name,
})
end)
end

return ant
1 change: 1 addition & 0 deletions test/info.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('aos Info', async () => {
'setTicker',
'initializeState',
'state',
'releaseName',
]);
});

Expand Down
93 changes: 93 additions & 0 deletions test/io.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
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('IO Network Updates', 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 send update to io network when a name is released', async () => {
const result = await handle({
Tags: [
{ name: 'Action', value: 'Release-Name' },
{ name: 'IO-Process-Id', value: 'io-process-id-'.padEnd(43, '1') },
{ name: 'Name', value: 'name' },
],
});

// two messages should be sent - one to the io process and one to the sender
assert(result.Messages?.length === 2, 'Expected two messages');

const message = result.Messages[0]?.Tags?.find(
(tag) => tag.name === 'Action' && tag.value === 'Release-Name-Notice',
);
assert(message, 'Release-Name-Notice message not found');

// ensure a message Target is the IO process id provided and the other is to the sender and both contain the initiator, name and process id
assert(
result.Messages.some(
(msg) =>
msg.Target === 'io-process-id-'.padEnd(43, '1') &&
msg.Tags.find(
(tag) => tag.name === 'Initiator' && tag.value === STUB_ADDRESS,
) &&
msg.Tags.find((tag) => tag.name === 'Name' && tag.value === 'name') &&
msg.Tags.find(
(tag) =>
tag.name === 'Action' && tag.value === 'Release-Name-Notice',
),
),
);

assert(
result.Messages.some(
(msg) =>
msg.Target === STUB_ADDRESS &&
msg.Tags.find(
(tag) => tag.name === 'Initiator' && tag.value === STUB_ADDRESS,
) &&
msg.Tags.find((tag) => tag.name === 'Name' && tag.value === 'name') &&
msg.Tags.find(
(tag) =>
tag.name === 'Action' && tag.value === 'Release-Name-Notice',
),
),
);
});

it('should send a release-name-error-notice if the sender is not the owner', async () => {
const result = await handle({
Tags: [
{ name: 'Action', value: 'Release-Name' },
{ name: 'IO-Process-Id', value: 'io-process-id' },
{ name: 'Name', value: 'name' },
],
From: 'not-owner',
Owner: 'not-owner',
});

// assert no other messages
assert(result.Messages?.length === 1, 'Expected only one message');

const error = result.Messages[0]?.Tags.find(
(tag) => tag.name === 'Error' && tag.value === 'Release-Name-Error',
);
assert(error, 'Release-Name-Error message not found');
});
});
6 changes: 4 additions & 2 deletions tools/constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const STUB_ADDRESS = ''.padEnd(43, '1');
const STUB_PROCESS_ID = 'process-id-'.padEnd(43, '1');
const STUB_ADDRESS = 'arweave-address-'.padEnd(43, '1');
/* ao READ-ONLY Env Variables */
const AO_LOADER_HANDLER_ENV = {
Process: {
Id: ''.padEnd(43, '1'),
Id: STUB_PROCESS_ID,
Owner: STUB_ADDRESS,
Tags: [
{ name: 'Authority', value: 'XXXXXX' },
Expand Down Expand Up @@ -84,6 +85,7 @@ export {
AO_LOADER_OPTIONS,
AO_LOADER_HANDLER_ENV,
STUB_ADDRESS,
STUB_PROCESS_ID,
DEFAULT_HANDLE_OPTIONS,
ANT_EVAL_OPTIONS,
};

0 comments on commit a6b3506

Please sign in to comment.