Skip to content

Commit 9b46986

Browse files
authored
all: use rlp.DecodeBytes instead of rlp.Decode where possible (ethereum#27994)
1 parent 60ec41c commit 9b46986

File tree

9 files changed

+13
-17
lines changed

9 files changed

+13
-17
lines changed

core/rawdb/accessors_chain.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ func ReadHeader(db ethdb.Reader, hash common.Hash, number uint64) *types.Header
381381
return nil
382382
}
383383
header := new(types.Header)
384-
if err := rlp.Decode(bytes.NewReader(data), header); err != nil {
384+
if err := rlp.DecodeBytes(data, header); err != nil {
385385
log.Error("Invalid block header RLP", "hash", hash, "err", err)
386386
return nil
387387
}
@@ -498,7 +498,7 @@ func ReadBody(db ethdb.Reader, hash common.Hash, number uint64) *types.Body {
498498
return nil
499499
}
500500
body := new(types.Body)
501-
if err := rlp.Decode(bytes.NewReader(data), body); err != nil {
501+
if err := rlp.DecodeBytes(data, body); err != nil {
502502
log.Error("Invalid block body RLP", "hash", hash, "err", err)
503503
return nil
504504
}
@@ -544,7 +544,7 @@ func ReadTd(db ethdb.Reader, hash common.Hash, number uint64) *big.Int {
544544
return nil
545545
}
546546
td := new(big.Int)
547-
if err := rlp.Decode(bytes.NewReader(data), td); err != nil {
547+
if err := rlp.DecodeBytes(data, td); err != nil {
548548
log.Error("Invalid block total difficulty RLP", "hash", hash, "err", err)
549549
return nil
550550
}

core/rawdb/accessors_sync.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
package rawdb
1818

1919
import (
20-
"bytes"
21-
2220
"github.com/ethereum/go-ethereum/core/types"
2321
"github.com/ethereum/go-ethereum/ethdb"
2422
"github.com/ethereum/go-ethereum/log"
@@ -53,7 +51,7 @@ func ReadSkeletonHeader(db ethdb.KeyValueReader, number uint64) *types.Header {
5351
return nil
5452
}
5553
header := new(types.Header)
56-
if err := rlp.Decode(bytes.NewReader(data), header); err != nil {
54+
if err := rlp.DecodeBytes(data, header); err != nil {
5755
log.Error("Invalid skeleton header RLP", "number", number, "err", err)
5856
return nil
5957
}

core/state/iterator.go

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

core/state/sync.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
package state
1818

1919
import (
20-
"bytes"
21-
2220
"github.com/ethereum/go-ethereum/common"
2321
"github.com/ethereum/go-ethereum/core/types"
2422
"github.com/ethereum/go-ethereum/ethdb"
@@ -45,7 +43,7 @@ func NewStateSync(root common.Hash, database ethdb.KeyValueReader, onLeaf func(k
4543
}
4644
}
4745
var obj types.StateAccount
48-
if err := rlp.Decode(bytes.NewReader(leaf), &obj); err != nil {
46+
if err := rlp.DecodeBytes(leaf, &obj); err != nil {
4947
return err
5048
}
5149
syncer.AddSubTrie(obj.Root, path, parent, parentPath, onSlot)

core/types/transaction_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func TestEIP2718TransactionEncode(t *testing.T) {
214214

215215
func decodeTx(data []byte) (*Transaction, error) {
216216
var tx Transaction
217-
t, err := &tx, rlp.Decode(bytes.NewReader(data), &tx)
217+
t, err := &tx, rlp.DecodeBytes(data, &tx)
218218
return t, err
219219
}
220220

eth/tracers/api.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package tracers
1818

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

light/odr_util.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package light
1818

1919
import (
20-
"bytes"
2120
"context"
2221
"errors"
2322
"math/big"
@@ -126,7 +125,7 @@ func GetBody(ctx context.Context, odr OdrBackend, hash common.Hash, number uint6
126125
return nil, err
127126
}
128127
body := new(types.Body)
129-
if err := rlp.Decode(bytes.NewReader(data), body); err != nil {
128+
if err := rlp.DecodeBytes(data, body); err != nil {
130129
return nil, err
131130
}
132131
return body, nil

p2p/discover/v4wire/v4wire.go

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

signer/core/api_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ func TestSignTx(t *testing.T) {
282282
t.Fatal(err)
283283
}
284284
parsedTx := &types.Transaction{}
285-
rlp.Decode(bytes.NewReader(res.Raw), parsedTx)
285+
rlp.DecodeBytes(res.Raw, parsedTx)
286286

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

313313
//The tx should be modified by the UI
314314
if parsedTx2.Value().Cmp(tx.Value.ToInt()) != 0 {

0 commit comments

Comments
 (0)