Ethereum Tutorial – A Deeper Look Into Ethereum!

Jul 19,2018
Ethereum Tutorial – A Deeper Look Into Ethereum!
Add to Bookmark Email this Post 2.9K    0

Ethereum Tutorial:

In this Ethereum Tutorial blog, I will explain the inner working of ethereum’s architecture and I will also show you the implementation of ethereum through a simple smart contract. 

I see Ethereum as a programmable blockchain that will serve a majority of  B2C enterprise in the future. Ethereum grants developers the freedom of creating complex models to be executed on the blockchain, instead of restricting them to predefined operations like the Bitcoin blockchain.

With this approach, Ethereum has turned itself into a platform for a plethora of decentralized applications and organizations which includes, but isn’t limited to crypto-currencies.

I’ll be covering a variety of topics in an exhaustive manner through the run of this “Ethereum Tutorial” blog. These topics include:

You may go through this recording of Ethereum Tutorial where our instructors have explained the topics in a detailed manner with examples that will help you to understand this concept better.

Ethereum Tutorial | Ethereum Smart Contracts | Edureka

 

Ethereum Tutorial: Ethereum Accounts

The Ethereum network has two types of accounts, namely:

  • External Accounts
  • Contract Accounts

These accounts, both External and Contract are referred to as “state objects” and comprise the “state” of the ethereum network. Every state object has a well-defined state. For external accounts, the state comprises of the account balance while for contract accounts the state is defined by the memory storage and balance.

I’ll be referring to external accounts simply as accounts. These accounts are owned by are represented by external agents of the network which include every ordinary user, miners, automated agents, etc.

These accounts are generally controlled with the help of public key cryptography algorithms like RSA. The main purpose of External accounts is to serve as a medium for users to interact with the Ethereum Blockchain.

Contract accounts, on the other hand, are a collection of code that resides on the blockchain at a specific address. These contracts are invoked by external accounts, or by other contracts through a specific call-to-action function.  These contracts are written in high-level scripting languages like Solidity, Serpent or LLL. Every contract that resides on the ethereum blockchain is stored in a specific format called EVM (Ethereum Virtual Machine) bytecode which is an ethereum specific binary format.

It will only be fair that I explain EVM now that I’ve told you about EVM-bytecode.

Ethereum Tutorial: Ethereum Virtual Machine

Ethereum, in a rustic way, defines a set of generalized protocols which have become the pillars of the development of decentralized applications. At the heart of this, lies the Ethereum Virtual Machine. The figure below explains the architecture:

Ethereum Architecture - Ethereum Tutorial - Edureka

It is important to note that, the Ethereum Virtual Machine is not only completely sandboxed, but also completely isolated. This means that code that is currently running on the EVM has no access to the network or the file-system and can sparingly access other contracts.

Now that we understand the core of the platform, let’s take a deeper look at the network nodes.

Ethereum Tutorial: The Ethereum Network

The ethereum network is a public blockchain network. It forms the basis of all decentralized peer-to-peer applications and organizations run on the network. The network is comprised of two types of nodes namely, full nodes and light-weight-nodes.

Full nodes contain the entire history of transactions since the genesis block. They are a full-fledged proof of the integrity of the blockchain network. Full nodes have to contain each and every transaction that has been verified according to the rules set up by Ethereum’s specifications.

Light-weight nodes on the other hand only contain a subset of the entire blockchain.these types of nodes are mostly used in e-wallets which have to be light-weight in nature and hence the entire blockchain cannot be stored on them. These nodes, in contrast, do not verify every block or transaction and may not have a copy of the current blockchain state. They rely on full nodes to provide them with missing details (or simply lack particular functionality). The advantage of light nodes is that they can get up and running much more quickly, can run on more computationally/memory constrained devices, and don’t eat up nearly as much storage.

Every public blockchain has a currency attached to it. Ethereum is no different. Let’s take a deeper look into Ethereum’s cryptocurrency.

Ethereum Tutorial: Ether and Gas

Ether is the name of the crypto-currency used to pay for transactions on the ethereum network. Asides from paying for general transactions and services, Ether is also used to buy Gas, which in turn is used to pay for computation within the EVM.

Ether is the metric unit and has a lot of denominations which help accurately pay for transactions and gas. The smallest denomination a.k.a base unit is called Wei. The denominations along with their specific names can be seen in the table below:

Units Wei ValueWei
wei1 wei1
Kwei1e3 wei1,000
Mwei1e6 wei1,000,000
Gwei1e9 wei1,000,000,000
microEther1e12 wei1,000,000,000,000
milliEther1e15 wei1,000,000,000,000,000
Ether1e18 wei1,000,000,000,000,000,000

As discussed earlier, we know that EVM is responsible for running code that is deployed on its network. So what’s stopping someone from running an infinite loop on the EVM and completely overloading its memory? This is where the concept of Gas comes in.

Gas is used as a metric for paying for computational resources on the network.  Every contract on the network has a set maximum amount of gas that it can use for its computations. This is known as the “Gas Limit”  Other associated gas terms are as follows:

  • Gas Price: This is the cost of gas in terms of tokens like Ether and its other denominations. To stabilize the value of gas, the Gas Price is a floating value such that if the cost of tokens or currency fluctuates, the Gas Price changes to keep the same real value.
  • Gas Fee: This is effectively the amount of Gas needed to be paid to run a particular transaction or program (called a contract).

Hence, if someone tries to run a piece of code that runs forever, the contract will eventually exceed its gas limit and the entire transaction that invoked the contract will be rolled back to its previous state.


Now that we know about the currency, let’s take a look at the process that generates new currency.

Ethereum Tutorial: Mining

Ethereum, much like other public blockchain technologies ensures security through an incentive-based model. This is called a proof-of-work mechanism. The figure below shows how ethereum mining works:

Ethreum Mining - Ethereum Tutorial - Edureka

From a more technical perspective, the proof-of-work algorithm used is called Ethash, which is a hashing algorithm inspired by the Dagger-Hashimoto Algorithm.

Now that we’ve seen the working architecture of ethereum and discussed it’s essential elements, let’s see a real-world problem and the ethereum approach to solve the same.

Ethereum Tutorial: Decentralized Crowd Funding Use Case

Problem Statement: A good ‘idea’ isn’t everything in today’s world to start a successful business. A lot of funding and effort is needed to implement an idea. This is where organizations like “Kickstarter” come into the picture. They provide projects with the public exposure needed for donations towards their project to get it up an running, but the centralized architecture of such a motive has its downsides, mainly in the way the rewards are handled. Since the centralized authority makes all the decisions, systems are prone to rules like:

  • anyone who missed the deadline for the campaign cannot get in any more
  • any donor who changed their mind can’t get out

Approach:

We take a decentralized methodology to address the problem as explained in the picture below:

Crowdfunding - Ethereum Tutorial - Edureka

Solution:

Here is the solidity smart contract for the above problem statement.

pragma solidity ^0.4.16;

interface token {
function transfer(address receiver, uint amount);
}

contract Crowdsale {
address public beneficiary;
uint public fundingGoal;
uint public amountRaised;
uint public deadline;
uint public price;
token public tokenReward;
mapping(address => uint256) public balanceOf;
bool fundingGoalReached = false;
bool crowdsaleClosed = false;

event GoalReached(address recipient, uint totalAmountRaised);
event FundTransfer(address backer, uint amount, bool isContribution);

/**
* Constrctor function
*
* Setup the owner
*/
function Crowdsale(
address ifSuccessfulSendTo; // the address of the owner when funding is successful
uint fundingGoalInEthers; // target amount to raise
uint durationInMinutes; //given time
uint etherCostOfEachToken; //cost of equity in ether
address addressOfTokenUsedAsReward; //token address
) {
beneficiary = ifSuccessfulSendTo;
fundingGoal = fundingGoalInEthers * 1 ether;
deadline = now + durationInMinutes * 1 minutes;
price = etherCostOfEachToken * 1 ether;
tokenReward = token(addressOfTokenUsedAsReward);
}

/**
* Fallback function
*
* The function without name is the default function that is called whenever anyone sends funds to a contract
*/
function () payable {
require(!crowdsaleClosed);
uint amount = msg.value;
balanceOf[msg.sender] += amount;
amountRaised += amount;
tokenReward.transfer(msg.sender, amount / price);
FundTransfer(msg.sender, amount, true);
}

modifier afterDeadline() { if (now <= deadline) _; }

/**
* Check if goal was reached
*
* Checks if the goal or time limit has been reached and ends the campaign
*/
function checkGoalReached() afterDeadline {
if (amountRaised >= fundingGoal){
fundingGoalReached = true;
GoalReached(beneficiary, amountRaised);
}
crowdsaleClosed = true;
}

/**
* Withdraw the funds
*
* Checks to see if goal or time limit has been reached, and if so, and the funding goal was reached,
* sends the entire amount to the beneficiary. If goal was not reached, each contributor can withdraw
* the amount they contributed.
*/
function safeWithdrawal() afterDeadline {
if (!fundingGoalReached) {
uint amount = balanceOf[msg.sender];
balanceOf[msg.sender] = 0;
if (amount > 0) {
if (msg.sender.send(amount)) {
FundTransfer(msg.sender, amount, false);
} else {
balanceOf[msg.sender] = amount;
}
}
}

if (fundingGoalReached && beneficiary == msg.sender) {
if (beneficiary.send(amountRaised)) {
FundTransfer(beneficiary, amountRaised, false);
} else {
//If we fail to send the funds to beneficiary, unlock funders balance
fundingGoalReached = false;
}
}
}
}

In case you are interested in learning solidity, check out our blog on Solidity Programming, which is used for developing personalized smart-contracts.

Learn Blockchain from Experts

If you wish to learn Blockchain and build a career in Blockchain Technologies, then check out our Blockchain Certification Training which comes with instructor-led live training and real-life project experience. This training will help you understand Blockchain in depth and help you achieve mastery over the subject.

Got a question for us? Please mention it in the comments section and we will get back to you as soon as possible.

About Paul (8 Posts)

Research Analyst at edureka with a proficiency in Ethereum, Cybersecurity and Cryptography!


Share on

Browse Categories

Comments
0 Comments