I'm starting to learn smart contracts in ethereum and have some questions about the concept of "Reading / Writing" state.

I know reading state doesn't cost gas.

But when we execute a read state and compute with it in the smart contract (without posting any transaction)

  1. Who's doing the computation? is it the node that initiated the contract function?

  2. If the computation is done locally, is it safe to use encryption algorithm in the function?

  3. Is there any network cost? ( or the node will just compute from its chaindata? )


  • your local machine will just be reading the the state that it has generated from the blockchain. No network cost. – Malone Nov 27 '17 at 17:46
  1. The local node does the computation with its own resources and its copy of the blockchain.

  2. You can safely pass unencrypted data to an encryption helper function in the contract as long as that function is marked with the modifier pure, view or constant. The information won't be transmitted to the network.

  3. There is no network cost but gas will still be computed. This can be relevant when read-only functions are called as part of on-chain transactions (i.e. called from functions that are doing things on the network). It provides an upper bound to the complexity of the operation.

Hope it helps.

  • Thanks for helping to clarify! As a follow up on 2 & 3, if I have a data that I want to encrypt before I post it to the blockchain, then the gas would be computed and taken even if it is computed on the local node? Or if the computation is part of a function that posts a transaction, then it will be computed by the network? (and no longer safe anymore) – Martinet Lee Nov 27 '17 at 18:04
  • You could have function hashHelper(bytes32 secret) public pure returns(bytes32 hash) { return keccak256(secret); } and that would be safe, local, not use too much "free" gas, and then function recordSecret(bytes32 hash) public returns(bool success) that would be broadcast everywhere. You'll find it tricky to reveal or prove on-chain without spilling the beans. – Rob Hitchens - B9lab Nov 28 '17 at 2:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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