-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler.go
141 lines (127 loc) · 4.64 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
135
136
137
138
139
140
141
// 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 backend
import (
"bytes"
"errors"
"io"
"math/big"
"reflect"
"github.com/electroneum/electroneum-sc/common"
"github.com/electroneum/electroneum-sc/consensus"
"github.com/electroneum/electroneum-sc/consensus/istanbul"
qbfttypes "github.com/electroneum/electroneum-sc/consensus/istanbul/types"
"github.com/electroneum/electroneum-sc/core/types"
"github.com/electroneum/electroneum-sc/p2p"
lru "github.com/hashicorp/golang-lru"
)
const (
NewBlockMsg = 0x07
istanbulMsg = 0x11
)
var (
// errDecodeFailed is returned when decode message fails
errDecodeFailed = errors.New("fail to decode istanbul message")
// errPayloadReadFailed is returned when qbft message read fails
errPayloadReadFailed = errors.New("unable to read payload from message")
)
// Protocol implements consensus.Engine.Protocol
func (sb *Backend) Protocol() consensus.Protocol {
return consensus.IstanbulProtocol
}
func (sb *Backend) decode(msg p2p.Msg) ([]byte, common.Hash, error) {
var data []byte = make([]byte, msg.Size)
if _, err := msg.Payload.Read(data); err != nil {
return nil, common.Hash{}, errPayloadReadFailed
}
return data, istanbul.RLPHash(data), nil
}
// 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 _, ok := qbfttypes.MessageCodes()[msg.Code]; ok || msg.Code == istanbulMsg {
if !sb.coreStarted {
return true, istanbul.ErrStoppedEngine
}
data, hash, err := sb.decode(msg)
if err != nil {
return true, errDecodeFailed
}
// 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{
Code: msg.Code,
Payload: data,
})
return true, nil
}
//https://github.com/ConsenSys/quorum/pull/539
//https://github.com/ConsenSys/quorum/issues/389
if msg.Code == NewBlockMsg && sb.core != nil && sb.core.IsProposer() { // eth.NewBlockMsg: import cycle
// this case is to safeguard the race of similar block which gets propagated from other node while this node is proposing
// as p2p.Msg can only be decoded once (get EOF for any subsequence read), we need to make sure the payload is restored after we decode it
sb.logger.Debug("IBFT: received NewBlockMsg", "size", msg.Size, "payload.type", reflect.TypeOf(msg.Payload), "sender", addr)
if reader, ok := msg.Payload.(*bytes.Reader); ok {
payload, err := io.ReadAll(reader)
if err != nil {
return true, err
}
reader.Reset(payload) // ready to be decoded
defer reader.Reset(payload) // restore so main eth/handler can decode
var request struct { // this has to be same as eth/protocol.go#newBlockData as we are reading NewBlockMsg
Block *types.Block
TD *big.Int
}
if err := msg.Decode(&request); err != nil {
sb.logger.Error("IBFT: unable to decode the NewBlockMsg", "error", err)
return false, nil
}
newRequestedBlock := request.Block
if newRequestedBlock.Header().MixDigest == types.IstanbulDigest && sb.core.IsCurrentProposal(newRequestedBlock.Hash()) {
sb.logger.Debug("IBFT: block already proposed", "hash", newRequestedBlock.Hash(), "sender", addr)
return true, nil
}
}
}
return false, nil
}
// SetBroadcaster implements consensus.Handler.SetBroadcaster
func (sb *Backend) SetBroadcaster(broadcaster consensus.Broadcaster) {
sb.broadcaster = broadcaster
}
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
}