Unverified Commit c0e201b6 authored by Felix Lange's avatar Felix Lange Committed by GitHub

eth/protocols/eth, les: avoid Raw() when decoding HashOrNumber (#22841)

Getting the raw value is not necessary to decode this type, and
decoding it directly from the stream is faster.
parent ae5fcdc6
......@@ -155,19 +155,19 @@ func (hn *HashOrNumber) EncodeRLP(w io.Writer) error {
// DecodeRLP is a specialized decoder for HashOrNumber to decode the contents
// into either a block hash or a block number.
func (hn *HashOrNumber) DecodeRLP(s *rlp.Stream) error {
_, size, _ := s.Kind()
origin, err := s.Raw()
if err == nil {
_, size, err := s.Kind()
switch {
case err != nil:
return err
case size == 32:
err = rlp.DecodeBytes(origin, &hn.Hash)
hn.Number = 0
return s.Decode(&hn.Hash)
case size <= 8:
err = rlp.DecodeBytes(origin, &hn.Number)
hn.Hash = common.Hash{}
return s.Decode(&hn.Number)
default:
err = fmt.Errorf("invalid input size %d for origin", size)
}
return fmt.Errorf("invalid input size %d for origin", size)
}
return err
}
// BlockHeadersPacket represents a block header response.
......
......@@ -307,19 +307,19 @@ func (hn *hashOrNumber) EncodeRLP(w io.Writer) error {
// DecodeRLP is a specialized decoder for hashOrNumber to decode the contents
// into either a block hash or a block number.
func (hn *hashOrNumber) DecodeRLP(s *rlp.Stream) error {
_, size, _ := s.Kind()
origin, err := s.Raw()
if err == nil {
_, size, err := s.Kind()
switch {
case err != nil:
return err
case size == 32:
err = rlp.DecodeBytes(origin, &hn.Hash)
hn.Number = 0
return s.Decode(&hn.Hash)
case size <= 8:
err = rlp.DecodeBytes(origin, &hn.Number)
hn.Hash = common.Hash{}
return s.Decode(&hn.Number)
default:
err = fmt.Errorf("invalid input size %d for origin", size)
}
return fmt.Errorf("invalid input size %d for origin", size)
}
return err
}
// CodeData is the network response packet for a node data retrieval.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment