-
Notifications
You must be signed in to change notification settings - Fork 4
/
instance.go
165 lines (136 loc) · 3.48 KB
/
instance.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
package minbft
import (
"bytes"
"github.com/ibalajiarun/go-consensus/peer/peerpb"
"github.com/ibalajiarun/go-consensus/pkg/command/commandpb"
pb "github.com/ibalajiarun/go-consensus/protocols/minbft/minbftpb"
)
type prPkg struct {
pr pb.NormalMessage
hash []byte
content []byte
cert []byte
from peerpb.PeerID
}
type instance struct {
p *minbft
slot pb.Order
view pb.View
command *commandpb.Command
commandHash []byte
prepared bool
commit bool
executed bool
quorum *Quorum
}
func makeInstance(m *pb.NormalMessage, p *minbft) *instance {
return &instance{
p: p,
view: m.View,
slot: m.Order,
command: m.Command,
commandHash: m.CommandHash,
commit: false,
quorum: newQuorum(p),
}
}
func (s *minbft) updateCommand(inst *instance, m *pb.NormalMessage) bool {
if inst.command != nil {
if !inst.command.Equal(m.Command) {
s.logger.Panicf("Different commands for same instance %v: Received: %v, Has %v.", inst, inst.command, m.Command)
// TODO: Trigger view change here.
return false
}
} else {
inst.command = m.Command
}
return true
}
func (s *minbft) updateCommandHash(inst *instance, m *pb.NormalMessage) bool {
if inst.commandHash != nil {
if !bytes.Equal(inst.commandHash, m.CommandHash) {
s.logger.Panicf("Different command hashes for same instance %v: Received: %v, Has %v.", inst, inst.command, m.Command)
// TODO: Trigger view change here.
return false
}
} else {
inst.commandHash = m.CommandHash
}
return true
}
func (p *minbft) checkCommandHash(cmd *commandpb.Command, hash []byte) bool {
compHash := cmd.Hash()
return bytes.Equal(hash, compHash[:])
}
func (p *minbft) onPrepare(m *pb.NormalMessage, from peerpb.PeerID) {
p.logger.Debugf("Replica %d ===[Prepare %v]===>>> Replica %d\n", from, m,
p.id)
if m.View < p.curView {
p.logger.Debugf("I have moved on to another view %v. %v", p.curView, m)
return
}
// update slot number
if p.slot < m.Order {
p.slot = m.Order
}
if !p.checkCommandHash(m.Command, m.CommandHash) {
p.logger.Panicf("The hash does not correspond to the command: %v", m)
}
inst, exists := p.log[m.Order]
if !exists {
inst = makeInstance(m, p)
p.log[m.Order] = inst
} else if !p.updateCommand(inst, m) || !p.updateCommandHash(inst, m) {
return
}
inst.quorum.log(from, m)
inst.prepared = true
cm := &pb.NormalMessage{
View: m.View,
Order: m.Order,
Type: pb.NormalMessage_Commit,
CommandHash: m.CommandHash,
}
inst.quorum.log(p.id, cm)
p.broadcastNormal(cm)
if inst.prepared && !inst.commit && inst.quorum.Majority(m) {
inst.commit = true
p.exec()
}
}
// HandleP2b handles P2b message
func (p *minbft) onCommit(m *pb.NormalMessage, from peerpb.PeerID) {
p.logger.Debugf("Replica %s ===[%v]===>>> Replica %s\n", from, m,
p.id)
// update slot number
if p.slot < m.Order {
p.slot = m.Order
}
inst, exists := p.log[m.Order]
// update instance
if !exists {
inst = makeInstance(m, p)
p.log[m.Order] = inst
} else if !p.updateCommandHash(inst, m) {
return
}
inst.quorum.log(from, m)
if inst.prepared && !inst.commit && inst.quorum.Majority(m) {
inst.commit = true
p.exec()
}
// p.logger.Debugf("ACKS %v", p.log[m.Order].quorum.acks)
}
func (p *minbft) exec() {
for {
e, ok := p.log[p.execute]
if !ok || !e.commit {
break
}
p.logger.Debugf("Replica %s execute [s=%d, cmd=%v]\n", p.id, p.execute,
e.command)
p.Execute(e.command)
e.executed = true
p.execute++
}
}