Awesome Open Source
Awesome Open Source



A blockchain implementation in Python only for study.

The Blockchain-python implements simple blockchain and transactions. Currently, the implementation already has mining, transaction, communication between nodes, and file persistence of blocks and transactions.
The communication between nodes is via rpc based on http, rather than p2p network. Because the implementation of P2p is more complicated, it is too complicated to understand the framework of blockchain.
The verification based on cryptography has not yet been realized, and the verification of blocks between nodes and the verification of transactions have not yet been realized.


  1. Make sure Python 3.6+ is installed.
  2. Git Clone
$ git clone
$ cd blockchain


  • Create Account
$ python console account create
  • Run the miner
$ python console miner start 3008
  • Transaction transfer.
$ python console tx transfer from_address to_address amount
  • Transaction list.
$ python console tx list
  • Blockchain shows.
$ python console blockchain list

Node Network

Copy the code resource to a new directory.While the miner before was running then:

$ cd {another_blockchain_directory}
$ python console node add 3008 
$ python console node run 3009

When a new block mined , block and transactions will broadcast to other nodes.

All command

Use like this:

$ python console [module] [action] params...

Such as:

$ python console tx list
Module Action Params Desc
account create NONEED Create new account
account get NONEED Show all account
account current NONEED The miner reward account
miner start ip:port/port Such as 3008 or
node run ip:port/port Such as 3008 or
node list NONEED Show all node that will broadcast to
node add ip:port Add a node that will broadcast to
tx transfer from_address to_address amount Transfer coin from from_address to to_address
tx list NONEED Show all transactions


About block

The blockchain is a data structure that is linked sequentially from back to forward by blocks containing transaction information. SHA256 cryptographic hashing is performed on each block header to generate a hash value. A bitcoin block is as follows:


  #[...many more transactions omitted...]

A blockchain is a linked list structure of blocks. The essence of mining is a new block, based on existing information such as parent block hash, timestamp, transaction merkle hash, plus a nonce (number from 0)
A sha256 representation string is generated after the connection. If the preceding digits start with several zeroes, the number of zeros is the difficulty of mining, and half is dynamically adjusted based on the remaining number and the generation speed of the previous block, such as:


Successful mining, block generation

About Blockchain-python block

Blockchain-python simplified block structure, a blockchain-python block data is as follows:

	"index": 7,
	"timestamp": 1528972070,
	"tx": [
	"previous_block": "00003e17e04d9c9d2c2f5629de20bda58f59af36417a7e50eb77a74a028b026a",
	"nouce": 11063,
	"hash": "00006805c75d0db1685616d9ea5730f6203eda744a16fcc78ef1f3c244083ea4"

The calculation of block hash is roughly the same as that of Bitcoin. Our difficulty setting is relatively low, so the hash in front of this block has only 4 zeros.
This is for easier mining to understand the principle and generally can be produced in a few seconds. One block. In addition, Bitcoin's tx field represents the root node hash of the merkle tree that consists of the transaction hash.
For simplicity, we put it directly into the array of transaction hash.

About miner

The sha256 used by the mining algorithm, Bitcoin's algorithm is based on the block header +Nouce (a number) as a string. Simple blockchain simplifies the header information, but the mechanism and Bitcoin are constant. The blockchain is stored locally in the file in json format. The generation of a block is related to the transaction information, so the block information is also stored when the block is stored. There will be rewards for mining, and the reward will be recorded as the first transaction in the blockchain.

  • Rewards for mining are rewarded by the generated block itself
  • The miner also gets the amount entered for all transactions in the block - the amount of money that was exported
  • There will be some sorting rules for the transactions to be certified, sorting according to the block age, transaction fee, transaction amount, etc.

We simplified the implementation and only implemented rewards. The reward will be awarded to the current account. If the current account does not exist, please generate an account through the following command line:

$ python console account create

About network

The blockchain network is a P2P (Peer-to-Peer, end-to-end) network. We use Python's own RPC mechanism for simplification.

  • Different nodes can be connected by adding node operations
  • Unicom's nodes will automatically spread new transaction information
  • The new node will synchronize all the data of other node's blockchain while ensuring the maximum chain
  • Digging out new blocks will notify other nodes to synchronize

About transaction

Bitcoin uses the UTXO model and does not directly exist in the concept of “balance”. The balance needs to be obtained by traversing the entire transaction history. We also implement this mechanism. A transaction is a combination of some input and output. In our transaction, we accept multiple inputs and generate multiple outputs.

  • The calculation of the balance is made through the unconsumed verified transaction output - the output of the consumer transaction, which is commonly known as UTXO
  • Transactions not placed in new block will be broadcast to all nodes waiting to be verified
  • After waiting for the miner to dig into a new block, the trade will be saved as transaction information in the transaction database.

The correctness check of the transaction is under development.


Contributions are welcome! Please feel free to submit a Pull Request.

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
python (54,528
blockchain (841
bitcoin (574
rpc (286
simple (167
blockchain-technology (38
block (33