Skip to content

Commit

Permalink
Clean up Operator and Sponsorship tests (#655)
Browse files Browse the repository at this point in the history
* test: organize all tests under describe blocks

* refactor: reorganize tests under describe blocks

* fix: bug in kicking and slashing all

also moved "is not possible to get slashed more than you have staked" to Sponsorship.test.ts, and fixed up other tests too

* test: removed TODO, fixed now

* test: cleaned up a very dodgy test

it was probably just testing the "2" case of TestAllocationPolicy

also made the "10" case more explicit

* test: fix lint

* fix: add coverage

remove double access checks

trigger undelegation self-delegation limit
  • Loading branch information
jtakalai authored Sep 28, 2023
1 parent be40171 commit 7bd2f1b
Show file tree
Hide file tree
Showing 10 changed files with 651 additions and 655 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ contract StakeModule is IStakeModule, Operator {
* Can only happen if all the delegators who want to undelegate have been paid out first.
* This means the operator must clear the queue as part of normal operation before they can change staking allocations.
**/
function _stake(Sponsorship sponsorship, uint amountWei) external onlyOperator {
function _stake(Sponsorship sponsorship, uint amountWei) external {
if(SponsorshipFactory(streamrConfig.sponsorshipFactory()).deploymentTimestamp(address(sponsorship)) == 0) {
revert AccessDeniedStreamrSponsorshipOnly();
}
Expand Down Expand Up @@ -40,13 +40,13 @@ contract StakeModule is IStakeModule, Operator {
* Take out some of the stake from a sponsorship without completely unstaking
* Except if you call this with targetStakeWei == 0, then it will actually call unstake
**/
function _reduceStakeTo(Sponsorship sponsorship, uint targetStakeWei) external onlyOperator {
function _reduceStakeTo(Sponsorship sponsorship, uint targetStakeWei) external {
_reduceStakeWithoutQueue(sponsorship, targetStakeWei);
payOutQueue(0);
}

/** In case the queue is very long (e.g. due to spamming), give the operator an option to free funds from Sponsorships to pay out the queue in parts */
function _reduceStakeWithoutQueue(Sponsorship sponsorship, uint targetStakeWei) public onlyOperator {
function _reduceStakeWithoutQueue(Sponsorship sponsorship, uint targetStakeWei) public {
if (targetStakeWei == 0) {
_unstakeWithoutQueue(sponsorship);
return;
Expand All @@ -59,7 +59,7 @@ contract StakeModule is IStakeModule, Operator {
}

/** In case the queue is very long (e.g. due to spamming), give the operator an option to free funds from Sponsorships to pay out the queue in parts */
function _unstakeWithoutQueue(Sponsorship sponsorship) public onlyOperator {
function _unstakeWithoutQueue(Sponsorship sponsorship) public {
uint balanceBeforeWei = token.balanceOf(address(this));
sponsorship.unstake();
_removeSponsorship(sponsorship, token.balanceOf(address(this)) - balanceBeforeWei);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,14 @@ contract Sponsorship is Initializable, ERC2771ContextUpgradeable, IERC677Receive
* Kicking does what slashing does, plus removes the operator
* NOTE: The caller MUST ensure that slashed tokens (if any) are added to some other account, e.g. remainingWei, via _addSponsorship
*/
function _kick(address operator, uint slashingWei) internal {
function _kick(address operator, uint slashingWei) internal returns (uint actualSlashingWei) {
if (slashingWei > 0) {
slashingWei = _reduceStakeBy(operator, slashingWei);
emit OperatorSlashed(operator, slashingWei);
actualSlashingWei = _reduceStakeBy(operator, slashingWei);
emit OperatorSlashed(operator, actualSlashingWei);
}
uint payoutWei = _removeOperator(operator);
if (operator.code.length > 0) {
try IOperator(operator).onKick(slashingWei, payoutWei) {} catch {}
try IOperator(operator).onKick(actualSlashingWei, payoutWei) {} catch {}
}
emit OperatorKicked(operator);
}
Expand All @@ -323,7 +323,7 @@ contract Sponsorship is Initializable, ERC2771ContextUpgradeable, IERC677Receive
* If operator had any locked stake, it is accounted as "forfeited stake" and will henceforth be controlled by the VoteKickPolicy.
*/
function _removeOperator(address operator) internal returns (uint payoutWei) {
if (stakedWei[operator] == 0) { revert OperatorNotStaked(); }
if (joinTimeOfOperator[operator] == 0) { revert OperatorNotStaked(); }

if (lockedStakeWei[operator] > 0) {
uint slashedWei = _slash(operator, lockedStakeWei[operator]);
Expand Down Expand Up @@ -354,7 +354,7 @@ contract Sponsorship is Initializable, ERC2771ContextUpgradeable, IERC677Receive
/** Get earnings out, leave stake in */
function withdraw() external returns (uint payoutWei) {
address operator = _msgSender();
if (stakedWei[operator] == 0) { revert OperatorNotStaked(); }
if (joinTimeOfOperator[operator] == 0) { revert OperatorNotStaked(); }

payoutWei = _withdraw(operator);
if (payoutWei > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ contract TestAllocationPolicy is IAllocationPolicy, Sponsorship {
bool failOnIncrease;
bool failEmptyOnIncrease;
bool sendDataWithFailInGetInsolvencyTimestamp;
bool failInGetInsolvencyTimestamp;
}

function localData() internal view returns(LocalStorage storage data) {
Expand All @@ -40,6 +41,8 @@ contract TestAllocationPolicy is IAllocationPolicy, Sponsorship {
localData().failEmptyOnIncrease = true;
} else if (testCase == 9) {
localData().sendDataWithFailInGetInsolvencyTimestamp = true;
} else if (testCase == 10) {
localData().failInGetInsolvencyTimestamp = true;
}
}

Expand All @@ -62,12 +65,14 @@ contract TestAllocationPolicy is IAllocationPolicy, Sponsorship {
}

/** Horizon means how long time the (unallocated) funds are going to still last */
function getInsolvencyTimestamp() public override view returns (uint horizonSeconds) {
function getInsolvencyTimestamp() public override view returns (uint) {
// return 2**255; // indefinitely solvent
if (localData().sendDataWithFailInGetInsolvencyTimestamp) {
require(false, "test_getInsolvencyTimestamp");
} else if (localData().failInGetInsolvencyTimestamp) {
require(false); // solhint-disable-line reason-string
}
require(false); // solhint-disable-line reason-string
return 0;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ contract TestKickPolicy is IKickPolicy, Sponsorship {
}

function onFlag(address operator) external {
_slash(operator, 100 ether);
_addSponsorship(address(this), 10 ether);
uint actualSlashingWei = _slash(operator, 10 ether);
_addSponsorship(address(this), actualSlashingWei);
}

function onVote(address operator, bytes32 voteData) external {
_kick(operator, uint(voteData));
_addSponsorship(address(this), uint(voteData));
uint actualSlashingWei = _kick(operator, uint(voteData));
_addSponsorship(address(this), actualSlashingWei);
}

function getFlagData(address operator) override external view returns (uint flagData) {
Expand Down
Loading

0 comments on commit 7bd2f1b

Please sign in to comment.