mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
Revert "core/types: transaction and receipt encoding/decoding optimizations (#27976)"
This reverts commit b92d89681f.
This commit is contained in:
parent
fda7577be1
commit
a4755450b5
6 changed files with 53 additions and 58 deletions
|
|
@ -18,12 +18,12 @@ func (l Log) MarshalJSON() ([]byte, error) {
|
|||
Address common.Address `json:"address" gencodec:"required"`
|
||||
Topics []common.Hash `json:"topics" gencodec:"required"`
|
||||
Data hexutil.Bytes `json:"data" gencodec:"required"`
|
||||
BlockNumber hexutil.Uint64 `json:"blockNumber" rlp:"-"`
|
||||
TxHash common.Hash `json:"transactionHash" gencodec:"required" rlp:"-"`
|
||||
TxIndex hexutil.Uint `json:"transactionIndex" rlp:"-"`
|
||||
BlockHash common.Hash `json:"blockHash" rlp:"-"`
|
||||
Index hexutil.Uint `json:"logIndex" rlp:"-"`
|
||||
Removed bool `json:"removed" rlp:"-"`
|
||||
BlockNumber hexutil.Uint64 `json:"blockNumber"`
|
||||
TxHash common.Hash `json:"transactionHash" gencodec:"required"`
|
||||
TxIndex hexutil.Uint `json:"transactionIndex"`
|
||||
BlockHash common.Hash `json:"blockHash"`
|
||||
Index hexutil.Uint `json:"logIndex"`
|
||||
Removed bool `json:"removed"`
|
||||
}
|
||||
var enc Log
|
||||
enc.Address = l.Address
|
||||
|
|
@ -44,12 +44,12 @@ func (l *Log) UnmarshalJSON(input []byte) error {
|
|||
Address *common.Address `json:"address" gencodec:"required"`
|
||||
Topics []common.Hash `json:"topics" gencodec:"required"`
|
||||
Data *hexutil.Bytes `json:"data" gencodec:"required"`
|
||||
BlockNumber *hexutil.Uint64 `json:"blockNumber" rlp:"-"`
|
||||
TxHash *common.Hash `json:"transactionHash" gencodec:"required" rlp:"-"`
|
||||
TxIndex *hexutil.Uint `json:"transactionIndex" rlp:"-"`
|
||||
BlockHash *common.Hash `json:"blockHash" rlp:"-"`
|
||||
Index *hexutil.Uint `json:"logIndex" rlp:"-"`
|
||||
Removed *bool `json:"removed" rlp:"-"`
|
||||
BlockNumber *hexutil.Uint64 `json:"blockNumber"`
|
||||
TxHash *common.Hash `json:"transactionHash" gencodec:"required"`
|
||||
TxIndex *hexutil.Uint `json:"transactionIndex"`
|
||||
BlockHash *common.Hash `json:"blockHash"`
|
||||
Index *hexutil.Uint `json:"logIndex"`
|
||||
Removed *bool `json:"removed"`
|
||||
}
|
||||
var dec Log
|
||||
if err := json.Unmarshal(input, &dec); err != nil {
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ package types
|
|||
import "github.com/ethereum/go-ethereum/rlp"
|
||||
import "io"
|
||||
|
||||
func (obj *Log) EncodeRLP(_w io.Writer) error {
|
||||
func (obj *rlpLog) EncodeRLP(_w io.Writer) error {
|
||||
w := rlp.NewEncoderBuffer(_w)
|
||||
_tmp0 := w.List()
|
||||
w.WriteBytes(obj.Address[:])
|
||||
|
|
|
|||
|
|
@ -18,8 +18,6 @@ package types
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"math"
|
||||
"sync"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
|
|
@ -38,22 +36,6 @@ var encodeBufferPool = sync.Pool{
|
|||
New: func() interface{} { return new(bytes.Buffer) },
|
||||
}
|
||||
|
||||
// getPooledBuffer retrieves a buffer from the pool and creates a byte slice of the
|
||||
// requested size from it.
|
||||
//
|
||||
// The caller should return the *bytes.Buffer object back into encodeBufferPool after use!
|
||||
// The returned byte slice must not be used after returning the buffer.
|
||||
func getPooledBuffer(size uint64) ([]byte, *bytes.Buffer, error) {
|
||||
if size > math.MaxInt {
|
||||
return nil, nil, fmt.Errorf("can't get buffer of size %d", size)
|
||||
}
|
||||
buf := encodeBufferPool.Get().(*bytes.Buffer)
|
||||
buf.Reset()
|
||||
buf.Grow(int(size))
|
||||
b := buf.Bytes()[:int(size)]
|
||||
return b, buf, nil
|
||||
}
|
||||
|
||||
// rlpHash encodes x and hashes the encoded bytes.
|
||||
func rlpHash(x interface{}) (h common.Hash) {
|
||||
sha := hasherPool.Get().(crypto.KeccakState)
|
||||
|
|
|
|||
|
|
@ -17,11 +17,13 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
"github.com/ethereum/go-ethereum/rlp"
|
||||
)
|
||||
|
||||
//go:generate go run ../../rlp/rlpgen -type Log -out gen_log_rlp.go
|
||||
//go:generate go run github.com/fjl/gencodec -type Log -field-override logMarshaling -out gen_log_json.go
|
||||
|
||||
// Log represents a contract log event. These events are generated by the LOG opcode and
|
||||
|
|
@ -38,19 +40,19 @@ type Log struct {
|
|||
// Derived fields. These fields are filled in by the node
|
||||
// but not secured by consensus.
|
||||
// block in which the transaction was included
|
||||
BlockNumber uint64 `json:"blockNumber" rlp:"-"`
|
||||
BlockNumber uint64 `json:"blockNumber"`
|
||||
// hash of the transaction
|
||||
TxHash common.Hash `json:"transactionHash" gencodec:"required" rlp:"-"`
|
||||
TxHash common.Hash `json:"transactionHash" gencodec:"required"`
|
||||
// index of the transaction in the block
|
||||
TxIndex uint `json:"transactionIndex" rlp:"-"`
|
||||
TxIndex uint `json:"transactionIndex"`
|
||||
// hash of the block in which the transaction was included
|
||||
BlockHash common.Hash `json:"blockHash" rlp:"-"`
|
||||
BlockHash common.Hash `json:"blockHash"`
|
||||
// index of the log in the block
|
||||
Index uint `json:"logIndex" rlp:"-"`
|
||||
Index uint `json:"logIndex"`
|
||||
|
||||
// The Removed field is true if this log was reverted due to a chain reorganisation.
|
||||
// You must pay attention to this field if you receive logs through a filter query.
|
||||
Removed bool `json:"removed" rlp:"-"`
|
||||
Removed bool `json:"removed"`
|
||||
}
|
||||
|
||||
type logMarshaling struct {
|
||||
|
|
@ -59,3 +61,28 @@ type logMarshaling struct {
|
|||
TxIndex hexutil.Uint
|
||||
Index hexutil.Uint
|
||||
}
|
||||
|
||||
//go:generate go run ../../rlp/rlpgen -type rlpLog -out gen_log_rlp.go
|
||||
|
||||
// rlpLog is used to RLP-encode both the consensus and storage formats.
|
||||
type rlpLog struct {
|
||||
Address common.Address
|
||||
Topics []common.Hash
|
||||
Data []byte
|
||||
}
|
||||
|
||||
// EncodeRLP implements rlp.Encoder.
|
||||
func (l *Log) EncodeRLP(w io.Writer) error {
|
||||
rl := rlpLog{Address: l.Address, Topics: l.Topics, Data: l.Data}
|
||||
return rlp.Encode(w, &rl)
|
||||
}
|
||||
|
||||
// DecodeRLP implements rlp.Decoder.
|
||||
func (l *Log) DecodeRLP(s *rlp.Stream) error {
|
||||
var dec rlpLog
|
||||
err := s.Decode(&dec)
|
||||
if err == nil {
|
||||
l.Address, l.Topics, l.Data = dec.Address, dec.Topics, dec.Data
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -153,7 +153,7 @@ func (r *Receipt) MarshalBinary() ([]byte, error) {
|
|||
// DecodeRLP implements rlp.Decoder, and loads the consensus fields of a receipt
|
||||
// from an RLP stream.
|
||||
func (r *Receipt) DecodeRLP(s *rlp.Stream) error {
|
||||
kind, size, err := s.Kind()
|
||||
kind, _, err := s.Kind()
|
||||
switch {
|
||||
case err != nil:
|
||||
return err
|
||||
|
|
@ -165,18 +165,12 @@ func (r *Receipt) DecodeRLP(s *rlp.Stream) error {
|
|||
}
|
||||
r.Type = LegacyTxType
|
||||
return r.setFromRLP(dec)
|
||||
case kind == rlp.Byte:
|
||||
return errShortTypedReceipt
|
||||
default:
|
||||
// It's an EIP-2718 typed tx receipt.
|
||||
b, buf, err := getPooledBuffer(size)
|
||||
b, err := s.Bytes()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer encodeBufferPool.Put(buf)
|
||||
if err := s.ReadBytes(b); err != nil {
|
||||
return err
|
||||
}
|
||||
return r.decodeTyped(b)
|
||||
}
|
||||
}
|
||||
|
|
@ -270,7 +264,7 @@ func (r *ReceiptForStorage) EncodeRLP(_w io.Writer) error {
|
|||
w.WriteUint64(r.CumulativeGasUsed)
|
||||
logList := w.List()
|
||||
for _, log := range r.Logs {
|
||||
if err := log.EncodeRLP(w); err != nil {
|
||||
if err := rlp.Encode(w, log); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -145,23 +145,15 @@ func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
|
|||
tx.setDecoded(&inner, rlp.ListSize(size))
|
||||
}
|
||||
return err
|
||||
case kind == rlp.Byte:
|
||||
return errShortTypedTx
|
||||
default:
|
||||
// It's an EIP-2718 typed TX envelope.
|
||||
// First read the tx payload bytes into a temporary buffer.
|
||||
b, buf, err := getPooledBuffer(size)
|
||||
if err != nil {
|
||||
var b []byte
|
||||
if b, err = s.Bytes(); err != nil {
|
||||
return err
|
||||
}
|
||||
defer encodeBufferPool.Put(buf)
|
||||
if err := s.ReadBytes(b); err != nil {
|
||||
return err
|
||||
}
|
||||
// Now decode the inner transaction.
|
||||
inner, err := tx.decodeTyped(b)
|
||||
if err == nil {
|
||||
tx.setDecoded(inner, size)
|
||||
tx.setDecoded(inner, uint64(len(b)))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue