1 of 55

Blockchain Workshop

By: Swapnil Kole

2 of 55

Blockchain is a distributed ledger based technology which uses consensus based decisions to achieve trust in a peer-2-peer network among unknown peers.

What is Blockchain from Tech side?

3 of 55

Original Idea

Digital Timestamp => No backdate & no tampering

Timestamp => https://www.epochconverter.com/

4 of 55

Hash

Number Systems => Binary(110), Octal(05), Decimal(9), Hexadecimal(0x4F)�Conversion

Hash => Unique hex no. to represent address

Function => i/p -> process -> o/p

Hashing Func => i/p = any type & o/p = a uniform length of hash

5 of 55

Contents of a Block

Timestamp

Nonce

6 of 55

Chain of Blocks

7 of 55

Checkpoint - 1

8 of 55

Underlying Technologies of Blockchain

Peer-2-peer network

Blockchain Protocol

Cryptography

9 of 55

Peer-2-peer network

  • Distributed ledger
  • Majority is required

10 of 55

Cryptography

Asymmetric Key Encryption : Generates keys & if public key encrypts then private key can decrypt vv.

Private key Public key

Piyush

Private key Public key

Roshni

11 of 55

Hashing : A hash function is any function that can be used to map data of arbitrary size to fixed-size values. The values returned by a hash function are called hash values.

Properties : One-way function, Avalanche effect, No dublicacy.

Best hashing functions : SHA-256 and Keccak

Blockchain Protocol

12 of 55

Checkpoint - 2

13 of 55

ADVANTAGES OF BLOCKCHAIN

  • Decentralized Control
  • Security
  • Transparency & Integrity
  • Immutable Storage

14 of 55

Decentralized Control

15 of 55

Security

  • Hashing : A function for giving out fixed length of string based on data.
  • Asymmetric Encryption : Generation of public and private keys.
  • No Hacking : Decentralized power

16 of 55

Transparency and Integrity

  • Transparency: In a blockchain especially a public one, everybody has a copy of the ledger with account numbers which is shown to everyone. But nobody knows who is behind the account. So the identity is kept secret but the ledger transparency is maintained.
  • Integrity

17 of 55

Immutable storage

Blockchain has immutable storage, once data goes into the block nobody can change it because changing the data will change the hash of the block. This also makes it unhackable.

18 of 55

STRUCTURE OF A BLOCK

Difficulty

19 of 55

MERKLE TREE

O(logn)

*Tree Data Structure

20 of 55

Checkpoint - 3

21 of 55

HOW BLOCKCHAIN WORKS

22 of 55

MINING PROCESS

  • Double Spending

  • Mining: Adding blocks to the blockchain. On successful addition of block they get incentives.
  • Miner: Has to solve difficult mathematical problems using his computational power. They take transactions from the mining pool and validate it by checking for double spending. They then add the transactions to a block.

23 of 55

  • Difficulty= Hashing Rate of Blockchain

Hashing rate for the target block coming through the miner’s calculations

The hash of each block starts with leading zeroes.

Nonce: Number only used once. Simple no. combined with the hash of the block to get the starting string with zeroes equal to the difficulty.

Consensus :A consensus mechanism is a fault-tolerant mechanism that is used in computer and blockchain systems to achieve the necessary agreement on a single data value or a single state of the network among distributed processes or multi-agent systems, such as with cryptocurrencies.

24 of 55

    • POW : The PoW consensus algorithm makes sure that miners are only able to validate a new block of transactions and add it to the blockchain if the distributed nodes of the network reach consensus and agree that the block hash provided by the miner is a valid proof of work.
    • Longest Chain Rule: The longest chain is what individual nodes accept as the valid version of the blockchain. The rule that nodes adopt the longest chain of blocks allows every node on the network to agree on what the blockchain looks like, and therefore agree on the same transaction history.

25 of 55

Checkpoint - 4

26 of 55

TYPES OF BLOCKCHAIN

  • Public Blockchain : Anyone can connect to the blockchain and can read and write data from Blockchain. Data on a public blockchain are secure as it is not possible to modify or alter data once they have been validated on the blockchain. Eg: Bitcoin & Ethereum.
  • Private blockchains work based on access controls which restrict the people who can participate in the network. In a private blockchain, only the entities participating in a transaction will have knowledge about it, whereas the others will not be able to access it. Privacy. Eg : Hyperledger and Medichain.
  • Consortium Blockchain : Advantages of both public and private blockchain. It combines privacy benefits of private blockchain and transparency and security of public blockchain. Eg : R3 Corda

27 of 55

BITCOIN

  • First decentralized digital currency
  • Founded by Satoshi Nakamoto
  • Distributed ledger
  • Public for all to access
  • Transactions are immutable, so impossible to hack.
  • Double spending not possible
  • Uses cryptography to control its creation and management.
  • Ledger is electronic every transaction is verified and validated before adding.
  • Proof-of-work is used for consensus mechanism.
  • Miners get reward for mining the blocks into the chain.

28 of 55

ETHEREUM

  • Ethereum is an open software platform based on Blockchain technology that enables developers to build and display decentralized applications.
  • Vitalik Buterin was the founder of Ethereum.
  • Ethereum coding language [solidity] helps write smart contracts which helps in Dapps.
  • Ethereum’s cryptocurrency is ether.
  • The Ethereum Virtual machine is a turing complete software that runs on Ethereum network. It enables anyone to run any program, regardless of the programming language given enough time and memory.
  • The applications are mostly written in solidity.

29 of 55

SMART CONTRACT

  • smart contract is a computer protocol intended to digitally facilitate, verify, or enforce the negotiation or performance of a contract. Smart contracts allow the performance of credible transactions without third parties. These transactions are trackable and irreversible.
  • Self-executing contract with the terms of the agreement between buyer and seller being directly written into lines of codes.
  • Execution is initiated by a message embedded in the transaction.
  • Ethereum enables transaction that may carry out more sophisticated operations.
  • Smart contract resembles class definition in an Object Oriented design.

30 of 55

ETHEREUM VIRTUAL MACHINES

  • An Ethereum Virtual machine provides a run anywhere obstruction layer for the contract code.
  • A smart contract written in HLL is translated in to EVM bytecode and then deployed on EVM.
  • Every node hosts the same smart contract codes on the EVM.
  • 256 bit machine.

31 of 55

DAPPLICATIONS

  • Decentralized Applications: It solves the problem that requires blockchain services and infrastructure for realizing its purposes.
  • End to end application on Blockchain.

32 of 55

Checkpoint - 5

33 of 55

Accounts

Externally Owned Accounts

Needs to join ethereum network

EOA are controlled by private keys

Interact with the blockchain using transactions.

Has a ether balance

Smart Contract Accounts

CA controlled by the code and can be activated only by an EOA

Represents a smart contract

Performs operations of arbitrary complexity, manipulate its own persistent storage i.e. can have its own permanent state, can call other contracts.

Has a ether balance

34 of 55

Gas

“Gas” is a special unit in Ethereum which is different from ether.

It measures the amount of “work” a set of action takes to perform.

Gas computational resources

Every action needs crypto fuel or gas

Transactions are rejected if fee specified and the gas points are not sufficient.

Leftover gas is transferred back to sender.

Gas limit for each block.

Example: gas limit/block = 15000 & (gas/trans = 300) => 50 trans/block

35 of 55

Ether Denomination Table

36 of 55

Smart Contract in Solidity

Smart contracts provides a layer of computational logic that can be executed on the blockchain, thus availing the features enabled by blockchain framework.

Solidity: Programming language

  • HLL
  • Target EVM
  • Remix support test environments

37 of 55

Checkpoint - 6

38 of 55

Practical Hands-on

Confidential

Customized for Lorem Ipsum LLC

Version 1.0

39 of 55

Metamask

remix.ethereum.org

A crypto wallet & gateway to blockchain apps.

40 of 55

Remix

Remix is a powerful, open source tool that helps you write Solidity contracts straight from browser. Written in Javascript, Remix supports both usage in the browser and locally.

Remix also supports testing, debugging and deploying of smart contracts and much more.

41 of 55

Solidity Language

Compiler stmts => pragma solidity ^0.5.0;

Contract Object => contract myContract{ }

42 of 55

Variables

All variables are initialized by zero, false, ”0x00000…” or ‘’.

Uint8 - uint256 … int8 - int256 - effective gas consumption.

  • Automatic wraparound
  • Range uint8 => 0-255 int8 => -128 - +127
  • uint256 public myuint = 2;

Bool used for either true or false

  • bool public mybool;

Strings are non-indexable

  • String manipulation are very costly
  • string public myStr;

43 of 55

Address - Sending funds and account balances. Holds 20 bytes.

  • address public myAdd;

44 of 55

Mapping - Associative arrays or key-value pairs.

  • Mapping (uint => address) public myMap;
  • 0=>0x05484; 1=>0x04efef; 2=>0x0abad; 3=>0x0fffe;
  • Initialized with value data types empty state.
  • Key should always be of elementary data types & values should be of any data type.
  • Accessed by name[key] = value

Struct - User defined data type

  • Packing a few primary data types
  • struct structName { DT1 id1; DT2 id2; …. }
  • structName visibility identifierName
  • Accessed by a dot operator => identifierName.id1 = 4;

45 of 55

Arrays- Many variables of same data type

  • DT[n] - fixed size array
  • DT[] -dynamic size array
  • DT[][7] - 7 dynamic sized arrays

Enums - User defined data type & limited set of values

  • Used for storing state
  • Always use dot to get values

46 of 55

Functions

Functions - A block of code that does work. Reusable.

2 Types of functions in solidity

  • Read Functions - call
  • Write Functions - trans

SubTypes of functions

  • View: Reading from state
  • Pure: Not reading or modifying state
  • Constructor: called at the time of deployment (init)
  • Receive: sending funds from account to smart contract

47 of 55

Visibility of functions

  • Public: called internally, externally and by inheritance
  • Private: same contract can call only not by trans
  • External: can be called by other contracts and externally
  • Internal: contract itself or from derived contracts not by trans

48 of 55

49 of 55

Important Properties

  • msg.sender: address of account calling the function
  • msg.value: amount of ether sent by the caller of the function
  • block.timestamp (time transaction completed): the timestamp currently Epoch Time
  • selfdestruct(address): The smart contract state won’t be there anymore. If you send funds in the contract you will lose it.
  • address(this) refers to the address of the instance of the contract where the call is being made.

50 of 55

Error Handling

Errors revert the state.

  • Revert and require can return an error string.
  • Require (if): returns remaining gas & mostly used for validating user inputs
  • Assert: consume all gas, used to validate invariants, terminates transaction
  • Revert (if - revert-else): reverts the transactions and returns gas

Modifiers

  • Automatically check a pre-condition
  • Change the behaviour of functions.

51 of 55

Importing of files

52 of 55

Inheritance

  • Using is keyboard
  • Inherited contracts are deployed as a single contract, not seperate ones.
  • Reusing of code.�Contract A {} parent�Contract B is A {} child

53 of 55

Industrial Topics

  • DeFi
  • FT
  • NFT
  • Token Design
  • Metaverse
  • IPFS

54 of 55

Understanding the market

55 of 55

Thank you.