From fbd477cb7c00f6e0dfbe84dd07b4a394b405ac73 Mon Sep 17 00:00:00 2001 From: doylet Date: Tue, 10 Sep 2024 12:06:52 +1000 Subject: [PATCH] Add request remove node by SNID admin function for stagenet --- contracts/test/TestnetServiceNodeRewards.sol | 9 +++ test/unit-js/TestnetServiceNodeRewardsTest.js | 79 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 test/unit-js/TestnetServiceNodeRewardsTest.js diff --git a/contracts/test/TestnetServiceNodeRewards.sol b/contracts/test/TestnetServiceNodeRewards.sol index ec60abd..0b64791 100644 --- a/contracts/test/TestnetServiceNodeRewards.sol +++ b/contracts/test/TestnetServiceNodeRewards.sol @@ -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]; diff --git a/test/unit-js/TestnetServiceNodeRewardsTest.js b/test/unit-js/TestnetServiceNodeRewardsTest.js new file mode 100644 index 0000000..b9fafe3 --- /dev/null +++ b/test/unit-js/TestnetServiceNodeRewardsTest.js @@ -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); + }); +}); +