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

pull/28001/head
Felix Lange 1 year ago committed by GitHub
parent 60ec41ce73
commit 9b46986edc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      core/rawdb/accessors_chain.go
  2. 4
      core/rawdb/accessors_sync.go
  3. 2
      core/state/iterator.go
  4. 4
      core/state/sync.go
  5. 2
      core/types/transaction_test.go
  6. 3
      eth/tracers/api.go
  7. 3
      light/odr_util.go
  8. 2
      p2p/discover/v4wire/v4wire.go
  9. 4
      signer/core/api_test.go

@ -381,7 +381,7 @@ func ReadHeader(db ethdb.Reader, hash common.Hash, number uint64) *types.Header
return nil
}
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)
return nil
}
@ -498,7 +498,7 @@ func ReadBody(db ethdb.Reader, hash common.Hash, number uint64) *types.Body {
return nil
}
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)
return nil
}
@ -544,7 +544,7 @@ func ReadTd(db ethdb.Reader, hash common.Hash, number uint64) *big.Int {
return nil
}
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)
return nil
}

@ -17,8 +17,6 @@
package rawdb
import (
"bytes"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
@ -53,7 +51,7 @@ func ReadSkeletonHeader(db ethdb.KeyValueReader, number uint64) *types.Header {
return nil
}
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)
return nil
}

@ -112,7 +112,7 @@ func (it *nodeIterator) step() error {
}
// Otherwise we've reached an account node, initiate data iteration
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
}
// Lookup the preimage of account hash

@ -17,8 +17,6 @@
package state
import (
"bytes"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"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
if err := rlp.Decode(bytes.NewReader(leaf), &obj); err != nil {
if err := rlp.DecodeBytes(leaf, &obj); err != nil {
return err
}
syncer.AddSubTrie(obj.Root, path, parent, parentPath, onSlot)

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

@ -18,7 +18,6 @@ package tracers
import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
@ -453,7 +452,7 @@ func (api *API) TraceBlockByHash(ctx context.Context, hash common.Hash, config *
// and returns them as a JSON object.
func (api *API) TraceBlock(ctx context.Context, blob hexutil.Bytes, config *TraceConfig) ([]*txTraceResult, error) {
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 api.traceBlock(ctx, block, config)

@ -17,7 +17,6 @@
package light
import (
"bytes"
"context"
"errors"
"math/big"
@ -126,7 +125,7 @@ func GetBody(ctx context.Context, odr OdrBackend, hash common.Hash, number uint6
return nil, err
}
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 body, nil

@ -238,6 +238,8 @@ func Decode(input []byte) (Packet, Pubkey, []byte, error) {
default:
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)
err = s.Decode(req)
return req, fromKey, hash, err

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

Loading…
Cancel
Save