github.com/iotexproject/iotex-core@v1.14.1-rc1/action/receipt_test.go (about)

     1  // Copyright (c) 2019 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package action
     7  
     8  import (
     9  	"encoding/hex"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/iotexproject/go-pkgs/hash"
    15  )
    16  
    17  func newTestLog() *Log {
    18  	return &Log{
    19  		Address:     "1",
    20  		Data:        []byte("cd07d8a74179e032f030d9244"),
    21  		BlockHeight: 1,
    22  		ActionHash:  hash.ZeroHash256,
    23  		Index:       1,
    24  	}
    25  
    26  }
    27  
    28  func TestConvert(t *testing.T) {
    29  	require := require.New(t)
    30  
    31  	topics := []hash.Hash256{
    32  		hash.Hash256b([]byte("test")),
    33  		hash.Hash256b([]byte("Pacific")),
    34  		hash.Hash256b([]byte("Aleutian")),
    35  	}
    36  	testLog := newTestLog()
    37  	testLog.Topics = topics
    38  	testLog.NotFixTopicCopyBug = true
    39  	receipt := &Receipt{
    40  		Status:             1,
    41  		BlockHeight:        1,
    42  		ActionHash:         hash.ZeroHash256,
    43  		GasConsumed:        1,
    44  		ContractAddress:    "test",
    45  		TxIndex:            1,
    46  		logs:               []*Log{testLog},
    47  		executionRevertMsg: "balance not enough",
    48  	}
    49  
    50  	typeReceipt := receipt.ConvertToReceiptPb()
    51  	require.NotNil(typeReceipt)
    52  	receipt2 := &Receipt{}
    53  	receipt2.ConvertFromReceiptPb(typeReceipt)
    54  	require.Equal(receipt.Status, receipt2.Status)
    55  	require.Equal(receipt.BlockHeight, receipt2.BlockHeight)
    56  	require.Equal(receipt.ActionHash, receipt2.ActionHash)
    57  	require.Equal(receipt.GasConsumed, receipt2.GasConsumed)
    58  	require.Equal(receipt.ContractAddress, receipt2.ContractAddress)
    59  	require.Equal(receipt.TxIndex, receipt2.TxIndex)
    60  	require.Equal(receipt.executionRevertMsg, receipt2.executionRevertMsg)
    61  	// block earlier than AleutianHeight overwrites all topics with last topic data
    62  	require.NotEqual(testLog, receipt2.logs[0])
    63  	h := receipt.Hash()
    64  
    65  	testLog.NotFixTopicCopyBug = false
    66  	typeReceipt = receipt.ConvertToReceiptPb()
    67  	require.NotNil(typeReceipt)
    68  	receipt2 = &Receipt{}
    69  	receipt2.ConvertFromReceiptPb(typeReceipt)
    70  	require.Equal(receipt, receipt2)
    71  	require.NotEqual(h, receipt.Hash())
    72  }
    73  
    74  func TestSerDer(t *testing.T) {
    75  	require := require.New(t)
    76  	receipt := &Receipt{
    77  		Status:      1,
    78  		BlockHeight: 1,
    79  		ActionHash:  hash.ZeroHash256,
    80  		GasConsumed: 1,
    81  	}
    82  	ser, err := receipt.Serialize()
    83  	require.NoError(err)
    84  
    85  	receipt2 := &Receipt{}
    86  	receipt2.Deserialize(ser)
    87  	require.Equal(receipt.Status, receipt2.Status)
    88  	require.Equal(receipt.BlockHeight, receipt2.BlockHeight)
    89  	require.Equal(receipt.ActionHash, receipt2.ActionHash)
    90  	require.Equal(receipt.GasConsumed, receipt2.GasConsumed)
    91  	require.Equal(receipt.ContractAddress, receipt2.ContractAddress)
    92  
    93  	hash := receipt.Hash()
    94  	oldHash := "9b1d77d8b8902e8d4e662e7cd07d8a74179e032f030d92441ca7fba1ca68e0f4"
    95  	require.Equal(oldHash, hex.EncodeToString(hash[:]))
    96  
    97  	// starting HawaiiHeight execution revert message is added to receipt
    98  	receipt = receipt.SetExecutionRevertMsg("test")
    99  	hash2 := receipt.Hash()
   100  	require.NotEqual(oldHash, hex.EncodeToString(hash2[:]))
   101  }
   102  
   103  func TestUpdateIndex(t *testing.T) {
   104  	require := require.New(t)
   105  	receipt := &Receipt{
   106  		Status:          1,
   107  		BlockHeight:     1,
   108  		ActionHash:      hash.ZeroHash256,
   109  		GasConsumed:     1,
   110  		ContractAddress: "test",
   111  		TxIndex:         1,
   112  		logs:            []*Log{newTestLog(), newTestLog(), newTestLog(), newTestLog()},
   113  	}
   114  
   115  	for _, v := range []struct {
   116  		txIndex, logIndex uint32
   117  	}{
   118  		{3, 6},
   119  		{4, 0},
   120  		{0, 0},
   121  		{0, 2},
   122  	} {
   123  		log := receipt.UpdateIndex(v.txIndex, v.logIndex)
   124  		require.Equal(v.txIndex, receipt.TxIndex)
   125  		require.Equal(v.logIndex+uint32(len(receipt.logs)), log)
   126  		// verify each log's index
   127  		for i, l := range receipt.logs {
   128  			require.Equal(v.logIndex+uint32(i), l.Index)
   129  		}
   130  	}
   131  }
   132  
   133  func TestConvertLog(t *testing.T) {
   134  	require := require.New(t)
   135  
   136  	topics := []hash.Hash256{
   137  		hash.ZeroHash256,
   138  		hash.Hash256b([]byte("Pacific")),
   139  		hash.Hash256b([]byte("Aleutian")),
   140  	}
   141  	testLog := newTestLog()
   142  	testLog.Topics = topics
   143  	testLog.NotFixTopicCopyBug = true
   144  	typeLog := testLog.ConvertToLogPb()
   145  	require.NotNil(typeLog)
   146  	log2 := &Log{}
   147  	log2.ConvertFromLogPb(typeLog)
   148  	require.Equal(testLog.Address, log2.Address)
   149  	require.Equal(testLog.Data, log2.Data)
   150  	require.Equal(testLog.BlockHeight, log2.BlockHeight)
   151  	require.Equal(testLog.ActionHash, log2.ActionHash)
   152  	require.Equal(testLog.Index, log2.Index)
   153  	// block earlier than AleutianHeight overwrites all topics with last topic data
   154  	last := len(log2.Topics) - 1
   155  	for _, v := range log2.Topics[:last] {
   156  		require.Equal(topics[last], v)
   157  	}
   158  
   159  	testLog.NotFixTopicCopyBug = false
   160  	typeLog = testLog.ConvertToLogPb()
   161  	require.NotNil(typeLog)
   162  	log2 = &Log{}
   163  	log2.ConvertFromLogPb(typeLog)
   164  	require.Equal(testLog, log2)
   165  }
   166  func TestSerDerLog(t *testing.T) {
   167  	require := require.New(t)
   168  
   169  	topics := []hash.Hash256{
   170  		hash.ZeroHash256,
   171  		hash.Hash256b([]byte("Pacific")),
   172  		hash.Hash256b([]byte("Aleutian")),
   173  	}
   174  	testLog := newTestLog()
   175  	testLog.Topics = topics
   176  	testLog.NotFixTopicCopyBug = true
   177  	typeLog, err := testLog.Serialize()
   178  	require.NoError(err)
   179  	log2 := &Log{}
   180  	log2.Deserialize(typeLog)
   181  	require.Equal(testLog.Address, log2.Address)
   182  	require.Equal(testLog.Data, log2.Data)
   183  	require.Equal(testLog.BlockHeight, log2.BlockHeight)
   184  	require.Equal(testLog.ActionHash, log2.ActionHash)
   185  	require.Equal(testLog.Index, log2.Index)
   186  	// block earlier than AleutianHeight overwrites all topics with last topic data
   187  	last := len(log2.Topics) - 1
   188  	for _, v := range log2.Topics[:last] {
   189  		require.Equal(topics[last], v)
   190  	}
   191  
   192  	testLog.NotFixTopicCopyBug = false
   193  	typeLog, err = testLog.Serialize()
   194  	require.NoError(err)
   195  	log2 = &Log{}
   196  	log2.Deserialize(typeLog)
   197  	require.Equal(testLog, log2)
   198  }