0

I'm trying to understand exactly how gas works. Wondering if it charges based on actual individual CPU instructions (like Assembly-level x86 instruction set architecture level of detail), or if it charges at a higher level such as individual functions or something like that.

The reason I ask is because if it tracked individual instructions to charge for gas, then that tracking is itself an instruction, so it would lead to an infinite recursion of charging. To prevent that, some level of "meta" instructions would need to be ignored. So I'm wondering what level of stuff is ignored, and what is charged, and how it gets charged.

2 Answers 2

1

The EVM runs bytecode, which uses assembler-like OPCODES. Bytecode is usually compiled from a higher-level language such as Solidity (but not limited to Solidity).

Gas pricing is by OPCODE at runtime.

It's not bound to CPU or other resource consumption on nodes. Indeed, different implementations Ethereum running on different OS platforms and CPU architecture will surely consume inconsistent hardware resources. Accounting of such concerns isn't feasible because the nodes wouldn't have any basis for agreement.

Gas approximates hardware resource accounting by accounting for OPCODES. The pricelist is a protocol-level network property. Therefore, all nodes can consider any transaction and correctly compute gas. Indeed, incorrect gas calculation for a transaction invalidates a block and the network will not accept it and the miner will not be rewarded for it.

Hope it helps.

0

Smart contracts in Ethereum run on the Ethereum Virtual Machine (EVM), which defines a set of low-level instructions that each have a gas cost associated with them.

So it's not actual CPU instructions but rather instructions for that virtual machine.

Your Answer

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

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