diff --git a/core/types/receipt.go b/core/types/receipt.go index 397026a0d11d2..6c89d2699a5c4 100644 --- a/core/types/receipt.go +++ b/core/types/receipt.go @@ -40,6 +40,9 @@ var ( var errShortTypedReceipt = errors.New("typed receipt too short") +// This error is returned when a typed receipt is decoded, but the string is empty. +var errEmptyTypedReceipt = errors.New("empty typed receipt bytes") + const ( // ReceiptStatusFailed is the status code of a transaction if execution failed. ReceiptStatusFailed = uint64(0) @@ -143,29 +146,13 @@ func (r *Receipt) EncodeRLP(w io.Writer) error { buf := encodeBufferPool.Get().(*bytes.Buffer) defer encodeBufferPool.Put(buf) buf.Reset() - if err := r.encodeTyped(data, buf); err != nil { + buf.WriteByte(r.Type) + if err := rlp.Encode(buf, data); err != nil { return err } return rlp.Encode(w, buf.Bytes()) } -// encodeTyped writes the canonical encoding of a typed receipt to w. -func (r *Receipt) encodeTyped(data *receiptRLP, w *bytes.Buffer) error { - w.WriteByte(r.Type) - return rlp.Encode(w, data) -} - -// MarshalBinary returns the consensus encoding of the receipt. -func (r *Receipt) MarshalBinary() ([]byte, error) { - if r.Type == LegacyTxType { - return rlp.EncodeToBytes(r) - } - data := &receiptRLP{r.statusEncoding(), r.CumulativeGasUsed, r.Bloom, r.Logs} - var buf bytes.Buffer - err := r.encodeTyped(data, &buf) - return buf.Bytes(), err -} - // DecodeRLP implements rlp.Decoder, and loads the consensus fields of a receipt // from an RLP stream. func (r *Receipt) DecodeRLP(s *rlp.Stream) error { @@ -191,27 +178,10 @@ func (r *Receipt) DecodeRLP(s *rlp.Stream) error { } } -// UnmarshalBinary decodes the consensus encoding of receipts. -// It supports legacy RLP receipts and EIP-2718 typed receipts. -func (r *Receipt) UnmarshalBinary(b []byte) error { - if len(b) > 0 && b[0] > 0x7f { - // It's a legacy receipt decode the RLP - var data receiptRLP - err := rlp.DecodeBytes(b, &data) - if err != nil { - return err - } - r.Type = LegacyTxType - return r.setFromRLP(data) - } - // It's an EIP2718 typed transaction envelope. - return r.decodeTyped(b) -} - // decodeTyped decodes a typed receipt from the canonical format. func (r *Receipt) decodeTyped(b []byte) error { - if len(b) <= 1 { - return errShortTypedReceipt + if len(b) == 0 { + return errEmptyTypedReceipt } switch b[0] { case DynamicFeeTxType, AccessListTxType: @@ -396,42 +366,42 @@ func (rs Receipts) EncodeIndex(i int, w *bytes.Buffer) { // DeriveFields fills the receipts with their computed fields based on consensus // data and contextual infos like containing block and transactions. -func (rs Receipts) DeriveFields(config *params.ChainConfig, hash common.Hash, number uint64, txs Transactions) error { +func (r Receipts) DeriveFields(config *params.ChainConfig, hash common.Hash, number uint64, txs Transactions) error { signer := MakeSigner(config, new(big.Int).SetUint64(number)) logIndex := uint(0) - if len(txs) != len(rs) { + if len(txs) != len(r) { return errors.New("transaction and receipt count mismatch") } - for i := 0; i < len(rs); i++ { + for i := 0; i < len(r); i++ { // The transaction type and hash can be retrieved from the transaction itself - rs[i].Type = txs[i].Type() - rs[i].TxHash = txs[i].Hash() + r[i].Type = txs[i].Type() + r[i].TxHash = txs[i].Hash() // block location fields - rs[i].BlockHash = hash - rs[i].BlockNumber = new(big.Int).SetUint64(number) - rs[i].TransactionIndex = uint(i) + r[i].BlockHash = hash + r[i].BlockNumber = new(big.Int).SetUint64(number) + r[i].TransactionIndex = uint(i) // The contract address can be derived from the transaction itself if txs[i].To() == nil { // Deriving the signer is expensive, only do if it's actually needed from, _ := Sender(signer, txs[i]) - rs[i].ContractAddress = crypto.CreateAddress(from, txs[i].Nonce()) + r[i].ContractAddress = crypto.CreateAddress(from, txs[i].Nonce()) } // The used gas can be calculated based on previous r if i == 0 { - rs[i].GasUsed = rs[i].CumulativeGasUsed + r[i].GasUsed = r[i].CumulativeGasUsed } else { - rs[i].GasUsed = rs[i].CumulativeGasUsed - rs[i-1].CumulativeGasUsed + r[i].GasUsed = r[i].CumulativeGasUsed - r[i-1].CumulativeGasUsed } // The derived log fields can simply be set from the block and transaction - for j := 0; j < len(rs[i].Logs); j++ { - rs[i].Logs[j].BlockNumber = number - rs[i].Logs[j].BlockHash = hash - rs[i].Logs[j].TxHash = rs[i].TxHash - rs[i].Logs[j].TxIndex = uint(i) - rs[i].Logs[j].Index = logIndex + for j := 0; j < len(r[i].Logs); j++ { + r[i].Logs[j].BlockNumber = number + r[i].Logs[j].BlockHash = hash + r[i].Logs[j].TxHash = r[i].TxHash + r[i].Logs[j].TxIndex = uint(i) + r[i].Logs[j].Index = logIndex logIndex++ } } diff --git a/core/types/receipt_test.go b/core/types/receipt_test.go index f9f1e30e58b50..b34c3db7e550a 100644 --- a/core/types/receipt_test.go +++ b/core/types/receipt_test.go @@ -29,59 +29,6 @@ import ( "github.com/blockcypher/go-ethereum/rlp" ) -var ( - legacyReceipt = &Receipt{ - Status: ReceiptStatusFailed, - CumulativeGasUsed: 1, - Logs: []*Log{ - { - Address: common.BytesToAddress([]byte{0x11}), - Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")}, - Data: []byte{0x01, 0x00, 0xff}, - }, - { - Address: common.BytesToAddress([]byte{0x01, 0x11}), - Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")}, - Data: []byte{0x01, 0x00, 0xff}, - }, - }, - } - accessListReceipt = &Receipt{ - Status: ReceiptStatusFailed, - CumulativeGasUsed: 1, - Logs: []*Log{ - { - Address: common.BytesToAddress([]byte{0x11}), - Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")}, - Data: []byte{0x01, 0x00, 0xff}, - }, - { - Address: common.BytesToAddress([]byte{0x01, 0x11}), - Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")}, - Data: []byte{0x01, 0x00, 0xff}, - }, - }, - Type: AccessListTxType, - } - eip1559Receipt = &Receipt{ - Status: ReceiptStatusFailed, - CumulativeGasUsed: 1, - Logs: []*Log{ - { - Address: common.BytesToAddress([]byte{0x11}), - Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")}, - Data: []byte{0x01, 0x00, 0xff}, - }, - { - Address: common.BytesToAddress([]byte{0x01, 0x11}), - Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")}, - Data: []byte{0x01, 0x00, 0xff}, - }, - }, - Type: DynamicFeeTxType, - } -) - func TestDecodeEmptyTypedReceipt(t *testing.T) { input := []byte{0x80} var r Receipt @@ -365,105 +312,6 @@ func TestTypedReceiptEncodingDecoding(t *testing.T) { } } -func TestReceiptMarshalBinary(t *testing.T) { - // Legacy Receipt - legacyReceipt.Bloom = CreateBloom(Receipts{legacyReceipt}) - have, err := legacyReceipt.MarshalBinary() - if err != nil { - t.Fatalf("marshal binary error: %v", err) - } - legacyReceipts := Receipts{legacyReceipt} - buf := new(bytes.Buffer) - legacyReceipts.EncodeIndex(0, buf) - haveEncodeIndex := buf.Bytes() - if !bytes.Equal(have, haveEncodeIndex) { - t.Errorf("BinaryMarshal and EncodeIndex mismatch, got %x want %x", have, haveEncodeIndex) - } - buf.Reset() - if err := legacyReceipt.EncodeRLP(buf); err != nil { - t.Fatalf("encode rlp error: %v", err) - } - haveRLPEncode := buf.Bytes() - if !bytes.Equal(have, haveRLPEncode) { - t.Errorf("BinaryMarshal and EncodeRLP mismatch for legacy tx, got %x want %x", have, haveRLPEncode) - } - legacyWant := common.FromHex("f901c58001b9010000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000000000000000010000080000000000000000000004000000000000000000000000000040000000000000000000000000000800000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000f8bef85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100fff85d940000000000000000000000000000000000000111f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff") - if !bytes.Equal(have, legacyWant) { - t.Errorf("encoded RLP mismatch, got %x want %x", have, legacyWant) - } - - // 2930 Receipt - buf.Reset() - accessListReceipt.Bloom = CreateBloom(Receipts{accessListReceipt}) - have, err = accessListReceipt.MarshalBinary() - if err != nil { - t.Fatalf("marshal binary error: %v", err) - } - accessListReceipts := Receipts{accessListReceipt} - accessListReceipts.EncodeIndex(0, buf) - haveEncodeIndex = buf.Bytes() - if !bytes.Equal(have, haveEncodeIndex) { - t.Errorf("BinaryMarshal and EncodeIndex mismatch, got %x want %x", have, haveEncodeIndex) - } - accessListWant := common.FromHex("01f901c58001b9010000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000000000000000010000080000000000000000000004000000000000000000000000000040000000000000000000000000000800000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000f8bef85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100fff85d940000000000000000000000000000000000000111f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff") - if !bytes.Equal(have, accessListWant) { - t.Errorf("encoded RLP mismatch, got %x want %x", have, accessListWant) - } - - // 1559 Receipt - buf.Reset() - eip1559Receipt.Bloom = CreateBloom(Receipts{eip1559Receipt}) - have, err = eip1559Receipt.MarshalBinary() - if err != nil { - t.Fatalf("marshal binary error: %v", err) - } - eip1559Receipts := Receipts{eip1559Receipt} - eip1559Receipts.EncodeIndex(0, buf) - haveEncodeIndex = buf.Bytes() - if !bytes.Equal(have, haveEncodeIndex) { - t.Errorf("BinaryMarshal and EncodeIndex mismatch, got %x want %x", have, haveEncodeIndex) - } - eip1559Want := common.FromHex("02f901c58001b9010000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000000000000000010000080000000000000000000004000000000000000000000000000040000000000000000000000000000800000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000f8bef85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100fff85d940000000000000000000000000000000000000111f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff") - if !bytes.Equal(have, eip1559Want) { - t.Errorf("encoded RLP mismatch, got %x want %x", have, eip1559Want) - } -} - -func TestReceiptUnmarshalBinary(t *testing.T) { - // Legacy Receipt - legacyBinary := common.FromHex("f901c58001b9010000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000000000000000010000080000000000000000000004000000000000000000000000000040000000000000000000000000000800000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000f8bef85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100fff85d940000000000000000000000000000000000000111f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff") - gotLegacyReceipt := new(Receipt) - if err := gotLegacyReceipt.UnmarshalBinary(legacyBinary); err != nil { - t.Fatalf("unmarshal binary error: %v", err) - } - legacyReceipt.Bloom = CreateBloom(Receipts{legacyReceipt}) - if !reflect.DeepEqual(gotLegacyReceipt, legacyReceipt) { - t.Errorf("receipt unmarshalled from binary mismatch, got %v want %v", gotLegacyReceipt, legacyReceipt) - } - - // 2930 Receipt - accessListBinary := common.FromHex("01f901c58001b9010000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000000000000000010000080000000000000000000004000000000000000000000000000040000000000000000000000000000800000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000f8bef85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100fff85d940000000000000000000000000000000000000111f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff") - gotAccessListReceipt := new(Receipt) - if err := gotAccessListReceipt.UnmarshalBinary(accessListBinary); err != nil { - t.Fatalf("unmarshal binary error: %v", err) - } - accessListReceipt.Bloom = CreateBloom(Receipts{accessListReceipt}) - if !reflect.DeepEqual(gotAccessListReceipt, accessListReceipt) { - t.Errorf("receipt unmarshalled from binary mismatch, got %v want %v", gotAccessListReceipt, accessListReceipt) - } - - // 1559 Receipt - eip1559RctBinary := common.FromHex("02f901c58001b9010000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000000000000000010000080000000000000000000004000000000000000000000000000040000000000000000000000000000800000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000f8bef85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100fff85d940000000000000000000000000000000000000111f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff") - got1559Receipt := new(Receipt) - if err := got1559Receipt.UnmarshalBinary(eip1559RctBinary); err != nil { - t.Fatalf("unmarshal binary error: %v", err) - } - eip1559Receipt.Bloom = CreateBloom(Receipts{eip1559Receipt}) - if !reflect.DeepEqual(got1559Receipt, eip1559Receipt) { - t.Errorf("receipt unmarshalled from binary mismatch, got %v want %v", got1559Receipt, eip1559Receipt) - } -} - func clearComputedFieldsOnReceipts(t *testing.T, receipts Receipts) { t.Helper() diff --git a/graphql/graphql.go b/graphql/graphql.go index b9d6c19abc9df..09fc5a06b37ec 100644 --- a/graphql/graphql.go +++ b/graphql/graphql.go @@ -530,7 +530,7 @@ func (t *Transaction) RawReceipt(ctx context.Context) (hexutil.Bytes, error) { if err != nil || receipt == nil { return hexutil.Bytes{}, err } - return receipt.MarshalBinary() + return rlp.EncodeToBytes(receipt) } type BlockType int diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 34abdb30cd920..30b532ff171a6 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -24,7 +24,6 @@ import ( "strings" "time" - "github.com/davecgh/go-spew/spew" "github.com/blockcypher/go-ethereum/accounts" "github.com/blockcypher/go-ethereum/accounts/abi" "github.com/blockcypher/go-ethereum/accounts/keystore" @@ -45,6 +44,7 @@ import ( "github.com/blockcypher/go-ethereum/params" "github.com/blockcypher/go-ethereum/rlp" "github.com/blockcypher/go-ethereum/rpc" + "github.com/davecgh/go-spew/spew" "github.com/tyler-smith/go-bip39" ) @@ -730,10 +730,10 @@ func (s *BlockChainAPI) GetHeaderByHash(ctx context.Context, hash common.Hash) m } // GetBlockByNumber returns the requested canonical block. -// * When blockNr is -1 the chain head is returned. -// * When blockNr is -2 the pending chain head is returned. -// * When fullTx is true all transactions in the block are returned, otherwise -// only the transaction hash is returned. +// - When blockNr is -1 the chain head is returned. +// - When blockNr is -2 the pending chain head is returned. +// - When fullTx is true all transactions in the block are returned, otherwise +// only the transaction hash is returned. func (s *BlockChainAPI) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber, fullTx bool) (map[string]interface{}, error) { block, err := s.b.BlockByNumber(ctx, number) if block != nil && err == nil { @@ -1930,7 +1930,7 @@ func (api *DebugAPI) GetRawReceipts(ctx context.Context, blockNrOrHash rpc.Block } result := make([]hexutil.Bytes, len(receipts)) for i, receipt := range receipts { - b, err := receipt.MarshalBinary() + b, err := rlp.EncodeToBytes(receipt) if err != nil { return nil, err }