Etherum Componets
Etherum Componets
Ethereum Blockchain
With Ethereum’s state machine, we begin with a “genesis state.” This is analogous
to a blank slate, before any transactions have happened on the network
When transactions are executed, this genesis state transitions into some final
state.
At any point in time, this final state represents the current state of Ethereum.
The state of Ethereum has millions of transactions. These transactions are grouped
into “blocks.” A block contains a series of transactions, and each block is chained
together with its previous block.
Components of the Ethereum ecosystem
• The Ethereum blockchain stack consists of various components.
• At the core, there is the Ethereum blockchain running on the
peer-to-peer Ethereum network.
• Secondly, there's an Ethereum client (usually Geth) that runs on
the nodes and connects to the peer-to-peer Ethereum network
from where blockchain is downloaded and stored locally.
– It provides various functions, such as mining and account
management.
– The local copy of the blockchain is synchronized regularly
with the network.
• Another component is the web3.js library that allows
interaction with the geth client via the Remote Procedure Call
(RPC) interface.
Ethereum Architecture
Ethereum Architecture
• Gas limit
✔ The gas limit field contains the value that represents the maximum
amount of gas that can be consumed to execute the transaction.
✔ it is sufficient to say that this is the amount of fee in ether that a
user (for example, the sender of the transaction) is willing to pay
for computation.
• To
✔ This field is a value that represents the address of the recipient of
the transaction. This is a 20-byte value.
• Value
✔ represents the total number of Wei to be transferred to the
recipient; in the case of a contract account, this represents the
balance that the contract will hold.
• Signature: The signature is based on ECDSA scheme and makes
use of the secp256k1 Elliptic curve
Gas and payment
• With every transaction, a sender sets a gas limit and gas price.
• The product of gas price and gas limit represents the maximum
amount of Wei that the sender is willing to pay for executing a
transaction.
• For example, let’s say the sender sets the gas limit to 50,000 and a
gas price to 20 gwei.
• This implies that the sender is willing to spend at most 50,000 x 20
gwei = 1,000,000,000,000,000 Wei = 0.001 Ether to execute that
transaction.
Transactions and messages
▪ Init: The Init field is used only in transactions that are intended to create
contracts, that is, contract creation transactions.
✔ This represents a byte array of unlimited length that specifies the EVM code
to be used in the account initialization process. The code contained in this
field is executed only once when the account is created for the first time, it
(init) gets destroyed immediately after that.
✔ Init also returns another code section called body, which persists and runs in
response to message calls that the contract account may receive. These
message calls may be sent via a transaction or an internal code execution.
▪ Data: If the transaction is a message call, then the data field is used instead
of init, which represents the input data of the message call. It is also
unlimited in size and is organized as a byte array.
✔ This structure can be visualized in the following diagram, where a transaction
is a tuple of the fields mentioned earlier, which is then included in a
transaction trie (a modified Merkle-Patricia tree) composed of the
transactions to be included.
✔ Finally, the root node of transaction trie is hashed using a Keccak 256-bit
algorithm and is included in the block header along with a list of transactions
in the block.
Transactions and messages
• Transactions can be found in either transaction pools or blocks.
• In transaction pools, they wait for verification by a node or in blocks, they
are added after successful verification.
• When a mining node starts its operation of verifying blocks, it starts with
the highest paying transactions in the transaction pool and executes them
one by one.
• When the gas limit is reached, or no more transactions are left to be
processed in the transaction pool, the mining starts.
• In this process, the block is repeatedly hashed until a valid nonce is found
such that, once hashed with the block, it results in a value less than the
difficulty target.
• Once the block is successfully mined, it will be broadcasted immediately to
the network, claiming success, and will be verified and accepted by the
network.
• This process is similar to Bitcoin's mining process discussed in the previous
chapters, The only difference is that Ethereum's PoW algorithm is ASIC-
resistant, known as Ethash, where finding a nonce requires large memory.
Transactions and messages
Contract creation transaction
• There are a few essential parameters that are required when
creating an account. These parameters are listed as follows:
– Sender
– Original transactor (transaction originator)
– Available gas
– Gas price
– Endowment, which is the amount of ether allocated
– A byte array of an arbitrary length
– Initialization EVM code
– Current depth of the message call/contract-creation stack (current depth
means the number of items that are already there in the stack)
Contract creation transaction
diagram shows an EVM stack on the left side showing that elements are
pushed and popped from the stack. It also shows that a program counter is
maintained which is incremented with instructions being read from main
memory. Main memory gets the program code from virtual ROM / storage via
the CODECOPY instruction.
The Ethereum Virtual Machine (EVM)
• EVM optimization is an active area of research, and recent research has
suggested that EVM can be optimized and tuned to a very fine degree
to achieve high performance.
• Research into the possibility of using WebAssembly (WASM) is
underway already.
• WASM is developed by Google, Mozilla, and Microsoft and is now being
designed as an open standard by the W3C community group.
• WASM aims to be able to run machine code in the browser that will
result in execution at native speed.
• Similarly, the aim of EVM 2.1 is to be able to run the EVM instruction
set (opcodes) natively in CPUs, thus making it faster and efficient.
• Another language called Joyfully Universal Language for (Inline)
Assembly (JULIA) that can compile to various backends such as EVM
and eWASM
Execution environment
• There are some key elements that are required by the execution
environment to execute the code. The key parameters are
provided by the execution agent, for example, a transaction.
These are listed as follows:
✔ System state.
✔ Remaining gas for execution.
✔ The address of the account that owns the executing code.
✔ The address of the sender of the transaction.
✔ The originating address of this execution (it can be different from
the sender).
✔ The gas price of the transaction that initiated the execution.
✔ Input data or transaction data depending on the type of executing
agent. This is a byte array; in the case of a message call, if the
execution agent is a transaction, then the transaction data is
included as input data.
Execution environment
✔ The address of the account that initiated the code execution or
transaction sender.
✔ This is the address of the sender in case the code execution is
initiated by a transaction; otherwise, it is the address of the
account.
✔ The value or transaction value. This is the amount in Wei. If the
execution agent is a transaction, then it is the transaction value.
✔ The code to be executed presented as a byte array that the
iterator function picks up in each execution cycle.
✔ The block header of the current block.
✔ The number of message calls or contract creation transactions
currently in execution. In other words, this is the number of
CALLs or CREATEs currently in execution.
✔ Permission to make modifications to the state.
Execution environment
• The execution environment can be visualized as a tuple of
10 elements, as follows:
Machine state
• Machine state is also maintained internally by the EVM.
• Machine state is updated after each execution cycle of
EVM.
• An iterator function runs in the virtual machine, which
outputs the results of a single cycle of the state machine.
• Machine state is a tuple that consists of the following
elements:
– Available gas
– The program counter, which is a positive integer up to 256
memory contents
– Contents of the stack
– Active number of words in memory
Machine state
• The EVM is designed to handle exceptions and will halt (stop
execution) in case any of the following exceptions occur:
- Not having enough gas required for execution invalid
instructions
- Insufficient stack items
- Invalid destination of jump opcodes
- Invalid stack size (greater than 1024)
2. Reentrancy
• In reality, Ethereum smart
contracts come with a special
feature that can call or even use
codes from other external contracts.
• Typically, contracts use Ether and send Ether to other external contract
addresses.
• However, to perform all of these functions, the contract needs to use
an external call.
• Actually, these external calls don’t have adequate safety features;
thus, the hacker can attack these codes and ensure to execute more
tasks with the contract calling it.
• Therefore, the code can re-enter the contract, misuse the
information, or even send tokens to other addresses.
• In reality, this is exactly what happened in the DAO attack. Mainly this
attack can happen when you send Ether to an unknown contract.
Solidity issues
• The attacker can create a malicious program in an external
address, and this will introduce the fallback function.
• Therefore, when a contract sends any amount of Ether to that
address, it will start the malicious program.
• Fallback function is a special function available to a contract. It
has following features −
• It is called when a non-existent function is called on the contract.
• It is required to be marked external.
• It has no name and no arguments
• It can not return any thing.
• It can be defined one per contract.
• If not marked payable, it will throw exception if contract
receives plain ether without data.
Solidity issues
4. Clearing Mappings
• Clearing mappings is a huge solidity
issue due to having some limitations
of this programing language.
• In reality, the Solidity type mapping offers a key-value data structure,
which is storage-only for blockchain platforms.
• Thus, it will not track all the keys with a value other than zero.
• you can’t clean a mapping without adding any extra informationt,.
• In a dynamic storage array, if you want to use Mapping as the base type, if
you delete or even pop the array, that won’t affect the Mapping.
• More so, if you use Mapping in a struct’s member field as a type in a
dynamic storage array, here the Mapping will also be ignored even if you
delete the array.
Solidity issues
5. Arithmetic Precision
• Solidity does not support any floating-
or fixed-point numbers.
Therefore, to represent these floating points ,
you need to use an integer type in the Solidity.
• In reality, many developers can make errors or
create loopholes if they can’t implement this correctly.
• Therefore, developers need to implement their very own fixed-point data type
using the standard integer data type.
• This process is not easy and quite complex and can pose a lot of problems if not
pulled off correctly.
• More so, the 256 bits Ethereum Virtual Machine can create a lot of issues for the
data types as types shorter than 32 bytes are assembled together into the 32
bytes slot.
• Therefore, it affects the precision of any calculations as you can’t expect a proper
rounding when you perform any division before multiplication.
Solidity issues
• 6.Unexpected Ether
• Usually, when you are sending Ether to an
address, it needs to execute the fallback
function or other functions needed for the
contract.
• However, there are certain exceptions to these rules. In this case, the Ether can
stay in the contract without executing any code.
• Thus, contracts that rely on programs for every single Ether transaction are
vulnerable because it can send Ether forcibly to another contract.
• Typically, there are 2 ways one can send Ether forcibly to another contract, and it
will not use any payable function for that.
• In most cases, the blockchain developers don’t realize that you can accept or
even obtain Ether via other means aside from the payable function.
• It can lead to contracts having a false Ether balance, and it will lead to
vulnerability.
• It’s best to check the current Ether stored in the contract when before calling
any functions
Solidity issues
• 7. Relying on tx.origin
• tx.origin is another one of the issues with
Solidity.
• This one is a global variable and will scan
the call stack and give you the
address of the account that sent the transaction.
• The problem is that if you use this variable for authentication purposes, it can make your smart
contracts vulnerable.
Therefore, there is a heavy chance that your contract will face a phishing attack.
Basically, the attacker can trick the user into using the tx.origin variable to authenticate the
attacker to the contract.
• Here, the attacker can hide the function withdrawAll() within the tx.origin variable and create a
malicious code for the phishable cont ract.
• Usually, the attacker will deploy the code and convince the user to send some Ether to this
contract.
• The user will think that it’s a typical authentication contract and will send the Ether to the
contract (attackers address disguised within the contract).
• Thus, once the victim does that, it will invoke a fallback function which will call the withdrawAll()
function.
• As a result, all the funds from the victim will go to the attacker’s address.
• Thus, users should not depend on tx.origin for authentication purpose only as it’s one of the
major blockchain risks.
Solidity issues
8. Default Visibilities
• In Solidity, the functions have visibility to the public
unless you are assigning how anyone can call that function.
• Therefore, the visibility can determine who can call the function – external users,
derived contracts, internal users, etc.
• Any function which is default to public viewing can be called by users externally.
• In reality, many times, the developers tend to use incorrect specifiers, which will
severely damage the integrity of the smart contract.
• As the default is public, so always make sure to specify the visibility if you want to
change it.
• For example, if you are offering a reward to users who can perform a certain task
make sure to keep the visibility of the function private.
• If it’s public, then any user can just see the function and call it and take the reward
without performing any single task.
• It’s really easy to misuse this type of blockchain security concerns as many
newcomers don’t understand the concept fully.
• Therefore, it can become one of the dangerous solidity security issues if not checked
in time.
Solidity issues