Skip to content

Commit

Permalink
fix(records): set default priority of 0 for @ records
Browse files Browse the repository at this point in the history
  • Loading branch information
atticusofsparta committed Feb 7, 2025
1 parent e0a6fba commit 194c94d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
14 changes: 13 additions & 1 deletion spec/ant_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,24 @@ describe("Arweave Name Token", function()
assert.are.same(_G.Records[name].priority, priority)
end)

it("fails to set @ record with priority order", function()
it("fails to set @ record with priority order greater than 0", function()
local name, transactionId, ttlSeconds, priority = "@", fake_address, 60, 1
local status, _ = pcall(records.setRecord, name, transactionId, ttlSeconds, priority)
assert.is_false(status)
end)

it("sets @ record with priority order of 0", function()
local name, transactionId, ttlSeconds, priority = "@", fake_address, 60, 0
records.setRecord(name, transactionId, ttlSeconds, priority)
assert.are.same(_G.Records[name].priority, priority)
end)

it("sets @ record with priority order of 0 when priority is nil", function()
local name, transactionId, ttlSeconds, priority = "@", fake_address, 60, nil
records.setRecord(name, transactionId, ttlSeconds, priority)
assert.are.same(_G.Records[name].priority, 0)
end)

it("gets all records", function()
_G.Records["@"] = {
transactionId = string.rep("1", 43),
Expand Down
12 changes: 7 additions & 5 deletions src/common/records.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,29 @@ Records = Records
["@"] = {
transactionId = "-k7t8xMoB8hW482609Z9F4bTFMC3MnuW8bTvTyT8pFI",
ttlSeconds = 900,
priority = nil,
priority = 0,
},
}

--- Set a record in the Records of the ANT.
---@param name string The name of the record.
---@param transactionId string The transaction ID of the record.
---@param ttlSeconds number The time-to-live in seconds for the record.
---@param priority integer|nil The sort order of the record
---@param priority integer|nil The sort order of the record - must be nil or 1 or greater
---@return Record
function records.setRecord(name, transactionId, ttlSeconds, priority)
utils.validateUndername(name)
assert(utils.isValidArweaveAddress(transactionId), "Invalid Arweave ID")
utils.validateTTLSeconds(ttlSeconds)
assert(
priority == nil or math.type(priority) == "integer",
"Priority must be an integer or nil, received " .. tostring(priority)
priority == nil
or (name == "@" and priority == 0)
or (name ~= "@" and math.type(priority) == "integer" and priority > 0),
"Priority must be an integer greater than 0, or nil, but received " .. tostring(priority)
)
-- TODO: verify this is a desired behaviour
if name == "@" then
assert(priority == nil, "Cannot set priority to @ record")
priority = 0
end

collectgarbage("stop")
Expand Down

0 comments on commit 194c94d

Please sign in to comment.