Skip to content

Commit

Permalink
fix: disallow create prmary name req during a records grace period PE…
Browse files Browse the repository at this point in the history
…-7326 (#284)
  • Loading branch information
fedellen authored Dec 18, 2024
2 parents 4de7fd4 + 102df6d commit 54d3f5e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
29 changes: 29 additions & 0 deletions spec/arns_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,35 @@ describe("arns", function()
assert.is_false(status)
assert.match("Name must be extended before it can be reassigned", error)
end)

it("should not allow reassigning names during the grace period", function()
-- Setup record in grace period
_G.NameRegistry.records["test-name"] = {
endTimestamp = 123456789,
processId = testProcessId,
purchasePrice = 600000000,
startTimestamp = 0,
type = "lease",
undernameLimit = 10,
}

-- Attempt to reassign
local newProcessId = "test-this-is-valid-arweave-wallet-address-2"
local status, error = pcall(
arns.reassignName,
"test-name",
testProcessId,
-- Just before the grace period ends
123456789
+ constants.gracePeriodMs
- 1,
newProcessId
)

-- Assertions
assert.is_false(status)
assert.match("Name must be extended before it can be reassigned", error)
end)
end)
end

Expand Down
20 changes: 20 additions & 0 deletions spec/primary_names_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ describe("Primary Names", function()
)
end)

it("should fail if the arns record is in its grace period", function()
_G.NameRegistry.records = {
["test"] = {
processId = "base-name-owner",
type = "lease",
endTimestamp = 1234567890,
},
}
local status, err = pcall(
primaryNames.createPrimaryNameRequest,
"test",
"user-requesting-primary-name",
-- Just after grace period starts
1234567890 + 1,
"test-msg-id"
)
assert.is_false(status)
assert.match("ArNS record 'test' is not active", err)
end)

it(
"should create a primary name request and transfer the cost from the initiator to the protocol balance",
function()
Expand Down
13 changes: 6 additions & 7 deletions src/arns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ function arns.pruneReservedNames(currentTimestamp)
end

--- Asserts that a name can be reassigned
--- @param record StoredRecord The record to check
--- @param record StoredRecord | nil The record to check
--- @param currentTimestamp number The current timestamp
--- @param from string The address of the sender
--- @param newProcessId string The new process id
Expand All @@ -1055,11 +1055,11 @@ function arns.assertValidReassignName(record, currentTimestamp, from, newProcess
assert(record.processId == from, "Not authorized to reassign this name")

if record.endTimestamp then
local isWithinGracePeriod = record.endTimestamp < currentTimestamp
and record.endTimestamp + constants.gracePeriodMs > currentTimestamp
local isExpired = record.endTimestamp + constants.gracePeriodMs < currentTimestamp
assert(not isWithinGracePeriod, "Name must be extended before it can be reassigned")
assert(not isExpired, "Name is expired")
assert(
not arns.recordInGracePeriod(record, currentTimestamp),
"Name must be extended before it can be reassigned"
)
assert(not arns.recordExpired(record, currentTimestamp), "Name is expired")
end

return true
Expand All @@ -1075,7 +1075,6 @@ end
function arns.reassignName(name, from, currentTimestamp, newProcessId, allowUnsafeProcessId)
allowUnsafeProcessId = allowUnsafeProcessId or false
local record = arns.getRecord(name)
assert(record, "Name is not registered")
arns.assertValidReassignName(record, currentTimestamp, from, newProcessId, allowUnsafeProcessId)
local updatedRecord = arns.modifyProcessId(name, newProcessId)
return updatedRecord
Expand Down
1 change: 1 addition & 0 deletions src/primary_names.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function primaryNames.createPrimaryNameRequest(name, initiator, timestamp, msgId

local record = arns.getRecord(baseName)
assert(record, "ArNS record '" .. baseName .. "' does not exist")
assert(arns.recordIsActive(record, timestamp), "ArNS record '" .. baseName .. "' is not active")

local requestCost = arns.getTokenCost({
intent = "Primary-Name-Request",
Expand Down

0 comments on commit 54d3f5e

Please sign in to comment.