Skip to content

Commit

Permalink
fix(test): update distribution tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Jun 26, 2024
1 parent 84c9d50 commit 9e8a78e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 20 deletions.
68 changes: 53 additions & 15 deletions spec/epochs_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe("epochs", function()
}
epochs.updateEpochSettings({
prescribedNameCount = 5,
maxObservers = 3,
maxObservers = 5,
epochZeroStartTimestamp = 0,
durationMs = 100,
distributionDelayMs = 15,
Expand Down Expand Up @@ -450,7 +450,7 @@ describe("epochs", function()
it("should distribute rewards for the epoch", function()
local epochIndex = 0
local hashchain = "c29tZSBzYW1wbGUgaGFzaA==" -- base64 of "some sample hash"
for i = 1, 3 do
for i = 1, 5 do
local gateway = {
operatorStake = gar.getSettings().operators.minStake,
totalDelegatedStake = 0,
Expand Down Expand Up @@ -486,12 +486,13 @@ describe("epochs", function()
-- save observations using saveObsevations function for each gateway, gateway1 failed, gateway2 and gateway3 passed
local failedGateways = {
"test-this-is-valid-arweave-wallet-address-1",
"test-this-is-valid-arweave-wallet-address-3",
}
local epochStartTimetamp, epochEndTimestamp, epochDistributionTimestamp =
epochs.getEpochTimestampsForIndex(epochIndex)
local validObservationTimestamp = epochStartTimetamp + epochs.getSettings().distributionDelayMs + 1
-- save observations for the epoch for last two gateways
for i = 2, 3 do
for i = 3, 5 do
local status, result = pcall(
epochs.saveObservations,
"test-this-very-valid-observer-wallet-addr-" .. i,
Expand All @@ -503,20 +504,23 @@ describe("epochs", function()
end
-- set the protocol balance to 5 million IO
local totalEligibleRewards = math.floor(protocolBalance * 0.0025)
local expectedGatewaryReward = math.floor(totalEligibleRewards * 0.95 / 3)
local expectedObserverReward = math.floor(totalEligibleRewards * 0.05 / 3)
local expectedGatewaryReward = math.floor(totalEligibleRewards * 0.95 / 5)
local expectedObserverReward = math.floor(totalEligibleRewards * 0.05 / 5)
-- clear the balances for the gateways
Balances["test-this-is-valid-arweave-wallet-address-1"] = 0

-- distribute rewards for the epoch
local status = pcall(epochs.distributeRewardsForEpoch, epochDistributionTimestamp)
assert.is_true(status)
-- gateway 1 should only get observer rewards
-- gateway 2 should get obesrver and gateway rewards
-- gateway 1 should not get any rewards
-- gateway 2 should get both observer and gateway rewards
-- gateway 3 should get observer and gateway rewards
local gateway1 = gar.getGateway("test-this-is-valid-arweave-wallet-address-1")
local gateway2 = gar.getGateway("test-this-is-valid-arweave-wallet-address-2")
local gateway3 = gar.getGateway("test-this-is-valid-arweave-wallet-address-3")
local gateway4 = gar.getGateway("test-this-is-valid-arweave-wallet-address-4")
local gateway5 = gar.getGateway("test-this-is-valid-arweave-wallet-address-5")
-- failed observation and did not observe
assert.are.same({
prescribedEpochCount = 1,
observedEpochCount = 0,
Expand All @@ -526,16 +530,27 @@ describe("epochs", function()
passedConsecutiveEpochs = 0,
totalEpochCount = 1,
}, gateway1.stats)
-- passed observation, did not observe
assert.are.same({
prescribedEpochCount = 1,
observedEpochCount = 1,
observedEpochCount = 0,
passedEpochCount = 1,
failedEpochCount = 0,
failedConsecutiveEpochs = 0,
passedConsecutiveEpochs = 1,
totalEpochCount = 1,
}, gateway2.stats)

-- failed observation, did observe
assert.are.same({
prescribedEpochCount = 1,
observedEpochCount = 1,
passedEpochCount = 0,
failedEpochCount = 1,
failedConsecutiveEpochs = 1,
passedConsecutiveEpochs = 0,
totalEpochCount = 1,
}, gateway3.stats)
-- passed observation, did observe
assert.are.same({
prescribedEpochCount = 1,
observedEpochCount = 1,
Expand All @@ -544,26 +559,49 @@ describe("epochs", function()
failedConsecutiveEpochs = 0,
passedConsecutiveEpochs = 1,
totalEpochCount = 1,
}, gateway3.stats)
}, gateway4.stats)
-- passed observation, did observe
assert.are.same({
prescribedEpochCount = 1,
observedEpochCount = 1,
passedEpochCount = 1,
failedEpochCount = 0,
failedConsecutiveEpochs = 0,
passedConsecutiveEpochs = 1,
totalEpochCount = 1,
}, gateway5.stats)
-- check balances
assert.are.equal(0, balances.getBalance("test-this-is-valid-arweave-wallet-address-1"))
assert.are.equal(
expectedGatewaryReward + expectedObserverReward,
math.floor(expectedGatewaryReward * 0.75),
balances.getBalance("test-this-is-valid-arweave-wallet-address-2")
)
assert.are.equal(expectedObserverReward, balances.getBalance("test-this-is-valid-arweave-wallet-address-3"))
assert.are.equal(expectedObserverReward, balances.getBalance("test-this-is-valid-arweave-wallet-address-3"))
assert.are.equal(
expectedGatewaryReward + expectedObserverReward,
balances.getBalance("test-this-is-valid-arweave-wallet-address-4")
)
assert.are.equal(
expectedGatewaryReward + expectedObserverReward,
balances.getBalance("test-this-is-valid-arweave-wallet-address-3")
balances.getBalance("test-this-is-valid-arweave-wallet-address-5")
)
-- check the epoch was updated
local distributions = epochs.getEpoch(epochIndex).distributions
local expectedTotalDistribution = 0 -- gateway 1 did not get any rewards
+ math.floor(expectedGatewaryReward * 0.75) -- gateway 2 got 75% of the gateway reward
+ expectedObserverReward * 3 -- gateway 3, 4, 5 got observer rewards
+ expectedGatewaryReward * 2 -- gateway 4, 5 got gateway rewards
assert.are.same({
totalEligibleRewards = totalEligibleRewards,
totalDistributedRewards = (expectedGatewaryReward + expectedObserverReward) * 2,
totalDistributedRewards = expectedTotalDistribution,
distributedTimestamp = epochDistributionTimestamp,
rewards = {
["test-this-is-valid-arweave-wallet-address-2"] = expectedGatewaryReward + expectedObserverReward,
["test-this-is-valid-arweave-wallet-address-3"] = expectedGatewaryReward + expectedObserverReward,
["test-this-is-valid-arweave-wallet-address-1"] = 0,
["test-this-is-valid-arweave-wallet-address-2"] = math.floor(expectedGatewaryReward * 0.75),
["test-this-is-valid-arweave-wallet-address-3"] = expectedObserverReward,
["test-this-is-valid-arweave-wallet-address-4"] = expectedGatewaryReward + expectedObserverReward,
["test-this-is-valid-arweave-wallet-address-5"] = expectedGatewaryReward + expectedObserverReward,
},
}, distributions)
end)
Expand Down
2 changes: 1 addition & 1 deletion spec/gar_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local testSettings = {
protocol = "https",
port = 443,
allowDelegatedStaking = true,
minDelegatedStake = 100,
minDelegatedStake = gar.getSettings().delegates.minStake,
autoStake = true,
label = "test",
delegateRewardShareRatio = 0,
Expand Down
3 changes: 3 additions & 0 deletions src/epochs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,9 @@ function epochs.distributeRewardsForEpoch(currentTimestamp)
gar.increaseOperatorStake(gatewayAddress, remaingOperatorReward)
end
epochDistributions[gatewayAddress] = remaingOperatorReward
else
-- mark it as zero rewards
epochDistributions[gatewayAddress] = 0
end

-- increment the total distributed
Expand Down
2 changes: 1 addition & 1 deletion src/gar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ function gar.assertValidGatewayParameters(from, stake, settings, observerAddress
type(settings.minDelegatedStake) == "number"
and utils.isInteger(settings.minDelegatedStake)
and settings.minDelegatedStake >= gar.getSettings().delegates.minStake,
"minDelegatedStake must be an integer and "
"minDelegatedStake must be an integer greater than or equal to the minimum delegated stake"
)
end
end
Expand Down
7 changes: 4 additions & 3 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -888,11 +888,12 @@ Handlers.add("tick", utils.hasMatchingTag("Action", "Tick"), function(msg)
local currentEpochIndex = epochs.getEpochIndexForTimestamp(msgTimestamp)
-- if epoch index is -1 then we are before the genesis epoch and we should not tick
if currentEpochIndex < 0 then
-- do nothing and just send a notice back to the sender
ao.send({
Target = msg.From,
Action = "Invalid-Tick-Notice",
Error = "Invalid-Tick",
Data = json.encode("Cannot tick before genesis epoch"),
Action = "Tick-Notice",
LastTickedEpochIndex = LastTickedEpochIndex,
Data = json.encode("Genesis epocch has not started yet."),
})
end

Expand Down

0 comments on commit 9e8a78e

Please sign in to comment.