github.com/gochain-io/gochain@v2.2.26+incompatible/core/types/receipt_test.go (about)

     1  package types
     2  
     3  import (
     4  	"bytes"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/gochain-io/gochain/common"
     9  	"github.com/gochain-io/gochain/rlp"
    10  )
    11  
    12  func TestReceiptsForStorage_EncodeRLP(t *testing.T) {
    13  	receipt1 := &Receipt{
    14  		Status:            ReceiptStatusFailed,
    15  		CumulativeGasUsed: 1,
    16  		Logs: []*Log{
    17  			{Address: common.BytesToAddress([]byte{0x11})},
    18  			{Address: common.BytesToAddress([]byte{0x01, 0x11})},
    19  		},
    20  		TxHash:          common.BytesToHash([]byte{0x11, 0x11}),
    21  		ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
    22  		GasUsed:         111111,
    23  	}
    24  	receipt2 := &Receipt{
    25  		PostState:         common.Hash{2}.Bytes(),
    26  		CumulativeGasUsed: 2,
    27  		Logs: []*Log{
    28  			{Address: common.BytesToAddress([]byte{0x22})},
    29  			{Address: common.BytesToAddress([]byte{0x02, 0x22})},
    30  		},
    31  		TxHash:          common.BytesToHash([]byte{0x22, 0x22}),
    32  		ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}),
    33  		GasUsed:         222222,
    34  	}
    35  
    36  	normal := []*ReceiptForStorage{
    37  		(*ReceiptForStorage)(receipt1),
    38  		(*ReceiptForStorage)(receipt2),
    39  	}
    40  	normalBytes, err := rlp.EncodeToBytes(normal)
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  
    45  	custom := ReceiptsForStorage{receipt1, receipt2}
    46  	customBytes, err := rlp.EncodeToBytes(custom)
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	if !bytes.Equal(normalBytes, customBytes) {
    52  		t.Errorf("expected %x but got %x", normalBytes, customBytes)
    53  	}
    54  
    55  	var normalDec []*ReceiptForStorage
    56  	if err := rlp.DecodeBytes(normalBytes, &normalDec); err != nil {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	var customDec ReceiptsForStorage
    61  	if err := rlp.DecodeBytes(normalBytes, &customDec); err != nil {
    62  		t.Fatal(err)
    63  	}
    64  	if len(normalDec) != len(customDec) {
    65  		t.Errorf("expected %v but got %v", normalDec, customDec)
    66  	} else {
    67  		for i := range normalDec {
    68  			normal := (*Receipt)(normalDec[i])
    69  			custom := customDec[i]
    70  			if !reflect.DeepEqual(*normal, *custom) {
    71  				t.Errorf("expected %v but got %v", normalDec, customDec)
    72  				break
    73  			}
    74  		}
    75  	}
    76  
    77  }