Skip to content

Commit

Permalink
Merge pull request #5 from ar-io/PE-6314-evolve-ant
Browse files Browse the repository at this point in the history
feat(evolve): prepend evolve handler to set txid on eval handler
  • Loading branch information
atticusofsparta authored Jul 15, 2024
2 parents ed04907 + b745d7d commit 3be161e
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

### Added

- Evolve capabilities and handlers.
18 changes: 18 additions & 0 deletions changelogs/template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Changelog

### Added

- Feature A
- Feature B

### Changed

- Updated component X to use Y.

### Fixed

- Bug in module Z.

### Deprecated

- Handler X is deprecated
26 changes: 25 additions & 1 deletion src/common/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ function ant.init()
Denomination = Denomination or 0
TotalSupply = TotalSupply or 1
Initialized = Initialized or false
-- INSERT placeholder used by build script to inject the appropriate ID
SourceCodeTxId = SourceCodeTxId or "__INSERT_SOURCE_CODE_ID__"

local ActionMap = {
-- write
Expand All @@ -38,6 +40,7 @@ function ant.init()
Record = "Record",
Records = "Records",
State = "State",
Evolve = "Evolve",
}

local TokenSpecActionMap = {
Expand Down Expand Up @@ -155,6 +158,7 @@ function ant.init()
Logo = Logo,
Denomination = tostring(Denomination),
Owner = Owner,
["Source-Code-TX-ID"] = SourceCodeTxId,
}
ao.send({
Target = msg.From,
Expand Down Expand Up @@ -412,6 +416,7 @@ function ant.init()
Denomination = Denomination,
TotalSupply = TotalSupply,
Initialized = Initialized,
["Source-Code-TX-ID"] = SourceCodeTxId,
}

-- Add forwarded tags to the records notice messages
Expand All @@ -421,9 +426,28 @@ function ant.init()
state[tagName] = tagValue
end
end

ao.send({ Target = msg.From, Action = "State-Notice", Data = json.encode(state) })
end)

Handlers.prepend(camel(ActionMap.Evolve), utils.hasMatchingTag("Action", "Eval"), function(msg)
local srcCodeTxId = msg.Tags["Source-Code-TX-ID"]
if not srcCodeTxId then
return
end
local srcCodeTxIdStatus, srcCodeTxIdResult = pcall(utils.validateArweaveId, srcCodeTxId)
if srcCodeTxIdStatus and not srcCodeTxIdStatus then
ao.send({
Target = msg.From,
Action = "Invalid-Evolve-Notice",
Error = "Evolve-Error",
["Message-Id"] = msg.Id,
Data = "Source-Code-TX-ID is required",
})
return
end
SourceCodeTxId = srcCodeTxId
end)
end

return ant
68 changes: 68 additions & 0 deletions test/evolve.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const { createAntAosLoader } = require('./utils');
const { describe, it } = require('node:test');
const assert = require('node:assert');
const {
AO_LOADER_HANDLER_ENV,
DEFAULT_HANDLE_OPTIONS,
STUB_ADDRESS,
BUNDLED_AOS_ANT_LUA,
} = require('../tools/constants');

describe('aos Evolve', 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 evolve the ant', async () => {
const srcCodeTxIdStub = ''.padEnd(43, '123-test');
const evolveResult = await handle({
Tags: [
{ name: 'Action', value: 'Eval' },
{ name: 'Source-Code-TX-ID', value: srcCodeTxIdStub },
],
Data: BUNDLED_AOS_ANT_LUA,
});

const result = await handle(
{
Tags: [{ name: 'Action', value: 'Info' }],
},
evolveResult.Memory,
);

const state = JSON.parse(result.Messages[0].Data);
assert(state);
assert(state['Source-Code-TX-ID'] === srcCodeTxIdStub);
});

it('should not evolve the ant', async () => {
const evolveResult = await handle({
Tags: [
{ name: 'Action', value: 'Eval' },
// omit src code id
],
Data: BUNDLED_AOS_ANT_LUA,
});

const result = await handle(
{
Tags: [{ name: 'Action', value: 'Info' }],
},
evolveResult.Memory,
);

const state = JSON.parse(result.Messages[0].Data);
assert(state);
assert(state['Source-Code-TX-ID'] === '__INSERT_SOURCE_CODE_ID__');
});
});
25 changes: 24 additions & 1 deletion tools/publish-aos.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const Arweave = require('arweave');
const path = require('path');
const fs = require('fs');

const srcCodeTxIdPlaceholder = '__INSERT_SOURCE_CODE_ID__';

const arweave = Arweave.init({
host: 'arweave.net',
port: 443,
Expand All @@ -16,13 +18,34 @@ async function main() {
const jwk = JSON.parse(wallet);
const address = await arweave.wallets.jwkToAddress(jwk);

const tx1 = await arweave.createTransaction({ data: bundledLua }, jwk);
tx1.addTag('App-Name', 'aos-LUA');
tx1.addTag('App-Version', '0.0.1');
tx1.addTag('Content-Type', 'text/x-lua');
tx1.addTag('Author', 'Permanent Data Solutions');
await arweave.transactions.sign(tx1, jwk);
await arweave.transactions.post(tx1);

// replace placeholder with actual tx id
const bundledLuaWithTxId = bundledLua.replace(srcCodeTxIdPlaceholder, tx1.id);

console.log(`Publish AOS ANT Lua with address ${address}`);

const tx = await arweave.createTransaction({ data: bundledLua }, jwk);
const tx = await arweave.createTransaction({ data: bundledLuaWithTxId }, jwk);
tx.addTag('App-Name', 'aos-LUA');
tx.addTag('App-Version', '0.0.1');
tx.addTag('Content-Type', 'text/x-lua');
tx.addTag('Author', 'Permanent Data Solutions');
tx.addTag('Originial-Tx-Id', tx1.id);
tx.addTag(
'Changelog',
`# Changelog
### Added
- Evolve capabilities and handlers.
`,
);
await arweave.transactions.sign(tx, jwk);
await arweave.transactions.post(tx);

Expand Down

0 comments on commit 3be161e

Please sign in to comment.