diff --git a/contracts/delegation/ValidatorService.sol b/contracts/delegation/ValidatorService.sol index 6332be996..c522e275a 100644 --- a/contracts/delegation/ValidatorService.sol +++ b/contracts/delegation/ValidatorService.sol @@ -88,6 +88,23 @@ contract ValidatorService is Permissions { trustedValidators[validatorId] = false; } + function getTrustedValidators() external view returns (uint[] memory) { + uint numberOfTrustedValidators = 0; + for (uint i = 1; i <= numberOfValidators; i++) { + if (trustedValidators[i]) { + numberOfTrustedValidators++; + } + } + uint[] memory whitelist = new uint[](numberOfTrustedValidators); + uint cursor = 0; + for (uint i = 1; i <= numberOfValidators; i++) { + if (trustedValidators[i]) { + whitelist[cursor++] = i; + } + } + return whitelist; + } + function requestForNewAddress(address oldValidatorAddress, address newValidatorAddress) external allow("DelegationService") { require(newValidatorAddress != address(0), "New address cannot be null"); uint validatorId = getValidatorId(oldValidatorAddress); diff --git a/test/delegation/ValidatorService.ts b/test/delegation/ValidatorService.ts index 8de43177b..85ac1a6bb 100644 --- a/test/delegation/ValidatorService.ts +++ b/test/delegation/ValidatorService.ts @@ -171,6 +171,31 @@ contract("ValidatorService", ([owner, holder, validator1, validator2, validator3 .should.be.eventually.rejectedWith("New address cannot be null"); }); + it("should return list of trusted validators", async () => { + const validatorId1 = 1; + const validatorId2 = 2; + const validatorId3 = 3; + await delegationService.registerValidator( + "ValidatorName", + "Really good validator", + 500, + 100, + {from: validator2}); + await delegationService.registerValidator( + "ValidatorName", + "Really good validator", + 500, + 100, + {from: validator3}); + const whitelist = []; + await validatorService.enableValidator(validatorId1, {from: owner}); + whitelist.push(validatorId1); + await validatorService.enableValidator(validatorId3, {from: owner}); + whitelist.push(validatorId3); + const trustedList = (await validatorService.getTrustedValidators()).map(Number); + assert.deepEqual(whitelist, trustedList); + }); + describe("when holder has enough tokens", async () => { let validatorId: number; let amount: number;