all: use rlp.DecodeBytes instead of rlp.Decode where possible (#27994)

This commit is contained in:
Felix Lange 2023-08-24 10:47:42 +02:00 committed by GitHub
parent 60ec41ce73
commit 9b46986edc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 13 additions and 17 deletions

@ -381,7 +381,7 @@ func ReadHeader(db ethdb.Reader, hash common.Hash, number uint64) *types.Header
return nil return nil
} }
header := new(types.Header) header := new(types.Header)
if err := rlp.Decode(bytes.NewReader(data), header); err != nil { if err := rlp.DecodeBytes(data, header); err != nil {
log.Error("Invalid block header RLP", "hash", hash, "err", err) log.Error("Invalid block header RLP", "hash", hash, "err", err)
return nil return nil
} }
@ -498,7 +498,7 @@ func ReadBody(db ethdb.Reader, hash common.Hash, number uint64) *types.Body {
return nil return nil
} }
body := new(types.Body) body := new(types.Body)
if err := rlp.Decode(bytes.NewReader(data), body); err != nil { if err := rlp.DecodeBytes(data, body); err != nil {
log.Error("Invalid block body RLP", "hash", hash, "err", err) log.Error("Invalid block body RLP", "hash", hash, "err", err)
return nil return nil
} }
@ -544,7 +544,7 @@ func ReadTd(db ethdb.Reader, hash common.Hash, number uint64) *big.Int {
return nil return nil
} }
td := new(big.Int) td := new(big.Int)
if err := rlp.Decode(bytes.NewReader(data), td); err != nil { if err := rlp.DecodeBytes(data, td); err != nil {
log.Error("Invalid block total difficulty RLP", "hash", hash, "err", err) log.Error("Invalid block total difficulty RLP", "hash", hash, "err", err)
return nil return nil
} }

@ -17,8 +17,6 @@
package rawdb package rawdb
import ( import (
"bytes"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
@ -53,7 +51,7 @@ func ReadSkeletonHeader(db ethdb.KeyValueReader, number uint64) *types.Header {
return nil return nil
} }
header := new(types.Header) header := new(types.Header)
if err := rlp.Decode(bytes.NewReader(data), header); err != nil { if err := rlp.DecodeBytes(data, header); err != nil {
log.Error("Invalid skeleton header RLP", "number", number, "err", err) log.Error("Invalid skeleton header RLP", "number", number, "err", err)
return nil return nil
} }

@ -112,7 +112,7 @@ func (it *nodeIterator) step() error {
} }
// Otherwise we've reached an account node, initiate data iteration // Otherwise we've reached an account node, initiate data iteration
var account types.StateAccount var account types.StateAccount
if err := rlp.Decode(bytes.NewReader(it.stateIt.LeafBlob()), &account); err != nil { if err := rlp.DecodeBytes(it.stateIt.LeafBlob(), &account); err != nil {
return err return err
} }
// Lookup the preimage of account hash // Lookup the preimage of account hash

@ -17,8 +17,6 @@
package state package state
import ( import (
"bytes"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
@ -45,7 +43,7 @@ func NewStateSync(root common.Hash, database ethdb.KeyValueReader, onLeaf func(k
} }
} }
var obj types.StateAccount var obj types.StateAccount
if err := rlp.Decode(bytes.NewReader(leaf), &obj); err != nil { if err := rlp.DecodeBytes(leaf, &obj); err != nil {
return err return err
} }
syncer.AddSubTrie(obj.Root, path, parent, parentPath, onSlot) syncer.AddSubTrie(obj.Root, path, parent, parentPath, onSlot)

@ -214,7 +214,7 @@ func TestEIP2718TransactionEncode(t *testing.T) {
func decodeTx(data []byte) (*Transaction, error) { func decodeTx(data []byte) (*Transaction, error) {
var tx Transaction var tx Transaction
t, err := &tx, rlp.Decode(bytes.NewReader(data), &tx) t, err := &tx, rlp.DecodeBytes(data, &tx)
return t, err return t, err
} }

@ -18,7 +18,6 @@ package tracers
import ( import (
"bufio" "bufio"
"bytes"
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
@ -453,7 +452,7 @@ func (api *API) TraceBlockByHash(ctx context.Context, hash common.Hash, config *
// and returns them as a JSON object. // and returns them as a JSON object.
func (api *API) TraceBlock(ctx context.Context, blob hexutil.Bytes, config *TraceConfig) ([]*txTraceResult, error) { func (api *API) TraceBlock(ctx context.Context, blob hexutil.Bytes, config *TraceConfig) ([]*txTraceResult, error) {
block := new(types.Block) block := new(types.Block)
if err := rlp.Decode(bytes.NewReader(blob), block); err != nil { if err := rlp.DecodeBytes(blob, block); err != nil {
return nil, fmt.Errorf("could not decode block: %v", err) return nil, fmt.Errorf("could not decode block: %v", err)
} }
return api.traceBlock(ctx, block, config) return api.traceBlock(ctx, block, config)

@ -17,7 +17,6 @@
package light package light
import ( import (
"bytes"
"context" "context"
"errors" "errors"
"math/big" "math/big"
@ -126,7 +125,7 @@ func GetBody(ctx context.Context, odr OdrBackend, hash common.Hash, number uint6
return nil, err return nil, err
} }
body := new(types.Body) body := new(types.Body)
if err := rlp.Decode(bytes.NewReader(data), body); err != nil { if err := rlp.DecodeBytes(data, body); err != nil {
return nil, err return nil, err
} }
return body, nil return body, nil

@ -238,6 +238,8 @@ func Decode(input []byte) (Packet, Pubkey, []byte, error) {
default: default:
return nil, fromKey, hash, fmt.Errorf("unknown type: %d", ptype) return nil, fromKey, hash, fmt.Errorf("unknown type: %d", ptype)
} }
// Here we use NewStream to allow for additional data after the first
// RLP object (forward-compatibility).
s := rlp.NewStream(bytes.NewReader(sigdata[1:]), 0) s := rlp.NewStream(bytes.NewReader(sigdata[1:]), 0)
err = s.Decode(req) err = s.Decode(req)
return req, fromKey, hash, err return req, fromKey, hash, err

@ -282,7 +282,7 @@ func TestSignTx(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
parsedTx := &types.Transaction{} parsedTx := &types.Transaction{}
rlp.Decode(bytes.NewReader(res.Raw), parsedTx) rlp.DecodeBytes(res.Raw, parsedTx)
//The tx should NOT be modified by the UI //The tx should NOT be modified by the UI
if parsedTx.Value().Cmp(tx.Value.ToInt()) != 0 { if parsedTx.Value().Cmp(tx.Value.ToInt()) != 0 {
@ -308,7 +308,7 @@ func TestSignTx(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
parsedTx2 := &types.Transaction{} parsedTx2 := &types.Transaction{}
rlp.Decode(bytes.NewReader(res.Raw), parsedTx2) rlp.DecodeBytes(res.Raw, parsedTx2)
//The tx should be modified by the UI //The tx should be modified by the UI
if parsedTx2.Value().Cmp(tx.Value.ToInt()) != 0 { if parsedTx2.Value().Cmp(tx.Value.ToInt()) != 0 {