0

I am little confuse in the concept of Gas Estimation and price. When i add new user i get popup through metamask for the transaction amount and gas used which is totally fine.

I checked my code in the remix. When i see details the function which are purely reading data or returning the array length of user also use some Gas estimation like function name "totalInvestorInvestment" and "totalLoans". Where as "totalInvestedAmountInLoan" has gas estimate of Infinite its because I am using "For" loop.

enter image description here

1) So how this Gas estimation work as i read that reading data in blockchain is free, so if its free how that Gas usage is paid by the function "totalInvestorInvestment" and "totalLoans"?

2) And is there any limit for the usage of Gas in reading data which is free?

3) Is it good practice to use for loop? To avoid large data set i am also storing the indexes in array and using for loop on that array.

2

So how this Gas estimation work as i read that reading data in blockchain is free, so if its free how that Gas usage is paid by the function "totalInvestorInvestment" and "totalLoans"?

It's net free because read-only ops can't update the chain state. Gas is still subject to accounting and limits. Further, if a read-only function is called in the context of a state-changing transaction, then the gas cost is accounting for and charged.

And is there any limit for the usage of Gas in reading data which is free?

Yes. The block gasLimit applies.

Is it good practice to use for loop? To avoid large data set i am also storing the indexes in array and using for loop on that array.

It's not a good practice to have an unbounded loop or recursion. Things need to be O(1). For more elucidations on patterns, see this: https://blog.b9lab.com/getting-loopy-with-solidity-1d51794622ad

Hope it helps.

  • Thanks @rob for the answer, can you explan how it relate with the gasLimit. As in the block we only have information about the transactions, whereas reading a data is not a transaction and GasLimit relates with the transaction – Uahmed Feb 13 at 15:49
  • Reading does not cost gas, however, gas is supplied, accounting takes place and the gasLimit applies and it IS possible for a read-only operation to run out of gas. This lets you run simulations including checking remaining gas in contract functions. Have a look at this for better description: blog.b9lab.com/… – Rob Hitchens Feb 13 at 19:32
  • Thanks @rob for the reply. I check the block header and I found state root, transaction list root and receipt root. All of them stores the data related to the transaction. I don't find any root (tree) for the reading of the data as reading query is not a transaction. How exactly it relates with the GasLimit, because in the block verification it checks for all the transaction data, and again i don't find any values for the gas consume on the reading data. whereas you mentioned the Gas is used for accounting so where exactly it happens? – Uahmed Feb 14 at 5:54
  • 1
    You won't find it in the blocks because, as you say, read-only doesn't change anything. However, the gas accounting process is still at work. You can check the remaining gas in a function, a read-only transaction can run out of gas, and if a read-only function is called from a contract processing a transaction, then the cost of gas consumed really does add to the transaction cost. While it is theoretically possible to imagine a read-only client with unlimited gas/transaction, I know of no such implementation. Read my blog post for elaboration this. It's confusing. – Rob Hitchens Feb 14 at 6:10

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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