forked from wang-sicheng/ssbcV2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
144 lines (127 loc) · 3.99 KB
/
main.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"github.com/cloudflare/cfssl/log"
"github.com/ssbcV2/account"
"github.com/ssbcV2/chain"
"github.com/ssbcV2/client"
"github.com/ssbcV2/common"
"github.com/ssbcV2/event"
"github.com/ssbcV2/global"
"github.com/ssbcV2/levelDB"
"github.com/ssbcV2/merkle"
"github.com/ssbcV2/meta"
"github.com/ssbcV2/network"
"github.com/ssbcV2/pbft"
"github.com/ssbcV2/util"
"os"
)
func main() {
// 为四个节点生成公私钥
util.GenRsaKeys()
if len(os.Args) != 2 {
log.Error("输入的参数有误!")
return
}
nodeID := os.Args[1]
// 删除 levelDB/path 和 contract/contract 目录
if nodeID == "clear" {
clear()
return
}
// 不存在该节点编号
if !util.Contains(common.Ssbc1Nodes, nodeID) &&
!util.Contains(common.Ssbc2Nodes, nodeID) {
log.Info("无此节点编号!")
return
}
merkle.AccountStatePath = "./levelDB/db/path/account/" + nodeID // 账户状态和事件状态分开存储
merkle.EventStatePath = "./levelDB/db/path/event/" + nodeID
// 数据库连接
levelDB.InitDB(nodeID)
// 从levelDB读取账户信息(必须在数据库建立连接后,所以不能在init()完成)
account.GetFromDisk()
// 从levelDB初始化事件和订阅数据
event.InitEventData()
rootDir, _ := os.Getwd()
global.RootDir = rootDir // 记录项目根目录
global.NodeID = nodeID
if util.Contains(common.Ssbc1Nodes, nodeID) {
global.ChainID = "ssbc1"
global.Master = "N0"
global.Client = "client1"
global.NodeTable = common.NodeTable1
global.ClientToNodeAddr = common.Client1ToNodeAddr
global.ClientToUserAddr = common.Client1ToUserAddr
if nodeID == "client1" {
go client.ListenRequest() // 启动客户端程序
p := pbft.NewPBFT(nodeID, common.Client1ToNodeAddr)
go p.DeploySysContract()
go p.TcpListen()
} else if addr, ok := common.NodeTable1[nodeID]; ok {
p := pbft.NewPBFT(nodeID, addr)
go p.DeploySysContract() // 部署系统智能合约
go p.TcpListen() //启动节点
}
}
if util.Contains(common.Ssbc2Nodes, nodeID) {
global.ChainID = "ssbc2"
global.Master = "N4"
global.Client = "client2"
global.NodeTable = common.NodeTable2
global.ClientToNodeAddr = common.Client2ToNodeAddr
global.ClientToUserAddr = common.Client2ToUserAddr
if nodeID == "client2" {
go client.ListenRequest() // 启动客户端程序
p := pbft.NewPBFT(nodeID, common.Client2ToNodeAddr)
go p.DeploySysContract()
go p.TcpListen()
} else if addr, ok := common.NodeTable2[nodeID]; ok {
p := pbft.NewPBFT(nodeID, addr)
go p.DeploySysContract()
go p.TcpListen() //启动节点
}
}
// 初始化
initBlockChain(nodeID)
select {}
}
//初始化
func initBlockChain(ID string) {
chain.BlockChain = make([]meta.Block, 0)
var accounts []meta.JFTreeData
var events []meta.JFTreeData
bc := chain.GetCurrentBlockChain()
// 初始化创世区块前,所有节点先初始化0版本的state
if len(bc) == 0 {
merkle.InitAccount.Address = "init account address"
merkle.InitEvent.EventID = "init event id"
accounts = append(accounts, merkle.InitAccount)
events = append(events, merkle.InitEvent)
stateRootHash, _ := merkle.UpdateStateTree(accounts, 0, merkle.AccountStatePath)
eventRootHash, _ := merkle.UpdateStateTree(events, 0, merkle.EventStatePath)
if ID == global.Master {
gb := chain.GenerateGenesisBlock()
gb.StateRoot = stateRootHash.Bytes()
gb.EventRoot = eventRootHash.Bytes()
chain.BlockChain = append(chain.BlockChain, gb)
chain.StoreBlockChain(chain.BlockChain)
}
}
if ID == global.Master {
if len(bc) != 0 {
chain.BlockChain = bc
}
} else {
//非主节点初始化时需要和主节点进行区块信息同步
//先生成区块链同步请求消息,再发送
msg := network.GenBlockSynReqMsg(ID)
log.Info("发送区块链同步消息,msg:", msg, "addr:", global.NodeTable[global.Master])
util.TCPSend(msg, global.NodeTable[global.Master])
}
}
// 清空数据
func clear() {
os.RemoveAll("./levelDB/db/path/")
os.RemoveAll("./contract/contract/")
log.Info("成功删除数据!")
}