-
Notifications
You must be signed in to change notification settings - Fork 61
/
precommit.go
194 lines (165 loc) · 6.95 KB
/
precommit.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
// 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/>.
package core
import (
"bytes"
"context"
"math/big"
"github.com/clearmatics/autonity/common"
"github.com/clearmatics/autonity/core/types"
)
func (c *core) sendPrecommit(ctx context.Context, isNil bool) {
logger := c.logger.New("step", c.currentRoundState.Step())
var precommit = Vote{
Round: big.NewInt(c.currentRoundState.Round().Int64()),
Height: big.NewInt(c.currentRoundState.Height().Int64()),
}
if isNil {
precommit.ProposedBlockHash = common.Hash{}
} else {
if h := c.currentRoundState.GetCurrentProposalHash(); h == (common.Hash{}) {
c.logger.Error("core.sendPrecommit Proposal is empty! It should not be empty!")
return
}
precommit.ProposedBlockHash = c.currentRoundState.GetCurrentProposalHash()
}
encodedVote, err := Encode(&precommit)
if err != nil {
logger.Error("Failed to encode", "subject", precommit)
return
}
c.logPrecommitMessageEvent("MessageEvent(Precommit): Sent", precommit, c.address.String(), "broadcast")
msg := &Message{
Code: msgPrecommit,
Msg: encodedVote,
Address: c.address,
CommittedSeal: []byte{},
}
// Create committed seal
seal := PrepareCommittedSeal(precommit.ProposedBlockHash)
msg.CommittedSeal, err = c.backend.Sign(seal)
if err != nil {
c.logger.Error("core.sendPrecommit error while signing committed seal", "err", err)
}
c.sentPrecommit = true
c.broadcast(ctx, msg)
}
func (c *core) handlePrecommit(ctx context.Context, msg *Message) error {
var preCommit Vote
err := msg.Decode(&preCommit)
if err != nil {
return errFailedDecodePrecommit
}
if err := c.checkMessage(preCommit.Round, preCommit.Height, precommit); err != nil {
// Store old precommits because if there is a quorum of precommits in the previous round we need to go to the next height
//if err == errOldRoundMessage {
// // The roundstate must exist as every roundstate is added to c.currentHeightRoundsState at startRound
// // And we only process old rounds while future rounds messages are pushed on to the backlog
// oldRoundState := c.currentHeightOldRoundsStates[preCommit.Round.Int64()]
// c.acceptVote(&oldRoundState, precommit, preCommit.ProposedBlockHash, *msg)
//
// // Check for old round precommit quorum
// if c.Quorum(oldRoundState.Precommits.VotesSize(oldRoundState.GetCurrentProposalHash())) {
// select {
// case <-ctx.Done():
// return ctx.Err()
// default:
// c.commit()
// }
//
// return nil
// }
//}
return err
}
// Don't want to decode twice, hence sending preCommit with message
if err := c.verifyPrecommitCommittedSeal(msg.Address, append([]byte(nil), msg.CommittedSeal...), preCommit.ProposedBlockHash); err != nil {
return err
}
// We don't care about which step we are in to accept a preCommit, since it has the highest importance
precommitHash := preCommit.ProposedBlockHash
curR := c.currentRoundState.Round().Int64()
curH := c.currentRoundState.Height().Int64()
c.acceptVote(c.currentRoundState, precommit, precommitHash, *msg)
c.logPrecommitMessageEvent("MessageEvent(Precommit): Received", preCommit, msg.Address.String(), c.address.String())
// Line 49 in Algorithm 1 of The latest gossip on BFT consensus
curProposalHash := c.currentRoundState.GetCurrentProposalHash()
if curProposalHash != (common.Hash{}) && c.Quorum(c.currentRoundState.Precommits.VotesSize(curProposalHash)) {
if err := c.precommitTimeout.stopTimer(); err != nil {
return err
}
c.logger.Debug("Stopped Scheduled Precommit Timeout")
select {
case <-ctx.Done():
return ctx.Err()
default:
c.commit()
}
// Line 47 in Algorithm 1 of The latest gossip on BFT consensus
} else if !c.precommitTimeout.timerStarted() && c.Quorum(c.currentRoundState.Precommits.TotalSize()) {
timeoutDuration := timeoutPrecommit(curR)
c.precommitTimeout.scheduleTimeout(timeoutDuration, curR, curH, c.onTimeoutPrecommit)
c.logger.Debug("Scheduled Precommit Timeout", "Timeout Duration", timeoutDuration)
}
return nil
}
func (c *core) verifyPrecommitCommittedSeal(addressMsg common.Address, committedSealMsg []byte, proposedBlockHash common.Hash) error {
committedSeal := PrepareCommittedSeal(proposedBlockHash)
addressOfSignerOfCommittedSeal, err := types.GetSignatureAddress(committedSeal, committedSealMsg)
if err != nil {
c.logger.Error("Failed to get signer address", "err", err)
return err
}
// ensure sender signed the committed seal
if !bytes.Equal(addressOfSignerOfCommittedSeal.Bytes(), addressMsg.Bytes()) {
c.logger.Error("verify precommit seal error", "got", addressMsg.String(), "expected", addressOfSignerOfCommittedSeal.String())
return errInvalidSenderOfCommittedSeal
}
return nil
}
func (c *core) handleCommit(ctx context.Context) {
c.logger.Debug("Received a final committed proposal", "step", c.currentRoundState.Step())
lastBlock, _ := c.backend.LastCommittedProposal()
height := new(big.Int).Add(lastBlock.Number(), common.Big1).Uint64()
if height == c.currentRoundState.Height().Uint64() {
c.logger.Debug("Discarding event as core is at the same height", "state_height", c.currentRoundState.Height().Uint64())
} else {
c.logger.Debug("Received proposal is ahead", "state_height", c.currentRoundState.Height().Uint64(), "block_height", height)
c.startRound(ctx, common.Big0)
}
}
func (c *core) logPrecommitMessageEvent(message string, precommit Vote, from, to string) {
currentProposalHash := c.currentRoundState.GetCurrentProposalHash()
c.logger.Debug(message,
"from", from,
"to", to,
"currentHeight", c.currentRoundState.Height(),
"msgHeight", precommit.Height,
"currentRound", c.currentRoundState.Round(),
"msgRound", precommit.Round,
"currentStep", c.currentRoundState.Step(),
"isProposer", c.isProposer(),
"currentProposer", c.valSet.GetProposer(),
"isNilMsg", precommit.ProposedBlockHash == common.Hash{},
"hash", precommit.ProposedBlockHash,
"type", "Precommit",
"totalVotes", c.currentRoundState.Precommits.TotalSize(),
"totalNilVotes", c.currentRoundState.Precommits.NilVotesSize(),
"quorumReject", c.Quorum(c.currentRoundState.Precommits.NilVotesSize()),
"totalNonNilVotes", c.currentRoundState.Precommits.VotesSize(currentProposalHash),
"quorumAccept", c.Quorum(c.currentRoundState.Precommits.VotesSize(currentProposalHash)),
)
}