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

DRAFT: Memory safety #73

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Fixed

- Added msg.reply to all APIs

## [13] - [zh4at_Y_GKJMD3SOkZ5Yx7mG2JRRLc89huEOspPtHq4] - (2025-1-29)

### Fixed

- Removed print with ID in Handler wrapper since ID can be null on dry runs.

## [12] - [mwCMAjglwV_96oEMEIi5epg_QXElOMzEcLkCUeQyGGo] - (2025-1-28)
Expand Down
45 changes: 22 additions & 23 deletions src/common/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ function ant.init()
local recipient = msg.Tags.Recipient
if msg.From ~= Owner then
utils.Send(msg, {
Target = msg.From,
Action = "Transfer-Error",
["Message-Id"] = msg.Id,
Error = "Insufficient Balance!",
Expand All @@ -108,6 +109,7 @@ function ant.init()
local balRes = balances.balance(addressToCheck, msg.Tags["Allow-Unsafe-Addresses"])

utils.Send(msg, {
Target = msg.From,
Action = "Balance-Notice",
Balance = tostring(balRes),
Ticker = Ticker,
Expand All @@ -123,13 +125,14 @@ function ant.init()
bals[k] = tostring(v)
end

utils.Send(msg, { Data = json.encode(bals) })
utils.Send(msg, { Target = msg.From, Data = json.encode(bals) })
end)

createActionHandler(TokenSpecActionMap.TotalSupply, function(msg)
assert(msg.From ~= ao.id, "Cannot call Total-Supply from the same process!")

utils.Send(msg, {
Target = msg.From,
Action = "Total-Supply",
Data = tostring(TotalSupply),
Ticker = Ticker,
Expand All @@ -149,6 +152,7 @@ function ant.init()
Handlers = utils.getHandlerNames(Handlers),
}
utils.Send(msg, {
Target = msg.From,
Action = "Info-Notice",
Tags = info,
Data = json.encode(info),
Expand Down Expand Up @@ -225,8 +229,8 @@ function ant.init()
return initialize.initializeANTState(msg.Data)
end)

createActionHandler(ActionMap.State, function(msg)
notices.notifyState(msg, msg.From)
createActionHandler(ActionMap.State, function()
return utils.getState()
end)

-- IO Network Contract Handlers
Expand All @@ -248,7 +252,7 @@ function ant.init()
Name = name,
})

ao.send({
utils.Send(msg, {
Target = msg.From,
Action = "Release-Name-Notice",
Initiator = msg.From,
Expand All @@ -275,7 +279,7 @@ function ant.init()
["Process-Id"] = antProcessIdToReassign,
})

ao.send({
utils.Send(msg, {
Target = msg.From,
Action = "Reassign-Name-Notice",
Initiator = msg.From,
Expand Down Expand Up @@ -329,6 +333,7 @@ function ant.init()
AOS provides a _boot handler that is designed to load Lua code on boot.
This handler OVERRIDES this and replaces it with our ANT state initialization handler.

NOTE: if we use utils.Send here memory blows up for some reason
]]
Handlers.once("_boot", function(msg)
return msg.Tags.Type == "Process" and Owner == msg.From
Expand All @@ -339,28 +344,22 @@ function ant.init()
initialize.initializeANTState(msg.Data)
end, utils.errorHandler)
if not status then
utils.Send(
msg,
notices.addForwardedTags(msg, {
Target = Owner,
Error = res or "",
Data = res or "",
Action = "Invalid-Boot-Notice",
["Message-Id"] = msg.Id,
})
)
ao.send(notices.addForwardedTags(msg, {
Target = Owner,
Error = res or "",
Data = res or "",
Action = "Invalid-Boot-Notice",
["Message-Id"] = msg.Id,
}))
end
end

if Owner then
utils.Send(
msg,
notices.credit({
From = msg.From,
Sender = Owner,
Recipient = Owner,
})
)
ao.send(notices.credit({
From = msg.From,
Sender = Owner,
Recipient = Owner,
}))
end

if AntRegistryId then
Expand Down
1 change: 1 addition & 0 deletions test/evolve.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('aos Evolve', async () => {
},
evolveResult.Memory,
);

assert(evalResult.Output.data);
assert(evalResult.Output.data.includes('info'));
});
Expand Down
40 changes: 40 additions & 0 deletions test/memory/json.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { createAntAosLoader } from '../utils.mjs';
import { describe, it } from 'node:test';
import assert from 'node:assert';
import {
AO_LOADER_HANDLER_ENV,
DEFAULT_HANDLE_OPTIONS,
} from '../../tools/constants.mjs';

describe('JSON limits', 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 not be able to parse a large json', async () => {
const data = JSON.stringify(
new Array(1).fill({
transactionId: ''.padEnd(43, '1'),
ttlSeconds: 3600,
name: '@',
}),
);
console.log('parsing data size: ' + data.length);
const result = await handle({
Data: data,
Tags: [{ name: 'Content-Type', value: 'application/json' }],
});

console.dir(result, { depth: null });
});
});
21 changes: 21 additions & 0 deletions test/records.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,25 @@ describe('aos Records', async () => {
assert(record.transactionId === ''.padEnd(43, '3'));
assert(record.ttlSeconds === 900);
});

it('should be able to set 3k records', async () => {
let recordMem = startMemory;
for (let i = 0; i < 10_000; i++) {
if (!(i % 100)) {
console.log(i + '/10k');
}
const res = await setRecord(
{
name: i.toString(),
transactionId: ''.padEnd(43, '1'),
ttl: 900,
},
recordMem,
);
recordMem = res.Memory;
}
const recordsRes = await getRecords(recordMem);
console.log('retrieved ' + Object.keys(recordsRes).length + ' records');
assert(recordsRes, 'unable to get records');
});
});
2 changes: 1 addition & 1 deletion version.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// TODO: make this an auto generated file

export default '12';
export default '13';
Loading