Skip to content

Commit c51e153

Browse files
committed
eth, metrics, p2p: prepare metrics and net packets to eth/62
1 parent d51d002 commit c51e153

7 files changed

Lines changed: 149 additions & 48 deletions

File tree

‎eth/downloader/downloader.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
const (
4040
eth60 = 60 // Constant to check for old protocol support
4141
eth61 = 61 // Constant to check for new protocol support
42+
eth62 = 62 // Constant to check for experimental protocol support
4243
)
4344

4445
var (
@@ -329,7 +330,7 @@ func (d *Downloader) syncWithPeer(p *peer, hash common.Hash, td *big.Int) (err e
329330
if err = d.fetchBlocks60(); err != nil {
330331
return err
331332
}
332-
case eth61:
333+
case eth61, eth62:
333334
// New eth/61, use forward, concurrent hash and block retrieval algorithm
334335
number, err := d.findAncestor(p)
335336
if err != nil {

‎eth/handler.go‎

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func (pm *ProtocolManager) Stop() {
176176
}
177177

178178
func (pm *ProtocolManager) newPeer(pv, nv int, p *p2p.Peer, rw p2p.MsgReadWriter) *peer {
179-
return newPeer(pv, nv, p, rw)
179+
return newPeer(pv, nv, p, newMeteredMsgWriter(rw))
180180
}
181181

182182
// handle is the callback invoked to manage the life cycle of an eth peer. When
@@ -281,14 +281,11 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
281281
case BlockHashesMsg:
282282
// A batch of hashes arrived to one of our previous requests
283283
msgStream := rlp.NewStream(msg.Payload, uint64(msg.Size))
284-
reqHashInPacketsMeter.Mark(1)
285284

286285
var hashes []common.Hash
287286
if err := msgStream.Decode(&hashes); err != nil {
288287
break
289288
}
290-
reqHashInTrafficMeter.Mark(int64(32 * len(hashes)))
291-
292289
// Deliver them all to the downloader for queuing
293290
err := pm.downloader.DeliverHashes(p.id, hashes)
294291
if err != nil {
@@ -340,7 +337,6 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
340337
case BlocksMsg:
341338
// Decode the arrived block message
342339
msgStream := rlp.NewStream(msg.Payload, uint64(msg.Size))
343-
reqBlockInPacketsMeter.Mark(1)
344340

345341
var blocks []*types.Block
346342
if err := msgStream.Decode(&blocks); err != nil {
@@ -349,7 +345,6 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
349345
}
350346
// Update the receive timestamp of each block
351347
for _, block := range blocks {
352-
reqBlockInTrafficMeter.Mark(block.Size().Int64())
353348
block.ReceivedAt = msg.ReceivedAt
354349
}
355350
// Filter out any explicitly requested blocks, deliver the rest to the downloader
@@ -365,9 +360,6 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
365360
if err := msgStream.Decode(&hashes); err != nil {
366361
break
367362
}
368-
propHashInPacketsMeter.Mark(1)
369-
propHashInTrafficMeter.Mark(int64(32 * len(hashes)))
370-
371363
// Mark the hashes as present at the remote node
372364
for _, hash := range hashes {
373365
p.MarkBlock(hash)
@@ -390,9 +382,6 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
390382
if err := msg.Decode(&request); err != nil {
391383
return errResp(ErrDecode, "%v: %v", msg, err)
392384
}
393-
propBlockInPacketsMeter.Mark(1)
394-
propBlockInTrafficMeter.Mark(request.Block.Size().Int64())
395-
396385
if err := request.Block.ValidateFields(); err != nil {
397386
return errResp(ErrDecode, "block validation %v: %v", msg, err)
398387
}
@@ -427,7 +416,6 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
427416
if err := msg.Decode(&txs); err != nil {
428417
return errResp(ErrDecode, "msg %v: %v", msg, err)
429418
}
430-
propTxnInPacketsMeter.Mark(1)
431419
for i, tx := range txs {
432420
// Validate and mark the remote transaction
433421
if tx == nil {
@@ -436,7 +424,6 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
436424
p.MarkTransaction(tx.Hash())
437425

438426
// Log it's arrival for later analysis
439-
propTxnInTrafficMeter.Mark(tx.Size().Int64())
440427
jsonlogger.LogJson(&logger.EthTxReceived{
441428
TxHash: tx.Hash().Hex(),
442429
RemoteId: p.ID().String(),

‎eth/metrics.go‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package eth
1818

1919
import (
2020
"github.com/ethereum/go-ethereum/metrics"
21+
"github.com/ethereum/go-ethereum/p2p"
2122
)
2223

2324
var (
@@ -41,4 +42,87 @@ var (
4142
reqBlockInTrafficMeter = metrics.NewMeter("eth/req/blocks/in/traffic")
4243
reqBlockOutPacketsMeter = metrics.NewMeter("eth/req/blocks/out/packets")
4344
reqBlockOutTrafficMeter = metrics.NewMeter("eth/req/blocks/out/traffic")
45+
reqHeaderInPacketsMeter = metrics.NewMeter("eth/req/header/in/packets")
46+
reqHeaderInTrafficMeter = metrics.NewMeter("eth/req/header/in/traffic")
47+
reqHeaderOutPacketsMeter = metrics.NewMeter("eth/req/header/out/packets")
48+
reqHeaderOutTrafficMeter = metrics.NewMeter("eth/req/header/out/traffic")
49+
reqStateInPacketsMeter = metrics.NewMeter("eth/req/state/in/packets")
50+
reqStateInTrafficMeter = metrics.NewMeter("eth/req/state/in/traffic")
51+
reqStateOutPacketsMeter = metrics.NewMeter("eth/req/state/out/packets")
52+
reqStateOutTrafficMeter = metrics.NewMeter("eth/req/state/out/traffic")
53+
miscInPacketsMeter = metrics.NewMeter("eth/misc/in/packets")
54+
miscInTrafficMeter = metrics.NewMeter("eth/misc/in/traffic")
55+
miscOutPacketsMeter = metrics.NewMeter("eth/misc/out/packets")
56+
miscOutTrafficMeter = metrics.NewMeter("eth/misc/out/traffic")
4457
)
58+
59+
// meteredMsgReadWriter is a wrapper around a p2p.MsgReadWriter, capable of
60+
// accumulating the above defined metrics based on the data stream contents.
61+
type meteredMsgReadWriter struct {
62+
p2p.MsgReadWriter
63+
}
64+
65+
// newMeteredMsgWriter wraps a p2p MsgReadWriter with metering support. If the
66+
// metrics system is disabled, this fucntion returns the original object.
67+
func newMeteredMsgWriter(rw p2p.MsgReadWriter) p2p.MsgReadWriter {
68+
if !metrics.Enabled {
69+
return rw
70+
}
71+
return &meteredMsgReadWriter{rw}
72+
}
73+
74+
func (rw *meteredMsgReadWriter) ReadMsg() (p2p.Msg, error) {
75+
// Read the message and short circuit in case of an error
76+
msg, err := rw.MsgReadWriter.ReadMsg()
77+
if err != nil {
78+
return msg, err
79+
}
80+
// Account for the data traffic
81+
packets, traffic := miscInPacketsMeter, miscInTrafficMeter
82+
switch msg.Code {
83+
case BlockHashesMsg:
84+
packets, traffic = reqHashInPacketsMeter, reqHashInTrafficMeter
85+
case BlocksMsg:
86+
packets, traffic = reqBlockInPacketsMeter, reqBlockInTrafficMeter
87+
case BlockHeadersMsg:
88+
packets, traffic = reqHeaderInPacketsMeter, reqHeaderInTrafficMeter
89+
case NodeDataMsg:
90+
packets, traffic = reqStateInPacketsMeter, reqStateInTrafficMeter
91+
case NewBlockHashesMsg:
92+
packets, traffic = propHashInPacketsMeter, propHashInTrafficMeter
93+
case NewBlockMsg:
94+
packets, traffic = propBlockInPacketsMeter, propBlockInTrafficMeter
95+
case TxMsg:
96+
packets, traffic = propTxnInPacketsMeter, propTxnInTrafficMeter
97+
}
98+
packets.Mark(1)
99+
traffic.Mark(int64(msg.Size))
100+
101+
return msg, err
102+
}
103+
104+
func (rw *meteredMsgReadWriter) WriteMsg(msg p2p.Msg) error {
105+
// Account for the data traffic
106+
packets, traffic := miscOutPacketsMeter, miscOutTrafficMeter
107+
switch msg.Code {
108+
case BlockHashesMsg:
109+
packets, traffic = reqHashOutPacketsMeter, reqHashOutTrafficMeter
110+
case BlocksMsg:
111+
packets, traffic = reqBlockOutPacketsMeter, reqBlockOutTrafficMeter
112+
case BlockHeadersMsg:
113+
packets, traffic = reqHeaderOutPacketsMeter, reqHeaderOutTrafficMeter
114+
case NodeDataMsg:
115+
packets, traffic = reqStateOutPacketsMeter, reqStateOutTrafficMeter
116+
case NewBlockHashesMsg:
117+
packets, traffic = propHashOutPacketsMeter, propHashOutTrafficMeter
118+
case NewBlockMsg:
119+
packets, traffic = propBlockOutPacketsMeter, propBlockOutTrafficMeter
120+
case TxMsg:
121+
packets, traffic = propTxnOutPacketsMeter, propTxnOutTrafficMeter
122+
}
123+
packets.Mark(1)
124+
traffic.Mark(int64(msg.Size))
125+
126+
// Send the packet to the p2p layer
127+
return rw.MsgReadWriter.WriteMsg(msg)
128+
}

‎eth/peer.go‎

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -129,37 +129,25 @@ func (p *peer) MarkTransaction(hash common.Hash) {
129129
// SendTransactions sends transactions to the peer and includes the hashes
130130
// in its transaction hash set for future reference.
131131
func (p *peer) SendTransactions(txs types.Transactions) error {
132-
propTxnOutPacketsMeter.Mark(1)
133132
for _, tx := range txs {
134-
propTxnOutTrafficMeter.Mark(tx.Size().Int64())
135133
p.knownTxs.Add(tx.Hash())
136134
}
137135
return p2p.Send(p.rw, TxMsg, txs)
138136
}
139137

140138
// SendBlockHashes sends a batch of known hashes to the remote peer.
141139
func (p *peer) SendBlockHashes(hashes []common.Hash) error {
142-
reqHashOutPacketsMeter.Mark(1)
143-
reqHashOutTrafficMeter.Mark(int64(32 * len(hashes)))
144-
145140
return p2p.Send(p.rw, BlockHashesMsg, hashes)
146141
}
147142

148143
// SendBlocks sends a batch of blocks to the remote peer.
149144
func (p *peer) SendBlocks(blocks []*types.Block) error {
150-
reqBlockOutPacketsMeter.Mark(1)
151-
for _, block := range blocks {
152-
reqBlockOutTrafficMeter.Mark(block.Size().Int64())
153-
}
154145
return p2p.Send(p.rw, BlocksMsg, blocks)
155146
}
156147

157148
// SendNewBlockHashes announces the availability of a number of blocks through
158149
// a hash notification.
159150
func (p *peer) SendNewBlockHashes(hashes []common.Hash) error {
160-
propHashOutPacketsMeter.Mark(1)
161-
propHashOutTrafficMeter.Mark(int64(32 * len(hashes)))
162-
163151
for _, hash := range hashes {
164152
p.knownBlocks.Add(hash)
165153
}
@@ -168,33 +156,55 @@ func (p *peer) SendNewBlockHashes(hashes []common.Hash) error {
168156

169157
// SendNewBlock propagates an entire block to a remote peer.
170158
func (p *peer) SendNewBlock(block *types.Block, td *big.Int) error {
171-
propBlockOutPacketsMeter.Mark(1)
172-
propBlockOutTrafficMeter.Mark(block.Size().Int64())
173-
174159
p.knownBlocks.Add(block.Hash())
175160
return p2p.Send(p.rw, NewBlockMsg, []interface{}{block, td})
176161
}
177162

163+
// SendBlockHeaders sends a batch of block headers to the remote peer.
164+
func (p *peer) SendBlockHeaders(headers []*types.Header) error {
165+
return p2p.Send(p.rw, BlockHeadersMsg, headers)
166+
}
167+
168+
// SendNodeData sends a batch of arbitrary internal data, corresponding to the
169+
// hashes requested.
170+
func (p *peer) SendNodeData(data [][]byte) error {
171+
return p2p.Send(p.rw, NodeDataMsg, data)
172+
}
173+
178174
// RequestHashes fetches a batch of hashes from a peer, starting at from, going
179175
// towards the genesis block.
180176
func (p *peer) RequestHashes(from common.Hash) error {
181-
glog.V(logger.Debug).Infof("Peer [%s] fetching hashes (%d) from %x...\n", p.id, downloader.MaxHashFetch, from[:4])
177+
glog.V(logger.Debug).Infof("%v fetching hashes (%d) from %x...\n", p, downloader.MaxHashFetch, from[:4])
182178
return p2p.Send(p.rw, GetBlockHashesMsg, getBlockHashesData{from, uint64(downloader.MaxHashFetch)})
183179
}
184180

185-
// RequestHashesFromNumber fetches a batch of hashes from a peer, starting at the
186-
// requested block number, going upwards towards the genesis block.
181+
// RequestHashesFromNumber fetches a batch of hashes from a peer, starting at
182+
// the requested block number, going upwards towards the genesis block.
187183
func (p *peer) RequestHashesFromNumber(from uint64, count int) error {
188-
glog.V(logger.Debug).Infof("Peer [%s] fetching hashes (%d) from #%d...\n", p.id, count, from)
184+
glog.V(logger.Debug).Infof("%v fetching hashes (%d) from #%d...\n", p, count, from)
189185
return p2p.Send(p.rw, GetBlockHashesFromNumberMsg, getBlockHashesFromNumberData{from, uint64(count)})
190186
}
191187

192188
// RequestBlocks fetches a batch of blocks corresponding to the specified hashes.
193189
func (p *peer) RequestBlocks(hashes []common.Hash) error {
194-
glog.V(logger.Debug).Infof("[%s] fetching %v blocks\n", p.id, len(hashes))
190+
glog.V(logger.Debug).Infof("%v fetching %v blocks\n", p, len(hashes))
195191
return p2p.Send(p.rw, GetBlocksMsg, hashes)
196192
}
197193

194+
// RequestHeaders fetches a batch of blocks' headers corresponding to the
195+
// specified hashes.
196+
func (p *peer) RequestHeaders(hashes []common.Hash) error {
197+
glog.V(logger.Debug).Infof("%v fetching %v headers\n", p, len(hashes))
198+
return p2p.Send(p.rw, GetBlockHeadersMsg, hashes)
199+
}
200+
201+
// RequestNodeData fetches a batch of arbitrary data from a node's known state
202+
// data, corresponding to the specified hashes.
203+
func (p *peer) RequestNodeData(hashes []common.Hash) error {
204+
glog.V(logger.Debug).Infof("%v fetching %v state data\n", p, len(hashes))
205+
return p2p.Send(p.rw, GetNodeDataMsg, hashes)
206+
}
207+
198208
// Handshake executes the eth protocol handshake, negotiating version number,
199209
// network IDs, difficulties, head and genesis blocks.
200210
func (p *peer) Handshake(td *big.Int, head common.Hash, genesis common.Hash) error {

‎eth/protocol.go‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import (
2424
)
2525

2626
// Supported versions of the eth protocol (first is primary).
27-
var ProtocolVersions = []uint{61, 60}
27+
var ProtocolVersions = []uint{62, 61, 60}
2828

2929
// Number of implemented message corresponding to different protocol versions.
30-
var ProtocolLengths = []uint64{9, 8}
30+
var ProtocolLengths = []uint64{13, 9, 8}
3131

3232
const (
3333
NetworkId = 1
@@ -36,6 +36,7 @@ const (
3636

3737
// eth protocol message codes
3838
const (
39+
// Protocol messages belonging to eth/60
3940
StatusMsg = iota
4041
NewBlockHashesMsg
4142
TxMsg
@@ -44,7 +45,15 @@ const (
4445
GetBlocksMsg
4546
BlocksMsg
4647
NewBlockMsg
48+
49+
// Protocol messages belonging to eth/61
4750
GetBlockHashesFromNumberMsg
51+
52+
// Protocol messages belonging to eth/62
53+
GetBlockHeadersMsg
54+
BlockHeadersMsg
55+
GetNodeDataMsg
56+
NodeDataMsg
4857
)
4958

5059
type errCode int
@@ -102,15 +111,14 @@ type statusData struct {
102111
GenesisBlock common.Hash
103112
}
104113

105-
// getBlockHashesData is the network packet for the hash based block retrieval
106-
// message.
114+
// getBlockHashesData is the network packet for the hash based hash retrieval.
107115
type getBlockHashesData struct {
108116
Hash common.Hash
109117
Amount uint64
110118
}
111119

112-
// getBlockHashesFromNumberData is the network packet for the number based block
113-
// retrieval message.
120+
// getBlockHashesFromNumberData is the network packet for the number based hash
121+
// retrieval.
114122
type getBlockHashesFromNumberData struct {
115123
Number uint64
116124
Amount uint64
@@ -121,3 +129,8 @@ type newBlockData struct {
121129
Block *types.Block
122130
TD *big.Int
123131
}
132+
133+
// nodeDataData is the network response packet for a node data retrieval.
134+
type nodeDataData []struct {
135+
Value []byte
136+
}

0 commit comments

Comments
 (0)