From c676ecb8cfce2c06c2d9a4214b028b70735a94b2 Mon Sep 17 00:00:00 2001 From: Greg Hysen Date: Tue, 22 Oct 2019 11:53:00 -0700 Subject: [PATCH] Force no-op when moving zero stake or moving from undelegated to undelegated. --- .../staking/contracts/src/stake/MixinStake.sol | 11 +++++++++++ .../staking/test/unit_tests/stake_test.ts | 18 +++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/contracts/staking/contracts/src/stake/MixinStake.sol b/contracts/staking/contracts/src/stake/MixinStake.sol index 415792238e..c0b11bbd86 100644 --- a/contracts/staking/contracts/src/stake/MixinStake.sol +++ b/contracts/staking/contracts/src/stake/MixinStake.sol @@ -111,6 +111,17 @@ contract MixinStake is { address staker = msg.sender; + // Sanity check: no-op if no stake is being moved. + if (amount == 0) { + return; + } + + // Sanity check: no-op if moving stake from undelegated to undelegated. + if (from.status == IStructs.StakeStatus.UNDELEGATED && + to.status == IStructs.StakeStatus.UNDELEGATED) { + return; + } + // handle delegation if (from.status == IStructs.StakeStatus.DELEGATED) { _undelegateStake( diff --git a/contracts/staking/test/unit_tests/stake_test.ts b/contracts/staking/test/unit_tests/stake_test.ts index a4b84fd251..76e5dd5d47 100644 --- a/contracts/staking/test/unit_tests/stake_test.ts +++ b/contracts/staking/test/unit_tests/stake_test.ts @@ -373,7 +373,7 @@ blockchainTests.resets('MixinStake unit tests', env => { expect(increaseNextBalanceEvents).to.be.length(0); }); - it('moves the owner stake between the same pointer when both are undelegated', async () => { + it('does nothing when moving the owner stake from undelegated to undelegated', async () => { const amount = getRandomInteger(0, 100e18); const { logs } = await testContract.moveStake.awaitTransactionSuccessAsync( { status: StakeStatus.Undelegated, poolId: VALID_POOL_IDS[0] }, @@ -381,10 +381,18 @@ blockchainTests.resets('MixinStake unit tests', env => { amount, ); const events = filterLogsToArguments(logs, StakeEvents.MoveStakeStorage); - expect(events).to.be.length(1); - expect(events[0].fromBalanceSlot).to.eq(stakerUndelegatedStakeSlot); - expect(events[0].toBalanceSlot).to.eq(stakerUndelegatedStakeSlot); - expect(events[0].amount).to.bignumber.eq(amount); + expect(events).to.be.length(0); + }); + + it('does nothing when moving zero stake', async () => { + const amount = new BigNumber(0); + const { logs } = await testContract.moveStake.awaitTransactionSuccessAsync( + { status: StakeStatus.Delegated, poolId: VALID_POOL_IDS[0] }, + { status: StakeStatus.Delegated, poolId: VALID_POOL_IDS[1] }, + amount, + ); + const events = filterLogsToArguments(logs, StakeEvents.MoveStakeStorage); + expect(events).to.be.length(0); }); it('moves the owner stake between the same pointer when both are delegated', async () => {