-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpbft.go
221 lines (193 loc) · 5.28 KB
/
pbft.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"fmt"
"log"
"strconv"
"sync"
)
//PBFT Status List
const (
PBFTStateNone = iota
PBFTStatePrepare
PBFTStateCommit
)
//ConsensusInfo c
type ConsensusInfo struct {
Height int64
Hash string
VotesNumber int64
Votes map[string]struct{}
}
//Pbft p
type Pbft struct {
MsgLock sync.RWMutex
Mutex sync.RWMutex
State int64
Node *Node
PendingBlocks map[string]*Block
CurrentSlot int64
PrepareInfo *ConsensusInfo
CommitInfos map[string]*ConsensusInfo
PrepareHashCache map[string]struct{}
CommitHashCache map[string]struct{}
Chain *Blockchain
}
func newConsensusInfo() *ConsensusInfo {
return &ConsensusInfo{
VotesNumber: 1,
Votes: make(map[string]struct{}, 0),
}
}
//NewPbft create a new pbft instance
func NewPbft(node *Node) *Pbft {
pbft := &Pbft{
State: PBFTStateNone,
Node: node,
PendingBlocks: make(map[string]*Block, 0),
CurrentSlot: 0,
PrepareInfo: newConsensusInfo(),
CommitInfos: make(map[string]*ConsensusInfo, 0),
PrepareHashCache: make(map[string]struct{}, 0),
CommitHashCache: make(map[string]struct{}, 0),
Chain: node.Chain,
}
return pbft
}
//AddBlock made block into prepare state
func (p *Pbft) AddBlock(block *Block, slotNumber int64) {
hash := block.GetHash()
p.Mutex.Lock()
p.PendingBlocks[hash] = block
p.Mutex.Unlock()
if slotNumber > p.CurrentSlot {
p.ClearState()
}
if p.State == PBFTStateNone {
p.CurrentSlot = slotNumber
p.State = PBFTStatePrepare
p.PrepareInfo.Height = block.GetHeight()
p.PrepareInfo.Hash = block.GetHash()
p.PrepareInfo.VotesNumber = 1
p.Mutex.Lock()
p.PrepareInfo.Votes[strconv.FormatInt(p.Node.ID, 10)] = struct{}{}
p.Mutex.Unlock()
//fmt.Println("node", p.Node.ID, "change state to prepare")
stageMsg := StageMessage{
Height: block.GetHeight(),
Hash: block.GetHash(),
Signer: strconv.FormatInt(p.Node.ID, 10),
}
p.Node.Broadcast(PrepareMessage( /*p.Node.ID, */ stageMsg))
}
}
//ClearState clear state
func (p *Pbft) ClearState() {
p.State = PBFTStateNone
p.Mutex.Lock()
p.PrepareInfo = newConsensusInfo()
p.CommitInfos = make(map[string]*ConsensusInfo)
p.PendingBlocks = make(map[string]*Block)
p.Mutex.Unlock()
}
func (p *Pbft) handlePrepareMessage(msg *Message) {
stageMsg := msg.Body.(StageMessage)
cacheKey := fmt.Sprintf("%s:%d:%s", stageMsg.Hash, stageMsg.Height, stageMsg.Signer)
p.Mutex.RLock()
_, ok := p.PrepareHashCache[cacheKey]
p.Mutex.RUnlock()
if !ok {
p.Mutex.Lock()
p.PrepareHashCache[cacheKey] = struct{}{}
p.Mutex.Unlock()
p.Node.Broadcast(msg)
} else {
return
}
p.Mutex.RLock()
_, voted := p.PrepareInfo.Votes[stageMsg.Signer]
p.Mutex.RUnlock()
if p.State == PBFTStatePrepare && stageMsg.Hash == p.PrepareInfo.Hash &&
stageMsg.Height == p.PrepareInfo.Height && !voted {
p.Mutex.Lock()
p.PrepareInfo.Votes[stageMsg.Signer] = struct{}{}
p.Mutex.Unlock()
p.PrepareInfo.VotesNumber++
//fmt.Println("pbft", p.Node.ID, "prepare votes", p.PrepareInfo.VotesNumber)
if p.PrepareInfo.VotesNumber > maxFPNode {
//fmt.Println("node", p.Node.ID, "change state to commit")
p.State = PBFTStateCommit
commitInfo := newConsensusInfo()
commitInfo.Hash = p.PrepareInfo.Hash
commitInfo.Height = p.PrepareInfo.Height
commitInfo.Votes[strconv.FormatInt(p.Node.ID, 10)] = struct{}{}
p.Mutex.Lock()
p.CommitInfos[commitInfo.Hash] = commitInfo
p.Mutex.Unlock()
stageMsg := StageMessage{
Height: p.PrepareInfo.Height,
Hash: p.PrepareInfo.Hash,
Signer: strconv.FormatInt(p.Node.ID, 10),
}
p.Node.Broadcast(CommitMessage( /*p.Node.ID, */ stageMsg))
}
}
}
func (p *Pbft) handleCommitMessage(msg *Message) {
stageMsg := msg.Body.(StageMessage)
cacheKey := fmt.Sprintf("%s:%d:%s", stageMsg.Hash, stageMsg.Height, stageMsg.Signer)
p.Mutex.RLock()
_, ok := p.CommitHashCache[cacheKey]
p.Mutex.RUnlock()
if !ok {
p.Mutex.Lock()
p.CommitHashCache[cacheKey] = struct{}{}
p.Mutex.Unlock()
p.Node.Broadcast(msg)
} else {
return
}
p.Mutex.RLock()
commitInfo := p.CommitInfos[stageMsg.Hash]
p.Mutex.RUnlock()
if commitInfo != nil {
if _, ok := commitInfo.Votes[stageMsg.Signer]; !ok {
p.Mutex.Lock()
commitInfo.Votes[stageMsg.Signer] = struct{}{}
p.Mutex.Unlock()
commitInfo.VotesNumber++
//fmt.Println("pbft", p.Node.ID, "commit votes", commitInfo.VotesNumber)
if commitInfo.VotesNumber > 2*maxFPNode {
p.Mutex.RLock()
_, ok := p.PendingBlocks[stageMsg.Hash]
if ok {
p.Chain.CommitBlock(p.PendingBlocks[stageMsg.Hash])
}
p.Mutex.RUnlock()
p.ClearState()
}
}
} else {
commitInfo := newConsensusInfo()
commitInfo.Hash = stageMsg.Hash
commitInfo.Height = stageMsg.Height
commitInfo.Votes[strconv.FormatInt(p.Node.ID, 10)] = struct{}{}
p.Mutex.Lock()
p.CommitInfos[stageMsg.Hash] = commitInfo
p.Mutex.Unlock()
}
}
//ProcessStageMessage process prepare and commit message
func (p *Pbft) ProcessStageMessage(msg *Message) {
switch msg.Type {
case MessageTypePrepare:
p.MsgLock.Lock()
p.handlePrepareMessage(msg)
p.MsgLock.Unlock()
case MessageTypeCommit:
p.MsgLock.Lock()
p.handleCommitMessage(msg)
p.MsgLock.Unlock()
default:
log.Println("ProcessStageMessage cannot find match message type")
}
}