-
Notifications
You must be signed in to change notification settings - Fork 189
/
backlog.go
191 lines (170 loc) · 5.14 KB
/
backlog.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
// 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/backlog.go (2018/06/04).
// Modified and improved for the klaytn development.
package core
import (
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/common/prque"
"github.com/klaytn/klaytn/consensus/istanbul"
)
var (
// msgPriority is defined for calculating processing priority to speedup consensus
// msgPreprepare > msgCommit > msgPrepare
msgPriority = map[uint64]int{
msgPreprepare: 1,
msgCommit: 2,
msgPrepare: 3,
}
)
// checkMessage checks the message state
// return errInvalidMessage if the message is invalid
// return errFutureMessage if the message view is larger than current view
// return errOldMessage if the message view is smaller than current view
func (c *core) checkMessage(msgCode uint64, view *istanbul.View) error {
if view == nil || view.Sequence == nil || view.Round == nil {
return errInvalidMessage
}
if msgCode == msgRoundChange {
if view.Sequence.Cmp(c.currentView().Sequence) > 0 {
return errFutureMessage
} else if view.Cmp(c.currentView()) < 0 {
return errOldMessage
}
return nil
}
if view.Cmp(c.currentView()) > 0 {
return errFutureMessage
}
if view.Cmp(c.currentView()) < 0 {
return errOldMessage
}
if c.waitingForRoundChange {
return errFutureMessage
}
// StateAcceptRequest only accepts msgPreprepare
// other messages are future messages
if c.state == StateAcceptRequest {
if msgCode > msgPreprepare {
return errFutureMessage
}
return nil
}
// For states(StatePreprepared, StatePrepared, StateCommitted),
// can accept all message types if processing with same view
return nil
}
func (c *core) storeBacklog(msg *message, src istanbul.Validator) {
logger := c.logger.NewWith("from", src, "state", c.state)
if src.Address() == c.Address() {
logger.Warn("Backlog from self")
return
}
logger.Trace("Store future message")
c.backlogsMu.Lock()
defer c.backlogsMu.Unlock()
backlog := c.backlogs[src.Address()]
if backlog == nil {
backlog = prque.New()
}
switch msg.Code {
case msgPreprepare:
var p *istanbul.Preprepare
err := msg.Decode(&p)
if err == nil {
backlog.Push(msg, toPriority(msg.Code, p.View))
}
// for msgRoundChange, msgPrepare and msgCommit cases
default:
var p *istanbul.Subject
err := msg.Decode(&p)
if err == nil {
backlog.Push(msg, toPriority(msg.Code, p.View))
}
}
c.backlogs[src.Address()] = backlog
}
func (c *core) processBacklog() {
c.backlogsMu.Lock()
defer c.backlogsMu.Unlock()
for src, backlog := range c.backlogs {
if backlog == nil {
continue
}
logger := c.logger.NewWith("from", src, "state", c.state)
isFuture := false
// We stop processing if
// 1. backlog is empty
// 2. The first message in queue is a future message
for !(backlog.Empty() || isFuture) {
m, prio := backlog.Pop()
msg := m.(*message)
var view *istanbul.View
var prevHash common.Hash
switch msg.Code {
case msgPreprepare:
var m *istanbul.Preprepare
err := msg.Decode(&m)
if err == nil {
view = m.View
}
prevHash = m.Proposal.ParentHash()
// for msgRoundChange, msgPrepare and msgCommit cases
default:
var sub *istanbul.Subject
err := msg.Decode(&sub)
if err == nil {
view = sub.View
}
prevHash = sub.PrevHash
}
if view == nil {
logger.Debug("Nil view", "msg", msg)
continue
}
// Push back if it's a future message
err := c.checkMessage(msg.Code, view)
if err != nil {
if err == errFutureMessage {
logger.Trace("Stop processing backlog", "msg", msg)
backlog.Push(msg, prio)
isFuture = true
break
}
logger.Trace("Skip the backlog event", "msg", msg, "err", err)
continue
}
logger.Trace("Post backlog event", "msg", msg)
go c.sendEvent(backlogEvent{
src: src,
msg: msg,
Hash: prevHash,
})
}
}
}
func toPriority(msgCode uint64, view *istanbul.View) int64 {
if msgCode == msgRoundChange {
// For msgRoundChange, set the message priority based on its sequence
return -int64(view.Sequence.Uint64() * 1000)
}
// FIXME: round will be reset as 0 while new sequence
// 10 * Round limits the range of message code is from 0 to 9
// 1000 * Sequence limits the range of round is from 0 to 99
return -int64(view.Sequence.Uint64()*1000 + view.Round.Uint64()*10 + uint64(msgPriority[msgCode]))
}