I'm just playing around with testrpc vs creating my own private blockchain and for some reason when I use testrpc and i use SHA256 in my smart contract to return a hash it gives me a proper one. But when I start using my own private blockchain it returns 0x. Anyone know a solution to this?

PS : I used keccak256 and it works good in both testrpc and my private blockchain. it only happens with SHA256

  • 1
    Since you seem to have done some work, you should share the code for what works and what doesn't. – ReyHaynes Mar 14 '18 at 13:53
  • alright! i'm going to update it later in a sec – Rain de Castro Mar 14 '18 at 16:03

You could try sending more gas. While SHA3 has its own EVM instruction, SHA256 is one of the prebuilt contracts, which means that it actually results in a call.

From the docs:

It might be that you run into Out-of-Gas for sha256, ripemd160 or ecrecover on a private blockchain. The reason for this is that those are implemented as so-called precompiled contracts and these contracts only really exist after they received the first message (although their contract code is hardcoded). Messages to non-existing contracts are more expensive and thus the execution runs into an Out-of-Gas error. A workaround for this problem is to first send e.g. 1 Wei to each of the contracts before you use them in your actual contracts. This is not an issue on the official or test net.

  • Thanks for the recommendation! I gave my contract the maximum amount of gas that it can take and sha256 still returns 0x to me. Just an additional question, do you know where the geth files are hidden, I've seen this github fix that might be able to fix it but i'm not sure where i'll be able to find the files. (link: github.com/ethereum/go-ethereum/pull/16229) – Rain de Castro Mar 14 '18 at 16:06
  • I'm not sure what you mean by this. The pull request has been merged, so this fix is already on the master branch. Another thought: have you tried sending 1 wei to the prebuilt contracts as described in the docs? The address for sha256 is 0x0000000000000000000000000000000000000002. – mafrasi2 Mar 14 '18 at 20:38

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.