Skip to content

DSR-pheonix45/First-Blockchain-using-Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

First-Blockchain-using-Python

What is Blockchain: Blockchain is a decentralized digital ledger that stores data in blocks that are linked together in a chain. The blocks are securely linked using cryptographic hashes, which are unique identifiers. The chain is immutable, meaning it can't be changed without consensus from the network. This makes blockchain a reliable way to track transactions, payments, accounts, and other information

Why to Be a Blockchain Developer

Lucrative salary Blockchain developers are among the highest-paid professionals in the tech industry. According to Payscale, blockchain developers in the United States earn an average of $120,000 per year. High demand Blockchain developers are in high demand. Continuous learning Blockchain technology is constantly evolving, so developers need to stay updated with the latest developments. Contribute to technological innovation Blockchain developers can contribute to technological innovation by building decentralized applications and securing blockchain networks. Work with complex algorithms Blockchain developers are valued for their ability to work with complex algorithms, data structures, and security protocols

How to be a Blockchain Developer:

Blockchain developer roadmap : https://roadmap.sh/blockchain

How to Create a basic blockchain using python:

Block Class

The Block class represents a single block in the blockchain. It has the following attributes:

index: a unique integer identifier for the block previous_hash: the hash of the previous block in the chain timestamp: the timestamp when the block was created data: the data stored in the block (e.g. transactions) hash: the hash of the block, calculated using the calculate_hash method The init method initializes a new block with the given attributes.

Blockchain Class

The Blockchain class represents the entire blockchain. It has the following attributes and methods:

chain: a list of blocks, starting with the genesis block create_genesis_block: a method that creates the first block in the chain, the genesis block get_latest_block: a method that returns the most recent block in the chain add_block: a method that adds a new block to the chain calculate_hash: a method that calculates the hash of a block validate_chain: a method that checks the integrity of the blockchain create_genesis_block Method

The create_genesis_block method creates the first block in the chain, the genesis block. It sets the index to 0, previous_hash to "0", timestamp to the current time, data to "Genesis Block", and calculates the hash using the calculate_hash method.

get_latest_block Method

The get_latest_block method returns the most recent block in the chain.

add_block Method

The add_block method adds a new block to the chain. It sets the previous_hash of the new block to the hash of the latest block, calculates the hash of the new block using the calculate_hash method, and appends the new block to the chain.

calculate_hash Method

The calculate_hash method calculates the hash of a block using the hashlib library. It takes the index, previous_hash, timestamp, and data as input, concatenates them into a string, and returns the SHA-256 hash of the string.

validate_chain Method

The validate_chain method checks the integrity of the blockchain. It iterates through the blocks in the chain, starting from the second block (since the genesis block is assumed to be valid). For each block, it checks two things:

Whether the block's hash matches the calculated hash using the calculate_hash method. Whether the block's previous_hash matches the hash of the previous block. If either of these checks fails, the method returns False. If all checks pass, the method returns True.

Main Code

The main code creates a new blockchain, adds three blocks to it, and prints out the blockchain. It then validates the blockchain using the validate_chain method and prints the result.

Note that this is a very basic implementation of a blockchain, and there are many ways to improve it (e.g., by adding more advanced cryptography, implementing a consensus algorithm, etc.).

About

This is code to understand how blockchain is created locally.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages