Skip to content
This repository was archived by the owner on Sep 13, 2022. It is now read-only.

Commit c695343

Browse files
authored
from scratch (#409)
stratification add strategy invalid structure new_tx add new bahaviors add test case of NetTx add recv_proposal test cases prepare for new proposal test cases add new proposal test cases simplify codes concurrency process check liveness add send_qc test cases add new choke test cases add new height test case add pull_txs test cases link pull_txs to recv_proposal add README rebase master fix some bug cargo clippy fix send_pre_vote rebase master
1 parent b27ac99 commit c695343

23 files changed

+3482
-8
lines changed

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2018"
66
repository = "https://github.com/nervosnetwork/muta"
77

88
[dependencies]
9+
byzantine = { path = "./byzantine" }
910
common-apm = { path = "./common/apm" }
1011
common-config-parser = { path = "./common/config-parser" }
1112
common-crypto = { path = "./common/crypto" }
@@ -78,6 +79,8 @@ members = [
7879
"built-in-services/authorization",
7980

8081
"protocol",
82+
83+
"byzantine",
8184
]
8285

8386
[features]

byzantine/Cargo.toml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
[package]
2+
name = "byzantine"
3+
version = "0.0.1-alpha.0"
4+
authors = ["Muta Dev <[email protected]>"]
5+
edition = "2018"
6+
repository = "https://github.com/nervosnetwork/muta"
7+
8+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
9+
10+
[dependencies]
11+
common-apm = { path = "../common/apm" }
12+
common-config-parser = { path = "../common/config-parser" }
13+
common-crypto = { path = "../common/crypto" }
14+
common-logger = { path = "../common/logger" }
15+
common-merkle = { path = "../common/merkle" }
16+
protocol = { path = "../protocol", package = "muta-protocol" }
17+
core-api = { path = "../core/api" }
18+
core-storage = { path = "../core/storage" }
19+
core-mempool = { path = "../core/mempool" }
20+
core-network = { path = "../core/network" }
21+
core-consensus = { path = "../core/consensus" }
22+
overlord = { git = "https://github.com/nervosnetwork/overlord", rev = "b965dc4b" }
23+
24+
binding-macro = { path = "../binding-macro" }
25+
framework = { path = "../framework" }
26+
27+
actix-rt = "1.0"
28+
async-trait = "0.1"
29+
derive_more = "0.99"
30+
lazy_static = "1.4"
31+
futures = "0.3"
32+
parking_lot = "0.11"
33+
serde = "1.0"
34+
serde_derive = "1.0"
35+
serde_json = "1.0"
36+
log = "0.4"
37+
clap = "2.33"
38+
bytes = "0.5"
39+
hex = "0.4"
40+
rlp = "0.4"
41+
rand = "0.7"
42+
toml = "0.5"
43+
tokio = { version = "0.2", features = ["macros", "rt-core", "rt-util", "signal", "time"] }
44+
muta-apm = "0.1.0-alpha.7"
45+
futures-timer="3.0"

byzantine/README.md

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# 1. 概述
2+
3+
拜占庭测试是通过模拟恶意攻击行为对系统进行安全性和稳定性测试。
4+
对于区块链这样的分布式系统,节点是通过对外发送/不发送消息来对系统施加影响的。
5+
因此通过控制消息的发送时间、内容、数量和接收方,我们就可以模拟出任意的恶意行为。
6+
考虑到这些控制因素理论上可以组合出无穷多的恶意行为,而我们无法穷尽所有的可能,
7+
所以在具体实现时一方面是通过随机组合来覆盖尽可能多的可能,另一方面要方便随时增加新的测试用例。
8+
后者会在[架构设计](#3-架构设计)部分做详细的解释。
9+
10+
`muta`系统中,可以将节点发送的消息分为`主动消息``被动消息`
11+
主动消息是指节点可以在任意时刻主动发起的消息,如发送新交易,提案,投票等。
12+
被动消息是指节点需要被其他消息触发才能发起的消息,
13+
比如节点在收到 `pull_txs` 的时候才会发送 `push_txs`
14+
本项目的核心思路就是精心构造许多不同的恶意消息类型,
15+
再与其他因素随机组合,以模拟出能够覆盖大部分场景的恶意行为。
16+
17+
# 2. 测试操作
18+
19+
首先根据测试需要,修改配置文件 `byzantine/generatoes.toml`,增加或删除某些测试用例。
20+
```
21+
interval = 500 // 主动消息的触发间隔,单位 ms
22+
23+
[[list]]
24+
req_end = "/gossip/consensus/signed_proposal" // 主动消息需忽略该项,被动消息填触发消息的 end
25+
msg_type = { RecvProposal = "InvalidHeight" } // 消息内容的类型
26+
probability = 0.2 // 每次被触发时,生成该类型消息的概率,最大 1.0,表示 100%
27+
num_range = [1, 10] // 消息数量的取值范围,实际数量从该范围内随机生成
28+
priority = "Normal" // 消息发送的优先级,目前只有 Normal 和 High 两个选项
29+
```
30+
测试命令
31+
```
32+
// 启动三个正常节点
33+
muta$ CONFIG=examples/config-1.toml GENESIS=examples/genesis.toml cargo run --release --example muta-chain
34+
muta$ CONFIG=examples/config-2.toml GENESIS=examples/genesis.toml cargo run --release --example muta-chain
35+
muta$ CONFIG=examples/config-3.toml GENESIS=examples/genesis.toml cargo run --release --example muta-chain
36+
// 启动一个拜占庭节点
37+
muta$ CONFIG=examples/config-4.toml GENESIS=examples/genesis.toml cargo run --release --example byzantine-node
38+
```
39+
40+
# 3. 架构设计
41+
42+
核心需求 1. 方便随时增加新的测试用例;2.提供未来可通过外部交互控制和组织编排恶意攻击的能力。
43+
44+
为了实现以上需求,将恶意消息的生成过程抽象成以下三个过程
45+
46+
1. 配置文件 -> 行为生成器,由 `strategy` 模块实现
47+
```rust
48+
pub struct BehaviorGenerator {
49+
pub req_end: Option<String>,
50+
pub msg_type: MessageType,
51+
pub probability: f64,
52+
pub num_range: (u64, u64),
53+
pub priority: Priority,
54+
}
55+
```
56+
57+
2. 行为生成器 -> 行为,由 `commander` 模块实现
58+
```rust
59+
pub struct Behavior {
60+
pub msg_type: MessageType,
61+
pub msg_num: u64,
62+
pub request: Option<Request>, // 主动消息为 None, 被动消息为触发消息的内容
63+
pub send_to: Vec<Bytes>,
64+
pub priority: Priority,
65+
}
66+
```
67+
68+
3. 行为 -> 消息,由 `worker` 模块实现
69+
70+
由于通过配置生成的消息具有很强的随机性且无法从外部进行操纵,因此若未来有更加针对性的测试需求时,
71+
可以增加交互功能,通过交互命令指导 `commander` 模块触发特定的 `Behavior`
72+
73+
![image](./resource/structure.png)
74+
75+
# 4. 文件列表
76+
```
77+
byzantine
78+
├── src
79+
│   ├── behaviors.rs # 定义 Behavior 和各种消息类型
80+
│   ├── commander.rs # 根据 BehaviorGenerator 生成 Behavior
81+
│   ├── config.rs # 定义配置文件的数据结构
82+
│   ├── default_start.rs # 启动逻辑
83+
│   ├── invalid_types.rs # 生成各种恶意消息的方法实现
84+
│   ├── lib.rs
85+
│   ├── message.rs # 将被动消息的触发消息传递给 commander
86+
│   ├── strategy.rs # 根据配置文件生成 BehaviorGenerator
87+
│   ├── utils.rs # 工具方法
88+
└── └── worker.rs # 根据 Behavior 生成和发送消息
89+
```
90+

0 commit comments

Comments
 (0)