forked from klaytn/klaytn
/
handler.go
134 lines (116 loc) · 3.71 KB
/
handler.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
// 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/backend/handler.go (2018/06/04).
// Modified and improved for the klaytn development.
package backend
import (
"errors"
"github.com/hashicorp/golang-lru"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/consensus"
"github.com/klaytn/klaytn/consensus/istanbul"
"github.com/klaytn/klaytn/networks/p2p"
"github.com/klaytn/klaytn/node"
)
const (
IstanbulMsg = 0x11
)
var (
// errDecodeFailed is returned when decode message fails
errDecodeFailed = errors.New("fail to decode istanbul message")
)
// Protocol implements consensus.Engine.Protocol
func (sb *backend) Protocol() consensus.Protocol {
return consensus.Protocol{
Name: "istanbul",
Versions: []uint{64},
//Lengths: []uint64{18},
//Lengths: []uint64{19}, // add PoRMsg
Lengths: []uint64{21},
}
}
// HandleMsg implements consensus.Handler.HandleMsg
func (sb *backend) HandleMsg(addr common.Address, msg p2p.Msg) (bool, error) {
sb.coreMu.Lock()
defer sb.coreMu.Unlock()
if msg.Code == IstanbulMsg {
if !sb.coreStarted {
return true, istanbul.ErrStoppedEngine
}
var cmsg istanbul.ConsensusMsg
//var data []byte
if err := msg.Decode(&cmsg); err != nil {
return true, errDecodeFailed
}
data := cmsg.Payload
hash := istanbul.RLPHash(data)
// Mark peer's message
ms, ok := sb.recentMessages.Get(addr)
var m *lru.ARCCache
if ok {
m, _ = ms.(*lru.ARCCache)
} else {
m, _ = lru.NewARC(inmemoryMessages)
sb.recentMessages.Add(addr, m)
}
m.Add(hash, true)
// Mark self known message
if _, ok := sb.knownMessages.Get(hash); ok {
return true, nil
}
sb.knownMessages.Add(hash, true)
go sb.istanbulEventMux.Post(istanbul.MessageEvent{
Payload: data,
Hash: cmsg.PrevHash,
})
return true, nil
}
return false, nil
}
func (sb *backend) ValidatePeerType(addr common.Address) error {
// istanbul.Start vs try to connect by peer
for sb.chain == nil {
return errors.New("sb.chain is nil! --mine option might be missing")
}
for _, val := range sb.getValidators(sb.chain.CurrentHeader().Number.Uint64(), sb.chain.CurrentHeader().Hash()).List() {
if addr == val.Address() {
return nil
}
}
return errors.New("invalid address")
}
// SetBroadcaster implements consensus.Handler.SetBroadcaster
func (sb *backend) SetBroadcaster(broadcaster consensus.Broadcaster, nodetype p2p.ConnType) {
sb.broadcaster = broadcaster
if nodetype == node.CONSENSUSNODE {
sb.broadcaster.RegisterValidator(node.CONSENSUSNODE, sb)
}
}
// RegisterConsensusMsgCode registers the channel of consensus msg.
func (sb *backend) RegisterConsensusMsgCode(peer consensus.Peer) {
peer.RegisterConsensusMsgCode(IstanbulMsg)
}
func (sb *backend) NewChainHead() error {
sb.coreMu.RLock()
defer sb.coreMu.RUnlock()
if !sb.coreStarted {
return istanbul.ErrStoppedEngine
}
go sb.istanbulEventMux.Post(istanbul.FinalCommittedEvent{})
return nil
}