29

Do contracts also have a nonce and if so how is it calculated? Usual accounts have a nonce that is increased with every transaction. However, contracts can't make transactions, they can only make calls that are triggered by transactions.

But contracts still can create other contracts and for the newly generated contract the nonce of the sender is relevant. So it seems that contracts also have nonces. Is this nonce increased every time they call another contract or only if they create another contract?

37

Yes, contracts have nonces. EIP 161 made the contract nonce start at 1, and before EIP 161 the nonce would start at 0. A nonce of a contract is only incremented when that contract creates another contract (@zanzu's YP reference). When a contract invokes a function on another contract, a so called "internal transaction" (in http://live.ether.camp), the nonce is not incremented.

The nonce can be obtained by using web3.eth.getTransactionCount. There is no built-in method for a contract to access an account's nonce, including its own. (A contract could use its storage to keep track of its own nonce.)

To make it clear, there are only 2 types of accounts in Ethereum and they all have the same 4 "physical" properties: nonce, balance (in wei), hash of storage trie root, and code. The only "physical" difference is that a contract has non-zero code.

In terms of behavior, there are some more differences between contracts and usual accounts. The incrementing of the nonce is one behavioral difference; another major one is Where is the private key for a contract stored?


To clarify "Usual accounts have a nonce that is increased with every transaction", the nonce of a non-contract account is only incremented when that account originates a transaction (when it's the sender from account). When an account is the recipient (the to account), its nonce is not incremented.

12

The answer to your question can be found in the yellow paper:

nonce: A scalar value equal to the number of trans- actions sent from this address or, in the case of accounts with associated code, the number of contract-creations made by this account

Remember: in Ethereum, contracts are accounts with code associated with them.

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.