Skip to content

Commit

Permalink
fix(ints): add integer checks and tests (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler authored Jun 24, 2024
2 parents 60d8711 + 766451f commit feeb8e2
Show file tree
Hide file tree
Showing 9 changed files with 378 additions and 59 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"scripts": {
"build": "node tools/bundle-aos.js",
"test": "node --test --test-concurrency 1 **/*.test.js"
"test": "yarn build && node --test --test-concurrency 1 **/*.test.js"
},
"devDependencies": {
"prettier": "^3.3.2",
Expand Down
17 changes: 17 additions & 0 deletions spec/arns_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,21 @@ describe("arns", function()
assert.match("Cannot extend lease beyond 5 years", error)
end)
end)

describe("calculateLeaseFee", function()
it("should return the correct fee for a lease", function()
local name = "test-name" -- 9 character name
local baseFee = demand.getFees()[#name] -- base fee is 500 IO
local fee = arns.calculateRegistrationFee("lease", baseFee, 1, 1)
assert.are.equal(600000000, fee)
end)

it("should return the correct fee for a permabuy", function()
local name = "test-name" -- 9 character name
local baseFee = demand.getFees()[#name] -- base fee is 500 IO
local fee = arns.calculateRegistrationFee("permabuy", baseFee, 1, 1)
local expected = (baseFee * 0.2 * 20) + baseFee
assert.are.equal(expected, fee)
end)
end)
end)
12 changes: 9 additions & 3 deletions spec/gar_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ describe("gar", function()
},
}

local status, result = pcall(gar.leaveNetwork, "test-this-is-valid-arweave-wallet-address-1", startTimestamp, "msgId")
local status, result =
pcall(gar.leaveNetwork, "test-this-is-valid-arweave-wallet-address-1", startTimestamp, "msgId")
assert.is_true(status)
assert.are.same(result, {
operatorStake = 0,
Expand Down Expand Up @@ -266,8 +267,13 @@ describe("gar", function()
status = "joined",
observerAddress = "observerAddress",
}
local status, result =
pcall(gar.decreaseOperatorStake, "test-this-is-valid-arweave-wallet-address-1", 1000, startTimestamp, "msgId")
local status, result = pcall(
gar.decreaseOperatorStake,
"test-this-is-valid-arweave-wallet-address-1",
1000,
startTimestamp,
"msgId"
)
assert.is_true(status)
assert.are.same(result, {
operatorStake = gar.getSettings().operators.minStake,
Expand Down
3 changes: 2 additions & 1 deletion src/arns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ function arns.calculateUndernameCost(baseFee, increaseQty, registrationType, yea
undernamePercentageFee = constants.UNDERNAME_LEASE_FEE_PERCENTAGE
elseif registrationType == "permabuy" then
undernamePercentageFee = constants.UNDERNAME_PERMABUY_FEE_PERCENTAGE
else
error("Invalid registration type")
end

local totalFeeForQtyAndYears = baseFee * undernamePercentageFee * increaseQty * years
Expand Down Expand Up @@ -340,7 +342,6 @@ function arns.getTokenCost(intendedAction)
if not record then
error("Name is not registered")
end
local qty = intendedAction.quantity
local yearsRemaining = constants.PERMABUY_LEASE_FEE_LENGTH
if record.type == "lease" then
yearsRemaining = arns.calculateYearsBetweenTimestamps(currentTimestamp, record.endTimestamp)
Expand Down
22 changes: 18 additions & 4 deletions src/gar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -485,14 +485,25 @@ function gar.assertValidGatewayParameters(from, stake, settings, observerAddress
assert(type(settings) == "table", "settings is required and must be a table!")
assert(type(observerAddress) == "string", "observerAddress is required and must be a string!")
assert(type(settings.allowDelegatedStaking) == "boolean", "allowDelegatedStaking must be a boolean")
assert(type(settings.minDelegatedStake) == "number", "minDelegatedStake must be a number")
assert(type(settings.label) == "string", "label is required and must be a string")
assert(type(settings.fqdn) == "string", "fqdn is required and must be a string")
assert(type(settings.protocol) == "string", "protocol is required and must be a string")
assert(type(settings.port) == "number", "port is required and must be a number")
assert(
type(settings.port) == "number"
and utils.isInteger(settings.port)
and settings.port >= 0
and settings.port <= 65535,
"port is required and must be an integer between 0 and 65535"
)
assert(type(settings.properties) == "string", "properties is required and must be a string")
if settings.delegateRewardShareRatio ~= nil then
assert(type(settings.delegateRewardShareRatio) == "number", "delegateRewardShareRatio must be a number")
assert(
type(settings.delegateRewardShareRatio) == "number"
and utils.isInteger(settings.delegateRewardShareRatio)
and settings.delegateRewardShareRatio >= 0
and settings.delegateRewardShareRatio <= 100,
"delegateRewardShareRatio must be an integer between 0 and 100"
)
end
if settings.autoStake ~= nil then
assert(type(settings.autoStake) == "boolean", "autoStake must be a boolean")
Expand All @@ -501,7 +512,10 @@ function gar.assertValidGatewayParameters(from, stake, settings, observerAddress
assert(type(settings.properties) == "string", "properties must be a table")
end
if settings.minDelegatedStake ~= nil then
assert(type(settings.minDelegatedStake) == "number", "minDelegatedStake must be a number")
assert(
type(settings.minDelegatedStake) == "number" and utils.isInteger(settings.minDelegatedStake),
"minDelegatedStake must be an integer and "
)
end
end

Expand Down
120 changes: 80 additions & 40 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ Handlers.add(ActionMap.Transfer, utils.hasMatchingTag("Action", ActionMap.Transf
-- assert recipient is a valid arweave address
local function checkAssertions()
assert(utils.isValidArweaveAddress(msg.Tags.Recipient), "Invalid recipient")
assert(tonumber(msg.Tags.Quantity) > 0, "Invalid quantity")
assert(
tonumber(msg.Tags.Quantity) > 0 and utils.isInteger(tonumber(msg.Tags.Quantity)),
"Invalid quantity. Must be integer greater than 0"
)
end

local inputStatus, inputResult = pcall(checkAssertions)
Expand All @@ -99,26 +102,26 @@ Handlers.add(ActionMap.Transfer, utils.hasMatchingTag("Action", ActionMap.Transf
-- Debit-Notice message template, that is sent to the Sender of the transfer
local debitNotice = {
Target = msg.From,
Action = 'Debit-Notice',
Action = "Debit-Notice",
Recipient = msg.Recipient,
Quantity = msg.Quantity,
Data = "You transferred " .. msg.Quantity .. " to " .. msg.Recipient
Data = "You transferred " .. msg.Quantity .. " to " .. msg.Recipient,
}
-- Credit-Notice message template, that is sent to the Recipient of the transfer
local creditNotice = {
Target = msg.Recipient,
Action = 'Credit-Notice',
Action = "Credit-Notice",
Sender = msg.From,
Quantity = msg.Quantity,
Data = "You received " .. msg.Quantity .. " from " .. msg.From
Data = "You received " .. msg.Quantity .. " from " .. msg.From,
}

-- Add forwarded tags to the credit and debit notice messages
for tagName, tagValue in pairs(msg) do
-- Tags beginning with "X-" are forwarded
if string.sub(tagName, 1, 2) == "X-" then
debitNotice[tagName] = tagValue
creditNotice[tagName] = tagValue
debitNotice[tagName] = tagValue
creditNotice[tagName] = tagValue
end
end

Expand All @@ -131,8 +134,14 @@ end)

Handlers.add(ActionMap.CreateVault, utils.hasMatchingTag("Action", ActionMap.CreateVault), function(msg)
local function checkAssertions()
assert(tonumber(msg.Tags.Quantity) > 0, "Invalid quantity")
assert(tonumber(msg.Tags.LockLength) > 0, "Invalid lock length")
assert(
tonumber(msg.Tags.LockLength) > 0 and utils.isInteger(tonumber(msg.Tags.LockLength)),
"Invalid lock length. Must be integer greater than 0"
)
assert(
tonumber(msg.Tags.Quantity) > 0 and utils.isInteger(tonumber(msg.Tags.Quantity)),
"Invalid quantity. Must be integer greater than 0"
)
end

local inputStatus, inputResult = pcall(checkAssertions)
Expand Down Expand Up @@ -165,8 +174,14 @@ end)
Handlers.add(ActionMap.VaultedTransfer, utils.hasMatchingTag("Action", ActionMap.VaultedTransfer), function(msg)
local function checkAssertions()
assert(utils.isValidArweaveAddress(msg.Tags.Recipient), "Invalid recipient")
assert(tonumber(msg.Tags.Quantity) > 0, "Invalid quantity")
assert(tonumber(msg.Tags.LockLength) > 0, "Invalid lock length")
assert(
tonumber(msg.Tags.LockLength) > 0 and utils.isInteger(tonumber(msg.Tags.LockLength)),
"Invalid lock length. Must be integer greater than 0"
)
assert(
tonumber(msg.Tags.Quantity) > 0 and utils.isInteger(tonumber(msg.Tags.Quantity)),
"Invalid quantity. Must be integer greater than 0"
)
end

local inputStatus, inputResult = pcall(checkAssertions)
Expand Down Expand Up @@ -210,8 +225,11 @@ end)

Handlers.add(ActionMap.ExtendVault, utils.hasMatchingTag("Action", ActionMap.ExtendVault), function(msg)
local checkAssertions = function()
assert(tonumber(msg.Tags.ExtendLength) > 0, "Invalid extend length")
assert(utils.isValidArweaveAddress(msg.Tags.VaultId), "Invalid vault id")
assert(
tonumber(msg.Tags.ExtendLength) > 0 and utils.isInteger(tonumber(msg.Tags.ExtendLength)),
"Invalid extension length. Must be integer greater than 0"
)
end

local inputStatus, inputResult = pcall(checkAssertions)
Expand Down Expand Up @@ -243,8 +261,11 @@ end)

Handlers.add(ActionMap.IncreaseVault, utils.hasMatchingTag("Action", ActionMap.IncreaseVault), function(msg)
local function checkAssertions()
assert(tonumber(msg.Tags.Quantity) > 0, "Invalid quantity")
assert(utils.isValidArweaveAddress(msg.Tags.VaultId), "Invalid vault id")
assert(
tonumber(msg.Tags.Quantity) > 0 and utils.isInteger(tonumber(msg.Tags.Quantity)),
"Invalid quantity. Must be integer greater than 0"
)
end

local inputStatus, inputResult = pcall(checkAssertions)
Expand Down Expand Up @@ -278,7 +299,10 @@ Handlers.add(ActionMap.BuyRecord, utils.hasMatchingTag("Action", ActionMap.BuyRe
local checkAssertions = function()
assert(type(msg.Tags.Name) == "string", "Invalid name")
assert(type(msg.Tags.PurchaseType) == "string", "Invalid purchase type")
assert(tonumber(msg.Tags.Years) > 0 and tonumber(msg.Tags.Years) < 5, "Invalid years")
assert(
tonumber(msg.Tags.Years) > 0 and tonumber(msg.Tags.Years) < 5 and utils.isInteger(tonumber(msg.Tags.Years)),
"Invalid years. Must be integer between 1 and 5"
)
end

local inputStatus, inputResult = pcall(checkAssertions)
Expand Down Expand Up @@ -323,7 +347,10 @@ end)
Handlers.add(ActionMap.ExtendLease, utils.hasMatchingTag("Action", ActionMap.ExtendLease), function(msg)
local checkAssertions = function()
assert(type(msg.Tags.Name) == "string", "Invalid name")
assert(tonumber(msg.Tags.Years) > 0 and tonumber(msg.Tags.Years) < 5, "Invalid years")
assert(
tonumber(msg.Tags.Years) > 0 and tonumber(msg.Tags.Years) < 5 and utils.isInteger(tonumber(msg.Tags.Years)),
"Invalid years. Must be integer between 1 and 5"
)
end

local inputStatus, inputResult = pcall(checkAssertions)
Expand Down Expand Up @@ -359,7 +386,12 @@ Handlers.add(
function(msg)
local checkAssertions = function()
assert(type(msg.Tags.Name) == "string", "Invalid name")
assert(tonumber(msg.Tags.Quantity) > 0, "Invalid quantity")
assert(
tonumber(msg.Tags.Quantity) > 0
and tonumber(msg.Tags.Quantity) < 9990
and utils.isInteger(msg.Tags.Quantity),
"Invalid quantity. Must be an integer value greater than 0 and less than 9990"
)
end

local inputStatus, inputResult = pcall(checkAssertions)
Expand Down Expand Up @@ -403,6 +435,15 @@ Handlers.add(ActionMap.TokenCost, utils.hasMatchingTag("Action", ActionMap.Token
.. msg.Tags.Intent
or "nil"
)
-- if years is provided, assert it is a number and integer between 1 and 5
if msg.Tags.Years then
assert(utils.isInteger(tonumber(msg.Tags.Years)), "Invalid years. Must be integer between 1 and 5")
end

-- if quantity provided must be a number and integer greater than 0
if msg.Tags.Quantity then
assert(utils.isInteger(tonumber(msg.Tags.Quantity)), "Invalid quantity. Must be integer greater than 0")
end
end

local inputStatus, inputResult = pcall(checkAssertions)
Expand All @@ -422,7 +463,7 @@ Handlers.add(ActionMap.TokenCost, utils.hasMatchingTag("Action", ActionMap.Token
years = tonumber(msg.Tags.Years) or 1,
quantity = tonumber(msg.Tags.Quantity),
purchaseType = msg.Tags.PurchaseType or "lease",
currentTimestamp = msg.Timestamp,
currentTimestamp = tonumber(msg.Timestamp),
})
if not status then
ao.send({
Expand Down Expand Up @@ -844,22 +885,21 @@ Handlers.add(ActionMap.Record, utils.hasMatchingTag("Action", ActionMap.Record),

local recordNotice = {
Target = msg.From,
Action = 'Record-Notice',
Action = "Record-Notice",
Name = msg.Tags.Name,
Data = json.encode(record)
Data = json.encode(record),
}

-- Add forwarded tags to the credit and debit notice messages
for tagName, tagValue in pairs(msg) do
-- Tags beginning with "X-" are forwarded
if string.sub(tagName, 1, 2) == "X-" then
recordNotice[tagName] = tagValue
end
end

-- Send Record-Notice
ao.send(recordNotice)
-- Add forwarded tags to the credit and debit notice messages
for tagName, tagValue in pairs(msg) do
-- Tags beginning with "X-" are forwarded
if string.sub(tagName, 1, 2) == "X-" then
recordNotice[tagName] = tagValue
end
end

-- Send Record-Notice
ao.send(recordNotice)
end)

Handlers.add(ActionMap.Records, utils.hasMatchingTag("Action", ActionMap.Records), function(msg)
Expand All @@ -868,20 +908,20 @@ Handlers.add(ActionMap.Records, utils.hasMatchingTag("Action", ActionMap.Records
-- Credit-Notice message template, that is sent to the Recipient of the transfer
local recordsNotice = {
Target = msg.From,
Action = 'Records-Notice',
Data = json.encode(records)
Action = "Records-Notice",
Data = json.encode(records),
}

-- Add forwarded tags to the records notice messages
for tagName, tagValue in pairs(msg) do
-- Tags beginning with "X-" are forwarded
if string.sub(tagName, 1, 2) == "X-" then
recordsNotice[tagName] = tagValue
end
end
-- Add forwarded tags to the records notice messages
for tagName, tagValue in pairs(msg) do
-- Tags beginning with "X-" are forwarded
if string.sub(tagName, 1, 2) == "X-" then
recordsNotice[tagName] = tagValue
end
end

-- Send Records-Notice
ao.send(recordsNotice)
-- Send Records-Notice
ao.send(recordsNotice)
end)

Handlers.add(ActionMap.Epoch, utils.hasMatchingTag("Action", ActionMap.Epoch), function(msg)
Expand Down
7 changes: 7 additions & 0 deletions src/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ function utils.isValidBase64Url(url)
return url
end

function utils.isInteger(value)
if type(value) == "string" then
value = tonumber(value)
end
return value % 1 == 0
end

function utils.isValidArweaveAddress(address)
local isValidArweaveAddress = #address == 43 and string.match(address, "^[%w-_]+$") ~= nil

Expand Down
Loading

0 comments on commit feeb8e2

Please sign in to comment.