-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
65 lines (54 loc) · 1.57 KB
/
main.go
File metadata and controls
65 lines (54 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"bft_valid/pbft"
"crypto/sha256"
"encoding/hex"
"fmt"
"time"
)
func main() {
// 初始化网络
totalNodes := 10
byzantineNodes := 2
network := pbft.NewNetwork(totalNodes, byzantineNodes)
// 选择客户端节点(这里不考虑客户端为 Byzantine 的情况)
clientNode := 3
// 创建 REQUEST 消息
request := pbft.Message{
Type: pbft.REQUEST,
From: clientNode,
To: -1,
Timestamp: time.Now().UnixNano(),
Content: "Transfer 100 tokens from A to B.",
Digest: "",
}
// 计算摘要
h := sha256.New()
h.Write([]byte(request.Content))
request.Digest = hex.EncodeToString(h.Sum(nil))
// 客户端先处理自己的请求
fmt.Println("Client node sending request:", request.Content)
network.Nodes[clientNode].ReceiveMessage(request, *network)
// 广播 REQUEST 消息
fmt.Printf("Broadcasting REQUEST message from node %d to node %d\n", clientNode, request.To)
network.Nodes[clientNode].Broadcast(request, *network)
// 模拟共识等待过程
time.Sleep(3 * time.Second)
// 验证结果
fmt.Println("\n--- Verification Results ---")
consensus := make(map[string]int)
for i, node := range network.Nodes {
if node.State == pbft.NORMAL {
for digest, commits := range node.CommitMsg {
if len(commits) >= 2*pbft.F(totalNodes)+1 {
consensus[digest]++
fmt.Printf("Node %d reached consensus on digest: %s\n", i, digest)
}
}
}
}
fmt.Println("\n--- Final Consensus ---")
for digest, count := range consensus {
fmt.Printf("Digest %s: %d normal nodes reached consensus\n", digest, count)
}
}