I read in many places that view functions do not cost gas however that's not true because they cost gas for blockchain operations even if those do not change the state. Correct me if I am wrong For example:
pragma solidity ^0.4.17;
contract myContract {
uint[] public anArray; /// assume it has 1,000,000 (1Million) elements
/* function which returns how many times a specific element is in the n array*/
function elementIndex(uint element) public view returns(uint){
uint counter;
for(uint i; i<anArray.length; i++){
if(anArray[i] == element){
counter++;
}
}
return counter;
}
function returnAllElements() public view returns(uint[]){
return anArray;
}}
The real problem comes on the front end when the array has 1 million elements. Running elementIndex() will never work because the iteration will take too long. returnAllElements will not work either (I assume).
What can be done in such circumstances?
viewonly costs gas when called from normal (state changing) function, no? I don't think it matters if you callviewfrom other contract as long as the function in other contract is alsoview. Or i'm wrong? – Maxpeinas Sep 25 '18 at 11:08the function can be resolved in the local node that you are using. What is a local node? If you running a website in Azure - you most likely do not have an access to the console of the Unix (unless owning a virtual machine) and you are not installing any Ethereum nodes locally. Do you STILL not pay transaction fee in such cases? – Alex Jun 15 '20 at 12:24