forked from mirror/go-ethereum
ChrisChinchilla-patch-3
parent
1528b791ac
commit
6b0ddd141e
@ -0,0 +1,133 @@ |
||||
// Copyright 2019 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package types |
||||
|
||||
import ( |
||||
"bytes" |
||||
"encoding/hex" |
||||
"encoding/json" |
||||
"math/big" |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/rlp" |
||||
) |
||||
|
||||
func TestLegacyReceiptDecoding(t *testing.T) { |
||||
tests := []struct { |
||||
name string |
||||
encode func(*Receipt) ([]byte, error) |
||||
}{ |
||||
{ |
||||
"StoredReceiptRLP", |
||||
encodeAsStoredReceiptRLP, |
||||
}, |
||||
{ |
||||
"V4StoredReceiptRLP", |
||||
encodeAsV4StoredReceiptRLP, |
||||
}, |
||||
{ |
||||
"LegacyStoredReceiptRLP", |
||||
encodeAsLegacyStoredReceiptRLP, |
||||
}, |
||||
} |
||||
|
||||
tx := NewTransaction(1, common.HexToAddress("0x1"), big.NewInt(1), 1, big.NewInt(1), nil) |
||||
receipt := &Receipt{ |
||||
Status: ReceiptStatusFailed, |
||||
CumulativeGasUsed: 1, |
||||
Logs: []*Log{ |
||||
{Address: common.BytesToAddress([]byte{0x11})}, |
||||
{Address: common.BytesToAddress([]byte{0x01, 0x11})}, |
||||
}, |
||||
TxHash: tx.Hash(), |
||||
ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}), |
||||
GasUsed: 111111, |
||||
} |
||||
receipt.Bloom = CreateBloom(Receipts{receipt}) |
||||
|
||||
want, err := json.Marshal(receipt) |
||||
if err != nil { |
||||
t.Fatalf("Error encoding reference receipt to JSON: %v", err) |
||||
} |
||||
|
||||
for _, tc := range tests { |
||||
t.Run(tc.name, func(t *testing.T) { |
||||
enc, err := tc.encode(receipt) |
||||
if err != nil { |
||||
t.Fatalf("Error encoding receipt: %v", err) |
||||
} |
||||
|
||||
var dec ReceiptForStorage |
||||
if err := rlp.DecodeBytes(enc, &dec); err != nil { |
||||
t.Fatalf("Error decoding RLP receipt: %v", err) |
||||
} |
||||
|
||||
have, err := rlp.EncodeToBytes((*Receipt)(receipt)) |
||||
if err != nil { |
||||
t.Fatalf("Error encoding receipt: %v", err) |
||||
} |
||||
|
||||
if !bytes.Equal(have, want) { |
||||
t.Fatalf("receipt mismatch: have %s, want %s", hex.EncodeToString(have), hex.EncodeToString(want)) |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
|
||||
func encodeAsStoredReceiptRLP(want *Receipt) ([]byte, error) { |
||||
stored := &storedReceiptRLP{ |
||||
PostStateOrStatus: want.statusEncoding(), |
||||
CumulativeGasUsed: want.CumulativeGasUsed, |
||||
Logs: make([]*LogForStorage, len(want.Logs)), |
||||
} |
||||
for i, log := range want.Logs { |
||||
stored.Logs[i] = (*LogForStorage)(log) |
||||
} |
||||
return rlp.EncodeToBytes(stored) |
||||
} |
||||
|
||||
func encodeAsV4StoredReceiptRLP(want *Receipt) ([]byte, error) { |
||||
stored := &v4StoredReceiptRLP{ |
||||
PostStateOrStatus: want.statusEncoding(), |
||||
CumulativeGasUsed: want.CumulativeGasUsed, |
||||
TxHash: want.TxHash, |
||||
ContractAddress: want.ContractAddress, |
||||
Logs: make([]*LogForStorage, len(want.Logs)), |
||||
GasUsed: want.GasUsed, |
||||
} |
||||
for i, log := range want.Logs { |
||||
stored.Logs[i] = (*LogForStorage)(log) |
||||
} |
||||
return rlp.EncodeToBytes(stored) |
||||
} |
||||
|
||||
func encodeAsLegacyStoredReceiptRLP(want *Receipt) ([]byte, error) { |
||||
stored := &legacyStoredReceiptRLP{ |
||||
PostStateOrStatus: want.statusEncoding(), |
||||
CumulativeGasUsed: want.CumulativeGasUsed, |
||||
Bloom: want.Bloom, |
||||
TxHash: want.TxHash, |
||||
ContractAddress: want.ContractAddress, |
||||
Logs: make([]*LogForStorage, len(want.Logs)), |
||||
GasUsed: want.GasUsed, |
||||
} |
||||
for i, log := range want.Logs { |
||||
stored.Logs[i] = (*LogForStorage)(log) |
||||
} |
||||
return rlp.EncodeToBytes(stored) |
||||
} |
Loading…
Reference in new issue