Code Monkey home page Code Monkey logo

wolfcoin's Introduction

wolfcoin is a Python-based mini-blockchain. The purpose of creating this project is to sharpen my understanding of blockchain's basic. The scope of the project covers hashing generating using the SHA-256 algorithm, and stacking blocks based on a previous hash. The output displays in local terminal setting, no transaction is made using this app.

Features

๐Ÿ”ฅ Quick Hashing Display

โšก Using SHA-256 algorithm to create instant hash

๐Ÿ’ฅ Compacted within 50 Lines of code

๐Ÿฑ Easy to read/follow code structure.

External Libraries

Third Party libraries are used in this project

Package Description
hashlib SHA-256 Algorithm Core
datetime Access real-time date

Getting Started

The user needs to have Terminal/Bash installed, Python version in this project is 2.7.

Run

๐Ÿ Python 2.7

$ python wolfcoin.py

Code Walkthrough

In class Block: Creating a Block data structure that takes a few args including index, timestamp, a random data that could be anything and most importantly, a previous hash that the newly created block can build upon.

class Block:
    def __init__(self, index, timestamp, data, previous_hash):
        self.index = index
        self.timestamp = timestamp
        self.data = data
        self.previous_hash = previous_hash
        self.hash = self.hash_block()

    def hash_block(self):
        """
        Function Explain.
        hash.update(arg) :Update the hash object with the string arg.
        hash.hexdigest(arg) : Return the digest of the strings passed to the
        update() method so far containing only hexadecimal digits.
        """
        sha = hasher.sha256()
        sha.update(str(self.index) +
                    str(self.timestamp) +
                    str(self.data) +
                    str(self.previous_hash))
        return sha.hexdigest()

In create_genesis_block(): When a blockchain is initiated, it needs to at least have one block, this function creates the very first block inside of the blockchain that has an arbitrary previous_hash since there is no previous_hash.

def create_genesis_block():
    """
    Manually create the first block with
    index zero and arbitary previous hash
    """
    return Block(0, date.datetime.now(), "Genesis Block", "0")

In def new_block(last_block): This function creates the rules on how a new block is created. In a nutshell, it gets previous_hash from the last element in the array, and update its own previous_hash to itself, so later when next block is created, it can read the current hash value.

def new_block(last_block):
    """
    Create new block based on previous block
    """
    this_index = last_block.index + 1
    this_timestamp = date.datetime.now()
    this_data = "Yo! I am block " + str(this_index)
    this_hash = last_block.hash
    return Block(this_index, this_timestamp, this_data, this_hash)

In main: After creating necessary function, simply create a blockchain, add a genesis block, then loop over 20 times to create 20 blocks. Eventually, print them out, and we are done here.

# Create the genesis within Blockchain
blockchain = [create_genesis_block()]
previous_block = blockchain[0]

# Upper Bound of blocks
blocks_num = 20

# Loop and create 20 subsequent blocks
for i in xrange(0, blocks_num):
    next_block = new_block(previous_block)
    blockchain.append(next_block)
    previous_block = next_block
    print "Block #{} has been added to the blockchain!".format(next_block.index)
    print "Hash: {}\n".format(next_block.hash)

Demo

The blockchain application prints out each hash along with a relative index.

Demo

License

๐ŸŒฑ MIT ๐ŸŒฑ


home yuzhoujr.com ย ยทย  github @yuzhoujr ย ยทย  linkedin @yuzhoujr

wolfcoin's People

Contributors

tech-cow avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.