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

Added benchmarking test for sorting array #11

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
35 changes: 35 additions & 0 deletions src/middleware/Middleware.sol
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,41 @@ contract Middleware is SimpleKeyRegistry32, Ownable {
}
}

function quickSort(ValidatorData[] memory arr, int256 left, int256 right) public pure {
int256 i = left;
int256 j = right;
if (i == j) return;
uint256 pivot = arr[uint256(left + (right - left) / 2)].stake;
while (i <= j) {
while (arr[uint256(i)].stake > pivot) i++;
while (pivot > arr[uint256(j)].stake) j--;
if (i <= j) {
(arr[uint256(i)], arr[uint256(j)]) = (arr[uint256(j)], arr[uint256(i)]);
i++;
j--;
}
}
if (left < j) {
quickSort(arr, left, j);
}
if (i < right) {
quickSort(arr, i, right);
}
}

/**
* @dev Sorts operators by their total stake in descending order, after 500 it will be almost impossible to be used on-chain since 500 ≈ 31M gas
* @param epoch The epoch number
* @return sortedValidatorData Array of sorted validator data
*/
function sortOperatorsByVaults(
uint48 epoch
) external view returns (ValidatorData[] memory sortedValidatorData) {
ValidatorData[] memory validatorSet = getValidatorSet(epoch);
quickSort(validatorSet, 0, int256(validatorSet.length - 1));
sortedValidatorData = validatorSet;
}

/**
* @notice Gets the operators' keys for latest epoch
* @return keys Array of operator keys
Expand Down
190 changes: 190 additions & 0 deletions test/integration/Middleware.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {Subnetwork} from "@symbiotic/contracts/libraries/Subnetwork.sol";
//**************************************************************************************************
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";

//**************************************************************************************************
// SNOWBRIDGE
Expand Down Expand Up @@ -734,6 +735,195 @@ contract MiddlewareTest is Test {
}
}

function _addOperatorsToNetwork(
uint256 _count
) public {
for (uint256 i = 0; i < _count; i++) {
address _operator = makeAddr(string.concat("operator", Strings.toString(i + 4)));
address _vault = address(vault);
address _delegator = address(vaultAddresses.delegator);
Token token = stETH;

vm.startPrank(owner);
if (i % 3 == 0) {
_vault = address(vaultSlashable);
_delegator = address(vaultAddresses.delegatorSlashable);
rETH.transfer(_operator, 1 ether);
token = rETH;
} else if (i % 3 == 1) {
_vault = address(vaultVetoed);
_delegator = address(vaultAddresses.delegatorVetoed);
wBTC.transfer(_operator, 1 ether);
token = wBTC;
} else {
stETH.transfer(_operator, 1 ether);
}
_registerOperator(_operator, tanssi, address(_vault));
vm.startPrank(_operator);
uint256 depositAmount = 0.001 ether * (i + 1);
_depositToVault(Vault(_vault), _operator, 0.001 ether * (i + 1), token);
vm.startPrank(owner);
if (i % 3 == 1) {
IFullRestakeDelegator(_delegator).setOperatorNetworkLimit(
tanssi.subnetwork(0), _operator, depositAmount
);
} else {
INetworkRestakeDelegator(_delegator).setOperatorNetworkShares(
tanssi.subnetwork(0), _operator, depositAmount
);
}
middleware.registerOperator(_operator, bytes32(uint256(i + 4)));
}
}

function testGasFor100OperatorsIn3Vaults() public {
uint16 count = 100;
_addOperatorsToNetwork(count);

vm.startPrank(owner);
uint48 currentEpoch = middleware.getCurrentEpoch();
uint256 gasBefore = gasleft();
Middleware.ValidatorData[] memory validators = middleware.sortOperatorsByVaults(currentEpoch);
uint256 gasAfter = gasleft();
uint256 gasSorted = gasBefore - gasAfter;
console2.log("Total gas used for sorted: ", gasSorted);

assertEq(validators.length, count + 3);
for (uint256 i = 0; i < validators.length - 1; i++) {
if (i > 0 && i < count - 1) {
assertLe(validators[i].stake, validators[i - 1].stake);
}
}
vm.stopPrank();
}

function testGasFor100OperatorsIn3VaultsNonSorted() public {
uint16 count = 100;
_addOperatorsToNetwork(count);

vm.startPrank(owner);
uint48 currentEpoch = middleware.getCurrentEpoch();

uint256 gasBefore = gasleft();
Middleware.ValidatorData[] memory validatorsNotSorted = middleware.getValidatorSet(currentEpoch);
uint256 gasAfter = gasleft();
uint256 gasNotSorted = gasBefore - gasAfter;
console2.log("Total gas used for non sorted: ", gasNotSorted);

vm.stopPrank();
}

function testGasFor250OperatorsIn3Vaults() public {
uint16 count = 250;
_addOperatorsToNetwork(count);

vm.startPrank(owner);
uint48 currentEpoch = middleware.getCurrentEpoch();
uint256 gasBefore = gasleft();
Middleware.ValidatorData[] memory validators = middleware.sortOperatorsByVaults(currentEpoch);
uint256 gasAfter = gasleft();
uint256 gasSorted = gasBefore - gasAfter;
console2.log("Total gas used for sorted: ", gasSorted);

assertEq(validators.length, count + 3);
for (uint256 i = 0; i < validators.length - 1; i++) {
if (i > 0 && i < count - 1) {
assertLe(validators[i].stake, validators[i - 1].stake);
}
}
vm.stopPrank();
}

function testGasFor250OperatorsIn3VaultsNonSorted() public {
uint16 count = 250;
_addOperatorsToNetwork(count);

vm.startPrank(owner);
uint48 currentEpoch = middleware.getCurrentEpoch();

uint256 gasBefore = gasleft();
Middleware.ValidatorData[] memory validatorsNotSorted = middleware.getValidatorSet(currentEpoch);
uint256 gasAfter = gasleft();
uint256 gasNotSorted = gasBefore - gasAfter;
console2.log("Total gas used for non sorted: ", gasNotSorted);

vm.stopPrank();
}

function testGasFor350OperatorsIn3Vaults() public {
uint16 count = 350;
_addOperatorsToNetwork(count);

vm.startPrank(owner);
uint48 currentEpoch = middleware.getCurrentEpoch();
uint256 gasBefore = gasleft();
Middleware.ValidatorData[] memory validators = middleware.sortOperatorsByVaults(currentEpoch);
uint256 gasAfter = gasleft();
uint256 gasSorted = gasBefore - gasAfter;
console2.log("Total gas used for sorted: ", gasSorted);

assertEq(validators.length, count + 3);
for (uint256 i = 0; i < validators.length - 1; i++) {
if (i > 0 && i < count - 1) {
assertLe(validators[i].stake, validators[i - 1].stake);
}
}
vm.stopPrank();
}

function testGasFor350OperatorsIn3VaultsNonSorted() public {
uint16 count = 350;
_addOperatorsToNetwork(count);

vm.startPrank(owner);
uint48 currentEpoch = middleware.getCurrentEpoch();

uint256 gasBefore = gasleft();
Middleware.ValidatorData[] memory validatorsNotSorted = middleware.getValidatorSet(currentEpoch);
uint256 gasAfter = gasleft();
uint256 gasNotSorted = gasBefore - gasAfter;
console2.log("Total gas used for non sorted: ", gasNotSorted);

vm.stopPrank();
}

function testGasFor500OperatorsIn3Vaults() public {
uint16 count = 500;
_addOperatorsToNetwork(count);

vm.startPrank(owner);
uint48 currentEpoch = middleware.getCurrentEpoch();

uint256 gasBefore = gasleft();
Middleware.ValidatorData[] memory validators = middleware.sortOperatorsByVaults(currentEpoch);
uint256 gasAfter = gasleft();
uint256 gasSorted = gasBefore - gasAfter;
console2.log("Total gas used for sorted: ", gasSorted);

assertEq(validators.length, count + 3);
for (uint256 i = 0; i < validators.length - 1; i++) {
if (i > 0 && i < count - 1) {
assertLe(validators[i].stake, validators[i - 1].stake);
}
}
vm.stopPrank();
}

function testGasFor500OperatorsIn3VaultsNonSorted() public {
uint16 count = 500;
_addOperatorsToNetwork(count);

vm.startPrank(owner);
uint48 currentEpoch = middleware.getCurrentEpoch();

uint256 gasBefore = gasleft();
Middleware.ValidatorData[] memory validatorsNotSorted = middleware.getValidatorSet(currentEpoch);
uint256 gasAfter = gasleft();
uint256 gasNotSorted = gasBefore - gasAfter;
console2.log("Total gas used for non sorted: ", gasBefore - gasAfter);
vm.stopPrank();
}

function _createGateway() internal returns (address) {
ParaID bridgeHubParaID = ParaID.wrap(1013);
bytes32 bridgeHubAgentID = 0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314;
Expand Down
Loading