Do we require a specific condition when working with indices on solidity?

0 votes

I know that an assert-style exception is generated in the situation if you access an array at a too large or negative index (i.e. x[i] where i >= x.length or i < 0).

But should I check the condition every time?

Following is my code:

pragma solidity ^0.4.0;

contract A{
    byte[10] arr;

    function setElement(uint index, byte value) public {
        require(index >= 0 && index < arr.length); //Should I leave it as is?
        arr[index] = value;
    }

    function getElement(uint index) view public returns (byte) {
        require(index >= 0 && index < arr.length); //Or not?
        return arr[index];
    }
}

Also, how do I refund the remaining gas to the executor?

Jul 23, 2018 in Blockchain by sabby
• 4,350 points
29 views

1 answer to this question.

0 votes

Your code is written correctly. require is intended to be used to check input parameters whereas assert is to verify the internals of your contract (mostly for testing purposes). If a require condition fails, the remaining gas will be refunded.

You can refer to the section in the solidity documentation here: http://solidity.readthedocs.io/en/develop/control-structures.html?highlight=require#error-handling-assert-require-revert-and-exceptions

answered Jul 23, 2018 by Perry
• 17,020 points

Related Questions In Blockchain

0 votes
1 answer

Should I require a specific condition when working with indexes in Solidity?

You are using it correctly. require is intended to ...READ MORE

answered Jul 17, 2018 in Blockchain by ariaholic
• 7,340 points
29 views
0 votes
1 answer

How can we decrypt a transaction payload when confidentiality is on?

You can try the following: you can only ...READ MORE

answered May 2, 2018 in Blockchain by Shashank
• 10,350 points
72 views
0 votes
2 answers

How do I interact with a smart contract on a private network through web3.js

I found a blog that explains how ...READ MORE

answered Aug 20, 2018 in Blockchain by slayer
• 29,170 points
369 views
+3 votes
4 answers

How do I add a new node on a Hyperledger network?

You can do this by generating the crypto ...READ MORE

answered Apr 6, 2018 in Blockchain by Christine
• 15,790 points
1,612 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
158 views
0 votes
1 answer
0 votes
1 answer

Solidity geth: Error encountered during contract execution [Bad instruction]

recipes is a dynamic storage array. You need ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 67,600 points
90 views
+1 vote
1 answer

Do we need metamask extension to make transactions on ether?

Actually, we don't need Metamask. To make ...READ MORE

answered Apr 7, 2018 in Blockchain by Perry
• 17,020 points
83 views
0 votes
1 answer

How do I develop a supply chain DApp on Blockchain?

Either of the ways is acceptable but ...READ MORE

answered May 30, 2018 in Blockchain by Perry
• 17,020 points
214 views