Over a million developers have joined DZone.

Blockchain Ethereum: Part 2

DZone's Guide to

Blockchain Ethereum: Part 2

In this part of our series, we take a closer look at Ethereum transactions, how they work, and some of the OpsCode needed to make them run.

· Security Zone ·
Free Resource

Discover how to provide active runtime protection for your web applications from known and unknown vulnerabilities including Remote Code Execution Attacks.

You can look back to Ethereum Blockchain Part 1 here.

Ethereum Transactions: 

"A signed data package that stores a message to be sent from an externally owned account (EOA) to another account on the Blockchain." - Hudson Jameson

Anything that you want to do inside a blockchain has to be via a transaction.

A transaction contains the below attributes:

  • To: The recipient (account or contract address).

  • From: The sender (account or contract address), who is initiating the transaction.

  • Gas Price: Each OpCode (any computation step we want to execute has the equivalent OpCode) in the EVM has a fixed price. Gas is an execution fee that senders of transactions need to pay for every operation made on the Ethereum Blockchain. Since it’s a kind of ‘crypto-fuel’ it has been given the name 'gas.' 

  • Gas Limit: The maximum computational steps the transaction can execute; it means every transaction has a maximum number of allowed computational steps. 

  • Gas Used: Sum of all the gas for all the OpCodes executed.

  • Value: The amount of Ether in Wei to be transferred (the base unit of Ether is called Wei).

  • Signature: To identify the sender, so that sender's intention can be verified.

  • Nonce: The transaction sequence number for the sending account; which helps to index and process transactions in the right order.

  • Block Height: This is the unique block number.

  • Data: The payload; raw data or a contract.

If you specify too little gas then the transaction will start to be executed but may run out of gas and be stopped with an 'Out of Gas' exception.

Note: Ethereum is not based on RSA encryption. Instead, it uses Elliptic Curve Cryptography (ECC). 

What Is OpCode?

This is a machine language instruction which instructs the machine to perform specific operations. Thus, OpCode could be understood as an assembly level language.

Whatever code we are writing as part of smart contracts or transactions, it gets compiled and interpreted by the EVM (Ethereum Virtual Machine) and generates equivalent OpCode so that the EVM can execute it when required.

Each of these operations has some cost, measured in units of 'gas.' The cumulative summation of all OpCode in any transaction is the total cost for that transaction. 

Some Ethereum OpCode and its Significance Is Shown Below:

Stop and Arithmetic Operations:

0x00    STOP        Halts execution
0x01    ADD         Addition operation
0x02    MUL         Multiplication operation
0x03    SUB         Subtraction operation
0x04    DIV         Integer division operation
0x05    SDIV        Signed integer
0x06    MOD         Modulo
0x07    SMOD        Signed modulo
0x08    ADDMOD      Modulo
0x09    MULMOD      Modulo
0x0a    EXP         Exponential operation
0x0b    SIGNEXTEND  Extend length of two's complement signed integer

Comparison & Bitwise Logic Operations:

0x10    LT      Lesser-than comparison
0x11    GT      Greater-than comparison
0x12    SLT     Signed less-than comparison
0x13    SGT     Signed greater-than comparison
0x14    EQ      Equality  comparison
0x15    ISZERO  Simple not operator
0x16    AND     Bitwise AND operation
0x17    OR      Bitwise OR operation
0x18    XOR     Bitwise XOR operation
0x19    NOT     Bitwise NOT operation
0x1a    BYTE    Retrieve single byte from word


0x20    SHA3    Compute Keccak-256 hash

Environmental Info:

0x30    ADDRESS         Get address of currently executing account
0x31    BALANCE         Get balance of the given account
0x32    ORIGIN          Get execution origination address
0x33    CALLER          Get caller address. This is the address of the account that is directly responsible for this execution
0x34    CALLVALUE       Get deposited value by the instruction/transaction responsible for this execution
0x35    CALLDATALOAD    Get input data of current environment
0x36    CALLDATASIZE    Get size of input data in current environment
0x37    CALLDATACOPY    Copy input data in current environment to memory This pertains to the input data passed with the message call instruction or transaction
0x38    CODESIZE        Get size of code running in current environment
0x39    CODECOPY        Copy code running in current environment to memory
0x3a    GASPRICE        Get price of gas in current environment
0x3b    EXTCODESIZE     Get size of an account's code
0x3c    EXTCODECOPY     Copy an account's code to memory

Block Info: 

0x40    BLOCKHASH   Get the hash of one of the 256 most recent complete blocks
0x41    COINBASE    Get the block's beneficiary address
0x42    TIMESTAMP   Get the block's timestamp
0x43    NUMBER      Get the block's number
0x44    DIFFICULTY  Get the block's difficulty
0x45    GASLIMIT    Get the block's gas limit

Stack, Memory, Storage, and Flow Operations:

0x50    POP         Remove item from stack
0x51    MLOAD       Load word from memory
0x52    MSTORE      Save word to memory
0x53    MSTORE8     Save byte to memory
0x54    SLOAD       Load word from storage
0x55    SSTORE      Save word to storage
0x56    JUMP        Alter the program counter
0x57    JUMPI       Conditionally alter the program counter
0x58    PC          Get the value of the program counter prior to the increment
0x59    MSIZE       Get the size of active memory in bytes
0x5a    GAS         Get the amount of available gas, including the corresponding reduction
0x5b    JUMPDEST    Mark a valid destination for jumps

System Operations:

0xf0    CREATE          Create a new account with associated code
0xf1    CALL            Message-call into an account
0xf2    CALLCODE        Message-call into this account with alternative account's code
0xf3    RETURN          Halt execution returning output data
0xf4    DELEGATECALL    Message-call into this account with an alternative account's code, but persisting the current values for `sender` and `value`

We don't need to write a program in low-level (using OpCode) as high-level languages exist for designing the smart contracts.

Understanding Transaction Cost

  • Estimated Transaction Cost

           If the GasLimit= 2100 and GasPrice= 23.089 gWei

           Interpretation of above line - "we wanted to spend a maximum 2100 Gas at the price of  23.089 gWei"

           So, the estimated maximum transaction cost = GasLimit * GasPrice

                                                         = 2100 * 23.089

                                                         = 53104.7 gWei

                                                         = 0.0000531047 ether (1 ether = 1000000000 gWei)

  • Actual Transaction Cost

           If the GasUsed = 2040 and GasPrice=23.089 gWei

           Interpretation of the above line - "we spent 2040 Gas at the price of  23.089 gWei."

           So, Actual transaction cost = GasUsed * GasPrice

                                                         = 2040 * 23.089

                                                         = 47101.56 gWei

                                                         =0.00004710156 ether (1 ether = 1000000000 gWei)

I hope this will give you a better understanding about blockchain and Ethereum transactions. 

Happy learning!

Find out how Waratek’s award-winning application security platform can improve the security of your new and legacy applications and platforms with no false positives, code changes or slowing your application.

blockchain ,ethereum ,security

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}