Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Conversation

hysz
Copy link
Contributor

@hysz hysz commented Sep 3, 2019

Description

These are the tests for #2118. I put them in this PR for readability.

Testing instructions

Types of changes

Checklist:

  • Prefix PR title with [WIP] if necessary.
  • Add tests to cover changes as needed.
  • Update documentation as needed.
  • Add new entries to the relevant CHANGELOG.jsons.

@hysz hysz requested a review from abandeali1 as a code owner September 3, 2019 21:48
@hysz hysz force-pushed the feature/staking/NewMechanicsSolidityOnly-Squashed-Tests-Squashed branch from 653941d to 3363852 Compare September 3, 2019 22:01
Copy link
Contributor

@jalextowle jalextowle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job on this one! The tests are beautiful! A few nits and comments, and then it seems g2g

@@ -21,8 +21,6 @@
pragma solidity ^0.5.5;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Update to 0.5.9.

@@ -1,9 +1,11 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this be updated in this PR, or another one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The simulations will be the last to get updated, after the implementation is finalized and the unit tests are done. Likely, next week.

@@ -1,3 +1,6 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(see above)

@@ -1,47 +1,357 @@
import { ERC20ProxyContract, ERC20Wrapper } from '@0x/contracts-asset-proxy';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(File Wide Comment) These tests are amazingly readable, and after reading through the StakerActor, they are also rigorous 😄. The actor pattern is sweet!

});
});
describe('Simulations', () => {
it('Simulation from Staking Spec', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have a simulation that is a bit shorter for debugging purposes. If this simulation finds a bug, it may be a bit non-trivial to figure out where the bug lives.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. I worked to include all of the edge cases individually, in the tests above. I wanted to keep this just to have some semblance of a legitimate sequence of interactions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough! Before writing this comment, I had the thought that the Simulations would likely not get hit in the case of a failure (because of an earlier failure). Up to you about whether to include a shorter sequence.

Out of scope: Given the complexity of the staking logic, I think that some potential bugs could be found in pathologically long sequences (if there are any). I think it would really behoove us to do some fuzzing using the created actors.

expect(balances.stakeBalanceInVault, 'stake balance, recorded in vault').to.be.bignumber.equal(
expectedBalances.stakeBalanceInVault,
);
expect(balances.withdrawableStakeBalance, 'withdrawable stake balance').to.be.bignumber.equal(
expectedBalances.withdrawableStakeBalance,
);
expect(balances.activatableStakeBalance, 'activatable stake balance').to.be.bignumber.equal(
expectedBalances.activatableStakeBalance,
expect(balances.activeStakeBalance.current, 'active stake balance (current)').to.be.bignumber.equal(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for the named expect. This is super nice to read, and I'm sure it's nice to debug.

expectedStakerBalances.stakeBalanceInVault = initStakerBalances.stakeBalanceInVault.plus(amount);
expectedStakerBalances.activatedStakeBalance = initStakerBalances.activatedStakeBalance.plus(amount);
expectedStakerBalances.activeStakeBalance.current = initStakerBalances.activeStakeBalance.current.plus(amount);
expectedStakerBalances.activeStakeBalance.next = initStakerBalances.activeStakeBalance.next.plus(amount);
await this.assertBalancesAsync(expectedStakerBalances);
// check zrx balance of vault
const finalZrxBalanceOfVault = await this._stakingWrapper.getZrxTokenBalanceOfZrxVaultAsync();
expect(finalZrxBalanceOfVault).to.be.bignumber.equal(initZrxBalanceOfVault.plus(amount));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Add descriptions to expects across the whole file for consistency and easier debugging. It may even be good to pass the calling functions name into assertBalancesAsync so that the error messages are even nicer when assertBalancesAsync is called by other functions in the actor.

@hysz hysz mentioned this pull request Sep 4, 2019
4 tasks
@hysz hysz force-pushed the feature/staking/NewMechanicsSolidityOnly-Squashed-Tests-Squashed branch from 3363852 to a6add55 Compare September 4, 2019 05:50
@hysz hysz requested a review from fabioberger as a code owner September 4, 2019 05:50
@hysz hysz force-pushed the feature/staking/NewMechanicsSolidityOnly-Squashed-Tests-Squashed branch from a6add55 to eca8607 Compare September 4, 2019 06:46
@hysz hysz force-pushed the feature/staking/NewMechanicsSolidityOnly-Squashed branch from 68c2ab6 to 20cabc1 Compare September 4, 2019 07:19
@hysz hysz force-pushed the feature/staking/NewMechanicsSolidityOnly-Squashed-Tests-Squashed branch from eca8607 to 313bb5b Compare September 4, 2019 07:21
Copy link
Contributor

@jalextowle jalextowle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Nice work, as always 🍻

const operatorShareByPoolId = await this._getOperatorShareByPoolIdAsync(this._poolIds);
const rewardVaultBalanceByPoolId = await this._getRewardVaultBalanceByPoolIdAsync(this._poolIds);
const memberBalancesByPoolId = await this._getMemberBalancesByPoolIdAsync(this._membersByPoolId);
// compute expecnted changes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
// compute expecnted changes
// compute expected changes

operatorRewardVaultBalance: rewardNotForDelegator,
});
});
it('Should stop collecting rewards after undelegating, after several epochs', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for test coverage

@@ -198,7 +198,7 @@ contract MixinExchangeFees is

// sanity check - this is a gas optimization that can be used because we assume a non-zero
// split between stake and fees generated in the cobb-douglas computation (see below).
if (totalFeesCollected == 0 || totalWeightedStake == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the purpose of this? Seems to just add more logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may no longer need this with the new math. But these were denominators in the old cobb-douglas implementation, so this avoided any division by zero.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, why remove the check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ~ if there's no stake in a given pool then the entire reward goes to the operator. Similarly, if no pools are staked then the reward goes entirely to operators. Note that it's still in a pool's best interest to stake since alpha != 0. So if all the pools are unstaked, then one pool could come along with relatively low volume but non-zero stake and take a good portion of the total reward.

Copy link
Contributor

@dorothy-zbornak dorothy-zbornak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Smash them together!

@hysz hysz force-pushed the feature/staking/NewMechanicsSolidityOnly-Squashed-Tests-Squashed branch 3 times, most recently from f84df45 to 5c9cebc Compare September 5, 2019 13:37
@hysz hysz force-pushed the feature/staking/NewMechanicsSolidityOnly-Squashed branch from cd23a16 to b202a74 Compare September 5, 2019 13:37
@hysz hysz force-pushed the feature/staking/NewMechanicsSolidityOnly-Squashed-Tests-Squashed branch from 5c9cebc to 07fce98 Compare September 5, 2019 19:12
@hysz hysz force-pushed the feature/staking/NewMechanicsSolidityOnly-Squashed branch from b202a74 to da83f75 Compare September 5, 2019 19:12
@hysz hysz force-pushed the feature/staking/NewMechanicsSolidityOnly-Squashed-Tests-Squashed branch from 07fce98 to 04f2665 Compare September 5, 2019 19:29
@hysz hysz force-pushed the feature/staking/NewMechanicsSolidityOnly-Squashed-Tests-Squashed branch from 04f2665 to 49baafa Compare September 5, 2019 19:38
@hysz hysz merged commit fc7f2e7 into feature/staking/NewMechanicsSolidityOnly-Squashed Sep 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants