Skip to content

Commit

Permalink
chore(test): add integration tests for state pruning (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler authored Sep 3, 2024
2 parents f958604 + 30ba6e3 commit 6986631
Show file tree
Hide file tree
Showing 5 changed files with 306 additions and 32 deletions.
48 changes: 37 additions & 11 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,13 @@ end)
Handlers.add(ActionMap.CreateVault, utils.hasMatchingTag("Action", ActionMap.CreateVault), function(msg)
local function checkAssertions()
assert(
tonumber(msg.Tags["Lock-Length"]) > 0 and utils.isInteger(tonumber(msg.Tags["Lock-Length"])),
msg.Tags["Lock-Length"]
and tonumber(msg.Tags["Lock-Length"]) > 0
and utils.isInteger(tonumber(msg.Tags["Lock-Length"])),
"Invalid lock length. Must be integer greater than 0"
)
assert(
tonumber(msg.Tags.Quantity) > 0 and utils.isInteger(tonumber(msg.Tags.Quantity)),
msg.Tags.Quantity and tonumber(msg.Tags.Quantity) > 0 and utils.isInteger(tonumber(msg.Tags.Quantity)),
"Invalid quantity. Must be integer greater than 0"
)
end
Expand All @@ -193,8 +195,13 @@ Handlers.add(ActionMap.CreateVault, utils.hasMatchingTag("Action", ActionMap.Cre
return
end

local result, err =
balances.createVault(msg.From, msg.Tags.Quantity, tonumber(msg.Tags["Lock-Length"]), msg.Timestamp, msg.Id)
local result, err = vaults.createVault(
msg.From,
tonumber(msg.Tags.Quantity),
tonumber(msg.Tags["Lock-Length"]),
tonumber(msg.Timestamp),
msg.Id
)
if err then
ao.send({
Target = msg.From,
Expand All @@ -204,7 +211,10 @@ Handlers.add(ActionMap.CreateVault, utils.hasMatchingTag("Action", ActionMap.Cre
else
ao.send({
Target = msg.From,
Tags = { Action = "Vault-Created-Notice" },
Tags = {
Action = "Vault-Created-Notice",
["Vault-Id"] = msg.Id,
},
Data = json.encode(result),
})
end
Expand Down Expand Up @@ -284,7 +294,7 @@ Handlers.add(ActionMap.ExtendVault, utils.hasMatchingTag("Action", ActionMap.Ext
return
end

local result, err = balances.extendVault(msg.From, msg.Tags["Extend-Length"], msg.Timestamp, msg.Tags["Vault-Id"])
local result, err = vaults.extendVault(msg.From, msg.Tags["Extend-Length"], msg.Timestamp, msg.Tags["Vault-Id"])
if err then
ao.send({
Target = msg.From,
Expand Down Expand Up @@ -320,7 +330,7 @@ Handlers.add(ActionMap.IncreaseVault, utils.hasMatchingTag("Action", ActionMap.I
return
end

local result, err = balances.increaseVault(msg.From, msg.Tags.Quantity, msg.Tags["Vault-Id"], msg.Timestamp)
local result, err = vaults.increaseVault(msg.From, msg.Tags.Quantity, msg.Tags["Vault-Id"], msg.Timestamp)
if err then
ao.send({
Target = msg.From,
Expand Down Expand Up @@ -1277,16 +1287,32 @@ Handlers.add(ActionMap.ReservedName, utils.hasMatchingTag("Action", ActionMap.Re
end)

Handlers.add(ActionMap.Vaults, utils.hasMatchingTag("Action", ActionMap.Vaults), function(msg)
local vaults = vaults.getVaults()
ao.send({ Target = msg.From, Action = "Vaults-Notice", Data = json.encode(vaults) })
ao.send({ Target = msg.From, Action = "Vaults-Notice", Data = json.encode(Vaults) })
end)

Handlers.add(ActionMap.Vault, utils.hasMatchingTag("Action", ActionMap.Vault), function(msg)
local vault = vaults.getVault(msg.Tags.Address or msg.From)
local address = msg.Tags.Address or msg.From
local vaultId = msg.Tags["Vault-Id"]
-- print all the vaults fo rhte address in json encode
local vault = vaults.getVault(address, vaultId)
print("vault " .. json.encode(vault))
if not vault then
ao.send({
Target = msg.From,
Action = "Invalid-Vault-Notice",
Error = "Vault-Not-Found",
Tags = {
Address = address,
["Vault-Id"] = vaultId,
},
})
return
end
ao.send({
Target = msg.From,
Action = "Vault-Notice",
Address = msg.Tags.Address or msg.From,
Address = address,
["Vault-Id"] = vaultId,
Data = json.encode(vault),
})
end)
Expand Down
9 changes: 7 additions & 2 deletions src/vaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local vaults = {}
local balances = require("balances")
local utils = require("utils")
local constants = require("constants")
local json = require("json")

function vaults.createVault(from, qty, lockLength, currentTimestamp, msgId)
if vaults.getVault(from, msgId) then
Expand All @@ -15,8 +16,9 @@ function vaults.createVault(from, qty, lockLength, currentTimestamp, msgId)
error(
"Invalid lock length. Must be between "
.. constants.MIN_TOKEN_LOCK_TIME
.. " - "
.. " and "
.. constants.MAX_TOKEN_LOCK_TIME
.. " ms"
)
end

Expand Down Expand Up @@ -114,7 +116,10 @@ end

function vaults.getVault(target, id)
local vaults = vaults.getVaults()
return vaults[target] and vaults[target][id]
if not vaults[target] then
return nil
end
return vaults[target][id]
end

function vaults.setVault(target, id, vault)
Expand Down
20 changes: 1 addition & 19 deletions tests/gar.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
AO_LOADER_HANDLER_ENV,
DEFAULT_HANDLE_OPTIONS,
STUB_ADDRESS,
validGatewayTags,
} from '../tools/constants.mjs';

describe('GatewayRegistry', async () => {
Expand All @@ -22,25 +23,6 @@ describe('GatewayRegistry', async () => {
);
}

const validGatewayTags = [
{ name: 'Action', value: 'Join-Network' },
{ name: 'Label', value: 'test-gateway' },
{ name: 'Note', value: 'test-note' },
{ name: 'FQDN', value: 'test-fqdn' },
{ name: 'Operator-Stake', value: '50000000000' }, // 50K IO
{ name: 'Port', value: '443' },
{ name: 'Protocol', value: 'https' },
{ name: 'Allow-Delegated-Staking', value: 'true' },
{ name: 'Min-Delegated-Stake', value: '500000000' }, // 500 IO
{ name: 'Delegate-Reward-Share-Ratio', value: '0' },
{ name: 'Observer-Address', value: STUB_ADDRESS },
{
name: 'Properties',
value: 'FH1aVetOoulPGqgYukj0VE0wIhDy90WiQoV3U2PeY44',
},
{ name: 'Auto-Stake', value: 'true' },
];

describe('Join-Network', () => {
it('should allow joining of the network record', async () => {
const joinNetworkResult = await handle({
Expand Down
Loading

0 comments on commit 6986631

Please sign in to comment.