2

Are there transactions with contract's codes inside it? (for example when a new contract is created or when a user call a method from a contract)

If not, how can ethereum vm know what code to be execute when needed?

1 Answer 1

2

The solidity documentation has a great section which addresses your question:

Transactions

A transaction is a message that is sent from one account to another account (which might be the same or the special zero-account, see below). It can include binary data (its payload) and Ether.

If the target account contains code, that code is executed and the payload is provided as input data.

If the target account is the zero-account (the account with the address 0), the transaction creates a new contract. As already mentioned, the address of that contract is not the zero address but an address derived from the sender and its number of transactions sent (the “nonce”). The payload of such a contract creation transaction is taken to be EVM bytecode and executed. The output of this execution is permanently stored as the code of the contract. This means that in order to create a contract, you do not send the actual code of the contract, but in fact code that returns that code.

You can see the contents of a transaction here.

Transactions contain:

  1. The recipient of the message
  2. A signature identifying the sender
  3. The amount of ether to transfer from the sender to the recipient
  4. An optional data field
  5. A STARTGAS value, representing the maximum number of computational steps the transaction execution is allowed to take
  6. A GASPRICE value, representing the fee the sender pays per computational step

The relevant property in regards to your question is the data field. This is where the transaction can specify the function it is trying to call, and any inputs to that function. This is also where a transaction will supply contract code which gets written to the blockchain.

See an example for the creation of the BAT contract here.

More details about code execution can be found here.

4
  • In fact, transactions can contain executable code -> the init code when creating a contract.
    – ivicaa
    Apr 21, 2018 at 21:38
  • I think your statement is in alignment with my explanation, but maybe the words I used are not the best choice. As i mentioned, the data field is where a transaction will contain the fully compiled contract code which gets written to the address, but it will never itself have functions or code which executes directly from the transaction. All code execution happens with the functions and code that is already written to the address. Is this accurate? Apr 21, 2018 at 21:49
  • AFAIK when sending TX to 0x0 (contract creation), the init/constructor code is immediately executed and its return value is the contract code which is written to the database.
    – ivicaa
    Apr 21, 2018 at 21:56
  • 1
    I think your statement is more accurate than mine. Details here. I updated my post. Thanks! Apr 21, 2018 at 22:07

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.