Überlauf-/Unterlaufprobleme?

pragma solidity 0.5.1;

contract basics{
    using SafeMath for *;
    uint time;
    address payable sender;
    uint amount;
    event moneySent(address s,uint a);

    mapping(address=>person) public accounts;

    struct person {
        uint time;
        uint vault;
    }        

    function sendToContract() public payable{// isWithinLimits(msg.value){
        accounts[msg.sender].vault=accounts[msg.sender].vault.add(msg.value);
        accounts[msg.sender].time=now;
    }

    function takeFromContract() public payable{
        if(now>=accounts[msg.sender].time+10 seconds){
            msg.sender.transfer(accounts[msg.sender].vault);
            accounts[msg.sender].time=now;
        }           
    }
}

/**
 * @title SafeMath v0.1.9
 * @dev Math operations with safety checks that throw on error
 * change notes:  original SafeMath library from OpenZeppelin modified by Inventor
 * - added sqrt
 * - added sq
 * - added pwr
 * - changed asserts to requires with error log outputs
 * - removed div, its useless
 */
library SafeMath {

    /**
    * @dev Multiplies two numbers, throws on overflow.
    */
    function mul(uint256 a, uint256 b)
        internal
        pure
        returns (uint256 c)
    {
        if (a == 0) {
            return 0;
        }
        c = a * b;
        require(c / a == b, "SafeMath mul failed");
        return c;
    }

    /**
    * @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
    */
    function sub(uint256 a, uint256 b)
        internal
        pure
        returns (uint256)
    {
        require(b <= a, "SafeMath sub failed");
        return a - b;
    }

    /**
    * @dev Adds two numbers, throws on overflow.
    */
    function add(uint256 a, uint256 b)
        internal
        pure
        returns (uint256 c)
    {
        c = a + b;
        require(c >= a, "SafeMath add failed");
        return c;
    }

    /**
     * @dev gives square root of given x.
     */
    function sqrt(uint256 x)
        internal
        pure
        returns (uint256 y)
    {
        uint256 z = ((add(x,1)) / 2);
        y = x;
        while (z < y)
        {
            y = z;
            z = ((add((x / z),z)) / 2);
        }
    }

    /**
     * @dev gives square. multiplies x by x
     */
    function sq(uint256 x)
        internal
        pure
        returns (uint256)
    {
        return (mul(x,x));
    }

    /**
     * @dev x to the power of y
     */
    function pwr(uint256 x, uint256 y)
        internal
        pure
        returns (uint256)
    {
        if (x==0)
            return (0);
        else if (y==0)
            return (1);
        else
        {
            uint256 z = x;
            for (uint256 i=1; i < y; i++)
                z = mul(z,x);
            return (z);
        }
    }
}

Wenn dieser Code ausgeführt und getestet wird und 10.000 eth aus der Brieftasche mit 99 eth gesendet werden, zeigt die Brieftasche an, dass sie jetzt 99.900 n eth hat, und wenn erneut 10.000 gesendet werden, wird sie auf 99 eth in der Brieftasche zurückgesetzt. Könnte dies ein Unterlauf- oder Überlauffehler sein?

Antworten (1)

NEIN.

msg.senderSie ziehen in der Auszahlungsphase nichts vom Kontostand ab. SafeMath verhindert einen Unterlauf, sodass Konten nicht überzogen werden können, wenn Sie dies tun.

Das Problem, denke ich, hängt von Ihrer Testmethode ab.

Wenn 10.000 eth aus der Brieftasche mit 99 eth gesendet werden, zeigt die Brieftasche an, dass sie jetzt 99.900 n eth hat, und wenn erneut 10.000 gesendet werden, wird sie zurückgesetzt

Ich glaube nicht, dass das stimmt. Sie können keine 10.000 eth aus einer Brieftasche senden, die nur 99 eth hat. Nicht möglich. Sie senden wahrscheinlich 10.000 Wei oder eine andere kleinere Einheit.

Ich stelle mir vor, das wirklich wahrscheinliche Problem ist, dass Sie die Brieftasche (senden) oder den Vertrag (abheben) bitten, mehr Geld zu senden, als es tatsächlich hat, auch nicht möglich, also kehrt es zurück.

Es gibt keine bekannte Möglichkeit, einen Vertrag zu erstellen, der gegen die Regel verstößt, dass niemand Geld senden kann, das er nicht wirklich hat.

Ich hoffe es hilft.