github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/staking/receipt_log_test.go (about) 1 // Copyright (c) 2020 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 staking 7 8 import ( 9 "context" 10 "testing" 11 12 "github.com/iotexproject/go-pkgs/hash" 13 "github.com/iotexproject/iotex-address/address" 14 "github.com/stretchr/testify/require" 15 16 "github.com/iotexproject/iotex-core/action" 17 "github.com/iotexproject/iotex-core/action/protocol" 18 "github.com/iotexproject/iotex-core/pkg/util/byteutil" 19 "github.com/iotexproject/iotex-core/test/identityset" 20 ) 21 22 func TestReceiptLog(t *testing.T) { 23 r := require.New(t) 24 25 h := hash.Hash160b([]byte(_protocolID)) 26 addr, _ := address.FromBytes(h[:]) 27 cand := identityset.Address(5) 28 voter := identityset.Address(11) 29 index := byteutil.Uint64ToBytesBigEndian(1) 30 b1 := cand.Bytes() 31 b2 := voter.Bytes() 32 33 ctx := protocol.WithActionCtx(context.Background(), protocol.ActionCtx{ 34 ActionHash: hash.Hash256b([]byte("test-action")), 35 }) 36 ctx = protocol.WithBlockCtx(ctx, protocol.BlockCtx{ 37 BlockHeight: 6, 38 }) 39 40 logTests := []struct { 41 addr string 42 name string 43 topics [][]byte 44 cand address.Address 45 voter address.Address 46 data []byte 47 }{ 48 { 49 addr.String(), 50 HandleCreateStake, 51 [][]byte{index, b1}, 52 cand, 53 voter, 54 index, 55 }, 56 { 57 addr.String(), 58 HandleUnstake, 59 [][]byte{index, b1}, 60 nil, 61 voter, 62 nil, 63 }, 64 { 65 addr.String(), 66 HandleWithdrawStake, 67 [][]byte{index, b1}, 68 nil, 69 voter, 70 nil, 71 }, 72 { 73 addr.String(), 74 HandleChangeCandidate, 75 [][]byte{index, b1, b2}, 76 cand, 77 voter, 78 nil, 79 }, 80 { 81 addr.String(), 82 HandleTransferStake, 83 [][]byte{index, b2, b1}, 84 nil, 85 voter, 86 nil, 87 }, 88 { 89 addr.String(), 90 HandleDepositToStake, 91 [][]byte{index, b2, b1}, 92 nil, 93 voter, 94 nil, 95 }, 96 { 97 addr.String(), 98 HandleRestake, 99 [][]byte{index, b1}, 100 nil, 101 voter, 102 nil, 103 }, 104 { 105 addr.String(), 106 HandleCandidateRegister, 107 [][]byte{index, b1}, 108 cand, 109 voter, 110 index, 111 }, 112 { 113 addr.String(), 114 HandleCandidateUpdate, 115 [][]byte{index, b1}, 116 nil, 117 voter, 118 nil, 119 }, 120 } 121 122 postFb := &action.Log{ 123 Address: addr.String(), 124 BlockHeight: 6, 125 ActionHash: hash.Hash256b([]byte("test-action")), 126 } 127 128 for _, v := range logTests { 129 log := newReceiptLog(v.addr, v.name, false) 130 log.AddTopics(v.topics...) 131 log.AddAddress(v.cand) 132 log.AddAddress(v.voter) 133 log.SetData(v.data) 134 r.Nil(log.Build(ctx, action.ErrInvalidAmount)) 135 r.Equal(createLog(ctx, v.name, v.cand, v.voter, v.data), log.Build(ctx, nil)) 136 137 log = newReceiptLog(v.addr, v.name, true) 138 log.AddTopics(v.topics...) 139 log.AddAddress(v.cand) 140 log.AddAddress(v.voter) 141 log.SetData(v.data) 142 postFb.Topics = action.Topics{hash.BytesToHash256([]byte(v.name))} 143 for i := range v.topics { 144 postFb.Topics = append(postFb.Topics, hash.BytesToHash256(v.topics[i])) 145 } 146 r.Equal(postFb, log.Build(ctx, action.ErrInvalidAmount)) 147 r.Equal(postFb, log.Build(ctx, nil)) 148 } 149 } 150 151 // only used for verify preFairbank receipt generation 152 func createLog( 153 ctx context.Context, 154 handlerName string, 155 candidateAddr, 156 voterAddr address.Address, 157 data []byte, 158 ) *action.Log { 159 actionCtx := protocol.MustGetActionCtx(ctx) 160 blkCtx := protocol.MustGetBlockCtx(ctx) 161 162 topics := []hash.Hash256{hash.Hash256b([]byte(handlerName))} 163 if candidateAddr != nil { 164 topics = append(topics, hash.Hash256b(candidateAddr.Bytes())) 165 } 166 topics = append(topics, hash.Hash256b(voterAddr.Bytes())) 167 168 h := hash.Hash160b([]byte(_protocolID)) 169 addr, _ := address.FromBytes(h[:]) 170 return &action.Log{ 171 Address: addr.String(), 172 Topics: topics, 173 Data: data, 174 BlockHeight: blkCtx.BlockHeight, 175 ActionHash: actionCtx.ActionHash, 176 } 177 }