-
Notifications
You must be signed in to change notification settings - Fork 465
Tests for new staking mechanics #2126
Tests for new staking mechanics #2126
Conversation
653941d
to
3363852
Compare
There was a problem hiding this 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; |
There was a problem hiding this comment.
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 @@ | |||
/* |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 @@ | |||
/* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as below
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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 () => { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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.
3363852
to
a6add55
Compare
a6add55
to
eca8607
Compare
68c2ab6
to
20cabc1
Compare
eca8607
to
313bb5b
Compare
There was a problem hiding this 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
// compute expecnted changes | |
// compute expected changes |
operatorRewardVaultBalance: rewardNotForDelegator, | ||
}); | ||
}); | ||
it('Should stop collecting rewards after undelegating, after several epochs', async () => { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Smash them together!
f84df45
to
5c9cebc
Compare
cd23a16
to
b202a74
Compare
5c9cebc
to
07fce98
Compare
b202a74
to
da83f75
Compare
07fce98
to
04f2665
Compare
04f2665
to
49baafa
Compare
Description
These are the tests for #2118. I put them in this PR for readability.
Testing instructions
Types of changes
Checklist:
[WIP]
if necessary.