fix msg discard

This commit is contained in:
Sahil-4555 2025-12-02 09:51:48 +05:30
parent da3822dcec
commit a2dc82fb3b
3 changed files with 10 additions and 7 deletions

View file

@ -25,6 +25,7 @@ import (
"time" "time"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
) )
@ -64,9 +65,11 @@ func (msg Msg) String() string {
} }
// Discard reads any remaining payload data into a black hole. // Discard reads any remaining payload data into a black hole.
func (msg Msg) Discard() error { func (msg Msg) Discard() {
_, err := io.Copy(io.Discard, msg.Payload) _, err := io.Copy(io.Discard, msg.Payload)
return err if err != nil {
log.Error("[p2p] discard msg", "code", msg.Code, "size", msg.Size, "err", err)
}
} }
func (msg Msg) Time() time.Time { func (msg Msg) Time() time.Time {
@ -233,7 +236,8 @@ func ExpectMsg(r MsgReader, code uint64, content interface{}) error {
return fmt.Errorf("message code mismatch: got %d, expected %d", msg.Code, code) return fmt.Errorf("message code mismatch: got %d, expected %d", msg.Code, code)
} }
if content == nil { if content == nil {
return msg.Discard() msg.Discard()
return nil
} }
contentEnc, err := rlp.EncodeToBytes(content) contentEnc, err := rlp.EncodeToBytes(content)
if err != nil { if err != nil {

View file

@ -379,7 +379,8 @@ func (p *Peer) handle(msg Msg) error {
return decodeDisconnectMessage(msg.Payload) return decodeDisconnectMessage(msg.Payload)
case msg.Code < baseProtocolLength: case msg.Code < baseProtocolLength:
// ignore other base protocol messages // ignore other base protocol messages
return msg.Discard() msg.Discard()
return nil
default: default:
// it's a subprotocol message // it's a subprotocol message
proto, err := p.getProto(msg.Code) proto, err := p.getProto(msg.Code)

View file

@ -43,9 +43,7 @@ var discard = Protocol{
return err return err
} }
fmt.Printf("discarding %d\n", msg.Code) fmt.Printf("discarding %d\n", msg.Code)
if err = msg.Discard(); err != nil { msg.Discard()
return err
}
} }
}, },
} }