p2p: revert reason decoding back to using array

This commit is contained in:
lorenzo 2024-12-05 16:05:15 +01:00
parent 67a3b08795
commit 82e82eb4bf
2 changed files with 18 additions and 3 deletions

View file

@ -343,11 +343,11 @@ func (p *Peer) handle(msg Msg) error {
case <-p.closed: case <-p.closed:
} }
case msg.Code == discMsg: case msg.Code == discMsg:
var reason [1]DiscReason
// This is the last message. We don't need to discard or // This is the last message. We don't need to discard or
// check errors because, the connection will be closed after it. // check errors because, the connection will be closed after it.
var m struct{ R DiscReason } rlp.Decode(msg.Payload, &reason)
rlp.Decode(msg.Payload, &m) return reason[0]
return m.R
case msg.Code < baseProtocolLength: case msg.Code < baseProtocolLength:
// ignore other base protocol messages // ignore other base protocol messages
return msg.Discard() return msg.Discard()

View file

@ -17,9 +17,12 @@
package p2p package p2p
import ( import (
"bytes"
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt" "fmt"
"github.com/autonity/autonity/rlp"
"github.com/stretchr/testify/require"
"math/rand" "math/rand"
"net" "net"
"reflect" "reflect"
@ -360,3 +363,15 @@ func TestMatchProtocols(t *testing.T) {
} }
} }
} }
func TestDiscReasonDecoding(t *testing.T) {
// as it is encoded func (t *rlpxTransport) close(err error) in transport.go
var payload bytes.Buffer
err := rlp.Encode(&payload, []DiscReason{DiscQuitting})
require.NoError(t, err)
p := &Peer{}
err = p.handle(Msg{Code: discMsg, Payload: bytes.NewReader(payload.Bytes())})
t.Log(err)
require.True(t, errors.Is(err, DiscQuitting))
}