github.com/iotexproject/iotex-core@v1.14.1-rc1/blockchain/block/body_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 block 7 8 import ( 9 "math/big" 10 "testing" 11 12 "github.com/iotexproject/go-pkgs/hash" 13 "github.com/stretchr/testify/require" 14 15 "github.com/iotexproject/iotex-core/action" 16 "github.com/iotexproject/iotex-core/test/identityset" 17 ) 18 19 func TestProto(t *testing.T) { 20 require := require.New(t) 21 body := Body{} 22 blockBody := body.Proto() 23 require.NotNil(blockBody) 24 require.Equal(0, len(blockBody.Actions)) 25 26 body, err := makeBody() 27 require.NoError(err) 28 blockBody = body.Proto() 29 require.NotNil(blockBody) 30 require.Equal(1, len(blockBody.Actions)) 31 } 32 33 func TestSerDer(t *testing.T) { 34 require := require.New(t) 35 body := Body{} 36 ser, err := body.Serialize() 37 require.NoError(err) 38 body2, err := (&Deserializer{}).DeserializeBody(ser) 39 require.NoError(err) 40 require.Equal(0, len(body2.Actions)) 41 42 body, err = makeBody() 43 require.NoError(err) 44 ser, err = body.Serialize() 45 require.NoError(err) 46 body2, err = (&Deserializer{}).DeserializeBody(ser) 47 require.NoError(err) 48 require.Equal(1, len(body2.Actions)) 49 require.Equal(&body, body2) 50 } 51 52 func TestLoadProto(t *testing.T) { 53 require := require.New(t) 54 body := Body{} 55 blockBody := body.Proto() 56 require.NotNil(blockBody) 57 body2, err := (&Deserializer{}).fromBodyProto(blockBody) 58 require.NoError(err) 59 require.Equal(0, len(body2.Actions)) 60 61 body, err = makeBody() 62 require.NoError(err) 63 blockBody = body.Proto() 64 require.NotNil(blockBody) 65 body2, err = (&Deserializer{}).fromBodyProto(blockBody) 66 require.NoError(err) 67 require.Equal(1, len(body2.Actions)) 68 require.Equal(body, body2) 69 } 70 71 func TestCalculateTxRoot(t *testing.T) { 72 require := require.New(t) 73 body := Body{} 74 h, err := body.CalculateTxRoot() 75 require.NoError(err) 76 require.Equal(h, hash.ZeroHash256) 77 78 body, err = makeBody() 79 require.NoError(err) 80 h, err = body.CalculateTxRoot() 81 require.NoError(err) 82 require.NotEqual(h, hash.ZeroHash256) 83 } 84 85 func TestCalculateTransferAmount(t *testing.T) { 86 require := require.New(t) 87 body := Body{} 88 i := body.CalculateTransferAmount() 89 require.Equal(i, big.NewInt(0)) 90 91 body, err := makeBody() 92 require.NoError(err) 93 i = body.CalculateTransferAmount() 94 require.NotEqual(i, big.NewInt(0)) 95 require.Equal(i, big.NewInt(20)) 96 } 97 98 func makeBody() (body Body, err error) { 99 A := make([]*action.SealedEnvelope, 0) 100 v, err := action.NewExecution("", 0, big.NewInt(10), uint64(10), big.NewInt(10), []byte("data")) 101 if err != nil { 102 return 103 } 104 t, err := action.NewTransfer(0, big.NewInt(20), "", []byte("payload"), uint64(20), big.NewInt(20)) 105 if err != nil { 106 return 107 } 108 bd := &action.EnvelopeBuilder{} 109 elp := bd.SetGasPrice(big.NewInt(10)). 110 SetGasLimit(uint64(100000)). 111 SetAction(v). 112 SetAction(t).Build() 113 114 selp, err := action.Sign(elp, identityset.PrivateKey(28)) 115 if err != nil { 116 return 117 } 118 A = append(A, selp) 119 body = Body{A} 120 return 121 }