near / core-contracts

Core contracts: reference staking pool, lockup, voting, whitelist, multisig.
326 stars 132 forks source link

bug: (staking-pool) edge case bug - contract account with low balance #99

Open luciotato opened 4 years ago

luciotato commented 4 years ago

If the staking pool is created on an account with balance = 1_000_000_000_000, then total_stake_shares is 0, and several view functions start to panic

Note: I'm preparing a PR to handle this edge case

To reproduce, add this unit test: (the 4th parameter is the account initial balance)

    #[test]
    fn test_low_staked_balance() {
        let mut emulator = Emulator::new(
            owner(),
            "KuTCtARNzxZQ3YvXDeLjx83FDqxv2SdQTSbiq876zR7".to_string(),
            zero_fee(),
            STAKE_SHARE_PRICE_GUARANTEE_FUND, //account initial balance, minimum, edge case
        );
        //bob deposits 1M
        let deposit_amount = ntoy(1_000_000);
        emulator.update_context(bob(), deposit_amount);
        emulator.contract.deposit();
        emulator.amount += deposit_amount;

        //bob wants to know how much unstaked balance he has
        emulator.update_context(bob(), 0);
        assert_eq!(
            emulator.contract.get_account_unstaked_balance(bob()).0,
            deposit_amount
        );
        //panic!
        //thread 'tests::test_low_staked_balance' panicked at 'The total number of stake shares can't be 0', src/internal.rs:297:9

    }
evgenykuzyakov commented 4 years ago

It's not an issue for the production, because the minimum amount of NEAR the account can have is 0.01 due to state staking: https://nomicon.io/Economics/README.html#state-stake

Still it's good to address it, so proceed with the fix.