Showing posts with label Blockchain. Show all posts
Showing posts with label Blockchain. Show all posts

Tuesday, 6 November 2018

Trust Trail in blockchain

  1. Validate Transaction -->
  2. Verify Gas and Resources -->
  3. Select set of transactions to create a block -->
  4. Execute transaction to get a new state -->
  5. Form a new block -->
  6. Work toward consensus -->
  7. New block added to chain and confirmed (finalize the block by the winner)


Consensus Protocol
At step 5 i.e. forming a new block, A PoW is computed. PoW stands for Proof of Work, and it uses hashing as well.
PoW is a puzzle to be solved by the miners.
PoW is used as consensus protocol by Bitcoin and Ethereum Byzantium Metropolis Blockchain.




Exceptions in the blockchain:
  1. More than one miner solves the consensus puzzle - Fork
They are asked to form further blocks. It is a very low probability that both will form another block at the same time. So this breaks the tie and the chain of new blocks are added to the original chain.
Ethereum gives runner-up incentives to the miners who built the block. Runners-up are called Ommers.

  1. More than one transaction references as input, the same digital asset - Double Spending.
In Bitcoin, the transaction referring to the asset first is considered valid transaction and rest of the transactions referencing the asset are rejected.
In Ethereum, a combination of account number and global nonce is used to address the double spending issue.
Every time a transaction is initiated by the account, a global nonce is included in the transaction. After that nonce is incremented. Timestamp and nonce in the transaction should be unique and verified to prevent the double spending problem.

Fork

Well managed forks help build credibility in the blockchain by providing approaches to manage unexpected faults and planned improvements.

Soft fork is like the release of software patches to existing software
Hard fork is like the release of a new version of the operating systems

Recent hard fork in Ethereum which was a change from Homestead to Metropolis Byzantium version. Oct 17, 2017.
PoW protocol still stays, except that, every 100 blocks, PoS consensus protocol is applied for evaluating the latter.
And Miner incentive was reduced from 5 Ethers to 3 Ethers for block creation.

After a hard fork, the emerging two chains are incompatible with each other.

Unplanned hard fork in Ethereum protocol -  Ethereum Core and Ethereum Classic split to address critical software issue in DAO application that resulted in a 150 Million dollar heist.



Hashing in blockchain

SHA-3, SHA-256, and Keccak-256 are important hashing algorithms in BlockChains

Tree hashing is used wherever a variable number of arguments is involved in the block.

  
In Ethereum, hashing is used to generate:
  1. Account addresses
  2. Digital Signatures
  3. Transaction Hash
  4. State Hash
  5. Receipt Hash
  6. Block Header Hash


To manage the integrity of the blockchain transaction
  1. Secure and Unique Account Addresses
  2. Authorization of the transaction by the sender through digital signing
  3. Verifying that the content of the transaction is not modified

Generating address of the accounts
Address of the blockchain accounts are generated through use of public key-private key pair
Here are the steps involved:
  1. 256 bit random number is generated and designated as private key
    • It is usually kept secure using a password or passphrase
  2. ECC algorithm is applied to the private key to generate a unique public key. This completes the private-public key pair. (ECC: Elliptic-curve cryptography)
  3. Hashing technique is applied to public key to obtain an account address (20 bytes)

Transaction for transferring assets has to be:
  1. Authorized
  2. Non - repudiable
  3. Unmodifiable

Verification of a blockchain transaction
  1. Find the hash of the data fields of the transaction
  2. Encrypt the hash using the private key of the participant originating the transaction, thus, digitally signing the transaction to authorize and making the transaction as non-repudiable.
  3. This hash is added to the transaction and other participants can verify it by decrypting it using the public key of the sender of the transaction and recomputing the hash of the transaction.
  4. Then they compare the re-computed hash and the hash received with the signature.
  5. If there is a match, accept the transaction, otherwise, reject it.
  6. Note that this is not the only verification required for the transaction acceptance. Transaction timestamp, nonce, account balances and sufficiency of fees are also verified.

What is non-repudiable?
What is nonce?

Securing the blockchain
Main components of the Ethereum Block are
  1. Block header
  2. Transactions including the transaction hash
  3. Transaction Root
  4. State variables: State hash and State root


In Ethereum Block hash is a block of all the elements in the block header including the transaction root and state root hashes. It is computed by applying a variant of the SHA-3 algorithm called Keccuk on all the items of the block header.

A typical Bitcoin block has about 2000 transactions
A typical Ethereum block has about 100 transactions



What is the advantage of Tree hashing over flat hashing?
If any transaction has to be verified, only one pathway to the tree has to be checked. You don't have to go through the entire set of transactions.

So, smart contract execution in Ethereum results in state transitions. Every state change requires state root (hash) re-computation. Instead of re-computing the hash for all set of states, Only the affected path in the Merkle Tree needs to be recomputed and not the entire tree path.


Block hash computation
Block hash in Ethereum is computed by first computing the State root hash,  Transaction root hash and the Receipt root hash shown at the bottom row of the block header diagram.

These roots and all the other items in the header are hashed together along with the variable nonce, using Keccak-256 to solve the Pow (Proof of Work) puzzle.
Block Hash solves two important purposes:
  1. Verification of the integrity of the block and the transaction
  2. Formation of the "Chain link" by embedding the previous block hash in the current block header.

If any participant node tampers with the block, its hash value changes resulting in the mismatch of the hash values and rendering the local chain of the blocks into an invalid state. Any future blocks proposed by the node will be rejected by other miners due to hash mismatch. This enforces the immutability of the chain.

Monday, 1 October 2018

Workshop 1: Creating a Hyperledger Fabric network

Hyperledger Fabric Network

Creating a first hyperledger fabric network through Amazon AWS
  1. create an AWS account
  2. I created an AWS student account through www.awseducate.com
    Through your workbench, go to AWS console and create an EC2 instance
    Go to Services-> ec2-> launchinstance-> Ubuntu Server 16.04 LTS (HVM), SSD Volume Type-> m4large
    Add tag->basicnetwork, Create a new key-pair, Download key pair and click launch instance
  3. Connecting to the instance
  4. Once the instance has started, you can connect it through Putty
    Before connecting, convert the .pem file (key pair) into .ppk file using PuttyGen. This step is required because Putty requires a .ppk file and cannot connect using .pem file
    Now add .ppk file to Putty, and connect to the IP of the running AWS instance
  5. Creating first network - installing prerequisites
    1. Install cURL
    2. Type on the terminal cURL command to check if its installed or run cURL --version. If it is not installed you can install it through below command
      sudo apt-get install curl
    3. Install Docker and Docker Compose
    4. and
    5. Install GO Programming Language
    6. Don't forget to set the path. Run command export PATH=$PATH:$GOPATH/bin
    7. Install Node.js Runtime and NPM
    8. Install Python
    9. Retrieve the Python version 2.7, which is goof for running Fabric Node.js SDK, by using command sudo apt-get install python
      You can also check python version by command "python --version"
      You are essentially done for installing the prerequisites. You can refer to detailed installation article herehttps://hyperledger-fabric.readthedocs.io/en/latest/prereqs.html
  6. Installing Sample Binaries and Docker Images
  7. Once you are ready, and in the directory into which you will install the Fabric Samples and binaries, go ahead and execute the following command: curl -sSL http://bit.ly/2ysbOFE | bash -s 1.2.0
    then, curl -sSL http://bit.ly/2ysbOFE | bash -s curl -sSL http://bit.ly/2ysbOFE | bash -s 1.2.0 1.2.0 0.4.10
    I faced an issue of Docker permission denied while trying to connect to the Docker daemon socket
    I solved the issue with command sudo usermod -a -G docker $USER . Don't forget to completely log out of your account and log back in (if in doubt, reboot!):
  8. Running first network
  9. cd fabric-samples/first-network
    Ready to give it a go. Run the command: ./byfn.sh generate
    This command generates all the certificates. And after this, bring up the network
    ./byfn.sh up
    If you’d like to run through this tutorial with node chaincode, pass the following command instead:
    we use the -l flag to specify the chaincode language forgoing the -l flag will default to Golang
    ./byfn.sh up -l node
    Bring down the network with the command ./byfn.sh down

Hyperledger

What is Hyperledger?

https://blockgeeks.com/guides/what-is-hyperledger/
A lot of explanation on the above link.

Important to know -

  1. Hyperledger was created by Linux Corporation in late 2015. 
  2. It is not Bitcoin or some other crypto-currency. It is not Ethereum or some other code execution engine
  3. It aims to create an blockchain based environment where companies and developers can collaborate to create frameworks/ projects.
  4. More than 100 companies are part of it.
  5. What projects have been already created using it:




  • Hyperledger Sawtooth: This is a modular blockchain suite developed by Intel, which uses a new consensus algorithm called Proof of Elapsed Time (PoeT).




  • Hyperledger Iroha: Iroha is a project of a couple of Japanese companies to create an easy to incorporate the framework for a blockchain.




  • Hyperledger Fabric: This project is lead by IBM. Fabric is a plug and plays implementation of blockchain technology designed as a foundation to develop high-scaling blockchain applications with a flexible degree of permissions.

  • Simple video to understand Hyperledger fabric


    Hyperledger glossary
    https://hyperledger-fabric.readthedocs.io/en/release-1.2/glossary.html



    Sunday, 9 September 2018

    Blockchain basics

    → Blockchain is a decentralized, incorruptible digital ledger that can record transactions (like, but not limited to, financial transactions) directly among peers without having involvement of a third party or centralized system. 

    As an analogy, it can be thought of as a distributed database that maintains a shared list of records. These records are called blocks. Each encrypted block holds the history of blocks that came before it, with timestamped transaction data which chain the blocks together and hence termed as blockchain.

    The blockchain is also called public ledger because it is openly available for everyone to read.

    Its main characteristics are:

    1. Decentralized peer to peer network
    2. Establishing trust among unknown peers
    3. Recording the transaction in immutable, distributed ledger
    How is trust achieved?
    1. Validate, Verify and confirm transactions
    2. Record the transactions in a distributed ledger of blocks
    3. Create a tamper-proof chain of blocks
    4. Implement a consensus protocol for agreement on the block to be added in the chain


    Distributed Ledger

    In its simplest form, a distributed ledger is a database held and updated independently by each participant (or node) in a large network. The distribution is unique: records are not communicated to various nodes by a central authority but are instead independently constructed and held by every node. That is, every single node on the network processes every transaction, coming to its own conclusions and then voting on those conclusions to make certain the majority agree with the conclusions.
    Once there is this consensus, the distributed ledger has been updated, and all nodes maintain their own identical copy of the ledger. This architecture allows for a new dexterity as a system of record that goes beyond being a simple database.


    From <https://www.coindesk.com/information/what-is-a-distributed-ledger/