# Breaking Down the EVM CREATE Opcode The CREATE opcode is an important piece of the Ethereum Virtual Machine (EVM), responsible for deploying smart contracts. Let’s walk through how it all works, using Geth (a popular Ethereum client) as a reference. ## Contract Deployment Workflow ### Step 1: Crafting the Deployment Transaction Deploying a Solidity smart contract begins with creating a deployment transaction. Here’s what makes this step special: - The **`to` field** is left blank. That’s the signal to the Ethereum client that this is a contract creation transaction. - The **`data` field** is filled with the compiled contract bytecode. This bytecode has two parts: the **initialization code** (which handles setup tasks, like initializing variables or accepting ETH in the contract's constructor function) and the **runtime code** (the part that lives on the blockchain). Once deployment is done, the initialization code is dropped, leaving only the contract code. ### Step 2: Processing the Transaction in Geth Now, after this transaction has been sent to the network, an execution client like Geth steps in to process the transaction, triggering the CREATE opcode (using the [opCreate function](https://github.com/ethereum/go-ethereum/blob/08e6bdb550712503873fb2a138b30132cc36c481/core/vm/instructions.go#L659)). Here’s what it handles behind the scenes: 1. **Calculating the Contract’s Address** Every new contract has a unique address. Geth uses this formula to calculate the contract's address: `keccak256(rlp_encode(sender_address, nonce))` This combines the sender’s address with their current transaction nonce to ensure the address is distinct. See [here](https://github.com/ethereum/go-ethereum/blob/08e6bdb550712503873fb2a138b30132cc36c481/core/vm/evm.go#L556) and [here](https://github.com/ethereum/go-ethereum/blob/08e6bdb550712503873fb2a138b30132cc36c481/crypto/crypto.go#L115). 2. **Incrementing the Sender’s Nonce** The nonce, which keeps transactions unique, gets bumped up by one. This also prevents replay attacks. 3. **Creating a Contract Account** The CREATE opcode sets up a new account for the contract in Ethereum’s state trie (its way of organizing data). This account will eventually hold the contract’s code and track its state. 4. **Running Initialization Code** Geth spins up a fresh EVM environment to execute the initialization code. ### Step 3: Running the Initialization Code This is where the magic happens. The initialization code is responsible for preparing the contract. It might set up storage, handle fund transfers, or configure key variables. This init code handles the initial setup—things like setting up state variables, executing the constructor, and more. If successful, it outputs the **runtime code** (the final version of the contract) that gets stored on the blockchain. - If everything runs smoothly, the final contract code is saved to the blockchain. - If something goes wrong—like running out of gas—the entire deployment is reverted. However, the sender still loses the gas spent, and their nonce is permanently incremented. ### Step 4: Updating the Blockchain When deployment succeeds, several things take place: - A **new contract account** is created. Its storage starts empty unless preloaded in the contract's constructor function. - The **contract code** is written to the blockchain. - Any **value sent with the transaction** is transferred to the contract. - The sender’s **balance decreases** by the gas cost and the transaction value. - The sender’s **nonce is updated**. ### Step 5: Wrapping Up The deployment wraps up with one of two outcomes: - **Success**: The contract’s address is returned, signaling that everything worked. - **Failure**: Any issues, like insufficient gas, cause a full rollback of state changes. The only things that stick are the gas costs and nonce increment. The CREATE opcode may seem like a behind-the-scenes player, but it’s the backbone of smart contract deployment on Ethereum. Knowing how it works—from address calculation to state updates—can help developers deploy contracts more efficiently.