2

I need some help.

I would like to know the best way to detect if a var is inside a range knowing that the range is dynamic, it can change (the owner could change the range).

e.g.

v = 4

is v inside any range in the list?

0-29 > yes
30-100 > no
101- 2 ** 256 > no

In the future, maybe I would need to add/remove ranges.

Any help? Thanks.

1 Answer 1

1

I rephrased the question in a way that makes more sense to me and sketched this out.

pragma solidity 0.4.19;

contract Ranges {

    address public owner;

    uint public lowerLine;
    uint public upperLine;

    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }

    event LogNewLines(address sender, uint lower, uint upper);

    function isValidRange(uint lower, uint upper) public pure returns(bool isIndeed) {
        return lower > 0 && upper > lower;
    }

    function Ranges(uint lower, uint upper) public {
        require(isValidRange(lower,upper));
        owner = msg.sender;
        lowerLine = lower;
        upperLine = upper;
        LogNewLines(msg.sender, lower, upper);
    }

    function isInRange(uint check) public view returns(bool isIndeed) {
        return check >= lowerLine && check <= upperLine;
    }

    function setRange(uint lower, uint upper) public onlyOwner returns(bool success) {
        require(isValidRange(lower,upper));
        lowerLine = lower;
        upperLine = upper;
        LogNewLines(msg.sender, lower, upper);
        return true;
    }
}

Hope it helps.

UPDATE

Based on the comment below, I'm going to suggest that you invert responsibilities.

Relying on the contract to determine which range the number falls in is intuitive in a world of approximately unlimited CPU power. It will always involve some sort of search that will increase in cost, by recursion or interations, etc. Make the client do that.

Clients will already have sufficient information to figure it out for themselves based on the event log. That is to say, with knowledge of the current ranges, a client doesn't even need help from the contract. The contract's function is to protect the integrity of the application from wrong, fraudulent, invalid, etc. input.

So have the contract check the validity of client claims.

Consider:

function isInRange(uint rangeId, uint candidate) public view returns(isIndeed) 

Hope it helps.

5
  • Thank you so much for your help. The thing is that I would really use a list a ranges, not only 1 range. What I need is to detect if a number is inside a range and get to return the associated value for that range. Do you know what I mean? Thank you again!
    – Eusthace
    Apr 4, 2018 at 23:46
  • Have a look at the update. rangeId would be a row in an array of pairs of uints. Just reduce the problem to a simple matter of maintaining the list of uint pairs and broadcasting list updates to clients. Apr 5, 2018 at 0:18
  • Hhuumm, ok. Let's say I want to make sure the first 30 users that joined the app have specific badge. Then the next users until 500 other badge. How would you do it with a combination of trust/smart contract and client?
    – Eusthace
    Apr 5, 2018 at 0:46
  • You should spell out the use-case. I'm not sure the test-a-range idea is ideal. Apr 5, 2018 at 0:56
  • Ok, yes, maybe not. I will post another questions explaining this case. Thank you again.
    – Eusthace
    Apr 5, 2018 at 1:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.