1

I would like to understand how the Ethereum Node is loading the contract bytecode in the EVM and then write everything that it needs, back to the blockchain.

This is how I understand it.

The contract get compiled into bytecode. It's deployed on the chain. When a user call that contract address to run a function. The node loads the contract in the EVM as bytecode. Run the request made by the user. It all happens in the EVM. So how, after execution on the node. does the node Write back the new data on the blockchain?

For example my contract have an empty array.

One of the function to call will add an entry in the array.

The new array contain 1 entry and need to go back on the chain. Does the node pull the data from EVM to recompile and rewrite it? I doubt it works like that since the EVM serve the purpose of running a code in the virtual environment.

My best guess would be that the EVM is actually program to run the bytecode and then rewrite the new content on the blockchain.

Now as per why this question?

I'm trying to repeat the process with the JVM or the V8 engine VM in NodeJS.

Instead of using solidity, I compile Javascript into Bytecode. So this would be my smart-contract like. And it's working. I can load a Javascript in the VM and call the functions and get response back.

Now all this code remains in the VM. And this is where I'm not sure what I would do next.

In my example where a call add an entry in an array. I want to write that new array back to the blockchain and I'm not sure about the approach. Does the Ethereum node make the bridge between EVM and the Blockchain? Or does the node use the EVM and only interact with the EVM that in turns interact with the blockchain?

2 Answers 2

1

In Ethereum each contract has a key-value storage that uses a Patricia Trie. The root of the storage tree is saved in the contract state, together with the bytecode, balance and nonce.

At a high level the solidity language defines how the different types are mapped to storage so at runtime the correct keys are computed when a variable is modified.

At a low level the EVM provides primitives to modify this storage: SSTORE(key, value) and SLOAD(key). Where key and value are exactly 32 bytes. The SSTORE opcode updates the storage root.

When the transaction finishes without a revert the final storage root is recorded in the contract state. If the transaction reverts the previous storage root is restored to the contract state.

5
  • Can you explain how the EVM load the contract state (I guess bytecode on the blockchain?) into the EVM. The way i would do it is get the bytecode at the contract address. create a context with it in the VM and execute transaction. And finally extract the new state from VM and write it back into the contract address bytecode on the blockchain. Thank you for your answer. Commented Feb 21, 2021 at 16:40
  • @MadeInDreams The bytecode is immutable so you don't need to write back. The contract state due to its size uses a Patricia Trie.
    – Ismael
    Commented Feb 21, 2021 at 20:34
  • So the Patricia Trie is the kinda of memory of the contract and it's on the blockchain. The node pick up a transaction that contains i presume bytecoded instruction that goes in the EVM to get executed by the contract. I'm looking for that missing link. So is the blockchain that each node have get loaded in the EVM. And the node are just injecting request and return the result. Commented Feb 21, 2021 at 20:49
  • @MadeInDreams The world state is not stored in the blockchain, only the latest root is stored in every block (the world state is also a patricia trie). But it can be reconstructed by processing every transaction. If a node wants to execute a transaction it should have the world state. The world state is a mapping from address to a some parameters: balance, bytecode, storage root, nonce (bytecode and storage root are empty for non-contract). To execute a contract it will read from the world state the bytecode and storage root and create a EVM instance with them and execute the call.
    – Ismael
    Commented Feb 21, 2021 at 22:25
  • Thanks that's what i was wondering! Commented Feb 21, 2021 at 23:20
1

Each contract account has persistent storage. The EVM loads and executes the contract code when calling the contract function. When executing the code, the EVM reads or writes the contract's storage value.

2

Your Answer

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

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