From c10babbcdbd0ddbcb7f109ed8312b74bc4b01cdd Mon Sep 17 00:00:00 2001 From: dtfiedler Date: Wed, 29 May 2024 08:05:22 -0600 Subject: [PATCH] fix(api): add additional getter APIs --- contract/spec/epochs_spec.lua | 63 +++++++++++++++++++++++++++++++++++ contract/src/epochs.lua | 20 ++++++----- contract/src/main.lua | 22 +++++++++--- 3 files changed, 93 insertions(+), 12 deletions(-) diff --git a/contract/spec/epochs_spec.lua b/contract/spec/epochs_spec.lua index 3f9bcc5e..3aeab7a8 100644 --- a/contract/spec/epochs_spec.lua +++ b/contract/spec/epochs_spec.lua @@ -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" @@ -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) diff --git a/contract/src/epochs.lua b/contract/src/epochs.lua index f15e73a2..34df8cb1 100644 --- a/contract/src/epochs.lua +++ b/contract/src/epochs.lua @@ -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) diff --git a/contract/src/main.lua b/contract/src/main.lua index 8b8ceabb..8643e63b 100644 --- a/contract/src/main.lua +++ b/contract/src/main.lua @@ -37,6 +37,7 @@ local ActionMap = { DemandFactor = "DemandFactor", Epochs = "Epochs", Epoch = "Epoch", + PrescribedObservers = "PrescribedObservers", -- writes CreateVault = "CreateVault", VaultedTransfer = "VaultedTransfer", @@ -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 @@ -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