Analyzing Ethereum Smart Contract

shivam pandey
Vitwit
Published in
4 min readJul 23, 2018

This is our learning journal of how Smart Contract engine works in the Ethereum Virtual Machine.

Ethereum has been described in several ways.The basic definition being a decentralized blockchain platform for “building unstoppable applications”, while Ether is the cryptocurrency used on this platform.

What are Smart Contracts ?

Smart Contracts is the business logic that runs on a blockchain. The complexity of the Smart Contract depends on the strategy that you would use to solve your business problem with blockchain.

“A smart contract, also known as a cryptocontract, is a computer program that directly controls the transfer of digital currencies or assets between parties under certain conditions.”

The limitations of blockchain like lack of Turing completeness, value-blindness,lack of state led to the proposal of Ethereum. In Ethereum, the state is made up of objects called “accounts” having an address and state transitions being transfer of value and information between accounts.

Getting into the details of how Ethereum works,it works on stack based architecture of word size 256 bytes. The storage model of EVM is similar to memory but uses word-addressale word array.When we create the basic object i.e “account”,it contains following main parameters :-

sender (s),
original transactor (o),
available gas (g),
gas price §,
endowment (v),
an arbitrary length byte array,
the initialization EVM code(i),
the present depth of the message-call/contract-creation stack (e)and
permission to make modifications to the state (w).

The address a is the rightmost 160 bits of the Keccak hash of the RLP encoding
of the structure containing only the sender and the account
nonce.
The initialization of account is done using code i with EVMs execution model which returns the resultant state (σ∗∗), remaining gas(g∗∗), the sub state A which changes multiple times during execution and the body code of the account.
(σ∗∗,g∗∗,A,o) ≡ Ξ(σ∗,g,I,{s,a})
where I contains the parameters for execution model which areas follows
Ia ≡ a =>the address of the account which owns the code
that is executing.

Io ≡ o =>the sender address of the transaction that originated this execution
Ip ≡ p=>the price of gas in the transaction that originated this execution.
Id ≡ ()=>the byte array that is the input data to this
execution

Is ≡ s=>the address of the account which caused the
code to be executing

Iv ≡ v=>the value, in Wei
Ib ≡ i=> the byte array that is the machine code to be
executed.

Ie ≡ e=>the block header of the present block.
Iw ≡ w=>the depth of the present message-call or
contract-creation

When initialization code is executing,the newly created address exists but contains no body.In this sub state there is no response in case of message call and in case of Self destruct due to error,it leaves a zombie account.This state is called subtleties as the code is subtle in this case.

Understanding Ethereum’s execution model

Before Understanding execution model let go through how EVM works ? Ethereum is a protocol for executing a virtual computer in an open and distributed manner. This virtual computer is called the Ethereum Virtual Machine(EVM). The programs on EVM are called Ethereum smart contracts . A deployed Ethereum smart contract is public under adversarial scrutiny, and the code is not updatable. Most applications (auctions, prediction markets, identity/reputation management etc.) involve smart contracts managing funds or authenticating external entities. In this environment, the code should be trustworthy.
we have identified the runtime state of EVM. While EVM is executing an account’s code, EVM has access to the stack, the memory,the memory usage counter, the storage, the program counter, the balances of all accounts, the caller, the value sent along the current call, the data sent along the current call, the initial state kept for reverting into, the external account that originated the transaction, the codes on all addresses, the current block,the remaining gas, existence of accounts, and the list of touched storage indices.

The execution model takes care of how fees is calculated, execution of contract, halting of EVM and updating machine state. The fees calculated(gas) is mainly for three reasons. The most common reason is for computation but fees is also calculated for other contracts implemented from within or for increased usage of memory. For execution it uses the previously mentioned parameters and additionally uses H which stores info on how EVM is halted.
The Ξ function is computed which returns μ’g which is remaining gas that gets added back to the user account. An error handling function Z is implemented which on becoming true results in exceptional halting else H undergoes controlled halt. Once the execution model comes to a controlled halt, the machine state is updated and returns gas left, program counter, memory count, active words and stack contents. This machine state is updated to the blockchain.

We, at Vitwit, are digging deep on smart contract engine this is just a introductory session , we will be back very soon with more insights of deigns and implementation of smart contract engine 4.0. Thats All Folk’s.

--

--