forked from dominant-strategies/go-quai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_eth.go
237 lines (211 loc) · 9.01 KB
/
handler_eth.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2015 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 eth
import (
"errors"
"fmt"
"math/big"
"sync/atomic"
"time"
"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/core"
"github.com/dominant-strategies/go-quai/core/types"
"github.com/dominant-strategies/go-quai/eth/protocols/eth"
"github.com/dominant-strategies/go-quai/log"
"github.com/dominant-strategies/go-quai/p2p/enode"
)
const (
MaxBlockFetchDist = 50
)
// ethHandler implements the eth.Backend interface to handle the various network
// packets that are sent as replies or broadcasts.
type ethHandler handler
func (h *ethHandler) Core() *core.Core { return h.core }
func (h *ethHandler) TxPool() eth.TxPool { return h.txpool }
// RunPeer is invoked when a peer joins on the `eth` protocol.
func (h *ethHandler) RunPeer(peer *eth.Peer, hand eth.Handler) error {
// Cannot Handshake with a peer before finishing the bad hashes cleanup
if h.core.BadHashExistsInChain() {
log.Warn("Bad Hashes still exist on chain, cannot handshake with any peer yet")
return nil
}
return (*handler)(h).runEthPeer(peer, hand)
}
// PeerInfo retrieves all known `eth` information about a peer.
func (h *ethHandler) PeerInfo(id enode.ID) interface{} {
if p := h.peers.peer(id.String()); p != nil {
return p.info()
}
return nil
}
// AcceptTxs retrieves whether transaction processing is enabled on the node
// or if inbound transactions should simply be dropped.
func (h *ethHandler) AcceptTxs() bool {
return atomic.LoadUint32(&h.acceptTxs) == 1
}
// Handle is invoked from a peer's message handler when it receives a new remote
// message that the handler couldn't consume and serve itself.
func (h *ethHandler) Handle(peer *eth.Peer, packet eth.Packet) error {
// Consume any broadcasts and announces, forwarding the rest to the downloader
switch packet := packet.(type) {
case *eth.BlockHeadersPacket:
return h.handleHeaders(peer, *packet)
case *eth.BlockBodiesPacket:
txset, uncleset, etxset, manifestset := packet.Unpack()
return h.handleBodies(peer, txset, uncleset, etxset, manifestset)
case *eth.NewBlockHashesPacket:
hashes, numbers := packet.Unpack()
return h.handleBlockAnnounces(peer, hashes, numbers)
case *eth.NewBlockPacket:
return h.handleBlockBroadcast(peer, packet.Block, packet.Entropy, packet.Relay)
case *eth.NewPooledTransactionHashesPacket:
return h.txFetcher.Notify(peer.ID(), *packet)
case *eth.TransactionsPacket:
return h.txFetcher.Enqueue(peer.ID(), *packet, false)
case *eth.PooledTransactionsPacket:
return h.txFetcher.Enqueue(peer.ID(), *packet, true)
default:
return fmt.Errorf("unexpected eth packet type: %T", packet)
}
}
// handleHeaders is invoked from a peer's message handler when it transmits a batch
// of headers for the local node to process.
func (h *ethHandler) handleHeaders(peer *eth.Peer, headers []*types.Header) error {
p := h.peers.peer(peer.ID())
if p == nil {
return errors.New("unregistered during callback")
}
// If no headers were received, but we're expencting a checkpoint header, consider it that
if len(headers) == 0 && p.syncDrop != nil {
// Stop the timer either way, decide later to drop or not
p.syncDrop.Stop()
p.syncDrop = nil
}
// Filter out any explicitly requested headers, deliver the rest to the downloader
filter := len(headers) == 1
if filter {
// Otherwise if it's a whitelisted block, validate against the set
if want, ok := h.whitelist[headers[0].Number().Uint64()]; ok {
if hash := headers[0].Hash(); want != hash {
peer.Log().Info("Whitelist mismatch, dropping peer", "number", headers[0].Number().Uint64(), "hash", hash, "want", want)
return errors.New("whitelist block mismatch")
}
peer.Log().Debug("Whitelist block verified", "number", headers[0].Number().Uint64(), "hash", want)
}
// Irrelevant of the fork checks, send the header to the fetcher just in case
headers = h.blockFetcher.FilterHeaders(peer.ID(), headers, time.Now())
}
if len(headers) > 0 || !filter {
err := h.downloader.DeliverHeaders(peer.ID(), headers)
if err != nil {
log.Debug("Failed to deliver headers", "err", err)
}
}
return nil
}
// handleBodies is invoked from a peer's message handler when it transmits a batch
// of block bodies for the local node to process.
func (h *ethHandler) handleBodies(peer *eth.Peer, txs [][]*types.Transaction, uncles [][]*types.Header, etxs [][]*types.Transaction, manifest []types.BlockManifest) error {
// Filter out any explicitly requested bodies, deliver the rest to the downloader
filter := len(txs) > 0 || len(uncles) > 0 || len(etxs) > 0 || len(manifest) > 0
if filter {
txs, uncles, etxs, manifest = h.blockFetcher.FilterBodies(peer.ID(), txs, uncles, etxs, manifest, time.Now())
}
if len(txs) > 0 || len(uncles) > 0 || len(etxs) > 0 || len(manifest) > 0 || !filter {
err := h.downloader.DeliverBodies(peer.ID(), txs, uncles, etxs, manifest)
if err != nil {
log.Debug("Failed to deliver bodies", "err", err)
}
}
return nil
}
// handleBlockAnnounces is invoked from a peer's message handler when it transmits a
// batch of block announcements for the local node to process.
func (h *ethHandler) handleBlockAnnounces(peer *eth.Peer, hashes []common.Hash, numbers []uint64) error {
// Do not handle any broadcast until we finish resetting from the bad state.
// This should be a very small time window
if h.Core().BadHashExistsInChain() {
log.Warn("Bad Hashes still exist on chain, cannot listen to Block Hash announcements yet")
return nil
}
// Schedule all the unknown hashes for retrieval
var (
unknownHashes = make([]common.Hash, 0, len(hashes))
unknownNumbers = make([]uint64, 0, len(numbers))
)
for i := 0; i < len(hashes); i++ {
if !h.core.HasBlock(hashes[i], numbers[i]) {
unknownHashes = append(unknownHashes, hashes[i])
unknownNumbers = append(unknownNumbers, numbers[i])
}
}
for i := 0; i < len(unknownHashes); i++ {
h.blockFetcher.Notify(peer.ID(), unknownHashes[i], unknownNumbers[i], time.Now(), peer.RequestOneHeader, peer.RequestBodies)
}
return nil
}
// handleBlockBroadcast is invoked from a peer's message handler when it transmits a
// block broadcast for the local node to process.
func (h *ethHandler) handleBlockBroadcast(peer *eth.Peer, block *types.Block, entropy *big.Int, relay bool) error {
// Do not handle any broadcast until we finish resetting from the bad state.
// This should be a very small time window
if h.Core().BadHashExistsInChain() {
log.Warn("Bad Hashes still exist on chain, cannot handle block broadcast yet")
return nil
}
syncEntropy, threshold := h.core.SyncTargetEntropy()
window := new(big.Int).Mul(threshold, big.NewInt(5))
syncThreshold := new(big.Int).Add(block.ParentEntropy(), window)
requestBlock := h.subSyncQueue.Contains(block.Hash())
beyondSyncPoint := syncEntropy.Cmp(syncThreshold) < 0
looseSyncEntropyDelta := new(big.Int).Div(syncEntropy, big.NewInt(100))
looseSyncEntropy := new(big.Int).Sub(syncEntropy, looseSyncEntropyDelta)
atFray := looseSyncEntropy.Cmp(h.core.CurrentHeader().ParentEntropy()) < 0
// If block is greater than sync entropy, or its manifest cache, handle it
// If block if its in manifest cache, relay is set to true, set relay to false and handle
// !atFray checked because when "synced" we want to be able to check entropy against later window
log.Debug("Handle Block", "requestBlock", requestBlock, "atFray", atFray, "relay", relay, "beyondSync", beyondSyncPoint)
if relay && !atFray {
if !beyondSyncPoint {
if !requestBlock {
// drop peer
if common.NodeLocation.Context() != common.PRIME_CTX {
log.Info("Peer broadcasting block not in requestQueue or beyond sync target, dropping peer")
h.downloader.DropPeer(peer)
}
return nil
} else {
relay = false
}
}
}
h.blockFetcher.ImportBlocks(peer.ID(), block, relay)
if block != nil && !h.broadcastCache.Contains(block.Hash()) {
log.Info("Received Block Broadcast", "Hash", block.Hash(), "Number", block.Header().NumberArray())
h.broadcastCache.Add(block.Hash(), true)
}
_, _, peerEntropy, _ := peer.Head()
if entropy != nil && peerEntropy != nil {
if peerEntropy.Cmp(entropy) < 0 {
peer.SetHead(block.Hash(), block.Number(), entropy, block.ReceivedAt)
// Only start the downloader in Prime
if common.NodeLocation.Context() == common.PRIME_CTX {
h.chainSync.handlePeerEvent(peer)
}
}
}
return nil
}