-
Notifications
You must be signed in to change notification settings - Fork 4
/
cinstance.go
156 lines (137 loc) · 4.09 KB
/
cinstance.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
package dqpbft
import (
"bytes"
"github.com/ibalajiarun/go-consensus/peer/peerpb"
pb "github.com/ibalajiarun/go-consensus/protocols/dqpbft/dqpbftpb"
)
type instance struct {
d *DQPBFT
is pb.InstanceState
pCert *Quorum
cCert *Quorum
}
func makeInstance(m *pb.AgreementMessage, p *DQPBFT) *instance {
return &instance{
is: pb.InstanceState{
View: m.View,
InstanceID: m.InstanceID,
Status: pb.InstanceState_None,
Command: m.Command,
CommandHash: m.CommandHash,
},
pCert: newQuorum(p),
cCert: newQuorum(p),
}
}
func (d *DQPBFT) updateCommand(inst *instance, m *pb.AgreementMessage) bool {
if inst.is.Command != nil {
if !inst.is.Command.Equal(m.Command) {
d.logger.Fatalf("Different commands for same instance %v: Received: %v, Has %v.", inst, inst.is.Command, m.Command)
// TODO: Trigger view change here.
return false
}
} else {
inst.is.Command = m.Command
}
return true
}
func (d *DQPBFT) updateCommandHash(inst *instance, m *pb.AgreementMessage) bool {
if inst.is.CommandHash != nil {
if !bytes.Equal(inst.is.CommandHash, m.CommandHash) {
d.logger.Panicf("Different command hashes for same instance %v: Received: %v, Has %v.", inst, inst.is.Command, m.Command)
// TODO: Trigger view change here.
return false
}
} else {
inst.is.CommandHash = m.CommandHash
}
return true
}
func (d *DQPBFT) onPrePrepare(m *pb.AgreementMessage, from peerpb.PeerID) {
if d.isPrimaryAtView(d.id, d.oview) {
d.onCRequest(m.InstanceID)
}
hash := m.Command.Hash()
m.CommandHash = hash
inst, exists := d.log[m.InstanceID]
// Update the log with the message if it not known already. Otherwise,
// ensure that the log is consistent with the message.
if !exists {
inst = makeInstance(m, d)
d.log[m.InstanceID] = inst
} else if !d.updateCommand(inst, m) || !d.updateCommandHash(inst, m) {
return
}
// The PrePrepare message of the primary serves as the prepare message.
m.Command = nil
inst.pCert.log(from, m)
inst.is.Status = pb.InstanceState_PrePrepared
pm := &pb.AgreementMessage{
View: inst.is.View,
InstanceID: inst.is.InstanceID,
Type: pb.AgreementMessage_Prepare,
CommandHash: inst.is.CommandHash,
}
inst.pCert.log(d.id, pm)
d.broadcast(pm, false)
if inst.pCert.Majority(pm) && inst.is.IsPrePrepared() {
inst.is.Status = pb.InstanceState_Prepared
cm := &pb.AgreementMessage{
View: pm.View,
InstanceID: pm.InstanceID,
Type: pb.AgreementMessage_Commit,
CommandHash: inst.is.CommandHash,
}
d.broadcast(cm, false)
inst.cCert.log(d.id, cm)
if inst.cCert.Majority(cm) && inst.is.IsPrepared() {
inst.is.Status = pb.InstanceState_Committed
d.exec()
}
}
}
func (d *DQPBFT) onPrepare(m *pb.AgreementMessage, from peerpb.PeerID) {
inst, exists := d.log[m.InstanceID]
// Update the log with the message if it not known already. Otherwise,
// ensure that the log is consistent with the message.
if !exists {
inst = makeInstance(m, d)
d.log[m.InstanceID] = inst
} else if !d.updateCommandHash(inst, m) {
return
}
inst.pCert.log(from, m)
if inst.pCert.Majority(m) && inst.is.IsPrePrepared() {
inst.is.Status = pb.InstanceState_Prepared
cm := &pb.AgreementMessage{
View: m.View,
InstanceID: m.InstanceID,
Type: pb.AgreementMessage_Commit,
CommandHash: inst.is.CommandHash,
}
d.broadcast(cm, false)
inst.cCert.log(d.id, cm)
if inst.cCert.Majority(cm) && inst.is.IsPrepared() {
inst.is.Status = pb.InstanceState_Committed
d.exec()
}
}
}
// HandleP2b handles P2b message
func (d *DQPBFT) onCommit(m *pb.AgreementMessage, from peerpb.PeerID) {
inst, exists := d.log[m.InstanceID]
// Update the log with the message if it not known already. Otherwise,
// ensure that the log is consistent with the message.
if !exists {
inst = makeInstance(m, d)
d.log[m.InstanceID] = inst
} else if !d.updateCommandHash(inst, m) {
return
}
inst.cCert.log(from, m)
if inst.cCert.Majority(m) && inst.is.IsPrepared() {
inst.is.Status = pb.InstanceState_Committed
d.exec()
}
//p.logger.Debugf("commit acks for %v: %v", m.Command.ID, len(p.log[m.Index].cCert.msgs))
}