Skip to content

Commit

Permalink
feat(gar): optimize prescribed observers, use lookup table and comput… (
Browse files Browse the repository at this point in the history
#279)

…e full array on read handlers
  • Loading branch information
dtfiedler authored Dec 17, 2024
2 parents a620d4c + f02067b commit d7132d9
Show file tree
Hide file tree
Showing 10 changed files with 452 additions and 359 deletions.
169 changes: 68 additions & 101 deletions spec/epochs_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ describe("epochs", function()
}
end)

describe("computePrescribedObserversForEpoch", function()
it("should return all eligible gateways if fewer than the maximum in network", function()
describe("getPrescribedObserversWithWeightsForEpoch", function()
it("should return the prescribed observers with weights for the epoch", function()
_G.GatewayRegistry["test-this-is-valid-arweave-wallet-address-1"] = {
operatorStake = gar.getSettings().operators.minStake,
totalDelegatedStake = 0,
Expand All @@ -71,26 +71,63 @@ describe("epochs", function()
settings = testSettings,
status = "joined",
observerAddress = "observerAddress",
weights = {
normalizedCompositeWeight = 1,
stakeWeight = 1,
tenureWeight = 1,
gatewayRewardRatioWeight = 1,
observerRewardRatioWeight = 1,
compositeWeight = 1,
},
}
_G.Epochs[0].prescribedObservers = {
["observerAddress"] = "test-this-is-valid-arweave-wallet-address-1",
}
local epochIndex = 0
local expectation = {
{
gatewayAddress = "test-this-is-valid-arweave-wallet-address-1",
observerAddress = "observerAddress",
stake = gar.getSettings().operators.minStake,
startTimestamp = startTimestamp,
gatewayAddress = "test-this-is-valid-arweave-wallet-address-1",
normalizedCompositeWeight = 1,
stakeWeight = 1,
tenureWeight = 1 / gar.getSettings().observers.tenureWeightPeriod,
tenureWeight = 1,
gatewayRewardRatioWeight = 1,
observerRewardRatioWeight = 1,
compositeWeight = 1 / gar.getSettings().observers.tenureWeightPeriod,
normalizedCompositeWeight = 1,
compositeWeight = 1,
},
}
local status, result = pcall(epochs.computePrescribedObserversForEpoch, 0, hashchain)
assert.is_true(status)
assert.are.equal(1, #result)
local result = epochs.getPrescribedObserversWithWeightsForEpoch(epochIndex)
assert.are.same(expectation, result)
end)
end)

describe("computePrescribedObserversForEpoch", function()
it("should return all eligible gateways if fewer than the maximum in network", function()
_G.GatewayRegistry["test-this-is-valid-arweave-wallet-address-1"] = {
operatorStake = gar.getSettings().operators.minStake,
totalDelegatedStake = 0,
vaults = {},
delegates = {},
startTimestamp = startTimestamp,
stats = {
prescribedEpochCount = 0,
observedEpochCount = 0,
totalEpochCount = 0,
passedEpochCount = 0,
failedEpochCount = 0,
failedConsecutiveEpochs = 0,
passedConsecutiveEpochs = 0,
},
settings = testSettings,
status = "joined",
observerAddress = "observerAddress",
}
local expectation = {
["observerAddress"] = "test-this-is-valid-arweave-wallet-address-1",
}
local prescribedObserverMap = epochs.computePrescribedObserversForEpoch(0, hashchain)
assert.are.same(expectation, prescribedObserverMap)
end)

it("should return the maximum number of gateways if more are enrolled in network", function()
local testHashchain = "c29tZSBzYW1wbGUgaGFzaA==" -- base64 of "some sample hash"
Expand Down Expand Up @@ -118,52 +155,25 @@ describe("epochs", function()
},
settings = testSettings,
status = "joined",
observerAddress = "observerAddress",
observerAddress = "observer-address-" .. i,
}
-- note - ordering of keys is not guaranteed when insert into maps
_G.GatewayRegistry["observer" .. i] = gateway
end

local expectation = {
{
gatewayAddress = "observer1",
observerAddress = "observerAddress",
stake = gar.getSettings().operators.minStake,
startTimestamp = startTimestamp,
stakeWeight = 1,
tenureWeight = 1 / gar.getSettings().observers.tenureWeightPeriod,
gatewayRewardRatioWeight = 1,
observerRewardRatioWeight = 1,
compositeWeight = 1 / gar.getSettings().observers.tenureWeightPeriod,
normalizedCompositeWeight = 1 / 3,
},
{
gatewayAddress = "observer3",
observerAddress = "observerAddress",
stake = gar.getSettings().operators.minStake,
startTimestamp = startTimestamp,
stakeWeight = 1,
tenureWeight = 1 / gar.getSettings().observers.tenureWeightPeriod,
gatewayRewardRatioWeight = 1,
observerRewardRatioWeight = 1,
compositeWeight = 1 / gar.getSettings().observers.tenureWeightPeriod,
normalizedCompositeWeight = 1 / 3,
},
["observer-address-1"] = "observer1",
["observer-address-3"] = "observer3",
}
local status, result = pcall(epochs.computePrescribedObserversForEpoch, 0, testHashchain)
assert.is_true(status)
assert.are.equal(2, #result)
table.sort(result, function(a, b)
return a.gatewayAddress < b.gatewayAddress
end)
assert.are.same(expectation, result)
local prescribedObserverMap = epochs.computePrescribedObserversForEpoch(0, testHashchain)
assert.are.same(expectation, prescribedObserverMap)
end)
end)

describe("computePrescribedNamesForEpoch", function()
-- NOTE: Record names in the tests below use spelled out numbers because without that
-- there's insufficient base64url information encoded in the final encoded block to
-- disambiguate the decoded vallues.
-- disambiguate the decoded values.
it("should return all eligible names if fewer than the maximum in name registry", function()
_G.NameRegistry.records = {
["arns-name-one"] = {
Expand Down Expand Up @@ -252,26 +262,15 @@ describe("epochs", function()
assert.match("Observations for the current epoch cannot be submitted before", error)
end)
it("should throw an error if the caller is not prescribed", function()
local observer = "test-this-is-valid-arweave-wallet-address-2"
local observer = "test-this-is-valid-arweave-observer-address-2"
local reportTxId = "test-this-very-valid-observations-report-tx"
local settings = epochs.getSettings()
local timestamp = settings.epochZeroStartTimestamp + settings.distributionDelayMs + 1
local failedGateways = {
"test-this-is-valid-arweave-wallet-address-1",
}
_G.Epochs[0].prescribedObservers = {
{
gatewayAddress = "test-this-is-valid-arweave-wallet-address-1",
observerAddress = "test-this-is-valid-arweave-wallet-address-1",
stake = gar.getSettings().operators.minStake,
startTimestamp = startTimestamp,
stakeWeight = 1,
tenureWeight = 1 / gar.getSettings().observers.tenureWeightPeriod,
gatewayRewardRatioWeight = 1,
observerRewardRatioWeight = 1,
compositeWeight = 1 / gar.getSettings().observers.tenureWeightPeriod,
normalizedCompositeWeight = 1,
},
["test-this-is-valid-arweave-observer-address-1"] = "test-this-is-valid-arweave-gateway-address-1",
}
local status, error = pcall(epochs.saveObservations, observer, reportTxId, failedGateways, timestamp)
assert.is_false(status)
Expand All @@ -280,7 +279,7 @@ describe("epochs", function()
it(
"should save observation when the timestamp is after the distribution delay and only mark gateways around during the full epoch as failed",
function()
local observer = "test-this-is-valid-arweave-wallet-address-2"
local observer = "test-this-is-valid-arweave-observer-address-2"
local reportTxId = "test-this-very-valid-observations-report-tx"
local settings = epochs.getSettings()
local timestamp = settings.epochZeroStartTimestamp + settings.distributionDelayMs + 1
Expand All @@ -302,7 +301,7 @@ describe("epochs", function()
},
settings = testSettings,
status = "joined",
observerAddress = "test-this-is-valid-arweave-wallet-address-1",
observerAddress = "test-this-is-valid-arweave-observer-address-1",
},
["test-this-is-valid-arweave-wallet-address-2"] = {
operatorStake = gar.getSettings().operators.minStake,
Expand All @@ -321,7 +320,7 @@ describe("epochs", function()
},
settings = testSettings,
status = "joined",
observerAddress = "test-this-is-valid-arweave-wallet-address-2",
observerAddress = "test-this-is-valid-arweave-observer-address-2",
},
["test-this-is-valid-arweave-wallet-address-3"] = {
operatorStake = gar.getSettings().operators.minStake,
Expand All @@ -340,7 +339,7 @@ describe("epochs", function()
},
settings = testSettings,
status = "joined",
observerAddress = "test-this-is-valid-arweave-wallet-address-3",
observerAddress = "test-this-is-valid-arweave-observer-address-3",
},
["test-this-is-valid-arweave-wallet-address-4"] = {
operatorStake = gar.getSettings().operators.minStake,
Expand All @@ -360,22 +359,11 @@ describe("epochs", function()
},
settings = testSettings,
status = "leaving", -- leaving, so it is not eligible to receive stats from this epoch
observerAddress = "test-this-is-valid-arweave-wallet-address-4",
observerAddress = "test-this-is-valid-arweave-observer-address-4",
},
}
_G.Epochs[0].prescribedObservers = {
{
gatewayAddress = "test-this-is-valid-arweave-wallet-address-2",
observerAddress = "test-this-is-valid-arweave-wallet-address-2",
stake = gar.getSettings().operators.minStake,
startTimestamp = startTimestamp,
stakeWeight = 1,
tenureWeight = 1 / gar.getSettings().observers.tenureWeightPeriod,
gatewayRewardRatioWeight = 1,
observerRewardRatioWeight = 1,
compositeWeight = 1 / gar.getSettings().observers.tenureWeightPeriod,
normalizedCompositeWeight = 1,
},
["test-this-is-valid-arweave-observer-address-2"] = "test-this-is-valid-arweave-wallet-address-2",
}
local failedGateways = {
"test-this-is-valid-arweave-wallet-address-1",
Expand Down Expand Up @@ -513,18 +501,7 @@ describe("epochs", function()
reports = {},
},
prescribedObservers = {
{
compositeWeight = 4.0,
gatewayAddress = "test-this-is-valid-arweave-wallet-address-1",
gatewayRewardRatioWeight = 1.0,
normalizedCompositeWeight = 1.0,
observerAddress = "test-this-is-valid-arweave-wallet-address-1",
observerRewardRatioWeight = 1.0,
stake = gar.getSettings().operators.minStake,
stakeWeight = 1.0,
startTimestamp = 0,
tenureWeight = 4,
},
["test-this-is-valid-arweave-wallet-address-1"] = "test-this-is-valid-arweave-wallet-address-1",
},
prescribedNames = {},
distributions = {
Expand Down Expand Up @@ -634,21 +611,11 @@ describe("epochs", function()
},
prescribedNames = {},
prescribedObservers = {
{
observerAddress = "test-this-very-valid-observer-wallet-addr-1",
},
{
observerAddress = "test-this-very-valid-observer-wallet-addr-2",
},
{
observerAddress = "test-this-very-valid-observer-wallet-addr-3",
},
{
observerAddress = "test-this-very-valid-observer-wallet-addr-4",
},
{
observerAddress = "test-this-very-valid-observer-wallet-addr-5",
},
["test-this-very-valid-observer-wallet-addr-1"] = "test-this-very-valid-arweave-wallet-addr-1",
["test-this-very-valid-observer-wallet-addr-2"] = "test-this-very-valid-arweave-wallet-addr-2",
["test-this-very-valid-observer-wallet-addr-3"] = "test-this-very-valid-arweave-wallet-addr-3",
["test-this-very-valid-observer-wallet-addr-4"] = "test-this-very-valid-arweave-wallet-addr-4",
["test-this-very-valid-observer-wallet-addr-5"] = "test-this-very-valid-arweave-wallet-addr-5",
},
}

Expand Down
Loading

0 comments on commit d7132d9

Please sign in to comment.