0

A transaction can send eth, interact with a smart contract, or deploy a smart contract.

From what I've heard, when you deploy a smart contract, you are actually running constructor code which then returns the bytecode for the smart contract to deploy.

From that, it appears that you can actually run code directly in an EVM transaction (code which is not part of a deployed contract).

  • Is that correct?

  • Does this mean that you can run arbitrary code once-off, without deploying a contract, by including it in a transaction?

1 Answer 1

1

No, you can't call code without deploying.

Here is the state transition function:

if contractCreation {
    ret, _, st.gas, vmerr = st.evm.Create(sender, st.data, st.gas, st.value)
} else {
    // Increment the nonce for the next transaction
    st.state.SetNonce(msg.From(), st.state.GetNonce(sender.Address())+1)
    ret, st.gas, vmerr = st.evm.Call(sender, st.to(), st.data, st.gas, st.value)
}

https://github.com/ethereum/go-ethereum/blob/3aea432b35f0344d9f7c1f1843a4eca496229ffc/core/state_transition.go#L307

It either has to be a creation of a new contract, or calling an exising contract. You can't even tell the EVM to run the code from some offset, it will run from PC = 0 (PC - Program counter)

Now, if you use the "Create" option, you can run the constructor code without contract being stored in StateDB (yet), but at the end, the contract must be either stored or return an error. If it returns an error, it will not be stored (state will be reverted) and your gas will be lost. But if you don't trigger an error in the constructor, your contract will be stored. Also, a call for CREATE will be very expensive since you have to pass all your code and you spend a Gas amount per every byte.

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.