-
Notifications
You must be signed in to change notification settings - Fork 189
/
commit.go
155 lines (130 loc) · 5.29 KB
/
commit.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
// Modifications Copyright 2018 The klaytn Authors
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
//
// This file is derived from quorum/consensus/istanbul/core/commit.go (2018/06/04).
// Modified and improved for the klaytn development.
package core
import (
"reflect"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/consensus/istanbul"
)
func (c *core) sendCommit() {
logger := c.logger.NewWith("state", c.state)
if c.current.Preprepare == nil {
logger.Error("Failed to get parentHash from roundState in sendCommit")
return
}
sub := c.current.Subject()
prevHash := c.current.Proposal().ParentHash()
// Do not send message if the owner of the core is not a member of the committee for the `sub.View`
if !c.valSet.CheckInSubList(prevHash, sub.View, c.Address()) {
return
}
// TODO-Klaytn-Istanbul: generalize broadcastCommit for all istanbul message types
c.broadcastCommit(sub)
}
func (c *core) sendCommitForOldBlock(view *istanbul.View, digest common.Hash, prevHash common.Hash) {
sub := &istanbul.Subject{
View: view,
Digest: digest,
PrevHash: prevHash,
}
c.broadcastCommit(sub)
}
func (c *core) broadcastCommit(sub *istanbul.Subject) {
logger := c.logger.NewWith("state", c.state)
encodedSubject, err := Encode(sub)
if err != nil {
logger.Error("Failed to encode", "subject", sub)
return
}
c.broadcast(&message{
Hash: sub.PrevHash,
Code: msgCommit,
Msg: encodedSubject,
})
}
func (c *core) handleCommit(msg *message, src istanbul.Validator) error {
// Decode COMMIT message
var commit *istanbul.Subject
err := msg.Decode(&commit)
if err != nil {
logger.Error("Failed to decode message", "code", msg.Code, "err", err)
return errInvalidMessage
}
// logger.Error("receive handle commit","num", commit.View.Sequence)
if err := c.checkMessage(msgCommit, commit.View); err != nil {
// logger.Error("### istanbul/commit.go checkMessage","num",commit.View.Sequence,"err",err)
return err
}
if err := c.verifyCommit(commit, src); err != nil {
return err
}
if !c.valSet.CheckInSubList(msg.Hash, commit.View, src.Address()) {
logger.Warn("received an istanbul commit message from non-committee",
"currentSequence", c.current.sequence.Uint64(), "sender", src.Address().String(), "msgView", commit.View.String())
return errNotFromCommittee
}
c.acceptCommit(msg, src)
// Change to Prepared state if we've received enough PREPARE/COMMIT messages or it is locked
// and we are in earlier state before Prepared state.
// Both of PREPARE and COMMIT messages are counted since the nodes which is hashlocked in
// the previous round skip sending PREPARE messages.
if c.state.Cmp(StatePrepared) < 0 {
if c.current.IsHashLocked() && commit.Digest == c.current.GetLockedHash() {
logger.Warn("received commit of the hash locked proposal and change state to prepared", "msgType", msgCommit)
c.setState(StatePrepared)
c.sendCommit()
} else if c.current.GetPrepareOrCommitSize() >= requiredMessageCount(c.valSet) {
logger.Info("received a quorum of the messages and change state to prepared", "msgType", msgCommit, "valSet", c.valSet.Size())
c.current.LockHash()
c.setState(StatePrepared)
c.sendCommit()
}
}
// Commit the proposal once we have enough COMMIT messages and we are not in the Committed state.
//
// If we already have a proposal, we may have chance to speed up the consensus process
// by committing the proposal without PREPARE messages.
//logger.Error("### consensus check","len(commits)",c.current.Commits.Size(),"f(2/3)",2*c.valSet.F(),"state",c.state.Cmp(StateCommitted))
if c.state.Cmp(StateCommitted) < 0 && c.current.Commits.Size() >= requiredMessageCount(c.valSet) {
// Still need to call LockHash here since state can skip Prepared state and jump directly to the Committed state.
c.current.LockHash()
c.commit()
}
return nil
}
// verifyCommit verifies if the received COMMIT message is equivalent to our subject
func (c *core) verifyCommit(commit *istanbul.Subject, src istanbul.Validator) error {
logger := c.logger.NewWith("from", src, "state", c.state)
sub := c.current.Subject()
if !reflect.DeepEqual(commit, sub) {
logger.Warn("Inconsistent subjects between commit and proposal", "expected", sub, "got", commit)
return errInconsistentSubject
}
return nil
}
func (c *core) acceptCommit(msg *message, src istanbul.Validator) error {
logger := c.logger.NewWith("from", src, "state", c.state)
// Add the COMMIT message to current round state
if err := c.current.Commits.Add(msg); err != nil {
logger.Error("Failed to record commit message", "msg", msg, "err", err)
return err
}
return nil
}