Ethereum Transactions

Hello friends, Hope you are doing good and having fun. I am here with another article on the blockchain. Previously, we have discussed the blockchain, some of its concepts, and its characteristics, and today I will talk about transactions. We would also explore the concept of gas in this tutorial. So let’s start by defining a transaction.

What is Ethereum Transaction?

  • A transaction is a message signed by an account owner that is added to a block and recorded in the blockchain.
  • A transaction is initiated by an externally owned account and then it gets transmitted to the Ethereum network.
  • It can be originated only by an externally owned account (an account owned by a human) and not by a contract account.
  • Through transactions, one can interact with the Ethereum world. As a result, they change and update the state of Ethereum.
  • The transaction can be any action such as transferring ether or calling functions of a smart contract. Consequently, the state of the Ethereum virtual machine (EVM) is changed. For example, when account x transfers some ether to account y, a transaction is initiated and after that, the balances of both accounts are updated. The new balances are then stored in the Ethereum network.
  • Any account or node has the right to initiate a transaction. But it must be verified through mining.
  • Every transaction has a gas limit and a gas fee. This fee is given to miners as a reward for verifying (or mining) that transaction. I will cover the concept of mining in my next tutorials.

These points highlight the importance of a transaction. You can see that a transaction is the only thing that can change the state of the Ethereum virtual machine. By transaction, you can execute a smart contract or deploy a new smart contract to Ethereum and there is no other way to do that. Contracts cannot deploy themselves, contracts cannot call other contracts unless a transaction is initiated by an externally owned account. So, the Ethereum network state depends on the transactions.

Now, this was the definition and importance of a transaction in the blockchain. In the next part, I am going to explain the structure of a transaction.

Structure of Ethereum Transaction

Any transaction in the Ethereum network has some specific components when it is transmitted to the network. Every node receives the transaction and stores it in its memory. The data of a transaction or the components are listed below:

1. Recipient:

  • The address of the account that will receive the transaction is called the recipient. The recipient can be an externally owned account or a contract account. Each transaction is directed towards a recipient.

2. Nonce:

  • A nonce is a number showing the sequence of a transaction, Nonce is assigned by the account initiating the transaction.

3. Gas Price:

  • Any transaction requires some fee that is paid by the creator of the transaction. The fee is calculated per gas unit. The unit is Wei a smaller unit of ether.

4. Gas Limit:

  • The creator of the transaction gives the number of gas units that can be used for the transaction. This is the maximum limit of gas that would be consumed.

5. Value:

  • The value specifies the amount of Wei or Ether that the sender account wants to send to the recipient.

6. Data:

  • If a transaction recipient is a smart contract then the data contains information for executing functions of the contract. This includes data of variable length.

7. Signature:

  • A Signature is the identification of the sender. The signature is generated when an externally owned account confirms and signs the transaction via its private key.

These are the components included in a transaction. In the next section, I will discuss the types of transactions in the Ethereum blockchain.

Types of Ethereum Transaction

The transactions can be classified into three general types that are listed below and defined further.
  1. Funds Transfer
  2. Contract Deployment
  3. Function Execution

Funds Transfer

This type of transaction takes place whenever an account sends some funds (in form of ether) to another account. This transaction has a value associated with it but does not have any data. For example, when account A sends some money to account B for buying something. The gas price is set by the sender.

Contract Deployment

Whenever a contract is deployed on the Ethereum network, this transaction takes place. It has data that is the bytecode of the smart contract. There is no recipient for this kind of transaction. The gas limit must be enough for deploying a contract.

Function Execution

After a contract is deployed on Ethereum, its functions can be called. When a function is called by an account, then this type of transaction occurs. The transaction is directed towards a contract account, therefore the recipient is a contract address. The data contains the function name and its arguments.

So, friends, this was a general classification of transactions. Up till now, you must have got an idea of a transaction and its components. The steps of a transaction generation till its confirmation can be listed as below:

  1. A transaction is initiated by the sender. The sender’s account provides important information such as recipient address, data, value, and gas limit. After generation, a hash is created that is called a transaction hash.
  2. The transaction has is a unique identifier for a transaction. An example of this hash is given below:

0x2446f1fd773fbb9f080f674b60c6a033c7ed7427b8b9413cf28a2a4a6da9b56c

  1. The transaction generated is transmitted to the network and added to the transactions pool waiting to be confirmed.
  2. A miner picks transactions for the pool to add to the block. This verifies the transactions. When there are a lot of transactions, the waiting time is more. Therefore, a transaction with a higher gas price would be picked first by a miner. In this way, the miner will get more money.
  3. A number called block confirmation number is given to a verified and added transaction. This shows the number of blocks created after the block in which the transaction was added. As the chain length of the blockchain increases, the transaction becomes more and more trusted and immutable.

This is the process through which a transaction passes. I hope you have an idea of it's working. Next time I will be here with another topic. Till then, take care!

What is Solidity Programming

Hello friends, hope you are doing fine and doing great. In the previous tutorial for introducing Smart Contracts, I told you about Solidity. Solidity is a high-level programming language for writing smart contracts. It is the most popular programming language for Ethereum. So, in this article, I will explain the basic components of solidity. Let’s get started by revising the key points of solidity.

Solidity Programming

  • Solidity programming language is made for writing code for the Ethereum platform. These codes (smart contracts) help in executing different functions in Ethereum virtual machine (EVM).
  • It is easy to learn the language. If you are familiar with C and JavaScript, you can easily learn solidity.
  • The code written in solidity is converted into bytecode by solidity compiler. The compiler of solidity language is solc.
  • The extension of a solidity file is .solc.
  • It is a case-sensitive language.
  • Solidity is a statically typed language.
  • The process of writing and compilation is shown in the figure below.

The layout of a Solidity File

In this section, I am going to discuss the general layout of a solidity file. It contains different components. The layout of a solidity file is explained below. It has:
  1. Pragma
  2. Comments
  3. Import
  4. Contract / Library

Pragma in Solidity Programming

  • Before writing the solidity program, you want to mention the version of solidity compiler. So that anyone using the file can choose the correct version for compilation.
  • For this purpose, pragma directive is used. It specifies the compiler version of the file.
  • Solidity is still growing and a lot of improvements have been made since its development. Therefore, the version keeps on improving.
  • The current solidity version is 0.7.4.
  • The version is mentioned by using the following syntax.

pragma Solidity ^0.7.0;

I should mention here that semi-colon is the statement terminator in solidity.
  • It is declared as the first statement of the program.
  • The version number comprises a major build number and a minor build number. When you use ^ character with version number, it means the latest version of the major build number.
  • For example, as I declared the version 0.7.0 with ^ character. Here major build number is 7. So solidity compiler of version 7 will compile it.

Comments in Solidity Programming

  • Guys, if you are familiar with programming, you must know that programming languages support the option of commenting. Solidity also provides this facility.
  • There are two types of comment in solidity:
    • Single-Line Comments: single-line comment means a comment comprising of one line only.
    • Multi-Line Comments: multiline comments consist of more than one line.
  • Forward slashes are used for denoting comments. Single line comment is denoted by double forward slash //. While multiline comment starts with /* and ends with */.
  • It is a good practice to add comments to your program. This improves readability.

Import in Solidity Programming

  • The import keyword helps in importing other solidity files. These files can then be used in code.
  • The functions of the imported file can then be used in a solidity file.
  • This functionality helps us in writing code in the form of modules. In this way, lengthy code can be converted into smaller readable ones.
  • The syntax for using the import keyword is given below:

import filename;

  • The address of the file is mentioned within the import statement.

Contract / Library in Solidity Programming

  • The contracts are defined in the solidity file.
  • You can define more than one contract or library in one file.
  • Contracts, libraries and interfaces can be defined one after the other in a file.
So, this was the general layout of a solidity file. Now, I will move to the next section in which we are going to discuss the structure of a contract.

Structure of a Contract

Contracts are somehow similar to classes in OOP (Object Oriented Programming) Languages. The contracts are written for ethereum virtual machine. Just like other languages, contract code contains functions and variables. The contracts can also inherit from other solidity contracts. There are two special types of contracts in solidity called library and interface. In this section, I have discussed different constructs in a contract. The contract consists of the following:
  1. State Variables
  2. Structures
  3. Modifier Functions
  4. Event Declarations
  5. Enumerations
  6. Functions
Now let’s define these for further understanding.

State Variables

  • Variables store values in storage locations. The value saved in a variable can be change or update while programming execution.
  • Once a variable is declared, you can use it anywhere in your program, at multiple locations.
  • State variables are stored in Ethereum blockchain permanently. The changes in their value and their current value is secured.
  • The data type of state variable is declared while writing the program.
  • Other qualifiers, used while declaration, are listed below:
    • Internal
    • Private
    • Public
    • Constant
  • The following data types can be used in solidity:
    • Bool for boolean
    • Uint / int for unsigned / signed integer
    • Bytes for 8 bit signed integer
    • Address for addresses of accounts
    • Mapping for mappings
    • Enum for enumerations
    • Struct for structures
    • String for character strings

Structure

  • With the help of structure, you can define a data type of your own.
  • A structure is a composite data type and that means it can contain different variables having different or same data types.
  • A group of variables is defined in a structure. Each one is given a name and a data type.
  • A structure is declared by using the struct keyword.

Modifier

  • A modifier is a function that can change the behavior of the code.
  • In solidity, modifiers are used with functions, so they can change the actions of a function.
  • If you are calling a solidity function that has a modifier associated with it, then the modifier function will execute first. After its execution, the called function would be executed.
  • You will define modifier function only once in the program but you can use it anywhere with multiple functions.

Events

  • If you are familiar with programming, you know that different programming languages support events.
  • Solidity also supports events. Events tell about the change in the state of a contract.
  • With the help of logs, the caller can view the state of the contract after execution.
  • Events are declared outside functions, at the global level. Then these events can be called in any function.
  • The keyword for event declaration is an event. After that, the identifier is declared followed by a parameter list. The parameter list contains the data type of the variables.

Enumeration

  • Enumeration provides us with an interesting facility. With the help of it, you can define a data type of your own choice.
  • It contains a list of constants, from which the variable can take values.
  • Each value is given an integer starting from 0 for solidity.
  • The enumeration is declared by the enum keyword. After the keyword, the identifier is defined followed by the list of constant values in brackets.
  • There is no semi-colon at the end of the enum declaration statement.

Function

  • Just like any other programming language, a function is a key component of solidity.
  • Whenever a transaction is initiated, a function of a smart contract is called.
  • Functions can read and write state variables. When a function is executed and a transaction is processed, the state variable may change its state.
  • A function can take input parameters, perform operations on them and can return values. The return values can be more than one.
  • A function has a name that is its identifier. This identifier can be used anywhere in the program for the execution of functions.
  • You can use various qualifiers with a function declaration that decides the visibility of a function. These qualifiers are listed below:
    • Public
    • Private
    • Internal
    • External
  • Other than visibility qualifiers, some other qualifiers are also used with functions. These show the ability of the underlying function in terms of changing values of state variables. The qualifiers are given below:
    • Constant
    • View
    • Pure
    • Payable
So, friends, I have tried to give you a basic idea of the structure of a solidity program. It is not compulsory for a program to have all of the above-listed features. I hope you have learned and got a good understanding of the basics of this language. That was all from my side. Please let me know if you have any questions. I would love to answer them. I am signing off for today, Take care!

Structure of a Block in Blockchain

Hello guys, hope you are doing good and enjoying your lives. Today, I am going to introduce you to the blocks of a blockchain. I gave you an understanding of blockchain, its characteristics, and some idea about accounts and wallets in my previous tutorials, and today my article is about the structure of a block in the blockchain. I will first define the block before going into the details about its structure. So let’s start without any further delay.

Block in Blockchain

  • A block is actually the building block or the key element of a blockchain.
  • The definition of a blockchain is based on its blocks. As I defined in my previous posts, a blockchain is a chain of multiple blocks.
  • Blocks contain transactions. Each block contains a different number of transactions.
  • These transactions are contained in blocks so that they would be added to the distributed ledger.
  • The number of transactions is limited by the block size and gas limit. Generally, the block contains more than 500 transactions.
  • Other than transactions, a block also consists of some metadata. This metadata is stored in the header of the blockchain.
  • The size of a block header is 80 bytes, the detail of which is given in the upcoming sections of this article.
  • Let’s first define a parent block in the next part.

Parent Block in Blockchain

As the blocks are linked together one after the other, they have a parent-child relationship. Each block is the parent of the upcoming block. Each child block contains the hash of the previous block i.e., its parent block. The first block of the blockchain is called Genesis Block and it has no parent. The block contains different fields which can be roughly categorized as listed below:
  • The block size: The size of this field is 4 bytes and it contains the size of the block.
  • The block header: The size of a block header is 80 bytes. It further contains different fields.
  • The transaction counter: This field contains the number of transactions and the size of it is between 1-9 bytes.
  • The transactions: This field contains transactions of the block and its size is variable.
This was some information regarding the fields of an Ethereum block. Now, we will move towards the next part in which I am going to give you an idea about the block header and its fields.

Block Header in Blockchain

The header of an Ethereum block contains different fields of metadata which are listed below.
  • Hash of the previous block: Every block header gives information about the previous or parent block. This field contains the hash value of the previous block and this reference connects all the blocks. The size of this field is 32 bytes.
  • Version: This field stores the version number to show software upgrades. The size of the version field is 4 bytes.
  • Difficulty: The mining difficulty at the time of the block creation is stored in this field. The concept of mining would be explained in the upcoming articles. Its size is 4 bytes.
  • Timestamp: This field contains the time at which the block was created. The size of this field is 4 bytes.
  • Nonce: A nonce is a value used during the mining of the block. This field’s size is also 4 bytes.
  • Merkle tree root: A Merkle tree is a structure obtained from hashing the transactional data of a block. The root of this tree is stored in the block header under this field. 32 bytes is the size of the Merkle tree root field.
This was all about the header of a block. In the next part, I am going to give you an idea about the properties of a block.

Properties of a Block:

There are a lot of properties of a block that give us important information about it. I am listing down the properties here.

Difficulty Property:

  • As I mentioned earlier, this property gives the difficulty level of solving the puzzle to mine the block.

totalDifficulty Property:

  • This property of the block tells us the total difficulty of the blockchain.

gasLimit Property:

  • This property tells us the maximum gas allowed by the block which in turn tells us about the number of transactions that the block can accommodate.

gasUsed Property:

  • The gasUsed property gives the amount of gas used by the block for executing all of its transactions.

Number Property:

The number shows the block number in the list of blocks.

Transactions Property:

  • This means all of the transactions contained in the block.

Hash Property:

  • This property shows the hash of the block.

parentHash Property:

  • This property saves the hash of the previous block.

Nonce Property:

  • Nonce property shows the nonce value that I defined earlier. It is a variable used in mining the block.

Miner Property:

  • The miner property gives information about the miner of the block. This gives the account of the block miner.
So guys these were the properties of a block. I will move towards the next part i.e., the identification of a block.

Block Identification:

The blocks of a blockchain need an identification to refer them or distinguish them from other blocks. Two parameters are used for this purpose which are given below:

1. Block Hash

Block hash is the main identification parameter of a block. This value is obtained by cryptographic hashing of the header of the block. The hashing operation is performed twice. The header of the block contains metadata of the block and when this data is hashed the result is the block hash, whose size is 32 bytes. The hash of the block is not stored in the block’s data whether the block is being transmitted to the other nodes or it is stored as part of the blockchain on some node. When a node receives a block from the network, it computes its hash itself.

2. Block Height

The second parameter used for identifying a block is its height. As we already know that the blocks are linked together in a list type structure starting from the genesis block. The genesis block is given a height 0 zero. The second block in the blockchain or the first block after the genesis block is at height 1 and so on. In this article, I have explained the structure of an Ethereum block. The article explained the header and some properties of the blocks. I hope you have learned something new from it. Take care!
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir