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

     1  // Copyright (c) 2022 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/binary"
    10  	"encoding/hex"
    11  	"fmt"
    12  	"math/big"
    13  	"testing"
    14  
    15  	"github.com/iotexproject/go-pkgs/hash"
    16  	"github.com/iotexproject/iotex-proto/golang/iotextypes"
    17  	"github.com/stretchr/testify/require"
    18  	"google.golang.org/protobuf/proto"
    19  
    20  	"github.com/iotexproject/iotex-core/test/identityset"
    21  	"github.com/iotexproject/iotex-core/testutil"
    22  )
    23  
    24  func TestActionDeserializer(t *testing.T) {
    25  	r := require.New(t)
    26  	for _, v := range []struct {
    27  		id   uint32
    28  		hash string
    29  	}{
    30  		{0, "322884fb04663019be6fb461d9453827487eafdd57b4de3bd89a7d77c9bf8395"},
    31  		{1, "80af7840d73772d3022d8bdc46278fb755352e5e9d5f2a1f12ee7ec4f1ea98e9"},
    32  	} {
    33  		se, err := createSealedEnvelope(v.id)
    34  		r.NoError(err)
    35  		rHash, err := se.Hash()
    36  		r.NoError(err)
    37  		r.Equal(v.hash, hex.EncodeToString(rHash[:]))
    38  		r.Equal(v.id, se.ChainID())
    39  		r.Equal(_publicKey, se.SrcPubkey().HexString())
    40  		r.Equal(_signByte, se.Signature())
    41  		r.Zero(se.Encoding())
    42  
    43  		// use valid signature and reset se.Hash
    44  		se.signature = _validSig
    45  		se.hash = hash.ZeroHash256
    46  		se.Hash()
    47  		se1, err := (&Deserializer{}).ActionToSealedEnvelope(se.Proto())
    48  		se1.Hash()
    49  		r.NoError(err)
    50  		r.Equal(se, se1)
    51  	}
    52  }
    53  
    54  func TestProtoWithChainID(t *testing.T) {
    55  	r := require.New(t)
    56  	txID0, _ := hex.DecodeString("0a10080118a08d062202313062040a023130124104dc4c548c3a478278a6a09ffa8b5c4b384368e49654b35a6961ee8288fc889cdc39e9f8194e41abdbfac248ef9dc3f37b131a36ee2c052d974c21c1d2cd56730b1a4161e219c2c5d5987f8a9efa33e8df0cde9d5541689fff05784cdc24f12e9d9ee8283a5aa720f494b949535b7969c07633dfb68c4ef9359eb16edb9abc6ebfadc801")
    57  	txID1, _ := hex.DecodeString("0a12080118a08d0622023130280162040a023130124104dc4c548c3a478278a6a09ffa8b5c4b384368e49654b35a6961ee8288fc889cdc39e9f8194e41abdbfac248ef9dc3f37b131a36ee2c052d974c21c1d2cd56730b1a41328c6912fa0e36414c38089c03e2fa8c88bba82ccc4ce5fb8ac4ef9f529dfce249a5b2f93a45b818e7f468a742b4e87be3b8077f95d1b3c49e9165b971848ead01")
    58  	var ad Deserializer
    59  	for _, v := range []struct {
    60  		data    []byte
    61  		chainID uint32
    62  		err     error
    63  	}{
    64  		{txID0, 0, nil},
    65  		{txID1, 1, nil},
    66  	} {
    67  		tx := iotextypes.Action{}
    68  		r.NoError(proto.Unmarshal(v.data, &tx))
    69  		selp, err := ad.ActionToSealedEnvelope(&tx)
    70  		r.NoError(err)
    71  		r.Equal(v.chainID, selp.Envelope.ChainID())
    72  		_, err = selp.Hash()
    73  		r.NoError(err)
    74  		r.Equal(v.err, selp.VerifySignature())
    75  	}
    76  }
    77  
    78  func TestDataContanetation(t *testing.T) {
    79  	require := require.New(t)
    80  
    81  	// raw data
    82  	tx1, _ := SignedTransfer(identityset.Address(28).String(),
    83  		identityset.PrivateKey(28), 3, big.NewInt(10), []byte{}, testutil.TestGasLimit,
    84  		big.NewInt(testutil.TestGasPriceInt64))
    85  	txHash1, _ := tx1.Hash()
    86  	serilizedTx1, err := proto.Marshal(tx1.Proto())
    87  	require.NoError(err)
    88  	tx2, _ := SignedExecution(identityset.Address(29).String(),
    89  		identityset.PrivateKey(29), 1, big.NewInt(0), testutil.TestGasLimit,
    90  		big.NewInt(testutil.TestGasPriceInt64), []byte{})
    91  	txHash2, _ := tx2.Hash()
    92  	serilizedTx2, err := proto.Marshal(tx2.Proto())
    93  	require.NoError(err)
    94  
    95  	t.Run("tx1", func(t *testing.T) {
    96  		act1 := &iotextypes.Action{}
    97  		err := proto.Unmarshal(serilizedTx1, act1)
    98  		require.NoError(err)
    99  		se1, err := (&Deserializer{}).ActionToSealedEnvelope(act1)
   100  		require.NoError(err)
   101  		actHash1, err := se1.Hash()
   102  		require.NoError(err)
   103  		require.Equal(txHash1, actHash1)
   104  
   105  		act2 := &iotextypes.Action{}
   106  		err = proto.Unmarshal(serilizedTx2, act2)
   107  		require.NoError(err)
   108  	})
   109  
   110  	t.Run("tx2", func(t *testing.T) {
   111  		act2 := &iotextypes.Action{}
   112  		err = proto.Unmarshal(serilizedTx2, act2)
   113  		require.NoError(err)
   114  		se2, err := (&Deserializer{}).ActionToSealedEnvelope(act2)
   115  		require.NoError(err)
   116  		actHash2, err := se2.Hash()
   117  		require.NoError(err)
   118  		require.Equal(txHash2, actHash2)
   119  	})
   120  
   121  	t.Run("tx1+tx2", func(t *testing.T) {
   122  		acts := &iotextypes.Actions{}
   123  		err = proto.Unmarshal(append(serilizedTx1, serilizedTx2...), acts)
   124  		require.NoError(err)
   125  		require.Equal(2, len(acts.Actions))
   126  		_, err := (&Deserializer{}).ActionToSealedEnvelope(acts.Actions[0])
   127  		require.Error(err)
   128  	})
   129  
   130  	rawActs1 := &iotextypes.Actions{
   131  		Actions: []*iotextypes.Action{tx1.Proto()},
   132  	}
   133  	serilizedActs1, err := proto.Marshal(rawActs1)
   134  	require.NoError(err)
   135  	rawActs2 := &iotextypes.Actions{
   136  		Actions: []*iotextypes.Action{tx2.Proto()},
   137  	}
   138  	serilizedActs2, err := proto.Marshal(rawActs2)
   139  	require.NoError(err)
   140  
   141  	t.Run("tx1", func(t *testing.T) {
   142  		acts := &iotextypes.Actions{}
   143  		err = proto.Unmarshal(serilizedActs1, acts)
   144  		require.NoError(err)
   145  		require.Equal(1, len(acts.Actions))
   146  		se, err := (&Deserializer{}).ActionToSealedEnvelope(acts.Actions[0])
   147  		require.NoError(err)
   148  		actHash, err := se.Hash()
   149  		require.NoError(err)
   150  		require.Equal(txHash1, actHash)
   151  	})
   152  
   153  	t.Run("tx2", func(t *testing.T) {
   154  		acts := &iotextypes.Actions{}
   155  		err = proto.Unmarshal(serilizedActs2, acts)
   156  		require.NoError(err)
   157  		require.Equal(1, len(acts.Actions))
   158  		se, err := (&Deserializer{}).ActionToSealedEnvelope(acts.Actions[0])
   159  		require.NoError(err)
   160  		actHash, err := se.Hash()
   161  		require.NoError(err)
   162  		require.Equal(txHash2, actHash)
   163  	})
   164  
   165  	t.Run("tx1+tx2+tx1", func(t *testing.T) {
   166  		acts := &iotextypes.Actions{}
   167  		data := append(serilizedActs1, serilizedActs2...)
   168  		data = append(data, serilizedActs1...)
   169  		fmt.Println(binary.Size(data))
   170  		err = proto.Unmarshal(data, acts)
   171  		require.NoError(err)
   172  		require.Equal(3, len(acts.Actions))
   173  		se, err := (&Deserializer{}).ActionToSealedEnvelope(acts.Actions[0])
   174  		require.NoError(err)
   175  		actHash, err := se.Hash()
   176  		require.NoError(err)
   177  		require.Equal(txHash1, actHash)
   178  
   179  		se, err = (&Deserializer{}).ActionToSealedEnvelope(acts.Actions[1])
   180  		require.NoError(err)
   181  		actHash, err = se.Hash()
   182  		require.NoError(err)
   183  		require.Equal(txHash2, actHash)
   184  
   185  		se, err = (&Deserializer{}).ActionToSealedEnvelope(acts.Actions[2])
   186  		require.NoError(err)
   187  		actHash, err = se.Hash()
   188  		require.NoError(err)
   189  		require.Equal(txHash1, actHash)
   190  	})
   191  }