Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into hotfix/epochs
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Dec 29, 2024
2 parents 66f2f20 + 029a449 commit b67fe2e
Show file tree
Hide file tree
Showing 5 changed files with 433 additions and 83 deletions.
8 changes: 7 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,11 @@
"editor.formatOnPaste": true,
"editor.formatOnSaveMode": "file"
},
"cSpell.words": ["ARIO", "hashchain", "redelegate"]
"cSpell.words": [
"ARIO",
"hashchain",
"redelegate",
"redelegation",
"redelegations"
]
}
228 changes: 197 additions & 31 deletions spec/gar_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1129,9 +1129,11 @@ describe("gar", function()
weights = testGateway.weights,
}, result)
end)
end)

it("should decrease delegated stake if the remaining stake is greater than the minimum stake", function()
local totalDelegatedStake = 750000000
describe("decreaseDelegateStake", function()
it("should decrease delegated stake if the remaining stake is at least the minimum stake", function()
local totalDelegatedStake = minDelegatedStake + 100000000
local decreaseAmount = 100000000
_G.GatewayRegistry[stubGatewayAddress] = testGateway
_G.GatewayRegistry[stubGatewayAddress].totalDelegatedStake = totalDelegatedStake
Expand All @@ -1141,13 +1143,13 @@ describe("gar", function()
vaults = {},
}

local expectation = {
local expectedGateway = {
operatorStake = testGateway.operatorStake,
totalDelegatedStake = totalDelegatedStake - decreaseAmount,
totalDelegatedStake = minDelegatedStake,
vaults = {},
delegates = {
[stubRandomAddress] = {
delegatedStake = totalDelegatedStake - decreaseAmount,
delegatedStake = minDelegatedStake,
startTimestamp = 0,
vaults = {
[stubMessageId] = {
Expand All @@ -1166,6 +1168,25 @@ describe("gar", function()
observerAddress = testGateway.observerAddress,
weights = testGateway.weights,
}

local expectation = {
amountWithdrawn = 0,
delegatePruned = false,
expeditedWithdrawalFee = 0,
gatewayTotalDelegatedStake = minDelegatedStake,
penaltyRate = 0,
updatedDelegate = {
delegatedStake = minDelegatedStake,
startTimestamp = 0,
vaults = {
[stubMessageId] = {
balance = decreaseAmount,
startTimestamp = startTimestamp,
endTimestamp = startTimestamp + (90 * 24 * 60 * 60 * 1000), -- 90 days
},
},
},
}
local status, result = pcall(
gar.decreaseDelegateStake,
stubGatewayAddress,
Expand All @@ -1175,58 +1196,203 @@ describe("gar", function()
stubMessageId
)
assert.is_true(status)
assert.are.same(expectation, result.gateway)
assert.are.same(expectation, _G.GatewayRegistry[stubGatewayAddress])
assert.are.same(expectation, result)
assert.are.same(expectedGateway, _G.GatewayRegistry[stubGatewayAddress])
end)

it("should decrease delegated stake with instant withdrawal and apply penalty and remove delegate", function()
_G.Balances[ao.id] = 0
local expeditedWithdrawalFee = 1000 * 0.50
local withdrawalAmount = 1000 - expeditedWithdrawalFee
_G.GatewayRegistry[stubGatewayAddress] = {
operatorStake = minOperatorStake,
totalDelegatedStake = minDelegatedStake + 1000,
it(
"should decrease delegated stake with instant withdrawal if the remaining stake is at least the minimum stake",
function()
local totalDelegatedStake = minDelegatedStake + 100000000
local decreaseAmount = 100000000
local expeditedWithdrawalFee = decreaseAmount * 0.50
local withdrawalAmount = decreaseAmount - expeditedWithdrawalFee
_G.GatewayRegistry[stubGatewayAddress] = testGateway
_G.GatewayRegistry[stubGatewayAddress].totalDelegatedStake = totalDelegatedStake
_G.GatewayRegistry[stubGatewayAddress].delegates[stubRandomAddress] = {
delegatedStake = totalDelegatedStake,
startTimestamp = 0,
vaults = {},
}

local expectedGateway = {
operatorStake = testGateway.operatorStake,
totalDelegatedStake = minDelegatedStake,
vaults = {},
delegates = {
[stubRandomAddress] = {
delegatedStake = minDelegatedStake,
startTimestamp = 0,
vaults = {},
},
},
startTimestamp = testGateway.startTimestamp,
stats = testGateway.stats,
services = testGateway.services,
settings = testGateway.settings,
status = testGateway.status,
observerAddress = testGateway.observerAddress,
weights = testGateway.weights,
}

local expectation = {
amountWithdrawn = withdrawalAmount,
delegatePruned = false,
expeditedWithdrawalFee = expeditedWithdrawalFee,
gatewayTotalDelegatedStake = minDelegatedStake,
penaltyRate = constants.MAX_EXPEDITED_WITHDRAWAL_PENALTY_RATE,
updatedDelegate = {
delegatedStake = minDelegatedStake,
startTimestamp = 0,
vaults = {},
},
}
local status, result = pcall(
gar.decreaseDelegateStake,
stubGatewayAddress,
stubRandomAddress,
decreaseAmount,
startTimestamp,
stubMessageId,
true -- Instant withdrawal flag
)
assert.is_true(status)
assert.are.same(expectation, result)
assert.are.same(expectedGateway, _G.GatewayRegistry[stubGatewayAddress])
assert.are.equal(withdrawalAmount, _G.Balances[stubRandomAddress])
assert.are.equal(expeditedWithdrawalFee, _G.Balances[ao.id])
end
)

it("should allow decreasing entire delegated stake", function()
local totalDelegatedStake = minDelegatedStake
local decreaseAmount = minDelegatedStake
_G.GatewayRegistry[stubGatewayAddress] = testGateway
_G.GatewayRegistry[stubGatewayAddress].totalDelegatedStake = totalDelegatedStake
_G.GatewayRegistry[stubGatewayAddress].delegates[stubRandomAddress] = {
delegatedStake = totalDelegatedStake,
startTimestamp = 0,
vaults = {},
startTimestamp = startTimestamp,
}

local expectedGateway = {
operatorStake = testGateway.operatorStake,
totalDelegatedStake = 0,
vaults = {},
delegates = {
[stubRandomAddress] = {
delegatedStake = 0,
startTimestamp = 0,
vaults = {
[stubMessageId] = {
balance = decreaseAmount,
startTimestamp = startTimestamp,
endTimestamp = startTimestamp + (90 * 24 * 60 * 60 * 1000), -- 90 days
},
},
},
},
startTimestamp = testGateway.startTimestamp,
stats = testGateway.stats,
services = testGateway.services,
settings = testGateway.settings,
status = testGateway.status,
observerAddress = testGateway.observerAddress,
delegates = {
[stubRandomAddress] = {
delegatedStake = minDelegatedStake + 1000,
startTimestamp = 0,
vaults = {},
weights = testGateway.weights,
}

local expectation = {
amountWithdrawn = 0,
delegatePruned = false,
expeditedWithdrawalFee = 0,
gatewayTotalDelegatedStake = 0,
penaltyRate = 0,
updatedDelegate = {
delegatedStake = 0,
startTimestamp = 0,
vaults = {
[stubMessageId] = {
balance = decreaseAmount,
startTimestamp = startTimestamp,
endTimestamp = startTimestamp + (90 * 24 * 60 * 60 * 1000), -- 90 days
},
},
},
}
local status, result = pcall(
gar.decreaseDelegateStake,
stubGatewayAddress,
stubRandomAddress,
1000,
decreaseAmount,
startTimestamp,
stubMessageId,
true -- instant withdrawal
stubMessageId
)
assert.is_true(status)
assert.are.same(expectation, result)
assert.are.same(expectedGateway, _G.GatewayRegistry[stubGatewayAddress])
end)

it("should allow decreasing entire delegated stake with instant withdrawal and prune the delegate", function()
local totalDelegatedStake = minDelegatedStake
local decreaseAmount = minDelegatedStake
local expeditedWithdrawalFee = decreaseAmount * 0.50
local withdrawalAmount = decreaseAmount - expeditedWithdrawalFee
_G.GatewayRegistry[stubGatewayAddress] = testGateway
_G.GatewayRegistry[stubGatewayAddress].totalDelegatedStake = totalDelegatedStake
_G.GatewayRegistry[stubGatewayAddress].delegates[stubRandomAddress] = {
delegatedStake = totalDelegatedStake,
startTimestamp = 0,
vaults = {},
}

local expectedGateway = {
operatorStake = testGateway.operatorStake,
totalDelegatedStake = 0,
vaults = {},
delegates = {},
startTimestamp = testGateway.startTimestamp,
stats = testGateway.stats,
services = testGateway.services,
settings = utils.deepCopy(testGateway.settings),
status = testGateway.status,
observerAddress = testGateway.observerAddress,
weights = testGateway.weights,
}
expectedGateway.settings.allowedDelegatesLookup["test-this-is-valid-arweave-wallet-address-3"] = true -- Add pruned delegate back to the allowlist

local expectation = {
amountWithdrawn = withdrawalAmount,
delegatePruned = true,
expeditedWithdrawalFee = expeditedWithdrawalFee,
gatewayTotalDelegatedStake = 0,
penaltyRate = constants.MAX_EXPEDITED_WITHDRAWAL_PENALTY_RATE,
updatedDelegate = {
delegatedStake = 0,
startTimestamp = 0,
vaults = {},
},
}
local status, result = pcall(
gar.decreaseDelegateStake,
stubGatewayAddress,
stubRandomAddress,
decreaseAmount,
startTimestamp,
stubMessageId,
true -- Instant withdrawal flag
)
assert.is_true(status)
assert.are.same(result.gateway.delegates[stubRandomAddress].delegatedStake, minDelegatedStake)
assert.are.equal(result.gateway.totalDelegatedStake, minDelegatedStake)
assert.are.equal(withdrawalAmount, result.amountWithdrawn)
assert.are.same(expectation, result)
assert.are.same(expectedGateway, _G.GatewayRegistry[stubGatewayAddress])
assert.are.equal(withdrawalAmount, _G.Balances[stubRandomAddress])
assert.are.equal(expeditedWithdrawalFee, result.expeditedWithdrawalFee)
assert.are.equal(expeditedWithdrawalFee, _G.Balances[ao.id])
assert.are.equal(constants.MAX_EXPEDITED_WITHDRAWAL_PENALTY_RATE, result.penaltyRate)
assert.are.equal(minDelegatedStake, _G.GatewayRegistry[stubGatewayAddress].totalDelegatedStake)
end)

it("should error if the remaining delegate stake is less than the minimum stake", function()
it("should error if the remaining delegate stake is less than the minimum stake and greater than 0", function()
local delegatedStake = minDelegatedStake
_G.GatewayRegistry[stubGatewayAddress] = {
operatorStake = minOperatorStake,
totalDelegatedStake = minDelegatedStake - 1,
totalDelegatedStake = minDelegatedStake,
vaults = {},
delegates = {
[stubRandomAddress] = {
Expand Down
Loading

0 comments on commit b67fe2e

Please sign in to comment.