go-ethereum/p2p/tracker/tracker_test.go
Felix Lange 0cba803fba
Some checks failed
/ Linux Build (push) Has been cancelled
/ Linux Build (arm) (push) Has been cancelled
/ Keeper Build (push) Has been cancelled
/ Windows Build (push) Has been cancelled
/ Docker Image (push) Has been cancelled
eth/protocols/eth, eth/protocols/snap: delayed p2p message decoding (#33835)
This changes the p2p protocol handlers to delay message decoding. It's
the first part of a larger change that will delay decoding all the way
through message processing. For responses, we delay the decoding until
it is confirmed that the response matches an active request and does not
exceed its limits.

In order to make this work, all messages have been changed to use
rlp.RawList instead of a slice of the decoded item type. For block
bodies specifically, the decoding has been delayed all the way until
after verification of the response hash.

The role of p2p/tracker.Tracker changes significantly in this PR. The
Tracker's original purpose was to maintain metrics about requests and
responses in the peer-to-peer protocols. Each protocol maintained a
single global Tracker instance. As of this change, the Tracker is now
always active (regardless of metrics collection), and there is a
separate instance of it for each peer. Whenever a response arrives, it
is first verified that a request exists for it in the tracker. The
tracker is also the place where limits are kept.
2026-02-15 21:21:16 +08:00

64 lines
2 KiB
Go

// Copyright 2026 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 tracker
import (
"testing"
"time"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/p2p"
)
// This checks that metrics gauges for pending requests are be decremented when a
// Tracker is stopped.
func TestMetricsOnStop(t *testing.T) {
metrics.Enable()
cap := p2p.Cap{Name: "test", Version: 1}
tr := New(cap, "peer1", time.Minute)
// Track some requests with different ReqCodes.
var id uint64
for i := 0; i < 3; i++ {
tr.Track(Request{ID: id, ReqCode: 0x01, RespCode: 0x02, Size: 1})
id++
}
for i := 0; i < 5; i++ {
tr.Track(Request{ID: id, ReqCode: 0x03, RespCode: 0x04, Size: 1})
id++
}
gauge1 := tr.trackedGauge(0x01)
gauge2 := tr.trackedGauge(0x03)
if gauge1.Snapshot().Value() != 3 {
t.Fatalf("gauge1 value mismatch: got %d, want 3", gauge1.Snapshot().Value())
}
if gauge2.Snapshot().Value() != 5 {
t.Fatalf("gauge2 value mismatch: got %d, want 5", gauge2.Snapshot().Value())
}
tr.Stop()
if gauge1.Snapshot().Value() != 0 {
t.Fatalf("gauge1 value after stop: got %d, want 0", gauge1.Snapshot().Value())
}
if gauge2.Snapshot().Value() != 0 {
t.Fatalf("gauge2 value after stop: got %d, want 0", gauge2.Snapshot().Value())
}
}