ChainXim-Team/ChainXim

GitHub: ChainXim-Team/ChainXim

ChainXim 是一款可在单机上模拟大规模区块链网络并验证共识协议安全性与性能的区块链模拟器。

Stars: 188 | Forks: 0

# ChainXim 用户指南 [中文](README.zh.md) | English ## 简介 ChainXim 是由 XinLab 开发的一款区块链模拟器,用于模拟和验证区块链系统。虽然去中心化是区块链系统的核心特性,但在模拟和测试大规模区块链网络时,它往往会导致巨大的成本和工程挑战。ChainXim 为低成本、大规模的区块链部署和测试提供了一种新颖的解决方案,同时也作为区块链应用的开发基础。 ChainXim 可以在单台机器上部署大规模区块链网络,在虚拟环境中模拟节点的操作和交互。当主链达到预定的区块高度时,环境将终止模拟循环并生成性能报告。除了性能评估之外,ChainXim 还可以模拟攻击者的行为,以测试区块链对潜在攻击向量的鲁棒性,并验证共识协议的安全属性。ChainXim 中的多种网络模型模拟了区块链系统中消息的传播特性,并且 ChainXim 同时支持基于真实拓扑的网络和源自理论区块链研究的抽象网络模型。这一能力将理论研究与模拟结果联系起来,促进了新区块链系统的开发和优化。 ChainXim 基于 Bitcoin Backbone Protocol 构建,具有高度可扩展的架构。其共识层在理论上通过模块化实现支持多种共识协议,这些模块可以轻松集成到模拟器中。通过配置五种主要网络模型可以实现所需的网络层行为,同时网络层也允许开发者使用其消息传递框架和网络接口创建新模型,从而扩展模拟场景。攻击层利用 miner 模块中的统一接口,结合共识层和网络层的功能来执行各种网络攻击。模拟后的评估模块会计算关键性能指标,包括吞吐量、分叉率和链质量,每一项都为区块链系统的安全性和性能提供了有价值的见解。 ![简介](https://raw.githubusercontent.com/ChainXim-Team/ChainXim/master/doc/intro.svg) ## 基于 ChainXim 的项目 - BagChain:利用基于 Bagging 的分布式学习的双功能区块链。[源代码](https://github.com/czxdev/BagChain) BagChain 通过使用机器学习模型训练来替代工作量证明中计算成本高昂的哈希操作,从而将区块链与分布式机器学习相结合。它利用各个 miner 的私有数据样本和有限的计算资源来训练基础模型,并进一步将其聚合为强大的集成模型。系统设计了一种三层区块链结构以及相应的生成和验证机制,以在无许可和开放的环境中实现非协作 miner 之间的分布式机器学习。 ## 快速入门 ### 下载 你可以使用 git 克隆 Github 仓库。 Git 克隆命令:`git clone https://github.com/ChainXim-Team/ChainXim.git` 或者你可以从 ChainXim 代码仓库下载 master 分支:[master.zip](https://github.com/ChainXim-Team/ChainXim/archive/refs/heads/master.zip)
文件结构 解压 zip 文件或克隆仓库后的文件结构如下: ``` chain-xim ├── attack │ ├── _adversary.py │ ├── adversary.py │ ├── attack_type │ │ ├── _atomization_behavior.py │ │ ├── atomization_behavior.py │ │ ├── attack_type.py │ │ ├── double_spending.py │ │ ├── eclipse_alpha.py │ │ ├── eclipsed_double_spending.py │ │ ├── eclipsed_double_spending_research_backup.py │ │ ├── eclipse_network_cutting.py │ │ ├── honest_mining.py │ │ ├── __init__.py │ │ └── selfish_mining.py │ └── __init__.py ├── conf │ └── topologies │ ├── circular16_coo.csv │ ├── default_adj.csv │ └── ... │ └── ... ├── consensus │ ├── consensus_abc.py │ ├── __init__.py │ ├── pow.py │ ├── solidpow.py │ └── virtualpow.py │ ├── ... ├── data │ ├── block.py │ ├── chain.py │ ├── __init__.py │ └── message.py ├── developer-guide.en.md ├── developer-guide.zh.md ├── environment.py ├── errors.py ├── external.py ├── functions.py ├── global_var.py ├── main.py ├── miner │ ├── _consts.py │ ├── __init__.py │ ├── miner.py │ └── network_interface │ ├── __init__.py │ ├── nic_abc.py │ ├── nic_without_tp.py │ └── nic_with_tp.py ├── network │ ├── adhoc.py │ ├── deterprop.py │ ├── __init__.py │ ├── network_abc.py │ ├── stochprop.py │ ├── synchronous.py │ ├── topology.py ├── README.md ├── README.zh.md ├── requirements.txt ├── simulation-example.en.md ├── simulation-example.zh.md ├── system_config.ini ``` | 文件或目录 | 描述 | | -------------------------- | -------------------------------------------------- | | main\.py | 模拟器的主程序 | | system_config\.ini | 配置文件 | | README.md | 用户文档 | | environment\.py | Environment 类及相关函数 | | miner/ | 包含 miner 类和网络接口的目录 | | data/ | 包含 Message、BlockHead、Block、Chain 类及相关函数的目录 | | consensus/ | 包含抽象共识类、PoW 类及相关函数的目录 | | network/ | 包含各种网络类的目录 | | attack/ | 攻击者类及相关函数 | | functions\.py | 包括 SHA256 哈希计算在内的函数 | | external\.py | 用于区块链统计的外部函数 | | errors\.py | 用于在模拟器中抛出错误的错误类 | | global_var\.py | 全局变量 |
### 环境设置 1. 安装 Anaconda。[Anaconda 下载链接](https://www.anaconda.com/download) 2. 从“开始”菜单打开 Anaconda Prompt。 3. 创建一个 conda 环境并激活它,选择 Python 3.10 版本。 ``` conda create -n chainxim python=3.10 python-graphviz activate chainxim ``` 4. 通过 pip 安装所需的包。 ``` cd pip install -r requirements.txt ``` ### 简易启动 1. 从“开始”菜单打开 Anaconda Prompt,并在运行前激活 conda 环境。 ``` activate chainxim ``` 2. 在 ChainXim 根目录下运行主程序。 ``` cd python main.py ``` ## 初学者示例 ChainXim 是一个离散时间模拟器,它通过“轮次”迭代地模拟 miner 和攻击者的行为。在这个例子中,将创建一个由 10 个节点组成的随机生成的网络。该网络包含两个攻击者,他们试图通过自私挖矿策略将尽可能多的区块插入主链,从而使他们的挖矿奖励最大化。通过将以下参数传递给主程序,模拟将开始并运行 3000 轮。 ``` python main.py --total_round 3000 --miner_num 10 --average_block_time 400 --network_type network.TopologyNetwork --attack_type SelfishMining --blocksize 8 --no_compact_outputfile --show_label ``` 模拟开始后,它将输出模拟参数并在模拟期间显示进度条。 ``` Network Attributes miner_num: 10 Generate Approach: adj Generate Edge Probability: None Diameter: 4 Average Shortest Path Length: 2.067 Degree Histogram: [0, 2, 2, 4, 2] Average Degree: 2.6 Average Cluster Coefficient: 0.067 Degree Assortativity: -0.2 Parameters: Miner Number: 10 Consensus Protocol: PoW Network Type: TopologyNetwork Network Param: {'show_label': True, 'save_routing_graph': False, 'save_routing_history': True, 'dynamic': False, 'enable_resume_transfer': True, 'ave_degree': 5.0, 'bandwidth_honest': 5.0, 'bandwidth_adv': 5.0, 'outage_prob': 0.0, 'avg_tp_change_interval': 10.0, 'edge_add_prob': 0.1, 'edge_remove_prob': 0.05, 'max_allowed_partitions': 1.0, 'init_mode': 'adj', 'stat_prop_times': [0.03, 0.05, 0.08, 0.1, 0.2, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.93, 0.95, 0.98, 1.0], 'rand_mode': 'binomial'} Consensus Param: {'target': '0010624dd2f1a9fc000000000000000000000000000000000000000000000000', 'q_ave': 1.0, 'q_distr': 'equal'} Adversary Miners: [1, 2, 3] Attack Execute Type: SelfishMining (Adversary's q: 3.0) Block Size: 8.0 ``` 模拟结束后,将显示模拟结果: ``` Chain Growth Property: 9 blocks are generated in 3000 rounds, in which 0 are stale blocks. Average chain growth in honest miners' chain: 9.0 Number of Forks: 0 Fork rate: 0.0 Stale rate: 0.0 Average block time (main chain): 333.33333333 rounds/block Average block time (total): 333.33333333 rounds/block Block throughput (main chain): 0.003 blocks/round Throughput in MB (main chain): 0.024 MB/round Block throughput (total): 0.003 blocks/round Throughput in MB (total): 0.024 MB/round Chain_Quality Property: {'Honest Block': 10, 'Adversary Block': 0} Ratio of blocks contributed by malicious players: 0.0 The simulation data of SelfishMining is as follows : {'The proportion of adversary block in the main chain': 'See [Ratio of blocks contributed by malicious players]', 'Theory proportion in LockstepNetwork': '0.2731'} Double spending success times: 0 Block propagation times: {0.03: 0, 0.05: 0, 0.08: 0, 0.1: 0, 0.2: 1.111, 0.4: 2.0, 0.5: 2.556, 0.6: 3.727, 0.7: 3.667, 0.8: 4.308, 0.9: 4.769, 0.93: 0, 0.95: 0, 0.98: 0, 1.0: 5.0} Count of INV interactions: 267 ``` 有关输出中指标的详细说明,请参阅[模拟器输出](developer-guide.en.md#Simulator-Output)。此外,模拟过程中产生的各种数据将保存在 Results 文件夹中,包括每个 miner 的区块链记录、区块链可视化图表、区块链攻击者日志、路由历史、网络拓扑、区块传播图等。以下是一些输出图表的示例。 区块链可视化图表 ([blockchain_visualization/Blockchain Structure.gv.svg](doc/Blockchain-Structure.svg))

Blockchain-Structure

Red blocks in the figure are generated by attackers, and blue blocks are generated by honest miners. 网络拓扑图 ([Network Results/network topology.svg](doc/network-topology.svg)) ![网络拓扑](https://raw.githubusercontent.com/ChainXim-Team/ChainXim/master/doc/network-topology.svg) 图中标注了相邻节点之间的传输带宽,单位为 MB/round。 ## 模拟示例 有关更多详细信息,请参阅[精选示例](simulation-example.en.md) ## 开发者文档 有关更多详细信息,请参阅[开发者指南](developer-guide.en.md) 凌昕彤 Xintong Ling * 指导教师
标签:仿真器, 共识协议, 区块链, 安全测试, 攻击性安全, 流量捕获, 网络拓扑, 逆向工具