Skip to content

Commit

Permalink
Merge pull request #39 from ar-io/remove-sorted-records-change
Browse files Browse the repository at this point in the history
fix(records): remove change on records return sorted
  • Loading branch information
atticusofsparta authored Nov 22, 2024
2 parents b2476f4 + 6f6e25b commit 258a7d2
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 42 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

- Refactored handlers to use a util that codifies responses on calls.
- Added documentation with luadoc types for improved linting.
- Records are now returned as an alphabetically sorted array of [{name, transactionId, ttlSeconds}] with the '@' record being the first.
- Removed `Evolve` handler and SourceCodeTxId from state and state responses.

### Fixed
Expand Down
7 changes: 1 addition & 6 deletions spec/ant_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,13 @@ describe("Arweave Name Token", function()
end)

it("gets all records", function()
_G.Records["zed"] = {
transactionId = string.rep("1", 43),
ttlSeconds = 3600,
}
_G.Records["@"] = {
transactionId = string.rep("1", 43),
ttlSeconds = 3600,
}
local recordEntries = records.getRecords()

assert.are.same(recordEntries[1].name, "@")
assert.are.same(recordEntries[#recordEntries].name, "zed")
assert(recordEntries["@"])
end)

it("removes a record", function()
Expand Down
23 changes: 2 additions & 21 deletions src/common/records.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,15 @@ end

--- Get all records from the ANT
---@alias RecordEntry {
--- name: string,
--- transactionId: string,
--- ttlSeconds: integer,
---}
---@return table<RecordEntry> The sorted records of the ANT
---@return table<string, RecordEntry> The sorted records of the ANT
function records.getRecords()
local antRecords = utils.deepCopy(Records)
assert(antRecords, "Failed to copy Records")

---@type table<RecordEntry>
local recordEntries = {}

for undername, record in pairs(antRecords) do
local entry = record
entry.name = undername
table.insert(recordEntries, entry)
end
table.sort(recordEntries, function(a, b)
if a.name == "@" then
return true
end
if b.name == "@" then
return false
end
return a.name < b.name
end)

return recordEntries
return antRecords
end

return records
17 changes: 3 additions & 14 deletions test/records.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,9 @@ describe('aos Records', async () => {

it('should get the records of the ant', async () => {
const setRecordRes = await setRecord({ name: 'test-1' });
const setRecordRes2 = await setRecord(
{ name: 'test-2' },
setRecordRes.Memory,
);
const setRecordRes3 = await setRecord(
{ name: 'test-3' },
setRecordRes2.Memory,
);

const records = await getRecords(setRecordRes3.Memory);
const records = await getRecords(setRecordRes);
assert(records);

assert.strictEqual(records[0].name, '@');
assert.strictEqual(records.at(-1).name, 'test-3');
});

it('should get a singular record of the ant', async () => {
Expand Down Expand Up @@ -100,7 +89,7 @@ describe('aos Records', async () => {
);

const records = JSON.parse(recordsResult.Messages[0].Data);
const record = records[0];
const record = records['@'];
assert(record.transactionId === ''.padEnd(43, '3'));
assert(record.ttlSeconds === 3600);
});
Expand Down Expand Up @@ -154,7 +143,7 @@ describe('aos Records', async () => {
);

const records = JSON.parse(recordsResult.Messages[0].Data);
const record = records.find((r) => r.name == 'timmy');
const record = records['timmy'];
assert(record.transactionId === ''.padEnd(43, '3'));
assert(record.ttlSeconds === 3600);
});
Expand Down

0 comments on commit 258a7d2

Please sign in to comment.