21

Why do precompiled contracts lead to Out of Gas on private blockchains?

Some tidbits are provided in the Solidity docs but can it be explained more?

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.

What is happening that "these contracts only really exist after they received the first message (although their contract code is hardcoded)"?

How does sending 1 Wei make these contracts "really exist"?

Will allocating 1 wei in the genesis block of a private blockchain, for each of these contracts, avoid the Out of Gas?

1 Answer 1

10

There is a general gas cost rule that states that sending message to any non-existing account costs more than sending message to an existing account. That also applies to the accounts of the precompiled contracts.

To not pay higher costs of the message you need to create the destination account before. That is the reason why some test/private genesis blocks creates the accounts for the precompiled contracts by setting theirs balances to 1 wei, e.g. testnet genesis block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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