Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: updated tests accounting for latest staking changes #12

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions test/ServiceStakingMechUsage.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ describe("ServiceStakingMechUsage", function () {
maxNumServices: 10,
rewardsPerSecond: "1" + "0".repeat(15),
minStakingDeposit: 10,
maxNumInactivityPeriods: 3,
livenessPeriod: livenessPeriod, // Ten seconds
livenessRatio: "1" + "0".repeat(16), // 0.01 transaction per second (TPS)
numAgentInstances: 1,
agentIds: [],
threshold: 0,
configHash: bytes32Zero
};
const maxInactivity = serviceParams.maxNumInactivityPeriods * livenessPeriod + 1;

beforeEach(async function () {
signers = await ethers.getSigners();
Expand Down Expand Up @@ -173,15 +175,15 @@ describe("ServiceStakingMechUsage", function () {
await serviceStakingMechUsage.stake(serviceId);

// Check that the service is staked
const isStaked = await serviceStakingMechUsage.isServiceStaked(serviceId);
expect(isStaked).to.equal(true);
const stakingState = await serviceStakingMechUsage.getServiceStakingState(serviceId);
expect(stakingState).to.equal(1);

// Get the service multisig contract
const service = await serviceRegistry.getService(serviceId);
const multisig = await ethers.getContractAt("GnosisSafe", service.multisig);

// Increase the time while the service does not reach the required amount of transactions per second (TPS)
await helpers.time.increase(livenessPeriod);
await helpers.time.increase(maxInactivity);

// Calculate service staking reward that must be zero
const reward = await serviceStakingMechUsage.calculateServiceStakingReward(serviceId);
Expand Down Expand Up @@ -217,8 +219,8 @@ describe("ServiceStakingMechUsage", function () {
await serviceStakingMechUsage.stake(serviceId);

// Check that the service is staked
const isStaked = await serviceStakingMechUsage.isServiceStaked(serviceId);
expect(isStaked).to.equal(true);
const stakingState = await serviceStakingMechUsage.getServiceStakingState(serviceId);
expect(stakingState).to.equal(1);

// Get the service multisig contract
const service = await serviceRegistry.getService(serviceId);
Expand All @@ -228,7 +230,7 @@ describe("ServiceStakingMechUsage", function () {
await agentMech.increaseRequestsCount(service.multisig);

// Increase the time while the service does not reach the required amount of transactions per second (TPS)
await helpers.time.increase(livenessPeriod);
await helpers.time.increase(maxInactivity);

// Calculate service staking reward that must be zero
const reward = await serviceStakingMechUsage.calculateServiceStakingReward(serviceId);
Expand Down Expand Up @@ -280,7 +282,7 @@ describe("ServiceStakingMechUsage", function () {
await safeContracts.executeTx(multisig, txHashData, [signMessageData], 0);

// Increase the time for the liveness period
await helpers.time.increase(livenessPeriod);
await helpers.time.increase(maxInactivity);

// Call the checkpoint at this time
await serviceStakingMechUsage.checkpoint();
Expand All @@ -292,7 +294,7 @@ describe("ServiceStakingMechUsage", function () {
await safeContracts.executeTx(multisig, txHashData, [signMessageData], 0);

// Increase the time for the liveness period
await helpers.time.increase(livenessPeriod);
await helpers.time.increase(maxInactivity);

// Calculate service staking reward that must be greater than zero
const reward = await serviceStakingMechUsage.calculateServiceStakingReward(serviceId);
Expand Down Expand Up @@ -373,7 +375,7 @@ describe("ServiceStakingMechUsage", function () {
await safeContracts.executeTx(multisig, txHashData, [signMessageData], 0);

// Increase the time for the liveness period
await helpers.time.increase(livenessPeriod);
await helpers.time.increase(maxInactivity);

// Calculate service staking reward that must be greater than zero
const reward = await serviceStakingTokenMechUsage.calculateServiceStakingReward(sId);
Expand Down
21 changes: 12 additions & 9 deletions test/ServiceStakingMechUsages.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ contract BaseSetup is Test {
uint256 internal rewardsPerSecond = 0.0001 ether;
// Minimum service staking deposit value required for staking
uint256 internal minStakingDeposit = regDeposit;
// Max number of accumulated inactivity periods after which the service is evicted
uint256 internal maxNumInactivityPeriods = 3;
// Liveness period
uint256 internal livenessPeriod = 1 days;
// Liveness ratio in the format of 1e18
Expand Down Expand Up @@ -107,7 +109,8 @@ contract BaseSetup is Test {

// Deploy service staking native token and arbitrary ERC20 token
ServiceStakingBase.StakingParams memory stakingParams = ServiceStakingBase.StakingParams(maxNumServices,
rewardsPerSecond, minStakingDeposit, livenessPeriod, livenessRatio, numAgentInstances, emptyArray, 0, bytes32(0));
rewardsPerSecond, minStakingDeposit, maxNumInactivityPeriods, livenessPeriod, livenessRatio,
numAgentInstances, emptyArray, 0, bytes32(0));
agentMech = new MockAgentMech();
serviceStakingMechUsage = new ServiceStakingMechUsage(stakingParams, address(serviceRegistry),
multisigProxyHash, address(agentMech));
Expand Down Expand Up @@ -236,7 +239,7 @@ contract ServiceStakingMechUsages is BaseSetup {
}

// Move one day ahead
vm.warp(block.timestamp + 1 days);
vm.warp(block.timestamp + serviceStakingMechUsage.maxAllowedInactivity() + 1);

// Call the checkpoint
serviceStakingMechUsage.checkpoint();
Expand All @@ -246,7 +249,7 @@ contract ServiceStakingMechUsages is BaseSetup {
for (uint256 j = 0; j < numServices; ++j) {
uint256 serviceId = j + 1;
// Unstake if the service is not yet unstaked, otherwise ignore
if (!serviceStakingMechUsage.isServiceStaked(serviceId)) {
if (uint8(serviceStakingMechUsage.getServiceStakingState(serviceId)) > 0) {
vm.startPrank(deployer);
serviceStakingMechUsage.unstake(serviceId);
vm.stopPrank();
Expand Down Expand Up @@ -307,7 +310,7 @@ contract ServiceStakingMechUsages is BaseSetup {
}

// Move one day ahead
vm.warp(block.timestamp + 1 days);
vm.warp(block.timestamp + serviceStakingMechUsage.maxAllowedInactivity() + 1);

// Call the checkpoint
serviceStakingMechUsage.checkpoint();
Expand All @@ -317,7 +320,7 @@ contract ServiceStakingMechUsages is BaseSetup {
for (uint256 j = 0; j < numServices; ++j) {
uint256 serviceId = j + 1;
// Unstake if the service is not yet unstaked, otherwise ignore
if (!serviceStakingMechUsage.isServiceStaked(serviceId)) {
if (uint8(serviceStakingMechUsage.getServiceStakingState(serviceId)) > 0) {
vm.startPrank(deployer);
serviceStakingMechUsage.unstake(serviceId);
vm.stopPrank();
Expand Down Expand Up @@ -387,7 +390,7 @@ contract ServiceStakingMechUsages is BaseSetup {
}

// Move one day ahead
vm.warp(block.timestamp + 1 days);
vm.warp(block.timestamp + serviceStakingMechUsage.maxAllowedInactivity() + 1);

// Call the checkpoint
serviceStakingMechUsage.checkpoint();
Expand All @@ -397,7 +400,7 @@ contract ServiceStakingMechUsages is BaseSetup {
for (uint256 j = 0; j < numServices; ++j) {
uint256 serviceId = j + 1;
// Unstake if the service is not yet unstaked, otherwise ignore
if (!serviceStakingMechUsage.isServiceStaked(serviceId)) {
if (uint8(serviceStakingMechUsage.getServiceStakingState(serviceId)) > 0) {
vm.startPrank(deployer);
serviceStakingMechUsage.unstake(serviceId);
vm.stopPrank();
Expand Down Expand Up @@ -467,7 +470,7 @@ contract ServiceStakingMechUsages is BaseSetup {
}

// Move one day ahead
vm.warp(block.timestamp + 1 days);
vm.warp(block.timestamp + serviceStakingMechUsage.maxAllowedInactivity() + 1);

// Call the checkpoint
serviceStakingTokenMechUsage.checkpoint();
Expand All @@ -477,7 +480,7 @@ contract ServiceStakingMechUsages is BaseSetup {
for (uint256 j = 0; j < numServices; ++j) {
uint256 serviceId = j + numServices + 1;
// Unstake if the service is not yet unstaked, otherwise ignore
if (!serviceStakingTokenMechUsage.isServiceStaked(serviceId)) {
if (uint8(serviceStakingTokenMechUsage.getServiceStakingState(serviceId)) > 0) {
vm.startPrank(deployer);
serviceStakingTokenMechUsage.unstake(serviceId);
vm.stopPrank();
Expand Down
Loading