当前位置: 首页 > 软件库 > Web3 > 区块链 >

blockchain-python

A blockchain implementation in Python
授权协议 Readme
开发语言 Python
所属分类 Web3、 区块链
软件类型 开源软件
地区 不详
投 递 者 温智明
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

中文说明

Blockchain-python

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.

Installation

  1. Make sure Python 3.6+ is installed.
  2. Git Clone
$ git clone https://github.com/Carlos-Zen/blockchain.git
$ cd blockchain

Usage

  • 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 127.0.0.1:3008
node run ip:port/port Such as 3008 or 127.0.0.1:3008
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

Introduce

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:

{
 "size":43560,
 "version":2,

 "previousblockhash":"00000000000000027e7ba6fe7bad39faf3b5a83daed765f05f7d1b71a1632249",
 "merkleroot":"5e049f4030e0ab2debb92378f53c0a6e09548aea083f3ab25e1d94ea1155e29d",
 "time":1388185038,
 "difficulty":1180923195.25802612,
 "nonce":4215469401,
 "tx":["257e7497fb8bc68421eb2c7b699dbab234831600e7352f0d9e6522c7cf3f6c77",
  #[...many more transactions omitted...]
  "05cfd38f6ae6aa83674cc99e4d75a1458c165b7ab84725eda41d018a09176634"
 ]
}

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:

00000000000000027e7ba6fe7bad39faf3b5a83daed765f05f7d1b71a1632249

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": [
        "b959b3d2099ca304c67087edbf05b79d1f2501b1f407df5e51a1a8c22bb3334d",
        "613e4af7266e01ea338d30681ef606bad26e4cdfa4ec7a6f431e22420c8291fd",
        "be7095a764cb241606a67c9064bc8dbc2da2370d49459bd492473ea5ce304cb3"
    ],
	"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.

Contributing

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

  • 区块结构: # 区块结构 class Block: ''' pre_hash:父区块哈希值 transaction:交易列表 timestamp:区块创建时间 hash:区块哈希值 nonce:随机值 ''' def __init__(self, transaction, pre_hash): # 将传入的父区块的哈

  • Download the full Jupyter/iPython notebook from Github here Build Your Own Blockchain – The Basics¶ This tutorial will walk you through the basics of how to build a blockchain from scratch. Focusing o

  • 步骤3:与我们的区块链交互 python blockchain.py #让我们通过创建一个新交易 POST 请求 #http://localhost:5000/transactions/new 包含我们的交易结构的主体: $ curl -X POST -H "Content-Type: application/json" -d '{ "sender": "d4ee26eee15148ee92c

  • # 导入hashlib包 import hashlib # 定义区块类 class Block: # 构造函数,包含4大属性 def __init__(self,index,timestamp,data,previous_hash): # 前一个区块的哈希值 self.previous_hash = previous_hash # 本

  • 欢迎关注专栏《区块链Blockchain》 《Python开发区块链之01环境搭建》 《Python开发区块链之02如何生成private key 与 public key》 《Python开发区块链之03如何生成bitcoin address》 Python开发区块链之03如何生成bitcoin address 前两节的demo中我们生成了private key和 public key,接下来我

 相关资料
  • Here we describe the data structures in the Tendermint blockchain and the rules for validating them. Data Structures The Tendermint blockchains consists of a short list of basic data types: Block Head

  • A simple implementation of blockchain This project aims to create a simple implementation of blockchain concept and demostrate it in a user friendly way. Design Concept This project consists of two ma

  • The Blockchain Reactor's high level responsibility is to enable peers who are far behind the current state of the consensus to quickly catch up by downloading many blocks in parallel, verifying their

  • Truffle 是以太坊的开发环境,测试框架和资产管道,可以让以太坊开发人员开发更轻松。 特性 内置智能合约编译、链接、部署和二进制管理 使用Mocha和Chai进行自动合同测试 可配置的构建管道,支持自定义构建过程 可编写脚本的部署和迁移框架 用于部署到许多公共和专用网络的网络管理 交互式控制台,用于直接合同通 在开发过程中即时重建资产 在Truffle环境中执行脚本的外部脚本运行器 安装 $

  • Stacks 2.0 Reference implementation of the Stacks blockchain in Rust. Stacks 2.0 is a layer-1 blockchain that connects to Bitcoin for security and enables decentralized apps and predictable smart cont

  • Awesome Blockchain Curated list of resources for the development and applications of block chain. The blockchain is an incorruptible digital ledger of economic transactions that can be programmed to r