Skip to content

Commit

Permalink
fix(api): add additional getter APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed May 29, 2024
1 parent 8f2c2ae commit c10babb
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 12 deletions.
63 changes: 63 additions & 0 deletions contract/spec/epochs_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ describe("epochs", function()
end)

describe("saveObservations", function()
before_each(function()
-- _G.Epochs = {}
end)
it("should throw an error when saving observation too early in the epoch", function()
local observer = "test-wallet-address-2"
local reportTxId = "reportTxId"
Expand Down Expand Up @@ -286,4 +289,64 @@ describe("epochs", function()
end
)
end)

describe("getPrescribedObserversForEpoch", function()
it("should return the prescribed observers for the epoch", function()
local epochIndex = 0
local expectation = {}
local result = epochs.getPrescribedObserversForEpoch(epochIndex)
assert.are.same(result, expectation)
end)
end)

describe("getEpochIndexForTimestamp", function()
it("should return the epoch index for the given timestamp", function()
-- update settings
epochs.updateEpochSettings({
epochZeroStartTimestamp = 0,
durationMs = 10,
distributionDelayMs = 15,
})
local timestamp = 100
local result = epochs.getEpochIndexForTimestamp(timestamp)
assert.are.equal(result, 10)
end)
end)

describe("getEpochTimestampsForIndex", function()
it("should return the epoch timestamps for the given epoch index", function()
local epochIndex = 0
epochs.updateEpochSettings({
epochZeroStartTimestamp = 0,
durationMs = 100,
distributionDelayMs = 15,
})
local expectation = { 0, 100, 115, 0 }
local result = { epochs.getEpochTimestampsForIndex(epochIndex) }
assert.are.same(result, expectation)
end)
end)

describe("createNewEpoch", function()
it("should create a new epoch for the given timestamp", function()
local timestamp = 100
local epochIndex = 1
local epochStartTimestamp = 100
local epochEndTimestamp = 200
local epochDistributionTimestamp = 215
local expectation = {
startTimestamp = epochStartTimestamp,
endTimestamp = epochEndTimestamp,
distributionTimestamp = epochDistributionTimestamp,
observations = {
failureSummaries = {},
reports = {},
},
prescribedObservers = {},
distributions = {},
}
epochs.createEpochForTimestamp(timestamp)
assert.are.same(Epochs[epochIndex], expectation)
end)
end)
end)
20 changes: 12 additions & 8 deletions contract/src/epochs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,40 @@ local epochSettings = {
distributionDelayMs = 60 * 1000 * 2 * 15, -- 15 blocks
}

function epochs.getEpochs()
return Epochs
end

function epochs.getEpoch(epochNumber)
return Epochs[epochNumber]
return Epochs[epochNumber] or {}
end

function epochs.getObservers()
return epochs.getCurrentEpoch().prescribedObservers
return epochs.getCurrentEpoch().prescribedObservers or {}
end

function epochs.getObservations()
return epochs.getCurrentEpoch().observations
return epochs.getCurrentEpoch().observations or {}
end

function epochs.getReports()
return epochs.getObservations().reports
return epochs.getObservations().reports or {}
end

function epochs.getDistribution()
return epochs.getCurrentEpoch().distributions
return epochs.getCurrentEpoch().distributions or {}
end

function epochs.getPrescribedObserversForEpoch(epochNumber)
return epochs.getEpoch(epochNumber).prescribedObservers
return epochs.getEpoch(epochNumber).prescribedObservers or {}
end

function epochs.getReportsForEpoch(epochNumber)
return epochs.getEpoch(epochNumber).observations.reports
return epochs.getEpoch(epochNumber).observations.reports or {}
end

function epochs.getDistributionForEpoch(epochNumber)
return epochs.getEpoch(epochNumber).distributions
return epochs.getEpoch(epochNumber).distributions or {}
end

function epochs.getEpochFromTimestamp(timestamp)
Expand Down
22 changes: 18 additions & 4 deletions contract/src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ local ActionMap = {
DemandFactor = "DemandFactor",
Epochs = "Epochs",
Epoch = "Epoch",
PrescribedObservers = "PrescribedObservers",
-- writes
CreateVault = "CreateVault",
VaultedTransfer = "VaultedTransfer",
Expand Down Expand Up @@ -446,7 +447,14 @@ Handlers.add(
)

Handlers.add(ActionMap.SaveObservations, utils.hasMatchingTag("Action", ActionMap.SaveObservations), function(msg)
gar.saveObservations(msg)
local status, result = pcall(epochs.saveObservations, msg.From, msg.Data.reportTxId, msg.Data.failedGateways, msg.Timestamp)
if status then
-- TODO: add tags for successfull save observation
ao.send({ Target = msg.From, Data = tostring(result) })
else
-- TODO: add additional tags for error
ao.send({ Target = msg.From, Error = json.encode(result) })
end
end)

-- handler showing how we can fetch data from classes in lua
Expand All @@ -471,14 +479,20 @@ Handlers.add(ActionMap.Records, utils.hasMatchingTag("Action", ActionMap.Records
end)

Handlers.add(ActionMap.Epoch, utils.hasMatchingTag("Action", ActionMap.Epochs), function(msg)
local epochIndex = msg.Tags.EpochNumber or epochs.getEpochIndexFromTimestamp(msg.Timestamp)
local epochIndex = tonumber(msg.Tags.EpochNumber) or epochs.getEpochIndexFromTimestamp(msg.Timestamp)
local epoch = epochs.getEpoch(epochIndex)
ao.send({ Target = msg.From, Data = json.encode(epoch) })
end)

Handlers.add(ActionMap.Epochs, utils.hasMatchingTag("Action", ActionMap.Epochs), function(msg)
local epochs = epochs.getEpoch(msg.Timestamp)
ao.send({ Target = msg.From, Data = json.encode(Epochs) })
local epochs = epochs.getEpochs()
ao.send({ Target = msg.From, Data = json.encode(epochs) })
end)

Handlers.add(ActionMap.PrescribedObservers, utils.hasMatchingTag("Action", ActionMap.PrescribedObservers), function(msg)
local epochIndex = epochs.getEpochTimestampsForIndex(msg.Timestamp)
local prescribedObservers = epochs.getPrescribedObserversForEpoch(epochIndex)
ao.send({ Target = msg.From, Data = json.encode(prescribedObservers) })
end)

return process

0 comments on commit c10babb

Please sign in to comment.