Skip to content

Commit

Permalink
Add request remove node by SNID admin function for stagenet
Browse files Browse the repository at this point in the history
  • Loading branch information
Doy-lee committed Sep 10, 2024
1 parent 2030c4f commit fbd477c
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
9 changes: 9 additions & 0 deletions contracts/test/TestnetServiceNodeRewards.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import "../ServiceNodeRewards.sol";

contract TestnetServiceNodeRewards is ServiceNodeRewards {
// NOTE: Admin function to remove node by ID for stagenet debugging
function requestRemoveNodeBySNID(uint64[] calldata ids) external onlyOwner {
for (uint256 i = 0; i < ids.length; i++) {
uint64 serviceNodeID = ids[i];
IServiceNodeRewards.ServiceNode memory node = this.serviceNodes(serviceNodeID);
require(node.operator != address(0));
_initiateRemoveBLSPublicKey(serviceNodeID, node.operator);
}
}

function removeNodeBySNID(uint64[] calldata ids) external onlyOwner {
for (uint256 i = 0; i < ids.length; i++) {
uint64 serviceNodeID = ids[i];
Expand Down
79 changes: 79 additions & 0 deletions test/unit-js/TestnetServiceNodeRewardsTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const { expect } = require("chai");
const { ethers, upgrades } = require("hardhat");

async function verifySeedData(contractSN, seedEntry) {
expect(contractSN.pubkey[0]).to.equal(BigInt(seedEntry.pubkey.X));
expect(contractSN.pubkey[1]).to.equal(BigInt(seedEntry.pubkey.Y));
expect(contractSN.deposit).to.equal(BigInt(seedEntry.deposit));
expect(contractSN.contributors.length).to.equal(seedEntry.contributors.length);
for (let contributorIndex = 0; contributorIndex < contractSN.contributors.length; contributorIndex++) {
expect(BigInt(contractSN.contributors[0].addr)).to.equal(BigInt(seedEntry.contributors[contributorIndex].addr));
expect(contractSN.contributors[0].stakedAmount).to.equal(seedEntry.contributors[contributorIndex].stakedAmount);
}
}

describe("TestnetServiceNodeRewards Contract Tests", function () {
let MockERC20;
let mockERC20;
let ServiceNodeRewards;
let serviceNodeRewards;
let owner;
let foundationPool;

const staking_req = 120000000000n;

beforeEach(async function () {
// Deploy a mock ERC20 token
try {
// Deploy a mock ERC20 token
MockERC20 = await ethers.getContractFactory("MockERC20");
mockERC20 = await MockERC20.deploy("SENT Token", "SENT", 18);
} catch (error) {
console.error("Error deploying MockERC20:", error);
}

// Get signers
[owner, foundationPool] = await ethers.getSigners();

ServiceNodeRewardsMaster = await ethers.getContractFactory("TestnetServiceNodeRewards");
serviceNodeRewards = await upgrades.deployProxy(ServiceNodeRewardsMaster,
[ await mockERC20.getAddress(), // token address
await foundationPool.getAddress(), // foundation pool address
staking_req, // testnet staking requirement
10, // max contributors
1, // liquidator reward ratio
0, // pool share of liquidation ratio
1 // recipient ratio
]);
});

it("Seed and test the admin removal", async function () {
const seedData = [
{
pubkey: {
X: "0x12c59fb45c483177873406e5b74a2e6914fe25a591185f30d2788e737da6f2ed",
Y: "0x016e56f330d11faaf90ec281b1c4184e98a52d4043075fcbe45a976de0f795ab",
},
contributors: [
{
addr: "0x66d801a70615979d82c304b7db374d11c232db66",
stakedAmount: staking_req,
}
]
},
];

await serviceNodeRewards.connect(owner).seedPublicKeyList(seedData);
expect(await serviceNodeRewards.serviceNodesLength()).to.equal(1);
let aggregate_pubkey = await serviceNodeRewards.aggregatePubkey();
expect(aggregate_pubkey[0]).to.equal(seedData[0].pubkey.X);
expect(aggregate_pubkey[1]).to.equal(seedData[0].pubkey.Y);
verifySeedData(await serviceNodeRewards.serviceNodes(1), seedData[0]);
await serviceNodeRewards.connect(owner).start();

await serviceNodeRewards.connect(owner).requestRemoveNodeBySNID([1])
await serviceNodeRewards.connect(owner).removeNodeBySNID([1])
expect(await serviceNodeRewards.serviceNodesLength()).to.equal(0);
});
});

0 comments on commit fbd477c

Please sign in to comment.